home *** CD-ROM | disk | FTP | other *** search
/ PC Press: Internet / PC_PRESS.ISO / software / dos / misc / inar-100.exe / SRC / PCROUTE / ETHER.INC < prev    next >
Encoding:
Text File  |  1995-05-21  |  9.6 KB  |  302 lines

  1. ;;*****************************************************************************
  2. ;;                        ether.inc           ether.inc
  3. ;;*****************************************************************************
  4. ;;
  5. ;;  Copyright (C) 1989 Northwestern University, Vance Morrison
  6. ;;
  7. ;;
  8. ;; Permission to view, compile, and modify for LOCAL (intra-organization) 
  9. ;; USE ONLY is hereby granted, provided that this copyright and permission 
  10. ;; notice appear on all copies.  Any other use by permission only.
  11. ;;
  12. ;; Northwestern University makes no representations about the suitability 
  13. ;; of this software for any purpose.  It is provided "as is" without expressed 
  14. ;; or implied warranty.  See the copywrite notice file for complete details.
  15. ;;
  16. ;;*****************************************************************************
  17. ;;
  18. ;; ether.inc constains the board independant, ethernet processing that needs 
  19. ;; to be done.  I rely only on the IF interface defined in if.inc.
  20. ;;
  21. ;; The functions provided by this file are
  22. ;;
  23. ;;   ETH_DECLARE name, interface, task
  24. ;;   ETH_DEFINE name
  25. ;;   ETH_R_READ name, type, code_label
  26. ;;   ETH_R_RETURN name
  27. ;;   ETH_R_CONT_in_BX_CX_ES_const_BX_CX_DX_BP_SI_DI_ES name, ok
  28. ;;   ETH_W_ACCESS_in_CX_out_DI_ES_const_BX_CX_BP name, no_buffer
  29. ;;   ETH_W_WRITE_in_AX_CX_SI_DI_ES_const_BX_BP_ES name
  30. ;;   ETH_IS_BROADCAST_in_BX_ES_const_AX_BX_CX_DX_BP_DI_ES name
  31. ;;   ETH_COPY_in_CX_SI_DI_ES_out_SI_DI_const_BX_BP_ES name
  32. ;;
  33. ;;  Variables Provided by this module (READ ONLY!!!!)
  34. ;;      
  35. ;;      eth_&name&_declared     1 if this module is declared
  36. ;;      eth_&name&_address      The hardware address
  37. ;;      eth_&name&_mtu          The maximum transmission unit
  38. ;;
  39. ;;*****************************************************************************
  40.  
  41.  
  42. ;;*****************************************************************************
  43. ;; defs for ethernet structures
  44.  
  45. IP_TYPE             = 0800h
  46. SWAPPED_IP_TYPE     = 0008h    
  47. ARP_TYPE            = 0806h
  48. SWAPPED_ARP_TYPE    = 0608h     
  49.  
  50. ether   STRUC   
  51.     ether_dst       DB 6 DUP (0)
  52.     ether_src       DB 6 DUP (0)
  53.     ether_type      DW 0
  54. ether   ENDS
  55.  
  56. ;;*****************************************************************************
  57. ;; data needed by this module
  58.  
  59. eth_jmp_entry STRUC
  60.     eth_read_type   DW ?
  61.     eth_read_label  DW ?
  62. eth_jmp_entry ENDS
  63.  
  64. eth_data STRUC
  65.     eth_read_cnt    DW 0
  66.     eth_read_jmp    eth_jmp_entry 10 DUP (<>)
  67. eth_data ENDS
  68.  
  69.  
  70. ;;*****************************************************************************
  71. ;;   ETH_DECLARE name, interface, task
  72. ;;       creates a new data link  object.  'name' is the name of this new
  73. ;;       object.  'interface' is the name of the interface to that will provide
  74. ;;       the low level services this module will need.  'task' is the name
  75. ;;       of a task that we need
  76. ;;   
  77. ETH_DECLARE MACRO name, interface, task
  78.     .errb <task>
  79.  
  80.     .DATA
  81.     eth_&name&_declared     = 1
  82.     eth_&name&_interface    = interface
  83.     eth_&name&_task         = task
  84.     eth_&name&_address      = if_&interface&_address
  85.     eth_&name&_mtu          = 1500              ;; maximum transmission unit
  86.  
  87.     global eth_&name&_data:eth_data 
  88.     .CODE
  89.     global eth_&name&_continue:near 
  90.     global eth_&name&_real_define:near
  91. ENDM
  92.  
  93.  
  94. ;;*****************************************************************************
  95. ;;   ETH_DEFINE name
  96. ;;      ETH_DEFINE declare all the things that have to be defined in
  97. ;;      every independantly assembled module.  ETH declares those
  98. ;;      things that need be be done only once (in particular memory allocation
  99. ;;      and initialzation code).  Every module including the one ETH_DEFINE
  100. ;;      is in) must have a ETH_DELCARE.   
  101. ;;
  102. ETH_DEFINE MACRO name
  103.  
  104.     call eth_&name&_real_define
  105. ENDM
  106.  
  107. ETH_REAL_DEFINE MACRO name
  108.     local start, around
  109.     .errb <name>
  110.  
  111. ifdef eth_&name&_declared 
  112.     .DATA
  113.     eth_&name&_data  eth_data <>             ;; create storage needed
  114.  
  115.     .CODE
  116.     jmp around
  117.         start:
  118.         ETH_TASK name
  119.             ;; this does NOT fall through
  120.     around:
  121.     eth_&name&_real_define:
  122.     TASK_DEFINE %eth_&name&_task, start          ;; start the task
  123.     RET
  124. endif
  125. ENDM
  126.  
  127.  
  128. ;;******************************************************************************
  129. ;;   ETH_R_READ name, type, code_label
  130. ;;      ETH_R_READ declares that code starting at 'code_label' should be 
  131. ;;      called whenever a packet with type 'type' is read from the IF.  
  132. ;;      The code is called with BX:ES pointing to the start of the packet
  133. ;;      CX loaded with the length of the packet and AX loaded with the 
  134. ;;      type of the packet.  The code should 
  135. ;;      execute ETH_R_RETURN when it is through.
  136. ;;       
  137. ETH_R_READ MACRO name, type, code_label
  138.     local skip
  139.     .errb <code_label>
  140.  
  141.     push DX
  142.     push DI
  143.     mov DX, word ptr eth_&name&_data.eth_read_cnt
  144.     cmp DX, 10
  145.     jge skip
  146.     mov DI, DX
  147.     inc DX
  148.     mov word ptr eth_&name&_data.eth_read_cnt, DX
  149.     shl DI, 1
  150.     shl DI, 1
  151.     add DI, offset eth_&name&_data.eth_read_jmp
  152.     mov word ptr [DI+eth_read_type], type
  153.     mov word ptr [DI+eth_read_label], offset code_label
  154.     skip:
  155.     pop DI
  156.     pop DX
  157. ENDM
  158.  
  159. ;;******************************************************************************
  160. ;;   ETH_R_CONT_in_BX_CX_ES name, ok
  161. ;;       ETH_R_CONT determines if the packet returned by R_READ in BX:ES
  162. ;;       of length CX is continuous.  If it is it jumps to 'ok' otherwise
  163. ;;       it just returns
  164. ;;
  165. ETH_R_CONT_in_BX_CX_ES_const_BX_CX_DX_BP_SI_DI_ES MACRO name, ok
  166.     .errb <ok>
  167.  
  168.     IF_R_CONT_in_BX_CX_ES_const_BX_CX_DX_BP_SI_DI_ES %eth_&name&_interface, ok
  169. ENDM
  170.  
  171. ;;******************************************************************************
  172. ;; ETH_R_RETURN 
  173. ;;      ETH_R_RETURN should be called at the end of the read code to signal
  174. ;;      that the read code is finished with the packet read in
  175. ;;
  176. ETH_R_RETURN MACRO name
  177.     .errb <name>
  178.  
  179.     jmp eth_&name&_continue
  180. ENDM
  181.  
  182.  
  183. ;;******************************************************************************
  184. ;;   ETH_W_ACCESS_in_CX_out_DI_ES name, no_buffer
  185. ;;       ETH_W_ACCESS returns a pointer to an output buffer for a (network) 
  186. ;;       packet.  The pointer is returned in DI:ES.  If the output buffer is 
  187. ;;       busy, this routine jump to 'no_buffer'.   The buffer return will
  188. ;;       be at least min(CX, 1500) bytes long
  189. ;;
  190. ETH_W_ACCESS_in_CX_out_DI_ES_const_BX_CX_BP MACRO name, no_buffer
  191.     .errb <no_buffer>
  192.     
  193.     add CX, size ether
  194.     IF_W_ACCESS_in_CX_out_DI_ES_const_BX_CX_BP  %eth_&name&_interface, no_buffer
  195.     sub CX, size ether
  196.     add DI, size ether             ;; make it point to the Data part
  197. ENDM
  198.  
  199.  
  200. ;;******************************************************************************
  201. ;;   ETH_W_WRITE_in_AX_CX_SI_DI_ES name, type
  202. ;;       ETH_W_WRITE actually signals the link layer to write a packet to the 
  203. ;;       network.  The packet is assumed to be in the buffer returned by 
  204. ;;       ETH_W_ACCESS.  CX is the length of the packet to send.   SI:DS
  205. ;;       is a pointer to the hardware destination address.  AX is the
  206. ;;       type code of the ethernet packet to send.  
  207. ;;       DI:ES MUST point be the pointer returned by W_ACCESS_out_DI_ES
  208. ;;
  209. ETH_W_WRITE_in_AX_CX_SI_DI_ES_const_BX_BP_ES MACRO name
  210.     local size_ok
  211.     .errb <name>
  212.     
  213.     add CX, size ether
  214.     cmp CX, 60
  215.     jae size_ok
  216.         mov CX, 60
  217.     size_ok:
  218.     sub DI, size ether
  219.     movsw
  220.     movsw
  221.     movsw
  222.  
  223.     mov SI, offset eth_&name&_address
  224.     movsw
  225.     movsw
  226.     movsw
  227.  
  228.     stosw 
  229.     IF_W_WRITE_in_CX_const_BX_BP_ES %eth_&name&_interface
  230. ENDM
  231.  
  232.  
  233.  
  234. ;;******************************************************************************
  235. ;;   ETH_IS_BROADCAST_in_BX_ES name
  236. ;;      ETH_IS_BROADCAST_in_BX_ES determines if the packet pointed to
  237. ;;      by BX:ES is a broadcast and sets the zero flag if it is NOT a 
  238. ;;      broadcast
  239. ;;
  240. ETH_IS_BROADCAST_in_BX_ES_const_AX_BX_CX_DX_BP_DI_ES MACRO name
  241.     .errb <name>
  242.  
  243.     mov SI, BX
  244.     sub SI, size ether
  245.     test byte ptr ES:[SI], 80H
  246. ENDM
  247.  
  248.  
  249. ;;******************************************************************************
  250. ;;   ETH_COPY_in_CX_SI_DI_ES_out_SI_DI interface
  251. ;;   ETH_COPY_in_CX_SI_DI_ES copies a packet from the input buffer (pointed 
  252. ;;      to by SI and the segement register given in IF_DECLARE) to an output 
  253. ;;      buffer (pointed to by DI and dest_reg) of length CX.   It assumes the
  254. ;;      output buffer is contiguous.  (and the caller shouldn't care if the 
  255. ;;      input buffer is contiguous)  COPY updates the pointers SI and DI
  256. ;;      to the end of the packet, and COPY could be called again if CX is not
  257. ;;      the total packet length (Note that CX MUST be even if you care about
  258. ;;      SI, and DI being updated properly)
  259. ;;
  260. ETH_COPY_in_CX_SI_DI_ES_out_SI_DI_const_BX_BP_ES MACRO name
  261.     .errb <name>
  262.     IF_COPY_in_CX_SI_DI_ES_out_SI_DI_const_BX_BP_ES %eth_&name&_interface
  263. ENDM
  264.  
  265.  
  266. ;;******************************************************************************
  267. ;;   ETH_TASK is the read task that reads the next packet from the interface
  268. ;;   and dispatches it to the next higher protocol layer
  269. ;;
  270. ETH_TASK MACRO name
  271.     local loop, found, done
  272.     .errb <name>
  273.  
  274.     IF_R_ACCESS_out_BX_CX_ES %eth_&name&_interface, done
  275.     mov AX, word ptr ES:[BX+ether_type]
  276.     add BX, size ether
  277.     sub CX, size ether
  278.  
  279.         ;; jump to proper routine
  280.     mov DX, word ptr eth_&name&_data.eth_read_cnt
  281.     inc DX
  282.     mov SI, offset eth_&name&_data.eth_read_jmp
  283.  
  284.     loop:
  285.     dec DX
  286.     jz eth_&name&_continue
  287.     cmp word ptr [SI+eth_read_type], AX     ;; compare type number
  288.     je found
  289.     add SI, (size eth_jmp_entry)
  290.     jmp loop
  291.  
  292.     found:
  293.     jmp word ptr [SI+eth_read_label]
  294.  
  295.     eth_&name&_continue:
  296.         IF_R_FREE_const_BX_CX_BP_SI_DI_ES %eth_&name&_interface
  297.  
  298.     done:
  299.     TASK_RETURN %eth_&name&_task
  300. ENDM
  301.  
  302.