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 / ICMP.INC < prev    next >
Text File  |  1992-06-09  |  8KB  |  258 lines

  1. ;;************************************************************************* 
  2. ;                         icmp.inc       icmp.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 package
  19. ;;
  20. ;;      ICMP_DECLARE name, net, router
  21. ;;      ICMP_DEFINE name
  22. ;;      ICMP_ERROR_in_SI_ES name, type, code, ip
  23. ;;      ICMP_REDIRECT_in_AX_BX_SI_ES name, code, ip
  24. ;;  
  25. ;; AUTHOR: Vance Morrison
  26. ;; DATE:  5/9/89
  27. ;;*****************************************************************************
  28.  
  29. ICMP_PROTO          = 1         
  30.  
  31. ;; values for the type field
  32. ICMP_ECHO_REPLY     = 0
  33. ICMP_ECHO_REQUEST   = 8
  34. ICMP_TIME           = 11
  35. ICMP_UNREACHABLE    = 3
  36. ICMP_REDIRECT       = 5
  37.  
  38. ;; values for the code field
  39. ICMP_TIME_TTL       = 0
  40.  
  41. ICMP_UNREACH_NET    = 0
  42. ICMP_UNREACH_HOST   = 1
  43. ICMP_UNREACH_PROTO  = 2
  44. ICMP_UNREACH_PORT   = 3
  45.  
  46. ICMP_REDIRECT_NET        = 0
  47. ICMP_REDIRECT_HOST       = 1
  48. ICMP_REDIRECT_TOS_NET    = 2
  49. ICMP_REDIRECT_TOS_HOST   = 3
  50.  
  51. icmp STRUC           ;; icmp packets
  52.     icmp_type       DB ?
  53.     icmp_code       DB ?
  54.     icmp_check      DW ?
  55.     icmp_id         DW ?
  56.     icmp_seq        DW ?
  57. icmp ENDS
  58.  
  59. icmp_data   STRUC
  60.     icmp_read_off   DW ?
  61.     icmp_read_seg   DW ?
  62.     icmp_read_len   DW ?
  63.     icmp_error_off  DW ?
  64.     icmp_error_seg  DW ?
  65.     icmp_gateway    DD ?
  66. icmp_data   ENDS
  67.  
  68.  
  69. ;;*****************************************************************************
  70. ;;   ICMP_DECLARE name, net, router
  71. ;; 
  72. ICMP_DECLARE MACRO name, net, router
  73.     .errb <router>
  74.  
  75.     .DATA
  76.     icmp_&name&_net = net
  77.     icmp_&name&_router = router
  78.     global icmp_&name&_data:icmp_data
  79.     .CODE
  80. ENDM
  81.  
  82. ;;*****************************************************************************
  83. ;;   ICMP_DEFINE name
  84. ;;      ARP_DEFINE declare all the things that have to be defined in
  85. ;;      every independantly assembled module.  DL_IP declares those
  86. ;;      things that need be be done only once (in particular memory allocation
  87. ;;      and initialzation code).  
  88. ;; 
  89. ICMP_DEFINE MACRO name
  90.     local icmp_packet, around
  91.     .errb <name>
  92.  
  93.     .DATA
  94.     icmp_&name&_data icmp_data <>
  95.  
  96.     .CODE
  97.     jmp around                                  ;; set up the listener
  98.         icmp_packet:
  99.             ICMP_PACKET_in_BX_ES name
  100.             RET
  101.     around:
  102.     IP_R_READ %icmp_&name&_net, ICMP_PROTO, icmp_packet
  103. ENDM
  104.  
  105.  
  106. ;;*****************************************************************************
  107. ;;      ICMP_REDIRECT_in_AX_BX_SI_ES name, code
  108. ;;  ICMP_REDIRECT sends a redirect with the ICMP code of 'code' to the host 
  109. ;;  sending the IP packet SI:ES instructing this host to redirect packets
  110. ;;  to the gateway AX,BX
  111. ;;
  112. ICMP_REDIRECT_in_AX_BX_SI_ES MACRO name, code, ip
  113.     local done
  114.     .errb <ip>
  115.  
  116.     ICMP_DO_REPLY_in_AX_BX_SI_ES name, ICMP_REDIRECT, code, ip
  117. ENDM
  118.  
  119.  
  120.  
  121. ;;*****************************************************************************
  122. ;; ICMP_ERROR_in_SI_ES returns an error code of type 'type' and code 'code'
  123. ;; to the sender of the IP packet pointed to by SI:ES
  124. ;;
  125. ICMP_ERROR_in_SI_ES MACRO name, type, code, ip
  126.     local done
  127.     .errb <ip>
  128.  
  129.     xor AX, AX
  130.     xor BX, BX
  131.     ICMP_DO_REPLY_in_AX_BX_SI_ES name, type, code, ip
  132. ENDM
  133.  
  134.  
  135.  
  136. ;;*****************************************************************************
  137. ;; ICMP_DO_REPLY returns an error code of type 'type' and code 'code'
  138. ;; to the sender of the IP packet pointed to by SI:ES with the gatway part
  139. ;; of the header set to AX,BX
  140. ;;
  141. ICMP_DO_REPLY_in_AX_BX_SI_ES MACRO name, type, code, ip
  142.     local done
  143.     .errb <ip>
  144.  
  145.     cmp byte ptr ES:[SI+ip_proto], ICMP_PROTO       ;; no errors about ICMP
  146.     jz done
  147.         mov word ptr icmp_&name&_data.icmp_gateway,   AX
  148.         mov word ptr icmp_&name&_data.icmp_gateway+2, BX
  149.  
  150.         mov word ptr icmp_&name&_data.icmp_error_off, SI
  151.         mov word ptr icmp_&name&_data.icmp_error_seg, ES
  152.  
  153.         mov AX, word ptr ES:[SI+ip_src]
  154.         mov BX, word ptr ES:[SI+ip_src+2]
  155.  
  156.         mov DL, ICMP_PROTO
  157.         mov CX, 8+60+8
  158.         IP_W_ACCESS_in_AX_BX_CX_DL_out_AX_DI_ES ip
  159.         or AX, AX
  160.         jnz done            ;; check return code
  161.         mov BP, DI          ;; save DI
  162.         
  163.         mov AL, type        ;; fill in the header
  164.         mov AH, code
  165.         stosw
  166.  
  167.         xor AX, AX
  168.         stosw               ;; the checksum
  169.  
  170.         mov AX, word ptr icmp_&name&_data.icmp_gateway
  171.         stosw
  172.         mov AX, word ptr icmp_&name&_data.icmp_gateway+2
  173.         stosw
  174.  
  175.         mov BX, DS                                  ;; save DS
  176.         lds SI, dword ptr icmp_&name&_data.icmp_error_off
  177.  
  178.         mov CX, 28
  179.         mov DX, CX                                  ;; save CX
  180.         rep                                         ;; copy the packet
  181.         movsb
  182.         mov DS, BX                                  ;; restore DS
  183.  
  184.         add DX, 8
  185.         mov CX, DX                                  ;; restore CX
  186.         mov SI, BP          ;; restore packet pointer
  187.         ICMP_COMPUTE_CHECK_in_CX_SI_ES_out_BX_const_DX_BP_DI_ES 
  188.         mov DI, BP
  189.         mov word ptr ES:[DI+icmp_check], BX
  190.  
  191.         mov CX, DX
  192.         IP_W_WRITE_in_CX ip
  193.     done:
  194. ENDM
  195.  
  196.  
  197. ;;*****************************************************************************
  198. ICMP_COMPUTE_CHECK_in_CX_SI_ES_out_BX_const_DX_BP_DI_ES MACRO
  199.     local check_loop
  200.  
  201.     xor BX, BX              ;; also clears carry
  202.     shr CX, 1
  203.     check_loop:
  204.         seges
  205.         lodsw
  206.         adc BX, AX
  207.     loop check_loop
  208.     adc BX, 0               ;; add in the last carry if any
  209.     not BX
  210. ENDM
  211.  
  212.  
  213.  
  214.  
  215. ;;*****************************************************************************
  216. ;; ICMP_PACKET processes the ICMP packet that starts at BX:ES (this is the
  217. ;; start if the IP part of the packet
  218. ;;
  219. ICMP_PACKET_in_BX_ES MACRO name
  220.     local not_ping, done
  221.     .errb <name>
  222.  
  223.     mov word ptr icmp_&name&_data.icmp_read_off, BX
  224.     mov word ptr icmp_&name&_data.icmp_read_seg, ES
  225.     mov word ptr icmp_&name&_data.icmp_read_len, CX
  226.     mov SI, BX
  227.     cmp byte ptr ES:[SI+icmp_type], ICMP_ECHO_REQUEST
  228.     jnz not_ping
  229.         IP_R_SRC_in_SI_ES_out_AX_BX_const_CX_DX_BP_SI_DI_ES %icmp_&name&_net
  230.         mov DL, ICMP_PROTO
  231.         IP_W_ACCESS_in_AX_BX_CX_DL_out_AX_DI_ES %icmp_&name&_net
  232.         or AX, AX
  233.         jnz done            ;; check return code
  234.         
  235.         mov DX, DS                                  ;; save DS
  236.         mov CX, word ptr icmp_&name&_data.icmp_read_len
  237.         lds SI, dword ptr icmp_&name&_data.icmp_read_off
  238.  
  239.         mov BX, CX                                  ;; save CX
  240.  
  241.         mov [SI+icmp_type], ICMP_ECHO_REPLY 
  242.         mov AX, [SI+icmp_check]             ;; fix up ICMP checksum
  243.         add AX, (ICMP_ECHO_REQUEST - ICMP_ECHO_REPLY) 
  244.         adc AX, 0
  245.         mov [SI+icmp_check], AX
  246.  
  247.         rep                                         ;; copy the ping
  248.         movsb
  249.  
  250.         mov DS, DX                                  ;; restore DS
  251.         mov CX, BX                                  ;; restore CX
  252.         IP_W_WRITE_in_CX %icmp_&name&_net
  253.         jmp done
  254.     not_ping:
  255.     done:
  256. ENDM
  257.  
  258.