utils.c revision 7c478bd95313f5f23a4c958a745db2134aa03244
/*
* Copyright (c) 1999-2001 by Sun Microsystems, Inc.
* All rights reserved.
*/
/*
* Copyright (c) 1987 Regents of the University of California.
* All rights reserved.
*
* Redistribution and use in source and binary forms are permitted
* provided that the above copyright notice and this paragraph are
* duplicated in all such forms and that any documentation,
* advertising materials, and other materials related to such
* distribution and use acknowledge that the software was developed
* by the University of California, Berkeley. The name of the
* University may not be used to endorse or promote products derived
* from this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* file: utils.c
*
* This file contains the miscellaneous routines
* that don't seem to belong anywhere else, such
* as randomizers, address conversion, character
* manipulation, etc.
*/
#include <stdio.h>
#include <string.h>
#include <syslog.h>
#include <stdlib.h>
#include <ctype.h>
#include <netinet/if_ether.h>
#include "mip.h"
#include "agent.h"
#include "auth.h"
#define SECONDS_IN_A_DAY 86400
#define _POSIX_THREAD_SAFE_FUNCTIONS 1
extern int logVerbosity;
/* --------------------- debug utils ------------------------- */
/*
* Function: hexdump
*
* Arguments: message, buffer, length
*
* Description: This function will dump out a buffer to stderr in hex and
* ascii. It is usefull in debugging.
*
* Returns: void
*
*/
int
{
int i;
char text[17];
char TmpBuf[30];
char output[200];
int offset = 0;
int currBytes;
int result;
if (!length) {
/* don't waste our time with empty buffers */
return (0);
}
if (result) {
return (-1);
}
output[0] = 0;
/* for each line . . */
if (length <= 16)
else
currBytes = 16;
while (currBytes > 0) {
for (i = 0; i < currBytes; i++) {
if (i == 7)
}
for (i = currBytes; i < 16; i++) {
if (i == 7)
text[i] = ' ';
}
output[0] = 0;
if (length <= 16)
else
currBytes = 16;
}
if (result) {
}
return (0);
} /* hexdump */
/* --------------------- Platform-specific utilities -------------------- */
/*
* Function: randomInit
*
* Arguments:
*
* Description: initialize the random number generator
*
* Returns:
*/
void
{
if (gettimeofday(&time, 0) < 0) {
"Gettimeofday failed in randominit(), using fixed seed.");
srand48(181067);
} else {
}
}
/*
* Function: randomLong
*
* Arguments:
*
* Description: This function is used to retrieve a random
* 32-bit value.
*
* Returns: random value
*/
{
return ((unsigned long) lrand48());
}
/*
* Function: CurrentTimeNTPSec
*
* Arguments:
*
* Description: Returns the number of seconds elapsed since
* Jan 1, 1990. Only the higher 32 bits of the 64-bit
* quantity representing current time in NTP format.
*
* Returns: uint32_t - the number of seconds since Jan 1, 1990.
*/
{
if (gettimeofday(&time, 0) < 0) {
"Error: gettimeofday failed in CurrentTimeNTPSec()");
return (0);
} else {
}
}
/*
* Function: sprintTime
*
* Arguments: buffer - Pointer to output buffer
* buflen - Length of output buffer
*
* Description: Print the current time in buffer. If buffer is large
* enough, the current time is printed in the format:
* Fri Sep 13 00:00:00 1986\0
*
* This function is used for debugging purposes only.
*
* Returns: If successful, a pointer to the buffer is returned, otherwise
* NULL is returned.
*/
char *
{
if (buflen >= 26) {
(void) gettimeofday(&clock, 0);
#ifndef lint
/* Lint has a problem picking the right one */
#ifdef _POSIX_THREAD_SAFE_FUNCTIONS
#else
#endif /* _POSIX_THREAD_SAFE_FUNCTIONS */
#endif /* lint */
return (buffer);
} else {
return ("");
}
}
/*
* Function: sprintRelativeTime
*
* Arguments: buffer - Pointer to output buffer
* buflen - Length of output buffer
*
* Description: Print relative time since first call to this routine
* in the form:
* seconds.xxxxxx\0
*
* This function is used for debugging purposes only.
*
* Returns: If successful, a pointer to the buffer is returned, otherwise
* NULL is returned.
*/
char
{
static int i = 0;
if (buflen < 20) {
return ("");
}
if (i == 0) {
(void) gettimeofday(&starttime, 0);
i = 1;
} else {
(void) gettimeofday(&now, 0);
diff_sec -= 1;
} else {
}
}
return (buffer);
}
/* --------------------- Platform-independent utilities -------------------- */
/*
* Function: inChecksum
*
* Arguments: addr - Pointer to address
* len - length of address
*
* Description: Compute the internet checksum for len number of
* bytes starting at addr
*
* Returns: a unsigned short containing the checksum.
*/
unsigned short
{
register unsigned short *w = addr;
register unsigned short answer;
unsigned short odd_byte = 0;
register int sum = 0;
/*
* Our algorithm is simple, using a 32 bit accumulator (sum),
* we add sequential 16 bit words to it, and at the end, fold
* back all the carry bits from the top 16 bits into the lower
* 16 bits.
*/
while (nleft > 1) {
sum += *w++;
nleft -= 2;
}
/* mop up an odd byte, if necessary */
if (nleft == 1) {
*(unsigned char *)(&odd_byte) = *(unsigned char *)w;
}
/*
* add back carry outs from top 16 bits to low 16 bits
*/
return (answer);
}
/*
* Function: hwAddrWrite
*
* Arguments: hwaddr - Pointer to the string containing
* the MAC address
* hwStr - Pointer to the output buffer
*
* Description: This function will convert a MAC address
* to a text format (e.g. aa:bb:cc:dd:ee:ff).
*
* Returns: a pointer to the output buffer
*/
char *
(unsigned char) hwaddr[0],
(unsigned char) hwaddr[1],
(unsigned char) hwaddr[2],
(unsigned char) hwaddr[3],
(unsigned char) hwaddr[4],
(unsigned char) hwaddr[5]);
return (hwStr);
}
/*
* Function: ntoa
*
* Arguments: addr_long - Address
* addr_string - Pointer to the output buffer
*
* Description: Converts the long value in addr_long to an internet
* address of the type a.b.c.d in addr_string and
* returns a pointer to addr_string.
*
* NOTE: addr_long is in network byte order and
* addr_string is assumed to be long enough (at
* least INET_ADDRSTRLEN in length).
*
* By having the caller supply its own buffer, we try
* to avoid problems associated with multiple threads
* writing the same buffer concurrently.
*
* Returns: the pointer to the output buffer
*/
char *
{
return (addr_string);
}
/*
* Function: hexDigit
*
* Arguments: c - character
*
* Description: This function is used to determine if a character
* is a valid hex digit, and returns the decimal
* equivalent.
*
* Returns: int, -1 if the character is not a valid hex digit.
* Otherwise, the decimal value is returned.
*/
static int
hexDigit(int c)
{
if (c >= '0' && c <= '9')
return (c - '0');
if (c >= 'a' && c <= 'f')
return (c - 'a' + 10);
if (c >= 'A' && c <= 'F')
return (c - 'A' + 10);
return (-1);
}
/*
* Function: hexConvert
*
* Arguments: key - Pointer to output buffer
* len - length of key
* keystr - Pointer to input buffer containing the key.
*
* Description: Convert a string of hexadecimal digits into an array
* of len bytes each containing a byte represented by a
* pair of hex digits, e.g. keystr="12abC4De" is converted
* to key[]={0x12, 0xab, 0xC4, 0xDe} key has space for
* len bytes only.
*
* Returns: int, Returns -1 if the key has invalid data
*/
int
{
for (i = 0; i < len; ++i) {
return (-1);
return (-1);
}
return (0);
}
/*
* Function: prefixLen
*
* Arguments: netmask - Network Mask
*
* Description: Computes prefix length of a network mask. Assumes
* it is the same as the number of bits set to one.
*
* Returns: int, containing the netmask bits.
*/
int
{
int len = 0;
while (netmask) {
len++;
}
return (len);
}
/*
* Function: printBuffer
*
* Arguments: buffer - Pointer to input buffer
* buflen - Length of data in buffer
*
* Description: Prints a buffer in hexadecimal
*
* Returns:
*/
void
{
unsigned int i;
if (buflen == 6) {
/* Is likely an ethernet address */
for (i = 0; i < buflen; i++) {
if (i < (buflen-1))
mipverbose((":"));
}
} else {
/* Short enough to be printed in one shot */
if (buflen <= 16) {
for (i = 0; i < buflen; i++)
} else {
/* Possibly a packet */
mipverbose(("\t"));
for (i = 0; i < buflen; i++) {
if ((i % 2) == 1)
mipverbose((" "));
if ((i % 16) == 15)
mipverbose(("\n\t"));
}
}
}
}
/*
* Function: printMaAdvConfigEntry
*
* Arguments: macep - Pointer to the interface entry.
* p1 - unused
*
* Description: Prints the contents of a MaAdvConfigEntry
*
* Returns:
*/
/* ARGSUSED */
static boolean_t
{
char addr[20];
mipverbose(("MaAdvConfigEntry contents:\n"));
mipverbose(("\tHWaddr : "));
mipverbose(("\n"));
}
mipverbose(("\tPrefix : %s\n",
return (_B_TRUE);
}
#ifdef FIREWALL_SUPPORT
/*
* Function: printProtectedDomainInfo
*
* Arguments: domainInfo - Pointer to the Domain Info
* entry.
*
* Description: Prints information about a DomainInfo
*
* Returns:
*/
void
{
int i;
if (domainInfo.addrIntervalCnt > 0) {
mipverbose(("Protected Domain Info: Address - Netmask pairs\n"));
for (i = 0; i < domainInfo.addrIntervalCnt; i++) {
mipverbose((" %s/%s\n",
}
}
if (domainInfo.firewallCnt > 0) {
mipverbose(("List of firewall addresses : "));
for (i = 0; i < domainInfo.firewallCnt; i++) {
}
}
mipverbose(("\n"));
}
#endif /* FIREWALL_SUPPORT */
/*
* Function: printMaAdvConfigHash
*
* Arguments: htbl - Pointer to the Hash Table
*
* Description: This function will print information about
* all interfaces in the hash.
*
* Returns:
*/
void
{
mipverbose(("------ MaAdvConfigHash contents ------\n"));
_B_FALSE);
}
#if 0
/*
* Function: printFaVisitorEntry
*
* Arguments: favep - Pointer to the visitor entry
*
* Description: Prints the contents of a FaVisitorEntry. This
* function is not currently in use today, but will
* if static visitor entries are implemented.
*
* Returns:
*/
/* ARGSUSED */
static boolean_t
{
char addr[INET_ADDRSTRLEN];
struct ether_addr ether;
mipverbose(("FaVisitorEntry contents:\n"));
mipverbose(("\tIface : %s\n",
"Accepted" : "Pending")));
mipverbose(("\tTimeLeft : %ld\n",
} else
mipverbose(("\tMN SLLA : unknown\n"));
return (_B_TRUE);
}
/*
* Function: printFaVisitorHash
*
* Arguments: htbl - Pointer to the Hash Table
*
* Description: Prints information about all visitor entries
* in the hash table. This function is not
* currently in use today, but will if static
* visitor entries are implemented.
*
* Returns:
*/
void
{
mipverbose(("------ FaVisitorHash contents ------\n"));
_B_FALSE);
}
/*
* Function: printFaUnreachableEntry
*
* Arguments: fauep - Pointer to the unreachable entry.
*
* Description: Prints the contents of a FaUnreachableEntry.
* This function is not currently in use, but could
* be if a feature was implemented that allowed
* blocking of access (statically) to a Home Agent.
*
* Returns:
*/
/* ARGSUSED */
static boolean_t
{
char addr[INET_ADDRSTRLEN];
mipverbose(("FaUnreachableEntry contents:\n"));
return (_B_TRUE);
}
/*
* Function: printFaUnreachableHash
*
* Arguments: htbl - Pointer to the Hash Table.
*
* Description: Prints out information about all unreachable
* entries in the hash.
*
* This function is not currently in use, but could
* be if a feature was implemented that allowed
* blocking of access (statically) to a Home Agent.
*
* Returns:
*/
void
{
mipverbose(("------ FaUnreachableHash contents ------\n"));
_B_FALSE);
}
#endif
/* Prints the contents of a HaBindingEntry. */
/*
* Function: printHaBindingEntry
*
* Arguments: habep - Pointer to the Binding Entry
*
* Description: This function will print out the contents
* of a binding entry. This function is not currently
* in use, and could be if static bindings were
* implemented.
*
* Returns:
*/
#ifndef lint
{
char addr[INET_ADDRSTRLEN];
mipverbose(("HaBindingEntry contents:\n"));
mipverbose(("\tExpires : %ld\n",
return (_B_TRUE);
}
/*
* Function: printHaBindingHash
*
* Arguments: htbl - Pointer to the Hash Table
*
* Description: This function will print out the contents
* of all binding entries in the hash. This
* function is not currently in use, and could
* be if static bindings were implemented.
*
* Returns:
*/
void
{
mipverbose(("------ HaBindingHash contents ------\n"));
_B_FALSE);
}
#endif /* lint */
/*
* Function: printMSAE
*
* Arguments: msae - Pointer to the Security Assoc Entry
*
* Description: This function will print out the contents of
* a security association entry.
*
* Returns:
*/
static void
{
mipverbose(("\tMipSecAssocEntry contents:\n"));
}
/*
* Function: printHaMobileNodeEntry
*
* Arguments: hamne - Pointer to the Mobile Node Entry
*
* Description: This function will print the contents of a
* Mobile Node Entry.
*
* Returns:
*/
static boolean_t
{
char addr[INET_ADDRSTRLEN];
mipverbose(("HaMobileNodeEntry contents:\n"));
while (habep) {
mipverbose(("\tHaMobileNodeEntry BindingEntry contents:\n"));
addr)));
addr)));
addr)));
mipverbose(("\t\tExpires : %ld\n",
mipverbose(("\t\tReg Flag : %x (SBDMGV__)\n",
}
}
return (_B_TRUE);
}
/*
* Print HaMobileNodeHash contents
*/
/*
* Function: printHaMobileNodeHash
*
* Arguments: htbl - Pointer to the Hash Table
*
* Description: This function will print the contents
* of all Mobile Node Entries in the hash.
*
* Returns:
*/
void
{
mipverbose(("------ HaMobileNodeHash contents ------\n"));
} /* printHaMobileNodeHash */