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., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
20 * Copyright 2000 The FreeRADIUS server project
23 static const char rcsid[] = "$Id$";
29 #include <sys/types.h>
33 #include "libradius.h"
36 * Convert a string to something printable.
37 * The output string has to be _at least_ 4x the size
38 * of the input string!
40 void librad_safeprint(char *in, int inlen, char *out, int outlen)
42 unsigned char *str = (unsigned char *)in;
46 if (inlen < 0) inlen = strlen(in);
48 while (inlen-- > 0 && (done + 3) < outlen) {
50 * Hack: never print trailing zero.
51 * Some clients send strings with an off-by-one
52 * length (confused with strings in C).
54 if (inlen == 0 && *str == 0)
76 if (*str < 32 || (*str >= 128)){
77 snprintf(out, outlen, "\\%03o", *str);
100 * Print one value into a string.
101 * delimitst will define if strings and dates will be delimited by '"'
103 int vp_prints_value(char * out, int outlen, VALUE_PAIR *vp, int delimitst)
116 if ((delimitst == 1) && vp->flags.has_tag) {
117 /* Tagged attribute: print delimter and ignore tag */
119 librad_safeprint((char *)(vp->strvalue),
120 vp->length, buf + 1, sizeof(buf) - 2);
122 } else if (delimitst == 1) {
123 /* Non-tagged attribute: print delimter */
125 librad_safeprint((char *)vp->strvalue,
126 vp->length, buf + 1, sizeof(buf) - 2);
129 } else if (delimitst < 0) {
130 strNcpy(out, vp->strvalue, outlen);
134 /* Non-tagged attribute: no delimiter */
135 librad_safeprint((char *)vp->strvalue,
136 vp->length, buf, sizeof(buf));
140 case PW_TYPE_INTEGER:
141 if ( vp->flags.has_tag ) {
142 /* Attribute value has a tag, need to ignore it */
143 if ((v = dict_valbyattr(vp->attribute, (vp->lvalue & 0xffffff)))
147 snprintf(buf, sizeof(buf), "%u", (vp->lvalue & 0xffffff));
151 /* Normal, non-tagged attribute */
152 if ((v = dict_valbyattr(vp->attribute, vp->lvalue))
156 snprintf(buf, sizeof(buf), "%u", vp->lvalue);
164 strftime(buf, sizeof(buf), "\"%b %e %Y %H:%M:%S %Z\"",
165 localtime_r(&t, &s_tm));
167 strftime(buf, sizeof(buf), "%b %e %Y %H:%M:%S %Z",
168 localtime_r(&t, &s_tm));
174 a = (char *)vp->strvalue;
176 a = ip_hostname((char *)vp->strvalue,
177 sizeof(vp->strvalue),
180 case PW_TYPE_ABINARY:
183 print_abinary(vp, (unsigned char *)buf, sizeof(buf));
191 for (t = 0; t < vp->length; t++) {
192 sprintf(a, "%02x", vp->strvalue[t]);
199 a = ifid_ntoa(buf, sizeof(buf), vp->strvalue);
202 case PW_TYPE_IPV6ADDR:
203 a = ipv6_ntoa(buf, sizeof(buf), vp->strvalue);
210 strNcpy(out, a?a:"UNKNOWN-TYPE", outlen);
216 * This is a hack, and has to be kept in sync with tokens.h
218 static const char *vp_tokens[] = {
219 "?", /* T_OP_INVALID */
250 * Print one attribute and value into a string.
252 int vp_prints(char *out, int outlen, VALUE_PAIR *vp)
255 const char *token = NULL;
260 if (strlen(vp->name) + 3 > (size_t)outlen) {
264 if ((vp->operator > T_OP_INVALID) &&
265 (vp->operator < T_TOKEN_LAST)) {
266 token = vp_tokens[vp->operator];
268 token = "<INVALID-TOKEN>";
271 if( vp->flags.has_tag ) {
273 snprintf(out, outlen, "%s:%d %s ", vp->name, vp->flags.tag,
277 vp_prints_value(out + len, outlen - len, vp, 1);
281 snprintf(out, outlen, "%s %s ", vp->name, token);
283 vp_prints_value(out + len, outlen - len, vp, 1);
292 * Print one attribute and value.
294 void vp_print(FILE *fp, VALUE_PAIR *vp)
298 vp_prints(buf, sizeof(buf), vp);
304 * Print a whole list of attributes, indented by a TAB
305 * and with a newline at the end.
307 void vp_printlist(FILE *fp, VALUE_PAIR *vp)
309 for (; vp; vp = vp->next) {