Home | History | Annotate | Line # | Download | only in ldpd
ldp_errors.c revision 1.3
      1 /* $NetBSD: ldp_errors.c,v 1.3 2013/01/26 17:29:55 kefren Exp $ */
      2 
      3 /*
      4  * Copyright (c) 2010 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Mihai Chelaru <kefren (at) NetBSD.org>
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <arpa/inet.h>
     33 #include <sys/stat.h>
     34 #include <fcntl.h>
     35 #include <stdio.h>
     36 #include <stdarg.h>
     37 #include <syslog.h>
     38 #include <unistd.h>
     39 
     40 #include "ldp.h"
     41 #include "ldp_errors.h"
     42 
     43 int	debug_f = 0, warn_f = 0, syslog_f = 0;
     44 
     45 static void do_syslog(int, const char*, va_list) __printflike(2, 0);
     46 static char satos_str[INET6_ADDRSTRLEN > INET_ADDRSTRLEN ? INET6_ADDRSTRLEN :
     47 		INET_ADDRSTRLEN];
     48 
     49 void
     50 debugp(const char *fmt, ...)
     51 {
     52 	va_list va;
     53 
     54 	if (!debug_f)
     55 		return;
     56 
     57 	va_start(va, fmt);
     58 	if (syslog_f)
     59 		do_syslog(LOG_DEBUG, fmt, va);
     60 	else
     61 		vprintf(fmt, va);
     62 	va_end(va);
     63 }
     64 
     65 void
     66 warnp(const char *fmt, ...)
     67 {
     68 	va_list va;
     69 
     70 	if (!debug_f && !warn_f)
     71 		return;
     72 
     73 	va_start(va, fmt);
     74 	if (syslog_f)
     75 		do_syslog(LOG_WARNING, fmt, va);
     76 	else
     77 		vprintf(fmt, va);
     78 	va_end(va);
     79 }
     80 
     81 void
     82 fatalp(const char *fmt, ...)
     83 {
     84 	va_list va;
     85 
     86 	va_start(va, fmt);
     87 	if (syslog_f)
     88 		do_syslog(LOG_ERR, fmt, va);
     89 	else
     90 		vprintf(fmt, va);
     91 	va_end(va);
     92 }
     93 
     94 static void
     95 do_syslog(int prio, const char *fmt, va_list va)
     96 {
     97 	vsyslog(prio, fmt, va);
     98 }
     99 
    100 void
    101 printtime()
    102 {
    103 	time_t          t;
    104 	char            buf[26];
    105 	int             i;
    106 
    107 	time(&t);
    108 	ctime_r(&t, buf);
    109 	for (i = 0; (i < 26 && buf[i] != 0); i++)
    110 		if (buf[i] == '\n')
    111 			buf[i] = 0;
    112 	printf("%s ", buf);
    113 }
    114 
    115 const char *
    116 satos(const struct sockaddr *sa)
    117 {
    118 	switch (sa->sa_family) {
    119 		case AF_INET:
    120 		{
    121 			const struct sockaddr_in *sin =
    122 			    (const struct sockaddr_in *)sa;
    123 			if (inet_ntop(AF_INET, &(sin->sin_addr), satos_str,
    124 			    sizeof(satos_str)) == NULL)
    125 				return "INET ERROR";
    126 			break;
    127 		}
    128 		case AF_INET6:
    129 		{
    130 			const struct sockaddr_in6 *sin6 =
    131 			    (const struct sockaddr_in6 *)sa;
    132 			if (inet_ntop(AF_INET6, &(sin6->sin6_addr), satos_str,
    133 			    sizeof(satos_str)) == NULL)
    134 				return "INET6 ERROR";
    135 			break;
    136 		}
    137 		default:
    138 			return "UNKNOWN AF";
    139 	}
    140 	return satos_str;
    141 }
    142