unstack, sort: cleanup and improvement
[minix.git] / commands / udpstat / udpstat.c
blobde26c12824607610b9af7b29ed416b7dc729c2d0
1 /*
2 udpstat.c
4 Created: March 2001 by Philip Homburg <philip@f-mnx.phicoh.com>
5 */
7 #define _MINIX_SOURCE
8 #define _POSIX_C_SOURCE 2
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 #include <sys/svrctl.h>
20 #ifndef __minix_vmd
21 #include <sys/times.h>
22 #endif
23 #include <net/netlib.h>
24 #include <net/gen/inet.h>
25 #include <net/gen/netdb.h>
26 #include <net/gen/socket.h>
27 #include <minix/queryparam.h>
29 #include <inet/generic/buf.h>
30 #include <inet/generic/clock.h>
31 #include <inet/generic/event.h>
32 #include <inet/generic/type.h>
33 #include <inet/generic/udp_int.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 struct timeval uptime;
57 clock_t now;
58 int fl;
59 int a_flag, n_flag;
60 struct tms tmsbuf;
62 (prog_name=strrchr(argv[0], '/')) ? prog_name++ : (prog_name=argv[0]);
64 a_flag= 0;
65 n_flag= 0;
66 while ((fl= getopt(argc, argv, "?an")) != -1)
68 switch(fl)
70 case '?':
71 usage();
72 case 'a':
73 a_flag= 1;
74 break;
75 case 'n':
76 n_flag= 1;
77 break;
78 default:
79 fprintf(stderr, "%s: getopt failed: '%c'\n",
80 prog_name, fl);
81 exit(1);
84 inclSel= !!a_flag;
85 numerical= !!n_flag;
87 ipstat_device= IPSTAT_DEVICE;
88 if ((fd= open(ipstat_device, O_RDWR)) == -1)
90 fprintf(stderr, "%s: unable to open '%s': %s\n", prog_name,
91 ipstat_device, strerror(errno));
92 exit(1);
95 v_size= 2*sizeof(udp_fd_table)+1;
96 valuesl= realloc(valuesl, v_size);
97 if (!valuesl)
99 fprintf(stderr, "%s: unable to malloc %u bytes for valuesl\n",
100 prog_name, v_size);
101 exit(1);
104 param= "udp_fd_table";
105 psize= strlen(param);
106 r= write(fd, param, psize);
107 if (r != psize)
109 fprintf(stderr, "%s: write to %s failed: %s\n", prog_name,
110 ipstat_device,
111 r < 0 ? strerror(errno) : "short write");
112 exit(1);
114 r= read(fd, valuesl, v_size);
115 if (r < 0)
117 fprintf(stderr, "%s: read from %s failed: %s\n", prog_name,
118 ipstat_device, strerror(errno));
119 exit(1);
121 pval= valuesl;
122 if (paramvalue(&pval, udp_fd_table, sizeof(udp_fd_table)) !=
123 sizeof(udp_fd_table))
125 fprintf(stderr,
126 "%s: unable to decode the results from queryparam (udp_fd_table)\n",
127 prog_name);
128 exit(1);
131 /* Get address, size, and element size of the UDP port table */
132 param = "&udp_port_table,$udp_port_table,$udp_port_table[0]";
133 psize = strlen(param);
134 r= write(fd, param, psize);
135 if (r != psize)
137 fprintf(stderr, "%s: write to %s failed: %s\n", prog_name,
138 ipstat_device,
139 r < 0 ? strerror(errno) : "short write");
140 exit(1);
142 r= read(fd, values, sizeof(values));
143 if (r < 0)
145 fprintf(stderr, "%s: read from %s failed: %s\n", prog_name,
146 ipstat_device, strerror(errno));
147 exit(1);
149 pval= values;
150 if (paramvalue(&pval, &udp_port_tablep, sizeof(udp_port_tablep)) !=
151 sizeof(udp_port_tablep) ||
152 paramvalue(&pval, &udp_port_table_s, sizeof(udp_port_table_s))
153 != sizeof(udp_port_table_s) ||
154 paramvalue(&pval, &udp_port_table_rs, sizeof(udp_port_table_rs))
155 != sizeof(udp_port_table_rs))
157 fprintf(stderr,
158 "%s: unable to decode the results from queryparam (&udp_port_table, ...)\n",
159 prog_name);
160 exit(1);
163 if (udp_port_table_rs != sizeof(udp_port_table[0]))
165 fprintf(stderr,
166 "%s: size mismatch in udp_port_table (different version of inet?)\n",
167 prog_name);
168 exit(1);
170 udp_port_nr= udp_port_table_s/udp_port_table_rs;
171 assert(udp_port_table_s == udp_port_nr*udp_port_table_rs);
172 udp_port_table= malloc(udp_port_table_s);
173 if (!udp_port_table)
175 fprintf(stderr,
176 "%s: unable to malloc %u bytes for udp_port_table\n",
177 prog_name, udp_port_table_s);
178 exit(1);
180 v_size= 2*udp_port_table_s+1;
181 valuesl= realloc(valuesl, v_size);
182 if (!valuesl)
184 fprintf(stderr, "%s: unable to malloc %u bytes for valuesl\n",
185 prog_name, v_size);
186 exit(1);
189 param = "udp_port_table";
190 psize = strlen(param);
191 r= write(fd, param, psize);
192 if (r != psize)
194 fprintf(stderr, "%s: write to %s failed: %s\n", prog_name,
195 ipstat_device,
196 r < 0 ? strerror(errno) : "short write");
197 exit(1);
199 r= read(fd, valuesl, v_size);
200 if (r < 0)
202 fprintf(stderr, "%s: read from %s failed: %s\n", prog_name,
203 ipstat_device, strerror(errno));
204 exit(1);
206 pval= valuesl;
207 if (paramvalue(&pval, udp_port_table, udp_port_table_s) !=
208 udp_port_table_s)
210 fprintf(stderr,
211 "%s: unable to decode the results from queryparam (udp_port_table)\n",
212 prog_name);
213 exit(1);
216 /* Get the uptime in clock ticks. */
217 #ifdef __minix_vmd
218 if (sysutime(UTIME_UPTIME, &uptime) == -1)
220 fprintf(stderr, "%s: sysutime failed: %s\n", prog_name,
221 strerror(errno));
222 exit(1);
224 now= uptime.tv_sec * HZ + (uptime.tv_usec*HZ/1000000);
225 #else /* Minix 3 */
226 now= times(&tmsbuf);
227 #endif
229 for (i= 0; i<UDP_FD_NR; i++)
230 print_fd(i, now);
231 exit(0);
234 void print_fd(int i, clock_t now)
236 unsigned long nwuo_flags;
237 int port_nr;
238 udp_fd_t *udp_fd;
239 udp_port_t *udp_port;
240 char *locaddr_str, *remaddr_str;
241 struct hostent *hostent;
242 struct servent *servent;
243 nwio_udpopt_t uf_udpopt;
245 udp_fd= &udp_fd_table[i];
246 if (!(udp_fd->uf_flags & UFF_INUSE))
247 return;
248 uf_udpopt= udp_fd->uf_udpopt;
249 nwuo_flags= uf_udpopt.nwuo_flags;
250 if (((nwuo_flags & NWUO_LOCPORT_MASK) != NWUO_LP_SET) && !inclSel)
251 return;
253 port_nr= udp_fd->uf_port-udp_port_tablep;
254 udp_port= &udp_port_table[port_nr];
256 printf("%3d", i);
258 if (nwuo_flags & NWUO_EN_LOC)
260 if (!numerical && (hostent=
261 gethostbyaddr((char *)&udp_port->up_ipaddr,
262 sizeof(ipaddr_t), AF_INET)) != NULL)
264 locaddr_str= hostent->h_name;
266 else
267 locaddr_str= inet_ntoa(udp_port->up_ipaddr);
269 else if (nwuo_flags & NWUO_EN_BROAD)
270 locaddr_str= "255.255.255.255";
271 else
272 locaddr_str= "0.0.0.0";
274 printf(" %s:", locaddr_str);
276 if ((nwuo_flags & NWUO_LOCPORT_MASK) != NWUO_LP_SEL &&
277 (nwuo_flags & NWUO_LOCPORT_MASK) != NWUO_LP_SET)
279 printf("*");
281 else if ((servent= getservbyport(uf_udpopt.nwuo_locport, "udp")) !=
282 NULL)
284 printf("%s", servent->s_name);
286 else
287 printf("%u", ntohs(uf_udpopt.nwuo_locport));
289 printf(" -> ");
291 if (!(nwuo_flags & NWUO_RA_SET))
292 remaddr_str= "*";
293 else if (!numerical &&
294 (hostent= gethostbyaddr((char *)&uf_udpopt.nwuo_remaddr,
295 sizeof(ipaddr_t), AF_INET)) != NULL)
297 remaddr_str= hostent->h_name;
299 else
300 remaddr_str= inet_ntoa(uf_udpopt.nwuo_remaddr);
301 printf("%s:", remaddr_str);
303 if (!(nwuo_flags & NWUO_RP_SET))
304 printf("*");
305 else if ((servent= getservbyport(uf_udpopt.nwuo_remport, "udp")) !=
306 NULL)
308 printf("%s", servent->s_name);
310 else
311 printf("%u", ntohs(uf_udpopt.nwuo_remport));
312 printf("\n");
315 void usage(void)
317 fprintf(stderr, "Usage: %s [-a] [-n]\n", prog_name);
318 exit(1);
322 * $PchId: udpstat.c,v 1.4 2005/01/30 01:04:57 philip Exp $