Browse code

*** empty log message ***

Andrei Pelinescu-Onciul authored on 09/09/2002 11:34:27
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,217 @@
1
+/* $Id$
2
+ *
3
+ *
4
+ * test programs, list all interfaces and their ip address
5
+ */
6
+
7
+
8
+#include <sys/ioctl.h>
9
+#include <net/if.h>
10
+
11
+#include <stdio.h>
12
+#include <stdlib.h>
13
+#include <ctype.h>
14
+#include <string.h>
15
+#include <unistd.h>
16
+#include <errno.h>
17
+#include <sys/types.h>
18
+#include <sys/socket.h>
19
+#include <netinet/in.h>
20
+
21
+#define FLAGS 1
22
+
23
+
24
+static char* version="ifls 0.1";
25
+static char* id="$Id$";
26
+static char* help_msg="\
27
+Usage: ifls [-6hV} [interface...]\n\
28
+(if no interface name is specified it will list all the interfaces)\n\
29
+Options:\n\
30
+    -a      list both ipv4 and ipv6 interfaces (default)\n\
31
+    -4      list only ipv4 interfaces\n\
32
+    -6      list only ipv6 interfaces\n\
33
+    -f      show also the interface flags\n\
34
+    -V      version number\n\
35
+    -h      this help message\n\
36
+";
37
+
38
+
39
+
40
+int ls_if(char* name, int family , int options)
41
+{
42
+	struct ifreq ifr;
43
+	int s;
44
+	unsigned char* buf;
45
+	int r;
46
+	
47
+	
48
+	s=socket(family, SOCK_DGRAM, 0);
49
+	strncpy(ifr.ifr_name, name, IFNAMSIZ);
50
+	if (ioctl(s, SIOCGIFADDR, &ifr)==-1){
51
+		if(errno==EBADF) return 0; /* invalid descriptor => no address*/
52
+		fprintf(stderr, "ls_if: ioctl failed: %s\n", strerror(errno));
53
+		goto error;
54
+	};
55
+	
56
+	printf("%s:\n", ifr.ifr_name);
57
+	if (ifr.ifr_addr.sa_family==AF_INET){
58
+		buf=&((struct sockaddr_in*)(&ifr.ifr_addr))->sin_addr;
59
+		printf("        %d.%d.%d.%d\n", buf[0], buf[1], buf[2], buf[3]);
60
+	}else{
61
+		buf=&((struct sockaddr_in6*)(&ifr.ifr_addr))->sin6_addr;
62
+		printf("        ");
63
+		for(r=0; r<16; r++) printf("%02x%s", buf[r], (r%2)?":":"" );
64
+		printf("\n");
65
+	}
66
+	if (options & FLAGS){
67
+		if (ioctl(s, SIOCGIFFLAGS, &ifr)==-1){
68
+			fprintf(stderr, "ls_if: ioctl failed: %s\n", strerror(errno));
69
+			goto error;
70
+		}
71
+		printf("        ");
72
+		if (ifr.ifr_flags & IFF_UP) printf ("UP ");
73
+		if (ifr.ifr_flags & IFF_BROADCAST) printf ("BROADCAST ");
74
+		if (ifr.ifr_flags & IFF_DEBUG) printf ("DEBUG ");
75
+		if (ifr.ifr_flags & IFF_LOOPBACK) printf ("LOOPBACK ");
76
+		if (ifr.ifr_flags & IFF_POINTOPOINT) printf ("POINTOPOINT ");
77
+		if (ifr.ifr_flags & IFF_RUNNING) printf ("RUNNING ");
78
+		if (ifr.ifr_flags & IFF_NOARP) printf ("NOARP ");
79
+		if (ifr.ifr_flags & IFF_PROMISC) printf ("PROMISC ");
80
+		if (ifr.ifr_flags & IFF_NOTRAILERS) printf ("NOTRAILERS ");
81
+		if (ifr.ifr_flags & IFF_ALLMULTI) printf ("ALLMULTI ");
82
+		if (ifr.ifr_flags & IFF_MASTER) printf ("MASTER ");
83
+		if (ifr.ifr_flags & IFF_SLAVE) printf ("SLAVE ");
84
+		if (ifr.ifr_flags & IFF_MULTICAST) printf ("MULTICAST ");
85
+		if (ifr.ifr_flags & IFF_PORTSEL) printf ("PORTSEL ");
86
+		if (ifr.ifr_flags & IFF_AUTOMEDIA) printf ("AUTOMEDIA ");
87
+		/*if (ifr.ifr_flags & IFF_DYNAMIC ) printf ("DYNAMIC ");*/
88
+		printf ("\n");
89
+	};
90
+
91
+	return 0;
92
+error:
93
+	return -1;
94
+}
95
+
96
+
97
+
98
+int ls_all(int family, int options)
99
+{
100
+	struct ifconf ifc;
101
+	struct ifreq* ifr;
102
+	char*  last;
103
+	int size;
104
+	int s;
105
+	
106
+	/* ipv4 or ipv6 only*/
107
+	s=socket(family, SOCK_DGRAM, 0);
108
+	for (size=10; ; size*=2){
109
+		ifc.ifc_len=size*sizeof(struct ifreq);
110
+		ifc.ifc_req=(struct ifreq*) malloc(size*sizeof(struct ifreq));
111
+		if (ifc.ifc_req==0){
112
+			fprintf(stderr, "memory allocation failure\n");
113
+			goto error;
114
+		}
115
+		if (ioctl(s, SIOCGIFCONF, &ifc)==-1){
116
+			if(errno==EBADF) return 0; /* invalid descriptor => no such ifs*/
117
+			fprintf(stderr, "ioctl failed: %s\n", strerror(errno));
118
+			goto error;
119
+		}
120
+		if (ifc.ifc_len<size*sizeof(struct ifreq)) break;
121
+		/* try a bigger array*/
122
+		free(ifc.ifc_req);
123
+	}
124
+	
125
+	last=(char*)ifc.ifc_req+ifc.ifc_len;
126
+	for(ifr=ifc.ifc_req; (char*)ifr<last;
127
+			ifr=(struct ifreq*)((char*)ifr+sizeof(ifr->ifr_name)+
128
+				( (ifr->ifr_addr.sa_family==AF_INET)?
129
+					sizeof(struct sockaddr_in): sizeof(struct sockaddr_in6)) )
130
+		)
131
+	{
132
+		ls_if(ifr->ifr_name, family, options);
133
+	}
134
+	free(ifc.ifc_req); /*clean up*/
135
+	return  0;
136
+error:
137
+	return -1;
138
+}
139
+
140
+
141
+int main(int argc, char**argv)
142
+{
143
+	char** name;
144
+	int no;
145
+	int options;
146
+	int ipv6, ipv4;
147
+	int r;
148
+	char c;
149
+	
150
+	
151
+	options=0;
152
+	ipv6=ipv4=1;
153
+	name=0;
154
+	no=0;
155
+	opterr=0;
156
+	while((c=getopt(argc, argv, "a46fhV"))!=-1){
157
+		switch(c){
158
+			case 'a':
159
+				ipv6=ipv4=1;
160
+				break;
161
+			case '4':
162
+				ipv6=0;
163
+				ipv4=1;
164
+				break;
165
+			case '6':
166
+				ipv4=0;
167
+				ipv6=1;
168
+				break;
169
+			case 'f':
170
+				options|=FLAGS;
171
+				break;
172
+			case 'V':
173
+				printf("version: %s\n", version);
174
+				printf("%s\n", id);
175
+				exit(0);
176
+				break;
177
+			case 'h':
178
+				printf("version: %s\n", version);
179
+				printf("%s", help_msg);
180
+				exit(0);
181
+				break;
182
+			case '?':
183
+				if (isprint(optopt))
184
+					fprintf(stderr, "Unknow option `-%c�\n", optopt);
185
+				else
186
+					fprintf(stderr, "Unknown character `\\x%x�\n", optopt);
187
+				goto error;
188
+			case ':':
189
+				fprintf(stderr, "Option `-%c� requires an argument\n",
190
+						optopt);
191
+				goto error;
192
+			default:
193
+				abort();
194
+		};
195
+	};
196
+	/* check if we have non-options */
197
+	if( optind < argc){
198
+		no=argc-optind;
199
+		name=&argv[optind];
200
+	}
201
+	
202
+	if (no==0){
203
+		/* list all interfaces */
204
+		if (ipv4) ls_all(AF_INET, options);
205
+		if (ipv6) ls_all(AF_INET6, options);
206
+	}else{
207
+		for(r=0; r<no; r++){
208
+			if (ipv4) ls_if(name[r], AF_INET, options);
209
+			if (ipv6) ls_if(name[r], AF_INET6, options);
210
+		}
211
+	};
212
+	
213
+	
214
+	exit(0);
215
+error:
216
+	exit(-1);
217
+};