Subversion Repositories Kolibri OS

Rev

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

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;                                                                 ;;
  3. ;; Copyright (C) KolibriOS team 2004-2011. All rights reserved.    ;;
  4. ;; Distributed under terms of the GNU General Public License       ;;
  5. ;;                                                                 ;;
  6. ;;  Part of the tcp/ip network stack for KolibriOS                 ;;
  7. ;;                                                                 ;;
  8. ;;   Written by hidnplayr@kolibrios.org                            ;;
  9. ;;                                                                 ;;
  10. ;;    Based on the code of 4.4BSD                                  ;;
  11. ;;                                                                 ;;
  12. ;;          GNU GENERAL PUBLIC LICENSE                             ;;
  13. ;;             Version 2, June 1991                                ;;
  14. ;;                                                                 ;;
  15. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  16.  
  17. $Revision: 1885 $
  18.  
  19. macro   TCP_checksum IP1, IP2 {
  20.  
  21. ;-------------
  22. ; Pseudoheader
  23.  
  24.         ; protocol type
  25.         mov     edx, IP_PROTO_TCP
  26.  
  27.         ; source address
  28.         add     dl, byte [IP1+1]
  29.         adc     dh, byte [IP1+0]
  30.         adc     dl, byte [IP1+3]
  31.         adc     dh, byte [IP1+2]
  32.  
  33.         ; destination address
  34.         adc     dl, byte [IP2+1]
  35.         adc     dh, byte [IP2+0]
  36.         adc     dl, byte [IP2+3]
  37.         adc     dh, byte [IP2+2]
  38.  
  39.         ; size
  40.         adc     dl, cl
  41.         adc     dh, ch
  42.  
  43. ;---------------------
  44. ; Real header and data
  45.  
  46.         push    esi
  47.         call    checksum_1
  48.         call    checksum_2
  49.         pop     esi
  50.  
  51. }       ; returns in dx only
  52.  
  53.  
  54.  
  55.  
  56. macro   TCP_sendseqinit ptr {
  57.  
  58.         push    edi                     ;;;; i dont like this static use of edi
  59.         mov     edi, [ptr + TCP_SOCKET.ISS]
  60.         mov     [ptr + TCP_SOCKET.SND_UP], edi
  61.         mov     [ptr + TCP_SOCKET.SND_MAX], edi
  62.         mov     [ptr + TCP_SOCKET.SND_NXT], edi
  63.         mov     [ptr + TCP_SOCKET.SND_UNA], edi
  64.         pop     edi
  65.  
  66. }
  67.  
  68.  
  69.  
  70. macro   TCP_rcvseqinit ptr {
  71.  
  72.         push    edi
  73.         mov     edi, [ptr + TCP_SOCKET.IRS]
  74.         inc     edi
  75.         mov     [ptr + TCP_SOCKET.RCV_NXT], edi
  76.         mov     [ptr + TCP_SOCKET.RCV_ADV], edi
  77.         pop     edi
  78.  
  79. }
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90. ;---------------------------
  91. ;
  92. ; TCP_pull_out_of_band
  93. ;
  94. ; IN:  eax =
  95. ;      ebx = socket ptr
  96. ;      edx = tcp packet ptr
  97. ;
  98. ; OUT: /
  99. ;
  100. ;---------------------------
  101.  
  102. align 4
  103. TCP_pull_out_of_band:
  104.  
  105.         DEBUGF  1,"TCP_pull_out_of_band\n"
  106.  
  107.         ;;;; 1282-1305
  108.  
  109.         ret
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118. ;-------------------------
  119. ;
  120. ; TCP_drop
  121. ;
  122. ;  IN:  eax = socket ptr
  123. ;       ebx = error number
  124. ;
  125. ;  OUT: eax = socket ptr
  126. ;
  127. ;-------------------------
  128. align 4
  129. TCP_drop:
  130.  
  131.         DEBUGF  1,"TCP_drop\n"
  132.  
  133.         cmp     [eax + TCP_SOCKET.t_state], TCPS_SYN_RECEIVED
  134.         jl      .no_syn_received
  135.  
  136.         mov     [eax + TCP_SOCKET.t_state], TCPS_CLOSED
  137.  
  138.         call    TCP_output
  139.  
  140. ;;; TODO: update stats
  141.  
  142.         jmp     TCP_close
  143.  
  144.   .no_syn_received:
  145.  
  146. ;;; TODO: update stats
  147.  
  148. ;;; TODO: check if error code is "Connection timed out' and handle accordingly
  149.  
  150.         mov     [eax + SOCKET.errorcode], ebx
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159. ;-------------------------
  160. ;
  161. ; TCP_close
  162. ;
  163. ;  IN:  eax = socket ptr
  164. ;  OUT: eax = socket ptr
  165. ;
  166. ;-------------------------
  167. align 4
  168. TCP_close:
  169.  
  170.         DEBUGF  1,"TCP_close\n"
  171.  
  172. ;;; TODO: update RTT and mean deviation
  173. ;;; TODO: update slow start threshold
  174. ;;; TODO: release connection resources
  175.  
  176.         call    SOCKET_is_disconnected
  177.  
  178.         ret
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189. ;-------------------------
  190. ;
  191. ; TCP_outflags
  192. ;
  193. ;  IN:  eax = socket ptr
  194. ;
  195. ;  OUT: edx = flags
  196. ;
  197. ;-------------------------
  198. align 4
  199. TCP_outflags:
  200.  
  201.         mov     edx, [eax + TCP_SOCKET.t_state]
  202.         movzx   edx, byte [edx + .flaglist]
  203.  
  204.         DEBUGF  1,"TCP_outflags, socket: %x, flags: %x\n", eax, dl
  205.  
  206.         ret
  207.  
  208.   .flaglist:
  209.  
  210.         db      TH_RST + TH_ACK         ; TCPS_CLOSED
  211.         db      0                       ; TCPS_LISTEN
  212.         db      TH_SYN                  ; TCPS_SYN_SENT
  213.         db      TH_SYN + TH_ACK         ; TCPS_SYN_RECEIVED
  214.         db               TH_ACK         ; TCPS_ESTABLISHED
  215.         db               TH_ACK         ; TCPS_CLOSE_WAIT
  216.         db      TH_SYN + TH_ACK         ; TCPS_FIN_WAIT_1
  217.         db      TH_SYN + TH_ACK         ; TCPS_CLOSING
  218.         db      TH_SYN + TH_ACK         ; TCPS_LAST_ACK
  219.         db               TH_ACK         ; TCPS_FIN_WAIT_2
  220.         db               TH_ACK         ; TCPS_TIMED_WAIT
  221.  
  222.  
  223.  
  224.  
  225.  
  226.  
  227. ;---------------------------------------
  228. ;
  229. ; The fast way to send an ACK/RST/keepalive segment
  230. ;
  231. ; TCP_respond_socket:
  232. ;
  233. ;  IN:  ebx = socket ptr
  234. ;        cl = flags
  235. ;
  236. ;--------------------------------------
  237. align 4
  238. TCP_respond_socket:
  239.  
  240.         DEBUGF  1,"TCP_respond_socket\n"
  241.  
  242. ;---------------------
  243. ; Create the IP packet
  244.  
  245.         push    cx ebx
  246.         mov     eax, [ebx + IP_SOCKET.RemoteIP]
  247.         mov     ebx, [ebx + IP_SOCKET.LocalIP]
  248.         mov     ecx, TCP_segment.Data
  249.         mov     di , IP_PROTO_TCP shl 8 + 128
  250.         call    IPv4_output
  251.         test    edi, edi
  252.         jz      .error
  253.         pop     esi cx
  254.         push    edx eax
  255.  
  256. ;-----------------------------------------------
  257. ; Fill in the TCP header by using the socket ptr
  258.  
  259.         mov     ax, [esi + TCP_SOCKET.LocalPort]
  260.         rol     ax, 8
  261.         stosw
  262.         mov     ax, [esi + TCP_SOCKET.RemotePort]
  263.         rol     ax, 8
  264.         stosw
  265.         mov     eax, [esi + TCP_SOCKET.SND_NXT]
  266.         bswap   eax
  267.         stosd
  268.         mov     eax, [esi + TCP_SOCKET.RCV_NXT]
  269.         bswap   eax
  270.         stosd
  271.         mov     al, 0x50        ; Dataoffset: 20 bytes
  272.         stosb
  273.         mov     al, cl
  274.         stosb
  275. ;        mov     ax, [esi + TCP_SOCKET.RCV_WND]
  276. ;        rol     ax, 8
  277.         mov     ax, 0x00a0      ;;;;;;; FIXME
  278.         stosw                   ; window
  279.         xor     eax, eax
  280.         stosd                   ; checksum + urgentpointer
  281.  
  282. ;---------------------
  283. ; Fill in the checksum
  284.  
  285.   .checksum:
  286.         sub     edi, TCP_segment.Data
  287.         mov     ecx, TCP_segment.Data
  288.         xchg    esi, edi
  289.         TCP_checksum (edi + IP_SOCKET.LocalIP), (edi + IP_SOCKET.RemoteIP)
  290.         mov     [esi+TCP_segment.Checksum], dx
  291.  
  292. ;--------------------
  293. ; And send the segment
  294.  
  295.         call    [ebx + NET_DEVICE.transmit]
  296.         ret
  297.  
  298.   .error:
  299.         DEBUGF  1,"TCP_respond failed\n"
  300.         add     esp, 2+4
  301.  
  302.         ret
  303.  
  304.  
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311. ;-------------------------
  312. ; TCP_respond.segment:
  313. ;
  314. ;  IN:  edx = segment ptr (a previously received segment)
  315. ;        cl = flags
  316.  
  317. align 4
  318. TCP_respond_segment:
  319.  
  320.         DEBUGF  1,"TCP_respond_segment\n"
  321.  
  322. ;---------------------
  323. ; Create the IP packet
  324.  
  325.         push    cx edx
  326.         mov     ebx, [edx - 20 + IPv4_Packet.SourceAddress]      ;;;; and what if ip packet had options?!
  327.         mov     eax, [edx - 20 + IPv4_Packet.DestinationAddress]   ;;;
  328.         mov     ecx, TCP_segment.Data
  329.         mov     di , IP_PROTO_TCP shl 8 + 128
  330.         call    IPv4_output
  331.         jz      .error
  332.         pop     esi cx
  333.  
  334.         push    edx eax
  335.  
  336. ;---------------------------------------------------
  337. ; Fill in the TCP header by using a received segment
  338.  
  339.         mov     ax, [esi + TCP_segment.DestinationPort]
  340.         rol     ax, 8
  341.         stosw
  342.         mov     ax, [esi + TCP_segment.SourcePort]
  343.         rol     ax, 8
  344.         stosw
  345.         mov     eax, [esi + TCP_segment.AckNumber]
  346.         bswap   eax
  347.         stosd
  348.         xor     eax, eax
  349.         stosd
  350.         mov     al, 0x50        ; Dataoffset: 20 bytes
  351.         stosb
  352.         mov     al, cl
  353.         stosb
  354.         mov     ax, 1280
  355.         rol     ax, 8
  356.         stosw                   ; window
  357.         xor     eax, eax
  358.         stosd                   ; checksum + urgentpointer
  359.  
  360. ;---------------------
  361. ; Fill in the checksum
  362.  
  363.   .checksum:
  364.         lea     esi, [edi - TCP_segment.Data]
  365.         mov     ecx, TCP_segment.Data
  366.         TCP_checksum (esi - 20 + IPv4_Packet.DestinationAddress), (esi - 20 + IPv4_Packet.DestinationAddress)
  367.         mov     [esi+TCP_segment.Checksum], dx
  368.  
  369. ;--------------------
  370. ; And send the segment
  371.  
  372.         call    [ebx + NET_DEVICE.transmit]
  373.         ret
  374.  
  375.   .error:
  376.         DEBUGF  1,"TCP_respond failed\n"
  377.         add     esp, 2+4
  378.  
  379.         ret