|
1 | 1 | /* |
| 2 | + * Copyright (C) 2015 Nikos Mavrogiannopoulos |
2 | 3 | * Copyright (C) 1995 Lars Fenneberg |
3 | 4 | * |
4 | 5 | * Copyright 1992 Livingston Enterprises, Inc. |
@@ -50,6 +51,20 @@ VALUE_PAIR *rc_avpair_add (rc_handle const *rh, VALUE_PAIR **list, int attrid, v |
50 | 51 |
|
51 | 52 | } |
52 | 53 |
|
| 54 | +/** Iterates through the attribute-value pairs |
| 55 | + * |
| 56 | + * The attribute-value are organized in a linked-list, and this |
| 57 | + * function provides a way to iterate them given the first element |
| 58 | + * initially. |
| 59 | + * |
| 60 | + * @param t the current pair. |
| 61 | + * @return pointer to the next pair, or NULL when finished. |
| 62 | + */ |
| 63 | +VALUE_PAIR *rc_avpair_next (VALUE_PAIR *t) |
| 64 | +{ |
| 65 | + return t->next; |
| 66 | +} |
| 67 | + |
53 | 68 | /** Assigns the given value to an attribute-value pair |
54 | 69 | * |
55 | 70 | * @param vp a pointer to a VALUE_PAIR structure. |
@@ -854,34 +869,94 @@ char *rc_avpair_log(rc_handle const *rh, VALUE_PAIR *pair, char *buf, size_t buf |
854 | 869 | return buf; |
855 | 870 | } |
856 | 871 |
|
857 | | -/** Get a sequence of attribute value pairs from the file input and make them into a list of value_pairs |
| 872 | +/** Get the integer value of the given attribute value-pair |
858 | 873 | * |
859 | | - * @param rh a handle to parsed configuration. |
860 | | - * @param input a %FILE handle. |
861 | | - * @return the array of value pairs. |
| 874 | + * This function is valid for PW_TYPE_INTEGER, PW_TYPE_IPADDR. |
| 875 | + * PW_TYPE_DATE. In PW_TYPE_IPADDR this value will contain the |
| 876 | + * IPv4 address in host by order. |
| 877 | + * |
| 878 | + * @param vp a pointer to a VALUE_PAIR structure. |
| 879 | + * @param res The integer value returned. |
| 880 | + * @return zero on success or -1 on failure. |
862 | 881 | */ |
863 | | -VALUE_PAIR *rc_avpair_readin(rc_handle const *rh, FILE *input) |
| 882 | +int rc_avpair_get_uint32 (VALUE_PAIR *vp, uint32_t *res) |
864 | 883 | { |
865 | | - VALUE_PAIR *vp = NULL; |
866 | | - char buffer[1024], *q; |
| 884 | + if (vp->type == PW_TYPE_DATE || vp->type == PW_TYPE_IPADDR || |
| 885 | + vp->type == PW_TYPE_INTEGER) { |
| 886 | + if (res) |
| 887 | + *res = vp->lvalue; |
| 888 | + return 0; |
| 889 | + } else { |
| 890 | + return -1; |
| 891 | + } |
| 892 | +} |
867 | 893 |
|
868 | | - while (fgets(buffer, sizeof(buffer), input) != NULL) |
869 | | - { |
870 | | - q = buffer; |
| 894 | +/** Get the IPv6 address and prefix value of the given attribute value-pair |
| 895 | + * |
| 896 | + * This function is valid for PW_TYPE_IPV6ADDR, PW_TYPE_IPV6PREFIX. |
| 897 | + * |
| 898 | + * @param vp a pointer to a VALUE_PAIR structure. |
| 899 | + * @param res An in6_addr structure for result to be copied in. |
| 900 | + * @param prefix If of type PW_TYPE_IPV6PREFIX the prefix will be copied (may be NULL). |
| 901 | + * @return zero on success or -1 on failure. |
| 902 | + */ |
| 903 | +int rc_avpair_get_in6 (VALUE_PAIR *vp, struct in6_addr *res, unsigned *prefix) |
| 904 | +{ |
| 905 | + if (vp->type == PW_TYPE_IPV6ADDR) { |
| 906 | + memcpy(res, vp->strvalue, 16); |
| 907 | + return 0; |
| 908 | + } else if (vp->type == PW_TYPE_IPV6PREFIX) { |
| 909 | + if (vp->lvalue < 2 || vp->lvalue > 18) |
| 910 | + return -1; |
871 | 911 |
|
872 | | - while(*q && isspace(*q)) q++; |
| 912 | + if (res) { |
| 913 | + memset(res, 0, 16); |
| 914 | + memcpy(res, vp->strvalue+2, vp->lvalue-2); |
| 915 | + } |
873 | 916 |
|
874 | | - if ((*q == '\n') || (*q == '#') || (*q == '\0')) |
875 | | - continue; |
| 917 | + if (prefix) |
| 918 | + *prefix = (unsigned char)vp->strvalue[1]; |
| 919 | + return 0; |
| 920 | + } |
876 | 921 |
|
877 | | - if (rc_avpair_parse(rh, q, &vp) < 0) { |
878 | | - rc_log(LOG_ERR, "rc_avpair_readin: malformed attribute: %s", buffer); |
879 | | - rc_avpair_free(vp); |
880 | | - return NULL; |
881 | | - } |
| 922 | + return -1; |
| 923 | +} |
| 924 | + |
| 925 | +/** Get the raw value of the given attribute value-pair |
| 926 | + * |
| 927 | + * This function is valid for PW_TYPE_STRING, PW_TYPE_IPV6ADDR, |
| 928 | + * PW_TYPE_IPV6PREFIX. |
| 929 | + * |
| 930 | + * @param vp a pointer to a VALUE_PAIR structure. |
| 931 | + * @param res The integer value returned. |
| 932 | + * @return zero on success or -1 on failure. |
| 933 | + */ |
| 934 | +int rc_avpair_get_raw (VALUE_PAIR *vp, char **res, unsigned *res_size) |
| 935 | +{ |
| 936 | + if (vp->type == PW_TYPE_STRING || vp->type == PW_TYPE_IPV6ADDR || |
| 937 | + vp->type == PW_TYPE_IPV6PREFIX) { |
| 938 | + if (res) |
| 939 | + *res = vp->strvalue; |
| 940 | + if (res_size) |
| 941 | + *res_size = vp->lvalue; |
| 942 | + return 0; |
| 943 | + } else { |
| 944 | + return -1; |
882 | 945 | } |
| 946 | +} |
883 | 947 |
|
884 | | - return vp; |
| 948 | +/** Get the attribute ID and type of the given attribute value-pair |
| 949 | + * |
| 950 | + * @param vp a pointer to a VALUE_PAIR structure. |
| 951 | + * @param type The attribute type, of type rc_attr_type |
| 952 | + * @param id The attribute identifier, of type rc_attr_id |
| 953 | + */ |
| 954 | +void rc_avpair_get_attr (VALUE_PAIR *vp, unsigned *type, unsigned *id) |
| 955 | +{ |
| 956 | + if (type) |
| 957 | + *type = vp->type; |
| 958 | + if (id) |
| 959 | + *id = vp->attribute; |
885 | 960 | } |
886 | 961 |
|
887 | 962 | /** @} */ |
0 commit comments