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, int inlen, char *out, int outlen)
132 unsigned char *str = (unsigned char *)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, int 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((char *)(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((char *)vp->vp_strvalue,
227 vp->length, buf + 1, sizeof(buf) - 2);
231 /* Non-tagged attribute: no delimiter */
232 librad_safeprint((char *)vp->vp_strvalue,
233 vp->length, buf, sizeof(buf));
237 case PW_TYPE_INTEGER:
238 if ( vp->flags.has_tag ) {
239 /* Attribute value has a tag, need to ignore it */
240 if ((v = dict_valbyattr(vp->attribute, (vp->vp_integer & 0xffffff)))
244 snprintf(buf, sizeof(buf), "%u", (vp->vp_integer & 0xffffff));
250 /* Normal, non-tagged attribute */
251 if ((v = dict_valbyattr(vp->attribute, vp->vp_integer))
255 snprintf(buf, sizeof(buf), "%u", vp->vp_integer);
263 len = strftime(buf, sizeof(buf), "\"%b %e %Y %H:%M:%S %Z\"",
264 localtime_r(&t, &s_tm));
266 len = strftime(buf, sizeof(buf), "%b %e %Y %H:%M:%S %Z",
267 localtime_r(&t, &s_tm));
269 if (len > 0) a = buf;
272 a = inet_ntop(AF_INET, &(vp->vp_ipaddr),
275 case PW_TYPE_ABINARY:
278 print_abinary(vp, buf, sizeof(buf));
284 if (outlen <= (2 * (vp->length + 1))) return 0;
288 lrad_bin2hex(vp->vp_octets, buf + 2, vp->length);
293 a = ifid_ntoa(buf, sizeof(buf), vp->vp_octets);
296 case PW_TYPE_IPV6ADDR:
297 a = inet_ntop(AF_INET6,
298 (const struct in6_addr *) vp->vp_strvalue,
302 case PW_TYPE_IPV6PREFIX:
304 struct in6_addr addr;
309 memcpy(&addr, vp->vp_strvalue + 2, sizeof(addr));
311 a = inet_ntop(AF_INET6, &addr, buf, sizeof(buf));
313 char *p = buf + strlen(buf);
314 sprintf(p, "/%u", (unsigned int) vp->vp_strvalue[1]);
319 case PW_TYPE_ETHERNET:
320 snprintf(buf, sizeof(buf), "%02x:%02x:%02x:%02x:%02x:%02x",
321 vp->vp_ether[0], vp->vp_ether[1],
322 vp->vp_ether[2], vp->vp_ether[3],
323 vp->vp_ether[4], vp->vp_ether[5]);
332 if (a != NULL) strlcpy(out, a, outlen);
338 * This is a hack, and has to be kept in sync with tokens.h
340 static const char *vp_tokens[] = {
341 "?", /* T_OP_INVALID */
372 * Print one attribute and value into a string.
374 int vp_prints(char *out, int outlen, VALUE_PAIR *vp)
377 const char *token = NULL;
385 DICT_ATTR *da = dict_attrbyvalue(vp->attribute);
391 if (strlen(name) + 3 > (size_t)outlen) {
395 if ((vp->operator > T_OP_INVALID) &&
396 (vp->operator < T_TOKEN_LAST)) {
397 token = vp_tokens[vp->operator];
399 token = "<INVALID-TOKEN>";
402 if( vp->flags.has_tag ) {
404 snprintf(out, outlen, "%s:%d %s ", name, vp->flags.tag,
408 vp_prints_value(out + len, outlen - len, vp, 1);
412 snprintf(out, outlen, "%s %s ", name, token);
414 vp_prints_value(out + len, outlen - len, vp, 1);
423 * Print one attribute and value.
425 void vp_print(FILE *fp, VALUE_PAIR *vp)
429 vp_prints(buf, sizeof(buf), vp);
435 * Print a whole list of attributes, indented by a TAB
436 * and with a newline at the end.
438 void vp_printlist(FILE *fp, VALUE_PAIR *vp)
440 for (; vp; vp = vp->next) {