Subversion Repositories Kolibri OS

Rev

Rev 593 | 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. ;;  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: 907 $
  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_mutex
  88.         call    wait_mutex
  89.         mov     ebx, net_sockets
  90.  
  91.   .next_socket:
  92.         mov     ebx, [ebx + SOCKET.NextPtr]
  93.         or      ebx, ebx
  94.         jz      .exit                                   ; No match, so exit
  95.         cmp     [ebx + SOCKET.LocalPort], ax            ; ax will hold the 'wrong' value,
  96.                                                         ; but the comparision is correct
  97.         jne     .next_socket                            ; Return back if no match
  98.  
  99.         ; For dhcp, we must allow any remote server to respond.
  100.         ; I will accept the first incoming response to be the one
  101.         ; I bind to, if the socket is opened with a destination IP address of
  102.         ; 255.255.255.255
  103.         cmp     [ebx + SOCKET.RemoteIP], 0xffffffff
  104.         je      @f
  105.  
  106.         mov     eax, [edx + IP_PACKET.SourceAddress]    ; get the Source address from the IP packet
  107.         cmp     [ebx + SOCKET.RemoteIP], ebx
  108.         jne     .exit              ; Quit if the source IP is not valid
  109.  
  110.     @@: ; OK - we have a valid UDP packet for this socket.
  111.         ; First, update the sockets remote port number with the incoming msg
  112.         ; - it will have changed
  113.         ; from the original ( 69 normally ) to allow further connects
  114.         mov     ax, [edx + 20 + UDP_PACKET.SourcePort]          ; get the UDP source port
  115.                                                                 ; ( was 69, now new )
  116.         mov     [ebx + SOCKET.RemotePort], ax
  117.  
  118.         ; Now, copy data to socket. We have socket address as [eax + sockets].
  119.         ; We have IP packet in edx
  120.  
  121.         ; get # of bytes in ecx
  122.         movzx   ecx, [edx + IP_PACKET.TotalLength]      ; total length of IP packet. Subtract
  123.         xchg    cl, ch                                  ; 20 + 8 gives data length
  124.         sub     ecx, 28
  125.  
  126.         mov     eax, [ebx + SOCKET.rxDataCount]         ; get # of bytes already in buffer
  127.         add     [ebx + SOCKET.rxDataCount], ecx         ; increment the count of bytes in buffer
  128.  
  129.         ; ecx has count, edx points to data
  130.  
  131.         add     edx, 28        ; edx now points to the data
  132.         lea     edi, [ebx + eax + SOCKETHEADERSIZE]
  133.         mov     esi, edx
  134.  
  135.         cld
  136.         rep     movsb          ; copy the data across
  137.  
  138.         ; flag an event to the application
  139.         mov     eax, [ebx + SOCKET.PID]                 ; get socket owner PID
  140.         mov     ecx, 1
  141.         mov     esi, TASK_DATA + TASKDATA.pid
  142.  
  143.   .next_pid:
  144.         cmp     [esi], eax
  145.         je      .found_pid
  146.         inc     ecx
  147.         add     esi, 0x20
  148.         cmp     ecx, [TASK_COUNT]
  149.         jbe     .next_pid
  150.  
  151.         jmp     .exit
  152.  
  153.   .found_pid:
  154.         shl     ecx, 8
  155.         or      [ecx + SLOT_BASE + APPDATA.event_mask], EVENT_NETWORK ; stack event
  156.  
  157.         mov     [check_idle_semaphore], 200
  158.  
  159.   .exit:
  160.         pop     eax
  161.         call    freeBuff    ; Discard the packet
  162.         ret
  163. endp
  164.