Subversion Repositories Kolibri OS

Rev

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