Subversion Repositories Kolibri OS

Rev

Rev 425 | Blame | Last modification | View Log | Download | RSS feed

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