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 / BOOTP.INC < prev    next >
Text File  |  1992-06-09  |  12KB  |  357 lines

  1. ;;************************************************************************* 
  2. ;;                         bootp.inc       bootp.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. ;; AUTHOR: Vance Morrison
  19. ;;****************************************************************************
  20. ;; definition of Bootp structure
  21.  
  22. BOOTPC = 44h                          ;; udp port for server responces 
  23. BOOTPS = 43h                          ;; udp port for client requests
  24.  
  25. ;;****************************************************************************
  26. ;; definition of BOOTP structure
  27.  
  28. BOOTP_REQUEST = 1
  29. BOOTP_REPLY = 2
  30.  
  31. MYID = 3845                                     ;; a unique ID
  32.  
  33. bootp           STRUC
  34.     bootp_op            DB 0                ;; operation
  35.     bootp_htype         DB 0                ;; hardware type (ethernet = 1)
  36.     bootp_hlen          DB 0                ;; hardware address len
  37.     bootp_hops          DB 0                ;; hop count (start at 0)
  38.     bootp_xid           DD 0                ;; random transaction id
  39.     bootp_secs          DW 0
  40.     bootp_unused        DW 0
  41.     bootp_ciaddr        DD 0                ;; client IP address
  42.     bootp_yiaddr        DD 0                ;; client IP address (servers reply)
  43.     bootp_siaddr        DD 0                ;; server IP address
  44.     bootp_giaddr        DD 0                ;; gateway IP address
  45.     bootp_chaddr        DB 16 DUP (0)       ;; client harware address
  46.     bootp_sname         DB 64 DUP (0)       ;; server name 
  47.     bootp_file          DB 128 DUP (0)      ;; file to boot from 
  48.     bootp_magic         DD 0                ;; magic number 
  49.     bootp_vendor        DB 60 DUP (0)       ;; vendor specific stuff
  50. bootp           ENDS
  51.  
  52.  
  53. ;;******************************************************************************
  54. ;;  BOOTP_DECLARE name, udp, dls, udp_sock1, udp_sock2
  55. ;;       BOOTP_DECLARE delcares a BOOTP listener called 'name'.  'udp'
  56. ;;       is a udp object, 'udp_sock1', 'udp_sock2' are two unique numbers
  57. ;;       that can but used to create sockets with the udp object.  The
  58. ;;       Data link objects are assumed to be 1..<dls>.
  59. ;;
  60. BOOTP_DECLARE   MACRO  name, udp, dls
  61.     .errb <dls>
  62.  
  63.     .DATA
  64.     bootp_&name&_udp = udp
  65.     bootp_&name&_dls = dls
  66.     bootp_&name&_my_ip = dl_ip_1_ip         ;; make equal to any of my addresses
  67.     bootp_&name&_request_sock = (name*100+1)
  68.     bootp_&name&_reply_sock = (name*100+2)
  69.     global bootp_&name&_forward:dword
  70.     .CODE
  71.  
  72.     UDP_SOCK_DECLARE %bootp_&name&_request_sock, %bootp_&name&_udp, BOOTPS
  73.     UDP_SOCK_DECLARE %bootp_&name&_reply_sock, %bootp_&name&_udp, BOOTPC
  74. ENDM
  75.  
  76.  
  77. ;;******************************************************************************
  78. ;;   BOOTP_DEFINE   my_ip, forward_host
  79. ;;       BOOTP_DEFINE sets aside the memory and acutally does the 
  80. ;;       initialization for the bootp object 'name'.  'forward_host'
  81. ;;       is the address of a IP address to forward any bootp request to.  
  82. ;;       This address can be a directed broadcast.  
  83. ;;
  84. BOOTP_DEFINE   MACRO   name, forward_host
  85.     local around, request_code, reply_code, drop, tftp_around, tftp_request
  86.     .errb <forward_host>
  87.  
  88.     .DATA
  89.     bootp_&name&_forward DD ?
  90.     bootp_&name&_last_xid DD ?
  91.  
  92.     .CODE
  93.     jmp around          ;; declare code objects
  94.         request_code:
  95.         reply_code:
  96.             BOOTP_REQUEST_PACKET_in_AX_BX_CX_ES name, drop
  97.             BOOTP_REPLY_PACKET_in_AX_BX_CX_ES name, drop
  98.             drop:
  99.             RET
  100.     around:
  101.  
  102.     ;; this code should be thrown out when CISCO do bootp properly
  103.     TFTP_PORT = 45H
  104.     jmp tftp_around
  105.         tftp_request:
  106.         TFTP_REQUEST_PACKET_in_AX_BX_CX_ES name, %myip
  107.         RET
  108.     tftp_around:
  109.     UDP_R_READ %bootp_&name&_udp, TFTP_PORT, tftp_request
  110.     ;; end of KLUDGE code (dont forget to trough out TFTP_REQUEST_PACKET too)
  111.  
  112.     cmp word ptr forward_host, 0
  113.     jz done
  114.     cmp word ptr forward_host+2, 0
  115.     jz done
  116.  
  117.         mov AX, word ptr forward_host               ;; save the forwarding host
  118.         mov word ptr bootp_&name&_forward, AX
  119.         mov AX, word ptr forward_host+2
  120.         mov word ptr bootp_&name&_forward+2, AX
  121.  
  122.         mov AX, 0
  123.         mov word ptr bootp_&name&_last_xid, AX
  124.         mov word ptr bootp_&name&_last_xid+2, AX
  125.  
  126.         UDP_SOCK_DEFINE %bootp_&name&_request_sock, request_code
  127.         UDP_SOCK_DEFINE %bootp_&name&_reply_sock, reply_code
  128.     done:
  129. ENDM
  130.  
  131.  
  132. ;;******************************************************************************
  133. ;;   BOOTP_REQUEST_PACKET   name
  134. ;;
  135. BOOTP_REQUEST_PACKET_in_AX_BX_CX_ES MACRO name, drop
  136.     local done
  137.     .errb <drop>
  138.  
  139.         ;; since bootp requests are often sent to a broadcast address,
  140.         ;; I am a bit paranoid about the packets I receive.  Basicly
  141.         ;; I ONLY forward requests from that poor sap that doesn't know
  142.         ;; his IP address.  Anyone else I assume can boot without my help 
  143.     mov SI, BX
  144.     cmp byte ptr ES:[SI+bootp_op], BOOTP_REQUEST
  145.     jnz done
  146.         mov AL, ES:[SI+bootp_hops]
  147.         cmp AL, 2
  148.         ja drop
  149.         inc AL
  150.         mov ES:[SI+bootp_hops], AL
  151.  
  152.         mov AX, word ptr ES:[SI+bootp_giaddr]       ;; is the gateway filled in
  153.         or AX, AX
  154.         jnz drop
  155.         mov AX, word ptr ES:[SI+bootp_giaddr+2]     ;; is the gateway filled in
  156.         or AX, AX
  157.         jnz drop
  158.  
  159.         mov AX, 0                                    ;; reset 
  160.         mov word ptr bootp_&name&_last_xid, AX
  161.         mov word ptr bootp_&name&_last_xid+2, AX
  162.  
  163.         mov AX, word ptr bootp_&name&_my_ip         ;; fill in me as gateway
  164.         mov word ptr ES:[SI+bootp_giaddr], AX
  165.         mov AX, word ptr bootp_&name&_my_ip+2
  166.         mov word ptr ES:[SI+bootp_giaddr+2], AX
  167.  
  168.         mov AX, word ptr bootp_&name&_forward
  169.         mov BX, word ptr bootp_&name&_forward+2
  170.         or AX, AX
  171.         jz drop
  172.         or BX, BX
  173.         jz drop
  174.         mov CX, size bootp
  175.         mov DX, BOOTPS
  176.  
  177.         push SI
  178.         push ES
  179.         UDP_SOCK_W_ACCESS_in_AX_BX_CX_DX_out_AX_DI_ES %bootp_&name&_reply_sock
  180.         pop  DX
  181.         pop  SI
  182.         or AX, AX
  183.         jnz drop
  184.  
  185.         mov BX, DS              ;; save DS
  186.         mov DS, DX
  187.         mov CX, size bootp
  188.         rep                     ;; copy the packet
  189.         movsb
  190.         mov DS, BX              ;; restore DS
  191.  
  192.         mov CX, size bootp
  193.         UDP_SOCK_W_WRITE_in_CX %bootp_&name&_reply_sock
  194.     done:
  195. ENDM
  196.  
  197.  
  198. ;;******************************************************************************
  199. ;;   BOOTP_REPLY_PACKET   name
  200. ;;
  201. BOOTP_REPLY_PACKET_in_AX_BX_CX_ES MACRO name, drop
  202.     local done, sendit, send_reply
  203.     .errb <drop>
  204.  
  205.     mov SI, BX
  206.     cmp ES:[SI+bootp_op], BOOTP_REPLY
  207.     jnz done
  208.         
  209.     mov AL, ES:[SI+bootp_hops]
  210.     cmp AL, 10
  211.     ja done
  212.     inc AL
  213.     mov ES:[SI+bootp_hops], AL
  214.  
  215.     mov AX, word ptr bootp_&name&_my_ip     ;; only reply if I am the designated
  216.     cmp word ptr ES:[SI+bootp_giaddr], AX    ;; gateway
  217.     jnz done
  218.     mov AX, word ptr bootp_&name&_my_ip+2
  219.     cmp word ptr ES:[SI+bootp_giaddr+2], AX
  220.     jnz done
  221.  
  222.     mov AX, word ptr bootp_&name&_last_xid    ;; dont send to same place twice
  223.     cmp word ptr ES:[SI+bootp_xid], AX    
  224.     jnz send_reply
  225.     mov AX, word ptr bootp_&name&_last_xid+2
  226.     cmp word ptr ES:[SI+bootp_xid+2], AX    
  227.     jz done
  228.  
  229.     send_reply:
  230.     mov AX, word ptr ES:[SI+bootp_xid]
  231.     mov word ptr bootp_&name&_last_xid, AX
  232.     mov AX, word ptr ES:[SI+bootp_xid+2]
  233.     mov word ptr bootp_&name&_last_xid+2, AX
  234.  
  235.     mov AX, word ptr ES:[SI+bootp_yiaddr]       ;; load up the destination
  236.     mov BX, word ptr ES:[SI+bootp_yiaddr+2]     
  237.  
  238.     IRP idx,<1,2,3,4,5,6,7,8>
  239.     if idx le bootp_&name&_dls
  240.         ;; send it out the right interface
  241.     BOOTP_GET_BROADCAST_DL_in_AX_BX_out_AX_BX_const_SI_ES name, idx, sendit
  242.     endif
  243.     endm
  244.     jmp drop
  245.  
  246.     sendit:
  247.     mov CX, size bootp
  248.     mov DX, BOOTPC
  249.  
  250.     push SI
  251.     push ES
  252.     UDP_SOCK_W_ACCESS_in_AX_BX_CX_DX_out_AX_DI_ES %bootp_&name&_request_sock
  253.     pop  DX
  254.     pop  SI
  255.     or AX, AX
  256.     jnz drop
  257.  
  258.     mov BX, DS              ;; save DS
  259.     mov DS, DX
  260.     mov CX, size bootp
  261.     rep                     ;; copy the packet
  262.     movsb
  263.     mov DS, BX              ;; restore DS
  264.  
  265.     mov CX, size bootp
  266.     UDP_SOCK_W_WRITE_in_CX %bootp_&name&_request_sock
  267.     done:
  268. ENDM
  269.  
  270.  
  271.  
  272. ;;************************************************************************* 
  273. ;; BOOTP_GET_BROADCAST checks to see if the address in AX,BX is on the
  274. ;; local dl 'mydl' and if it is loads AX,BX with the broadcast address 
  275. ;; for 'mydl' and jumps to 'success'.  AX, BX is not on 'mydl' then
  276. ;; AX,BX, is unchanged
  277. ;;
  278. BOOTP_GET_BROADCAST_DL_in_AX_BX_out_AX_BX_const_SI_ES MACRO name, mydl, success
  279.     local done
  280.     .errb <success>
  281.  
  282.     cmp AX, word ptr dl_ip_&mydl&_net
  283.     jnz done
  284.     mov CX, BX
  285.     and CX, word ptr dl_ip_&mydl&_mask+2
  286.     cmp CX, word ptr dl_ip_&mydl&_net+2
  287.     jnz done
  288.     mov BX, word ptr dl_ip_&mydl&_broad+2
  289.     jmp success
  290.     done:
  291. ENDM
  292.  
  293.  
  294. ;;******************************************************************************
  295. ;; TFTP_REQUEST_PACKET MACRO name
  296. ;; tftp_request_packet is a KLUGE that will make CISCO's boot properly though
  297. ;; a gateway.  You see Cisco's are brain dead and after they receive the 
  298. ;; bootp reply they ignore the fact that they now have the servers address
  299. ;; and try to TFTP their configuration file by broadcasting a TFTP command
  300. ;; on the local network.  Here i simply act as a repeater of these packets
  301. ;;
  302. TFTP_REQUEST_PACKET_in_AX_BX_CX_ES MACRO bootp, ip
  303.     local done
  304.     .errb <ip>
  305.  
  306.         ;; much of this is TOTALLY ILLEGAL, and breaks every rule in the
  307.         ;; book.  It essentially has the effect of sending out the
  308.         ;; IP packet as if it came from a source other than here
  309.         ;; (in this case it acts as if it came from the original
  310.         ;; sender).  I only do this because I really hope that this
  311.         ;; code will only be needed for a short time.
  312.     mov SI, BX
  313.     sub SI, size udp
  314.     add CX, size udp
  315.     mov AX, word ptr bootp_&bootp&_forward
  316.     or AX, AX
  317.     jz done
  318.     mov BX, word ptr bootp_&bootp&_forward+2
  319.     mov DL, UDP_PROTO
  320.     push SI
  321.     push ES
  322.     push CX
  323.     IP_W_ACCESS_in_AX_BX_CX_DL_out_AX_DI_ES ip
  324.     pop CX
  325.     pop DX
  326.     pop SI
  327.     or AX, AX
  328.     jnz done
  329.  
  330.     mov BX, ES      ;; save ES
  331.     mov ES, DX      ;; restore the input ES
  332.  
  333.     sub SI, 20
  334.     mov AX, word ptr ES:[SI+ip_src]
  335.     mov word ptr ip_&ip&_data.ip_write_src, AX
  336.     mov AX, word ptr ES:[SI+ip_src+2]
  337.     mov word ptr ip_&ip&_data.ip_write_src+2, AX
  338.     add SI, 20
  339.  
  340.     mov ES, BX      ;; restore ES
  341.  
  342.     mov BX, DS      ;; save DS
  343.     mov DS, DX      ;; DS = input ES
  344.  
  345.     mov DX, CX      ;; save CX
  346.     inc CX
  347.     shr CX, 1
  348.     rep
  349.     movsw
  350.     mov DS, BX      ;; restore DS
  351.     mov CX, DX      ;; restore CX
  352.  
  353.     IP_W_WRITE_in_CX ip
  354.     done:
  355. ENDM
  356.  
  357.