Subversion Repositories Kolibri OS

Rev

Rev 1193 | 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: 983 $
  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. ; TCP opening modes
  54. SOCKET_PASSIVE  equ 0
  55. SOCKET_ACTIVE   equ 1
  56.  
  57. include "queue.inc"
  58. include "ARP.inc"
  59. include "IPv4.inc"
  60. include "ethernet.inc"
  61. include "socket.inc"
  62. ;include "tcp.inc"
  63. include "udp.inc"
  64. include "icmp.inc"
  65.  
  66. ;-----------------------------------------------
  67. ;
  68. ; stack_init
  69. ;
  70. ;  This function calls all network init procedures
  71. ;
  72. ;  IN:  /
  73. ;  OUT: /
  74. ;
  75. ;-----------------------------------------------
  76.  
  77. align 4
  78. stack_init:
  79.  
  80.         call    ETH_init
  81.         call    IPv4_init
  82.         call    ARP_init
  83.         call    UDP_init
  84.         call    ICMP_init
  85.         call    socket_init
  86.  
  87.         mov     al, 0x0                 ; set up 1s timer
  88.         out     0x70, al
  89.         in      al, 0x71
  90.         mov     [last_1sTick], al
  91.  
  92.         ret
  93.  
  94.  
  95.  
  96. ;-----------------------------------------------
  97. ;
  98. ; stack_handler
  99. ;
  100. ;  This function calls all network init procedures
  101. ;
  102. ;  IN:  /
  103. ;  OUT: /
  104. ;
  105. ;-----------------------------------------------
  106.  
  107. align 4
  108. stack_handler:
  109.  
  110.     cmp     [ETH_RUNNING], 0
  111.     je      .exit
  112.  
  113.     call    ETH_handler                 ; handle all queued ethernet packets
  114.     call    ETH_send_queued
  115.  
  116.     ; Test for 10ms tick, call tcp timer
  117.     mov     eax, [timer_ticks]
  118.     cmp     eax, [last_1hsTick]
  119.     je      .exit
  120.  
  121.     mov     [last_1hsTick], eax
  122. ;    call    tcp_tx_handler
  123.  
  124.   .sec_tick:
  125.  
  126.     ; Test for 1 second event, call 1s timer functions
  127.     mov     al, 0x0   ;second
  128.     out     0x70, al
  129.     in      al, 0x71
  130.     cmp     al, [last_1sTick]
  131.     je      .exit
  132.  
  133.     mov     [last_1sTick], al
  134.  
  135.     call    ARP_decrease_entry_ttls
  136.     call    IPv4_decrease_fragment_ttls
  137. ;    call    tcp_tcb_handler
  138.  
  139.   .exit:
  140.     ret
  141.  
  142.  
  143.  
  144. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  145. ;; Checksum [by Johnny_B]
  146. ;;  IN:
  147. ;;    buf_ptr=POINTER to buffer
  148. ;;    buf_size=SIZE of buffer
  149. ;;  OUT:
  150. ;;    AX=16-bit checksum
  151. ;;              Saves all used registers
  152. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  153. proc checksum_jb stdcall uses ebx esi ecx,\
  154.      buf_ptr:DWORD, buf_size:DWORD
  155.  
  156.     xor     eax, eax
  157.     xor     ebx, ebx  ;accumulator
  158.     mov     esi, dword[buf_ptr]
  159.     mov     ecx, dword[buf_size]
  160.     shr     ecx, 1  ; ecx=ecx/2
  161.     jnc     @f      ; if CF==0 then size is even number
  162.     mov     bh, byte[esi + ecx*2]
  163.   @@:
  164.     cld
  165.  
  166.   .loop:
  167.     lodsw               ;eax=word[esi],esi=esi+2
  168.     xchg    ah,al       ;cause must be a net byte-order
  169.     add     ebx, eax
  170.     loop    .loop
  171.  
  172.     mov     eax, ebx
  173.     shr     eax, 16
  174.     add     ax, bx
  175.     not     ax
  176.  
  177.     ret
  178. endp
  179.  
  180.  
  181.  
  182. ;----------------------------------------------------------------
  183. ;
  184. ;  System function to work with network devices (73)
  185. ;
  186. ;----------------------------------------------------------------
  187.  
  188. align 4
  189. sys_network:
  190.  
  191.         cmp     ebx, -1
  192.         jne     @f
  193.  
  194.         mov     eax, [ETH_RUNNING]
  195.         jmp     .return
  196.  
  197.    @@:
  198.         cmp     bh, MAX_NET_DEVICES              ; Check if device number exists
  199.         jge     .doesnt_exist
  200.  
  201.         mov     esi, ebx
  202.         and     esi, 0x0000ff00
  203.         shr     esi, 6
  204.  
  205.         cmp     dword [esi + ETH_DRV_LIST], 0 ; check if driver is running
  206.         je      .doesnt_exist
  207.  
  208.         test    bl, bl                  ; 0 = Get device type (ethernet/token ring/...)
  209.         jnz     @f
  210.                                          ; todo
  211.         xor     eax, eax
  212.         jmp     .return
  213.  
  214.  
  215.   @@:
  216.         dec     bl                      ; 1 = Get device name
  217.         jnz     @f
  218.  
  219.         mov     esi, [esi + ETH_DRV_LIST]
  220.         mov     esi, [esi + ETH_DEVICE.name]
  221.         mov     edi, ecx
  222.  
  223.         mov     ecx, 64 ; max length
  224.         repnz   movsb
  225.  
  226.         xor     eax, eax
  227.         jmp     .return
  228.  
  229.   @@:
  230.  
  231.         dec     bl                      ; 2 = Reset the device
  232.         jnz     @f
  233.  
  234.         mov     esi, [esi + ETH_DRV_LIST]
  235.         call    [esi + ETH_DEVICE.reset]
  236.         jmp     .return
  237.  
  238.   @@:
  239.  
  240.         dec     bl                      ; 3 = Stop driver for this device
  241.         jnz     @f
  242.  
  243.         mov     esi, [esi + ETH_DRV_LIST]
  244.         call    [esi + ETH_DEVICE.unload]
  245.         jmp     .return
  246.  
  247.   @@:
  248.  
  249.   .doesnt_exist:
  250.         DEBUGF  1,"sys_network: invalid device/function specified!\n"
  251.         mov     eax, -1
  252.  
  253.   .return:
  254.         mov     [esp+28+4], eax
  255.         ret
  256.  
  257.  
  258. ;----------------------------------------------------------------
  259. ;
  260. ;  System Function To work with Protocols  (75)
  261. ;
  262. ;----------------------------------------------------------------
  263.  
  264. align 4
  265. sys_protocols:
  266.         cmp     bh, MAX_NET_DEVICES             ; Check if device number exists
  267.         jge     .doesnt_exist
  268.  
  269.         mov     esi, ebx
  270.         and     esi, 0x0000ff00
  271.         shr     esi, 6
  272.         cmp     dword [esi + ETH_DRV_LIST], 0   ; check if driver is running TODO: check other lists too
  273.         je      .doesnt_exist
  274.  
  275.         push    .return                         ; return address (we will be using jumps instead of calls)
  276.  
  277.         mov     eax, ebx                        ; set ax to protocol number
  278.         shr     eax, 16                         ;
  279.  
  280.         cmp     ax , IP_PROTO_IP
  281.         je      IPv4_API
  282.  
  283.         cmp     ax , IP_PROTO_ICMP
  284.         je      ICMP_API
  285.  
  286.         cmp     ax , IP_PROTO_UDP
  287.         je      UDP_API
  288.  
  289.         cmp     ax , IP_PROTO_TCP
  290. ;        je      TCP_API
  291.  
  292.         cmp     ax , ETHER_ARP
  293.         je      ARP_API
  294.  
  295.         cmp     ax , ETHER
  296.         je      ETH_API
  297.  
  298.         add     esp, 4                           ; if we reached here, no function was called, so we need to balance stack
  299.  
  300.   .doesnt_exist:
  301.         DEBUGF  1,"sys_protocols: protocol %u doesnt exist on device %u!\n",ax, bh
  302.         mov     eax, -1
  303.  
  304.   .return:
  305.         mov     [esp+28+4], eax
  306.         ret