📄 bootplib.c
字号:
/* bootpLib.c - BOOTP client library *//* Copyright 1984 - 1999 Wind River Systems, Inc. */#include "copyright_wrs.h"/*modification history--------------------01v,17mar99,spm added support for identical unit numbers (SPR #20913)01u,04sep98,ham corrected lack of params for etherInputHookAdd(),SPR#2190901t,28aug98,n_s corrected MAC address comparison in bootpInputHook. spr #2090201s,17jul97,dgp doc: correct unsupported interfaces per SPR 894002c,14dec97,jdi doc: cleanup.02b,10dec97,gnn making man page fixes02a,08dec97,gnn END code review fixes01z,03dec97,spm corrected parameter description for bootpMsgSend (SPR #9401); minor changes to man pages and code spacing01y,03oct97,gnn removed references to endDriver global.01x,25sep97,gnn SENS beta feedback fixes01w,26aug97,spm fixed bootpParamsGet - gateway not retrieved (SPR #9137)01v,12aug97,gnn changes necessitated by MUX/END update.01u,30apr97,jag man page edit for function bootParamsGet()01t,07apr97,spm changed BOOTP interface to DHCP style: all options supported01s,17dec96,gnn added code to handle the new etherHooks and END stuff.01r,08nov96,spm Updated example of bootpParamsGet() for SPR 712001q,22sep96,spm Fixed SPR 7120: added support for gateways to bootpParamsGet()01p,01feb96,gnn added the end of vendor data (0xff) to the request packet we send01o,16jan94,rhp fix typo in library man page01n,17oct94,rhp remove docn reference to bootpdLib (SPR#2351)01n,22sep92,jdi documentation cleanup.01m,14aug92,elh documentation changes.01l,11jun92,elh modified parameters to bootpParamsGet.01k,26may92,rrr the tree shuffle -changed includes to have absolute path from h/01j,16apr92,elh moved routines shared by icmp to icmpLib.01i,28feb92,elh ansified.01h,27aug91,elh rewritten to use standard bootp protocol, redesigned to be more modular, rewrote documentation.01g,15aug90,dnw added slot parameter to bootpParamsGet() +hjb fixed bug in bootpForwarder() not setting hw addr in arp ioctl01f,12aug90,dnw changed bootpParamsGet() to check every tick for reply message instead of every 4 seconds, for faster response. changed bootpParamsGet() to print '.' every time it broadcasts request.01e,12aug90,hjb major redesign and implementation of the protocol01d,07may90,hjb made bootp IP checksum portable; modifications to the protocol for better forwarding service.01c,19apr90,hjb minor fixups bootpRequestSend(), added protocol extension to solve the routing problem when bootp forwarder is used.01b,11apr90,hjb de-linted.01a,11mar90,hjb written.*//*DESCRIPTIONThis library implements the client side of the Bootstrap Protocol(BOOTP). This network protocol allows the dynamic configuration of the target's boot parameters at boot time. This is in contrast to using the boot information encoded in system non-volatile RAM or ROM. Thus, at boot time, BOOTP goes over the network to get an IP address, a boot file name, and the boot host's IP address. The actual transfer of the boot image is handled by a file transfer protocol, such as TFTP or FTP, or by an RSH command.To access BOOTP services, you can use either the high-level interfacesupported by bootpParamsGet(), or the low-level interface supportedby bootpMsgSend().HIGH-LEVEL INTERFACEThe bootpParamsGet() routine provides the highest level interface to BOOTP. It accepts a parameter descriptor structure that allows the retrieval of any combination of the options described in RFC 1533 (if supported by the BOOTP server and if specified in the database). During system boot, the routine obtains the boot file, the Internet address, and the host Internet address. It also obtains the subnet mask and the Internet address of an IP router, if available.LOW-LEVEL INTERFACEThe bootpMsgSend() routine provides a lower-level interface to BOOTP. It accepts and returns a BOOTP message as a parameter. This interface is moreflexible because it gives the caller direct access to the data in theBOOTP request/reply messages. For example, if the BOOTP message includes implementation-specific options not defined in an RFC, the caller canuse bootpMsgSend() to retrieve them from the vendor-specific field in the BOOTP message. The bootpParamsGet() routine already provides all defined options.EXAMPLEThe following code provides and example of how to use bootpParamsGet(): .CS #include "bootpLib.h" struct bootpParams bootParams; struct in_addr clntAddr; struct in_addr hostAddr; char bootFile [FILENAME_SIZE]; int subnetMask; struct in_addr_list routerList; struct in_addr gateway; char clntAddr [INET_ADDR_LEN]; char bootServer [INET_ADDR_LEN]; char bootFile [SIZE_FILE]; int fileSize; int subnetMask; char gateway [INET_ADDR_LEN]; bzero ( (char *)&clntAddr, sizeof (struct in_addr)); bzero ( (char *)&hostAddr, sizeof (struct in_addr)); bzero (bootFile, FILENAME_SIZE); subnetMask = 0; bzero ( (char *)&gateway, sizeof (struct in_addr)); /@ Set all pointers in parameter descriptor to NULL. @/ bzero ((char *)&bootParams, sizeof (struct bootpParams)); /@ Set pointers corresponding to desired options. @/ bootParams.clientAddr = &clntAddr; bootParams.bootHostAddr = &hostAddr; bootParams.bootfile = pBootFile; bootParams.netmask = (struct in_addr *)&subnetMask; routerlist.addr = &gateway; routerlist.num = 1; bootParams.routers = &routerlist; if (bootpParamsGet ("ln0", 0, 0, &bootParams) == ERROR) return (ERROR);.CENOTECertain targets (typically those with no NVRAM) construct their Ethernet address based on the target's IP address. An IP addressmust be entered for these targets in order to boot over the network.The remaining information can be obtained with BOOTP.BOOTP is not supported over the following network interfaces: if_sl (SLIP) and if_ie (Sun IE driver).if_sl (SLIP) and if_ppp (PPP).INCLUDE FILES: bootpLib.hSEE ALSO: bootLib, RFC 951, RFC 1542, RFC 1533,.pG "Network"INTERNALThe BOOTP implementation uses driver input hooks to access the network interface. The link-level access that mechanism provides is required since the network is not initially available during boot time because the Internet address is unknown.The diagram below defines the structure chart of bootpLib. (network interface driver) | | v v bootpParamsGet bootpInputHook / \ | | | v v v bootpMsgSend bootpParamsFill*//* includes */#include "vxWorks.h"#include "bootpLib.h"#include "netinet/in.h"#include "netinet/udp.h"#include "netinet/in_var.h"#include "netinet/if_ether.h"#include "etherLib.h"#include "taskLib.h"#include "string.h"#include "stdio.h"#include "errno.h"#include "sysLib.h"#include "tickLib.h"#include "icmpLib.h"#include "inetLib.h"#include "end.h"#include "muxLib.h"#include "ipProto.h" /* defines */#define ETHER_ADDR_TYPE 1 /* ethernet type */#define ETHER_ADDR_LEN 6 /* ethernet length *//* globals */LOCAL struct { struct ip ih; /* IP header */ struct udphdr uh; /* UDP header */ BOOTP_MSG bp; /* Bootp message */ } bootpMsg;u_long bootpBroadcastAddr = INADDR_BROADCAST; /* bootp broadcast */int bootpReXmitSecs = INIT_BOOTP_DELAY; /* bootp delay *//* locals */LOCAL BOOTP_MSG * pBootpReply; /* pointer to reply */LOCAL BOOL bootpReplyReceived; /* received reply? */LOCAL char inputBuffer [ETHERMTU]; /* message buffer */ /* 1048 cookie type */LOCAL u_char magicCookie1048 [4] = VM_RFC1048;#define VEOF_RFC1048 {255}LOCAL u_char endOfVend[1] = VEOF_RFC1048;/* forward declarations */LOCAL BOOL bootpInputHook (struct ifnet * pIf, char *einput, int length);extern in_broadcast ();/******************************************************************************** bootpParamsGet - retrieve boot parameters using BOOTP** This routine transmits a BOOTP request message over the network interface* associated with <ifName>. This interface must already be attached and* initialized prior to calling this routine.** A non-zero value for <port> specifies an alternate BOOTP server port.* A zero value means the default BOOTP server port (67).** <timeOut> specifies a timeout value in ticks. If no reply is received * within this period, an error is returned. Specify zero for an infinite* <timeout> value.** <pBootpParams> is a structure pointer to a `bootpParams' structure that* you can use to indicate the parameters of interest to you. The `bootpParams' * structure is defined as follows:** .CS* struct bootpParams* {* struct in_addr * clientAddr;* struct in_addr * bootHostAddr;* char * bootfile;* char * serverName;** struct in_addr * netmask;* unsigned short * timeOffset;* struct in_addr_list * routers;* struct in_addr_list * timeServers;* struct in_addr_list * nameServers;* struct in_addr_list * dnsServers;* struct in_addr_list * logServers;* struct in_addr_list * cookieServers;* struct in_addr_list * lprServers;* struct in_addr_list * impressServers;* struct in_addr_list * rlpServers;* char * clientName;* unsigned short * filesize;* char * dumpfile;* char * domainName;* struct in_addr * swapServer;* char * rootPath;* char * extoptPath;* unsigned char * ipForward;* unsigned char * nonlocalSourceRoute;* struct in_addr_list * policyFilter;* unsigned short * maxDgramSize;* unsigned char * ipTTL;* unsigned long * mtuTimeout;* struct ushort_list * mtuTable;* unsigned short * intfaceMTU;* unsigned char * allSubnetsLocal;* struct in_addr * broadcastAddr;* unsigned char * maskDiscover;* unsigned char * maskSupplier;* unsigned char * routerDiscover;* struct in_addr * routerDiscAddr;* struct in_addr_list * staticRoutes;* unsigned char * arpTrailers;* unsigned long * arpTimeout;* unsigned char * etherPacketType;* unsigned char * tcpTTL;* unsigned long * tcpInterval;* unsigned char * tcpGarbage;* char * nisDomain;* struct in_addr_list * nisServers;* struct in_addr_list * ntpServers;* char * vendString;* struct in_addr_list * nbnServers;* struct in_addr_list * nbddServers;* unsigned char * nbNodeType;* char * nbScope;* struct in_addr_list * xFontServers;* struct in_addr_list * xDisplayManagers;* char * nispDomain;* struct in_addr_list * nispServers;* struct in_addr_list * ipAgents;* struct in_addr_list * smtpServers;* struct in_addr_list * pop3Servers;* struct in_addr_list * nntpServers;* struct in_addr_list * wwwServers;* struct in_addr_list * fingerServers;* struct in_addr_list * ircServers;* struct in_addr_list * stServers;* struct in_addr_list * stdaServers; * };* .CE** This structure allows the retrieval of any BOOTP option specified in* RFC 1533. The list of 2-byte (unsigned short) values is defined as:** .CS* struct ushort_list* {* unsigned char num;* unsigned short * shortlist;* };* .CE** The IP address lists use the following similar definition:** .CS* struct in_addr_list* {* unsigned char num;* struct in_addr * addrlist;* };* .CE** When these lists are present, the routine stores values retrieved from* the BOOTP reply in the location indicated by the `shortlist' or `addrlist'* members. The amount of space available is indicated by the `num' member.* When the routine returns, the `num' member indicates the actual number of* entries retrieved. In the case of `bootpParams.policyFilter.num' * and `bootpParams.staticRoutes.num', the `num' member value should be * interpreted as the number of IP address pairs requested and received.** The following members of the `bootpParams' structure are also used for * both input and output:** .IP `clientAddr' * Contains a pointer that holds the client's Internet address. * On input, if it contains a non-NULL value, it is interpreted as a * pointer to an Internet address of type `struct in_addr' and passed * on to the BOOTP server in the `bp_ciaddr' member of the BOOTP * message structure (BOOTP_MSG). The server will use it as a lookup * field into the BOOTP database. When a reply is received, the client's* assigned Internet address is copied to the `clientAddr' member.** .IP `bootHostAddr' * Contains a pointer that holds the host's IP address. On input, * if it contains a non-NULL value, it is interpreted as the host where * the BOOTP message is to be sent. Note that this host must be local* to the <pIf> network. If NULL, the BOOTP message is sent to the local* broadcast address. On return, the host's IP address is copied to* the `bootHostAddr' member.** On input, if the `bootpParams.bootfile' member points to a non-empty * string, the contents are passed to the BOOTP server in the `bp_file' * member of the BOOTP message structure (BOOTP_MSG). When a reply is * received, the file name retrieved from the BOOTP server is copied to * the `bootpParams.bootfile' member as a NULL-terminated string.** .LP* The remaining elements in the BOOTP parameters descriptor are used to select* options for retrieval from the BOOTP server. The BOOTP library attempts to* retrieve the values for any options whose corresponding field pointers are* non-NULL values. To obtain these parameters, the BOOTP server must support* the vendor-specific options described in RFC 1048 (or its successors) and the * corresponding parameters must be specified in the BOOTP server database. * Where meaningful, the values are returned in host byte order. ** The BOOTP request issued during system startup attempts to retrieve a subnet* mask for the boot device, in addition to the host and client addresses,* and the boot file name.** RETURNS: OK, or ERROR if unsuccessful.** SEE ALSO: bootLib, RFC 1048, RFC 1533*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -