4 Created: March 2001 by Philip Homburg <philip@f-mnx.phicoh.com>
8 #define _POSIX_C_SOURCE 2
10 #include <inet/inet.h>
19 #include <sys/svrctl.h>
21 #include <sys/times.h>
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>
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
;
42 char values
[6 * sizeof(void *) + 3];
45 int inclSel
, numerical
;
47 void print_fd(int i
, clock_t now
);
50 int main(int argc
, char*argv
[])
56 struct timeval uptime
;
62 (prog_name
=strrchr(argv
[0], '/')) ? prog_name
++ : (prog_name
=argv
[0]);
66 while ((fl
= getopt(argc
, argv
, "?an")) != -1)
79 fprintf(stderr
, "%s: getopt failed: '%c'\n",
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
));
95 v_size
= 2*sizeof(udp_fd_table
)+1;
96 valuesl
= realloc(valuesl
, v_size
);
99 fprintf(stderr
, "%s: unable to malloc %u bytes for valuesl\n",
104 param
= "udp_fd_table";
105 psize
= strlen(param
);
106 r
= write(fd
, param
, psize
);
109 fprintf(stderr
, "%s: write to %s failed: %s\n", prog_name
,
111 r
< 0 ? strerror(errno
) : "short write");
114 r
= read(fd
, valuesl
, v_size
);
117 fprintf(stderr
, "%s: read from %s failed: %s\n", prog_name
,
118 ipstat_device
, strerror(errno
));
122 if (paramvalue(&pval
, udp_fd_table
, sizeof(udp_fd_table
)) !=
123 sizeof(udp_fd_table
))
126 "%s: unable to decode the results from queryparam (udp_fd_table)\n",
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
);
137 fprintf(stderr
, "%s: write to %s failed: %s\n", prog_name
,
139 r
< 0 ? strerror(errno
) : "short write");
142 r
= read(fd
, values
, sizeof(values
));
145 fprintf(stderr
, "%s: read from %s failed: %s\n", prog_name
,
146 ipstat_device
, strerror(errno
));
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
))
158 "%s: unable to decode the results from queryparam (&udp_port_table, ...)\n",
163 if (udp_port_table_rs
!= sizeof(udp_port_table
[0]))
166 "%s: size mismatch in udp_port_table (different version of inet?)\n",
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
);
176 "%s: unable to malloc %u bytes for udp_port_table\n",
177 prog_name
, udp_port_table_s
);
180 v_size
= 2*udp_port_table_s
+1;
181 valuesl
= realloc(valuesl
, v_size
);
184 fprintf(stderr
, "%s: unable to malloc %u bytes for valuesl\n",
189 param
= "udp_port_table";
190 psize
= strlen(param
);
191 r
= write(fd
, param
, psize
);
194 fprintf(stderr
, "%s: write to %s failed: %s\n", prog_name
,
196 r
< 0 ? strerror(errno
) : "short write");
199 r
= read(fd
, valuesl
, v_size
);
202 fprintf(stderr
, "%s: read from %s failed: %s\n", prog_name
,
203 ipstat_device
, strerror(errno
));
207 if (paramvalue(&pval
, udp_port_table
, udp_port_table_s
) !=
211 "%s: unable to decode the results from queryparam (udp_port_table)\n",
216 /* Get the uptime in clock ticks. */
218 if (sysutime(UTIME_UPTIME
, &uptime
) == -1)
220 fprintf(stderr
, "%s: sysutime failed: %s\n", prog_name
,
224 now
= uptime
.tv_sec
* HZ
+ (uptime
.tv_usec
*HZ
/1000000);
229 for (i
= 0; i
<UDP_FD_NR
; i
++)
234 void print_fd(int i
, clock_t now
)
236 unsigned long nwuo_flags
;
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
))
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
)
253 port_nr
= udp_fd
->uf_port
-udp_port_tablep
;
254 udp_port
= &udp_port_table
[port_nr
];
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
;
267 locaddr_str
= inet_ntoa(udp_port
->up_ipaddr
);
269 else if (nwuo_flags
& NWUO_EN_BROAD
)
270 locaddr_str
= "255.255.255.255";
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
)
281 else if ((servent
= getservbyport(uf_udpopt
.nwuo_locport
, "udp")) !=
284 printf("%s", servent
->s_name
);
287 printf("%u", ntohs(uf_udpopt
.nwuo_locport
));
291 if (!(nwuo_flags
& NWUO_RA_SET
))
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
;
300 remaddr_str
= inet_ntoa(uf_udpopt
.nwuo_remaddr
);
301 printf("%s:", remaddr_str
);
303 if (!(nwuo_flags
& NWUO_RP_SET
))
305 else if ((servent
= getservbyport(uf_udpopt
.nwuo_remport
, "udp")) !=
308 printf("%s", servent
->s_name
);
311 printf("%u", ntohs(uf_udpopt
.nwuo_remport
));
317 fprintf(stderr
, "Usage: %s [-a] [-n]\n", prog_name
);
322 * $PchId: udpstat.c,v 1.4 2005/01/30 01:04:57 philip Exp $