home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / network / src_1218.zip / BOOTP.C < prev    next >
C/C++ Source or Header  |  1991-06-04  |  9KB  |  368 lines

  1. /*
  2.  * Center for Information Technology Integration
  3.  *           The University of Michigan
  4.  *                    Ann Arbor
  5.  *
  6.  * Dedicated to the public domain.
  7.  * Send questions to info@citi.umich.edu
  8.  *
  9.  * BOOTP is documented in RFC 951 and RFC 1048
  10.  * Delinted, ANSIfied and reformatted - 5/30/91 P. Karn
  11.  */
  12.  
  13.  
  14. #include <time.h>
  15. #include "global.h"
  16. #include "mbuf.h"
  17. #include "socket.h"
  18. #include "netuser.h"
  19. #include "udp.h"
  20. #include "iface.h"
  21. #include "ip.h"
  22. #include "internet.h"
  23. #include "domain.h"
  24. #include "rip.h"
  25. #include "cmdparse.h"
  26. #include "bootp.h"
  27.  
  28. static int bootp_rx __ARGS((struct iface *ifp,struct mbuf *bp));
  29. static void ntoh_bootp __ARGS((struct mbuf **bpp,struct bootp *bootpp));
  30. static int mask2width __ARGS((int32 mask));
  31.  
  32. #define BOOTP_TIMEOUT    30        /* Time limit for booting       */
  33. #define BOOTP_RETRANS    5        /* The inteval between sendings */
  34.  
  35. int WantBootp = 0;
  36. static int SilentStartup = 0;
  37.  
  38. int
  39. dobootp(argc,argv,p)
  40. int argc;
  41. char *argv[];
  42. void *p;
  43. {
  44.     struct iface *ifp = NULLIF;
  45.     struct socket lsock, fsock;
  46.     struct mbuf *bp;
  47.     struct udp_cb *bootp_cb;
  48.     register char *cp;
  49.     time_t        now,        /* The current time (seconds)   */
  50.               starttime,    /* The start time of sending BOOTP */
  51.               lastsendtime;    /* The last time of sending BOOTP  */
  52.     int i;
  53.  
  54.     if(argc < 2)            /* default to the first interface */
  55.         ifp = Ifaces;
  56.     else {
  57.         for(i = 1; i != argc; ++i){
  58.             
  59.             if((ifp = if_lookup(argv[i])) != NULLIF) 
  60.                 continue;
  61.             else if(strncmp(argv[i], "silent", strlen(argv[i])) == 0)
  62.                 SilentStartup = 1;
  63.             else if(strncmp(argv[i], "noisy", strlen(argv[i])) == 0)
  64.                 SilentStartup = 0;
  65.             else {
  66.                 tprintf("bootp [net_name] [silent] [noisy]\n");
  67.                 return 1;
  68.             }
  69.         }
  70.     }
  71.  
  72.     if(ifp == NULLIF)
  73.         return 0;
  74.     
  75.     lsock.address = ifp->addr;
  76.     lsock.port = IPPORT_BOOTPC;
  77.  
  78.     bootp_cb = open_udp(&lsock,NULLVFP);
  79.  
  80.     fsock.address = ifp->broadcast;
  81.     fsock.port = IPPORT_BOOTPS;
  82.  
  83.       /* Get boot starting time */
  84.       time(&starttime);
  85.       lastsendtime = 0;
  86.  
  87.       /* Send the bootp request packet until a response is received or time
  88.        out */
  89.       for(;;){
  90.  
  91.         /* Allow bootp packets should be passed through iproute. */
  92.         WantBootp = 1;
  93.  
  94.         /* Get the current time */
  95.         time(&now);
  96.  
  97.         /* Stop, if time out */
  98.         if(now - starttime >= BOOTP_TIMEOUT){
  99.             tprintf("bootp: timed out, values not set\n");
  100.             break;
  101.         }
  102.  
  103.         /* Don't flood the network, send in intervals */
  104.         if(now - lastsendtime > BOOTP_RETRANS){
  105.             if(!SilentStartup) tprintf("Requesting...\n");
  106.  
  107.             /* Allocate BOOTP packet and fill it in */
  108.             if((bp = alloc_mbuf(sizeof(struct bootp))) == NULLBUF)
  109.                 break;
  110.  
  111.             cp = bp->data;        /* names per the RFC: */
  112.             *cp++ = BOOTREQUEST;        /* op */
  113.             *cp++ = ifp->iftype->type;    /* htype */
  114.             *cp++ = ifp->iftype->hwalen;    /* hlen */
  115.             *cp++ = 0;            /* hops */
  116.             cp = put32(cp,(int32) now);    /* xid */
  117.             cp = put16(cp, now - starttime);/* secs */
  118.             cp = put16(cp, 0);        /* unused */
  119.             cp = put32(cp, ifp->addr);    /* ciaddr */
  120.             cp = put32(cp, 0L);        /* yiaddr */
  121.             cp = put32(cp, 0L);        /* siaddr */
  122.             cp = put32(cp, 0L);        /* giaddr */
  123.             memcpy(cp, ifp->hwaddr, ifp->iftype->hwalen);
  124.             cp += 16;            /* chaddr */
  125.             memset(cp, 0, 64);        /* sname */
  126.             cp += 64;
  127.             memset(cp, 0, 128);        /* file */
  128.             cp += 128;
  129.             memset(cp, 0, 64);        /* vend */
  130.             cp += 64;
  131.             bp->cnt = cp - bp->data;
  132.             /* assert(bp->cnt == BOOTP_LEN) */
  133.  
  134.             /* Send out one BOOTP Request packet as a broadcast */
  135.             send_udp(&lsock, &fsock,0,0,bp,bp->cnt,0,0);
  136.  
  137.             lastsendtime = now;
  138.         }
  139.  
  140.         /* Give other tasks a chance to run. */
  141.         pwait(NULL);
  142.  
  143.         /* Test for and process any replies */
  144.         if(recv_udp(bootp_cb, &fsock, &bp) > -1){
  145.             if(bootp_rx(ifp,bp))
  146.                 break;
  147.         } else if(Net_error != WOULDBLK){
  148.             tprintf("bootp: Net_error %d, no values set\n",
  149.                  Net_error);
  150.             break;
  151.         }
  152.       }
  153.  
  154.     WantBootp = 0;
  155.     del_udp(bootp_cb);
  156.     return 0;
  157. }
  158.  
  159. /* Process BOOTP input received from 'interface'. */
  160. static int
  161. bootp_rx(ifp,bp)
  162. struct iface *ifp;
  163. struct mbuf *bp;
  164. {
  165.     int         ch;
  166.     int            count;
  167.     int32         gateway = 0;
  168.     int32         nameserver = 0;
  169.     int32         broadcast, netmask;
  170.     struct route     *rp;
  171.     struct bootp    reply;
  172.     char        *cp;
  173.  
  174.     if(len_p(bp) != sizeof(struct bootp)){
  175.         free_p(bp);
  176.         return 0;
  177.     }
  178.     ntoh_bootp(&bp, &reply);
  179.     free_p(bp);
  180.  
  181.     if(reply.op != BOOTREPLY) 
  182.         return 0;
  183.  
  184.     if(!SilentStartup)
  185.         tprintf("Network %s configured:\n", ifp->name);
  186.  
  187.     if(ifp->addr == 0){
  188.         Ip_addr = (int) reply.yiaddr.s_addr;    /* yiaddr */
  189.         ifp->addr =  reply.yiaddr.s_addr;    /* yiaddr */
  190.         if(!SilentStartup)
  191.             tprintf("     IP address: %s\n",  
  192.         inet_ntoa(ifp->addr));
  193.     }
  194.  
  195.  
  196.     /* now process the vendor-specific block, check for cookie first. */
  197.     cp = reply.vend;
  198.     if(get32(cp) != 0x63825363L){
  199.         printf("Invalid magic cookie.\n");
  200.         return(0);
  201.     }
  202.  
  203.     cp += 4;
  204.     while(((ch = *cp) != BOOTP_END) && (++cp < (reply.vend + 64))) 
  205.         switch(ch){
  206.         case BOOTP_PAD:        /* They're just padding */
  207.             continue;
  208.         case BOOTP_SUBNET:    /* fixed length, 4 octets */
  209.             cp++;        /* moved past length */
  210.  
  211.             /* Set the netmask */
  212.                 /* Remove old entry if it exists */
  213.             netmask = get32(cp);
  214.             cp += 4;
  215.  
  216.                 rp = rt_blookup(ifp->addr & ifp->netmask,mask2width(ifp->netmask));
  217.                 if(rp != NULLROUTE)
  218.                         rt_drop(rp->target,rp->bits);
  219.                 ifp->netmask = netmask;
  220.                 rt_add(ifp->addr,mask2width(ifp->netmask),0L,ifp,0L,0L,0);
  221.  
  222.             if(!SilentStartup)
  223.                 tprintf("     Subnet mask: %s\n", inet_ntoa(netmask));
  224.             
  225.             /* Set the broadcast */
  226.             broadcast = ifp->addr | ~(ifp->netmask);
  227.                 rp = rt_blookup(ifp->broadcast,32);
  228.                 if(rp != NULLROUTE && rp->iface == ifp)
  229.                         rt_drop(ifp->broadcast,32);
  230.                 ifp->broadcast = broadcast;
  231.                 rt_add(ifp->broadcast,32,0L,ifp,1L,0L,1);
  232.             
  233.             if(!SilentStartup)
  234.                 tprintf("     Broadcast: %s\n", inet_ntoa(broadcast));
  235.  
  236.             break;
  237.         case BOOTP_HOSTNAME:
  238.             count = (int) *cp;
  239.             cp++;
  240.  
  241.             if(Hostname != NULLCHAR)
  242.                 free(Hostname);
  243.             Hostname = mallocw(count);
  244.             strncpy(Hostname, cp, count);
  245.             cp += count;
  246.  
  247.             if(!SilentStartup)
  248.                 tprintf("     Hostname: %s\n", Hostname);
  249.             break;
  250.         case BOOTP_DNS:
  251.             count = (int) *cp;
  252.             cp++;
  253.  
  254.             while(count){
  255.                 nameserver = get32(cp);
  256.                 add_nameserver(nameserver);
  257.                 if(!SilentStartup)
  258.                     tprintf("     Nameserver: %s\n", inet_ntoa(nameserver));
  259.                 cp += 4;
  260.                 count -= 4;
  261.             }
  262.             break;
  263.         case BOOTP_GATEWAY:
  264.             count = (int) *cp;
  265.             cp++;
  266.  
  267.             gateway = get32(cp);
  268.  
  269.             /* Add the gateway as the default */
  270.             rt_add(0,0,gateway,ifp,1,0,0);
  271.  
  272.             if(!SilentStartup)
  273.                 tprintf("     Default gateway: %s\n", inet_ntoa(gateway));
  274.             cp += count;
  275.             break;
  276.         default:        /* variable field we don't know about */
  277.             count = (int) *cp;
  278.             cp++;
  279.  
  280.             cp += count;
  281.             break;
  282.     }
  283.  
  284.     rt_add(ifp->addr,mask2width(ifp->netmask),0L,ifp,1,0,0);
  285.  
  286.     return(1);
  287. }
  288.  
  289.  
  290. static void
  291. ntoh_bootp(bpp, bootpp)
  292. struct mbuf **bpp;
  293. struct bootp *bootpp;
  294. {
  295.     bootpp->op = pullchar(bpp);                     /* op */
  296.     bootpp->htype = pullchar(bpp);            /* htype */
  297.     bootpp->hlen = pullchar(bpp);            /* hlen */
  298.     bootpp->hops = pullchar(bpp);            /* hops */
  299.     bootpp->xid = pull32(bpp);            /* xid */
  300.     bootpp->secs = pull16(bpp);            /* secs */
  301.     bootpp->unused = pull16(bpp);            /* unused */
  302.     bootpp->ciaddr.s_addr = pull32(bpp);        /* ciaddr */
  303.     bootpp->yiaddr.s_addr = pull32(bpp);        /* ciaddr */
  304.     bootpp->siaddr.s_addr = pull32(bpp);        /* siaddr */
  305.     bootpp->giaddr.s_addr = pull32(bpp);        /* giaddr */
  306.     pullup(bpp, bootpp->chaddr, 16);        /* chaddr */
  307.     pullup(bpp, bootpp->sname, 64);        /* sname */
  308.     pullup(bpp, bootpp->file, 128);        /* file name */
  309.     pullup(bpp, bootpp->vend, 64);            /* vendor */
  310. }
  311.  
  312.  
  313.  
  314.  
  315. int
  316. bootp_validPacket(ip, bpp)
  317. struct ip *ip;
  318. struct mbuf **bpp;
  319. {
  320.     struct udp udp;
  321.     struct pseudo_header ph;
  322.     int status;
  323.  
  324.  
  325.     /* Must be a udp packet */
  326.     if(ip->protocol !=  UDP_PTCL) 
  327.         return 0;
  328.  
  329.     /* Invalid if packet is not the right size */
  330.     if(len_p(*bpp) != (sizeof(struct udp) + sizeof(struct bootp)))
  331.         return 0;
  332.  
  333.     /* Invalid if not a udp bootp packet */
  334.     ntohudp(&udp, bpp);
  335.  
  336.     status = (udp.dest == IPPORT_BOOTPC) ? 1 : 0;
  337.     
  338.     /* Restore packet, data hasn't changed */
  339.         /* Create pseudo-header and verify checksum */
  340.         ph.source = ip->source;
  341.         ph.dest = ip->dest;
  342.         ph.protocol = ip->protocol;
  343.         ph.length = ip->length - IPLEN - ip->optlen;
  344.  
  345.     *bpp = htonudp(&udp, *bpp, &ph);
  346.  
  347.     return status;
  348. }
  349.  
  350. /* Given a network mask, return the number of contiguous 1-bits starting
  351.  * from the most significant bit.
  352.  */
  353. static int
  354. mask2width(mask)
  355. int32 mask;
  356. {
  357.         int width,i;
  358.  
  359.         width = 0;
  360.         for(i = 31;i >= 0;i--){
  361.                 if(!(mask & (1L << i)))
  362.                         break;
  363.                 width++;
  364.         }
  365.         return width;
  366. }
  367.  
  368.