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: #include <basic.h>
47: #include <stdio.h>
48: #include <stdlib.h>
49: #include <stdarg.h>
50: #include <string.h>
51: #include <t2ex/socket.h>
52:
53: #include "network_sample/util.h"
54: #include "network_sample/route.h"
55:
56: static void net_show_inet(const struct ifaddrs *ifa)
57: {
58: unsigned char str[32];
59: struct sockaddr_in *sin;
60:
61: sin = (struct sockaddr_in *)ifa->ifa_addr;
62: printf(" inet: %s\n",
63: inet_ntop(AF_INET, &sin->sin_addr, str, sizeof(str)));
64:
65: if ((ifa->ifa_flags & IFF_BROADCAST) != 0) {
66: sin = (struct sockaddr_in *)ifa->ifa_broadaddr;
67: printf(" broadcast: %s\n",
68: inet_ntop(AF_INET, &sin->sin_addr, str, sizeof(str)));
69: }
70:
71: sin = (struct sockaddr_in *)ifa->ifa_netmask;
72: printf(" netmask: %s\n",
73: inet_ntop(AF_INET, &sin->sin_addr, str, sizeof(str)));
74: }
75:
76: static void net_show_link(const struct ifaddrs *ifa)
77: {
78: char buf[32];
79: int len;
80:
81: len = so_getnameinfo(ifa->ifa_addr, ifa->ifa_addr->sa_len,
82: buf, sizeof(buf), NULL, 0, NI_NUMERICHOST, NULL);
83: if (len < 0) {
84: return;
85: }
86: if (buf[0] == '\0') {
87: return;
88: }
89:
90: printf(" address: %s\n", buf);
91: }
92:
93: static void net_show_if(void)
94: {
95: struct ifaddrs *ifa;
96: void *buf;
97: int len;
98: char lastname[IFNAMSIZ];
99:
100: len = so_getifaddrs(&ifa, NULL, 0);
101: if (len < 0) {
102: printf("error: so_getifaddrs: %d(%d,%d)\n", len, MERCD(len),
103: SERCD(len));
104: return;
105: }
106: buf = malloc(len);
107: if (buf == NULL) {
108: return;
109: }
110: len = so_getifaddrs(&ifa, buf, len);
111: if (len < 0) {
112: printf("error: so_getifaddrs: %d(%d,%d)\n", len, MERCD(len),
113: SERCD(len));
114: free(buf);
115: return;
116: }
117:
118: lastname[0] = '\0';
119: for( ; ifa != NULL; ifa=ifa->ifa_next) {
120: if (strncmp(ifa->ifa_name, lastname, IFNAMSIZ) != 0) {
121: printf("%s:\n", ifa->ifa_name);
122: strncpy(lastname, ifa->ifa_name, IFNAMSIZ-1);
123: }
124: if (ifa->ifa_addr->sa_family == AF_LINK) {
125: net_show_link(ifa);
126: }
127: else if (ifa->ifa_addr->sa_family == AF_INET) {
128: net_show_inet(ifa);
129: }
130: }
131: free(buf);
132: }
133:
134: static void net_show_dns(void)
135: {
136: union {
137: struct sockaddr *stop;
138: char *ctop;
139: UB c[256];
140: } buf;
141: int len, i;
142: struct sockaddr_in *sin;
143: struct sockaddr **res;
144: unsigned char rbuf[32];
145: char **domains;
146:
147: len = so_resctl(SO_RES_GET_SERVERS, &buf.stop, sizeof(buf));
148: if (len < 0) {
149: printf("error: so_resctl()\n");
150: return;
151: }
152: res = &buf.stop;
153: for( i=0; res[i] != NULL; i++ ) {
154: sin = (struct sockaddr_in *)res[i];
155: printf( "server: %s\n",
156: inet_ntop(AF_INET, &sin->sin_addr, rbuf, sizeof(rbuf)));
157: }
158:
159: len = so_resctl(SO_RES_GET_DOMAINS, &buf.ctop, sizeof(buf));
160: if (len < 0) {
161: printf("error: so_resctl()\n");
162: return;
163: }
164: domains = &buf.ctop;
165: for( i=0; domains[i] != NULL; i++ ) {
166: printf( "domain: %s\n", domains[i] );
167: }
168: }
169:
170: static void net_show_hosttable(void) __attribute__((noinline));
171: static void net_show_hosttable(void)
172: {
173: union {
174: struct hosttable top;
175: UB c[256];
176: } buf;
177: int len, i;
178: struct hosttable *table;
179: unsigned char rbuf[32];
180: struct sockaddr_in *sin;
181:
182: len = so_resctl(SO_RES_GET_TABLES, &buf.top, sizeof(buf));
183: if (len < 0) {
184: printf("error: so_resctl()\n");
185: return;
186: }
187: table = &buf.top;
188: for(i=0; table[i].addr != NULL; i++) {
189: sin = (struct sockaddr_in *)table[i].addr;
190: printf("%-18s ", inet_ntop(AF_INET, &sin->sin_addr, rbuf, sizeof(rbuf)));
191: printf("%s ", table[i].host );
192: printf("%s\n", table[i].aliases != NULL ? table[i].aliases : "");
193: }
194: }
195:
196: void net_show(void)
197: {
198: printf("\n--------------------------------\n");
199: printf("Network interface configurations\n");
200: printf("--------------------------------\n");
201:
202: net_show_if();
203:
204: printf("\n-----------------\n");
205: printf("DNS configuraions\n");
206: printf("-----------------\n");
207:
208: net_show_dns();
209:
210: printf("\n----------\n");
211: printf("Host table\n");
212: printf("----------\n");
213:
214: net_show_hosttable();
215:
216: printf("\n-------------------\n");
217: printf("Routing information\n");
218: printf("-------------------\n");
219:
220: dump_rtlist();
221:
222: printf("\n");
223: }