funcs.c

来自「RADIUS协议的认证计费服务」· C语言 代码 · 共 2,608 行 · 第 1/5 页

C
2,608
字号
			logit (LOG_DAEMON, LOG_ERR,			   "%s: FATAL: unable to determine index_case for '%s'",				func, string);			dumpcore = 1;			abort ();		}		find = add_string_table[index];		for ( ; find; find = find->next)		{			if (add_string_strlowercmp (string, find) == 0)			{				return (find->buff);			}		}	}	else	{		index = add_string_hash (string);		if (index < 0)		{			logit (LOG_DAEMON, LOG_ERR,				"%s: FATAL: unable to determine index for '%s'",				func, string);			dumpcore = 1;			abort ();		}		find = add_string_table[index];		for ( ; find; find = find->next)		{			if (strcmp (string, find->buff) == 0)			{				return (find->buff);			}		}	}	/* Check to see if this is an existence test... */	if (convert & FINDONLY)        {		return (NULL);	}	/* Insert it now (or fail) */	return add_string_insert (string, index, convert);} /* end of add_string () *//****************************************************************************** * *	Function: add_string_dump * *	Purpose: Report on utilization of add_string hash table. * *	Note: Only generates report if debugging is on. * *****************************************************************************/voidadd_string_dump (){	int                i;	int                len;	static char       *func = "add_string_dump";	dprintf(4, (LOG_DAEMON, LOG_DEBUG, "%s: entered", func));	if (debug_flag <= 0)	{		return;	}	logit (LOG_DAEMON, LOG_DEBUG, "%s: %d strings added",		func, add_string_news);	for (i = 0; i < ADD_STRING_HASH_SIZE; i++)	{		len = add_string_qlen (add_string_table[i]);		if (len > 0)		{			logit (LOG_DAEMON, LOG_DEBUG,				"%s: %5d -> %3d (%lu bytes)",				func, i, len,				add_string_qstrlen (add_string_table[i]));		}	}} /* end of add_string_dump () *//****************************************************************************** * *	Function: add_string_hash * *	Purpose: Produce a hash value between 0 and ADD_STRING_HASH_SIZE *		 based on an ASCII string. * *	Returns: hash value ( 0 <= value < ADD_STRING_HASH_SIZE ) *		 -1 indicates a NULL string *		 -2 indicates a non-ASCII string. * *****************************************************************************/static intadd_string_hash (str)char *str;{	int                result = 0;		/* Returned value. */	int                value;		/* Intermediate value. */	static char       *func = "add_string_hash";	dprintf(5, (LOG_DAEMON, LOG_DEBUG, "%s: entered", func));	if (str == (char *) NULL)	{		logit (LOG_ERR, LOG_DAEMON, "%s: NULL parameter", func);		return -1;	}	for ( ; *str ; str++ )	{	        /* Allow only ASCII values in here. */		if ((value = (u_char) *str) < 0x01)		{			logit (LOG_ERR, LOG_DAEMON,				"%s: Non-ASCII value, 0x%2.2x", func, *str);			return (-2);		}		value -= (u_char) 0x01;		result += value + 1;	/* Include length of string in hash. */	}	/* Normalize this to the hash table size. */	result = (result % ADD_STRING_HASH_SIZE);	return (result);	/* Return calculated hash value. */} /* end of add_string_hash () *//****************************************************************************** * *	Function: add_string_hash_lowercase * *	Purpose: Produce a hash value between 0 and ADD_STRING_HASH_SIZE *		 based on an ASCII string.  Make the hash case-insensitive. * *	Returns: hash value ( 0 <= value < ADD_STRING_HASH_SIZE ) *		 -1 indicates a NULL string *		 -2 indicates a non-ASCII string. * *****************************************************************************/static intadd_string_hash_lowercase (str)char *str;{	int                result = 0;		/* Returned value. */	int                value;		/* Intermediate value. */	static char       *func = "add_string_hash_lowercase";	dprintf(5, (LOG_DAEMON, LOG_DEBUG, "%s: entered", func));	if (str == (char *) NULL)	{		logit (LOG_ERR, LOG_DAEMON, "%s: NULL parameter", func);		return -1;	}	for ( ; *str ; str++)	{		/* Allow only ASCII values in here. */		if ((value = (u_char) *str) < 0x01)		{			logit (LOG_ERR, LOG_DAEMON,				"%s: Non-ASCII value, 0x%2.2x", func, *str);			return (-2);		}		/* Make this lowercase, as necessary. */		if (isupper(value))		{			value = tolower(value);		}				value -= (u_char) 0x01;		result += value + 1;	/* Include length of string in hash. */	}	/* Normalize this to the hash table size. */	result = (result % ADD_STRING_HASH_SIZE);	return (result);	/* Return calculated hash value. */} /* end of add_string_hash_lowercase () *//****************************************************************************** * *	Function: add_string_insert * *	Purpose: Allocate and insert a new string into the hashed *		 string tables. * *	Returns: A pointer to the string in the table. * *	Remarks: Uses pre-computed hash values, if available. * *****************************************************************************/static char *add_string_insert (str, index, convert)char       *str;	/* Source string to copy and insert. */int         index;	/* Hash index value, or -1 */AS_CONVERT  convert;	/* Convert string to lowercase? */{	int                 changed = 0;	/* Was string changed? */	int                 len;		/* Length of string. */	char               *dest;		/* Copy character to here. */	char               *from;		/* Copy character from here. */	ADD_STRING_STRING  *new;		/* New string to insert. */	ADD_STRING_STRING **put;		/* How we insert. */	static char        *func = "add_string_insert";	dprintf(5, (LOG_DAEMON, LOG_DEBUG, "%s: entered", func));	/* Use separate malloc() for every string.  Don't block for now. */	len = sizeof (ADD_STRING_STRING) + strlen (str); /* buf[1] makes room */	if ((new = (ADD_STRING_STRING *) malloc (len)) ==						(ADD_STRING_STRING *) NULL)	{		logit (LOG_DAEMON, LOG_ERR, "%s: FATAL: malloc(%d) for '%s'",			func, len, str);		abort ();	}	new->next = (ADD_STRING_STRING *) NULL;	add_string_news++;		/* Count number we allocate. */	/* If lowercase conversion required, do it here. */	if (convert & ASLC)	{		for (dest = new->buff, from = str;			*from != '\0';			dest++, from++)		{			if (isupper(*from))			{				changed = 1;	/* remember to change hash */				*dest = tolower(*from);			}			else			{				*dest = *from;	/* simple copy */			}		}		*dest = '\0';	/* Terminate string properly. */	}	else	{		strcpy (new->buff, str);	/* straight in */	}	/* If we changed the string to lowercase, recompute the hash. */	if (changed > 0)	{		index = add_string_hash (new->buff);	}	/* Put it at the end of the hash-list. */	for (put = &add_string_table[index]; *put; put = &((*put)->next))	{		;	/* continue */	}	*put = new;		/* Add it to the end of the list. */	/* And return the string. */	return (new->buff);} /* end of add_string_insert () *//****************************************************************************** * *	Function: add_string_strlowercmp * *	Purpose: Compare a test string against a table entry like *		 strcmp()/strcasecmp(). * *	Returns: 0 if the strings are equal *		-1 if the test string is "less" than the table entry. *		 1 if the test string is "more" than the table entry. *		-2 if there is a parameter error (and errno is set.) * *****************************************************************************/static intadd_string_strlowercmp (test, list)char           *test;		/* case insensitive test string */ADD_STRING_STRING *list;	/* !!! case SENSITIVE comparison string */{	u_char             ch;		/* Copy here from test for lowering. */	u_char            *pos;		/* Our position in list. */	static char       *func = "add_string_strlowercmp";	dprintf(4, (LOG_DAEMON, LOG_DEBUG, "%s: entered", func));	if ((test == (char *) NULL) || (list == (ADD_STRING_STRING *) NULL))	{		errno = EINVAL;		return (-2);		/* XXX: Should abort() here instead? */	}	for (pos = (u_char *) list->buff;		(*test != '\0') && (*pos != '\0');		test++, pos++)	{		ch = (u_char) *test;  /* Get test character to check. */		if (isupper(ch))		{			ch = tolower(ch);	/* Force it to lowercase. */		}		if (ch != (u_char) *pos)		{			if (ch < (u_char) *pos)			{				return (-1);			}			/* fall through to else. */			return (1);		}	}	if ((*pos == '\0') && (*test == '\0'))	{		return (0);	/* equal strings! */	}	if (*test != '\0')	{		return (1);	/* If any of 'test' remains, it is greater! */	}	return (-1);		/* Otherwise... */} /* end of add_string_strlowercmp () *//****************************************************************************** * *	Function: add_string_qlen * *	Purpose: Report the number of elements of the hash list. * *	Returns: Count of the number of elements in the hash list. * *****************************************************************************/static intadd_string_qlen (list)ADD_STRING_STRING *list;{	int                result = 0;	static char       *func = "add_string_qlen";	dprintf(5, (LOG_DAEMON, LOG_DEBUG, "%s: entered", func));	for ( ; list != (ADD_STRING_STRING *) NULL; list = list->next)	{		result++;	}	return result;} /* end of add_string_qlen () *//****************************************************************************** * *	Function: add_string_qstrlen * *	Purpose: Sum the lengths of all the strings in the hash list. * *	Returns: Sum of the lengths of all the strings in the hash list. * *****************************************************************************/static UINT4add_string_qstrlen (list)ADD_STRING_STRING *list;{	UINT4              result = 0L;	static char       *func = "add_string_qstrlen";	dprintf(4, (LOG_DAEMON, LOG_DEBUG, "%s: entered", func));	for ( ; list != (ADD_STRING_STRING *) NULL; list = list->next)	{		result += strlen (list->buff);	}	return result;} /* end of add_string_qstrlen () *//****************************************************************************** * *	Function: attribute_out * *	Purpose: Directly add an attribute (w/o an avpair) to the output buffer * *	Returns: length of packet (positive values), *		  0, error -- unable to insert attribute (general), *		 -1, fatal error ???, *		 -2, maxlen exceeded. * *****************************************************************************/intattribute_out (auth, maxlen, attribute, vendor_id, valptr, len, av_flags, veps)AUTH_HDR       *auth;          /* packet buffer */UINT4           maxlen;int             attribute;UINT4           vendor_id;void           *valptr;int             len;int             av_flags;VENDOR_LIST    *veps;		/* Do vendor specific mapping, if present */{	int              ret;	DICT_ATTR       *attr;	static char     *func = "attribute_out";	dprintf(3, (LOG_DAEMON, LOG_DEBUG, "%s: entered", func));	if ((attr = dict_attrget (attribute, vendor_id)) == (DICT_ATTR *) NULL)	{		logit (LOG_DAEMON, LOG_ALERT,			"%s: FATAL: Couldn't find attr %d(%ld)",			func, attribute, vendor_id);		dumpcore = 1;		/* Force a core dump to find out why */		abort ();	}	/* This code doesn't support tags. */	if (attr->type == PW_TYPE_TAG_STR)	{		logit (LOG_DAEMON, LOG_ALERT,			"%s: Doesn't support tag-str data type for %s",			func, attr->name);		return (0);	}	else if (attr->type == PW_TYPE_TAG_INT)	{		logit (LOG_DAEMON, LOG_ALERT,			"%s: Doesn't support tag-int data type for %s",			func, attr->name);		return (0);	}	if ((attr->flags & ATTR_ENCAPS) != 0)	{		av_flags |= VPF_ENCAPS;	}	ret = insert_attribute (attr, auth, maxlen, valptr,				len, av_flags, veps, 0);	/* tag == 0 */	if (ret <= 0)	{		logit (LOG_DAEMON, LOG_ALERT,	"%s: insert_attribute(%s(%d), ..., %d, 0x%p, %d, 0x%x, %s) returns %d",			func, attr->name, attribute, maxlen, valptr, len,			av_flags, vendor_list_toa (veps), ret);	}	return ret;} /* end of attribute_out () *//****************************************************************************** * *	Function: attribute_out_tag * *	Purpose: Directly add an attribute (w/o an avpair) to the output buffer * *	Returns: length of packet (positive values), *		  0, error -- unable to insert attribute (general), *		 -1, fatal error ???, *		 -2, maxlen exceeded. * *****************************************************************************/intattribute_out_tag (auth, max, attr, vendor_id, valptr, len, tag, av_flags, veps)AUTH_HDR       *auth;		/* The packet buffer. */UINT4           max;		/* Maximum output length. */int             attr;		/* The attribute number. */UINT4           vendor_id;void           *valptr;int             len;int             tag;		/* The tag for tagged attributes. */int             av_flags;VENDOR_LIST    *veps;		/* Do vendor specific mapping, if present. */{	int              ret;	DICT_ATTR       *attribute;	static char     *func = "attribute_out_tag";	dprintf(3, (LOG_DAEMON, LOG_DEBUG, "%s: entered", func));	if ((attribute = dict_attrget (attr, vendor_id)) == (DICT_ATTR *) NULL)	{		logit (LOG_DAEMON, LOG_ALERT,

⌨️ 快捷键说明

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