1 /* $NetBSD: ldp_errors.c,v 1.4 2013/07/16 02:54:32 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@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 <netmpls/mpls.h> 34 #include <sys/stat.h> 35 #include <fcntl.h> 36 #include <stdio.h> 37 #include <stdarg.h> 38 #include <string.h> 39 #include <syslog.h> 40 #include <unistd.h> 41 42 #include "ldp.h" 43 #include "ldp_errors.h" 44 45 int debug_f = 0, warn_f = 0, syslog_f = 0; 46 47 static void do_syslog(int, const char*, va_list) __printflike(2, 0); 48 static char satos_str[INET6_ADDRSTRLEN > INET_ADDRSTRLEN ? INET6_ADDRSTRLEN : 49 INET_ADDRSTRLEN]; 50 static char *mpls_ntoa(const struct sockaddr_mpls *smpls); 51 52 void 53 debugp(const char *fmt, ...) 54 { 55 va_list va; 56 57 if (!debug_f) 58 return; 59 60 va_start(va, fmt); 61 if (syslog_f) 62 do_syslog(LOG_DEBUG, fmt, va); 63 else 64 vprintf(fmt, va); 65 va_end(va); 66 } 67 68 void 69 warnp(const char *fmt, ...) 70 { 71 va_list va; 72 73 if (!debug_f && !warn_f) 74 return; 75 76 va_start(va, fmt); 77 if (syslog_f) 78 do_syslog(LOG_WARNING, fmt, va); 79 else 80 vprintf(fmt, va); 81 va_end(va); 82 } 83 84 void 85 fatalp(const char *fmt, ...) 86 { 87 va_list va; 88 89 va_start(va, fmt); 90 if (syslog_f) 91 do_syslog(LOG_ERR, fmt, va); 92 else 93 vprintf(fmt, va); 94 va_end(va); 95 } 96 97 static void 98 do_syslog(int prio, const char *fmt, va_list va) 99 { 100 vsyslog(prio, fmt, va); 101 } 102 103 void 104 printtime() 105 { 106 time_t t; 107 char buf[26]; 108 int i; 109 110 time(&t); 111 ctime_r(&t, buf); 112 for (i = 0; (i < 26 && buf[i] != 0); i++) 113 if (buf[i] == '\n') 114 buf[i] = 0; 115 printf("%s ", buf); 116 } 117 118 const char * 119 satos(const struct sockaddr *sa) 120 { 121 switch (sa->sa_family) { 122 case AF_INET: 123 { 124 const struct sockaddr_in *sin = 125 (const struct sockaddr_in *)sa; 126 if (inet_ntop(AF_INET, &(sin->sin_addr), satos_str, 127 sizeof(satos_str)) == NULL) 128 return "INET ERROR"; 129 break; 130 } 131 case AF_INET6: 132 { 133 const struct sockaddr_in6 *sin6 = 134 (const struct sockaddr_in6 *)sa; 135 if (inet_ntop(AF_INET6, &(sin6->sin6_addr), satos_str, 136 sizeof(satos_str)) == NULL) 137 return "INET6 ERROR"; 138 break; 139 } 140 case AF_LINK: 141 { 142 strlcpy(satos_str, 143 link_ntoa((const struct sockaddr_dl *)sa), 144 sizeof(satos_str)); 145 break; 146 } 147 case AF_MPLS: 148 { 149 strlcpy(satos_str, 150 mpls_ntoa((const struct sockaddr_mpls *)sa), 151 sizeof(satos_str)); 152 break; 153 } 154 default: 155 return "UNKNOWN AF"; 156 } 157 return satos_str; 158 } 159 160 static char * 161 mpls_ntoa(const struct sockaddr_mpls *smpls) 162 { 163 static char ret[10]; 164 union mpls_shim ms2; 165 166 ms2.s_addr = ntohl(smpls->smpls_addr.s_addr); 167 snprintf(ret, sizeof(ret), "%d", ms2.shim.label); 168 return ret; 169 } 170