Improve the process for GNU tools
[minix3.git] / minix / commands / udpstat / udpstat.c
blob882152245217aa3626c9cdb1f273e0d6c8524567
1 /*
2 udpstat.c
4 Created: March 2001 by Philip Homburg <philip@f-mnx.phicoh.com>
5 */
7 #define _POSIX_C_SOURCE 2
8 #define _NETBSD_SOURCE 1
10 #include <inet/inet.h>
11 #undef printf
12 #undef send
14 #include <assert.h>
15 #include <fcntl.h>
16 #include <stdio.h>
17 #include <time.h>
18 #include <unistd.h>
19 #ifndef __minix_vmd
20 #include <sys/times.h>
21 #endif
22 #include <net/netlib.h>
23 #include <net/gen/inet.h>
24 #include <netdb.h>
25 #include <net/gen/socket.h>
26 #include <minix/queryparam.h>
28 #include <inet/generic/buf.h>
29 #include <inet/generic/clock.h>
30 #include <inet/generic/event.h>
31 #include <inet/generic/type.h>
32 #include <inet/generic/udp_int.h>
33 #include <arpa/inet.h>
35 char *prog_name;
36 udp_fd_t udp_fd_table[UDP_FD_NR];
37 udp_port_t *udp_port_table;
38 udp_port_t *udp_port_tablep;
39 size_t udp_port_table_s;
40 size_t udp_port_table_rs;
41 int udp_port_nr;
42 char values[6 * sizeof(void *) + 3];
43 char *valuesl= NULL;
44 size_t v_size;
45 int inclSel, numerical;
47 void print_fd(int i, clock_t now);
48 void usage(void);
50 int main(int argc, char*argv[])
52 char *ipstat_device;
53 int fd, i, r;
54 size_t psize;
55 char *pval, *param;
56 #ifdef __minix_vmd
57 struct timeval uptime;
58 #endif
59 clock_t now;
60 int fl;
61 int a_flag, n_flag;
62 struct tms tmsbuf;
64 (prog_name=strrchr(argv[0], '/')) ? prog_name++ : (prog_name=argv[0]);
66 a_flag= 0;
67 n_flag= 0;
68 while ((fl= getopt(argc, argv, "?an")) != -1)
70 switch(fl)
72 case '?':
73 usage();
74 case 'a':
75 a_flag= 1;
76 break;
77 case 'n':
78 n_flag= 1;
79 break;
80 default:
81 fprintf(stderr, "%s: getopt failed: '%c'\n",
82 prog_name, fl);
83 exit(1);
86 inclSel= !!a_flag;
87 numerical= !!n_flag;
89 ipstat_device= IPSTAT_DEVICE;
90 if ((fd= open(ipstat_device, O_RDWR)) == -1)
92 fprintf(stderr, "%s: unable to open '%s': %s\n", prog_name,
93 ipstat_device, strerror(errno));
94 exit(1);
97 v_size= 2*sizeof(udp_fd_table)+1;
98 valuesl= realloc(valuesl, v_size);
99 if (!valuesl)
101 fprintf(stderr, "%s: unable to malloc %u bytes for valuesl\n",
102 prog_name, v_size);
103 exit(1);
106 param= "udp_fd_table";
107 psize= strlen(param);
108 r= write(fd, param, psize);
109 if (r != psize)
111 fprintf(stderr, "%s: write to %s failed: %s\n", prog_name,
112 ipstat_device,
113 r < 0 ? strerror(errno) : "short write");
114 exit(1);
116 r= read(fd, valuesl, v_size);
117 if (r < 0)
119 fprintf(stderr, "%s: read from %s failed: %s\n", prog_name,
120 ipstat_device, strerror(errno));
121 exit(1);
123 pval= valuesl;
124 if (paramvalue(&pval, udp_fd_table, sizeof(udp_fd_table)) !=
125 sizeof(udp_fd_table))
127 fprintf(stderr,
128 "%s: unable to decode the results from queryparam (udp_fd_table)\n",
129 prog_name);
130 exit(1);
133 /* Get address, size, and element size of the UDP port table */
134 param = "&udp_port_table,$udp_port_table,$udp_port_table[0]";
135 psize = strlen(param);
136 r= write(fd, param, psize);
137 if (r != psize)
139 fprintf(stderr, "%s: write to %s failed: %s\n", prog_name,
140 ipstat_device,
141 r < 0 ? strerror(errno) : "short write");
142 exit(1);
144 r= read(fd, values, sizeof(values));
145 if (r < 0)
147 fprintf(stderr, "%s: read from %s failed: %s\n", prog_name,
148 ipstat_device, strerror(errno));
149 exit(1);
151 pval= values;
152 if (paramvalue(&pval, &udp_port_tablep, sizeof(udp_port_tablep)) !=
153 sizeof(udp_port_tablep) ||
154 paramvalue(&pval, &udp_port_table_s, sizeof(udp_port_table_s))
155 != sizeof(udp_port_table_s) ||
156 paramvalue(&pval, &udp_port_table_rs, sizeof(udp_port_table_rs))
157 != sizeof(udp_port_table_rs))
159 fprintf(stderr,
160 "%s: unable to decode the results from queryparam (&udp_port_table, ...)\n",
161 prog_name);
162 exit(1);
165 if (udp_port_table_rs != sizeof(udp_port_table[0]))
167 fprintf(stderr,
168 "%s: size mismatch in udp_port_table (different version of inet?)\n",
169 prog_name);
170 exit(1);
172 udp_port_nr= udp_port_table_s/udp_port_table_rs;
173 assert(udp_port_table_s == udp_port_nr*udp_port_table_rs);
174 udp_port_table= malloc(udp_port_table_s);
175 if (!udp_port_table)
177 fprintf(stderr,
178 "%s: unable to malloc %u bytes for udp_port_table\n",
179 prog_name, udp_port_table_s);
180 exit(1);
182 v_size= 2*udp_port_table_s+1;
183 valuesl= realloc(valuesl, v_size);
184 if (!valuesl)
186 fprintf(stderr, "%s: unable to malloc %u bytes for valuesl\n",
187 prog_name, v_size);
188 exit(1);
191 param = "udp_port_table";
192 psize = strlen(param);
193 r= write(fd, param, psize);
194 if (r != psize)
196 fprintf(stderr, "%s: write to %s failed: %s\n", prog_name,
197 ipstat_device,
198 r < 0 ? strerror(errno) : "short write");
199 exit(1);
201 r= read(fd, valuesl, v_size);
202 if (r < 0)
204 fprintf(stderr, "%s: read from %s failed: %s\n", prog_name,
205 ipstat_device, strerror(errno));
206 exit(1);
208 pval= valuesl;
209 if (paramvalue(&pval, udp_port_table, udp_port_table_s) !=
210 udp_port_table_s)
212 fprintf(stderr,
213 "%s: unable to decode the results from queryparam (udp_port_table)\n",
214 prog_name);
215 exit(1);
218 /* Get the uptime in clock ticks. */
219 #ifdef __minix_vmd
220 if (sysutime(UTIME_UPTIME, &uptime) == -1)
222 fprintf(stderr, "%s: sysutime failed: %s\n", prog_name,
223 strerror(errno));
224 exit(1);
226 now= uptime.tv_sec * HZ + (uptime.tv_usec*HZ/1000000);
227 #else /* Minix 3 */
228 now= times(&tmsbuf);
229 #endif
231 for (i= 0; i<UDP_FD_NR; i++)
232 print_fd(i, now);
233 exit(0);
236 void print_fd(int i, clock_t now)
238 unsigned long nwuo_flags;
239 int port_nr;
240 udp_fd_t *udp_fd;
241 udp_port_t *udp_port;
242 char *locaddr_str, *remaddr_str;
243 struct hostent *hostent;
244 struct servent *servent;
245 nwio_udpopt_t uf_udpopt;
247 udp_fd= &udp_fd_table[i];
248 if (!(udp_fd->uf_flags & UFF_INUSE))
249 return;
250 uf_udpopt= udp_fd->uf_udpopt;
251 nwuo_flags= uf_udpopt.nwuo_flags;
252 if (((nwuo_flags & NWUO_LOCPORT_MASK) != NWUO_LP_SET) && !inclSel)
253 return;
255 port_nr= udp_fd->uf_port-udp_port_tablep;
256 udp_port= &udp_port_table[port_nr];
258 printf("%3d", i);
260 if (nwuo_flags & NWUO_EN_LOC)
262 if (!numerical && (hostent=
263 gethostbyaddr((char *)&udp_port->up_ipaddr,
264 sizeof(ipaddr_t), AF_INET)) != NULL)
266 locaddr_str= hostent->h_name;
268 else
269 locaddr_str=
270 inet_ntoa(*(struct in_addr *)&udp_port->up_ipaddr);
272 else if (nwuo_flags & NWUO_EN_BROAD)
273 locaddr_str= "255.255.255.255";
274 else
275 locaddr_str= "0.0.0.0";
277 printf(" %s:", locaddr_str);
279 if ((nwuo_flags & NWUO_LOCPORT_MASK) != NWUO_LP_SEL &&
280 (nwuo_flags & NWUO_LOCPORT_MASK) != NWUO_LP_SET)
282 printf("*");
284 else if ((servent= getservbyport(uf_udpopt.nwuo_locport, "udp")) !=
285 NULL)
287 printf("%s", servent->s_name);
289 else
290 printf("%u", ntohs(uf_udpopt.nwuo_locport));
292 printf(" -> ");
294 if (!(nwuo_flags & NWUO_RA_SET))
295 remaddr_str= "*";
296 else if (!numerical &&
297 (hostent= gethostbyaddr((char *)&uf_udpopt.nwuo_remaddr,
298 sizeof(ipaddr_t), AF_INET)) != NULL)
300 remaddr_str= hostent->h_name;
302 else
303 remaddr_str=
304 inet_ntoa(*(struct in_addr *)&uf_udpopt.nwuo_remaddr);
305 printf("%s:", remaddr_str);
307 if (!(nwuo_flags & NWUO_RP_SET))
308 printf("*");
309 else if ((servent= getservbyport(uf_udpopt.nwuo_remport, "udp")) !=
310 NULL)
312 printf("%s", servent->s_name);
314 else
315 printf("%u", ntohs(uf_udpopt.nwuo_remport));
316 printf("\n");
319 void usage(void)
321 fprintf(stderr, "Usage: %s [-a] [-n]\n", prog_name);
322 exit(1);
326 * $PchId: udpstat.c,v 1.4 2005/01/30 01:04:57 philip Exp $