Subversion Repositories Kolibri OS

Rev

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

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