Jackdaw additions
[contiki-2.x.git] / apps / shell / shell-netstat.c
blob1fbb715cbfe8ca1a02c2a121abe4f4888c7efe67
1 /*
2 * Copyright (c) 2004, Adam Dunkels.
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the Institute nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
29 * This file is part of the Contiki operating system.
31 * Author: Adam Dunkels <adam@sics.se>
33 * $Id: shell-netstat.c,v 1.1 2009/05/10 21:02:24 adamdunkels Exp $
36 #include <string.h>
37 #include <stddef.h>
38 #include <stdio.h>
40 #include "contiki.h"
41 #include "shell.h"
42 #include "contiki-net.h"
44 static const char closed[] = /* "CLOSED",*/
45 {0x43, 0x4c, 0x4f, 0x53, 0x45, 0x44, 0};
46 static const char syn_rcvd[] = /* "SYN-RCVD",*/
47 {0x53, 0x59, 0x4e, 0x2d, 0x52, 0x43, 0x56,
48 0x44, 0};
49 static const char syn_sent[] = /* "SYN-SENT",*/
50 {0x53, 0x59, 0x4e, 0x2d, 0x53, 0x45, 0x4e,
51 0x54, 0};
52 static const char established[] = /* "ESTABLISHED",*/
53 {0x45, 0x53, 0x54, 0x41, 0x42, 0x4c, 0x49,
54 0x53, 0x48, 0x45, 0x44, 0};
55 static const char fin_wait_1[] = /* "FIN-WAIT-1",*/
56 {0x46, 0x49, 0x4e, 0x2d, 0x57, 0x41, 0x49,
57 0x54, 0x2d, 0x31, 0};
58 static const char fin_wait_2[] = /* "FIN-WAIT-2",*/
59 {0x46, 0x49, 0x4e, 0x2d, 0x57, 0x41, 0x49,
60 0x54, 0x2d, 0x32, 0};
61 static const char closing[] = /* "CLOSING",*/
62 {0x43, 0x4c, 0x4f, 0x53, 0x49,
63 0x4e, 0x47, 0};
64 static const char time_wait[] = /* "TIME-WAIT,"*/
65 {0x54, 0x49, 0x4d, 0x45, 0x2d, 0x57, 0x41,
66 0x49, 0x54, 0};
67 static const char last_ack[] = /* "LAST-ACK"*/
68 {0x4c, 0x41, 0x53, 0x54, 0x2d, 0x41, 0x43,
69 0x4b, 0};
70 static const char none[] = /* "NONE"*/
71 {0x4e, 0x4f, 0x4e, 0x45, 0};
72 static const char running[] = /* "RUNNING"*/
73 {0x52, 0x55, 0x4e, 0x4e, 0x49, 0x4e, 0x47,
74 0};
75 static const char called[] = /* "CALLED"*/
76 {0x43, 0x41, 0x4c, 0x4c, 0x45, 0x44, 0};
77 static const char file_name[] = /* "file-stats"*/
78 {0x66, 0x69, 0x6c, 0x65, 0x2d, 0x73, 0x74,
79 0x61, 0x74, 0x73, 0};
80 static const char tcp_name[] = /* "tcp-connections"*/
81 {0x74, 0x63, 0x70, 0x2d, 0x63, 0x6f, 0x6e,
82 0x6e, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e,
83 0x73, 0};
84 static const char proc_name[] = /* "processes"*/
85 {0x70, 0x72, 0x6f, 0x63, 0x65, 0x73, 0x73,
86 0x65, 0x73, 0};
88 static const char *states[] = {
89 closed,
90 syn_rcvd,
91 syn_sent,
92 established,
93 fin_wait_1,
94 fin_wait_2,
95 closing,
96 time_wait,
97 last_ack,
98 none,
99 running,
100 called};
102 #define BUFLEN 100
104 /*---------------------------------------------------------------------------*/
105 PROCESS(shell_netstat_process, "netstat");
106 SHELL_COMMAND(netstat_command,
107 "netstat",
108 "netstat: show UDP and TCP connections",
109 &shell_netstat_process);
110 /*---------------------------------------------------------------------------*/
111 PROCESS_THREAD(shell_netstat_process, ev, data)
113 char buf[BUFLEN];
114 int i;
115 struct uip_conn *conn;
116 PROCESS_BEGIN();
118 for(i = 0; i < UIP_CONNS; ++i) {
119 conn = &uip_conns[i];
120 snprintf(buf, BUFLEN,
121 "%d, %u.%u.%u.%u:%u, %s, %u, %u, %c %c",
122 htons(conn->lport),
123 conn->ripaddr.u8[0],
124 conn->ripaddr.u8[1],
125 conn->ripaddr.u8[2],
126 conn->ripaddr.u8[3],
127 htons(conn->rport),
128 states[conn->tcpstateflags & UIP_TS_MASK],
129 conn->nrtx,
130 conn->timer,
131 (uip_outstanding(conn))? '*':' ',
132 (uip_stopped(conn))? '!':' ');
133 shell_output_str(&netstat_command, "TCP ", buf);
135 PROCESS_END();
137 /*---------------------------------------------------------------------------*/
138 void
139 shell_netstat_init(void)
141 shell_register_command(&netstat_command);
143 /*---------------------------------------------------------------------------*/