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