Subversion Repositories Kolibri OS

Rev

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

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