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
[])
57 struct timeval uptime
;
64 (prog_name
=strrchr(argv
[0], '/')) ? prog_name
++ : (prog_name
=argv
[0]);
68 while ((fl
= getopt(argc
, argv
, "?an")) != -1)
81 fprintf(stderr
, "%s: getopt failed: '%c'\n",
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
));
97 v_size
= 2*sizeof(udp_fd_table
)+1;
98 valuesl
= realloc(valuesl
, v_size
);
101 fprintf(stderr
, "%s: unable to malloc %u bytes for valuesl\n",
106 param
= "udp_fd_table";
107 psize
= strlen(param
);
108 r
= write(fd
, param
, psize
);
111 fprintf(stderr
, "%s: write to %s failed: %s\n", prog_name
,
113 r
< 0 ? strerror(errno
) : "short write");
116 r
= read(fd
, valuesl
, v_size
);
119 fprintf(stderr
, "%s: read from %s failed: %s\n", prog_name
,
120 ipstat_device
, strerror(errno
));
124 if (paramvalue(&pval
, udp_fd_table
, sizeof(udp_fd_table
)) !=
125 sizeof(udp_fd_table
))
128 "%s: unable to decode the results from queryparam (udp_fd_table)\n",
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
);
139 fprintf(stderr
, "%s: write to %s failed: %s\n", prog_name
,
141 r
< 0 ? strerror(errno
) : "short write");
144 r
= read(fd
, values
, sizeof(values
));
147 fprintf(stderr
, "%s: read from %s failed: %s\n", prog_name
,
148 ipstat_device
, strerror(errno
));
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
))
160 "%s: unable to decode the results from queryparam (&udp_port_table, ...)\n",
165 if (udp_port_table_rs
!= sizeof(udp_port_table
[0]))
168 "%s: size mismatch in udp_port_table (different version of inet?)\n",
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
);
178 "%s: unable to malloc %u bytes for udp_port_table\n",
179 prog_name
, udp_port_table_s
);
182 v_size
= 2*udp_port_table_s
+1;
183 valuesl
= realloc(valuesl
, v_size
);
186 fprintf(stderr
, "%s: unable to malloc %u bytes for valuesl\n",
191 param
= "udp_port_table";
192 psize
= strlen(param
);
193 r
= write(fd
, param
, psize
);
196 fprintf(stderr
, "%s: write to %s failed: %s\n", prog_name
,
198 r
< 0 ? strerror(errno
) : "short write");
201 r
= read(fd
, valuesl
, v_size
);
204 fprintf(stderr
, "%s: read from %s failed: %s\n", prog_name
,
205 ipstat_device
, strerror(errno
));
209 if (paramvalue(&pval
, udp_port_table
, udp_port_table_s
) !=
213 "%s: unable to decode the results from queryparam (udp_port_table)\n",
218 /* Get the uptime in clock ticks. */
220 if (sysutime(UTIME_UPTIME
, &uptime
) == -1)
222 fprintf(stderr
, "%s: sysutime failed: %s\n", prog_name
,
226 now
= uptime
.tv_sec
* HZ
+ (uptime
.tv_usec
*HZ
/1000000);
231 for (i
= 0; i
<UDP_FD_NR
; i
++)
236 void print_fd(int i
, clock_t now
)
238 unsigned long nwuo_flags
;
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
))
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
)
255 port_nr
= udp_fd
->uf_port
-udp_port_tablep
;
256 udp_port
= &udp_port_table
[port_nr
];
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
;
269 locaddr_str
= inet_ntoa(udp_port
->up_ipaddr
);
271 else if (nwuo_flags
& NWUO_EN_BROAD
)
272 locaddr_str
= "255.255.255.255";
274 locaddr_str
= "0.0.0.0";
276 printf(" %s:", locaddr_str
);
278 if ((nwuo_flags
& NWUO_LOCPORT_MASK
) != NWUO_LP_SEL
&&
279 (nwuo_flags
& NWUO_LOCPORT_MASK
) != NWUO_LP_SET
)
283 else if ((servent
= getservbyport(uf_udpopt
.nwuo_locport
, "udp")) !=
286 printf("%s", servent
->s_name
);
289 printf("%u", ntohs(uf_udpopt
.nwuo_locport
));
293 if (!(nwuo_flags
& NWUO_RA_SET
))
295 else if (!numerical
&&
296 (hostent
= gethostbyaddr((char *)&uf_udpopt
.nwuo_remaddr
,
297 sizeof(ipaddr_t
), AF_INET
)) != NULL
)
299 remaddr_str
= hostent
->h_name
;
302 remaddr_str
= inet_ntoa(uf_udpopt
.nwuo_remaddr
);
303 printf("%s:", remaddr_str
);
305 if (!(nwuo_flags
& NWUO_RP_SET
))
307 else if ((servent
= getservbyport(uf_udpopt
.nwuo_remport
, "udp")) !=
310 printf("%s", servent
->s_name
);
313 printf("%u", ntohs(uf_udpopt
.nwuo_remport
));
319 fprintf(stderr
, "Usage: %s [-a] [-n]\n", prog_name
);
324 * $PchId: udpstat.c,v 1.4 2005/01/30 01:04:57 philip Exp $