📄 database.c
字号:
(pDhcpsRelaySourceTbl [loop].pAddress); loop++) { sprintf (relayIP, "%s", pDhcpsRelaySourceTbl [loop].pAddress); sprintf (subnet_mask, "%s", pDhcpsRelaySourceTbl [loop].pMask); acl = (struct relay_acl *)calloc (1, sizeof (struct relay_acl)); if (acl == NULL) {#ifdef DHCPS_DEBUG logMsg ("Memory allocation error reading relay agent database.\n", 0, 0, 0, 0, 0, 0);#endif break; } acl->relay.s_addr = inet_addr (relayIP); acl->subnet_mask.s_addr = inet_addr (subnet_mask); if (acl->relay.s_addr == -1 || acl->subnet_mask.s_addr == -1) {#ifdef DHCPS_DEBUG logMsg ("Conversion error reading relay database.\n", 0, 0, 0, 0, 0, 0);#endif free (acl); continue; } /* Store entry in hash table, keyed on relay agent IP address. */ result = hash_ins (&relayhashtable, (char *)&acl->relay, sizeof (struct in_addr), relayipcmp, &acl->relay, acl); if (result < 0) {#ifdef DHCPS_DEBUG logMsg ("hash insertion failed in read_relay_db()", 0, 0, 0, 0, 0, 0);#endif free (acl); break; } nrelay++; }#ifdef DHCPS_DEBUG logMsg ("dhcps: read %d entries from relay agent database.\n", nrelay, 0, 0, 0, 0, 0);#endif return; }/********************************************************************************* strtotime - parse lease expiration time** This routine converts the lease expiration time stored in the binding* database into the internal calendar time. That field has the following* format:* "Wed Feb 19 14:17:39 1997"** On generic VxWorks systems, the internal calendar time is the number of * seconds since system startup. For correct operation of DHCP, it should be * the number of seconds since some absolute base time, such as 00:00:00 GMT, * Jan. 1, 1970, which is the base value used on Unix systems. Adding this* functionality to VxWorks requires BSP modifications. Without this feature, * rebooting the server automatically extends all leases. This behavior will * reduce protocol efficiency, since the server will not re-assign addresses * that (incorrectly) are recorded as still in use, but it will not cause * duplicate IP address assignment.** RETURNS: Calendar time, or 0 if not available.** ERRNO: N/A** NOMANUAL*/static time_t strtotime ( char *date_str /* String containing lease expiration time. */ ) { struct tm tmval; char week[4]; char month[4]; int year = 0; int i = 0; struct map { char symbol[4]; int code; }; struct map week_list[7]; struct map month_list[12]; if (*date_str == '\0') return (0); /* Initialize days of week and months of year. */ week_list[0].code = 0; strcpy (week_list[0].symbol, "Sun"); week_list[1].code = 1; strcpy (week_list[1].symbol, "Mon"); week_list[2].code = 2; strcpy (week_list[2].symbol, "Tue"); week_list[3].code = 3; strcpy (week_list[3].symbol, "Wed"); week_list[4].code = 4; strcpy (week_list[4].symbol, "Thu"); week_list[5].code = 5; strcpy (week_list[5].symbol, "Fri"); week_list[6].code = 6; strcpy (week_list[6].symbol, "Sat"); month_list[0].code = 0; strcpy (month_list[0].symbol, "Jan"); month_list[1].code = 1; strcpy (month_list[1].symbol, "Feb"); month_list[2].code = 2; strcpy (month_list[2].symbol, "Mar"); month_list[3].code = 3; strcpy (month_list[3].symbol, "Apr"); month_list[4].code = 4; strcpy (month_list[4].symbol, "May"); month_list[5].code = 5; strcpy (month_list[5].symbol, "Jun"); month_list[6].code = 6; strcpy (month_list[6].symbol, "Jul"); month_list[7].code = 7; strcpy (month_list[7].symbol, "Aug"); month_list[8].code = 8; strcpy (month_list[8].symbol, "Sep"); month_list[9].code = 9; strcpy (month_list[9].symbol, "Oct"); month_list[10].code = 10; strcpy (month_list[10].symbol, "Nov"); month_list[11].code = 11; strcpy (month_list[11].symbol, "Dec"); bzero ( (char *)&tmval, sizeof (tmval)); bzero (week, sizeof (week)); bzero (month, sizeof (month)); /* * Parse date string into separate fields. Day of month, hour, * minute, and second are in format used by mktime() below. */ sscanf (date_str, "%s %s %u %u:%u:%u %u", week, month, &(tmval.tm_mday), &(tmval.tm_hour), &(tmval.tm_min), &(tmval.tm_sec), &year); /* Adjust entry for base year. */ tmval.tm_year = year - 1900; /* Store value for day of week. (Sunday = 0). */ for (i = 0; i < 7; i++) if (strcmp(week_list[i].symbol, week) == 0) break; if (i < 7) tmval.tm_wday = week_list[i].code; else return (0); /* Store value for month of year. (January = 0, December = 11). */ for (i = 0; i < 12; i++) if (strcmp (month_list[i].symbol, month) == 0) break; if (i < 12) tmval.tm_mon = month_list[i].code; else return (0); return (mktime (&tmval)); }/********************************************************************************* get_ip - extract numbers in IP address format** This routine extracts IP addresses and subnet masks (in dotted decimal * format) from the input string into the provided structure. It is used when * retrieving entries from the address pool and binding databases.** RETURNS: OK if successful, or ERROR otherwise.** ERRNO: N/A** NOMANUAL*/static STATUS get_ip ( char **src, /* current location in input string */ struct in_addr *target /* pointer to storage for extracted value */ ) { if (prs_inaddr (src, &target->s_addr) != 0) return (ERROR); return (OK); }/********************************************************************************* resipcmp - verify IP addresses for address pool entries** This routine compares a hash table key against the IP address of a address * pool entry. It is used when storing and retrieving lease descriptors with * the internal hash tables.** RETURNS: TRUE if addresses match, or FALSE otherwise.** ERRNO: N/A** NOMANUAL*/int resipcmp ( struct in_addr * key, /* pointer to hash table key */ struct dhcp_resource * rp /* pointer to lease resource */ ) { return (key->s_addr == rp->ip_addr.s_addr); }/********************************************************************************* relayipcmp - verify IP addresses for relay agent entries** This routine compares a hash table key against the IP address of a relay* agent. It is used when storing and retrieving relay agent entries in the * internal hash table.** RETURNS: TRUE if addresses match, or FALSE otherwise.** ERRNO: N/A** NOMANUAL*/int relayipcmp ( struct in_addr * key, /* pointer to hash table key */ struct relay_acl * aclp /* pointer to relay agent entry */ ) { return (key->s_addr == aclp->relay.s_addr); }/********************************************************************************* paramcidcmp - verify client identifiers for additional parameters** This routine compares a hash table key against the client identifier * contained in a lease descriptor to verify matching types and values. It * also checks the recorded subnet against the actual subnet. It is used to* verify client information when accessing the parameters hash table.** RETURNS: TRUE if entries OK, or FALSE otherwise.** ERRNO: N/A** NOMANUAL*/int paramcidcmp ( struct client_id * key, /* pointer to hash table key */ struct dhcp_resource * rp /* pointer to lease descriptor */ ) { return (rp != NULL && rp->binding != NULL && key->idtype == rp->binding->cid.idtype && key->idlen == rp->binding->cid.idlen && bcmp (key->id, rp->binding->cid.id, key->idlen) == 0); }/********************************************************************************* bindcidcmp - verify client identifiers for active or offered leases** This routine compares a hash table key against the client identifier * contained in a lease record to verify matching types and values. It also * checks the recorded subnet against the actual subnet. It is used to verify * client information when establishing or renewing leases in response to DHCP* request messages.** RETURNS: TRUE if entries OK, or FALSE otherwise.** ERRNO: N/A** NOMANUAL*/int bindcidcmp ( struct client_id * key, /* pointer to hash table key */ struct dhcp_binding * bp /* pointer to lease record */ ) { return (bp != NULL && key->subnet.s_addr == bp->cid.subnet.s_addr && key->idtype == bp->cid.idtype && key->idlen == bp->cid.idlen && bcmp (key->id, bp->cid.id, key->idlen) == 0); }/********************************************************************************* set_default - provide values for mandatory configuration parameters** This routine provides default values for fields in a lease descriptor if* not specified by the administrator in the corresponding address pool entry. * It ensures that every lease descriptor includes a default and maximum lease* length, and a subnet mask. Additionally, it makes entries which contain a * client identifier available for BOOTP clients, and resets the maximum lease * for all such entries to infinity.** RETURNS: TRUE if entries OK, or FALSE otherwise.** ERRNO: N/A** NOMANUAL*/void set_default ( struct dhcp_resource * rp /* pointer to lease descriptor */ ) { if (ISCLR (rp->valid, S_DEFAULT_LEASE)) { SETBIT (rp->valid, S_DEFAULT_LEASE); rp->default_lease = dhcps_dflt_lease; } if (ISCLR (rp->valid, S_MAX_LEASE)) { SETBIT (rp->valid, S_MAX_LEASE); rp->max_lease = dhcps_max_lease; } if (ISSET (rp->valid, S_CLIENT_ID)) { SETBIT (rp->valid, S_ALLOW_BOOTP); rp->allow_bootp = TRUE; } if (ISSET (rp->valid, S_ALLOW_BOOTP) && rp->allow_bootp == TRUE) { SETBIT(rp->valid, S_MAX_LEASE); rp->max_lease = 0xffffffff; /* infinity */ } if (ISCLR(rp->valid, S_SUBNET_MASK)) { SETBIT (rp->valid, S_SUBNET_MASK); default_netmask (&rp->ip_addr, &rp->subnet_mask); } return;}/********************************************************************************* default_netmask - determine the subnet for an IP address** This routine assigns a netmask for the given IP address based on the IP * class value. It is called if no netmask was specified in the corresponding * address pool entry. It makes no provision for subnetting. The DHCP server * will not issue an address to a client if it is not on the client's subnet. * Therefore, if subnetting is used, the netmask for each address pool entry * must be specified explicitly.** RETURNS: N/A** ERRNO: N/A** NOMANUAL*/static void default_netmask ( struct in_addr *ipaddr, /* pointer to IP address */ struct in_addr *dfltnetmask /* pointer to storage for extracted value */ ) { int top = 0; top = ntohl (ipaddr->s_addr) >> 30; switch (top) { case 0: /* class A */ case 1: /* class A */ dfltnetmask->s_addr = htonl (0xff000000); break; case 2: /* class B */ dfltnetmask->s_addr = htonl (0xffff0000); break; case 3: /* class C */ dfltnetmask->s_addr = htonl (0xffffff00); break; default: dfltnetmask->s_addr = 0xffffffff; break; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -