Browse code

- tests

Andrei Pelinescu-Onciul authored on 09/09/2002 16:28:17
Showing 1 changed files
... ...
@@ -5,8 +5,14 @@
5 5
  */
6 6
 
7 7
 
8
+#include <sys/types.h>
9
+#include <sys/socket.h>
10
+#include <netinet/in.h>
8 11
 #include <sys/ioctl.h>
9 12
 #include <net/if.h>
13
+#ifdef __sun__
14
+#include <sys/sockio.h>
15
+#endif
10 16
 
11 17
 #include <stdio.h>
12 18
 #include <stdlib.h>
... ...
@@ -14,9 +20,6 @@
14 20
 #include <string.h>
15 21
 #include <unistd.h>
16 22
 #include <errno.h>
17
-#include <sys/types.h>
18
-#include <sys/socket.h>
19
-#include <netinet/in.h>
20 23
 
21 24
 #define FLAGS 1
22 25
 
... ...
@@ -36,39 +39,80 @@ Options:\n\
36 39
 ";
37 40
 
38 41
 
42
+#define MAX(a,b) ( ((a)>(b))?(a):(b))
43
+
44
+
39 45
 
40
-int ls_if(char* name, int family , int options)
46
+void print_sockaddr(struct sockaddr* sa)
41 47
 {
42
-	struct ifreq ifr;
43
-	int s;
44 48
 	unsigned char* buf;
45 49
 	int r;
46 50
 	
51
+	switch(sa->sa_family){
52
+	case AF_INET:
53
+		buf=(char*)&(((struct sockaddr_in*)sa)->sin_addr).s_addr;
54
+		printf("%d.%d.%d.%d\n", buf[0], buf[1], buf[2], buf[3]);
55
+		break;
56
+	case AF_INET6:
57
+		buf=(((struct sockaddr_in6*)sa)->sin6_addr).s6_addr;
58
+		for(r=0; r<16; r++) 
59
+			printf("%02x%s", buf[r], ((r%2)&&(r!=15))?":":"" );
60
+		printf("\n");
61
+		break;
62
+	default:
63
+		printf("unknown af %d\n", sa->sa_family);
64
+#ifdef __FreeBSD__
65
+		for (r=0; r<sa->sa_len; r++) 
66
+			printf("%02x ", ((unsigned char*)sa)[r]);
67
+		printf("\n");
68
+#endif
69
+	}
70
+}
71
+
72
+
73
+
74
+int ls_ifflags(char* name, int family , int options)
75
+{
76
+	struct ifreq ifr;
77
+	int s;
47 78
 	
79
+	memset(&ifr, 0, sizeof(ifr)); /* init to 0 (check if filled)*/
48 80
 	s=socket(family, SOCK_DGRAM, 0);
49 81
 	strncpy(ifr.ifr_name, name, IFNAMSIZ);
82
+#if 0	
50 83
 	if (ioctl(s, SIOCGIFADDR, &ifr)==-1){
51 84
 		if(errno==EBADF) return 0; /* invalid descriptor => no address*/
52
-		fprintf(stderr, "ls_if: ioctl failed: %s\n", strerror(errno));
85
+		fprintf(stderr, "ls_if: ioctl for %s failed: %s\n", name, 
86
+					strerror(errno));
53 87
 		goto error;
54 88
 	};
55 89
 	
56 90
 	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");
91
+	printf("        dbg: family=%d", ifr.ifr_addr.sa_family);
92
+#ifdef __FreeBSD__
93
+	printf(", len=%d\n", ifr.ifr_addr.sa_len);
94
+#else
95
+	printf("\n");
96
+#endif
97
+	if (ifr.ifr_addr.sa_family==0){
98
+		printf("ls_if: OS BUG: SIOCGIFADDR doesn't work!\n");
99
+		goto error;
100
+	}
101
+	
102
+	printf("        ");
103
+	print_sockaddr(&ifr.ifr_addr);
104
+
105
+	if (ifr.ifr_addr.sa_family!=family){
106
+		printf("ls_if: strange family %d\n", ifr.ifr_addr.sa_family);
107
+		/*goto error;*/
65 108
 	}
109
+#endif
66 110
 	if (options & FLAGS){
67 111
 		if (ioctl(s, SIOCGIFFLAGS, &ifr)==-1){
68
-			fprintf(stderr, "ls_if: ioctl failed: %s\n", strerror(errno));
112
+			fprintf(stderr, "ls_if: flags ioctl for %s  failed: %s\n",
113
+					name, strerror(errno));
69 114
 			goto error;
70 115
 		}
71
-		printf("        ");
72 116
 		if (ifr.ifr_flags & IFF_UP) printf ("UP ");
73 117
 		if (ifr.ifr_flags & IFF_BROADCAST) printf ("BROADCAST ");
74 118
 		if (ifr.ifr_flags & IFF_DEBUG) printf ("DEBUG ");
... ...
@@ -77,13 +121,13 @@ int ls_if(char* name, int family , int options)
77 121
 		if (ifr.ifr_flags & IFF_RUNNING) printf ("RUNNING ");
78 122
 		if (ifr.ifr_flags & IFF_NOARP) printf ("NOARP ");
79 123
 		if (ifr.ifr_flags & IFF_PROMISC) printf ("PROMISC ");
80
-		if (ifr.ifr_flags & IFF_NOTRAILERS) printf ("NOTRAILERS ");
124
+		/*if (ifr.ifr_flags & IFF_NOTRAILERS) printf ("NOTRAILERS ");*/
81 125
 		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 ");
126
+		/*if (ifr.ifr_flags & IFF_MASTER) printf ("MASTER ");*/
127
+		/*if (ifr.ifr_flags & IFF_SLAVE) printf ("SLAVE ");*/
84 128
 		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 ");
129
+		/*if (ifr.ifr_flags & IFF_PORTSEL) printf ("PORTSEL ");*/
130
+		/*if (ifr.ifr_flags & IFF_AUTOMEDIA) printf ("AUTOMEDIA ");*/
87 131
 		/*if (ifr.ifr_flags & IFF_DYNAMIC ) printf ("DYNAMIC ");*/
88 132
 		printf ("\n");
89 133
 	};
... ...
@@ -95,17 +139,19 @@ error:
95 139
 
96 140
 
97 141
 
98
-int ls_all(int family, int options)
142
+int ls_ifs(char* name, int family, int options)
99 143
 {
100 144
 	struct ifconf ifc;
101 145
 	struct ifreq* ifr;
102 146
 	char*  last;
103 147
 	int size;
148
+	int lastlen;
104 149
 	int s;
105 150
 	
106 151
 	/* ipv4 or ipv6 only*/
107 152
 	s=socket(family, SOCK_DGRAM, 0);
108
-	for (size=10; ; size*=2){
153
+	lastlen=0;
154
+	for (size=2; ; size*=2){
109 155
 		ifc.ifc_len=size*sizeof(struct ifreq);
110 156
 		ifc.ifc_req=(struct ifreq*) malloc(size*sizeof(struct ifreq));
111 157
 		if (ifc.ifc_req==0){
... ...
@@ -117,7 +163,9 @@ int ls_all(int family, int options)
117 163
 			fprintf(stderr, "ioctl failed: %s\n", strerror(errno));
118 164
 			goto error;
119 165
 		}
120
-		if (ifc.ifc_len<size*sizeof(struct ifreq)) break;
166
+		if  ((lastlen) && (ifc.ifc_len==lastlen)) break; /*success,
167
+														   len not changed*/
168
+		lastlen=ifc.ifc_len;
121 169
 		/* try a bigger array*/
122 170
 		free(ifc.ifc_req);
123 171
 	}
... ...
@@ -125,11 +173,40 @@ int ls_all(int family, int options)
125 173
 	last=(char*)ifc.ifc_req+ifc.ifc_len;
126 174
 	for(ifr=ifc.ifc_req; (char*)ifr<last;
127 175
 			ifr=(struct ifreq*)((char*)ifr+sizeof(ifr->ifr_name)+
176
+			#ifdef  __FreeBSD__
177
+				MAX(ifr->ifr_addr.sa_len, sizeof(struct sockaddr))
178
+			#else
128 179
 				( (ifr->ifr_addr.sa_family==AF_INET)?
129
-					sizeof(struct sockaddr_in): sizeof(struct sockaddr_in6)) )
180
+					sizeof(struct sockaddr_in):
181
+					((ifr->ifr_addr.sa_family==AF_INET6)?
182
+						sizeof(struct sockaddr_in6):sizeof(struct sockaddr)) )
183
+			#endif
184
+				)
130 185
 		)
131 186
 	{
132
-		ls_if(ifr->ifr_name, family, options);
187
+/*
188
+		printf("\nls_all dbg: %s family=%d", ifr->ifr_name,
189
+				 						ifr->ifr_addr.sa_family);
190
+#ifdef __FreeBSD__
191
+		printf(", len=%d\n", ifr->ifr_addr.sa_len);
192
+#else
193
+		printf("\n");
194
+#endif
195
+*/
196
+		if (ifr->ifr_addr.sa_family!=family){
197
+			/*printf("strange family %d skipping...\n",
198
+					ifr->ifr_addr.sa_family);*/
199
+			continue;
200
+		}
201
+		if ((name==0)||
202
+			(strncmp(name, ifr->ifr_name, sizeof(ifr->ifr_name))==0)){
203
+			printf("%s:\n", ifr->ifr_name);
204
+			printf("        ");
205
+			print_sockaddr(&(ifr->ifr_addr));
206
+			printf("        ");
207
+			ls_ifflags(ifr->ifr_name, family, options);
208
+			printf("\n");
209
+		}
133 210
 	}
134 211
 	free(ifc.ifc_req); /*clean up*/
135 212
 	return  0;
... ...
@@ -201,12 +278,12 @@ int main(int argc, char**argv)
201 278
 	
202 279
 	if (no==0){
203 280
 		/* list all interfaces */
204
-		if (ipv4) ls_all(AF_INET, options);
205
-		if (ipv6) ls_all(AF_INET6, options);
281
+		if (ipv4) ls_ifs(0, AF_INET, options);
282
+		if (ipv6) ls_ifs(0, AF_INET6, options);
206 283
 	}else{
207 284
 		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);
285
+			if (ipv4) ls_ifs(name[r], AF_INET, options);
286
+			if (ipv6) ls_ifs(name[r], AF_INET6, options);
210 287
 		}
211 288
 	};
212 289
 	
... ...
@@ -214,4 +291,4 @@ int main(int argc, char**argv)
214 291
 	exit(0);
215 292
 error:
216 293
 	exit(-1);
217
-};	
294
+};