Subversion Repositories Kolibri OS

Rev

Rev 380 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

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