Subversion Repositories Kolibri OS

Rev

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