home *** CD-ROM | disk | FTP | other *** search
/ PC Press: Internet / PC_PRESS.ISO / software / dos / misc / inar-100.exe / SRC / PCROUTE / ICMP.INC < prev    next >
Encoding:
Text File  |  1995-05-21  |  8.5 KB  |  282 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. ICMP_CANT_FRAGMENT  = 4
  46.  
  47. ICMP_REDIRECT_NET        = 0
  48. ICMP_REDIRECT_HOST       = 1
  49. ICMP_REDIRECT_TOS_NET    = 2
  50. ICMP_REDIRECT_TOS_HOST   = 3
  51.  
  52. icmp STRUC           ;; icmp packets
  53.     icmp_type       DB ?
  54.     icmp_code       DB ?
  55.     icmp_check      DW ?
  56.     icmp_id         DW ?
  57.     icmp_seq        DW ?
  58. icmp ENDS
  59.  
  60. icmp_data   STRUC
  61.     icmp_read_off   DW ?
  62.     icmp_read_seg   DW ?
  63.     icmp_read_len   DW ?
  64.     icmp_error_off  DW ?
  65.     icmp_error_seg  DW ?
  66.     icmp_gateway    DD ?
  67. icmp_data   ENDS
  68.  
  69.  
  70. ;;*****************************************************************************
  71. ;;   ICMP_DECLARE name, net, router
  72. ;; 
  73. ICMP_DECLARE MACRO name, net, router
  74.     .errb <router>
  75.  
  76.     .DATA
  77.     icmp_&name&_net = net
  78.     icmp_&name&_router = router
  79.     global icmp_&name&_data:icmp_data
  80.     .CODE
  81. ENDM
  82.  
  83. ;;*****************************************************************************
  84. ;;   ICMP_DEFINE name
  85. ;;      ARP_DEFINE declare all the things that have to be defined in
  86. ;;      every independantly assembled module.  DL_IP declares those
  87. ;;      things that need be be done only once (in particular memory allocation
  88. ;;      and initialzation code).  
  89. ;; 
  90. ICMP_DEFINE MACRO name
  91.     local icmp_packet, around
  92.     .errb <name>
  93.  
  94.     .DATA
  95.     icmp_&name&_data icmp_data <>
  96.  
  97.     .CODE
  98.     jmp around                                  ;; set up the listener
  99.         icmp_packet:
  100.             ICMP_PACKET_in_BX_ES name
  101.             RET
  102.     around:
  103.     IP_R_READ %icmp_&name&_net, ICMP_PROTO, icmp_packet
  104. ENDM
  105.  
  106.  
  107. ;;*****************************************************************************
  108. ;;      ICMP_REDIRECT_in_AX_BX_SI_ES name, code
  109. ;;  ICMP_REDIRECT sends a redirect with the ICMP code of 'code' to the host 
  110. ;;  sending the IP packet SI:ES instructing this host to redirect packets
  111. ;;  to the gateway AX,BX
  112. ;;
  113. ICMP_REDIRECT_in_AX_BX_SI_ES MACRO name, code, ip
  114.     local done
  115.     .errb <ip>
  116.  
  117.     ICMP_DO_REPLY_in_AX_BX_SI_ES name, ICMP_REDIRECT, code, ip
  118. ENDM
  119.  
  120.  
  121.  
  122. ;;*****************************************************************************
  123. ;; ICMP_ERROR_in_SI_ES returns an error code of type 'type' and code 'code'
  124. ;; to the sender of the IP packet pointed to by SI:ES
  125. ;;
  126. ICMP_ERROR_in_SI_ES MACRO name, type, code, ip
  127.     local done
  128.     .errb <ip>
  129.  
  130.     xor AX, AX
  131.     xor BX, BX
  132.     ICMP_DO_REPLY_in_AX_BX_SI_ES name, type, code, ip
  133. ENDM
  134.  
  135.  
  136.  
  137. ;;*****************************************************************************
  138. ;; ICMP_DO_REPLY returns an error code of type 'type' and code 'code'
  139. ;; to the sender of the IP packet pointed to by SI:ES with the gatway part
  140. ;; of the header set to AX,BX
  141. ;;
  142. ICMP_DO_REPLY_in_AX_BX_SI_ES MACRO name, type, code, ip
  143.     local done
  144.     .errb <ip>
  145.  
  146.     cmp byte ptr ES:[SI+ip_proto], ICMP_PROTO       ;; no errors about ICMP
  147.     jz done
  148.     test word ptr ES:[SI+ip_frag], SWAPPED_IP_FRAG_OFFSET  ;; errors only for
  149.     jnz done                                               ;; fragment zero
  150.         mov word ptr icmp_&name&_data.icmp_gateway,   AX
  151.         mov word ptr icmp_&name&_data.icmp_gateway+2, BX
  152.  
  153.         mov word ptr icmp_&name&_data.icmp_error_off, SI
  154.         mov word ptr icmp_&name&_data.icmp_error_seg, ES
  155.  
  156.         mov AX, word ptr ES:[SI+ip_src]
  157.         mov BX, word ptr ES:[SI+ip_src+2]
  158.  
  159.         mov DL, ICMP_PROTO
  160.         mov CX, 8+60+8
  161.         IP_W_ACCESS_in_AX_BX_CX_DL_out_AX_DI_ES ip
  162.         or AX, AX
  163.         jnz done            ;; check return code
  164.         mov BP, DI          ;; save DI
  165.         
  166.         mov AL, type        ;; fill in the header
  167.         mov AH, code
  168.         stosw
  169.  
  170.         xor AX, AX
  171.         stosw               ;; the checksum
  172.  
  173.         mov AX, word ptr icmp_&name&_data.icmp_gateway
  174.         stosw
  175.         mov AX, word ptr icmp_&name&_data.icmp_gateway+2
  176.         stosw
  177.  
  178.         mov BX, DS                                  ;; save DS
  179.         lds SI, dword ptr icmp_&name&_data.icmp_error_off
  180.  
  181.         mov CX, 28
  182.         mov DX, CX                                  ;; save CX
  183.         rep                                         ;; copy the packet
  184.         movsb
  185.         mov DS, BX                                  ;; restore DS
  186.  
  187.         add DX, 8
  188.         mov CX, DX                                  ;; restore CX
  189.         mov SI, BP          ;; restore packet pointer
  190.         ICMP_COMPUTE_CHECK_in_CX_SI_ES_out_BX_const_DX_BP_DI_ES 
  191.         mov DI, BP
  192.         mov word ptr ES:[DI+icmp_check], BX
  193.  
  194.         mov CX, DX
  195.         IP_W_WRITE_in_CX ip
  196.     done:
  197. ENDM
  198.  
  199.  
  200. ;;*****************************************************************************
  201. ICMP_COMPUTE_CHECK_in_CX_SI_ES_out_BX_const_DX_BP_DI_ES MACRO
  202.     local check_loop, done
  203.  
  204.     shr CX, 1
  205.     pushf
  206.     xor BX, BX              ;; also clears carry
  207.     check_loop:
  208.         seges
  209.         lodsw
  210.         adc BX, AX
  211.     loop check_loop
  212.     adc BX, 0               ;; add in the last carry if any
  213.     popf
  214.     jnc done
  215.         xor AX, AX          ;; odd number of bytes
  216.         mov AL, byte ptr ES:[SI]
  217.         add BX, AX
  218.         adc BX, 0           ;; add in the last carry if any
  219.     done:
  220.     not BX
  221. ENDM
  222.  
  223.  
  224.  
  225.  
  226. ;;*****************************************************************************
  227. ;; ICMP_PACKET processes the ICMP packet that starts at BX:ES (this is the
  228. ;; start if the IP part of the packet
  229. ;;
  230. ICMP_PACKET_in_BX_ES MACRO name
  231.     local not_ping, done
  232.     .errb <name>
  233.  
  234.     mov word ptr icmp_&name&_data.icmp_read_off, BX
  235.     mov word ptr icmp_&name&_data.icmp_read_seg, ES
  236.     mov word ptr icmp_&name&_data.icmp_read_len, CX
  237.  
  238.     cmp byte ptr ES:[BX+icmp_type], ICMP_ECHO_REQUEST
  239.     jnz not_ping
  240.         mov BP, BX                          ;; save BX
  241.         mov DX, word ptr ES:[BX+icmp_check] ;; prepare for checksum calculation
  242.         mov word ptr ES:[BX+icmp_check], 0
  243.         mov SI, BX
  244.         ICMP_COMPUTE_CHECK_in_CX_SI_ES_out_BX_const_DX_BP_DI_ES
  245.         cmp DX, BX
  246.         jne done
  247.         mov word ptr ES:[BP+icmp_check], DX ;; restore checksum
  248.  
  249.         IP_R_SRC_in_ES_out_AX_BX_const_CX_DX_BP_SI_DI_ES %icmp_&name&_net
  250.         mov CX, word ptr icmp_&name&_data.icmp_read_len
  251.         mov DL, ICMP_PROTO
  252.         IP_W_ACCESS_in_AX_BX_CX_DL_out_AX_DI_ES %icmp_&name&_net
  253.         or AX, AX
  254.         jnz done            ;; check return code
  255.         
  256.         mov DX, DS                                  ;; save DS
  257.         mov CX, word ptr icmp_&name&_data.icmp_read_len
  258.         lds SI, dword ptr icmp_&name&_data.icmp_read_off
  259.  
  260.         mov BX, CX                                  ;; save CX
  261.  
  262.         mov [SI+icmp_type], ICMP_ECHO_REPLY 
  263.         mov AX, [SI+icmp_check]             ;; fix up ICMP checksum
  264.         add AX, (ICMP_ECHO_REQUEST - ICMP_ECHO_REPLY) 
  265.         adc AX, 0
  266.         mov [SI+icmp_check], AX
  267.  
  268.         rep                                         ;; copy the ping
  269.         movsb
  270.  
  271.         mov DS, DX                                  ;; restore DS
  272.         mov CX, BX                                  ;; restore CX
  273.         mov ES, word ptr icmp_&name&_data.icmp_read_seg
  274.         IP_R_DST_in_ES_out_AX_BX_const_CX_DX_BP_SI_DI_ES %icmp_&name&_net
  275.         IP_SET_SRC_in_AX_BX_const_AX_BX_CX_DX_BP_SI_DI_ES %icmp_&name&_net
  276.         IP_W_WRITE_in_CX %icmp_&name&_net
  277.         jmp done
  278.     not_ping:
  279.     done:
  280. ENDM
  281.  
  282.