Skip to content

Commit

Permalink
Convert pairparsevalue to value_data_from_str which does the same thi…
Browse files Browse the repository at this point in the history
…ng but writes the result to a value_data_t

This makes it more useful for evaluating conditions
  • Loading branch information
arr2036 committed Oct 30, 2014
1 parent e5a530f commit 3942e4d
Show file tree
Hide file tree
Showing 14 changed files with 624 additions and 642 deletions.
47 changes: 36 additions & 11 deletions src/include/libradius.h
Expand Up @@ -341,6 +341,23 @@ typedef struct value_pair_raw {
FR_TOKEN op; //!< Operator.
} VALUE_PAIR_RAW;

#define vd_strvalue strvalue
#define vd_integer integer
#define vd_ipaddr ipaddr.s_addr
#define vd_date date
#define vd_filter filter
#define vd_octets octets
#define vd_ifid ifid
#define vd_ipv6addr data.ipv6addr
#define vd_ipv6prefix data.ipv6prefix
#define vd_byte data.byte
#define vd_short data.ushort
#define vd_ether data.ether
#define vd_signed data.sinteger
#define vd_integer64 data.integer64
#define vd_ipv4prefix data.ipv4prefix
#define vd_tlv data.tlv

#define vp_strvalue data.strvalue
#define vp_integer data.integer
#define vp_ipaddr data.ipaddr.s_addr
Expand Down Expand Up @@ -571,7 +588,7 @@ int rad_vp2attr(RADIUS_PACKET const *packet,
RADIUS_PACKET const *original, char const *secret,
VALUE_PAIR const **pvp, uint8_t *ptr, size_t room);

/* valuepair.c */
/* pair.c */
VALUE_PAIR *pairalloc(TALLOC_CTX *ctx, DICT_ATTR const *da);
VALUE_PAIR *paircreate(TALLOC_CTX *ctx, unsigned int attr, unsigned int vendor);
int pair2unknown(VALUE_PAIR *vp);
Expand Down Expand Up @@ -599,12 +616,6 @@ VALUE_PAIR *fr_cursor_replace(vp_cursor_t *cursor, VALUE_PAIR *new);
void pairdelete(VALUE_PAIR **, unsigned int attr, unsigned int vendor, int8_t tag);
void pairadd(VALUE_PAIR **, VALUE_PAIR *);
void pairreplace(VALUE_PAIR **first, VALUE_PAIR *add);
int value_data_cmp(PW_TYPE type_one, size_t length_one, value_data_t const *value_one,
PW_TYPE type_two, size_t length_two, value_data_t const *value_two);
#define paircmp_op(_op, _a, _b) value_data_cmp_op(_op, _a->da->type, _a->length, &_a->data, _b->da->type, _b->length, &_b->data)
int value_data_cmp_op(FR_TOKEN op,
PW_TYPE type_a, size_t length_a, value_data_t const *a,
PW_TYPE type_b, size_t length_b, value_data_t const *b);
int paircmp(VALUE_PAIR *a, VALUE_PAIR *b);
int pairlistcmp(VALUE_PAIR *a, VALUE_PAIR *b);

Expand Down Expand Up @@ -638,6 +649,20 @@ FR_TOKEN pairread(char const **ptr, VALUE_PAIR_RAW *raw);
FR_TOKEN userparse(TALLOC_CTX *ctx, char const *buffer, VALUE_PAIR **head);
int readvp2(TALLOC_CTX *ctx, VALUE_PAIR **out, FILE *fp, bool *pfiledone);

#define paircmp_op(_op, _a, _b) value_data_cmp_op(_op, _a->da->type, _a->length, &_a->data, _b->da->type, _b->length, &_b->data)

/* value.c */
int value_data_cmp(PW_TYPE a_type, size_t a_length, value_data_t const *a,
PW_TYPE b_type, size_t b_length, value_data_t const *b);

int value_data_cmp_op(FR_TOKEN op,
PW_TYPE a_type, size_t a_length, value_data_t const *a,
PW_TYPE b_type, size_t b_length, value_data_t const *b);

ssize_t value_data_from_str(TALLOC_CTX *ctx, value_data_t *out,
PW_TYPE type, DICT_ATTR const *enumv,
char const *value, ssize_t inlen);

/*
* Error functions.
*/
Expand Down Expand Up @@ -665,9 +690,9 @@ int fr_set_signal(int sig, sig_t func);
int fr_link_talloc_ctx_free(TALLOC_CTX *parent, TALLOC_CTX *child);
char const *fr_inet_ntop(int af, void const *src);
char const *ip_ntoa(char *, uint32_t);
int fr_pton4(fr_ipaddr_t *out, char const *value, size_t inlen, bool resolve, bool fallback);
int fr_pton6(fr_ipaddr_t *out, char const *value, size_t inlen, bool resolve, bool fallback);
int fr_pton(fr_ipaddr_t *out, char const *value, size_t inlen, bool resolve);
int fr_pton4(fr_ipaddr_t *out, char const *value, ssize_t inlen, bool resolve, bool fallback);
int fr_pton6(fr_ipaddr_t *out, char const *value, ssize_t inlen, bool resolve, bool fallback);
int fr_pton(fr_ipaddr_t *out, char const *value, ssize_t inlen, bool resolve);
int fr_ntop(char *out, size_t outlen, fr_ipaddr_t *addr);
char *ifid_ntoa(char *buffer, size_t size, uint8_t const *ifid);
uint8_t *ifid_aton(char const *ifid_str, uint8_t *ifid);
Expand Down Expand Up @@ -714,7 +739,7 @@ void fr_talloc_verify_cb(const void *ptr, int depth,

#ifdef WITH_ASCEND_BINARY
/* filters.c */
int ascend_parse_filter(VALUE_PAIR *vp, char const *value, size_t len);
int ascend_parse_filter(value_data_t *out, char const *value, size_t len);
void print_abinary(char *out, size_t outlen, uint8_t const *data, size_t len, int8_t quote);
#endif /*WITH_ASCEND_BINARY*/

Expand Down
72 changes: 18 additions & 54 deletions src/lib/filters.c
Expand Up @@ -941,19 +941,17 @@ static int ascend_parse_generic(int argc, char **argv,
}


/*
* filterBinary:
/** Filter binary
*
* This routine will call routines to parse entries from an ASCII format
* to a binary format recognized by the Ascend boxes.
*
* pair: Pointer to value_pair to place return.
*
* valstr: The string to parse
*
* return: -1 for error or 0.
* @param out Where to write parsed filter.
* @param value ascend filter text.
* @param len of value.
* @return -1 for error or 0.
*/
int ascend_parse_filter(VALUE_PAIR *vp, char const *value, size_t len)
int ascend_parse_filter(value_data_t *out, char const *value, size_t len)
{
int token, type;
int rcode;
Expand All @@ -964,21 +962,22 @@ int ascend_parse_filter(VALUE_PAIR *vp, char const *value, size_t len)

rcode = -1;

/*
* Rather than printing specific error messages, we create
* a general one here, which won't be used if the function
* returns OK.
*/
fr_strerror_printf("Text is not in proper format");

/*
* Tokenize the input string in the VP.
*
* Once the filter is *completelty* parsed, then we will
* over-write it with the final binary filter.
*/
p = talloc_memdup(vp, value, len);
p = talloc_memdup(NULL, value, len);
p[len] = '\0';

/*
* Rather than printing specific error messages, we create
* a general one here, which won't be used if the function
* returns OK.
*/
fr_strerror_printf("Failed parsing \"%s\" as ascend filer", p);

argc = str2argv(p, argv, 32);
if (argc < 3) {
talloc_free(p);
Expand Down Expand Up @@ -1065,46 +1064,11 @@ int ascend_parse_filter(VALUE_PAIR *vp, char const *value, size_t len)
/*
* Touch the VP only if everything was OK.
*/
if (rcode == 0) {
vp->length = sizeof(filter);
memcpy(vp->vp_filter, &filter, sizeof(filter));
}

if (rcode == 0) memcpy(out->filter, &filter, sizeof(filter));
talloc_free(p);
return rcode;
printf("%i", rcode);

#if 0
/*
* if 'more' is set then this new entry must exist, be a
* FILTER_GENERIC_TYPE, direction and disposition must match for
* the previous 'more' to be valid. If any should fail then TURN OFF
* previous 'more'
*/
if( prevRadvp ) {
filt = ( RadFilter * )prevRadvp->vp_strvalue;
if(( tok != FILTER_GENERIC_TYPE ) || (rc == -1 ) ||
( prevRadvp->attribute != vp->attribute ) ||
( filt->indirection != radFil.indirection ) ||
( filt->forward != radFil.forward ) ) {
gen = &filt->u.generic;
gen->more = false;
fr_strerror_printf("filterBinary: 'more' for previous entry doesn't match: %s.\n",
valstr);
}
}
prevRadvp = NULL;
if( rc != -1 && tok == FILTER_GENERIC_TYPE ) {
if( radFil.u.generic.more ) {
prevRadvp = vp;
}
}

if( rc != -1 ) {
vpmemcpy(vp, &radFil, vp->length );
}
return(rc);

#endif
return rcode;
}

/*
Expand Down
26 changes: 13 additions & 13 deletions src/lib/misc.c
Expand Up @@ -207,12 +207,12 @@ char const *ip_ntoa(char *buffer, uint32_t ipaddr)
*
* @param out Where to write the ip address value.
* @param value to parse, may be dotted quad [+ prefix], or integer, or octal number, or '*' (INADDR_ANY).
* @param inlen Length of value, if value is \0 terminated inlen may be 0.
* @param inlen Length of value, if value is \0 terminated inlen may be -1.
* @param resolve If true and value doesn't look like an IP address, try and resolve value as a hostname.
* @param fallback to IPv4 resolution if no A records can be found.
* @return 0 if ip address was parsed successfully, else -1 on error.
*/
int fr_pton4(fr_ipaddr_t *out, char const *value, size_t inlen, bool resolve, bool fallback)
int fr_pton4(fr_ipaddr_t *out, char const *value, ssize_t inlen, bool resolve, bool fallback)
{
char *p;
unsigned int prefix;
Expand All @@ -224,8 +224,8 @@ int fr_pton4(fr_ipaddr_t *out, char const *value, size_t inlen, bool resolve, bo
/*
* Copy to intermediary buffer if we were given a length
*/
if (inlen > 0) {
if (inlen >= sizeof(buffer)) {
if (inlen >= 0) {
if (inlen >= (ssize_t)sizeof(buffer)) {
fr_strerror_printf("Invalid IPv4 address string \"%s\"", value);
return -1;
}
Expand Down Expand Up @@ -275,7 +275,7 @@ int fr_pton4(fr_ipaddr_t *out, char const *value, size_t inlen, bool resolve, bo
/*
* Copy the IP portion into a temporary buffer if we haven't already.
*/
if (inlen == 0) memcpy(buffer, value, p - value);
if (inlen < 0) memcpy(buffer, value, p - value);
buffer[p - value] = '\0';

if (!resolve) {
Expand Down Expand Up @@ -310,12 +310,12 @@ int fr_pton4(fr_ipaddr_t *out, char const *value, size_t inlen, bool resolve, bo
*
* @param out Where to write the ip address value.
* @param value to parse.
* @param inlen Length of value, if value is \0 terminated inlen may be 0.
* @param inlen Length of value, if value is \0 terminated inlen may be -1.
* @param resolve If true and value doesn't look like an IP address, try and resolve value as a hostname.
* @param fallback to IPv4 resolution if no AAAA records can be found.
* @return 0 if ip address was parsed successfully, else -1 on error.
*/
int fr_pton6(fr_ipaddr_t *out, char const *value, size_t inlen, bool resolve, bool fallback)
int fr_pton6(fr_ipaddr_t *out, char const *value, ssize_t inlen, bool resolve, bool fallback)
{
char const *p;
unsigned int prefix;
Expand All @@ -327,8 +327,8 @@ int fr_pton6(fr_ipaddr_t *out, char const *value, size_t inlen, bool resolve, bo
/*
* Copy to intermediary buffer if we were given a length
*/
if (inlen > 0) {
if (inlen >= sizeof(buffer)) {
if (inlen >= 0) {
if (inlen >= (ssize_t)sizeof(buffer)) {
fr_strerror_printf("Invalid IPv6 address string \"%s\"", value);
return -1;
}
Expand Down Expand Up @@ -364,7 +364,7 @@ int fr_pton6(fr_ipaddr_t *out, char const *value, size_t inlen, bool resolve, bo
/*
* Copy string to temporary buffer if we didn't do it earlier
*/
if (inlen == 0) memcpy(buffer, value, p - value);
if (inlen < 0) memcpy(buffer, value, p - value);
buffer[p - value] = '\0';

if (!resolve) {
Expand Down Expand Up @@ -402,15 +402,15 @@ int fr_pton6(fr_ipaddr_t *out, char const *value, size_t inlen, bool resolve, bo
*
* @param out Where to write the ip address value.
* @param value to parse.
* @param inlen Length of value, if value is \0 terminated inlen may be 0.
* @param inlen Length of value, if value is \0 terminated inlen may be -1.
* @param resolve If true and value doesn't look like an IP address, try and resolve value as a hostname.
* @return 0 if ip address was parsed successfully, else -1 on error.
*/
int fr_pton(fr_ipaddr_t *out, char const *value, size_t inlen, bool resolve)
int fr_pton(fr_ipaddr_t *out, char const *value, ssize_t inlen, bool resolve)
{
size_t len, i;

len = (inlen == 0) ? strlen(value) : inlen;
len = (inlen < 0) ? strlen(value) : inlen;
for (i = 0; i < len; i++) switch (value[i]) {
/*
* Chars illegal in domain names and IPv4 addresses.
Expand Down

0 comments on commit 3942e4d

Please sign in to comment.