home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-386-Vol-2of3.iso / b / bootp.zip / BOOTP.C next >
Text File  |  1992-03-19  |  6KB  |  252 lines

  1. /*
  2.  
  3. BOOTP - resident BOOTP service.
  4.  
  5. written by Bruce Campbell, WATSTAR Office, Engineering Computing, University
  6. of Waterloo.
  7.  
  8. This program is a TSR that watches for any packet driver application sending
  9. out a BOOTP request.  When it is seen, this program fakes the upcalls to
  10. make it seem like a real BOOTP response.
  11.  
  12. Usage:
  13.  
  14. BOOTP options...
  15.  
  16. where options can be:
  17.  
  18. QUIET            - so that is loads without screen activity
  19. MYIP=ip_number
  20. MY=ip_number
  21. PC_IP=ip_number        - these 3 do the same thing, that is, set your local IP number
  22. SERV=ip_number        - sets the IP number of the server supposedly responding
  23. SIP=ip_number        - sets the IP number of a gateway
  24. HW=hardware_address    - physical address of the server supposedly responding
  25. MASK=subnet_mask    - sets the subnet mask on magic cookie (63.82.53.63) BOOTPs
  26. NAME=ip_number        - sets the name server on magic cookie (63.82.53.63) BOOTPs
  27. GATE=ip_number        - sets the gateway on magic cookie (63.82.53.63) BOOTPs
  28. EXTRA=xx,xx,xx,xx etc    - fills in the vendor field with more stuff on magic cookie (63.82.53.63) BOOTPs
  29. @filename        - a filename full of more options like above (not recursive!)
  30.             - multiple @filename options can be specified on the command line
  31. if the SERV, SIP, NAME or GATE are set to 255.255.255.255, an ip number is
  32. created for the field automatically which consists of the local ip anded with
  33. the subnet mask plus 1.
  34.  
  35. The ip_number, hardware_address, and subnet_mask are specified as nnn.nnn.nnn.nnn
  36. or a hexadecimal address like x628462FE
  37.  
  38. Questions etc. mail to BRUCE@DEVELOPMENT.WATSTAR.uwaterloo.ca
  39.  
  40. Other issues: The upcalls are done during the transmit.  So if an application
  41. sends a BOOTP and immediately clears all received packets, it will not see the
  42. BOOTP response.  This could be fixed by deferring the upcalls to a timer tick
  43. mechanism, however, it works for everything I've tried so far anyway.
  44.  
  45. Example usage:
  46.  
  47. bootp my=129.97.20.102 @defaults
  48.  
  49. where the file 'defaults' contains:
  50.  
  51. serv=129.97.20.1
  52. sip=129.97.20.1
  53. hw=xc800c700
  54. gate=129.97.20.1
  55. name=129.97.129.140
  56. name=129.97.128.152
  57. mask=255.255.254.0
  58. extra=8,4,129.97.128.152
  59.  
  60. Notice how 2 name servers are given.  Also, a quote server has been given
  61. using the "extra" option.
  62.  
  63. See RFC1084 and RFC 951 for more information on BOOTP
  64.  
  65. Source code freely available.  Distribute freely.  Make any changes.
  66. Please leave my name on it though.
  67.  
  68. */
  69.  
  70. extern unsigned long rdvct();
  71. extern newpack();
  72. extern char *strstr();
  73. extern unsigned long oldpack;
  74. extern char *nval();
  75.  
  76. extern unsigned long your_ip;
  77. extern unsigned long server_ip;
  78. extern unsigned long gateway_ip;
  79. extern unsigned long server_hw;
  80. extern unsigned char vendor[];
  81. #define CMDLEN 2048
  82. unsigned char tmpcmd[CMDLEN];
  83.  
  84. #define MAXG 7
  85.  
  86. unsigned long auto_create( inip, msk )
  87. unsigned long inip;
  88. unsigned long msk;
  89. {
  90. if( inip == 0x0FFFFFFFFl )
  91.   return( (your_ip & msk) + 0x001000000l );
  92. else
  93.   return( inip );
  94. }
  95.  
  96. install_wpack( cmd )
  97. char *cmd;
  98. {
  99. unsigned int vec;
  100. char *ts;
  101. unsigned long tmpl;
  102. char *tp;
  103. int i, j;
  104. int numgates = 0;
  105. int numnames = 0;
  106. unsigned long gates[MAXG];
  107. unsigned long names[MAXG];
  108. unsigned long *llp;
  109. unsigned long submask = 0x000FEFFFFl;
  110. int quiet = 0;
  111. int handle;
  112.  
  113. strupr( cmd );
  114. strcpy( tmpcmd, cmd );
  115. strcat( tmpcmd, " " );
  116.  
  117. tp = cmd;
  118. while( tp = strstr( tp, "@" ) )
  119.   {
  120.   tp++;
  121.   if( ts = strstr( tp, " " ) ) *ts = 0;
  122.   handle = bopen( tp );
  123.   if( handle == -1 )
  124.     {
  125.     pmsg( "Unable to open " );
  126.     pmsg( tp );
  127.     pmsg( "\r\n" );
  128.     if( ts ) *ts = 0x020;
  129.     continue;
  130.     }
  131.   if( ts ) *ts = 0x020;
  132.   i = strlen( tmpcmd );
  133.   while( i < (CMDLEN-1) )
  134.     {
  135.     j = bread( handle );
  136.     if( j == -1 ) break;
  137.     j = toupper( j & 0x07F );
  138.     if( j == 0x01A ) break;
  139.     if( iscntrl( j ) ) j = 0x020;
  140.     tmpcmd[i++] = j;
  141.     tmpcmd[i] = 0;
  142.     }
  143.   bclose( handle );
  144.   }
  145.  
  146. if( strstr( tmpcmd, "QUIET" ) ) quiet = 1;
  147.  
  148. do_one_arg( tmpcmd, "MY=", &your_ip, 4 );
  149. do_one_arg( tmpcmd, "MYIP=", &your_ip, 4 );
  150. do_one_arg( tmpcmd, "PC_IP=", &your_ip, 4 );
  151. do_one_arg( tmpcmd, "SERV=", &server_ip, 4 );
  152. do_one_arg( tmpcmd, "SIP=", &gateway_ip, 4 );
  153. do_one_arg( tmpcmd, "HW=", &server_hw, 4 );
  154. do_one_arg( tmpcmd, "MASK=", &submask, 4 );
  155.  
  156. server_ip = auto_create( server_ip, submask );
  157. gateway_ip = auto_create( gateway_ip, submask );
  158.  
  159. while( tp = strstr( tmpcmd, "NAME=" ) )
  160.   {
  161.   do_one_arg( tmpcmd, "NAME=", &tmpl, 4 );
  162.   tmpl = auto_create( tmpl, submask );
  163.   names[numnames++] = tmpl;
  164.   if( numnames >= MAXG ) break;
  165.   tp[0] = 'n';
  166.   }
  167.  
  168. while( tp = strstr( tmpcmd, "GATE=" ) )
  169.   {
  170.   do_one_arg( tmpcmd, "GATE=", &tmpl, 4 );
  171.   tmpl = auto_create( tmpl, submask );
  172.   gates[numgates++] = tmpl;
  173.   if( numgates >= MAXG ) break;
  174.   tp[0] = 'g';
  175.   }
  176.  
  177. i = 0;
  178.  
  179. vendor[i+0] = 1;
  180. vendor[i+1] = 4;
  181. llp = &vendor[i+2];
  182. llp[0] = submask;
  183. i += (2 + vendor[i+1]);
  184.  
  185. if( numgates )
  186.   {
  187.   vendor[i+0] = 3;
  188.   vendor[i+1] = numgates*4;
  189.   llp = &vendor[i+2];
  190.   for( j=0; j<numgates; j++ ) llp[j] = gates[j];
  191.   i += (2 + vendor[i+1]);
  192.   }
  193.  
  194. if( numnames )
  195.   {
  196.   vendor[i+0] = 6;
  197.   vendor[i+1] = numnames*4;
  198.   llp = &vendor[i+2];
  199.   for( j=0; j<numnames; j++ ) llp[j] = names[j];
  200.   i += (2 + vendor[i+1]);
  201.   }
  202.  
  203. if( tp = strstr( tmpcmd, "EXTRA=" ) )
  204.   {
  205.   tp += 6;
  206.   while( tp && *tp && (i<59) )
  207.     {
  208.     tp = nval( tp, &tmpl );
  209.     if( tp ) vendor[i++] = tmpl;
  210.     }
  211.   }
  212.  
  213. vendor[i+0] = 255;
  214.  
  215. if( !quiet )
  216.   {
  217.   pmsg( " Your_ip=" );
  218.   printa( your_ip, 8 );
  219.   pmsg( " Server_ip=" );
  220.   printa( server_ip, 8 );
  221.   pmsg( " Gateway_ip=" );
  222.   printa( gateway_ip, 8 );
  223.   pmsg( " Server_hw=" );
  224.   printa( server_hw, 8 );
  225.   pmsg( " Vendor=" );
  226.   for( j=0; j<i; j++ )
  227.     {
  228.     printa( (unsigned long) vendor[j], 2 );
  229.     pmsg( "." );
  230.     }
  231.   pmsg( "\r\n" );
  232.   }
  233.  
  234. for( vec=0x060; vec<=0x080; vec++ )
  235.   {
  236.   oldpack = rdvct( vec );
  237.   if( !oldpack ) continue;
  238.   if( bufcmp( oldpack+3l, "PKT DRVR", readds(), 9 ) ) continue;
  239.   if( !bufcmp( oldpack+12l, "BOOTP by Bruce Campbell", readds(), 24 ) )
  240.     {
  241.     if( !quiet ) pmsg( "BOOTP already loaded\r\n" );
  242.     return(1);
  243.     }
  244.   wrtvct( vec, newpack, readcs() );
  245.   return(0);
  246.   }
  247.  
  248. if( !quiet ) pmsg( "No packet driver(s) found\r\n" );
  249.  
  250. return(2);
  251. }
  252.