Subversion Repositories Kolibri OS

Rev

Rev 431 | 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: 593 $
  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. udp_rx:
  75.     push    eax
  76.  
  77.     ; First validate the header & checksum. Discard buffer if error
  78.  
  79.     ; Look for a socket where
  80.     ; IP Packet UDP Destination Port = local Port
  81.     ; IP Packet SA = Remote IP
  82.  
  83.     movzx   ebx, word [edx + 22]   ; get the local port from
  84.                                   ; the IP packet's UDP header
  85.     mov     eax, SOCKETBUFFSIZE * NUM_SOCKETS
  86.     mov     ecx, NUM_SOCKETS
  87.  
  88. fs1:
  89.     sub     eax, SOCKETBUFFSIZE
  90.     cmp     [eax + sockets + 12], bx ; bx will hold the 'wrong' value,
  91.                                     ; but the comparision is correct
  92.     loopnz  fs1                     ; Return back if no match
  93.     jz      fs_done
  94.  
  95.     ; No match, so exit
  96.     jmp     udprx_001
  97.  
  98. fs_done:
  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.     mov     ebx, [eax + sockets + 16]
  104.     cmp     ebx, 0xffffffff
  105.     je      udprx_002
  106.  
  107.     mov     ebx, [edx + 12]    ; get the Source address from the IP packet
  108.     cmp     [eax + sockets + 16], ebx
  109.     jne     udprx_001          ; Quit if the source IP is not valid
  110.  
  111. udprx_002:
  112.     ; OK - we have a valid UDP packet for this socket.
  113.     ; First, update the sockets remote port number with the incoming msg
  114.     ; - it will have changed
  115.     ; from the original ( 69 normally ) to allow further connects
  116.     movzx   ebx, word [edx + 20]      ; get the UDP source port
  117.                                      ; ( was 69, now new )
  118.     mov     [eax + sockets + 20], bx
  119.  
  120.     ; Now, copy data to socket. We have socket address as [eax + sockets].
  121.     ; We have IP packet in edx
  122.  
  123.     ; get # of bytes in ecx
  124.     movzx   ecx, byte [edx + 3]  ; total length of IP packet. Subtract
  125.     mov     ch, byte [edx + 2]   ; 20 + 8 gives data length
  126.     sub     ecx, 28
  127.  
  128.     mov     ebx, eax
  129.     add     ebx, sockets         ; ebx = address of actual socket
  130.  
  131.     mov     eax, [ebx+ 4]       ; get socket owner PID
  132.     push    eax
  133.  
  134.     mov     eax, [ebx + 24]      ; get # of bytes already in buffer
  135.     add     [ebx + 24], ecx      ; increment the count of bytes in buffer
  136.  
  137.     ; point to the location to store the data
  138.     add     ebx, eax
  139.     add     ebx, SOCKETHEADERSIZE
  140.  
  141.     ; ebx = location for first byte, ecx has count,
  142.     ; edx points to data
  143.  
  144.     add     edx, 28        ; edx now points to the data
  145.     mov     edi, ebx
  146.     mov     esi, edx
  147.  
  148.     cld
  149.     rep     movsb          ; copy the data across
  150.  
  151.     ; flag an event to the application
  152.     pop     eax
  153.     mov     ecx,1
  154.     mov     esi,TASK_DATA+TASKDATA.pid
  155.  
  156. newsearch:
  157.     cmp     [esi],eax
  158.     je      foundPID
  159.     inc     ecx
  160.     add     esi,0x20
  161.     cmp     ecx,[TASK_COUNT]
  162.     jbe     newsearch
  163.  
  164. foundPID:
  165.     shl     ecx,8
  166.     or      dword [ecx+SLOT_BASE+APPDATA.event_mask],dword 10000000b ; stack event
  167.  
  168.     mov     [check_idle_semaphore],200
  169.  
  170. udprx_001:
  171.     pop     eax
  172.     call    freeBuff    ; Discard the packet
  173.     ret
  174.