Subversion Repositories Kolibri OS

Rev

Rev 1249 | 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. ;;  STACK.INC                                                      ;;
  7. ;;                                                                 ;;
  8. ;;  BASIC TCP/IP stack for KolibriOS                               ;;
  9. ;;                                                                 ;;
  10. ;;    Written by hidnplayr@kolibrios.org                           ;;
  11. ;;                                                                 ;;
  12. ;;     based on the work of Mike Hibbett, mikeh@oceanfree.net      ;;
  13. ;;     but also Paolo Franchetti                                   ;;
  14. ;;                                                                 ;;
  15. ;;          GNU GENERAL PUBLIC LICENSE                             ;;
  16. ;;             Version 2, June 1991                                ;;
  17. ;;                                                                 ;;
  18. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  19.  
  20. $Revision: 1251 $
  21.  
  22. uglobal
  23.         last_1sTick     db ?
  24.         last_1hsTick    dd ?
  25. endg
  26.  
  27. MAX_NET_DEVICES equ 16
  28.  
  29. MIN_EPHEMERAL_PORT equ 49152
  30. MAX_EPHEMERAL_PORT equ 61000
  31.  
  32. ETHER           equ 1337
  33. ETHER_ARP       equ 0x0608
  34.  
  35. ;AF_UNSPEC       equ 0
  36. AF_UNIX         equ 1
  37. AF_INET4        equ 2
  38. ;AF_AX25         equ 3
  39. ;AF_IPX          equ 4
  40. ;AF_APPLETALK    equ 5
  41. ;AF_NETROM       equ 6
  42. ;AF_BRIDGE       equ 7
  43. ;AF_AAL5         equ 8
  44. ;AF_X25          equ 9
  45. ;AF_INET6        equ 10
  46. ;AF_MAX          equ 12
  47.  
  48. IP_PROTO_IP     equ 0
  49. IP_PROTO_ICMP   equ 1
  50. IP_PROTO_TCP    equ 6
  51. IP_PROTO_UDP    equ 17
  52.  
  53. ; Socket types
  54. SOCK_STREAM     = 1
  55. SOCK_DGRAM      = 2
  56. SOCK_RAW        = 3
  57.  
  58. ; TCP opening modes
  59. SOCKET_PASSIVE  equ 0
  60. SOCKET_ACTIVE   equ 1
  61.  
  62. include "queue.inc"
  63. include "ARP.inc"
  64. include "IPv4.inc"
  65. include "ethernet.inc"
  66. include "socket.inc"
  67. include "tcp.inc"
  68. include "udp.inc"
  69. include "icmp.inc"
  70.  
  71. ;-----------------------------------------------
  72. ;
  73. ; stack_init
  74. ;
  75. ;  This function calls all network init procedures
  76. ;
  77. ;  IN:  /
  78. ;  OUT: /
  79. ;
  80. ;-----------------------------------------------
  81.  
  82. align 4
  83. stack_init:
  84.  
  85.         call    ETH_init
  86.         call    IPv4_init
  87.         call    ARP_init
  88.         call    UDP_init
  89.         call    TCP_init
  90.         call    ICMP_init
  91.         call    socket_init
  92.  
  93.         mov     al, 0x0                 ; set up 1s timer
  94.         out     0x70, al
  95.         in      al, 0x71
  96.         mov     [last_1sTick], al
  97.  
  98.         ret
  99.  
  100.  
  101.  
  102. ;-----------------------------------------------
  103. ;
  104. ; stack_handler
  105. ;
  106. ;  This function calls all network init procedures
  107. ;
  108. ;  IN:  /
  109. ;  OUT: /
  110. ;
  111. ;-----------------------------------------------
  112.  
  113. align 4
  114. stack_handler:
  115.  
  116.     cmp     [ETH_RUNNING], 0
  117.     je      .exit
  118.  
  119.     ; Test for 10ms tick
  120.     mov     eax, [timer_ticks]
  121.     cmp     eax, [last_1hsTick]
  122.     je      .exit
  123.  
  124.     mov     [last_1hsTick], eax
  125.  
  126.     call    ETH_handler                 ; handle all queued ethernet packets
  127.     call    ETH_send_queued
  128.     call    TCP_send_queued
  129.  
  130.   .sec_tick:
  131.  
  132.     ; Test for 1 second event
  133.     mov     al, 0x0   ;second
  134.     out     0x70, al
  135.     in      al, 0x71
  136.     cmp     al, [last_1sTick]
  137.     je      .exit
  138.  
  139.     mov     [last_1sTick], al
  140.  
  141.     call    ARP_decrease_entry_ttls
  142.     call    IPv4_decrease_fragment_ttls
  143.     call    TCP_decrease_socket_ttls
  144.  
  145.   .exit:
  146.     ret
  147.  
  148.  
  149.  
  150.  
  151.  
  152. ;-----------------------------------------------------------------
  153. ;
  154. ; checksum_1
  155. ;
  156. ;  This is the first of two functions needed to calculate the TCP checksum.
  157. ;
  158. ;  IN:  edx = start offeset for semi-checksum
  159. ;       esi = pointer to data
  160. ;       ecx = data size
  161. ;  OUT: edx = semi-checksum
  162. ;
  163. ;-----------------------------------------------------------------
  164.  
  165. align 4
  166. checksum_1:
  167.  
  168.         xor     eax, eax
  169.         shr     ecx, 1
  170.         pushf
  171. .loop:
  172.         lodsw
  173.         xchg    al, ah
  174.         add     edx, eax
  175.         loop    .loop
  176.  
  177.         popf
  178.         jnc     .end
  179.  
  180.         add     dh, [esi]
  181.         adc     edx, 0
  182.  
  183. .end:
  184.  
  185.         ret
  186.  
  187.  
  188.  
  189. ;-----------------------------------------------------------------
  190. ;
  191. ; checksum_2
  192. ;
  193. ;  This function calculates the final ip/tcp/udp checksum for you
  194. ;
  195. ;  IN:  edx = semi-checksum
  196. ;  OUT: dx = checksum (in INET byte order)
  197. ;
  198. ;-----------------------------------------------------------------
  199.  
  200. align 4
  201. checksum_2:
  202.  
  203.         mov     ecx, edx
  204.         shr     ecx, 16
  205.         and     edx, 0xffff
  206.         add     edx, ecx
  207.         mov     eax, edx
  208.         shr     eax, 16
  209.         add     edx, eax
  210.  
  211.         not     dx
  212.         jnz     .not_zero
  213.         dec     dx
  214.   .not_zero:
  215.         xchg    dl, dh
  216.  
  217.         DEBUGF 1,"Checksum: %x\n",dx
  218.  
  219.         ret
  220.  
  221.  
  222.  
  223. ;----------------------------------------------------------------
  224. ;
  225. ;  System function to work with network devices (73)
  226. ;
  227. ;----------------------------------------------------------------
  228.  
  229. align 4
  230. sys_network:
  231.  
  232.         cmp     ebx, -1
  233.         jne     @f
  234.  
  235.         mov     eax, [ETH_RUNNING]
  236.         jmp     .return
  237.  
  238.    @@:
  239.         cmp     bh, MAX_NET_DEVICES              ; Check if device number exists
  240.         jge     .doesnt_exist
  241.  
  242.         mov     esi, ebx
  243.         and     esi, 0x0000ff00
  244.         shr     esi, 6
  245.  
  246.         cmp     dword [esi + ETH_DRV_LIST], 0 ; check if driver is running
  247.         je      .doesnt_exist
  248.  
  249.         test    bl, bl                  ; 0 = Get device type (ethernet/token ring/...)
  250.         jnz     @f
  251.                                          ; todo
  252.         xor     eax, eax
  253.         jmp     .return
  254.  
  255.  
  256.   @@:
  257.         dec     bl                      ; 1 = Get device name
  258.         jnz     @f
  259.  
  260.         mov     esi, [esi + ETH_DRV_LIST]
  261.         mov     esi, [esi + ETH_DEVICE.name]
  262.         mov     edi, ecx
  263.  
  264.         mov     ecx, 64 ; max length
  265.         repnz   movsb
  266.  
  267.         xor     eax, eax
  268.         jmp     .return
  269.  
  270.   @@:
  271.  
  272.         dec     bl                      ; 2 = Reset the device
  273.         jnz     @f
  274.  
  275.         mov     esi, [esi + ETH_DRV_LIST]
  276.         call    [esi + ETH_DEVICE.reset]
  277.         jmp     .return
  278.  
  279.   @@:
  280.  
  281.         dec     bl                      ; 3 = Stop driver for this device
  282.         jnz     @f
  283.  
  284.         mov     esi, [esi + ETH_DRV_LIST]
  285.         call    [esi + ETH_DEVICE.unload]
  286.         jmp     .return
  287.  
  288.   @@:
  289.         dec     bl                      ; 4 = Get driver pointer
  290.         jnz     @f
  291.  
  292.         ; ..;
  293.  
  294.  
  295.   @@:
  296. ;  ...                                   ; 5 Get driver name
  297.  
  298.   .doesnt_exist:
  299.         DEBUGF  1,"sys_network: invalid device/function specified!\n"
  300.         mov     eax, -1
  301.  
  302.   .return:
  303.         mov     [esp+28+4], eax
  304.         ret
  305.  
  306.  
  307. ;----------------------------------------------------------------
  308. ;
  309. ;  System Function To work with Protocols  (75)
  310. ;
  311. ;----------------------------------------------------------------
  312.  
  313. align 4
  314. sys_protocols:
  315.         cmp     bh, MAX_NET_DEVICES             ; Check if device number exists
  316.         jge     .doesnt_exist
  317.  
  318.         mov     esi, ebx
  319.         and     esi, 0x0000ff00
  320.         shr     esi, 6
  321.         cmp     dword [esi + ETH_DRV_LIST], 0   ; check if driver is running TODO: check other lists too
  322.         je      .doesnt_exist
  323.  
  324.         push    .return                         ; return address (we will be using jumps instead of calls)
  325.  
  326.         mov     eax, ebx                        ; set ax to protocol number
  327.         shr     eax, 16                         ;
  328.  
  329.         cmp     ax , IP_PROTO_IP
  330.         je      IPv4_API
  331.  
  332.         cmp     ax , IP_PROTO_ICMP
  333.         je      ICMP_API
  334.  
  335.         cmp     ax , IP_PROTO_UDP
  336.         je      UDP_API
  337.  
  338.         cmp     ax , IP_PROTO_TCP
  339. ;        je      TCP_API
  340.  
  341.         cmp     ax , ETHER_ARP
  342.         je      ARP_API
  343.  
  344.         cmp     ax , ETHER
  345.         je      ETH_API
  346.  
  347.         add     esp, 4                           ; if we reached here, no function was called, so we need to balance stack
  348.  
  349.   .doesnt_exist:
  350.         DEBUGF  1,"sys_protocols: protocol %u doesnt exist on device %u!\n",ax, bh
  351.         mov     eax, -1
  352.  
  353.   .return:
  354.         mov     [esp+28+4], eax
  355.         ret