source: trunk/minix/commands/simple/udpstat.c@ 10

Last change on this file since 10 was 9, checked in by Mattia Monga, 13 years ago

Minix 3.1.2a

File size: 6.5 KB
Line 
1/*
2udpstat.c
3
4Created: March 2001 by Philip Homburg <philip@f-mnx.phicoh.com>
5*/
6
7#define _MINIX_SOURCE
8#define _POSIX_C_SOURCE 2
9
10#include <inet/inet.h>
11#undef printf
12
13#include <assert.h>
14#include <fcntl.h>
15#include <stdio.h>
16#include <time.h>
17#include <unistd.h>
18#include <sys/svrctl.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 <net/gen/netdb.h>
25#include <net/gen/socket.h>
26#include <minix/queryparam.h>
27
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
34char *prog_name;
35udp_fd_t udp_fd_table[UDP_FD_NR];
36udp_port_t *udp_port_table;
37udp_port_t *udp_port_tablep;
38size_t udp_port_table_s;
39size_t udp_port_table_rs;
40int udp_port_nr;
41char values[6 * sizeof(void *) + 3];
42char *valuesl= NULL;
43size_t v_size;
44int inclSel, numerical;
45
46void print_fd(int i, clock_t now);
47void usage(void);
48
49int main(int argc, char*argv[])
50{
51 char *udp_device;
52 int fd, i;
53 struct svrqueryparam qpar;
54 char *pval;
55 struct timeval uptime;
56 clock_t now;
57 int fl;
58 int a_flag, n_flag;
59
60 (prog_name=strrchr(argv[0], '/')) ? prog_name++ : (prog_name=argv[0]);
61
62 a_flag= 0;
63 n_flag= 0;
64 while ((fl= getopt(argc, argv, "?an")) != -1)
65 {
66 switch(fl)
67 {
68 case '?':
69 usage();
70 case 'a':
71 a_flag= 1;
72 break;
73 case 'n':
74 n_flag= 1;
75 break;
76 default:
77 fprintf(stderr, "%s: getopt failed: '%c'\n",
78 prog_name, fl);
79 exit(1);
80 }
81 }
82 inclSel= !!a_flag;
83 numerical= !!n_flag;
84
85 udp_device= UDP_DEVICE;
86 if ((fd= open(udp_device, O_RDWR)) == -1)
87 {
88 fprintf(stderr, "%s: unable to open '%s': %s\n", prog_name,
89 udp_device, strerror(errno));
90 exit(1);
91 }
92
93 v_size= 2*sizeof(udp_fd_table)+1;
94 valuesl= realloc(valuesl, v_size);
95 if (!valuesl)
96 {
97 fprintf(stderr, "%s: unable to malloc %u bytes for valuesl\n",
98 prog_name, v_size);
99 exit(1);
100 }
101
102 qpar.param = "udp_fd_table";
103 qpar.psize = strlen(qpar.param);
104 qpar.value = valuesl;
105 qpar.vsize = v_size;
106 if (ioctl(fd, NWIOQUERYPARAM, &qpar) == -1)
107 {
108 fprintf(stderr, "%s: queryparam failed: %s\n", prog_name,
109 strerror(errno));
110 exit(1);
111 }
112 pval= valuesl;
113 if (paramvalue(&pval, udp_fd_table, sizeof(udp_fd_table)) !=
114 sizeof(udp_fd_table))
115 {
116 fprintf(stderr,
117 "%s: unable to decode the results from queryparam (udp_fd_table)\n",
118 prog_name);
119 exit(1);
120 }
121
122 /* Get address, size, and element size of the UDP port table */
123 qpar.param = "&udp_port_table,$udp_port_table,$udp_port_table[0]";
124 qpar.psize = strlen(qpar.param);
125 qpar.value = values;
126 qpar.vsize = sizeof(values);
127 if (ioctl(fd, NWIOQUERYPARAM, &qpar) == -1)
128 {
129 fprintf(stderr, "%s: queryparam failed: %s\n", prog_name,
130 strerror(errno));
131 exit(1);
132 }
133 pval= values;
134 if (paramvalue(&pval, &udp_port_tablep, sizeof(udp_port_tablep)) !=
135 sizeof(udp_port_tablep) ||
136 paramvalue(&pval, &udp_port_table_s, sizeof(udp_port_table_s))
137 != sizeof(udp_port_table_s) ||
138 paramvalue(&pval, &udp_port_table_rs, sizeof(udp_port_table_rs))
139 != sizeof(udp_port_table_rs))
140 {
141 fprintf(stderr,
142"%s: unable to decode the results from queryparam (&udp_port_table, ...)\n",
143 prog_name);
144 exit(1);
145 }
146
147 if (udp_port_table_rs != sizeof(udp_port_table[0]))
148 {
149 fprintf(stderr,
150 "%s: size mismatch in udp_port_table (different version of inet?)\n",
151 prog_name);
152 exit(1);
153 }
154 udp_port_nr= udp_port_table_s/udp_port_table_rs;
155 assert(udp_port_table_s == udp_port_nr*udp_port_table_rs);
156 udp_port_table= malloc(udp_port_table_s);
157 if (!udp_port_table)
158 {
159 fprintf(stderr,
160 "%s: unable to malloc %u bytes for udp_port_table\n",
161 prog_name, udp_port_table_s);
162 exit(1);
163 }
164 v_size= 2*udp_port_table_s+1;
165 valuesl= realloc(valuesl, v_size);
166 if (!valuesl)
167 {
168 fprintf(stderr, "%s: unable to malloc %u bytes for valuesl\n",
169 prog_name, v_size);
170 exit(1);
171 }
172
173 qpar.param = "udp_port_table";
174 qpar.psize = strlen(qpar.param);
175 qpar.value = valuesl;
176 qpar.vsize = v_size;
177 if (ioctl(fd, NWIOQUERYPARAM, &qpar) == -1)
178 {
179 fprintf(stderr, "%s: queryparam failed: %s\n", prog_name,
180 strerror(errno));
181 exit(1);
182 }
183 pval= valuesl;
184 if (paramvalue(&pval, udp_port_table, udp_port_table_s) !=
185 udp_port_table_s)
186 {
187 fprintf(stderr,
188 "%s: unable to decode the results from queryparam (udp_port_table)\n",
189 prog_name);
190 exit(1);
191 }
192
193 /* Get the uptime in clock ticks. */
194#ifdef __minix_vmd
195 if (sysutime(UTIME_UPTIME, &uptime) == -1)
196 {
197 fprintf(stderr, "%s: sysutime failed: %s\n", prog_name,
198 strerror(errno));
199 exit(1);
200 }
201 now= uptime.tv_sec * HZ + (uptime.tv_usec*HZ/1000000);
202#else /* Minix 3 */
203 now= times(NULL);
204#endif
205
206 for (i= 0; i<UDP_FD_NR; i++)
207 print_fd(i, now);
208 exit(0);
209}
210
211void print_fd(int i, clock_t now)
212{
213 unsigned long nwuo_flags;
214 int port_nr;
215 udp_fd_t *udp_fd;
216 udp_port_t *udp_port;
217 char *locaddr_str, *remaddr_str;
218 struct hostent *hostent;
219 struct servent *servent;
220 nwio_udpopt_t uf_udpopt;
221
222 udp_fd= &udp_fd_table[i];
223 if (!(udp_fd->uf_flags & UFF_INUSE))
224 return;
225 uf_udpopt= udp_fd->uf_udpopt;
226 nwuo_flags= uf_udpopt.nwuo_flags;
227 if (((nwuo_flags & NWUO_LOCPORT_MASK) != NWUO_LP_SET) && !inclSel)
228 return;
229
230 port_nr= udp_fd->uf_port-udp_port_tablep;
231 udp_port= &udp_port_table[port_nr];
232
233 printf("%3d", i);
234
235 if (nwuo_flags & NWUO_EN_LOC)
236 {
237 if (!numerical && (hostent=
238 gethostbyaddr((char *)&udp_port->up_ipaddr,
239 sizeof(ipaddr_t), AF_INET)) != NULL)
240 {
241 locaddr_str= hostent->h_name;
242 }
243 else
244 locaddr_str= inet_ntoa(udp_port->up_ipaddr);
245 }
246 else if (nwuo_flags & NWUO_EN_BROAD)
247 locaddr_str= "255.255.255.255";
248 else
249 locaddr_str= "0.0.0.0";
250
251 printf(" %s:", locaddr_str);
252
253 if ((nwuo_flags & NWUO_LOCPORT_MASK) != NWUO_LP_SEL &&
254 (nwuo_flags & NWUO_LOCPORT_MASK) != NWUO_LP_SET)
255 {
256 printf("*");
257 }
258 else if ((servent= getservbyport(uf_udpopt.nwuo_locport, "udp")) !=
259 NULL)
260 {
261 printf("%s", servent->s_name);
262 }
263 else
264 printf("%u", ntohs(uf_udpopt.nwuo_locport));
265
266 printf(" -> ");
267
268 if (!(nwuo_flags & NWUO_RA_SET))
269 remaddr_str= "*";
270 else if (!numerical &&
271 (hostent= gethostbyaddr((char *)&uf_udpopt.nwuo_remaddr,
272 sizeof(ipaddr_t), AF_INET)) != NULL)
273 {
274 remaddr_str= hostent->h_name;
275 }
276 else
277 remaddr_str= inet_ntoa(uf_udpopt.nwuo_remaddr);
278 printf("%s:", remaddr_str);
279
280 if (!(nwuo_flags & NWUO_RP_SET))
281 printf("*");
282 else if ((servent= getservbyport(uf_udpopt.nwuo_remport, "udp")) !=
283 NULL)
284 {
285 printf("%s", servent->s_name);
286 }
287 else
288 printf("%u", ntohs(uf_udpopt.nwuo_remport));
289 printf("\n");
290}
291
292void usage(void)
293{
294 fprintf(stderr, "Usage: %s [-a] [-n]\n", prog_name);
295 exit(1);
296}
297
298/*
299 * $PchId: udpstat.c,v 1.4 2005/01/30 01:04:57 philip Exp $
300 */
Note: See TracBrowser for help on using the repository browser.