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

  1. ;;************************************************************************* 
  2. ;;                         udp.inc       udp.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. ;; Routines provided by this module
  19. ;;
  20. ;;   UDP_DECLARE name, net, icmp
  21. ;;   UDP_DEFINE name
  22. ;;   UDP_SOCK_DEFINE name, udp, port, code_label
  23. ;;
  24. ;;  Variables Provided by this Module (READ ONLY!!!)
  25. ;;      
  26. ;;
  27. ;;*****************************************************************************
  28. ;; definition of User datagram prococol packet header structure
  29. ;;
  30. udp            STRUC
  31.     udp_src       DW ?
  32.     udp_dst       DW ?
  33.     udp_length    DW ?
  34.     udp_check     DW ?
  35. udp            ENDS
  36.  
  37. UDP_PROTO = 17
  38.  
  39. ;;****************************************************************************
  40. ;; data needed by this module
  41. ;;
  42. udp_jmp_entry STRUC
  43.     udp_read_port   DW ?
  44.     udp_read_label  DW ?
  45. udp_jmp_entry ENDS
  46.  
  47. udp_data STRUC
  48.     udp_read_cnt    DW 0
  49.     udp_read_jmp    udp_jmp_entry 32 DUP (<>)
  50. udp_data ENDS
  51.  
  52. udp_sock_data STRUC
  53.     udp_write_off   DW ?
  54.     udp_write_seg   DW ?
  55.     udp_write_dst   DW ?
  56. udp_sock_data ENDS
  57.  
  58.  
  59. ;;******************************************************************************
  60. ;;   UDP_DECLARE   name, net, icmp
  61. ;;      UDP_DECLARE declares 
  62. ;;
  63. UDP_DECLARE MACRO name, net, icmp
  64.     .errb <icmp>
  65.  
  66.     .DATA
  67.     udp_&name&_net = net
  68.     udp_&name&_icmp = icmp
  69.     global udp_&name&_data:udp_data
  70.     .CODE
  71.     global udp_&name&_read_drop:near
  72. ENDM
  73.  
  74.  
  75. ;;*****************************************************************************
  76. ;;   UDP_DEFINE name
  77. ;;       UDP_DECLARE 
  78. ;;
  79. UDP_DEFINE MACRO name
  80.     local around, udp_read, skip
  81.     .errb <name>
  82.  
  83.     .DATA
  84.     udp_&name&_data udp_data <>
  85.  
  86.     .CODE
  87.     jmp around
  88.         udp_read:
  89.         UDP_PACKET_in_AX_BX_CX_ES name 
  90.         RET
  91.  
  92.         udp_&name&_read_drop:
  93.             IP_R_BROAD_const_AX_BX_CX_DX_BP_SI_DI_ES %udp_&name&_net, skip
  94.             IP_R_HEADER_in_ES_out_SI_const_BX_CX_DX_BP_DI_ES %udp_&name&_net
  95.             ICMP_ERROR_in_SI_ES %udp_&name&_icmp, ICMP_UNREACHABLE, ICMP_UNREACH_PORT, %udp_&name&_net
  96.             skip:
  97.         RET
  98.     around:
  99.     IP_R_READ %udp_&name&_net, UDP_PROTO, udp_read
  100. ENDM
  101.  
  102.  
  103. ;;*****************************************************************************
  104. UDP_SOCK_DECLARE MACRO name, udp, port
  105.    .errb <port>
  106.  
  107.     .DATA
  108.     udp_sock_&name&_udp = udp
  109.     udp_sock_&name&_port = ((port mod 256)*256 + (port/256))
  110.     udp_sock_&name&_net = udp_&udp&_net
  111.     global udp_sock_&name&_data:udp_sock_data 
  112.     .CODE
  113. ENDM
  114.  
  115.  
  116. ;;*****************************************************************************
  117. ;;
  118. UDP_SOCK_DEFINE MACRO name, code_label
  119.    .errb <code_label>
  120.  
  121.     .DATA
  122.     udp_sock_&name&_data udp_sock_data <>
  123.  
  124.     .CODE
  125.     UDP_R_READ %udp_sock_&name&_udp, %udp_sock_&name&_port, code_label
  126. ENDM
  127.  
  128.  
  129. ;;*****************************************************************************
  130. ;; UDP_R_SRC_in_SI_ES_out_AX_BX_CX name
  131. ;;      UDP_R_SRC returns the source port of the UDP data packet pointed to 
  132. ;;      by SI:ES that was given to the UDP_R_READ routine.  The IP address 
  133. ;;      is put in AX,BX and the port number in CX
  134. ;;
  135. UDP_SOCK_R_SRC_in_SI_ES_out_AX_BX_CX_const_DX_BP_SI_DI_ES MACRO name
  136.    .errb <name>
  137.  
  138.     sub SI, (size udp)
  139.     IP_R_SRC_in_ES_out_AX_BX_const_CX_DX_BP_SI_DI_ES %udp_sock_&name&_net
  140.     mov CX, word ptr ES:[SI+udp_src]
  141.     xchg CH, CL
  142.     add SI, (size udp)
  143. ENDM
  144.  
  145.  
  146. ;;*****************************************************************************
  147. ;; UDP_SOCK_W_ACCESS_in_AX_BX_CX_DX_out_AX_DI_ES name
  148. ;;      UDP_SOCK_W_ACCESS retrieves a write buffer for a UDP packet data.
  149. ;;      AX,BX holds the destination IP address of the packet and DX holds
  150. ;;      the destination port for the packet.   The output buffer is returned
  151. ;;      in DI:ES.  UDP_W_ACCESS returns a status code in AX that is 0 if
  152. ;;      the access was successful  If the length is greater than the MTU
  153. ;;      of the interface this routine fails.  (thus the maximum guarenteed
  154. ;;      length is dl_ip_min_mtu)
  155. ;;
  156. UDP_SOCK_W_ACCESS_in_AX_BX_CX_DX_out_AX_DI_ES MACRO name
  157.     .errb <fail>
  158.  
  159.     xchg DH, DL
  160.     mov udp_sock_&name&_data.udp_write_dst, DX      ;; save the destination
  161.     mov DL, UDP_PROTO
  162.     add CX, size udp
  163.     IP_W_ACCESS_in_AX_BX_CX_DL_out_AX_DI_ES %udp_sock_&name&_net
  164.     mov udp_sock_&name&_data.udp_write_off, DI
  165.     mov udp_sock_&name&_data.udp_write_seg, ES
  166.     add DI, size udp
  167. ENDM
  168.  
  169.  
  170. ;;*****************************************************************************
  171. ;; UDP_SOCK_W_WRITE_in_CX name
  172. ;;      UDP_SOCK_W_WRITE tells the UDP interface to send the PACKET that is been
  173. ;;      loaded into the buffer DI:ES.  The length of the packet is in CX
  174. ;;      note that this UDP level does NOT support fragmentation, so CX better
  175. ;;      be less than the MTU of the interface (~1500 for ethernet)
  176. ;;
  177. UDP_SOCK_W_WRITE_in_CX MACRO name
  178.     .errb <name>
  179.  
  180.     les DI, dword ptr udp_sock_&name&_data.udp_write_off
  181.     add CX, size udp
  182.     xchg CH, CL
  183.     mov ES:[DI+udp_length], CX
  184.     xchg CH, CL
  185.     mov word ptr ES:[DI+udp_check], 0
  186.     mov AX, udp_sock_&name&_data.udp_write_dst
  187.     mov ES:[DI+udp_dst], AX
  188.     mov AX, udp_sock_&name&_port
  189.     mov ES:[DI+udp_src], AX
  190.  
  191.     mov DX, CX
  192.     xchg DH, DL
  193.     IP_GET_SRC_out_AX_BX_const_CX_DX_BP_SI_DI_ES %udp_sock_&name&_net
  194.     add DX, AX
  195.     adc DX, BX
  196.     IP_GET_DST_out_AX_BX_const_CX_DX_BP_SI_DI_ES %udp_sock_&name&_net
  197.     adc DX, AX
  198.     adc DX, BX
  199.     mov AL, 0
  200.     mov AH, UDP_PROTO
  201.     adc DX, AX
  202.     adc DX, 0                               ;; add in the last carry if any
  203.     mov SI, DI
  204.     mov BX, DX
  205.     mov DX, CX                              ;; save CX
  206.     UDP_COMPUTE_CHECK_in_BX_CX_SI_ES_out_BX_const_DX_BP_DI_ES
  207.     mov word ptr ES:[DI+udp_check], BX
  208.     mov CX, DX                              ;; restore CX
  209.  
  210.     IP_W_WRITE_in_CX %udp_sock_&name&_net
  211. ENDM
  212.  
  213.  
  214. ;;*****************************************************************************
  215. ;; UDP_R_READ name, port, code_label
  216. ;;      UDP_READ declares that the code starting at 'code_label' should
  217. ;;      be called when a UDP packet for port 'port' is read in
  218. ;;      The data in the UDP packet is passed to the object in BX:ES the 
  219. ;;      port number in AX   and the length of the data in CX. 
  220. ;;      If the source address is requires UDP_R_SRC should be called
  221. ;;
  222. UDP_R_READ MACRO name, port, code_label
  223.     local skip
  224.     .errb <code_label>
  225.  
  226.     push DX
  227.     push DI
  228.     mov DX, word ptr udp_&name&_data.udp_read_cnt
  229.     cmp DX, 32
  230.     jge skip
  231.     mov DI, DX
  232.     inc DX
  233.     mov word ptr udp_&name&_data.udp_read_cnt, DX
  234.     shl DI, 1
  235.     shl DI, 1
  236.     add DI, offset udp_&name&_data.udp_read_jmp
  237.     mov word ptr [DI+udp_read_port], port
  238.     mov word ptr [DI+udp_read_label], offset code_label
  239.     skip:
  240.     pop DI
  241.     pop DX
  242. ENDM
  243.  
  244.  
  245. ;;*****************************************************************************
  246. ;; UDP_PACKET_in_AX_BX_CX_ES name
  247. ;;      UDP_PACKET_in_BX_ES does all the proessing of a packet that is destined
  248. ;;      for this node.  BX:ES points to the begining of the data in the UDP 
  249. ;;      packet.  CX holds the length.  AX holds the protocol number.
  250. ;;      Basicly this routine just dispatches it to the proper READ routine.  
  251. ;;
  252. UDP_PACKET_in_AX_BX_CX_ES MACRO name
  253.     local continue, valid, no_checksum, loop, found, not_found
  254.  
  255.     cmp AL, UDP_PROTO
  256.     je continue
  257.         ret
  258.     continue:
  259.     mov DX, word ptr ES:[BX+udp_check]
  260.     or DX, DX
  261.     jz no_checksum
  262.     mov BP, BX                              ;; save BX
  263.     mov BX, AX
  264.     xchg BH, BL
  265.     IP_R_HEADER_in_ES_out_SI_const_BX_CX_DX_BP_DI_ES %udp_&name&_net
  266.     add BX, word ptr ES:[SI+ip_src]
  267.     adc BX, word ptr ES:[SI+ip_src+2]
  268.     adc BX, word ptr ES:[SI+ip_dst]
  269.     adc BX, word ptr ES:[SI+ip_dst+2]
  270.     xchg CH, CL
  271.     adc BX, CX
  272.     xchg CH, CL
  273.     adc BX, 0                               ;; add in the last carry if any
  274.     mov word ptr ES:[BP+udp_check], 0       ;; prepare for checksum calculation
  275.     mov SI, BP
  276.     UDP_COMPUTE_CHECK_in_BX_CX_SI_ES_out_BX_const_DX_BP_DI_ES
  277.     cmp DX, BX
  278.     je valid
  279.         ret
  280.     valid:
  281.     mov BX, BP
  282.     mov word ptr ES:[BX+udp_check], DX      ;; restore checksum
  283.  
  284.     no_checksum:
  285.     mov AX, word ptr ES:[BX+udp_dst]        ;; dest_socket
  286.     mov CX, word ptr ES:[BX+udp_length]     ;; load length
  287.     xchg CH, CL
  288.     sub CX, size udp
  289.     add BX, size udp
  290.  
  291.         ;; jump to proper routine
  292.     mov DX, word ptr udp_&name&_data.udp_read_cnt
  293.     inc DX
  294.     mov SI, offset udp_&name&_data.udp_read_jmp
  295.  
  296.     loop:
  297.     dec DX
  298.     jz not_found
  299.     cmp word ptr [SI+udp_read_port], AX     ;; compare port number
  300.     je found
  301.     add SI, (size udp_jmp_entry)
  302.     jmp loop
  303.  
  304.     not_found:
  305.     jmp udp_&name&_read_drop
  306.  
  307.     found:
  308.     jmp word ptr [SI+udp_read_label]        ;; Note this is a jump, thus
  309.                                             ;; when the callie returns it
  310.                                             ;; will return to the caller of
  311.                                             ;; this routine
  312. ENDM
  313.  
  314. ;;*****************************************************************************
  315. UDP_COMPUTE_CHECK_in_BX_CX_SI_ES_out_BX_const_DX_BP_DI_ES MACRO
  316.     local check_loop, done, not_zero
  317.  
  318.     shr CX, 1
  319.     pushf
  320.     clc
  321.     check_loop:
  322.         seges
  323.         lodsw
  324.         adc BX, AX
  325.     loop check_loop
  326.     adc BX, 0               ;; add in the last carry if any
  327.     popf
  328.     jnc done
  329.         xor AX, AX          ;; odd number of bytes
  330.         mov AL, byte ptr ES:[SI]
  331.         add BX, AX
  332.         adc BX, 0           ;; add in the last carry if any
  333.     done:
  334.     xor BX, 0FFFFH
  335.     jnz not_zero
  336.         not BX
  337.     not_zero:
  338. ENDM
  339.