-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtables.c
232 lines (210 loc) · 4.64 KB
/
tables.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*
* Copyright 2013 Kurt Van Dijck <[email protected]>
*
* This file is part of libenumif.
*
* This library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser Public License for more details.
*
* You should have received a copy of the GNU Lesser Public License
* along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* This file exports string representations of numbers that
* are forwarded from rtnetlink
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <linux/if.h>
#include <linux/if_arp.h>
#include "libenumif.h"
struct lookup {
int i;
const char *a;
};
static const char *lookup_i(int i, const struct lookup *table)
{
for (; table->a; ++table) {
if (table->i == i)
return table->a;
}
return NULL;
}
static int lookup_a(const char *str, const struct lookup *table, int _default)
{
const struct lookup *saved = NULL;
if (*str == '#')
return strtoul(str+1, 0, 0);
for (; table->a; ++table) {
if (!strncasecmp(table->a, str, strlen(str))) {
if (saved)
/* duplicate match */
return _default;
saved = table;
}
}
return saved ? saved->i : _default;
}
#define __PF(f,n) { ARPHRD_##f, #n },
static const struct lookup iftypes[] = {
{ 0, "generic", },
__PF(ETHER,ether)
__PF(EETHER,eether)
__PF(AX25,ax25)
__PF(PRONET,pronet)
__PF(CHAOS,chaos)
__PF(IEEE802,ieee802)
__PF(ARCNET,arcnet)
__PF(APPLETLK,atalk)
__PF(DLCI,dlci)
__PF(ATM,atm)
__PF(METRICOM,metricom)
__PF(IEEE1394,ieee1394)
__PF(INFINIBAND,infiniband)
__PF(SLIP,slip)
__PF(CSLIP,cslip)
__PF(SLIP6,slip6)
__PF(CSLIP6,cslip6)
__PF(RSRVD,rsrvd)
__PF(ADAPT,adapt)
__PF(ROSE,rose)
__PF(X25,x25)
__PF(HWX25,hwx25)
__PF(CAN,can)
__PF(PPP,ppp)
__PF(HDLC,hdlc)
__PF(LAPB,lapb)
__PF(DDCMP,ddcmp)
__PF(RAWHDLC,rawhdlc)
__PF(TUNNEL,ipip)
__PF(TUNNEL6,tunnel6)
__PF(FRAD,frad)
__PF(SKIP,skip)
__PF(LOOPBACK,loopback)
__PF(LOCALTLK,ltalk)
__PF(FDDI,fddi)
__PF(BIF,bif)
__PF(SIT,sit)
__PF(IPDDP,ip/ddp)
__PF(IPGRE,gre)
__PF(PIMREG,pimreg)
__PF(HIPPI,hippi)
__PF(ASH,ash)
__PF(ECONET,econet)
__PF(IRDA,irda)
__PF(FCPP,fcpp)
__PF(FCAL,fcal)
__PF(FCPL,fcpl)
__PF(FCFABRIC,fcfb0)
__PF(FCFABRIC+1,fcfb1)
__PF(FCFABRIC+2,fcfb2)
__PF(FCFABRIC+3,fcfb3)
__PF(FCFABRIC+4,fcfb4)
__PF(FCFABRIC+5,fcfb5)
__PF(FCFABRIC+6,fcfb6)
__PF(FCFABRIC+7,fcfb7)
__PF(FCFABRIC+8,fcfb8)
__PF(FCFABRIC+9,fcfb9)
__PF(FCFABRIC+10,fcfb10)
__PF(FCFABRIC+11,fcfb11)
__PF(FCFABRIC+12,fcfb12)
__PF(IEEE802_TR,tr)
__PF(IEEE80211,ieee802.11)
__PF(IEEE80211_PRISM,ieee802.11/prism)
__PF(IEEE80211_RADIOTAP,ieee802.11/radiotap)
#ifdef ARPHD_IEEE802154
__PF(IEEE802154, ieee802.15.4)
#endif
#ifdef ARPHD_PHONET
__PF(PHONET, phonet)
#endif
#ifdef ARPHD_PIPE
__PF(PHONET_PIPE, phonet_pipe)
#endif
__PF(NONE, none)
__PF(VOID,void)
{ },
};
#define __IF(f) { IFF_##f, #f, }
static const struct lookup ifflags[] = {
__IF(LOOPBACK),
__IF(BROADCAST),
__IF(POINTOPOINT),
__IF(MULTICAST),
__IF(NOARP),
__IF(ALLMULTI),
__IF(PROMISC),
__IF(MASTER),
__IF(SLAVE),
__IF(DEBUG),
__IF(DYNAMIC),
__IF(AUTOMEDIA),
__IF(PORTSEL),
__IF(NOTRAILERS),
__IF(UP),
__IF(LOWER_UP),
__IF(DORMANT),
__IF(ECHO),
{},
};
#define __ST(f) { IF_OPER_##f, #f, }
static const struct lookup ifstates[] = {
__ST(UNKNOWN),
__ST(NOTPRESENT),
__ST(DOWN),
__ST(LOWERLAYERDOWN),
__ST(TESTING),
__ST(DORMANT),
__ST(UP),
{},
};
const char *if_typestr(int iftype)
{
static char buf[16];
return lookup_i(iftype, iftypes) ?: (sprintf(buf, "#%i", iftype), buf);
}
const char *if_flagsstr(int if_flags)
{
static char buf[1024];
char *str = buf;
const struct lookup *lp;
/* from iproute2 */
if ((if_flags & IFF_UP) && !(if_flags & IFF_RUNNING))
str += sprintf(str, "%s%s", "NO-CARRIER", if_flags ? "," : "");
if_flags &= ~IFF_RUNNING;
for (lp = ifflags; lp->a; ++lp) {
if (if_flags & lp->i) {
if_flags &= ~lp->i;
str += sprintf(str, "%s%s", lp->a, if_flags ? "," : "");
}
}
if (if_flags)
str += sprintf(str, "%x", if_flags);
return buf;
}
const char *if_statestr(int ifstate)
{
static char buf[16];
return lookup_i(ifstate, ifstates) ?: (sprintf(buf, "#%i", ifstate), buf);
}
int if_strtype(const char *str)
{
return lookup_a(str, iftypes, 0);
}
int if_strflags(const char *str)
{
return lookup_a(str, ifflags, 0);
}
int if_strstate(const char *str)
{
return lookup_a(str, ifstates, 0);
}