home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / p / pcrte224.zip / SOURCE.ZIP / UDP.INC < prev    next >
Text File  |  1992-06-09  |  8KB  |  246 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_data STRUC
  43.     udp_read_jmp    DW 256 DUP (0)
  44. udp_data ENDS
  45.  
  46. udp_sock_data STRUC
  47.     udp_write_off   DW ?
  48.     udp_write_seg   DW ?
  49.     udp_write_dst   DW ?
  50. udp_sock_data ENDS
  51.  
  52.  
  53. ;;******************************************************************************
  54. ;;   UDP_DECLARE   name, net, icmp
  55. ;;      UDP_DECLARE declares 
  56. ;;
  57. UDP_DECLARE MACRO name, net, icmp
  58.     .errb <icmp>
  59.  
  60.     .DATA
  61.     udp_&name&_net = net
  62.     udp_&name&_icmp = icmp
  63.     global udp_&name&_data:udp_data
  64.     .CODE
  65. ENDM
  66.  
  67.  
  68. ;;*****************************************************************************
  69. ;;   UDP_DEFINE name
  70. ;;       UDP_DECLARE 
  71. ;;
  72. UDP_DEFINE MACRO name
  73.     local around, udp_read, udp_read_drop, skip
  74.     .errb <name>
  75.  
  76.     .DATA
  77.     udp_&name&_data udp_data <>
  78.  
  79.     .CODE
  80.     jmp around
  81.         udp_read:
  82.         UDP_PACKET_in_AX_BX_CX_ES name 
  83.         RET
  84.  
  85.         udp_read_drop:
  86.             mov SI, BX
  87.             sub SI, size udp
  88.             IP_R_BROAD_in_SI_ES_const_CX_DX_BP_SI_DI_ES %udp_&name&_net, skip
  89.  
  90.             IP_R_HEADER_in_ES_out_SI_const_BX_CX_DX_BP_DI_ES %udp_&name&_net
  91.             ICMP_ERROR_in_SI_ES %udp_&name&_icmp, ICMP_UNREACHABLE, ICMP_UNREACH_PORT, %udp_&name&_net
  92.             skip:
  93.         RET
  94.     around:
  95.     mov AX, DS
  96.     mov ES, AX
  97.     mov AX, offset udp_read_drop
  98.     mov DI, offset udp_&name&_data.udp_read_jmp
  99.     mov CX, 256
  100.     rep
  101.     stosw 
  102.  
  103.     IP_R_READ %udp_&name&_net, UDP_PROTO, udp_read
  104. ENDM
  105.  
  106.  
  107. ;;*****************************************************************************
  108. UDP_SOCK_DECLARE MACRO name, udp, port
  109.    .errb <port>
  110.  
  111.     .DATA
  112.     udp_sock_&name&_udp = udp
  113.     udp_sock_&name&_port = ((port mod 256)*256 + (port/256))
  114.     udp_sock_&name&_net = udp_&udp&_net
  115.     global udp_sock_&name&_data:udp_sock_data 
  116.     .CODE
  117. ENDM
  118.  
  119.  
  120. ;;*****************************************************************************
  121. ;;
  122. UDP_SOCK_DEFINE MACRO name, code_label
  123.    .errb <code_label>
  124.  
  125.     .DATA
  126.     udp_sock_&name&_data udp_sock_data <>
  127.  
  128.     .CODE
  129.     UDP_R_READ %udp_sock_&name&_udp, %udp_sock_&name&_port, code_label
  130. ENDM
  131.  
  132.  
  133. ;;*****************************************************************************
  134. ;; UDP_R_SRC_in_SI_ES_out_AX_BX_CX name
  135. ;;      UDP_R_SRC returns the source port of the UDP data packet pointed to 
  136. ;;      by SI:ES that was given to the UDP_R_READ routine.  The IP address 
  137. ;;      is put in AX,BX and the port number in CX
  138. ;;
  139. UDP_SOCK_R_SRC_in_SI_ES_out_AX_BX_CX_const_DX_BP_SI_DI_ES MACRO name
  140.    .errb <name>
  141.  
  142.     sub SI, (size udp)
  143.     IP_R_SRC_in_SI_ES_out_AX_BX_const_CX_DX_BP_SI_DI_ES %udp_sock_&name&_net
  144.     mov CX, word ptr ES:[SI+udp_src]
  145.     xchg CH, CL
  146.     add SI, (size udp)
  147. ENDM
  148.  
  149.  
  150. ;;*****************************************************************************
  151. ;; UDP_SOCK_W_ACCESS_in_AX_BX_CX_DX_out_AX_DI_ES name
  152. ;;      UDP_SOCK_W_ACCESS retrieves a write buffer for a UDP packet data.
  153. ;;      AX,BX holds the destination IP address of the packet and DX holds
  154. ;;      the destination port for the packet.   The output buffer is returned
  155. ;;      in DI:ES.  UDP_W_ACCESS returns a status code in AX that is 0 if
  156. ;;      the access was successful  If the length is greater than the MTU
  157. ;;      of the interface this routine fails.  (thus the maximum guarenteed
  158. ;;      length is dl_ip_min_mtu)
  159. ;;
  160. UDP_SOCK_W_ACCESS_in_AX_BX_CX_DX_out_AX_DI_ES MACRO name
  161.     .errb <fail>
  162.  
  163.     xchg DH, DL
  164.     mov udp_sock_&name&_data.udp_write_dst, DX      ;; save the destination
  165.     mov DL, UDP_PROTO
  166.     add CX, size udp
  167.     IP_W_ACCESS_in_AX_BX_CX_DL_out_AX_DI_ES %udp_sock_&name&_net
  168.     mov udp_sock_&name&_data.udp_write_off, DI
  169.     mov udp_sock_&name&_data.udp_write_seg, ES
  170.     add DI, size udp
  171. ENDM
  172.  
  173.  
  174. ;;*****************************************************************************
  175. ;; UDP_SOCK_W_WRITE_in_CX name
  176. ;;      UDP_SOCK_W_WRITE tells the UDP interface to send the PACKET that is been
  177. ;;      loaded into the buffer DI:ES.  The length of the packet is in CX
  178. ;;      note that this UDP level does NOT support fragmentation, so CX better
  179. ;;      be less than the MTU of the interface (~1500 for ethernet)
  180. ;;
  181. UDP_SOCK_W_WRITE_in_CX MACRO name
  182.     .errb <name>
  183.  
  184.     les DI, dword ptr udp_sock_&name&_data.udp_write_off
  185.     add CX, size udp
  186.     xchg CH, CL
  187.     mov ES:[DI+udp_length], CX
  188.     xchg CH, CL
  189.     mov word ptr ES:[DI+udp_check], 0
  190.     mov AX, udp_sock_&name&_data.udp_write_dst
  191.     mov ES:[DI+udp_dst], AX
  192.     mov AX, udp_sock_&name&_port
  193.     mov ES:[DI+udp_src], AX
  194.  
  195.     IP_W_WRITE_in_CX %udp_sock_&name&_net
  196. ENDM
  197.  
  198.  
  199. ;;*****************************************************************************
  200. ;; UDP_R_READ name, port, code_label
  201. ;;      UDP_READ declares that the code starting at 'code_label' should
  202. ;;      be called when a UDP packet for port 'port' is read in
  203. ;;      The data in the UDP packet is passed to the object in BX:ES the 
  204. ;;      port number in AX   and the length of the data in CX. 
  205. ;;      If the source address is requires UDP_R_SRC should be called
  206. ;;
  207. UDP_R_READ MACRO name, port, code_label
  208.    .errb <code_label>
  209.  
  210.     mov word ptr udp_&name&_data.udp_read_jmp+2*((port mod 256) xor (port / 256)), offset code_label
  211. ENDM
  212.  
  213.  
  214. ;;*****************************************************************************
  215. ;; UDP_PACKET_in_AX_BX_CX_ES name
  216. ;;      UDP_PACKET_in_BX_ES does all the proessing of a packet that is destined
  217. ;;      for this node.  BX:ES points to the begining of the data in the UDP 
  218. ;;      packet.  CX holds the length.  AX hold the port number.
  219. ;;      Basicly this routine just dispatches it to the proper READ routine.  
  220. ;;
  221. UDP_PACKET_in_AX_BX_CX_ES MACRO name
  222.     local continue
  223.  
  224.     cmp AL, UDP_PROTO
  225.     jz continue
  226.         ret
  227.     continue:
  228.     mov AX, word ptr ES:[BX+udp_dst]        ;; dest_socket
  229.     mov CX, word ptr ES:[BX+udp_length]     ;; load length
  230.     xchg CH, CL
  231.     sub CX, size udp
  232.     add BX, size udp
  233.  
  234.     xor DX, DX
  235.     mov DL, AL                              ;; jump to proper routine
  236.     xor DL, AH
  237.     shl DX, 1
  238.     mov SI, offset  udp_&name&_data.udp_read_jmp
  239.     add SI, DX
  240.     jmp [SI]                                ;; Note this is a jump, thus
  241.                                             ;; when the callie returns it
  242.                                             ;; will return to the caller of
  243.                                             ;; this routine
  244. ENDM
  245.  
  246.