Subversion Repositories Kolibri OS

Rev

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

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;                                                                 ;;
  3. ;; Copyright (C) KolibriOS team 2004-2009. All rights reserved.    ;;
  4. ;; Distributed under terms of the GNU General Public License       ;;
  5. ;;                                                                 ;;
  6. ;;  UDP.INC                                                        ;;
  7. ;;                                                                 ;;
  8. ;;  Part of the tcp/ip network stack for KolibriOS                 ;;
  9. ;;                                                                 ;;
  10. ;;    Written by hidnplayr@kolibrios.org                           ;;
  11. ;;                                                                 ;;
  12. ;;          GNU GENERAL PUBLIC LICENSE                             ;;
  13. ;;             Version 2, June 1991                                ;;
  14. ;;                                                                 ;;
  15. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  16.  
  17. $Revision: 1206 $
  18.  
  19.  
  20. struct  UDP_Packet
  21.         .SourcePort             dw  ?
  22.         .DestinationPort        dw  ?
  23.         .Length                 dw  ?  ; Length of (UDP Header + Data)
  24.         .Checksum               dw  ?
  25.         .Data:
  26.  
  27. ends
  28.  
  29.  
  30. align 4
  31. uglobal
  32.         UDP_PACKETS_TX          rd  MAX_IP
  33.         UDP_PACKETS_RX          rd  MAX_IP
  34. endg
  35.  
  36.  
  37. ;-----------------------------------------------------------------
  38. ;
  39. ; UDP_init
  40. ;
  41. ;  This function resets all UDP variables
  42. ;
  43. ;  IN:  /
  44. ;  OUT: /
  45. ;
  46. ;-----------------------------------------------------------------
  47. align 4
  48. UDP_init:
  49.  
  50.         xor     eax, eax
  51.         mov     edi, UDP_PACKETS_TX
  52.         mov     ecx, 2*MAX_IP
  53.         rep     stosd
  54.  
  55.         ret
  56.  
  57.  
  58.  
  59. ;-----------------------------------------------------------------
  60. ;
  61. ; UDP_Handler:
  62. ;
  63. ;  Called by IPv4_handler,
  64. ;  this procedure will inject the udp data diagrams in the application sockets.
  65. ;
  66. ;  IN:  Pointer to buffer in [esp]
  67. ;       size of buffer in [esp+4]
  68. ;       pointer to device struct in ebx
  69. ;       UDP Packet size in ecx
  70. ;       pointer to UDP Packet data in edx
  71. ;  OUT: /
  72. ;
  73. ;-----------------------------------------------------------------
  74. align 4
  75. UDP_handler:
  76.  
  77.         DEBUGF 1,"UDP_Handler\n"
  78.         ; TODO: First validate the header & checksum!
  79.  
  80.         ; Look for a socket where
  81.         ; IP Packet UDP Destination Port = local Port
  82.         ; IP Packet SA = Remote IP
  83.  
  84.         mov     eax, net_sockets
  85.   .try_more:
  86.         mov     bx , [edx + UDP_Packet.DestinationPort]   ; get the local port from the IP Packet's UDP header
  87.   .next_socket:
  88.         mov     eax, [eax + SOCKET.NextPtr]
  89.         or      eax, eax
  90.         jz      .dump
  91.         cmp     [eax + SOCKET.Domain], AF_INET4
  92.         jne     .next_socket
  93.         cmp     [eax + SOCKET.Type], IP_PROTO_UDP
  94.         jne     .next_socket
  95.         cmp     [eax + SOCKET.LocalPort], bx
  96.         jne     .next_socket
  97.  
  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.         cmp     [eax + SOCKET.RemoteIP], 0xffffffff
  103.         je      .ok1
  104.  
  105.         mov     ebx, [esp]
  106.         mov     ebx, [ebx + ETH_FRAME.Data + IPv4_Packet.SourceAddress] ; get the Source address from the IP Packet
  107.         cmp     [eax + SOCKET.RemoteIP], eax
  108.         jne     .try_more                                             ; Quit if the source IP is not valid, check for more sockets with this IP/PORT combination
  109.  
  110.   .ok1:
  111.  
  112.         mov     bx, [edx + UDP_Packet.SourcePort]                     ; Remote port must be 0, or equal to sourceport of packet
  113.  
  114.         cmp     [eax + SOCKET.RemotePort],0
  115.         je      .ok2
  116.  
  117.         cmp     [eax + SOCKET.RemotePort], bx
  118.         jne     .dump
  119.  
  120.   .ok2:
  121.  
  122.         DEBUGF 1,"Found valid UDP packet for socket %x\n", eax
  123.         lea     esi, [edx + UDP_Packet.Data]
  124.         movzx   ecx, [edx + UDP_Packet.Length]
  125.         rol     cx , 8
  126.         sub     cx , UDP_Packet.Data
  127.         mov     dx , bx
  128.  
  129.         call    socket_internal_receiver
  130.  
  131.         inc     [UDP_PACKETS_RX]
  132.  
  133.   .dump:
  134.         DEBUGF 1,"Dumping UDP packet\n"
  135.         call    kernel_free
  136.         add     esp, 4 ; pop (balance stack)
  137.  
  138.         ret
  139.  
  140.  
  141.  
  142.  
  143. ;-----------------------------------------------------------------
  144. ;
  145. ; Note: UDP works only on top of IP protocol :)
  146. ;
  147. ; IN: eax = dest ip
  148. ;     ebx = source ip
  149. ;     ecx = data length
  150. ;     edx = remote port shl 16 + local port (both in INET order)
  151. ;     esi = data offset
  152. ;
  153. ;-----------------------------------------------------------------
  154.  
  155. UDP_create_packet:
  156.  
  157.         DEBUGF 1,"Create UDP Packet (size=%u)\n",ecx
  158.  
  159.         push    edx esi
  160.  
  161.         add     ecx, UDP_Packet.Data
  162.         mov     di , IP_PROTO_UDP
  163.  
  164. ;       dx  = fragment id
  165.  
  166.         call    IPv4_create_packet                              ; TODO: figure out a way to choose between IPv4 and IPv6
  167.         cmp     edi, -1
  168.         je      .fail
  169.  
  170.         mov     byte[edi + UDP_Packet.Length], ch
  171.         mov     byte[edi + UDP_Packet.Length+1], cl
  172.         sub     ecx , UDP_Packet.Data
  173.  
  174.         pop     esi
  175.         push    edi
  176.         add     edi, UDP_Packet.Data
  177.         push    cx
  178.         shr     ecx, 2
  179.         rep     movsd
  180.         pop     cx
  181.         and     cx , 3
  182.         rep     movsb
  183.         pop     edi
  184.  
  185.         pop     ecx
  186.         mov     dword [edi + UDP_Packet.SourcePort], ecx        ; notice: we write both port's at once
  187.  
  188.         mov     [edi + UDP_Packet.Checksum], 0
  189.  
  190.         ; TODO: calculate checksum using Pseudo-header  (However, using a 0 as checksum shouldnt generate any errors :)
  191.  
  192.         inc     [UDP_PACKETS_TX]
  193.  
  194.         push    edx eax                      ; TODO: make this work on other protocols besides ethernet
  195.         DEBUGF 1,"Sending UDP Packet to device %x\n", ebx      ;
  196.         jmp     ETH_Sender                   ;
  197.  
  198.   .exit:
  199.         ret
  200.  
  201.   .fail:
  202.         ; todo: queue the packet
  203.         add     esp, 8
  204.         ret
  205.  
  206.  
  207.  
  208. ;---------------------------------------------------------------------------
  209. ;
  210. ; UDP_API
  211. ;
  212. ; This function is called by system function 75
  213. ;
  214. ; IN:  subfunction number in bl
  215. ;      device number in bh
  216. ;      ecx, edx, .. depends on subfunction
  217. ;
  218. ; OUT:
  219. ;
  220. ;---------------------------------------------------------------------------
  221.  
  222. align 4
  223. UDP_API:
  224.  
  225.         movzx   eax, bh
  226.         shl     eax, 2
  227.  
  228.         test    bl, bl
  229.         jz      .packets_tx     ; 0
  230.         dec     bl
  231.         jz      .packets_rx     ; 1
  232.  
  233. .error:
  234.         mov     eax, -1
  235.         ret
  236.  
  237. .packets_tx:
  238.         add     eax, UDP_PACKETS_TX
  239.         mov     eax, [eax]
  240.         ret
  241.  
  242. .packets_rx:
  243.         add     eax, UDP_PACKETS_RX
  244.         mov     eax, [eax]
  245.         ret
  246.