📄 bootplib.c
字号:
/* bootpLib.c - Bootstrap Protocol (BOOTP) client library *//* Copyright 1984 - 2001 Wind River Systems, Inc. */#include "copyright_wrs.h"/*modification history--------------------01x,07dec01,wap Use htons() to set ip_len and ip_id fields in IP header, and htonl() to set the XID field in the packet body/BPF filter so that BOOTP works on little-endian targets (SPR #72059)01w,15oct01,rae merge from truestack ver 01z, base 01v01v,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 protocol allows a host to initialize automatically by obtaining its IP address, boot file name, and boot host's IP address overa network. The bootpLibInit() routine links this library into theVxWorks image. This happens automatically if INCLUDE_BOOTP is definedat the time the image is built.CONFIGURATION INTERFACEWhen used during boot time, the BOOTP library attempts to retrievethe required configuration information from a BOOTP server usingthe interface described below. If it is successful, the remainderof the boot process continues as if the information were entered manually.HIGH-LEVEL INTERFACEThe bootpParamsGet() routine retrieves a set of configuration parametersaccording to the client-server interaction described in RFC 951 andclarified in RFC 1542. The parameter descriptor structure it accepts asan argument allows the retrieval of any combination of the options describedin RFC 1533 (if supported by the BOOTP server and specified in the database).During the default system boot process, the routine obtains the boot file, theInternet address, and the host Internet address. It also obtains the subnetmask and the Internet address of an IP router, if available.LOW-LEVEL INTERFACEThe bootpMsgGet() routine transmits an arbitrary BOOTP request message andprovides direct access to any reply. This interface provides a method forsupporting alternate BOOTP implementations which may not fully comply withthe recommended behavior in RFC 1542. For example, it allows transmissionof BOOTP messages to an arbitrary UDP port and provides access to thevendor-specific field to handle custom formats which differ from the RFC 1533implementation. The bootpParamsGet() routine already extracts all options which that document defines.EXAMPLEThe following code fragment demonstrates use of the BOOTP library: .CS #include "bootpLib.h" #define _MAX_BOOTP_RETRIES 1 struct bootpParams bootParams; struct in_addr clntAddr; struct in_addr hostAddr; char bootFile [SIZE_FILE]; int subnetMask; struct in_addr_list routerList; struct in_addr gateway; struct ifnet * pIf; /@ Retrieve the interface descriptor of the transmitting device. @/ pIf = ifunit ("ln0"); if (pIf == NULL) { printf ("Device not found.\n"); return (ERROR); } /@ Setup buffers for information from BOOTP server. @/ bzero ( (char *)&clntAddr, sizeof (struct in_addr)); bzero ( (char *)&hostAddr, sizeof (struct in_addr)); bzero (bootFile, SIZE_FILE); 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.netmask = (struct in_addr *)&subnetMask; routerlist.addr = &gateway; routerlist.num = 1; bootParams.routers = &routerlist; /@ @ Send request and wait for reply, retransmitting as necessary up to @ given limit. Copy supplied entries into buffers if reply received. @/ result = bootpParamsGet (pIf, _MAX_BOOTP_RETRIES, &clntAddr, &hostAddr, NULL, bootFile, &bootParams); if (result != OK) return (ERROR);.CEINCLUDE FILES: bootpLib.hSEE ALSO: RFC 951, RFC 1542, RFC 1533,INTERNALThe diagram below defines the structure chart of bootpLib. | | v v bootpLibInit bootpParamsGet / \ | | v v bootpMsgGet bootpParamsFill*//* includes */#include "vxWorks.h"#include "ioLib.h"#include "bootpLib.h"#include "m2Lib.h" /* M2_blah */#include "bpfDrv.h"#include "vxLib.h" /* checksum() declaration */#include "netinet/ip.h"#include "netinet/udp.h"#include "netinet/if_ether.h"#include "stdio.h" /* sprintf() declaration */#include "stdlib.h" /* rand() declaration */#include "end.h"#include "muxLib.h"/* defines */#define _BOOTP_MAX_DEVNAME 13 /* "/bpf/bootpc0" *//* locals */LOCAL int bootpConvert (int);LOCAL BOOL bootpInitialized;LOCAL struct { struct ip ih; /* IP header */ struct udphdr uh; /* UDP header */ BOOTP_MSG bp; /* Bootp message */ } bootpMsg;LOCAL char * pMsgBuffer; /* Storage for BOOTP replies from BPF. */LOCAL int bootpBufSize; /* Size of storage buffer. */ /* Berkeley Packet Filter instructions for catching BOOTP messages. */LOCAL struct bpf_insn bootpfilter[] = { BPF_STMT(BPF_LD+BPF_TYPE,0), /* Save lltype in accumulator */ BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, ETHERTYPE_IP, 0, 22), /* IP packet? */ /* * The remaining statements use the (new) BPF_HLEN alias to avoid any * link-layer dependencies. The expected destination port and transaction * ID values are altered when necessary by the BOOTP routines to match the * actual settings. */ BPF_STMT(BPF_LD+BPF_H+BPF_ABS+BPF_HLEN, 6), /* A <- IP FRAGMENT field */ BPF_JUMP(BPF_JMP+BPF_JSET+BPF_K, 0x1fff, 20, 0), /* OFFSET == 0 ? */ BPF_STMT(BPF_LDX+BPF_HLEN, 0), /* X <- frame data offset */ BPF_STMT(BPF_LD+BPF_B+BPF_IND, 9), /* A <- IP_PROTO field */ BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, IPPROTO_UDP, 0, 17), /* UDP ? */ BPF_STMT(BPF_LD+BPF_HLEN, 0), /* A <- frame data offset */ BPF_STMT(BPF_LDX+BPF_B+BPF_MSH+BPF_HLEN, 0), /* X <- IPHDR LEN field */ BPF_STMT(BPF_ALU+BPF_ADD+BPF_X, 0), /* A <- start of UDP datagram */ BPF_STMT(BPF_MISC+BPF_TAX, 0), /* X <- start of UDP datagram */ BPF_STMT(BPF_LD+BPF_H+BPF_IND, 2), /* A <- UDP DSTPORT */ BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 68, 0, 11), /* check DSTPORT */ BPF_STMT(BPF_LD+BPF_H+BPF_ABS+BPF_HLEN, 2), /* A <- IP LEN field */ BPF_JUMP(BPF_JMP+BPF_JGE+BPF_K, BOOTPLEN + UDPHL + IPHL, 0, 9), /* Correct IP length? */ BPF_STMT(BPF_LD+BPF_H+BPF_IND, 4), /* A <- UDP LEN field */ BPF_JUMP(BPF_JMP+BPF_JGE+BPF_K, BOOTPLEN + UDPHL, 0, 7), /* Correct UDP length? */ BPF_STMT(BPF_LD+BPF_B+BPF_IND, 8), /* A <- BOOTP op field */ BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, BOOTREPLY, 0, 5), BPF_STMT(BPF_LD+BPF_W+BPF_IND, 12), /* A <- BOOTP xid field */ BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, -1, 0, 3), /* -1 replaced with real xid */ BPF_STMT(BPF_LD+BPF_W+BPF_IND, 244), /* A <- BOOTP options */ BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x63825363, 0, 1), /* Matches magic cookie? */ BPF_STMT(BPF_RET+BPF_K+BPF_HLEN, BOOTPLEN + UDPHL + IPHL), /* * ignore data beyond expected * size (some drivers add padding). */ BPF_STMT(BPF_RET+BPF_K, 0) /* unrecognized message: ignore frame */ };LOCAL struct bpf_program bootpread = { sizeof (bootpfilter) / sizeof (struct bpf_insn), bootpfilter };LOCAL u_char magicCookie1048 [4] = VM_RFC1048; /* 1048 cookie type */#define VEOF_RFC1048 {255}LOCAL u_char endOfVend[1] = VEOF_RFC1048;LOCAL void bootpParamsFill (struct in_addr *, struct in_addr *, char *, char *, struct bootpParams *, BOOTP_MSG *);LOCAL u_char * bootpTagFind (u_char *, int, int *);/********************************************************************************* bootpLibInit - BOOTP client library initialization** This routine creates and initializes the global data structures used by* the BOOTP client library to obtain configuration parameters. The <maxSize>* parameter specifies the largest link level header for all supported devices.* This value determines the maximum length of the outgoing IP packet containing* a BOOTP message.** This routine must be called before using any other library routines. The* routine is called automatically if INCLUDE_BOOTP is defined at the time* the system is built and uses the BOOTP_MAXSIZE configuration setting* for the <maxSize> parameter.** RETURNS: OK, or ERROR if initialization fails.** ERRNO: S_bootpLib_MEM_ERROR;**/STATUS bootpLibInit ( int maxSize /* largest link-level header, in bytes */ ) { int bufSize; /* Size of receive buffer (BOOTP msg + BPF header) */ if (bootpInitialized) return (OK); if (bpfDrv () == ERROR) { return (ERROR); } bufSize = maxSize + BOOTPLEN + UDPHL + IPHL + sizeof (struct bpf_hdr); if (bpfDevCreate ("/bpf/bootpc", 1, bufSize) == ERROR) { bpfDrvRemove (); return (ERROR); } /* Allocate receive buffer based on maximum message size. */ pMsgBuffer = memalign (4, bufSize); if (pMsgBuffer == NULL) { bpfDevDelete ("/bpf/bootpc"); bpfDrvRemove (); errno = S_bootpLib_MEM_ERROR; return (ERROR); } bootpBufSize = bufSize; bootpInitialized = TRUE; return (OK); }/******************************************************************************** bootpParamsGet - retrieve boot parameters using BOOTP** This routine performs a BOOTP message exchange according to the process* described in RFC 1542, so the server and client UDP ports are always* equal to the defined values of 67 and 68.** The <pIf> argument indicates the network device which will be used to send* and receive BOOTP messages. The BOOTP client only supports devices attached* to the IP protocol with the MUX/END interface. The MTU size must be large* enough to receive an IP packet of 328 bytes (corresponding to the BOOTP* message length of 300 bytes). The specified device also must be capable of* sending broadcast messages, unless this routine sends the request messages* directly to the IP address of a specific server.** The <maxSends> parameter specifies the total number of requests before* before this routine stops waiting for a reply. After the final request,* this routine will wait for the current interval before returning error.* The timeout interval following each request follows RFC 1542, beginning* at 4 seconds and doubling until a maximum limit of 64 seconds.** The <pClientAddr> parameter provides optional storage for the assigned* IP address from the `yiaddr' field of a BOOTP reply. Since this routine* can execute before the system is capable of accepting unicast datagrams* or responding to ARP requests for a specific IP address, the corresponding* `ciaddr' field in the BOOTP request message is equal to zero.** The <pServerAddr> parameter provides optional storage for the IP address* of the responding server (from the `siaddr' field of a BOOTP reply).* This routine broadcasts the BOOTP request message unless this buffer* is available (i.e. not NULL) and contains the explicit IP address of a* BOOTP server as a non-zero value.** The <pHostName> parameter provides optional storage for the server's* host name (from the `sname' field of a BOOTP reply). This routine also* copies any initial string in that buffer into the `sname' field of the* BOOTP request (which restricts booting to a specified host).** The <pBootFile> parameter provides optional storage for the boot file* name (from the `file' field of a BOOTP reply). This routine also copies* any initial string in that buffer into the `file' field of the BOOTP* request message, which typically supplies a generic name to the server.** The remaining fields in the BOOTP request message use the values which* RFC 1542 defines. In particular, the `giaddr' field is set to zero and* the suggested "magic cookie" is always inserted in the (otherwise empty)* `vend' field.* The <pBootpParams> argument provides access to any options defined in
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -