⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dhcpcbootlib.c

📁 vxworks源码源码解读是学习vxworks的最佳途径
💻 C
📖 第 1 页 / 共 4 页
字号:
/* dhcpcBootLib.c - DHCP boot-time client library *//* Copyright 1984 - 1999 Wind River Systems, Inc. */#include "copyright_wrs.h"/*modification history--------------------01r,16mar99,spm  recovered orphaned code from tor2_0_x branch (SPR #25770)01q,16mar99,spm  recovered orphaned code from tor1_0_1.sens1_1 (SPR #25770)01p,01dec98,spm  added missing memory allocation (SPR #22881)01p,06oct98,spm  fixed copying of parameters with IP address pairs (SPR #22416)01o,14dec97,jdi  doc: cleanup.01n,dec1097,kbw  fiddled manpage format01m,04dec97,spm  added code review modifications01l,06oct97,spm  removed reference to deleted endDriver global; replaced with                 support for dynamic driver type detection01k,25sep97,spm  restored man page for dhcpcBootOptionSet() routine01j,25sep97,gnn  SENS beta feedback fixes01i,02sep97,spm  removed name conflicts with runtime DHCP client (SPR #9241)01h,26aug97,spm  major overhaul: included version 01j of dhcpcCommonLib.c                 to retain single-lease library for boot time use01g,12aug97,gnn  changes necessitated by MUX/END update.01f,02jun97,spm  changed DHCP option tags to prevent name conflicts (SPR #8667)                 and updated man pages01e,18apr97,spm  added call to dhcp_boot_client to reduce boot ROM size01d,15apr97,kbw  fixed manpage format01c,07apr97,spm  altered to use current value of configAll.h defines,                 rewrote documentation01b,29jan97,spm  added END driver support and modified to fit coding standards01a,14nov96,spm  created by removing unneeded functions from dhcpcLib.c*//*DESCRIPTIONThis library contains the interface for the client side of the Dynamic HostConfiguration Protocol (DHCP) used during system boot. DHCP is an extensionof BOOTP, the bootstrap protocol.  Like BOOTP, the protocol allows automaticsystem startup by providing an IP address, boot file name, and boot host'sIP address over a network.  Additionally, DHCP provides the complete set ofconfiguration parameters defined in the Host Requirements RFCs and allowsautomatic reuse of network addresses by specifying a lease duration for aset of configuration parameters.  This library is linked into the boot ROM image automatically if INCLUDE_DHCPC is defined at the time that image is constructed.HIGH-LEVEL INTERFACEThe VxWorks boot program uses this library to obtain configuration parameters with DHCP according to the client-server interaction detailed in RFC 1541 usingthe boot device specified in the boot parameters.  The boot device must be capable of sending broadcast messages.  Currently, only Ethernet devices and the shared-memory network drivers are supported.  To use DHCP, first build a boot ROM image with INCLUDE_DHCPC defined and set the appropriate flag in the boot parameters before initiating booting with the "@" command.  The DHCP client willattempt to retrieve entries for the boot file name, host IP address, and target IP address, as well as a subnet mask and broadcast address for the boot device.Any entries retrieved will only be used if the corresponding fields in theboot parameters are blank.NOTEAfter DHCP retrieves the boot parameters, the specified boot file is loadedand the system restarts. As a result, the boot-time DHCP client cannot renewthe lease associated with the assigned IP address.  To avoid potential IPaddress conflicts while loading the boot file, the DHCPC_MIN_LEASE value should be set to exceed the file transfer time.  In addition, the boot filemust also contain the DHCP client library so that the lease obtained beforethe restart can be renewed. Otherwise, the network initialization using theboot parameters will fail.INTERNALThis module was created to reduce the size of the boot ROM image so thatthe DHCP client could be used with target architectures such as the MV147which have limited ROM capacity. The routines in this library are a modifiedversion of similar routines available to the user in the dhcpcLib library. These routines are only intended for use during boot time by the VxWorks system. Among other differences, they do not support multiple leases and they always apply configuration parameters to the underlying network device. They should only be called by the boot program.INCLUDE FILES: dhcpcBootLib.hSEE ALSO: dhcpcLib, RFC 1541, RFC 1533*//* includes */#include "dhcp/copyright_dhcp.h"#include "vxWorks.h"#include "wdLib.h"#include "semLib.h"#include "etherLib.h"#include "rngLib.h"#include "muxLib.h"#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <string.h>#include <errno.h>#include <signal.h>#include <fcntl.h>#include <sys/types.h>#include <sys/socket.h>#include <sys/ioctl.h>#include <net/if.h>#include <netinet/in.h>#include <netinet/in_systm.h>#include <netinet/if_ether.h>#include <netinet/ip.h>#include <netinet/udp.h>#include <arpa/inet.h> #include "dhcpcBootLib.h"#include "end.h"#include "ipProto.h"/* defines */#define _BYTESPERWORD 		4 	/* Conversion factor for IP header */#define _DHCP_MAX_OPTLEN 	255 	/* Max. number of bytes in an option */#define _IPADDRLEN 		4 	/* Length of address in an ARP reply */#define _ETHER 			1 	/* Hardware type for Ethernet */                                        /* from assigned numbers RFC */#define _ETHERADDRLEN 		6 	/* Length of an Ethernet address */     /* Host requirements documents default values. */#define _HRD_MAX_DGRAM 	576 	/* Default maximum datagram size. */#define _HRD_IP_TTL 	64 	/* Default IP time-to-live (seconds) */#define _HRD_MTU 	576 	/* Default interface MTU */#define _HRD_ROUTER 	0xffffffff /* Default router solication */                                   /* address - 255.255.255.255 */#define _HRD_ARP_TIME 	60 	/* Default ARP cache timeout (seconds) */#define _HRD_TCP_TTL 	64 	/* Default TCP time-to-live (seconds) */#define _HRD_TCP_TIME 	7200 	/* Default TCP keepalive interval (seconds) *//* globals */WDOG_ID 	dhcpcBootEventTimer; 	/* Timeout for DHCP events */RING_ID 	dhcpcBootEventRing; 	/* Ring buffer of DHCP events */SEM_ID 		dhcpcBootRingSem; 	/* Protection for event ring buffer. */RING_ID 	dhcpcBootMsgRing; 	/* Ring buffer of DHCP messages */struct msg 	dhcpcRbufMsg; 		/* Components of incoming messages. */char * 		pDhcpcRbuf; 		/* Temporary buffer for input hooks. */SEM_ID 		dhcpcBootEventSem; 	/* DHCP event notification */SEM_ID 		dhcpcBootArpEventSem; 	/* ARP reply notification */                                    	/* IP address to check ARP replies */u_char 		dhcpcBootArpSpa [_IPADDRLEN];int 		dhcpcBindType; 		/* Type of DHCP lease, if any. */struct if_info 	dhcpcDevice; 		/* Specifies underlying hardware. */int 		dhcpcOfferTimeout; 	/* configAll.h #define value */int 		dhcpcDefaultLease; 	/* configAll.h #define value *//* locals */LOCAL char inputBuffer [ETHERHL + sizeof (struct ether_arp)]; /* ARP replies *//* forward declarations */LOCAL void dhcpcBootParamsCopy (struct dhcp_param *);LOCAL void dhcpcBootCleanup (void);/********************************************************************************* dhcpcBootInit - set up the DHCP client parameters and data structures** This routine creates any necessary data structures and sets the client's * option request list to retrieve a subnet mask and broadcast address for the * network interface indicated by <pIf>.  The routine is executed automatically * by the boot program when INCLUDE_DHCPC is defined and the automatic * configuration option is set in the boot flags.  The network interface * specified by <pIf> is used to transmit and receive all DHCP messages during * the lease negotiation.  That interface must be capable of sending broadcast * messages.  Currently, only Ethernet devices and the shared-memory network * drivers are supported.** ERRNO: N/A* * RETURNS: OK, or ERROR if could not initialize.*/STATUS dhcpcBootInit    (    struct ifnet *	pIf	/* network device used by client */    )    {    bzero ( (char *)&dhcpcBootReqSpec, sizeof (struct dhcp_reqspec));    bzero ( (char *)&dhcpcDevice, sizeof (struct if_info));    if (pIf == NULL)        {        printf ("Error: Network device not specified.\n");        return (ERROR);        }    /* Initialize WIDE project global containing network device data. */    sprintf (dhcpcDevice.name, "%s", pIf->if_name);      dhcpcDevice.unit = pIf->if_unit;    dhcpcDevice.iface = pIf;     dhcpcDevice.haddr.htype = _ETHER;    dhcpcDevice.haddr.hlen = _ETHERADDRLEN;    bcopy( (char *) ( (struct arpcom *)pIf)->ac_enaddr,          (char *)&dhcpcDevice.haddr.haddr, dhcpcDevice.haddr.hlen);    /* set duration of client's wait for multiple offers */    dhcpcBootReqSpec.waitsecs = dhcpcOfferTimeout;     /* set default lease - may be overridden with dhcpcBootOptionSet(). */    dhcpcBootReqSpec.lease = dhcpcDefaultLease;    /*      * Initialize request list with tags for options provided by server,     * according to RFC 1533.     */    dhcpcBootReqSpec.reqlist.len = 0;    dhcpcBootReqSpec.reqlist.list [dhcpcBootReqSpec.reqlist.len++] =                                                          _DHCP_SUBNET_MASK_TAG;    dhcpcBootReqSpec.reqlist.list [dhcpcBootReqSpec.reqlist.len++] =                                                         _DHCP_BRDCAST_ADDR_TAG;    /* Create event timer for state machine. */    dhcpcBootEventTimer = wdCreate ();    if (dhcpcBootEventTimer == NULL)        {        return (ERROR);        }    /* Create signalling semaphore. */    dhcpcBootEventSem = semCCreate (SEM_Q_FIFO, 0);    if (dhcpcBootEventSem == NULL)        {        wdDelete (dhcpcBootEventTimer);        return (ERROR);        }    /* Create protection semaphore for event ring. */    dhcpcBootRingSem = semBCreate (SEM_Q_FIFO, SEM_FULL);    if (dhcpcBootRingSem == NULL)        {        wdDelete (dhcpcBootEventTimer);        semDelete (dhcpcBootEventSem);        return (ERROR);        }    /* Create event storage. */    dhcpcBootEventRing = rngCreate (EVENT_RING_SIZE);    if (dhcpcBootEventRing == NULL)        {        wdDelete (dhcpcBootEventTimer);        semDelete (dhcpcBootEventSem);        semDelete (dhcpcBootRingSem);        return (ERROR);        }    /* Create message storage. */    dhcpcBootMsgRing = rngCreate (MESSAGE_RING_SIZE);    if (dhcpcBootMsgRing == NULL)        {        wdDelete (dhcpcBootEventTimer);        semDelete (dhcpcBootEventSem);        semDelete (dhcpcBootRingSem);        rngDelete (dhcpcBootEventRing);        return (ERROR);        }    return (OK);    }/********************************************************************************* dhcpcBootBind - initialize the network with DHCP at boot time** This routine performs the client side of a DHCP negotiation according to * RFC 1541.  The negotiation uses the network device specified with the* initialization call.  The addressing information retrieved is applied to that * network device.  Because the boot image is replaced by the downloaded target * image, the resulting lease cannot be renewed.  Therefore, the minimum lease * length specified by DHCPC_MIN_LEASE must be set so that the target image has * sufficient time to download and begin monitoring the lease.  This routine is * called automatically by the boot program when INCLUDE_DHCPC is defined and * the automatic configuration option is set in the boot flags.** RETURNS: OK if negotiation is successful, or ERROR otherwise.** ERRNO: N/A*/STATUS dhcpcBootBind (void)    {    STATUS result = OK;    dhcpcBindType = DHCP_MANUAL;    result = dhcp_boot_client (&dhcpcDevice);    dhcpcBootCleanup ();    if (result != OK)      return (ERROR);    /* Bind was successful. */    return (OK);    }/********************************************************************************* dhcpcBootParamsGet - retrieve current network parameters** This routine copies the configuration parameters obtained at boot time to * the caller-supplied structure.  That structure contains non-NULL pointers to * indicate the parameters of interest.  All other values within the structure * must be set to 0 before calling the routine.  This routine is called * internally at boot time when the DHCP client is in the BOUND state.** RETURNS: OK.** ERRNO: N/A** NOMANUAL*/STATUS dhcpcBootParamsGet    (    struct dhcp_param*	pParamList     )    {    dhcpcBootParamsCopy (pParamList);    return (OK);    }/********************************************************************************* dhcpcBootOptionSet - add an option to the option request list** This routine sets most client-to-server transmission options for a lease* established by the boot program.  The <option> parameter specifies an option * tag as defined in RFC 1533 and the updates published in the Internet Draft* of November 1996.  The boot program automatically sets all necessary options * for target configuration.  This routine is only provided to support special* circumstances in which additional options are necessary.  Any options * requested with this routine may be retrieved after the runtime image has* started.  For a listing of defined aliases for the known option tags, see * dhcp/dhcp.h.  This routine cannot set the options associated with the * following tags: **     _DHCP_PAD_TAG*     _DHCP_OPT_OVERLOAD_TAG*     _DHCP_MSGTYPE_TAG*     _DHCP_SERVER_ID_TAG*     _DHCP_REQ_LIST_TAG*     _DHCP_MAXMSGSIZE_TAG*     _DHCP_END_TAG** Most options only require specification of the appropriate tag in the * <option> parameter.  In those cases, the dhcpcBootOptionSet() call adds the * specified option tag to the option request list, if possible.  However, some * options require additional information.  The tags for these options are: **     _DHCP_VENDOR_SPEC_TAG*     _DHCP_REQUEST_IPADDR_TAG*     _DHCP_LEASE_TIME_TAG *     _DHCP_ERR_MSG_TAG*     _DHCP_CLASS_ID_TAG*     _DHCP_CLIENT_ID_TAG** The _DHCP_LEASE_TIME_TAG and _DHCP_CLIENT_ID_TAG options each require a* <value> parameter.  For _DHCP_LEASE_TIME_TAG, <value> specifies the desired * lease length.  For _DHCP_CLIENT_ID_TAG, <value> specifies the type for a * type/value pair.  No other options use this parameter.** The _DHCP_VENDOR_SPEC_TAG, _DHCP_CLASS_ID_TAG and _DHCP_CLIENT_ID_TAG* tags each require a value for the <length> parameter to specify the number * of bytes of data provided.  No other options use this parameter.** Use the <data> parameter with the following option tags:* .IP _DHCP_VENDOR_SPEC_TAG 25 * The <data> parameter points to a list of "length" bytes of options * in the format specified by RFC 1533. * .IP _DHCP_REQUEST_IPADDR_TAG* The <data> parameter points to the string representation of the * desired Internet address for the client. * .IP _DHCP_ERRMSG_TAG* The <data> parameter points to the error message to send to the server * when releasing the current IP address.* .IP _DHCP_CLASS_ID_TAG* The <data> parameter points to <length> bytes used as the value 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -