Contiki 3.x
shell-netstat.c
1 /*
2  * Copyright (c) 2004, Adam Dunkels.
3  * All rights reserved.
4  *
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.
16  *
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.
28  *
29  * This file is part of the Contiki operating system.
30  *
31  * Author: Adam Dunkels <adam@sics.se>
32  *
33  */
34 
35 #include <string.h>
36 #include <stddef.h>
37 #include <stdio.h>
38 
39 #include "contiki.h"
40 #include "shell.h"
41 #include "contiki-net.h"
42 
43 static const char closed[] = /* "CLOSED",*/
44 {0x43, 0x4c, 0x4f, 0x53, 0x45, 0x44, 0};
45 static const char syn_rcvd[] = /* "SYN-RCVD",*/
46 {0x53, 0x59, 0x4e, 0x2d, 0x52, 0x43, 0x56,
47  0x44, 0};
48 static const char syn_sent[] = /* "SYN-SENT",*/
49 {0x53, 0x59, 0x4e, 0x2d, 0x53, 0x45, 0x4e,
50  0x54, 0};
51 static const char established[] = /* "ESTABLISHED",*/
52 {0x45, 0x53, 0x54, 0x41, 0x42, 0x4c, 0x49,
53  0x53, 0x48, 0x45, 0x44, 0};
54 static const char fin_wait_1[] = /* "FIN-WAIT-1",*/
55 {0x46, 0x49, 0x4e, 0x2d, 0x57, 0x41, 0x49,
56  0x54, 0x2d, 0x31, 0};
57 static const char fin_wait_2[] = /* "FIN-WAIT-2",*/
58 {0x46, 0x49, 0x4e, 0x2d, 0x57, 0x41, 0x49,
59  0x54, 0x2d, 0x32, 0};
60 static const char closing[] = /* "CLOSING",*/
61 {0x43, 0x4c, 0x4f, 0x53, 0x49,
62  0x4e, 0x47, 0};
63 static const char time_wait[] = /* "TIME-WAIT,"*/
64 {0x54, 0x49, 0x4d, 0x45, 0x2d, 0x57, 0x41,
65  0x49, 0x54, 0};
66 static const char last_ack[] = /* "LAST-ACK"*/
67 {0x4c, 0x41, 0x53, 0x54, 0x2d, 0x41, 0x43,
68  0x4b, 0};
69 static const char none[] = /* "NONE"*/
70 {0x4e, 0x4f, 0x4e, 0x45, 0};
71 static const char running[] = /* "RUNNING"*/
72 {0x52, 0x55, 0x4e, 0x4e, 0x49, 0x4e, 0x47,
73  0};
74 static const char called[] = /* "CALLED"*/
75 {0x43, 0x41, 0x4c, 0x4c, 0x45, 0x44, 0};
76 static const char file_name[] = /* "file-stats"*/
77 {0x66, 0x69, 0x6c, 0x65, 0x2d, 0x73, 0x74,
78  0x61, 0x74, 0x73, 0};
79 static const char tcp_name[] = /* "tcp-connections"*/
80 {0x74, 0x63, 0x70, 0x2d, 0x63, 0x6f, 0x6e,
81  0x6e, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e,
82  0x73, 0};
83 static const char proc_name[] = /* "processes"*/
84 {0x70, 0x72, 0x6f, 0x63, 0x65, 0x73, 0x73,
85  0x65, 0x73, 0};
86 
87 static const char *states[] = {
88  closed,
89  syn_rcvd,
90  syn_sent,
91  established,
92  fin_wait_1,
93  fin_wait_2,
94  closing,
95  time_wait,
96  last_ack,
97  none,
98  running,
99  called};
100 
101 #define BUFLEN 100
102 
103 /*---------------------------------------------------------------------------*/
104 PROCESS(shell_netstat_process, "netstat");
105 SHELL_COMMAND(netstat_command,
106  "netstat",
107  "netstat: show UDP and TCP connections",
108  &shell_netstat_process);
109 /*---------------------------------------------------------------------------*/
110 PROCESS_THREAD(shell_netstat_process, ev, data)
111 {
112  char buf[BUFLEN];
113  int i;
114  struct uip_conn *conn;
115  PROCESS_BEGIN();
116 
117  for(i = 0; i < UIP_CONNS; ++i) {
118  conn = &uip_conns[i];
119  snprintf(buf, BUFLEN,
120  "%d, %u.%u.%u.%u:%u, %s, %u, %u, %c %c",
121  uip_htons(conn->lport),
122  conn->ripaddr.u8[0],
123  conn->ripaddr.u8[1],
124  conn->ripaddr.u8[2],
125  conn->ripaddr.u8[3],
126  uip_htons(conn->rport),
127  states[conn->tcpstateflags & UIP_TS_MASK],
128  conn->nrtx,
129  conn->timer,
130  (uip_outstanding(conn))? '*':' ',
131  (uip_stopped(conn))? '!':' ');
132  shell_output_str(&netstat_command, "TCP ", buf);
133  }
134  PROCESS_END();
135 }
136 /*---------------------------------------------------------------------------*/
137 void
138 shell_netstat_init(void)
139 {
140  shell_register_command(&netstat_command);
141 }
142 /*---------------------------------------------------------------------------*/
#define UIP_CONNS
The maximum number of simultaneously open TCP connections.
Definition: uipopt.h:419
void shell_output_str(struct shell_command *c, char *text1, const char *text2)
Output strings from a shell command.
Definition: shell.c:383
#define PROCESS_BEGIN()
Define the beginning of a process.
Definition: process.h:120
Representation of a uIP TCP connection.
Definition: uip.h:1336
uint8_t nrtx
The number of retransmissions for the last segment sent.
Definition: uip.h:1359
uint16_t lport
The local TCP port, in network byte order.
Definition: uip.h:1339
Main header file for the Contiki shell
uint8_t timer
The retransmission timer.
Definition: uip.h:1358
#define PROCESS_THREAD(name, ev, data)
Define the body of a process.
Definition: process.h:273
#define PROCESS_END()
Define the end of a process.
Definition: process.h:131
uint16_t rport
The local remote TCP port, in network byte order.
Definition: uip.h:1340
CCIF uint16_t uip_htons(uint16_t val)
Convert a 16-bit quantity from host byte order to network byte order.
Definition: uip6.c:2298
void shell_register_command(struct shell_command *c)
Register a command with the shell.
Definition: shell.c:413
uint8_t tcpstateflags
TCP state and flags.
Definition: uip.h:1357
#define PROCESS(name, strname)
Declare a process.
Definition: process.h:307
#define uip_stopped(conn)
Find out if the current connection has been previously stopped with uip_stop().
Definition: uip.h:700
uip_ipaddr_t ripaddr
The IP address of the remote host.
Definition: uip.h:1337
#define SHELL_COMMAND(name, command, description, process)
Define a shell command.
Definition: shell.h:219