tools/llvm: Do not build with symbols
[minix3.git] / minix / commands / tcpstat / tcpstat.c
blobf5200a4564eae51da3593d50fae7b6e6373a7b41
1 /*
2 tcpstat.c
4 Created: June 1995 by Philip Homburg <philip@f-mnx.phicoh.com>
5 */
7 #define _POSIX_C_SOURCE 2
8 #define _NETBSD_SOURCE 1
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 <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 #ifdef __minix_vmd
52 struct timeval uptime;
53 #endif
54 clock_t now;
55 int fl;
56 int a_flag, n_flag, v_flag;
57 struct tms tmsbuf;
59 system_hz = (u32_t) sysconf(_SC_CLK_TCK);
61 (prog_name=strrchr(argv[0], '/')) ? prog_name++ : (prog_name=argv[0]);
63 a_flag= 0;
64 n_flag= 0;
65 v_flag= 0;
66 while ((fl= getopt(argc, argv, "?anv")) != -1)
68 switch(fl)
70 case '?':
71 usage();
72 case 'a':
73 a_flag= 1;
74 break;
75 case 'n':
76 n_flag= 1;
77 break;
78 case 'v':
79 v_flag= 1;
80 break;
81 default:
82 fprintf(stderr, "%s: getopt failed: '%c'\n",
83 prog_name, fl);
84 exit(1);
87 inclListen= !!a_flag;
88 numerical= !!n_flag;
89 verbose= !!v_flag;
91 ipstat_device= IPSTAT_DEVICE;
92 if ((fd= open(ipstat_device, O_RDWR)) == -1)
94 fprintf(stderr, "%s: unable to open '%s': %s\n", prog_name,
95 ipstat_device, strerror(errno));
96 exit(1);
99 query= "tcp_conn_table";
100 len= strlen(query);
101 r= write(fd, query, len);
102 if (r != len)
104 fprintf(stderr, "%s: write to %s failed: %s\n",
105 prog_name, ipstat_device, r < 0 ? strerror(errno) :
106 "short write");
107 exit(1);
109 r= read(fd, values, sizeof(values));
110 if (r == -1)
112 fprintf(stderr, "%s: read from %s failed: %s\n", prog_name,
113 ipstat_device, strerror(errno));
114 exit(1);
116 pval= values;
117 if (paramvalue(&pval, tcp_conn_table, sizeof(tcp_conn_table)) !=
118 sizeof(tcp_conn_table))
120 fprintf(stderr,
121 "%s: unable to decode the results from queryparam\n",
122 prog_name);
123 exit(1);
126 #ifdef __minix_vmd
127 /* Get the uptime in clock ticks. */
128 if (sysutime(UTIME_UPTIME, &uptime) == -1)
130 fprintf(stderr, "%s: sysutime failed: %s\n", prog_name,
131 strerror(errno));
132 exit(1);
134 now= uptime.tv_sec * HZ + (uptime.tv_usec*HZ/1000000);
135 #else /* Minix 3 */
136 now= times(&tmsbuf);
137 #endif
139 for (i= 0; i<TCP_CONN_NR; i++)
140 print_conn(i, now);
141 exit(0);
144 void print_conn(int i, clock_t now)
146 tcp_conn_t *tcp_conn;
147 char *addr_str;
148 struct hostent *hostent;
149 struct servent *servent;
150 ipaddr_t a1, a2;
151 tcpport_t p1, p2;
152 unsigned flags;
153 int no_verbose;
154 clock_t rtt, artt, drtt;
156 tcp_conn= &tcp_conn_table[i];
157 if (!(tcp_conn->tc_flags & TCF_INUSE))
158 return;
159 if (tcp_conn->tc_state == TCS_LISTEN && !inclListen)
160 return;
161 if (tcp_conn->tc_state == TCS_CLOSED && tcp_conn->tc_fd == NULL &&
162 tcp_conn->tc_senddis < now)
164 return;
167 printf("%3d", i);
169 a1= tcp_conn->tc_locaddr;
170 p1= tcp_conn->tc_locport;
171 a2= tcp_conn->tc_remaddr;
172 p2= tcp_conn->tc_remport;
174 if (a1 == 0)
175 addr_str= "*";
176 else if (!numerical &&
177 (hostent= gethostbyaddr((char *)&a1,
178 sizeof(a1), AF_INET)) != NULL)
180 addr_str= hostent->h_name;
182 else
183 addr_str= inet_ntoa(a1);
184 printf(" %s:", addr_str);
186 if (p1 == 0)
187 printf("*");
188 else if ((servent= getservbyport(p1, "tcp")) != NULL)
190 printf("%s", servent->s_name);
192 else
193 printf("%u", ntohs(p1));
195 if (tcp_conn->tc_orglisten)
196 printf(" <- ");
197 else
198 printf(" -> ");
200 if (a2 == 0)
201 addr_str= "*";
202 else if (!numerical &&
203 (hostent= gethostbyaddr((char *)&a2,
204 sizeof(a2), AF_INET)) != NULL)
206 addr_str= hostent->h_name;
208 else
209 addr_str= inet_ntoa(a2);
210 printf("%s:", addr_str);
212 if (p2 == 0)
213 printf("*");
214 else if ((servent= getservbyport(p2, "tcp")) !=
215 NULL)
217 printf("%s", servent->s_name);
219 else
220 printf("%u", ntohs(p2));
222 printf(" ");
223 no_verbose= 0;
224 switch(tcp_conn->tc_state)
226 case TCS_CLOSED: printf("CLOSED");
227 if (tcp_conn->tc_senddis >= now)
229 printf("(time wait %d s)",
230 (tcp_conn->tc_senddis-now)/system_hz);
232 no_verbose= 1;
233 break;
234 case TCS_LISTEN: printf("LISTEN"); no_verbose= 1; break;
235 case TCS_SYN_RECEIVED: printf("SYN_RECEIVED"); break;
236 case TCS_SYN_SENT: printf("SYN_SENT"); break;
237 case TCS_ESTABLISHED: printf("ESTABLISHED"); break;
238 case TCS_CLOSING: printf("CLOSING"); break;
239 default: printf("state(%d)", tcp_conn->tc_state);
240 break;
243 if (tcp_conn->tc_flags & TCF_FIN_RECV)
244 printf(" F<");
245 if (tcp_conn->tc_flags & TCF_FIN_SENT)
247 printf(" F>");
248 if (tcp_conn->tc_SND_UNA == tcp_conn->tc_SND_NXT)
249 printf("+");
251 if (tcp_conn->tc_state != TCS_CLOSED &&
252 tcp_conn->tc_state != TCS_LISTEN)
254 printf("\n\t");
255 printf("RQ: %u, SQ: %u, RWnd: %u, SWnd: %u, SWThresh: %u",
256 tcp_conn->tc_RCV_NXT - tcp_conn->tc_RCV_LO,
257 tcp_conn->tc_SND_NXT - tcp_conn->tc_SND_UNA,
258 tcp_conn->tc_rcv_wnd,
259 tcp_conn->tc_snd_cwnd - tcp_conn->tc_SND_UNA,
260 tcp_conn->tc_snd_cthresh);
263 printf("\n");
265 if (!verbose || no_verbose)
266 return;
267 rtt= tcp_conn->tc_rtt;
268 artt= tcp_conn->tc_artt;
269 drtt= tcp_conn->tc_drtt;
270 printf("\tmss %u, mtu %u%s, rtt %.3f (%.3f+%d*%.3f) s\n",
271 tcp_conn->tc_max_mtu-IP_TCP_MIN_HDR_SIZE,
272 tcp_conn->tc_mtu,
273 (tcp_conn->tc_flags & TCF_PMTU) ? "" : " (no PMTU)",
274 rtt/(system_hz+0.0),
275 artt/(system_hz+0.0)/TCP_RTT_SCALE, TCP_DRTT_MULT,
276 drtt/(system_hz+0.0)/TCP_RTT_SCALE);
277 flags= tcp_conn->tc_flags;
278 printf("\tflags:");
279 if (!flags)
280 printf(" TCF_EMPTY");
281 if (flags & TCF_INUSE)
282 flags &= ~TCF_INUSE;
283 if (flags & TCF_FIN_RECV)
285 printf(" TCF_FIN_RECV");
286 flags &= ~TCF_FIN_RECV;
288 if (flags & TCF_RCV_PUSH)
290 printf(" TCF_RCV_PUSH");
291 flags &= ~TCF_RCV_PUSH;
293 if (flags & TCF_MORE2WRITE)
295 printf(" TCF_MORE2WRITE");
296 flags &= ~TCF_MORE2WRITE;
298 if (flags & TCF_SEND_ACK)
300 printf(" TCF_SEND_ACK");
301 flags &= ~TCF_SEND_ACK;
303 if (flags & TCF_FIN_SENT)
305 printf(" TCF_FIN_SENT");
306 flags &= ~TCF_FIN_SENT;
308 if (flags & TCF_BSD_URG)
310 printf(" TCF_BSD_URG");
311 flags &= ~TCF_BSD_URG;
313 if (flags & TCF_NO_PUSH)
315 printf(" TCF_NO_PUSH");
316 flags &= ~TCF_NO_PUSH;
318 if (flags & TCF_PUSH_NOW)
320 printf(" TCF_PUSH_NOW");
321 flags &= ~TCF_PUSH_NOW;
323 if (flags & TCF_PMTU)
324 flags &= ~TCF_PMTU;
325 if (flags)
326 printf(" 0x%x", flags);
327 printf("\n");
328 printf("\ttimer: ref %d, time %f, active %d\n",
329 tcp_conn->tc_transmit_timer.tim_ref,
330 (0.0+tcp_conn->tc_transmit_timer.tim_time-now)/system_hz,
331 tcp_conn->tc_transmit_timer.tim_active);
334 void usage(void)
336 fprintf(stderr, "Usage: %s [-anv]\n", prog_name);
337 exit(1);
341 * $PchId: tcpstat.c,v 1.8 2005/01/30 01:04:38 philip Exp $