2 * print.c Routines to print stuff.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 * Copyright 2000,2006 The FreeRADIUS server project
23 #include <freeradius-devel/ident.h>
26 #include <freeradius-devel/libradius.h>
31 * Checks for utf-8, taken from:
33 * http://www.w3.org/International/questions/qa-forms-utf-8
35 * Note that we don't care about the length of the input string,
36 * because '\0' is an invalid UTF-8 character.
38 static int utf8_char(uint8_t *str)
40 if (*str < 0x20) return 0;
42 if (*str <= 0x7e) return 1; /* 1 */
44 if (*str <= 0xc1) return 0;
46 if ((str[0] >= 0xc2) && /* 2 */
53 if ((str[0] == 0xe0) && /* 3 */
61 if ((str[0] >= 0xe1) && /* 4a */
70 if ((str[0] >= 0xee) && /* 4b */
79 if ((str[0] == 0xed) && /* 5 */
87 if ((str[0] == 0xf0) && /* 6 */
97 if ((str[0] >= 0xf1) && /* 6 */
109 if ((str[0] == 0xf4) && /* 7 */
120 * Invalid UTF-8 Character
126 * Convert a string to something printable. The output string
127 * has to be larger than the input string by at least 5 bytes.
128 * If not, the output is silently truncated...
130 void librad_safeprint(char *in, size_t inlen, char *out, size_t outlen)
132 uint8_t *str = (uint8_t *)in;
136 if (inlen == 0) inlen = strlen(in);
141 while ((inlen > 0) && (outlen > 4)) {
143 * Hack: never print trailing zero.
144 * Some clients send strings with an off-by-one
145 * length (confused with strings in C).
147 if ((inlen == 0) && (*str == 0)) break;
179 utf8 = utf8_char((uint8_t *)str);
181 snprintf(out, outlen, "\\%03o", *str);
193 } while (--utf8 > 0);
200 * Print one value into a string.
201 * delimitst will define if strings and dates will be delimited by '"'
203 int vp_prints_value(char * out, size_t outlen, VALUE_PAIR *vp, int delimitst)
207 const char *a = NULL;
217 if ((delimitst == 1) && vp->flags.has_tag) {
218 /* Tagged attribute: print delimter and ignore tag */
220 librad_safeprint(vp->vp_strvalue,
221 vp->length, buf + 1, sizeof(buf) - 2);
223 } else if (delimitst == 1) {
224 /* Non-tagged attribute: print delimter */
226 librad_safeprint(vp->vp_strvalue,
227 vp->length, buf + 1, sizeof(buf) - 2);
230 } else if (delimitst < 0) { /* xlat.c */
231 strlcpy(out, vp->vp_strvalue, outlen);
235 /* Non-tagged attribute: no delimiter */
236 librad_safeprint(vp->vp_strvalue,
237 vp->length, buf, sizeof(buf));
241 case PW_TYPE_INTEGER:
242 if ( vp->flags.has_tag ) {
243 /* Attribute value has a tag, need to ignore it */
244 if ((v = dict_valbyattr(vp->attribute, (vp->vp_integer & 0xffffff)))
248 snprintf(buf, sizeof(buf), "%u", (vp->vp_integer & 0xffffff));
254 /* Normal, non-tagged attribute */
255 if ((v = dict_valbyattr(vp->attribute, vp->vp_integer))
259 snprintf(buf, sizeof(buf), "%u", vp->vp_integer);
267 len = strftime(buf, sizeof(buf), "\"%b %e %Y %H:%M:%S %Z\"",
268 localtime_r(&t, &s_tm));
270 len = strftime(buf, sizeof(buf), "%b %e %Y %H:%M:%S %Z",
271 localtime_r(&t, &s_tm));
273 if (len > 0) a = buf;
276 a = inet_ntop(AF_INET, &(vp->vp_ipaddr),
279 case PW_TYPE_ABINARY:
282 print_abinary(vp, buf, sizeof(buf));
288 if (outlen <= (2 * (vp->length + 1))) return 0;
292 fr_bin2hex(vp->vp_octets, buf + 2, vp->length);
297 a = ifid_ntoa(buf, sizeof(buf), vp->vp_octets);
300 case PW_TYPE_IPV6ADDR:
301 a = inet_ntop(AF_INET6,
302 (const struct in6_addr *) vp->vp_strvalue,
306 case PW_TYPE_IPV6PREFIX:
308 struct in6_addr addr;
313 memcpy(&addr, vp->vp_strvalue + 2, sizeof(addr));
315 a = inet_ntop(AF_INET6, &addr, buf, sizeof(buf));
317 char *p = buf + strlen(buf);
318 snprintf(p, buf + sizeof(buf) - p - 1, "/%u",
319 (unsigned int) vp->vp_octets[1]);
324 case PW_TYPE_ETHERNET:
325 snprintf(buf, sizeof(buf), "%02x:%02x:%02x:%02x:%02x:%02x",
326 vp->vp_ether[0], vp->vp_ether[1],
327 vp->vp_ether[2], vp->vp_ether[3],
328 vp->vp_ether[4], vp->vp_ether[5]);
337 if (a != NULL) strlcpy(out, a, outlen);
343 * This is a hack, and has to be kept in sync with tokens.h
345 static const char *vp_tokens[] = {
346 "?", /* T_OP_INVALID */
377 * Print one attribute and value into a string.
379 int vp_prints(char *out, size_t outlen, VALUE_PAIR *vp)
382 const char *token = NULL;
390 DICT_ATTR *da = dict_attrbyvalue(vp->attribute);
396 if (strlen(name) + 3 > (size_t)outlen) {
400 if ((vp->operator > T_OP_INVALID) &&
401 (vp->operator < T_TOKEN_LAST)) {
402 token = vp_tokens[vp->operator];
404 token = "<INVALID-TOKEN>";
407 if( vp->flags.has_tag ) {
409 snprintf(out, outlen, "%s:%d %s ", name, vp->flags.tag,
413 vp_prints_value(out + len, outlen - len, vp, 1);
417 snprintf(out, outlen, "%s %s ", name, token);
419 vp_prints_value(out + len, outlen - len, vp, 1);
428 * Print one attribute and value.
430 void vp_print(FILE *fp, VALUE_PAIR *vp)
434 vp_prints(buf, sizeof(buf), vp);
440 * Print a whole list of attributes, indented by a TAB
441 * and with a newline at the end.
443 void vp_printlist(FILE *fp, VALUE_PAIR *vp)
445 for (; vp; vp = vp->next) {