funcs.c
来自「RADIUS协议的认证计费服务」· C语言 代码 · 共 2,608 行 · 第 1/5 页
C
2,608 行
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 */ default: logit (LOG_DAEMON, LOG_ERR, "%s: Warning -- Unknown type (%d) for %s or %s", func, a->ap->type, a->ap->name, b->ap->name); return (4); } /* end of switch */} /* end of avpair_comp () *//************************************************************************* * * Function: avpair_del * * Purpose: Remove the value pair with the given attribute * and Vendor ID. * *************************************************************************/voidavpair_del (vp_list, attrid, vend_id)VALUE_PAIR **vp_list;int attrid;UINT4 vend_id;{ VALUE_PAIR **prev; VALUE_PAIR *curr; static char *func = "avpair_del"; dprintf(3, (LOG_DAEMON, LOG_DEBUG, "%s: entered", func)); prev = vp_list; curr = *prev; while (curr != NULL_VP) { if (curr->attribute == attrid && curr->ap->vendor_id == vend_id && !(curr->ap->flags & ATTR_CONFIG)) { *prev = curr->next; avpair_free (curr); curr = *prev; continue; } prev = &curr->next; curr = curr->next; } return;} /* end of avpair_del () *//************************************************************************* * * Function: avpair_del_ci * * Purpose: Remove the configuration value pair with the given * attribute and Vendor ID. * *************************************************************************/voidavpair_del_ci (vp_list, attrid, vend_id)VALUE_PAIR **vp_list;int attrid;UINT4 vend_id;{ VALUE_PAIR **prev; VALUE_PAIR *curr; static char *func = "avpair_del_ci"; dprintf(3, (LOG_DAEMON, LOG_DEBUG, "%s: entered", func)); prev = vp_list; curr = *prev; while (curr != NULL_VP) { if (curr->attribute == attrid && curr->ap->vendor_id == vend_id && (curr->ap->flags & ATTR_CONFIG)) { *prev = curr->next; avpair_free (curr); curr = *prev; continue; } prev = &curr->next; curr = curr->next; } return;} /* end of avpair_del_ci () *//****************************************************************************** * * Function: avpair_dup * * Purpose: Make a copy of the given attribute-value pair. * * Returns: pointer to the new copy. * ******************************************************************************/VALUE_PAIR *avpair_dup (psrc)VALUE_PAIR *psrc;{ VALUE_PAIR *pnew; VP_STRING *vpstr; static char *func = "avpair_dup"; dprintf(3, (LOG_DAEMON, LOG_DEBUG, "%s: entered", func)); if ((pnew = (VALUE_PAIR *) calloc (1, sizeof (VALUE_PAIR))) == NULL_VP) { logit (LOG_DAEMON, LOG_ALERT, "%s: FATAL out of memory", func); abort (); } vp_mf.m++; memcpy ((char *) pnew, (char *) psrc, sizeof (VALUE_PAIR)); /* Copy the string, if not an add_string () */ if (pnew->strvalue != (char *) NULL && pnew->strsize != 0) { /* Just bump the use count for the string */ vpstr = (VP_STRING *) (pnew->strvalue - VPSTR_HDR_LEN); vpstr->usecnt++; } pnew->next = NULL_VP; return pnew;} /* end of avpair_dup () *//****************************************************************************** * * Function: avpair_free * * Purpose: Frees the given avpair. Free's any allocated string storage. * * *****************************************************************************/voidavpair_free (vp)VALUE_PAIR *vp;{ static char *func = "avpair_free"; dprintf(4, (LOG_DAEMON, LOG_DEBUG, "%s: entered", func)); if (vp->strvalue != (char *) NULL && vp->strsize != 0) { avpair_string_free (vp->strvalue); } free (vp); vp_mf.f++; return;} /* end of avpair_free () *//******************************************************************************* * * Function: avpair_get * * Purpose: Find specified a/v pair from a list and return its value. * * Returns: whatever avpair_get_vend() returns. * ******************************************************************************/intavpair_get (pval, vplist, attrid)void *pval; /* OUT: buffer for returning value */VALUE_PAIR *vplist; /* pointer to list of attribute-value pairs */int attrid; /* attribute to find */{ static char *func = "avpair_get"; dprintf(3, (LOG_DAEMON, LOG_DEBUG, "%s: entered", func)); return avpair_get_vend (pval, vplist, attrid, 0);} /* end of avpair_get () *//******************************************************************************* * * Function: avpair_get_vend * * Purpose: Find specified vendor a/v pair from a list and return its * value. * * Returns: type of the attribute, on success (look at PW_TYPE_*), * or -1, on failure. * * Remarks: This function does not support PW_TYPE_TAG_INT, etc. * ******************************************************************************/intavpair_get_vend (pval, vplist, attrid, vendorid)void *pval; /* OUT: buffer for returning value */VALUE_PAIR *vplist; /* pointer to list of attribute-value pairs */int attrid; /* attribute to find */int vendorid; /* vendor id of attribute to find */{ VALUE_PAIR *vp; static char *func = "avpair_get_vend"; dprintf(3, (LOG_DAEMON, LOG_DEBUG, "%s: entered", func)); if ((vp = get_vp_vend (vplist, attrid, vendorid)) == NULL_VP) { return (-1); } switch (vp->ap->type) {#ifdef BINARY_FILTERS case PW_TYPE_FILTER_BINARY:#endif /* BINARY_FILTERS */ case PW_TYPE_OCTETS: case PW_TYPE_VENDOR: case PW_TYPE_STRING: memcpy ((char *) pval, vp->strvalue, vp->lvalue); break; case PW_TYPE_OCTET: *((u_char *) pval) = vp->lvalue; break; case PW_TYPE_SHORT: *((UINT2 *) pval) = vp->lvalue; break; case PW_TYPE_DATE: case PW_TYPE_INTEGER: case PW_TYPE_IPADDR: *((UINT4 *) pval) = vp->lvalue; break; default: logit (LOG_DAEMON, LOG_ALERT, "%s: Unknown attribute %s type %d", func, vp->ap->name, vp->ap->type); } return vp->ap->type;} /* end of avpair_get_vend () *//************************************************************************* * * Function: avpair_list_length * * Purpose: Compute the length of a singly linked list. * * Returns: list length, * or -1, if local "infinity" exceeded. * *************************************************************************/static intavpair_list_length (vp)VALUE_PAIR *vp; /* list to calculate. */{ int count = 0; /* Start with nothing. */ static char *func = "avpair_list_length"; for ( ; vp != NULL_VP; vp = vp->next) { count++; /* Count it up. */ if (count > AVPAIR_LIST_LENGTH_MAX) { return (-1); } } return count;} /* end of avpair_list_length () *//************************************************************************* * * Function: avpair_out * * Purpose: Puts avpair in output buffer, prefixing with vendor * specific code, if necessary. * *************************************************************************/intavpair_out (vp, auth, maxlen, veps)VALUE_PAIR *vp;AUTH_HDR *auth;UINT4 maxlen; /* Maximum length of output buffer */VENDOR_LIST *veps; /* Do vendor specific mapping, if present */{ int ret; static char *func = "avpair_out"; dprintf(3, (LOG_DAEMON, LOG_DEBUG, "%s: entered", func)); debug_pair (stdout, vp); switch (vp->ap->type) {#ifdef PW_TYPE_FILTER_BINARY case PW_TYPE_FILTER_BINARY:#endif /* PW_TYPE_FILTER_BINARY */ case PW_TYPE_OCTETS: case PW_TYPE_VENDOR: case PW_TYPE_STRING: case PW_TYPE_TAG_STR: ret = insert_attribute (vp->ap, auth, maxlen, vp->strvalue, vp->lvalue, vp->flags, veps, vp->tag); if (ret <= 0) { logit (LOG_DAEMON, LOG_ALERT, "%s: insert_attribute(%s(%d), ..., %u, 0x%p, %d, 0x%x, %s) returns %d", func, vp->ap->name, vp->attribute, maxlen, vp->strvalue, vp->lvalue, vp->flags, vendor_list_toa (veps), ret); } return ret; default: /* Assume all others are represented in lvalue */ ret = insert_attribute (vp->ap, auth, maxlen, &vp->lvalue, 4, vp->flags, veps, vp->tag); if (ret <= 0) { logit (LOG_DAEMON, LOG_ALERT, "%s: insert_attribute(%s(%d), ..., %d, 0x%p->%d, 0, 0x%x, %s) returns %d", func, vp->ap->name, vp->attribute, maxlen, &vp->lvalue, vp->lvalue, vp->flags, vendor_list_toa (veps), ret); } return ret; }} /* end of avpair_out () *//****************************************************************************** * * Function: avpair_string_free * * Purpose: Decrements string use count and frees if 0. * * *****************************************************************************/static voidavpair_string_free (string)char *string;{ VP_STRING *vpstr; static char *func = "avpair_string_free"; dprintf(3, (LOG_DAEMON, LOG_DEBUG, "%s: entered", func)); vpstr = (VP_STRING *) (string - VPSTR_HDR_LEN); if (--vpstr->usecnt == 0) { active_vpstrings--; free (vpstr); } return;} /* end of avpair_string_free () *//************************************************************************* * * Function: avpair_string_mod * * Purpose: Modify strvalue in an a/v pair. * *************************************************************************/voidavpair_string_mod (vp, string, len)VALUE_PAIR *vp;char *string;int len;{ int blen; VP_STRING *vpstr; static char *func = "avpair_string_mod"; dprintf(3, (LOG_DAEMON, LOG_DEBUG, "%s: entered", func)); if (len == -1) { len = strlen (string); } /* First see if we can just copy into existing region */ if (vp->strvalue == NULL || vp->strsize <= len || ((VP_STRING *) (vp->strvalue - VPSTR_HDR_LEN))->usecnt > 1) { /* If currently an add_string(), don't free it or mod it */ if (vp->strsize != 0) { avpair_string_free (vp->strvalue); } for (blen = VPSTR_MIN; blen <= len; blen += blen) { continue; } vp->strsize = blen; 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; } memcpy (vp->strvalue, string, len); vp->strvalue[len] = '\0'; vp->lvalue = len;} /* end of avpair_string_mod () */#define MAX_AVPAIR_VTOA 20/************************************************************************* * * Function: avpair_vtoa * * Purpose: Produce a string representation of the value of a pair. * *************************************************************************/char *avpair_vtoa (vp, sws)VALUE_PAIR *vp;int sws;{ int num; int pos; char *buff; char *c; char *fmt; char *p; DICT_VALUE *dval;#ifdef BINARY_FILTERS radfilter *filt;#endif /* BINARY_FILTERS */ VENDOR *vend_ptr; struct tm *tm; struct in_addr inad; char tmp[10]; static char abuffers[MAX_AVPAIR_VTOA][AUTH_STRING2_LEN + 1]; static int andx = 0; static char *func = "avpair_vtoa"; dprintf(3, (LOG_DAEMON, LOG_DEBUG, "%s: entered", func)); if (vp == NULL_VP) { if ((sws & A
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?