📄 bootplib.c
字号:
/* bootpLib.c - Bootstrap Protocol (BOOTP) client library *//* Copyright 1984 - 2003 Wind River Systems, Inc. */#include "copyright_wrs.h"/*modification history--------------------01z,22jan03,wap Make init routine backwards compatible01y,14jan03,rae Merged from velocecp branch01x,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 16 /* /bpf/bootpc/0 to /bpf/bootpc/N *//* locals */LOCAL int bootpConvert (int);LOCAL BOOL bootpInitialized;typedef struct { struct ip ih; /* IP header */ struct udphdr uh; /* UDP header */ BOOTP_MSG bp; /* Bootp message */ } BOOTP_PKT;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 */ ) { return (bootpLibMultiInit (1, maxSize)); }/********************************************************************************* bootpLibMultiInit - BOOTP client multi-interface library initialization** This routine behaves almost identically to bootpLibInit(), except it* is designed for systems that need to perform BOOTP requests over several* interfaces at once. The <numIfs> parameter specifies the maximum number* of interfaces available in the system. The <maxSize> parameter specifies* the maximum link level header size, the same as for bootpLibInit().** This routine must be called before using any other library routines.* It may be called at startup in place of bootpLibInit() if multi-interface* support is desired, however bootpLibInit() is called by default when* INCLUDE_BOOTP is defined. The caller may specify BOOTP_MAXSIZE as the* value for the <maxSize> parameter.** RETURNS: OK, or ERROR if initialization fails.** ERRNO: S_bootpLib_MEM_ERROR;**/STATUS bootpLibMultiInit ( ulong_t numIfs, /* number of interfaces */ int maxSize /* largest link-level header, in bytes */ ) { int bufSize; /* Size of receive buffer (BOOTP msg + BPF header) */ int ix; char devName[32]; if (bootpInitialized) return (OK); if (bpfDrv () == ERROR) { return (ERROR); } bufSize = maxSize + BOOTPLEN + UDPHL + IPHL + sizeof (struct bpf_hdr); for ( ix = 0; ix < numIfs; ix++ ) { sprintf(devName, "/bpf/bootpc/%d", ix); if (bpfDevCreate (devName, 1, bufSize) == ERROR) { for ( --ix; ix > -1; ix-- ) { sprintf(devName, "/bpf/bootpc/%d", ix); bpfDevDelete (devName); } bpfDrvRemove (); return (ERROR); } } /* Allocate receive buffer based on maximum message size. */ pMsgBuffer = memalign (4, bufSize); if (pMsgBuffer == NULL) { for ( ix = 0; ix < numIfs; ix++ ) { sprintf(devName, "/bpf/bootpc/%d", ix); bpfDevDelete (devName); } bpfDrvRemove (); errno = S_bootpLib_MEM_ERROR; return (ERROR); } bootpBufSize = bufSize; bootpInitialized = TRUE; return (OK); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -