Subversion Repositories Kolibri OS

Rev

Rev 837 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;                                                              ;;
  3. ;; Copyright (C) KolibriOS team 2004-2007. All rights reserved. ;;
  4. ;; Distributed under terms of the GNU General Public License    ;;
  5. ;;                                                              ;;
  6. ;;                                                              ;;
  7. ;; Copyright (C) KolibriOS team 2004-2007. All rights reserved. ;;
  8. ;; Distributed under terms of the GNU General Public License    ;;
  9. ;;                                                              ;;
  10. ;;                                                              ;;
  11. ;;  UDP.INC                                                     ;;
  12. ;;                                                              ;;
  13. ;;  UDP Processes for Menuet OS  TCP/IP stack                   ;;
  14. ;;                                                              ;;
  15. ;;  Version 0.3  29 August 2002                                 ;;
  16. ;;                                                              ;;
  17. ;;  Copyright 2002 Mike Hibbett, mikeh@oceanfree.net            ;;
  18. ;;                                                              ;;
  19. ;;  See file COPYING for details                                ;;
  20. ;;                                                              ;;
  21. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  22.  
  23. $Revision: 914 $
  24.  
  25.  
  26. ;*******************************************************************
  27. ;   Interface
  28. ;
  29. ;       udp_rx      Handles received IP packets with the UDP protocol
  30. ;
  31. ;*******************************************************************
  32.  
  33.  
  34. ;
  35. ;   UDP Payload ( Data field in IP datagram )
  36. ;
  37. ;    0                   1                   2                   3
  38. ;    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  39. ;
  40. ;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  41. ;   |       Source Port             |      Destination Port         |
  42. ;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  43. ;   | Length ( UDP Header + Data )  |           Checksum            |
  44. ;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  45. ;   |       UDP Data                                                |
  46. ;   +-+-+-..........                                               -+
  47. ;
  48.  
  49. struc UDP_PACKET
  50. {  .SourcePort       dw  ?  ;+00
  51.    .DestinationPort  dw  ?  ;+02
  52.    .Length           dw  ?  ;+04 - Length of (UDP Header + Data)
  53.    .Checksum         dw  ?  ;+06
  54.    .Data             db  ?  ;+08
  55. }
  56.  
  57. virtual at 0
  58.   UDP_PACKET UDP_PACKET
  59. end virtual
  60.  
  61.  
  62. ;***************************************************************************
  63. ;   Function
  64. ;      udp_rx  [by Johnny_B]
  65. ;
  66. ;   Description
  67. ;       UDP protocol handler
  68. ;       This is a kernel function, called by ip_rx
  69. ;       IP buffer address given in edx
  70. ;          IP buffer number in eax
  71. ;          Free up (or re-use) IP buffer when finished
  72. ;
  73. ;***************************************************************************
  74.  
  75. proc udp_rx stdcall
  76.     push    eax
  77.  
  78.     ; First validate the header & checksum. Discard buffer if error
  79.  
  80.     ; Look for a socket where
  81.     ; IP Packet UDP Destination Port = local Port
  82.     ; IP Packet SA = Remote IP
  83.  
  84.         mov     ax, [edx + 20 + UDP_PACKET.DestinationPort]   ; get the local port from
  85.                                   ; the IP packet's UDP header
  86.  
  87.         mov     ebx, net_sockets
  88.  
  89.   .next_socket:
  90.         mov     ebx, [ebx + SOCKET.NextPtr]
  91.         or      ebx, ebx
  92.         jz      .exit                                   ; No match, so exit
  93.         cmp     [ebx + SOCKET.LocalPort], ax            ; ax will hold the 'wrong' value,
  94.                                     ; but the comparision is correct
  95.         jne     .next_socket                            ; Return back if no match
  96.  
  97.     ; For dhcp, we must allow any remote server to respond.
  98.     ; I will accept the first incoming response to be the one
  99.     ; I bind to, if the socket is opened with a destination IP address of
  100.     ; 255.255.255.255
  101.         cmp     [ebx + SOCKET.RemoteIP], 0xffffffff
  102.         je      @f
  103.  
  104.         mov     eax, [edx + IP_PACKET.SourceAddress]    ; get the Source address from the IP packet
  105.         cmp     [ebx + SOCKET.RemoteIP], ebx
  106.         jne     .exit              ; Quit if the source IP is not valid
  107.  
  108.     @@: ; OK - we have a valid UDP packet for this socket.
  109.     ; First, update the sockets remote port number with the incoming msg
  110.     ; - it will have changed
  111.     ; from the original ( 69 normally ) to allow further connects
  112.         mov     ax, [edx + 20 + UDP_PACKET.SourcePort]          ; get the UDP source port
  113.                                      ; ( was 69, now new )
  114.         mov     [ebx + SOCKET.RemotePort], ax
  115.  
  116.     ; Now, copy data to socket. We have socket address as [eax + sockets].
  117.     ; We have IP packet in edx
  118.  
  119.     ; get # of bytes in ecx
  120.         movzx   ecx, [edx + IP_PACKET.TotalLength]      ; total length of IP packet. Subtract
  121.         xchg    cl, ch                                  ; 20 + 8 gives data length
  122.     sub     ecx, 28
  123.  
  124.         mov     eax, [ebx + SOCKET.rxDataCount]         ; get # of bytes already in buffer
  125.         add     [ebx + SOCKET.rxDataCount], ecx         ; increment the count of bytes in buffer
  126.  
  127.         ; ecx has count, edx points to data
  128.  
  129.     add     edx, 28        ; edx now points to the data
  130.         lea     edi, [ebx + eax + SOCKETHEADERSIZE]
  131.     mov     esi, edx
  132.  
  133.     cld
  134.     rep     movsb          ; copy the data across
  135.  
  136.     ; flag an event to the application
  137.         mov     eax, [ebx + SOCKET.PID]                 ; get socket owner PID
  138.         mov     ecx, 1
  139.         mov     esi, TASK_DATA + TASKDATA.pid
  140.  
  141.   .next_pid:
  142.         cmp     [esi], eax
  143.         je      .found_pid
  144.     inc     ecx
  145.         add     esi, 0x20
  146.         cmp     ecx, [TASK_COUNT]
  147.         jbe     .next_pid
  148.  
  149.         jmp     .exit
  150.  
  151.   .found_pid:
  152.         shl     ecx, 8
  153.         or      [ecx + SLOT_BASE + APPDATA.event_mask], EVENT_NETWORK ; stack event
  154.  
  155.         mov     [check_idle_semaphore], 200
  156.  
  157.   .exit:
  158.     pop     eax
  159.     call    freeBuff    ; Discard the packet
  160.     ret
  161. endp
  162.