mkfs, mkproto: minor improvements
[minix.git] / commands / tcpstat / tcpstat.c
blobe43f2c2111f221a6f604b89902fc85df35d61c7f
1 /*
2 tcpstat.c
4 Created: June 1995 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 <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 #include <minix/com.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/tcp.h>
34 #include <inet/generic/tcp_int.h>
36 u32_t system_hz;
37 char *prog_name;
38 tcp_conn_t tcp_conn_table[TCP_CONN_NR];
39 char values[2 * sizeof(tcp_conn_table) + 1];
40 int inclListen, numerical, verbose;
42 void print_conn(int i, clock_t now);
43 void usage(void);
45 int main(int argc, char*argv[])
47 char *ipstat_device;
48 int fd, i, r;
49 char *query, *pval;
50 size_t len;
51 struct timeval uptime;
52 clock_t now;
53 int fl;
54 int a_flag, n_flag, v_flag;
55 struct tms tmsbuf;
57 system_hz = (u32_t) sysconf(_SC_CLK_TCK);
59 (prog_name=strrchr(argv[0], '/')) ? prog_name++ : (prog_name=argv[0]);
61 a_flag= 0;
62 n_flag= 0;
63 v_flag= 0;
64 while ((fl= getopt(argc, argv, "?anv")) != -1)
66 switch(fl)
68 case '?':
69 usage();
70 case 'a':
71 a_flag= 1;
72 break;
73 case 'n':
74 n_flag= 1;
75 break;
76 case 'v':
77 v_flag= 1;
78 break;
79 default:
80 fprintf(stderr, "%s: getopt failed: '%c'\n",
81 prog_name, fl);
82 exit(1);
85 inclListen= !!a_flag;
86 numerical= !!n_flag;
87 verbose= !!v_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 query= "tcp_conn_table";
98 len= strlen(query);
99 r= write(fd, query, len);
100 if (r != len)
102 fprintf(stderr, "%s: write to %s failed: %s\n",
103 prog_name, ipstat_device, r < 0 ? strerror(errno) :
104 "short write");
105 exit(1);
107 r= read(fd, values, sizeof(values));
108 if (r == -1)
110 fprintf(stderr, "%s: read from %s failed: %s\n", prog_name,
111 ipstat_device, strerror(errno));
112 exit(1);
114 pval= values;
115 if (paramvalue(&pval, tcp_conn_table, sizeof(tcp_conn_table)) !=
116 sizeof(tcp_conn_table))
118 fprintf(stderr,
119 "%s: unable to decode the results from queryparam\n",
120 prog_name);
121 exit(1);
124 #ifdef __minix_vmd
125 /* Get the uptime in clock ticks. */
126 if (sysutime(UTIME_UPTIME, &uptime) == -1)
128 fprintf(stderr, "%s: sysutime failed: %s\n", prog_name,
129 strerror(errno));
130 exit(1);
132 now= uptime.tv_sec * HZ + (uptime.tv_usec*HZ/1000000);
133 #else /* Minix 3 */
134 now= times(&tmsbuf);
135 #endif
137 for (i= 0; i<TCP_CONN_NR; i++)
138 print_conn(i, now);
139 exit(0);
142 void print_conn(int i, clock_t now)
144 tcp_conn_t *tcp_conn;
145 char *addr_str;
146 struct hostent *hostent;
147 struct servent *servent;
148 ipaddr_t a1, a2;
149 tcpport_t p1, p2;
150 unsigned flags;
151 int no_verbose;
152 clock_t rtt, artt, drtt;
154 tcp_conn= &tcp_conn_table[i];
155 if (!(tcp_conn->tc_flags & TCF_INUSE))
156 return;
157 if (tcp_conn->tc_state == TCS_LISTEN && !inclListen)
158 return;
159 if (tcp_conn->tc_state == TCS_CLOSED && tcp_conn->tc_fd == NULL &&
160 tcp_conn->tc_senddis < now)
162 return;
165 printf("%3d", i);
167 a1= tcp_conn->tc_locaddr;
168 p1= tcp_conn->tc_locport;
169 a2= tcp_conn->tc_remaddr;
170 p2= tcp_conn->tc_remport;
172 if (a1 == 0)
173 addr_str= "*";
174 else if (!numerical &&
175 (hostent= gethostbyaddr((char *)&a1,
176 sizeof(a1), AF_INET)) != NULL)
178 addr_str= hostent->h_name;
180 else
181 addr_str= inet_ntoa(a1);
182 printf(" %s:", addr_str);
184 if (p1 == 0)
185 printf("*");
186 else if ((servent= getservbyport(p1, "tcp")) != NULL)
188 printf("%s", servent->s_name);
190 else
191 printf("%u", ntohs(p1));
193 if (tcp_conn->tc_orglisten)
194 printf(" <- ");
195 else
196 printf(" -> ");
198 if (a2 == 0)
199 addr_str= "*";
200 else if (!numerical &&
201 (hostent= gethostbyaddr((char *)&a2,
202 sizeof(a2), AF_INET)) != NULL)
204 addr_str= hostent->h_name;
206 else
207 addr_str= inet_ntoa(a2);
208 printf("%s:", addr_str);
210 if (p2 == 0)
211 printf("*");
212 else if ((servent= getservbyport(p2, "tcp")) !=
213 NULL)
215 printf("%s", servent->s_name);
217 else
218 printf("%u", ntohs(p2));
220 printf(" ");
221 no_verbose= 0;
222 switch(tcp_conn->tc_state)
224 case TCS_CLOSED: printf("CLOSED");
225 if (tcp_conn->tc_senddis >= now)
227 printf("(time wait %ld s)",
228 (tcp_conn->tc_senddis-now)/system_hz);
230 no_verbose= 1;
231 break;
232 case TCS_LISTEN: printf("LISTEN"); no_verbose= 1; break;
233 case TCS_SYN_RECEIVED: printf("SYN_RECEIVED"); break;
234 case TCS_SYN_SENT: printf("SYN_SENT"); break;
235 case TCS_ESTABLISHED: printf("ESTABLISHED"); break;
236 case TCS_CLOSING: printf("CLOSING"); break;
237 default: printf("state(%d)", tcp_conn->tc_state);
238 break;
241 if (tcp_conn->tc_flags & TCF_FIN_RECV)
242 printf(" F<");
243 if (tcp_conn->tc_flags & TCF_FIN_SENT)
245 printf(" F>");
246 if (tcp_conn->tc_SND_UNA == tcp_conn->tc_SND_NXT)
247 printf("+");
249 if (tcp_conn->tc_state != TCS_CLOSED &&
250 tcp_conn->tc_state != TCS_LISTEN)
252 printf("\n\t");
253 printf("RQ: %lu, SQ: %lu, RWnd: %u, SWnd: %lu, SWThresh: %lu",
254 tcp_conn->tc_RCV_NXT - tcp_conn->tc_RCV_LO,
255 tcp_conn->tc_SND_NXT - tcp_conn->tc_SND_UNA,
256 tcp_conn->tc_rcv_wnd,
257 tcp_conn->tc_snd_cwnd - tcp_conn->tc_SND_UNA,
258 tcp_conn->tc_snd_cthresh);
261 printf("\n");
263 if (!verbose || no_verbose)
264 return;
265 rtt= tcp_conn->tc_rtt;
266 artt= tcp_conn->tc_artt;
267 drtt= tcp_conn->tc_drtt;
268 printf("\tmss %u, mtu %u%s, rtt %.3f (%.3f+%d*%.3f) s\n",
269 tcp_conn->tc_max_mtu-IP_TCP_MIN_HDR_SIZE,
270 tcp_conn->tc_mtu,
271 (tcp_conn->tc_flags & TCF_PMTU) ? "" : " (no PMTU)",
272 rtt/(system_hz+0.0),
273 artt/(system_hz+0.0)/TCP_RTT_SCALE, TCP_DRTT_MULT,
274 drtt/(system_hz+0.0)/TCP_RTT_SCALE);
275 flags= tcp_conn->tc_flags;
276 printf("\tflags:");
277 if (!flags)
278 printf(" TCF_EMPTY");
279 if (flags & TCF_INUSE)
280 flags &= ~TCF_INUSE;
281 if (flags & TCF_FIN_RECV)
283 printf(" TCF_FIN_RECV");
284 flags &= ~TCF_FIN_RECV;
286 if (flags & TCF_RCV_PUSH)
288 printf(" TCF_RCV_PUSH");
289 flags &= ~TCF_RCV_PUSH;
291 if (flags & TCF_MORE2WRITE)
293 printf(" TCF_MORE2WRITE");
294 flags &= ~TCF_MORE2WRITE;
296 if (flags & TCF_SEND_ACK)
298 printf(" TCF_SEND_ACK");
299 flags &= ~TCF_SEND_ACK;
301 if (flags & TCF_FIN_SENT)
303 printf(" TCF_FIN_SENT");
304 flags &= ~TCF_FIN_SENT;
306 if (flags & TCF_BSD_URG)
308 printf(" TCF_BSD_URG");
309 flags &= ~TCF_BSD_URG;
311 if (flags & TCF_NO_PUSH)
313 printf(" TCF_NO_PUSH");
314 flags &= ~TCF_NO_PUSH;
316 if (flags & TCF_PUSH_NOW)
318 printf(" TCF_PUSH_NOW");
319 flags &= ~TCF_PUSH_NOW;
321 if (flags & TCF_PMTU)
322 flags &= ~TCF_PMTU;
323 if (flags)
324 printf(" 0x%x", flags);
325 printf("\n");
326 printf("\ttimer: ref %d, time %f, active %d\n",
327 tcp_conn->tc_transmit_timer.tim_ref,
328 (0.0+tcp_conn->tc_transmit_timer.tim_time-now)/system_hz,
329 tcp_conn->tc_transmit_timer.tim_active);
332 void usage(void)
334 fprintf(stderr, "Usage: %s [-anv]\n", prog_name);
335 exit(1);
339 * $PchId: tcpstat.c,v 1.8 2005/01/30 01:04:38 philip Exp $