funcs.c

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

C
2,608
字号
	if (pda->type == PW_TYPE_TAG_INT)	{		logit (LOG_DAEMON, LOG_ALERT,			"%s: Doesn't support type tag-int for %s",			func, pda->name);		return (NULL_VP);	}	else	{		if (pda->type == PW_TYPE_TAG_STR)		{			logit (LOG_DAEMON, LOG_ALERT,				"%s: Doesn't support type tag-str for %s",				func, pda->name);			return (NULL_VP);		}	}	return avpair_add_attr_tag (list, pda, pval, len, 0);} /* end of avpair_add_vend () *//****************************************************************************** * *	Function: avpair_add_vend_tag * *	Purpose: Add a Vendor Specific attribute-value pair to the given list. * *	Returns: pointer to added a/v pair upon success, *		 NULL pointer, upon failure. * *	Remarks: Always appends the new pair to the end of the list. * *****************************************************************************/VALUE_PAIR *avpair_add_vend_tag (list, attrid, pval, len, vend_id, tag)VALUE_PAIR    **list;	 /* A list of attribute-value pairs. */int             attrid;	 /* Attribute id number. */void           *pval;	 /* Pointer to value. */int             len;	 /* for strings: */			 /*    len < 0  ==> null-terminated ASCII string */			 /*    len == 0 ==> this is a zero length string (!) */			 /*    len > 0  ==> len is length of raw binary data */			 /* for non-strings: */			 /*    len == 0 ==> just plain data, just gets copied */			 /*    len > 0  ==> convert data from network ... */			 /*		   ... representation before copying */UINT4           vend_id; /* Vendor ID */int             tag;	 /* The tag for tagged attributes */{	DICT_ATTR      *pda;	static char    *func = "avpair_add_vend_tag";	dprintf(3, (LOG_DAEMON, LOG_DEBUG, "%s: entered", func));	if ((pda = dict_attrget (attrid, vend_id)) == (DICT_ATTR *) NULL)	{		logit (LOG_DAEMON, LOG_ERR,			"%s: Unknown vendor %ld attribute %d",			func, vend_id, attrid);		return NULL_VP;	}	return avpair_add_attr_tag (list, pda, pval, len, tag);} /* end of avpair_add_vend_tag () *//****************************************************************************** * *	Function: avpair_assign * *	Purpose: Assign the given raw value to an attribute-value pair. * *	Returns: pointer to the value pair which was allocated, *		 or NULL on failure. * *****************************************************************************/VALUE_PAIR *avpair_assign (dp, pval, len)DICT_ATTR      *dp;	/* Pointer to attribute dictionary entry */void           *pval;	/* pointer to value to be assigned */int             len;	/* for strings: */			/*    len < 0  ==> null-terminated ASCII string */			/*    len == 0 ==> this is a zero length string (!) */			/*    len > 0  ==> len is length of raw binary data */			/* for non-strings: */			/*    len == 0 ==> just plain data, just gets copied */			/*    len > 0  ==> convert data from network ... */			/*		   ... representation before copying */{	static char    *func = "avpair_assign";	dprintf(3, (LOG_DAEMON, LOG_DEBUG, "%s: entered", func));	if (dp->type == PW_TYPE_TAG_INT)	{		logit (LOG_DAEMON, LOG_ALERT,			"%s: Doesn't support type tag-int for %s",			func, dp->name);		return (NULL_VP);	}	else	{		if (dp->type == PW_TYPE_TAG_STR)		{			logit (LOG_DAEMON, LOG_ALERT,				"%s: Doesn't support type tag-str for %s",				func, dp->name);			return (NULL_VP);		}	}	return avpair_assign_tag (dp, pval, len, -1);} /* end of avpair_assign () *//****************************************************************************** * *	Function: avpair_assign_tag * *	Purpose: Assign the given raw value to an attribute-value pair. * *	Returns: pointer to the value pair which was allocated, *		 or NULL, on failure. * *****************************************************************************/VALUE_PAIR *avpair_assign_tag (dp, pval, len, tag)DICT_ATTR      *dp;	/* Pointer to attribute dictionary entry */void           *pval;	/* pointer to value to be assigned */int             len;	/* for strings: */			/*    len < 0  ==> null-terminated ASCII string */			/*    len == 0 ==> this is a zero length string (!) */			/*    len > 0  ==> len is length of raw binary data */			/* for non-strings: */			/*    len == 0 ==> just plain data, just gets copied */			/*    len > 0  ==> convert data from network ... */			/*		   ... representation before copying */int             tag;	/* The tag for tagged attributes */{	UINT2           temp_short;	char           *dest;	char           *ptr;	VALUE_PAIR     *vp;	VP_STRING      *vpstr;	int             blen;	static char    *func = "avpair_assign_tag";	dprintf(3, (LOG_DAEMON, LOG_DEBUG, "%s: entered", func));	if (len > AUTH_STRING2_LEN)	{		logit (LOG_DAEMON, LOG_ERR, "%s: bad attribute length %d",			func, len);		return NULL_VP;	}	if ((vp = (VALUE_PAIR *) calloc (1, sizeof (VALUE_PAIR))) == NULL_VP)	{		logit (LOG_DAEMON, LOG_ALERT, "%s: FATAL out of memory", func);		abort ();	}	vp_mf.m++;	vp->attribute = dp->value;	vp->ap = dp;	/*	 *	If the dictionary says to always	 *	encapsulate, then always encapsulate!	 */	if ((dp->flags & ATTR_ENCAPS) != 0)	{		vp->flags |= VPF_ENCAPS;	}	vp->strsize = 0;	if (tag >= 0)	{		vp->tag = tag;	}	else	{		vp->tag = 0;	}	vp->next = NULL_VP;	ptr = pval;	switch (dp->type)	{#ifdef PW_TYPE_FILTER_BINARY	    case PW_TYPE_FILTER_BINARY:		if (len <= 0)		{			logit (LOG_DAEMON, LOG_ERR,				"%s: bad binary filter length %d", func, len);			free (vp);			vp_mf.f++;			return NULL_VP;		}		/***FALLTHROUGH***/#endif	/* PW_TYPE_FILTER_BINARY */	    case PW_TYPE_TAG_STR:		if (tag == -1)		{			vp->tag = *ptr;			ptr++;			len--;		}		/***FALLTHROUGH***/	    case PW_TYPE_OCTETS:	    case PW_TYPE_VENDOR:	    case PW_TYPE_STRING: /* Use lvalue field to store length. */		if (ptr == (char *) NULL)		{			ptr = "";		}		if (len < 0)		{			len = strlen (ptr);			if (doing_init)			{				vp->lvalue = len;				vp->strvalue = add_string (ptr, ASIS);				break;			}		}		vp->lvalue = len;		/* Allocate 2**x storage block for string */		for (blen = VPSTR_MIN; blen <= len; blen += blen)		{			continue;		}		if ((vpstr = (VP_STRING *) calloc (1, blen + VPSTR_HDR_LEN))							== (VP_STRING *) NULL)		{			logit (LOG_DAEMON, LOG_ALERT,				"%s: FATAL out of memory", func);			abort ();		}		active_vpstrings++;		vpstr->usecnt = 1;		vp->strvalue = vpstr->string;		vp->strsize = blen;	/* Remember the length we determined. */		memcpy (vp->strvalue, ptr, len);		vp->strvalue[len] = 0;	/* For the case of string type. */		break;	    case PW_TYPE_OCTET:		if (len == 1)		/* network value */		{			vp->lvalue = *(u_char *) pval;	/* Get the octet. */		}		else		{			if (len == 0)	/* internal value */			{				memcpy ((char *) &vp->lvalue, pval,					sizeof (vp->lvalue));			}			else			{				logit (LOG_DAEMON, LOG_ERR,			 "%s: Invalid len (%d) for PW_TYPE_OCTET attribute %s",					func, len, dp->name);				free (vp);				vp_mf.f++;				return NULL_VP;			}		}		vp->strvalue = (char *) NULL;	/* Insure null string */		break;	    case PW_TYPE_SHORT:		memcpy ((char *) &temp_short, pval, sizeof (temp_short));		if (len == 2)		/* network value */		{			temp_short = ntohs(temp_short);		}		else		{			if (len != 0)	/* NOT internal value */			{				logit (LOG_DAEMON, LOG_ERR,		      "%s: Invalid length (%d) for PW_TYPE_SHORT attribute %s",					func, len, dp->name);				free (vp);				vp_mf.f++;				return NULL_VP;			}		}		vp->lvalue = temp_short;		vp->strvalue = (char *) NULL;	/* Insure null string */		break;	    case PW_TYPE_TAG_INT:		ptr = pval;		vp->strvalue = (char *) NULL;	/* Insure null string */		dest = (char *) &vp->lvalue;		vp->lvalue = 0;			/* Initially nothing */		if (tag == -1)		{			vp->tag = *((u_char *) pval);			ptr++;		}		if (len > 0) /* need to convert the raw network value */		{			memcpy (dest + 1, ptr, sizeof (vp->lvalue) - 1);			vp->lvalue = ntohl(vp->lvalue);		}		else		{			if (tag == -1)			{				logit (LOG_DAEMON, LOG_ERR,				   "%s: FATAL ERROR, len=%d and tag=-1 for %s",					func, len, dp->name);				dumpcore = 1;				abort ();			}			else			{				memcpy (dest, ptr, sizeof (vp->lvalue));			}		}		break;	    case PW_TYPE_DATE:	    case PW_TYPE_INTEGER:	    case PW_TYPE_IPADDR:		memcpy ((char *) &vp->lvalue, pval, sizeof (vp->lvalue));		if (len > 0) /* need to convert the raw network value */		{			vp->lvalue = ntohl(vp->lvalue);		}		vp->strvalue = (char *) NULL; /* Insure null string */		break;	    default:		dprintf(1, (LOG_DAEMON, LOG_DEBUG,			"%s: Unknown type %d", func, dp->type));		free (vp);		vp_mf.f++;		return NULL_VP;	} /* end of switch */	return vp;} /* end of avpair_assign_tag () *//**************************************************************************** * *	Function: avpair_comp * *	Purpose: Compare two attribute/value pairs and *		 return an integer describing what happened... * *	returns: *		-1   both attribute/value pairs are of the same type *			but the value of a < value of b. *		 0   both attribute/value pairs are of the same type *			the value of a == the value of b. *		 1   both attribute/value pairs are of the same type *			but the value of a > value of b. *		 2   the types of the two attribute/value pairs are different. *		 3   One or both of the attribute/value pairs is NULL. * ****************************************************************************/intavpair_comp (a, b)VALUE_PAIR     *a;VALUE_PAIR     *b;{	int             result;	static char    *func = "avpair_comp";	if ((a == NULL_VP) || (b == NULL_VP))	{		return (3);	}	/* Simple, short circut check. */	if (a == b)	{		return (0);	}	/* Check to see if the types are the same. */	if (a->ap->type != b->ap->type)	{		return (2);	}	switch (a->ap->type)	{		/* Integer types. */	    case PW_TYPE_TAG_INT:		if (a->tag == b->tag)		{			if (a->lvalue == b->lvalue)			{				return (0);	/* a == b */			}			if (a->lvalue > b->lvalue)			{				return (1);	/* a > b */			}			return (-1);		/* a < b */		}		if (a->tag > b->tag)		{			return (1);	/* a > b */		}		return (-1);		/* a < b */	    case PW_TYPE_INTEGER:	    case PW_TYPE_IPADDR:	    case PW_TYPE_DATE:	    case PW_TYPE_OCTET:	    case PW_TYPE_SHORT:		if (a->lvalue == b->lvalue)		{			return (0);	/* a == b */		}		if (a->lvalue > b->lvalue)		{			return (1);	/* a > b */		}		return (-1);		/* a < b */		/* String types. */	    case PW_TYPE_TAG_STR:		if (a->tag == b->tag)		{			if (a->lvalue == b->lvalue)			{				return memcmp (a->strvalue, b->strvalue,						a->lvalue);			}			if (a->lvalue < b->lvalue)			{				if ((result =					memcmp (a->strvalue, b->strvalue,						a->lvalue)) != 0)				{					return (result);				}				return (-1);  /* a < b */			}			if ((result =				memcmp (a->strvalue, b->strvalue,					b->lvalue)) != 0)			{				return (result);			}			return (1);  /* a > b */		}		if (a->tag > b->tag)		{			return (1);	/* a > b */		}		return (-1);		/* a < b */	    case PW_TYPE_STRING:	    case PW_TYPE_VENDOR:	    case PW_TYPE_OCTETS:#ifdef PW_TYPE_FILTER_BINARY	    case PW_TYPE_FILTER_BINARY:#endif	/* PW_TYPE_FILTER_BINARY */		/*		 *	If the string pointers are the same,		 *	a pointer comparison will do.		 */		if (a->strvalue == b->strvalue)		{			return (0);		}		/* If the lengths are the same, a simple memcmp() will do */

⌨️ 快捷键说明

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