forked from kamailio/kamailio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathifls.c
343 lines (310 loc) · 8.03 KB
/
ifls.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*
* test programs, list all interfaces and their ip address
*
* Copyright (C) 2001-2003 FhG Fokus
*
* This file is part of Kamailio, a free SIP server.
*
* Kamailio is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version
*
* Kamailio 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/*
* History:
* --------
* 2002-09-09 created by andrei
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/ioctl.h>
#include <net/if.h>
#ifdef __sun__
#include <sys/sockio.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#define FLAGS 1
#define IF_DOWN 2
#define IF_UP 4
static char* version="ifls 0.1";
static char* id="$Id$";
static char* help_msg="\
Usage: ifls [-6hV} [interface...]\n\
(if no interface name is specified it will list all the interfaces)\n\
Options:\n\
-a list both ipv4 and ipv6 interfaces (default)\n\
-4 list only ipv4 interfaces\n\
-6 list only ipv6 interfaces\n\
-f show also the interface flags\n\
-U brings all the matching interfaces up\n\
-D brings all the matching interfaces down\n\
-V version number\n\
-h this help message\n\
";
#define MAX(a,b) ( ((a)>(b))?(a):(b))
void print_sockaddr(struct sockaddr* sa)
{
unsigned char* buf;
int r;
switch(sa->sa_family){
case AF_INET:
buf=(char*)&(((struct sockaddr_in*)sa)->sin_addr).s_addr;
printf("%d.%d.%d.%d\n", buf[0], buf[1], buf[2], buf[3]);
break;
case AF_INET6:
buf=(((struct sockaddr_in6*)sa)->sin6_addr).s6_addr;
for(r=0; r<16; r++)
printf("%02x%s", buf[r], ((r%2)&&(r!=15))?":":"" );
printf("\n");
break;
default:
printf("unknown af %d\n", sa->sa_family);
#ifdef __FreeBSD__
for (r=0; r<sa->sa_len; r++)
printf("%02x ", ((unsigned char*)sa)[r]);
printf("\n");
#endif
}
}
int ls_ifflags(char* name, int family , int options)
{
struct ifreq ifr;
int s;
memset(&ifr, 0, sizeof(ifr)); /* init to 0 (check if filled)*/
s=socket(family, SOCK_DGRAM, 0);
strncpy(ifr.ifr_name, name, IFNAMSIZ);
#if 0
if (ioctl(s, SIOCGIFADDR, &ifr)==-1){
if(errno==EBADF) return 0; /* invalid descriptor => no address*/
fprintf(stderr, "ls_if: ioctl for %s failed: %s\n", name,
strerror(errno));
goto error;
};
printf("%s:\n", ifr.ifr_name);
printf(" dbg: family=%d", ifr.ifr_addr.sa_family);
#ifdef __FreeBSD__
printf(", len=%d\n", ifr.ifr_addr.sa_len);
#else
printf("\n");
#endif
if (ifr.ifr_addr.sa_family==0){
printf("ls_if: OS BUG: SIOCGIFADDR doesn't work!\n");
goto error;
}
printf(" ");
print_sockaddr(&ifr.ifr_addr);
if (ifr.ifr_addr.sa_family!=family){
printf("ls_if: strange family %d\n", ifr.ifr_addr.sa_family);
/*goto error;*/
}
#endif
if (options & (FLAGS|IF_DOWN|IF_UP)){
if (ioctl(s, SIOCGIFFLAGS, &ifr)==-1){
fprintf(stderr, "ls_if: flags ioctl for %s failed: %s\n",
name, strerror(errno));
goto error;
}
if (ifr.ifr_flags & IFF_UP) printf ("UP ");
if (ifr.ifr_flags & IFF_BROADCAST) printf ("BROADCAST ");
if (ifr.ifr_flags & IFF_DEBUG) printf ("DEBUG ");
if (ifr.ifr_flags & IFF_LOOPBACK) printf ("LOOPBACK ");
if (ifr.ifr_flags & IFF_POINTOPOINT) printf ("POINTOPOINT ");
if (ifr.ifr_flags & IFF_RUNNING) printf ("RUNNING ");
if (ifr.ifr_flags & IFF_NOARP) printf ("NOARP ");
if (ifr.ifr_flags & IFF_PROMISC) printf ("PROMISC ");
/*if (ifr.ifr_flags & IFF_NOTRAILERS) printf ("NOTRAILERS ");*/
if (ifr.ifr_flags & IFF_ALLMULTI) printf ("ALLMULTI ");
/*if (ifr.ifr_flags & IFF_MASTER) printf ("MASTER ");*/
/*if (ifr.ifr_flags & IFF_SLAVE) printf ("SLAVE ");*/
if (ifr.ifr_flags & IFF_MULTICAST) printf ("MULTICAST ");
/*if (ifr.ifr_flags & IFF_PORTSEL) printf ("PORTSEL ");*/
/*if (ifr.ifr_flags & IFF_AUTOMEDIA) printf ("AUTOMEDIA ");*/
/*if (ifr.ifr_flags & IFF_DYNAMIC ) printf ("DYNAMIC ");*/
printf ("\n");
if (options & IF_DOWN){
ifr.ifr_flags &= ~IFF_UP;
}
if (options & IF_UP){
ifr.ifr_flags |= IFF_UP;
}
if (options & (IF_UP|IF_DOWN)){
if (ioctl(s, SIOCSIFFLAGS, &ifr)==-1){
fprintf(stderr, "ls_if: set flags ioctl for %s failed: %s\n",
name, strerror(errno));
goto error;
}
}
};
close(s);
return 0;
error:
close(s);
return -1;
}
int ls_ifs(char* name, int family, int options)
{
struct ifconf ifc;
struct ifreq* ifr;
char* last;
int size;
int lastlen;
int s;
/* ipv4 or ipv6 only*/
s=socket(family, SOCK_DGRAM, 0);
lastlen=0;
for (size=2; ; size*=2){
ifc.ifc_len=size*sizeof(struct ifreq);
ifc.ifc_req=(struct ifreq*) malloc(size*sizeof(struct ifreq));
if (ifc.ifc_req==0){
fprintf(stderr, "memory allocation failure\n");
goto error;
}
if (ioctl(s, SIOCGIFCONF, &ifc)==-1){
if(errno==EBADF) return 0; /* invalid descriptor => no such ifs*/
fprintf(stderr, "ioctl failed: %s\n", strerror(errno));
goto error;
}
if ((lastlen) && (ifc.ifc_len==lastlen)) break; /*success,
len not changed*/
lastlen=ifc.ifc_len;
/* try a bigger array*/
free(ifc.ifc_req);
}
last=(char*)ifc.ifc_req+ifc.ifc_len;
for(ifr=ifc.ifc_req; (char*)ifr<last;
ifr=(struct ifreq*)((char*)ifr+sizeof(ifr->ifr_name)+
#ifdef __FreeBSD__
MAX(ifr->ifr_addr.sa_len, sizeof(struct sockaddr))
#else
( (ifr->ifr_addr.sa_family==AF_INET)?
sizeof(struct sockaddr_in):
((ifr->ifr_addr.sa_family==AF_INET6)?
sizeof(struct sockaddr_in6):sizeof(struct sockaddr)) )
#endif
)
)
{
/*
printf("\nls_all dbg: %s family=%d", ifr->ifr_name,
ifr->ifr_addr.sa_family);
#ifdef __FreeBSD__
printf(", len=%d\n", ifr->ifr_addr.sa_len);
#else
printf("\n");
#endif
*/
if (ifr->ifr_addr.sa_family!=family){
/*printf("strange family %d skipping...\n",
ifr->ifr_addr.sa_family);*/
continue;
}
if ((name==0)||
(strncmp(name, ifr->ifr_name, sizeof(ifr->ifr_name))==0)){
printf("%s:\n", ifr->ifr_name);
printf(" ");
print_sockaddr(&(ifr->ifr_addr));
printf(" ");
ls_ifflags(ifr->ifr_name, family, options);
printf("\n");
}
}
free(ifc.ifc_req); /*clean up*/
close(s);
return 0;
error:
close(s);
return -1;
}
int main(int argc, char**argv)
{
char** name;
int no;
int options;
int ipv6, ipv4;
int r;
char c;
options=0;
ipv6=ipv4=1;
name=0;
no=0;
opterr=0;
while((c=getopt(argc, argv, "a46fhVUD"))!=-1){
switch(c){
case 'a':
ipv6=ipv4=1;
break;
case '4':
ipv6=0;
ipv4=1;
break;
case '6':
ipv4=0;
ipv6=1;
break;
case 'f':
options|=FLAGS;
break;
case 'V':
printf("version: %s\n", version);
printf("%s\n", id);
exit(0);
break;
case 'D':
options|=IF_DOWN;
break;
case 'U':
options|=IF_UP;
break;
case 'h':
printf("version: %s\n", version);
printf("%s", help_msg);
exit(0);
break;
case '?':
if (isprint(optopt))
fprintf(stderr, "Unknown option `-%c´\n", optopt);
else
fprintf(stderr, "Unknown character `\\x%x´\n", optopt);
goto error;
case ':':
fprintf(stderr, "Option `-%c´ requires an argument\n",
optopt);
goto error;
default:
abort();
};
};
/* check if we have non-options */
if( optind < argc){
no=argc-optind;
name=&argv[optind];
}
if (no==0){
/* list all interfaces */
if (ipv4) ls_ifs(0, AF_INET, options);
if (ipv6) ls_ifs(0, AF_INET6, options);
}else{
for(r=0; r<no; r++){
if (ipv4) ls_ifs(name[r], AF_INET, options);
if (ipv6) ls_ifs(name[r], AF_INET6, options);
}
};
exit(0);
error:
exit(-1);
};