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 * Convert a string to something printable.
32 * The output string has to be _at least_ 4x the size
33 * of the input string!
35 void librad_safeprint(char *in, int inlen, char *out, int outlen)
37 unsigned char *str = (unsigned char *)in;
41 if (inlen < 0) inlen = strlen(in);
43 while (inlen-- > 0 && (done + 3) < outlen) {
45 * Hack: never print trailing zero.
46 * Some clients send strings with an off-by-one
47 * length (confused with strings in C).
49 if (inlen == 0 && *str == 0)
71 if (*str < 32 || (*str >= 128)){
72 snprintf(out, outlen, "\\%03o", *str);
95 * Print one value into a string.
96 * delimitst will define if strings and dates will be delimited by '"'
98 int vp_prints_value(char * out, int outlen, VALUE_PAIR *vp, int delimitst)
102 const char *a = NULL;
112 if ((delimitst == 1) && vp->flags.has_tag) {
113 /* Tagged attribute: print delimter and ignore tag */
115 librad_safeprint((char *)(vp->vp_strvalue),
116 vp->length, buf + 1, sizeof(buf) - 2);
118 } else if (delimitst == 1) {
119 /* Non-tagged attribute: print delimter */
121 librad_safeprint((char *)vp->vp_strvalue,
122 vp->length, buf + 1, sizeof(buf) - 2);
125 } else if (delimitst < 0) {
126 strlcpy(out, vp->vp_strvalue, outlen);
130 /* Non-tagged attribute: no delimiter */
131 librad_safeprint((char *)vp->vp_strvalue,
132 vp->length, buf, sizeof(buf));
136 case PW_TYPE_INTEGER:
137 if ( vp->flags.has_tag ) {
138 /* Attribute value has a tag, need to ignore it */
139 if ((v = dict_valbyattr(vp->attribute, (vp->vp_integer & 0xffffff)))
143 snprintf(buf, sizeof(buf), "%u", (vp->vp_integer & 0xffffff));
149 /* Normal, non-tagged attribute */
150 if ((v = dict_valbyattr(vp->attribute, vp->vp_integer))
154 snprintf(buf, sizeof(buf), "%u", vp->vp_integer);
162 len = strftime(buf, sizeof(buf), "\"%b %e %Y %H:%M:%S %Z\"",
163 localtime_r(&t, &s_tm));
165 len = strftime(buf, sizeof(buf), "%b %e %Y %H:%M:%S %Z",
166 localtime_r(&t, &s_tm));
168 if (len > 0) a = buf;
171 a = inet_ntop(AF_INET, &(vp->vp_ipaddr),
174 case PW_TYPE_ABINARY:
177 print_abinary(vp, buf, sizeof(buf));
183 if (outlen <= (2 * (vp->length + 1))) return 0;
187 lrad_bin2hex(vp->vp_octets, buf + 2, vp->length);
192 a = ifid_ntoa(buf, sizeof(buf), vp->vp_octets);
195 case PW_TYPE_IPV6ADDR:
196 a = inet_ntop(AF_INET6,
197 (const struct in6_addr *) vp->vp_strvalue,
201 case PW_TYPE_IPV6PREFIX:
203 struct in6_addr addr;
208 memcpy(&addr, vp->vp_strvalue + 2, sizeof(addr));
210 a = inet_ntop(AF_INET6, &addr, buf, sizeof(buf));
212 char *p = buf + strlen(buf);
213 sprintf(p, "/%u", (unsigned int) vp->vp_strvalue[1]);
223 if (a != NULL) strlcpy(out, a, outlen);
229 * This is a hack, and has to be kept in sync with tokens.h
231 static const char *vp_tokens[] = {
232 "?", /* T_OP_INVALID */
263 * Print one attribute and value into a string.
265 int vp_prints(char *out, int outlen, VALUE_PAIR *vp)
268 const char *token = NULL;
276 DICT_ATTR *da = dict_attrbyvalue(vp->attribute);
282 if (strlen(name) + 3 > (size_t)outlen) {
286 if ((vp->operator > T_OP_INVALID) &&
287 (vp->operator < T_TOKEN_LAST)) {
288 token = vp_tokens[vp->operator];
290 token = "<INVALID-TOKEN>";
293 if( vp->flags.has_tag ) {
295 snprintf(out, outlen, "%s:%d %s ", name, vp->flags.tag,
299 vp_prints_value(out + len, outlen - len, vp, 1);
303 snprintf(out, outlen, "%s %s ", name, token);
305 vp_prints_value(out + len, outlen - len, vp, 1);
314 * Print one attribute and value.
316 void vp_print(FILE *fp, VALUE_PAIR *vp)
320 vp_prints(buf, sizeof(buf), vp);
326 * Print a whole list of attributes, indented by a TAB
327 * and with a newline at the end.
329 void vp_printlist(FILE *fp, VALUE_PAIR *vp)
331 for (; vp; vp = vp->next) {