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 The FreeRADIUS server project
23 static const char rcsid[] = "$Id$";
25 #include <freeradius-devel/autoconf.h>
29 #include <sys/types.h>
33 #include <freeradius-devel/missing.h>
34 #include <freeradius-devel/libradius.h>
37 * Convert a string to something printable.
38 * The output string has to be _at least_ 4x the size
39 * of the input string!
41 void librad_safeprint(char *in, int inlen, char *out, int outlen)
43 unsigned char *str = (unsigned char *)in;
47 if (inlen < 0) inlen = strlen(in);
49 while (inlen-- > 0 && (done + 3) < outlen) {
51 * Hack: never print trailing zero.
52 * Some clients send strings with an off-by-one
53 * length (confused with strings in C).
55 if (inlen == 0 && *str == 0)
77 if (*str < 32 || (*str >= 128)){
78 snprintf(out, outlen, "\\%03o", *str);
101 * Print one value into a string.
102 * delimitst will define if strings and dates will be delimited by '"'
104 int vp_prints_value(char * out, int outlen, VALUE_PAIR *vp, int delimitst)
108 const char *a = NULL;
118 if ((delimitst == 1) && vp->flags.has_tag) {
119 /* Tagged attribute: print delimter and ignore tag */
121 librad_safeprint((char *)(vp->vp_strvalue),
122 vp->length, buf + 1, sizeof(buf) - 2);
124 } else if (delimitst == 1) {
125 /* Non-tagged attribute: print delimter */
127 librad_safeprint((char *)vp->vp_strvalue,
128 vp->length, buf + 1, sizeof(buf) - 2);
131 } else if (delimitst < 0) {
132 strNcpy(out, vp->vp_strvalue, outlen);
136 /* Non-tagged attribute: no delimiter */
137 librad_safeprint((char *)vp->vp_strvalue,
138 vp->length, buf, sizeof(buf));
142 case PW_TYPE_INTEGER:
143 if ( vp->flags.has_tag ) {
144 /* Attribute value has a tag, need to ignore it */
145 if ((v = dict_valbyattr(vp->attribute, (vp->lvalue & 0xffffff)))
149 snprintf(buf, sizeof(buf), "%u", (vp->lvalue & 0xffffff));
153 /* Normal, non-tagged attribute */
154 if ((v = dict_valbyattr(vp->attribute, vp->lvalue))
158 snprintf(buf, sizeof(buf), "%u", vp->lvalue);
166 len = strftime(buf, sizeof(buf), "\"%b %e %Y %H:%M:%S %Z\"",
167 localtime_r(&t, &s_tm));
169 len = strftime(buf, sizeof(buf), "%b %e %Y %H:%M:%S %Z",
170 localtime_r(&t, &s_tm));
172 if (len > 0) a = buf;
175 a = inet_ntop(AF_INET, &(vp->lvalue),
178 case PW_TYPE_ABINARY:
181 print_abinary(vp, (unsigned char *)buf, sizeof(buf));
187 if (outlen <= (2 * (vp->length + 1))) return 0;
191 lrad_bin2hex(vp->vp_strvalue, buf + 2, vp->length);
196 a = ifid_ntoa(buf, sizeof(buf), vp->vp_strvalue);
199 case PW_TYPE_IPV6ADDR:
200 a = inet_ntop(AF_INET6,
201 (const struct in6_addr *) vp->vp_strvalue,
205 case PW_TYPE_IPV6PREFIX:
207 struct in6_addr addr;
212 memcpy(&addr, vp->vp_strvalue + 2, sizeof(addr));
214 a = inet_ntop(AF_INET6, &addr, buf, sizeof(buf));
216 char *p = buf + strlen(buf);
217 sprintf(p, "/%u", (unsigned int) vp->vp_strvalue[1]);
227 if (a != NULL) strlcpy(out, a, outlen);
233 * This is a hack, and has to be kept in sync with tokens.h
235 static const char *vp_tokens[] = {
236 "?", /* T_OP_INVALID */
267 * Print one attribute and value into a string.
269 int vp_prints(char *out, int outlen, VALUE_PAIR *vp)
272 const char *token = NULL;
277 if (strlen(vp->name) + 3 > (size_t)outlen) {
281 if ((vp->operator > T_OP_INVALID) &&
282 (vp->operator < T_TOKEN_LAST)) {
283 token = vp_tokens[vp->operator];
285 token = "<INVALID-TOKEN>";
288 if( vp->flags.has_tag ) {
290 snprintf(out, outlen, "%s:%d %s ", vp->name, vp->flags.tag,
294 vp_prints_value(out + len, outlen - len, vp, 1);
298 snprintf(out, outlen, "%s %s ", vp->name, token);
300 vp_prints_value(out + len, outlen - len, vp, 1);
309 * Print one attribute and value.
311 void vp_print(FILE *fp, VALUE_PAIR *vp)
315 vp_prints(buf, sizeof(buf), vp);
321 * Print a whole list of attributes, indented by a TAB
322 * and with a newline at the end.
324 void vp_printlist(FILE *fp, VALUE_PAIR *vp)
326 for (; vp; vp = vp->next) {