2 Copyright (c) 2011, Network RADIUS SARL
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7 * Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above copyright
10 notice, this list of conditions and the following disclaimer in the
11 documentation and/or other materials provided with the distribution.
12 * Neither the name of the <organization> nor the
13 names of its contributors may be used to endorse or promote products
14 derived from this software without specific prior written permission.
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
20 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * \brief Functions to print things.
34 #ifdef RS_TYPE_IPV6ADDR
35 #include <arpa/inet.h>
39 void nr_packet_print_hex(RADIUS_PACKET *packet)
43 if (!packet->data) return;
45 printf(" Code:\t\t%u\n", packet->data[0]);
46 printf(" Id:\t\t%u\n", packet->data[1]);
47 printf(" Length:\t%u\n", ((packet->data[2] << 8) |
50 for (i = 4; i < 20; i++) {
51 printf("%02x", packet->data[i]);
54 if ((packet->flags & RS_PACKET_SIGNED) == 0) printf("\t\tWARNING: nr_packet_sign() was not called!\n");
56 if (packet->length > 20) {
61 total = packet->length - 20;
62 ptr = packet->data + 20;
68 if (total < 2) { /* too short */
69 printf("%02x\n", *ptr);
73 if (ptr[1] > total) { /* too long */
74 for (i = 0; i < total; i++) {
75 printf("%02x ", ptr[i]);
80 printf("%02x %02x ", ptr[0], ptr[1]);
85 for (i = 0; i < attrlen; i++) {
86 if ((i > 0) && ((i & 0x0f) == 0x00))
88 printf("%02x ", ptr[i]);
89 if ((i & 0x0f) == 0x0f) printf("\n");
92 if (!attrlen || ((attrlen & 0x0f) != 0x00)) printf("\n");
103 size_t nr_vp_snprintf_value(char *buffer, size_t buflen, const VALUE_PAIR *vp)
108 switch (vp->da->type) {
111 * FIXME: escape backslash && quotes!
113 len = snprintf(p, buflen, "%s", vp->vp_strvalue);
117 case RS_TYPE_INTEGER:
120 len = snprintf(p, buflen, "%u", vp->vp_integer);
124 len = snprintf(p, buflen, "%u.%u.%u.%u",
125 (vp->vp_ipaddr >> 24) & 0xff,
126 (vp->vp_ipaddr >> 16) & 0xff,
127 (vp->vp_ipaddr >> 8) & 0xff,
128 vp->vp_ipaddr & 0xff);
131 #ifdef RS_TYPE_IPV6ADDR
132 case RS_TYPE_IPV6ADDR:
133 if (!inet_ntop(AF_INET6, &vp->vp_ipv6addr, buffer, buflen)) {
141 len = snprintf(p, buflen, "%02x%02x%02x%02x%02x%02x%02x%02x",
142 vp->vp_ifid[0], vp->vp_ifid[1],
143 vp->vp_ifid[2], vp->vp_ifid[3],
144 vp->vp_ifid[4], vp->vp_ifid[5],
145 vp->vp_ifid[6], vp->vp_ifid[7]);
150 len = snprintf(p, buflen, "0x");
151 if (len >= buflen) return 0;
156 for (i = 0; i < vp->length; i++) {
157 len = snprintf(p, buflen, "%02x", vp->vp_octets[i]);
158 if (len >= buflen) return 0;
171 if (len >= buflen) return 0;
179 size_t nr_vp_snprintf(char *buffer, size_t buflen, const VALUE_PAIR *vp)
184 len = snprintf(p, buflen, "%s = ", vp->da->name);
185 if (len >= buflen) return 0;
190 len = nr_vp_snprintf_value(p, buflen, vp);
191 if (len == 0) return 0;
193 if (len >= buflen) return 0;
201 void nr_vp_fprintf_list(FILE *fp, const VALUE_PAIR *vps)
203 const VALUE_PAIR *vp;
206 for (vp = vps; vp != NULL; vp = vp->next) {
207 nr_vp_snprintf(buffer, sizeof(buffer), vp);
208 fprintf(fp, "\t%s\n", buffer);
214 #define NR_STRERROR_BUFSIZE (1024)
215 static char nr_strerror_buffer[NR_STRERROR_BUFSIZE];
217 void nr_strerror_printf(const char *fmt, ...)
221 vsnprintf(nr_strerror_buffer, sizeof(nr_strerror_buffer), fmt, ap);
224 fprintf(stderr, "ERROR: %s\n", nr_strerror_buffer);