Subversion Repositories Kolibri OS

Rev

Rev 1206 | 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: 1249 $
  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.         lodsb
  181.         shl     ax, 8
  182.         add     edx, eax
  183.  
  184. .end:
  185.  
  186.         ret
  187.  
  188.  
  189.  
  190. ;-----------------------------------------------------------------
  191. ;
  192. ; checksum_2
  193. ;
  194. ;  This function calculates the final ip/tcp/udp checksum for you
  195. ;
  196. ;  IN:  edx = semi-checksum
  197. ;  OUT: dx = checksum (in INET byte order)
  198. ;
  199. ;-----------------------------------------------------------------
  200.  
  201. align 4
  202. checksum_2:
  203.  
  204.         mov     ecx, edx
  205.         shr     ecx, 16
  206.         and     edx, 0xffff
  207.         add     edx, ecx
  208.         mov     eax, edx
  209.         shr     eax, 16
  210.         add     edx, eax
  211.  
  212.         not     dx
  213.         jnz     .not_zero
  214.         dec     dx
  215.   .not_zero:
  216.         xchg    dl, dh
  217.  
  218.         DEBUGF 1,"Checksum: %x\n",dx
  219.  
  220.         ret
  221.  
  222.  
  223.  
  224. ;----------------------------------------------------------------
  225. ;
  226. ;  System function to work with network devices (73)
  227. ;
  228. ;----------------------------------------------------------------
  229.  
  230. align 4
  231. sys_network:
  232.  
  233.         cmp     ebx, -1
  234.         jne     @f
  235.  
  236.         mov     eax, [ETH_RUNNING]
  237.         jmp     .return
  238.  
  239.    @@:
  240.         cmp     bh, MAX_NET_DEVICES              ; Check if device number exists
  241.         jge     .doesnt_exist
  242.  
  243.         mov     esi, ebx
  244.         and     esi, 0x0000ff00
  245.         shr     esi, 6
  246.  
  247.         cmp     dword [esi + ETH_DRV_LIST], 0 ; check if driver is running
  248.         je      .doesnt_exist
  249.  
  250.         test    bl, bl                  ; 0 = Get device type (ethernet/token ring/...)
  251.         jnz     @f
  252.                                          ; todo
  253.         xor     eax, eax
  254.         jmp     .return
  255.  
  256.  
  257.   @@:
  258.         dec     bl                      ; 1 = Get device name
  259.         jnz     @f
  260.  
  261.         mov     esi, [esi + ETH_DRV_LIST]
  262.         mov     esi, [esi + ETH_DEVICE.name]
  263.         mov     edi, ecx
  264.  
  265.         mov     ecx, 64 ; max length
  266.         repnz   movsb
  267.  
  268.         xor     eax, eax
  269.         jmp     .return
  270.  
  271.   @@:
  272.  
  273.         dec     bl                      ; 2 = Reset the device
  274.         jnz     @f
  275.  
  276.         mov     esi, [esi + ETH_DRV_LIST]
  277.         call    [esi + ETH_DEVICE.reset]
  278.         jmp     .return
  279.  
  280.   @@:
  281.  
  282.         dec     bl                      ; 3 = Stop driver for this device
  283.         jnz     @f
  284.  
  285.         mov     esi, [esi + ETH_DRV_LIST]
  286.         call    [esi + ETH_DEVICE.unload]
  287.         jmp     .return
  288.  
  289.   @@:
  290.         dec     bl                      ; 4 = Get driver pointer
  291.         jnz     @f
  292.  
  293.         ; ..;
  294.  
  295.  
  296.   @@:
  297. ;  ...                                   ; 5 Get driver name
  298.  
  299.   .doesnt_exist:
  300.         DEBUGF  1,"sys_network: invalid device/function specified!\n"
  301.         mov     eax, -1
  302.  
  303.   .return:
  304.         mov     [esp+28+4], eax
  305.         ret
  306.  
  307.  
  308. ;----------------------------------------------------------------
  309. ;
  310. ;  System Function To work with Protocols  (75)
  311. ;
  312. ;----------------------------------------------------------------
  313.  
  314. align 4
  315. sys_protocols:
  316.         cmp     bh, MAX_NET_DEVICES             ; Check if device number exists
  317.         jge     .doesnt_exist
  318.  
  319.         mov     esi, ebx
  320.         and     esi, 0x0000ff00
  321.         shr     esi, 6
  322.         cmp     dword [esi + ETH_DRV_LIST], 0   ; check if driver is running TODO: check other lists too
  323.         je      .doesnt_exist
  324.  
  325.         push    .return                         ; return address (we will be using jumps instead of calls)
  326.  
  327.         mov     eax, ebx                        ; set ax to protocol number
  328.         shr     eax, 16                         ;
  329.  
  330.         cmp     ax , IP_PROTO_IP
  331.         je      IPv4_API
  332.  
  333.         cmp     ax , IP_PROTO_ICMP
  334.         je      ICMP_API
  335.  
  336.         cmp     ax , IP_PROTO_UDP
  337.         je      UDP_API
  338.  
  339.         cmp     ax , IP_PROTO_TCP
  340. ;        je      TCP_API
  341.  
  342.         cmp     ax , ETHER_ARP
  343.         je      ARP_API
  344.  
  345.         cmp     ax , ETHER
  346.         je      ETH_API
  347.  
  348.         add     esp, 4                           ; if we reached here, no function was called, so we need to balance stack
  349.  
  350.   .doesnt_exist:
  351.         DEBUGF  1,"sys_protocols: protocol %u doesnt exist on device %u!\n",ax, bh
  352.         mov     eax, -1
  353.  
  354.   .return:
  355.         mov     [esp+28+4], eax
  356.         ret