forget difference between big and small commands - obsolete with vm.
[minix.git] / commands / simple / tcpstat.c
blob0afc85233c858d56b8e4f999f1d77f076ab437c2
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>
28 #include <minix/sysinfo.h>
30 #include <inet/generic/buf.h>
31 #include <inet/generic/clock.h>
32 #include <inet/generic/event.h>
33 #include <inet/generic/type.h>
34 #include <inet/generic/tcp.h>
35 #include <inet/generic/tcp_int.h>
37 u32_t system_hz;
38 char *prog_name;
39 tcp_conn_t tcp_conn_table[TCP_CONN_NR];
40 char values[2 * sizeof(tcp_conn_table) + 1];
41 int inclListen, numerical, verbose;
43 void print_conn(int i, clock_t now);
44 void usage(void);
46 int main(int argc, char*argv[])
48 char *ipstat_device;
49 int fd, i, r;
50 char *query, *pval;
51 size_t len;
52 struct timeval uptime;
53 clock_t now;
54 int fl;
55 int a_flag, n_flag, v_flag;
56 struct tms tmsbuf;
58 getsysinfo_up(PM_PROC_NR, SIU_SYSTEMHZ, sizeof(system_hz), &system_hz);
60 (prog_name=strrchr(argv[0], '/')) ? prog_name++ : (prog_name=argv[0]);
62 a_flag= 0;
63 n_flag= 0;
64 v_flag= 0;
65 while ((fl= getopt(argc, argv, "?anv")) != -1)
67 switch(fl)
69 case '?':
70 usage();
71 case 'a':
72 a_flag= 1;
73 break;
74 case 'n':
75 n_flag= 1;
76 break;
77 case 'v':
78 v_flag= 1;
79 break;
80 default:
81 fprintf(stderr, "%s: getopt failed: '%c'\n",
82 prog_name, fl);
83 exit(1);
86 inclListen= !!a_flag;
87 numerical= !!n_flag;
88 verbose= !!v_flag;
90 ipstat_device= IPSTAT_DEVICE;
91 if ((fd= open(ipstat_device, O_RDWR)) == -1)
93 fprintf(stderr, "%s: unable to open '%s': %s\n", prog_name,
94 ipstat_device, strerror(errno));
95 exit(1);
98 query= "tcp_conn_table";
99 len= strlen(query);
100 r= write(fd, query, len);
101 if (r != len)
103 fprintf(stderr, "%s: write to %s failed: %s\n",
104 prog_name, ipstat_device, r < 0 ? strerror(errno) :
105 "short write");
106 exit(1);
108 r= read(fd, values, sizeof(values));
109 if (r == -1)
111 fprintf(stderr, "%s: read from %s failed: %s\n", prog_name,
112 ipstat_device, strerror(errno));
113 exit(1);
115 pval= values;
116 if (paramvalue(&pval, tcp_conn_table, sizeof(tcp_conn_table)) !=
117 sizeof(tcp_conn_table))
119 fprintf(stderr,
120 "%s: unable to decode the results from queryparam\n",
121 prog_name);
122 exit(1);
125 #ifdef __minix_vmd
126 /* Get the uptime in clock ticks. */
127 if (sysutime(UTIME_UPTIME, &uptime) == -1)
129 fprintf(stderr, "%s: sysutime failed: %s\n", prog_name,
130 strerror(errno));
131 exit(1);
133 now= uptime.tv_sec * HZ + (uptime.tv_usec*HZ/1000000);
134 #else /* Minix 3 */
135 now= times(&tmsbuf);
136 #endif
138 for (i= 0; i<TCP_CONN_NR; i++)
139 print_conn(i, now);
140 exit(0);
143 void print_conn(int i, clock_t now)
145 tcp_conn_t *tcp_conn;
146 char *addr_str;
147 struct hostent *hostent;
148 struct servent *servent;
149 ipaddr_t a1, a2;
150 tcpport_t p1, p2;
151 unsigned flags;
152 int no_verbose;
153 clock_t rtt, artt, drtt;
155 tcp_conn= &tcp_conn_table[i];
156 if (!(tcp_conn->tc_flags & TCF_INUSE))
157 return;
158 if (tcp_conn->tc_state == TCS_LISTEN && !inclListen)
159 return;
160 if (tcp_conn->tc_state == TCS_CLOSED && tcp_conn->tc_fd == NULL &&
161 tcp_conn->tc_senddis < now)
163 return;
166 printf("%3d", i);
168 a1= tcp_conn->tc_locaddr;
169 p1= tcp_conn->tc_locport;
170 a2= tcp_conn->tc_remaddr;
171 p2= tcp_conn->tc_remport;
173 if (a1 == 0)
174 addr_str= "*";
175 else if (!numerical &&
176 (hostent= gethostbyaddr((char *)&a1,
177 sizeof(a1), AF_INET)) != NULL)
179 addr_str= hostent->h_name;
181 else
182 addr_str= inet_ntoa(a1);
183 printf(" %s:", addr_str);
185 if (p1 == 0)
186 printf("*");
187 else if ((servent= getservbyport(p1, "tcp")) != NULL)
189 printf("%s", servent->s_name);
191 else
192 printf("%u", ntohs(p1));
194 if (tcp_conn->tc_orglisten)
195 printf(" <- ");
196 else
197 printf(" -> ");
199 if (a2 == 0)
200 addr_str= "*";
201 else if (!numerical &&
202 (hostent= gethostbyaddr((char *)&a2,
203 sizeof(a2), AF_INET)) != NULL)
205 addr_str= hostent->h_name;
207 else
208 addr_str= inet_ntoa(a2);
209 printf("%s:", addr_str);
211 if (p2 == 0)
212 printf("*");
213 else if ((servent= getservbyport(p2, "tcp")) !=
214 NULL)
216 printf("%s", servent->s_name);
218 else
219 printf("%u", ntohs(p2));
221 printf(" ");
222 no_verbose= 0;
223 switch(tcp_conn->tc_state)
225 case TCS_CLOSED: printf("CLOSED");
226 if (tcp_conn->tc_senddis >= now)
228 printf("(time wait %ld s)",
229 (tcp_conn->tc_senddis-now)/system_hz);
231 no_verbose= 1;
232 break;
233 case TCS_LISTEN: printf("LISTEN"); no_verbose= 1; break;
234 case TCS_SYN_RECEIVED: printf("SYN_RECEIVED"); break;
235 case TCS_SYN_SENT: printf("SYN_SENT"); break;
236 case TCS_ESTABLISHED: printf("ESTABLISHED"); break;
237 case TCS_CLOSING: printf("CLOSING"); break;
238 default: printf("state(%d)", tcp_conn->tc_state);
239 break;
242 if (tcp_conn->tc_flags & TCF_FIN_RECV)
243 printf(" F<");
244 if (tcp_conn->tc_flags & TCF_FIN_SENT)
246 printf(" F>");
247 if (tcp_conn->tc_SND_UNA == tcp_conn->tc_SND_NXT)
248 printf("+");
250 if (tcp_conn->tc_state != TCS_CLOSED &&
251 tcp_conn->tc_state != TCS_LISTEN)
253 printf("\n\t");
254 printf("RQ: %lu, SQ: %lu, RWnd: %u, SWnd: %lu, SWThresh: %lu",
255 tcp_conn->tc_RCV_NXT - tcp_conn->tc_RCV_LO,
256 tcp_conn->tc_SND_NXT - tcp_conn->tc_SND_UNA,
257 tcp_conn->tc_rcv_wnd,
258 tcp_conn->tc_snd_cwnd - tcp_conn->tc_SND_UNA,
259 tcp_conn->tc_snd_cthresh);
262 printf("\n");
264 if (!verbose || no_verbose)
265 return;
266 rtt= tcp_conn->tc_rtt;
267 artt= tcp_conn->tc_artt;
268 drtt= tcp_conn->tc_drtt;
269 printf("\tmss %u, mtu %u%s, rtt %.3f (%.3f+%d*%.3f) s\n",
270 tcp_conn->tc_max_mtu-IP_TCP_MIN_HDR_SIZE,
271 tcp_conn->tc_mtu,
272 (tcp_conn->tc_flags & TCF_PMTU) ? "" : " (no PMTU)",
273 rtt/(system_hz+0.0),
274 artt/(system_hz+0.0)/TCP_RTT_SCALE, TCP_DRTT_MULT,
275 drtt/(system_hz+0.0)/TCP_RTT_SCALE);
276 flags= tcp_conn->tc_flags;
277 printf("\tflags:");
278 if (!flags)
279 printf(" TCF_EMPTY");
280 if (flags & TCF_INUSE)
281 flags &= ~TCF_INUSE;
282 if (flags & TCF_FIN_RECV)
284 printf(" TCF_FIN_RECV");
285 flags &= ~TCF_FIN_RECV;
287 if (flags & TCF_RCV_PUSH)
289 printf(" TCF_RCV_PUSH");
290 flags &= ~TCF_RCV_PUSH;
292 if (flags & TCF_MORE2WRITE)
294 printf(" TCF_MORE2WRITE");
295 flags &= ~TCF_MORE2WRITE;
297 if (flags & TCF_SEND_ACK)
299 printf(" TCF_SEND_ACK");
300 flags &= ~TCF_SEND_ACK;
302 if (flags & TCF_FIN_SENT)
304 printf(" TCF_FIN_SENT");
305 flags &= ~TCF_FIN_SENT;
307 if (flags & TCF_BSD_URG)
309 printf(" TCF_BSD_URG");
310 flags &= ~TCF_BSD_URG;
312 if (flags & TCF_NO_PUSH)
314 printf(" TCF_NO_PUSH");
315 flags &= ~TCF_NO_PUSH;
317 if (flags & TCF_PUSH_NOW)
319 printf(" TCF_PUSH_NOW");
320 flags &= ~TCF_PUSH_NOW;
322 if (flags & TCF_PMTU)
323 flags &= ~TCF_PMTU;
324 if (flags)
325 printf(" 0x%x", flags);
326 printf("\n");
327 printf("\ttimer: ref %d, time %f, active %d\n",
328 tcp_conn->tc_transmit_timer.tim_ref,
329 (0.0+tcp_conn->tc_transmit_timer.tim_time-now)/system_hz,
330 tcp_conn->tc_transmit_timer.tim_active);
333 void usage(void)
335 fprintf(stderr, "Usage: %s [-anv]\n", prog_name);
336 exit(1);
340 * $PchId: tcpstat.c,v 1.8 2005/01/30 01:04:38 philip Exp $