PageRenderTime 27ms CodeModel.GetById 34ms RepoModel.GetById 1ms app.codeStats 0ms

/commands/tcpstat/tcpstat.c

https://bitbucket.org/gthummalapalle/project3
C | 342 lines | 304 code | 29 blank | 9 comment | 62 complexity | d1a1e0a6b27fbf304ea75a6a5f716a35 MD5 | raw file
  1. /*
  2. tcpstat.c
  3. Created: June 1995 by Philip Homburg <philip@f-mnx.phicoh.com>
  4. */
  5. #define _POSIX_C_SOURCE 2
  6. #define _NETBSD_SOURCE 1
  7. #include <inet/inet.h>
  8. #undef printf
  9. #undef send
  10. #include <fcntl.h>
  11. #include <stdio.h>
  12. #include <time.h>
  13. #include <unistd.h>
  14. #include <sys/svrctl.h>
  15. #ifndef __minix_vmd
  16. #include <sys/times.h>
  17. #endif
  18. #include <net/netlib.h>
  19. #include <net/gen/inet.h>
  20. #include <net/gen/netdb.h>
  21. #include <net/gen/socket.h>
  22. #include <minix/queryparam.h>
  23. #include <minix/com.h>
  24. #include <inet/generic/buf.h>
  25. #include <inet/generic/clock.h>
  26. #include <inet/generic/event.h>
  27. #include <inet/generic/type.h>
  28. #include <inet/generic/tcp.h>
  29. #include <inet/generic/tcp_int.h>
  30. u32_t system_hz;
  31. char *prog_name;
  32. tcp_conn_t tcp_conn_table[TCP_CONN_NR];
  33. char values[2 * sizeof(tcp_conn_table) + 1];
  34. int inclListen, numerical, verbose;
  35. void print_conn(int i, clock_t now);
  36. void usage(void);
  37. int main(int argc, char*argv[])
  38. {
  39. char *ipstat_device;
  40. int fd, i, r;
  41. char *query, *pval;
  42. size_t len;
  43. #ifdef __minix_vmd
  44. struct timeval uptime;
  45. #endif
  46. clock_t now;
  47. int fl;
  48. int a_flag, n_flag, v_flag;
  49. struct tms tmsbuf;
  50. system_hz = (u32_t) sysconf(_SC_CLK_TCK);
  51. (prog_name=strrchr(argv[0], '/')) ? prog_name++ : (prog_name=argv[0]);
  52. a_flag= 0;
  53. n_flag= 0;
  54. v_flag= 0;
  55. while ((fl= getopt(argc, argv, "?anv")) != -1)
  56. {
  57. switch(fl)
  58. {
  59. case '?':
  60. usage();
  61. case 'a':
  62. a_flag= 1;
  63. break;
  64. case 'n':
  65. n_flag= 1;
  66. break;
  67. case 'v':
  68. v_flag= 1;
  69. break;
  70. default:
  71. fprintf(stderr, "%s: getopt failed: '%c'\n",
  72. prog_name, fl);
  73. exit(1);
  74. }
  75. }
  76. inclListen= !!a_flag;
  77. numerical= !!n_flag;
  78. verbose= !!v_flag;
  79. ipstat_device= IPSTAT_DEVICE;
  80. if ((fd= open(ipstat_device, O_RDWR)) == -1)
  81. {
  82. fprintf(stderr, "%s: unable to open '%s': %s\n", prog_name,
  83. ipstat_device, strerror(errno));
  84. exit(1);
  85. }
  86. query= "tcp_conn_table";
  87. len= strlen(query);
  88. r= write(fd, query, len);
  89. if (r != len)
  90. {
  91. fprintf(stderr, "%s: write to %s failed: %s\n",
  92. prog_name, ipstat_device, r < 0 ? strerror(errno) :
  93. "short write");
  94. exit(1);
  95. }
  96. r= read(fd, values, sizeof(values));
  97. if (r == -1)
  98. {
  99. fprintf(stderr, "%s: read from %s failed: %s\n", prog_name,
  100. ipstat_device, strerror(errno));
  101. exit(1);
  102. }
  103. pval= values;
  104. if (paramvalue(&pval, tcp_conn_table, sizeof(tcp_conn_table)) !=
  105. sizeof(tcp_conn_table))
  106. {
  107. fprintf(stderr,
  108. "%s: unable to decode the results from queryparam\n",
  109. prog_name);
  110. exit(1);
  111. }
  112. #ifdef __minix_vmd
  113. /* Get the uptime in clock ticks. */
  114. if (sysutime(UTIME_UPTIME, &uptime) == -1)
  115. {
  116. fprintf(stderr, "%s: sysutime failed: %s\n", prog_name,
  117. strerror(errno));
  118. exit(1);
  119. }
  120. now= uptime.tv_sec * HZ + (uptime.tv_usec*HZ/1000000);
  121. #else /* Minix 3 */
  122. now= times(&tmsbuf);
  123. #endif
  124. for (i= 0; i<TCP_CONN_NR; i++)
  125. print_conn(i, now);
  126. exit(0);
  127. }
  128. void print_conn(int i, clock_t now)
  129. {
  130. tcp_conn_t *tcp_conn;
  131. char *addr_str;
  132. struct hostent *hostent;
  133. struct servent *servent;
  134. ipaddr_t a1, a2;
  135. tcpport_t p1, p2;
  136. unsigned flags;
  137. int no_verbose;
  138. clock_t rtt, artt, drtt;
  139. tcp_conn= &tcp_conn_table[i];
  140. if (!(tcp_conn->tc_flags & TCF_INUSE))
  141. return;
  142. if (tcp_conn->tc_state == TCS_LISTEN && !inclListen)
  143. return;
  144. if (tcp_conn->tc_state == TCS_CLOSED && tcp_conn->tc_fd == NULL &&
  145. tcp_conn->tc_senddis < now)
  146. {
  147. return;
  148. }
  149. printf("%3d", i);
  150. a1= tcp_conn->tc_locaddr;
  151. p1= tcp_conn->tc_locport;
  152. a2= tcp_conn->tc_remaddr;
  153. p2= tcp_conn->tc_remport;
  154. if (a1 == 0)
  155. addr_str= "*";
  156. else if (!numerical &&
  157. (hostent= gethostbyaddr((char *)&a1,
  158. sizeof(a1), AF_INET)) != NULL)
  159. {
  160. addr_str= hostent->h_name;
  161. }
  162. else
  163. addr_str= inet_ntoa(a1);
  164. printf(" %s:", addr_str);
  165. if (p1 == 0)
  166. printf("*");
  167. else if ((servent= getservbyport(p1, "tcp")) != NULL)
  168. {
  169. printf("%s", servent->s_name);
  170. }
  171. else
  172. printf("%u", ntohs(p1));
  173. if (tcp_conn->tc_orglisten)
  174. printf(" <- ");
  175. else
  176. printf(" -> ");
  177. if (a2 == 0)
  178. addr_str= "*";
  179. else if (!numerical &&
  180. (hostent= gethostbyaddr((char *)&a2,
  181. sizeof(a2), AF_INET)) != NULL)
  182. {
  183. addr_str= hostent->h_name;
  184. }
  185. else
  186. addr_str= inet_ntoa(a2);
  187. printf("%s:", addr_str);
  188. if (p2 == 0)
  189. printf("*");
  190. else if ((servent= getservbyport(p2, "tcp")) !=
  191. NULL)
  192. {
  193. printf("%s", servent->s_name);
  194. }
  195. else
  196. printf("%u", ntohs(p2));
  197. printf(" ");
  198. no_verbose= 0;
  199. switch(tcp_conn->tc_state)
  200. {
  201. case TCS_CLOSED: printf("CLOSED");
  202. if (tcp_conn->tc_senddis >= now)
  203. {
  204. printf("(time wait %d s)",
  205. (tcp_conn->tc_senddis-now)/system_hz);
  206. }
  207. no_verbose= 1;
  208. break;
  209. case TCS_LISTEN: printf("LISTEN"); no_verbose= 1; break;
  210. case TCS_SYN_RECEIVED: printf("SYN_RECEIVED"); break;
  211. case TCS_SYN_SENT: printf("SYN_SENT"); break;
  212. case TCS_ESTABLISHED: printf("ESTABLISHED"); break;
  213. case TCS_CLOSING: printf("CLOSING"); break;
  214. default: printf("state(%d)", tcp_conn->tc_state);
  215. break;
  216. }
  217. if (tcp_conn->tc_flags & TCF_FIN_RECV)
  218. printf(" F<");
  219. if (tcp_conn->tc_flags & TCF_FIN_SENT)
  220. {
  221. printf(" F>");
  222. if (tcp_conn->tc_SND_UNA == tcp_conn->tc_SND_NXT)
  223. printf("+");
  224. }
  225. if (tcp_conn->tc_state != TCS_CLOSED &&
  226. tcp_conn->tc_state != TCS_LISTEN)
  227. {
  228. printf("\n\t");
  229. printf("RQ: %u, SQ: %u, RWnd: %u, SWnd: %u, SWThresh: %u",
  230. tcp_conn->tc_RCV_NXT - tcp_conn->tc_RCV_LO,
  231. tcp_conn->tc_SND_NXT - tcp_conn->tc_SND_UNA,
  232. tcp_conn->tc_rcv_wnd,
  233. tcp_conn->tc_snd_cwnd - tcp_conn->tc_SND_UNA,
  234. tcp_conn->tc_snd_cthresh);
  235. }
  236. printf("\n");
  237. if (!verbose || no_verbose)
  238. return;
  239. rtt= tcp_conn->tc_rtt;
  240. artt= tcp_conn->tc_artt;
  241. drtt= tcp_conn->tc_drtt;
  242. printf("\tmss %u, mtu %u%s, rtt %.3f (%.3f+%d*%.3f) s\n",
  243. tcp_conn->tc_max_mtu-IP_TCP_MIN_HDR_SIZE,
  244. tcp_conn->tc_mtu,
  245. (tcp_conn->tc_flags & TCF_PMTU) ? "" : " (no PMTU)",
  246. rtt/(system_hz+0.0),
  247. artt/(system_hz+0.0)/TCP_RTT_SCALE, TCP_DRTT_MULT,
  248. drtt/(system_hz+0.0)/TCP_RTT_SCALE);
  249. flags= tcp_conn->tc_flags;
  250. printf("\tflags:");
  251. if (!flags)
  252. printf(" TCF_EMPTY");
  253. if (flags & TCF_INUSE)
  254. flags &= ~TCF_INUSE;
  255. if (flags & TCF_FIN_RECV)
  256. {
  257. printf(" TCF_FIN_RECV");
  258. flags &= ~TCF_FIN_RECV;
  259. }
  260. if (flags & TCF_RCV_PUSH)
  261. {
  262. printf(" TCF_RCV_PUSH");
  263. flags &= ~TCF_RCV_PUSH;
  264. }
  265. if (flags & TCF_MORE2WRITE)
  266. {
  267. printf(" TCF_MORE2WRITE");
  268. flags &= ~TCF_MORE2WRITE;
  269. }
  270. if (flags & TCF_SEND_ACK)
  271. {
  272. printf(" TCF_SEND_ACK");
  273. flags &= ~TCF_SEND_ACK;
  274. }
  275. if (flags & TCF_FIN_SENT)
  276. {
  277. printf(" TCF_FIN_SENT");
  278. flags &= ~TCF_FIN_SENT;
  279. }
  280. if (flags & TCF_BSD_URG)
  281. {
  282. printf(" TCF_BSD_URG");
  283. flags &= ~TCF_BSD_URG;
  284. }
  285. if (flags & TCF_NO_PUSH)
  286. {
  287. printf(" TCF_NO_PUSH");
  288. flags &= ~TCF_NO_PUSH;
  289. }
  290. if (flags & TCF_PUSH_NOW)
  291. {
  292. printf(" TCF_PUSH_NOW");
  293. flags &= ~TCF_PUSH_NOW;
  294. }
  295. if (flags & TCF_PMTU)
  296. flags &= ~TCF_PMTU;
  297. if (flags)
  298. printf(" 0x%x", flags);
  299. printf("\n");
  300. printf("\ttimer: ref %d, time %f, active %d\n",
  301. tcp_conn->tc_transmit_timer.tim_ref,
  302. (0.0+tcp_conn->tc_transmit_timer.tim_time-now)/system_hz,
  303. tcp_conn->tc_transmit_timer.tim_active);
  304. }
  305. void usage(void)
  306. {
  307. fprintf(stderr, "Usage: %s [-anv]\n", prog_name);
  308. exit(1);
  309. }
  310. /*
  311. * $PchId: tcpstat.c,v 1.8 2005/01/30 01:04:38 philip Exp $
  312. */