Subversion Repositories Kolibri OS

Rev

Rev 1171 | 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     bh, MAX_NET_DEVICES              ; Check if device number exists
  192.         jge     .doesnt_exist
  193.  
  194.         mov     esi, ebx
  195.         and     esi, 0x0000ff00
  196.         shr     esi, 6
  197.  
  198.         cmp     dword [esi + ETH_DRV_LIST], 0 ; check if driver is running
  199.         je      .doesnt_exist
  200.  
  201.         test    bl, bl                  ; 0 = Get device type (ethernet/token ring/...)
  202.         jnz     @f
  203.  
  204.         ;TODO: write code here
  205.  
  206.  
  207.   @@:
  208.         dec     bl                      ; 1 = Get device name
  209.         jnz     @f
  210.  
  211.         mov     esi, [esi + ETH_DRV_LIST]
  212.         mov     esi, [esi + ETH_DEVICE.name]
  213.         mov     edi, ecx
  214.  
  215.         mov     ecx, 64 ; max length
  216.         repnz   movsb
  217.  
  218.         ret
  219.  
  220.         ; TODO: create function wich outputs number of active network devices
  221.  
  222.   @@:
  223.   .doesnt_exist:
  224.         DEBUGF  1,"sys_network: invalid device/function specified!\n"
  225.         mov     eax, -1
  226.  
  227.         ret
  228.  
  229.  
  230.  
  231.  
  232. ;----------------------------------------------------------------
  233. ;
  234. ;  System Function To work with Protocols  (75)
  235. ;
  236. ;----------------------------------------------------------------
  237.  
  238. align 4
  239. sys_protocols:
  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.         cmp     dword [esi + ETH_DRV_LIST], 0   ; check if driver is running TODO: check other lists too
  247.         je      .doesnt_exist
  248.  
  249.         push    .return                         ; return address (we will be using jumps instead of calls)
  250.  
  251.         mov     eax, ebx                        ; set ax to protocol number
  252.         shr     eax, 16                         ;
  253.  
  254.         cmp     ax , IP_PROTO_IP
  255.         je      IPv4_API
  256.  
  257.         cmp     ax , IP_PROTO_ICMP
  258.         je      ICMP_API
  259.  
  260.         cmp     ax , IP_PROTO_UDP
  261.         je      UDP_API
  262.  
  263.         cmp     ax , IP_PROTO_TCP
  264. ;        je      TCP_API
  265.  
  266.         cmp     ax , ETHER_ARP
  267.         je      ARP_API
  268.  
  269.         cmp     ax , ETHER
  270.         je      ETH_API
  271.  
  272.         add     esp, 4                           ; if we reached here, no function was called, so we need to balance stack
  273.  
  274.   .doesnt_exist:
  275.         DEBUGF  1,"sys_protocols: protocol %u doesnt exist on device %u!\n",ax, bh
  276.         mov     eax, -1
  277.  
  278.   .return:
  279.         mov     [esp+28+4], eax
  280.         ret