Subversion Repositories Kolibri OS

Rev

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

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;                                                                 ;;
  3. ;; Copyright (C) KolibriOS team 2004-2012. 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: 2980 $
  18.  
  19.  
  20. struct  UDP_header
  21.  
  22.         SourcePort              dw  ?
  23.         DestinationPort         dw  ?
  24.         Length                  dw  ?  ; Length of (UDP Header + Data)
  25.         Checksum                dw  ?
  26.  
  27. ends
  28.  
  29.  
  30. align 4
  31. uglobal
  32.         UDP_PACKETS_TX          rd  MAX_NET_DEVICES
  33.         UDP_PACKETS_RX          rd  MAX_NET_DEVICES
  34. endg
  35.  
  36.  
  37. ;-----------------------------------------------------------------
  38. ;
  39. ; UDP_init
  40. ;
  41. ;  This function resets all UDP variables
  42. ;
  43. ;-----------------------------------------------------------------
  44. macro   UDP_init {
  45.  
  46.         xor     eax, eax
  47.         mov     edi, UDP_PACKETS_TX
  48.         mov     ecx, 2*MAX_NET_DEVICES
  49.         rep     stosd
  50. }
  51.  
  52.  
  53. macro   UDP_checksum    IP1, IP2  { ; esi = ptr to udp packet, ecx = packet size, destroys: ecx, edx
  54.  
  55. ; Pseudoheader
  56.         mov     edx, IP_PROTO_UDP
  57.  
  58.         add     dl, [IP1+1]
  59.         adc     dh, [IP1+0]
  60.         adc     dl, [IP1+3]
  61.         adc     dh, [IP1+2]
  62.  
  63.         adc     dl, [IP2+1]
  64.         adc     dh, [IP2+0]
  65.         adc     dl, [IP2+3]
  66.         adc     dh, [IP2+2]
  67.  
  68.         adc     dl, cl ; byte[esi+UDP_header.Length+1]
  69.         adc     dh, ch ; byte[esi+UDP_header.Length+0]
  70.  
  71. ; Done with pseudoheader, now do real header
  72.         adc     dl, byte[esi+UDP_header.SourcePort+1]
  73.         adc     dh, byte[esi+UDP_header.SourcePort+0]
  74.  
  75.         adc     dl, byte[esi+UDP_header.DestinationPort+1]
  76.         adc     dh, byte[esi+UDP_header.DestinationPort+0]
  77.  
  78.         adc     dl, byte[esi+UDP_header.Length+1]
  79.         adc     dh, byte[esi+UDP_header.Length+0]
  80.  
  81.         adc     edx, 0
  82.  
  83. ; Done with header, now do data
  84.         push    esi
  85.         movzx   ecx, [esi+UDP_header.Length]
  86.         rol     cx , 8
  87.         sub     cx , sizeof.UDP_header
  88.         add     esi, sizeof.UDP_header
  89.  
  90.         call    checksum_1
  91.         call    checksum_2
  92.         pop     esi
  93.  
  94.         add     [esi+UDP_header.Checksum], dx   ; this final instruction will set or clear ZF :)
  95.  
  96. }
  97.  
  98.  
  99. ;-----------------------------------------------------------------
  100. ;
  101. ; UDP_input:
  102. ;
  103. ;  Called by IPv4_input,
  104. ;  this procedure will inject the udp data diagrams in the application sockets.
  105. ;
  106. ;  IN:   [esp]  = Pointer to buffer
  107. ;       [esp+4] = size of buffer
  108. ;       ebx = ptr to device struct
  109. ;       ecx = UDP Packet size
  110. ;       esi = ptr to UDP header
  111. ;       edi = ptr to ipv4 source and dest address
  112. ;
  113. ;  OUT: /
  114. ;
  115. ;-----------------------------------------------------------------
  116. align 4
  117. UDP_input:
  118.  
  119.         DEBUGF  1,"UDP_input: size=%u\n", ecx
  120.  
  121.         ; First validate, checksum
  122.  
  123.         neg     [esi + UDP_header.Checksum]     ; substract checksum from 0
  124.         jz      .no_checksum                    ; if checksum is zero, it is considered valid
  125.  
  126.         ; otherwise, we will re-calculate the checksum and add it to this value, thus creating 0 when it is correct
  127.  
  128.         UDP_checksum (edi), (edi+4)
  129.         jnz     .checksum_mismatch
  130.  
  131.   .no_checksum:
  132.         DEBUGF  1,"UDP_input: checksum ok\n"
  133.  
  134.         ; Convert port numbers to intel format
  135.  
  136.         rol     [esi + UDP_header.DestinationPort], 8
  137.         rol     [esi + UDP_header.SourcePort], 8
  138.         rol     [esi + UDP_header.Length], 8
  139.  
  140.         ; Look for a socket where
  141.         ; IP Packet UDP Destination Port = local Port
  142.         ; IP Packet SA = Remote IP
  143.  
  144.         mov     cx, [esi + UDP_header.SourcePort]
  145.         mov     dx, [esi + UDP_header.DestinationPort]
  146.         mov     edi, [edi + 4]                          ; ipv4 source address
  147.         mov     eax, net_sockets
  148.  
  149.   .next_socket:
  150.         mov     eax, [eax + SOCKET.NextPtr]
  151.         or      eax, eax
  152.         jz      .dump
  153.  
  154.         cmp     [eax + SOCKET.Domain], AF_INET4
  155.         jne     .next_socket
  156.  
  157.         cmp     [eax + SOCKET.Protocol], IP_PROTO_UDP
  158.         jne     .next_socket
  159.  
  160.         cmp     [eax + UDP_SOCKET.LocalPort], dx
  161.         jne     .next_socket
  162.  
  163.         DEBUGF  1,"UDP_input: socket=%x\n", eax
  164.  
  165.         ;;; TODO: when packet is processed, check more sockets!
  166.  
  167. ;        cmp     [eax + IP_SOCKET.RemoteIP], 0xffffffff
  168. ;        je      @f
  169. ;        cmp     [eax + IP_SOCKET.RemoteIP], edi
  170. ;        jne     .next_socket
  171. ;       @@:
  172. ;
  173. ; FIXME: UDP should check remote IP, but not under all circumstances!
  174.  
  175.         cmp     [eax + UDP_SOCKET.firstpacket], 0
  176.         je      .updateport
  177.  
  178.         cmp     [eax + UDP_SOCKET.RemotePort], cx
  179.         jne     .dump
  180.  
  181.         pusha
  182.         lea     ecx, [eax + SOCKET.mutex]
  183.         call    mutex_lock
  184.         popa
  185.  
  186.   .updatesock:
  187.         inc     [UDP_PACKETS_RX]        ; Fixme: correct interface?
  188.  
  189.         movzx   ecx, [esi + UDP_header.Length]
  190.         sub     ecx, sizeof.UDP_header
  191.         add     esi, sizeof.UDP_header
  192.  
  193.         jmp     SOCKET_input
  194.  
  195.   .updateport:
  196.         pusha
  197.         lea     ecx, [eax + SOCKET.mutex]
  198.         call    mutex_lock
  199.         popa
  200.  
  201.         DEBUGF  1,"UDP_input: new remote port=%u\n", cx
  202.         mov     [eax + UDP_SOCKET.RemotePort], cx
  203.         inc     [eax + UDP_SOCKET.firstpacket]
  204.  
  205.         jmp     .updatesock
  206.  
  207.  
  208.   .checksum_mismatch:
  209.         DEBUGF  2,"UDP_input: checksum mismatch\n"
  210.  
  211.   .dump:
  212.         call    kernel_free
  213.         add     esp, 4 ; pop (balance stack)
  214.         DEBUGF  2,"UDP_input: dumping\n"
  215.  
  216.         ret
  217.  
  218.  
  219.  
  220.  
  221. ;-----------------------------------------------------------------
  222. ;
  223. ; UDP_output
  224. ;
  225. ; IN: eax = socket pointer
  226. ;     ecx = number of bytes to send
  227. ;     esi = pointer to data
  228. ;
  229. ;-----------------------------------------------------------------
  230.  
  231. align 4
  232. UDP_output:
  233.  
  234.         DEBUGF  1,"UDP_output: socket=%x bytes=%u data_ptr=%x\n", eax, ecx, esi
  235.  
  236.         mov     dx, [eax + UDP_SOCKET.RemotePort]
  237.         DEBUGF  1,"UDP_output: remote port=%u, ", dx
  238.         rol     dx, 8
  239.         rol     edx, 16
  240.         mov     dx, [eax + UDP_SOCKET.LocalPort]
  241.         DEBUGF  1,"local port=%u\n", dx
  242.         rol     dx, 8
  243.  
  244.         sub     esp, 8                                          ; Data ptr and data size will be placed here
  245.         push    edx esi
  246.         mov     ebx, [eax + SOCKET.device]
  247.         mov     edx, [eax + IP_SOCKET.LocalIP]
  248.         mov     eax, [eax + IP_SOCKET.RemoteIP]
  249.         mov     di, IP_PROTO_UDP shl 8 + 128
  250.         add     ecx, sizeof.UDP_header
  251.         call    IPv4_output
  252.         jz      .fail
  253.         mov     [esp + 8], eax                                  ; pointer to buffer start
  254.         mov     [esp + 8 + 4], edx                              ; buffer size
  255.  
  256.         mov     [edi + UDP_header.Length], cx
  257.         rol     [edi + UDP_header.Length], 8
  258.  
  259.         pop     esi
  260.         push    edi ecx
  261.         sub     ecx, sizeof.UDP_header
  262.         add     edi, sizeof.UDP_header
  263.         shr     ecx, 2
  264.         rep     movsd
  265.         mov     ecx, [esp]
  266.         and     ecx, 3
  267.         rep     movsb
  268.         pop     ecx edi
  269.  
  270.         pop     dword [edi + UDP_header.SourcePort]
  271.  
  272. ; Checksum
  273.         mov     esi, edi
  274.         mov     [edi + UDP_header.Checksum], 0
  275.         UDP_checksum (edi-4), (edi-8)                           ; FIXME: IPv4 packet could have options..
  276.  
  277.         DEBUGF  1,"UDP_output: sending with device %x\n", ebx
  278.         call    [ebx + NET_DEVICE.transmit]
  279.         test    eax, eax
  280.         jnz     @f
  281.         inc     [UDP_PACKETS_TX]                                ; FIXME: correct device?
  282.        @@:
  283.  
  284.         ret
  285.  
  286.   .fail:
  287.         DEBUGF  1,"UDP_output: failed\n"
  288.         add     esp, 4+4+8
  289.         or      eax, -1
  290.         ret
  291.  
  292.  
  293.  
  294. ;---------------------------------------------------------------------------
  295. ;
  296. ; UDP_API
  297. ;
  298. ; This function is called by system function 75
  299. ;
  300. ; IN:  subfunction number in bl
  301. ;      device number in bh
  302. ;      ecx, edx, .. depends on subfunction
  303. ;
  304. ; OUT:
  305. ;
  306. ;---------------------------------------------------------------------------
  307.  
  308. align 4
  309. UDP_api:
  310.  
  311.         movzx   eax, bh
  312.         shl     eax, 2
  313.  
  314.         test    bl, bl
  315.         jz      .packets_tx     ; 0
  316.         dec     bl
  317.         jz      .packets_rx     ; 1
  318.  
  319.   .error:
  320.         mov     eax, -1
  321.         ret
  322.  
  323.   .packets_tx:
  324.         mov     eax, [UDP_PACKETS_TX + eax]
  325.         ret
  326.  
  327.   .packets_rx:
  328.         mov     eax, [UDP_PACKETS_RX + eax]
  329.         ret
  330.