1 /* $OpenBSD: logmsg.c,v 1.1 2016/09/02 14:08:50 benno Exp $ */
2
3 /*
4 * Copyright (c) 2006 Claudio Jeker <claudio@openbsd.org>
5 * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20 #include <sys/types.h>
21 #include <sys/socket.h>
22 #include <netinet/in.h>
23 #include <arpa/inet.h>
24
25 #include <netdb.h>
26 #include <errno.h>
27 #include <stdarg.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <syslog.h>
32 #include <unistd.h>
33
34 #include "ospf6d.h"
35 #include "log.h"
36
37 const char *
log_in6addr(const struct in6_addr * addr)38 log_in6addr(const struct in6_addr *addr)
39 {
40 struct sockaddr_in6 sa_in6;
41
42 bzero(&sa_in6, sizeof(sa_in6));
43 sa_in6.sin6_len = sizeof(sa_in6);
44 sa_in6.sin6_family = AF_INET6;
45 memcpy(&sa_in6.sin6_addr, addr, sizeof(sa_in6.sin6_addr));
46
47 /*
48 * Destination addresses contain embedded scopes.
49 * They must be recovered for ospf6ctl show fib.
50 */
51 recoverscope(&sa_in6);
52
53 return (log_sockaddr(&sa_in6));
54 }
55
56 const char *
log_in6addr_scope(const struct in6_addr * addr,unsigned int ifindex)57 log_in6addr_scope(const struct in6_addr *addr, unsigned int ifindex)
58 {
59 struct sockaddr_in6 sa_in6;
60
61 bzero(&sa_in6, sizeof(sa_in6));
62 sa_in6.sin6_len = sizeof(sa_in6);
63 sa_in6.sin6_family = AF_INET6;
64 memcpy(&sa_in6.sin6_addr, addr, sizeof(sa_in6.sin6_addr));
65
66 addscope(&sa_in6, ifindex);
67
68 return (log_sockaddr(&sa_in6));
69 }
70
71 #define NUM_LOGS 4
72 const char *
log_rtr_id(u_int32_t id)73 log_rtr_id(u_int32_t id)
74 {
75 static char buf[NUM_LOGS][16];
76 static int round = 0;
77 struct in_addr addr;
78
79 round = (round + 1) % NUM_LOGS;
80
81 addr.s_addr = id;
82 if (inet_ntop(AF_INET, &addr, buf[round], 16) == NULL)
83 return ("?");
84 else
85 return buf[round];
86 }
87
88 const char *
log_sockaddr(void * vp)89 log_sockaddr(void *vp)
90 {
91 static char buf[NUM_LOGS][NI_MAXHOST];
92 static int round = 0;
93 struct sockaddr *sa = vp;
94
95 round = (round + 1) % NUM_LOGS;
96
97 if (getnameinfo(sa, sa->sa_len, buf[round], NI_MAXHOST, NULL, 0,
98 NI_NUMERICHOST))
99 return ("(unknown)");
100 else
101 return (buf[round]);
102 }
103
104 /* names */
105 const char *
nbr_state_name(int state)106 nbr_state_name(int state)
107 {
108 switch (state) {
109 case NBR_STA_DOWN:
110 return ("DOWN");
111 case NBR_STA_ATTEMPT:
112 return ("ATTMP");
113 case NBR_STA_INIT:
114 return ("INIT");
115 case NBR_STA_2_WAY:
116 return ("2-WAY");
117 case NBR_STA_XSTRT:
118 return ("EXSTA");
119 case NBR_STA_SNAP:
120 return ("SNAP");
121 case NBR_STA_XCHNG:
122 return ("EXCHG");
123 case NBR_STA_LOAD:
124 return ("LOAD");
125 case NBR_STA_FULL:
126 return ("FULL");
127 default:
128 return ("UNKNW");
129 }
130 }
131
132 const char *
if_state_name(int state)133 if_state_name(int state)
134 {
135 switch (state) {
136 case IF_STA_DOWN:
137 return ("DOWN");
138 case IF_STA_LOOPBACK:
139 return ("LOOP");
140 case IF_STA_WAITING:
141 return ("WAIT");
142 case IF_STA_POINTTOPOINT:
143 return ("P2P");
144 case IF_STA_DROTHER:
145 return ("OTHER");
146 case IF_STA_BACKUP:
147 return ("BCKUP");
148 case IF_STA_DR:
149 return ("DR");
150 default:
151 return ("UNKNW");
152 }
153 }
154
155 const char *
if_type_name(enum iface_type type)156 if_type_name(enum iface_type type)
157 {
158 switch (type) {
159 case IF_TYPE_POINTOPOINT:
160 return ("POINTOPOINT");
161 case IF_TYPE_BROADCAST:
162 return ("BROADCAST");
163 case IF_TYPE_NBMA:
164 return ("NBMA");
165 case IF_TYPE_POINTOMULTIPOINT:
166 return ("POINTOMULTIPOINT");
167 case IF_TYPE_VIRTUALLINK:
168 return ("VIRTUALLINK");
169 }
170 /* NOTREACHED */
171 return ("UNKNOWN");
172 }
173
174 const char *
dst_type_name(enum dst_type type)175 dst_type_name(enum dst_type type)
176 {
177 switch (type) {
178 case DT_NET:
179 return ("Network");
180 case DT_RTR:
181 return ("Router");
182 }
183 /* NOTREACHED */
184 return ("unknown");
185 }
186
187 const char *
path_type_name(enum path_type type)188 path_type_name(enum path_type type)
189 {
190 switch (type) {
191 case PT_INTRA_AREA:
192 return ("Intra-Area");
193 case PT_INTER_AREA:
194 return ("Inter-Area");
195 case PT_TYPE1_EXT:
196 return ("Type 1 ext");
197 case PT_TYPE2_EXT:
198 return ("Type 2 ext");
199 }
200 /* NOTREACHED */
201 return ("unknown");
202 }
203