Contiki 3.x
shell-tcpsend.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 
38 #include "contiki.h"
39 #include "sys/cc.h"
40 #include "shell.h"
41 #include "telnet.h"
42 
43 /*---------------------------------------------------------------------------*/
44 PROCESS(shell_tcpsend_process, "tcpsend");
45 SHELL_COMMAND(tcpsend_command,
46  "tcpsend",
47  "tcpsend <host> <port>: open a TCP connection",
48  &shell_tcpsend_process);
49 /*---------------------------------------------------------------------------*/
50 
51 #define MAX_SERVERLEN 16
52 
53 static uip_ipaddr_t serveraddr;
54 static char server[MAX_SERVERLEN + 1];
55 
56 static struct telnet_state s;
57 
58 static unsigned char running;
59 
60 #define MAX_LINELEN 80
61 
62 static char outputline[MAX_LINELEN];
63 static uint8_t sending;
64 /*---------------------------------------------------------------------------*/
65 void
66 telnet_text_output(struct telnet_state *s, char *text1, char *text2)
67 {
68  char buf1[MAX_SERVERLEN];
69  int len;
70 
71  strncpy(buf1, text1, sizeof(buf1));
72  len = strlen(buf1);
73  if(len < sizeof(buf1) - 1) {
74  buf1[len] = ' ';
75  buf1[len + 1] = 0;
76  }
77  shell_output_str(&tcpsend_command, buf1, text2);
78 }
79 /*---------------------------------------------------------------------------*/
80 void
81 telnet_newdata(struct telnet_state *s, char *data, uint16_t len)
82 {
83  shell_output(&tcpsend_command, data, len, "", 0);
84 }
85 /*---------------------------------------------------------------------------*/
86 static void
87 send_line(struct telnet_state *s, char *data, int len)
88 {
89  if(!sending) {
90  strncpy(outputline, data, MIN(sizeof(outputline), len));
91  telnet_send(s, data, len);
92  sending = 1;
93  } else {
94  shell_output_str(&tcpsend_command, "Cannot send data, still sending previous data", "");
95  }
96 }
97 /*---------------------------------------------------------------------------*/
98 void
99 telnet_sent(struct telnet_state *s)
100 {
101  sending = 0;
102 }
103 /*---------------------------------------------------------------------------*/
104 void
105 telnet_closed(struct telnet_state *s)
106 {
107  telnet_text_output(s, server, "connection closed");
108  running = 0;
109 }
110 /*---------------------------------------------------------------------------*/
111 void
112 telnet_aborted(struct telnet_state *s)
113 {
114  telnet_text_output(s, server, "connection aborted");
115  running = 0;
116 }
117 /*---------------------------------------------------------------------------*/
118 void
119 telnet_timedout(struct telnet_state *s)
120 {
121  telnet_text_output(s, server, "connection timed out");
122  running = 0;
123 }
124 /*---------------------------------------------------------------------------*/
125 void
126 telnet_connected(struct telnet_state *s)
127 {
128  telnet_text_output(s, server, "connected");
129 }
130 /*---------------------------------------------------------------------------*/
131 PROCESS_THREAD(shell_tcpsend_process, ev, data)
132 {
133  char *next;
134  const char *dummy;
135  struct shell_input *input;
136  uint16_t port;
137 
138  PROCESS_BEGIN();
139 
140  next = strchr(data, ' ');
141  if(next == NULL) {
142  shell_output_str(&tcpsend_command,
143  "tcpsend <server> <port>: server as address", "");
144  PROCESS_EXIT();
145  }
146  *next = 0;
147  ++next;
148  strncpy(server, data, sizeof(server));
149  port = shell_strtolong(next, &dummy);
150 
151  running = 1;
152 
153  uiplib_ipaddrconv(server, &serveraddr);
154  telnet_connect(&s, &serveraddr, port);
155  while(running) {
157 
158  if(ev == shell_event_input) {
159  input = data;
160  if(input->len1 + input->len2 == 0) {
161  PROCESS_EXIT();
162  }
163 
164  if(input->len1 > 0) {
165  send_line(&s, input->data1, input->len1);
166  }
167  } else if(ev == tcpip_event) {
168  telnet_app(data);
169 #if 0
170  } else if(ev == resolv_event_found) {
171  /* Either found a hostname, or not. */
172  if((char *)data != NULL &&
173  resolv_lookup((char *)data, &ipaddr) == RESOLV_STATUS_CACHED) {
174  uip_ipaddr_copy(serveraddr, ipaddr);
175  telnet_connect(&s, server, serveraddr, nick);
176  } else {
177  shell_output_str(&tcpsend_command, "Host not found.", "");
178  }
179 #endif /* 0 */
180  }
181  }
182 
183  PROCESS_END();
184 }
185 /*---------------------------------------------------------------------------*/
186 void
187 shell_tcpsend_init(void)
188 {
189  shell_register_command(&tcpsend_command);
190 }
191 /*---------------------------------------------------------------------------*/
static uip_ipaddr_t ipaddr
Pointer to prefix information option in uip_buf.
Definition: uip-nd6.c:129
process_event_t tcpip_event
The uIP event.
Definition: tcpip.c:80
Default definitions of C compiler quirk work-arounds.
Main header file for the Contiki shell
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_END()
Define the end of a process.
Definition: process.h:131
#define PROCESS(name, strname)
Declare a process.
Definition: process.h:307
static void input(void)
Process a received 6lowpan packet.
Definition: sicslowpan.c:1503
unsigned long shell_strtolong(const char *str, const char **retstr)
Convert a string to a number.
Definition: shell.c:521
#define PROCESS_THREAD(name, ev, data)
Define the body of a process.
Definition: process.h:273
#define PROCESS_EXIT()
Exit the currently running process.
Definition: process.h:200
void shell_output(struct shell_command *c, void *data1, int len1, const void *data2, int len2)
Output data from a shell command.
Definition: shell.c:395
#define uip_ipaddr_copy(dest, src)
Copy an IP address from one place to another.
Definition: uip.h:1027
#define SHELL_COMMAND(name, command, description, process)
Define a shell command.
Definition: shell.h:219
process_event_t resolv_event_found
Event that is broadcasted when a DNS name has been resolved.
Definition: resolv.c:286
int shell_event_input
The event number for shell input data.
Definition: shell.c:70
void shell_register_command(struct shell_command *c)
Register a command with the shell.
Definition: shell.c:413
#define NULL
The null pointer.
resolv_status_t resolv_lookup(const char *name, uip_ipaddr_t **ipaddr)
Look up a hostname in the array of known hostnames.
Definition: resolv.c:1349
Structure for shell input data.
Definition: shell.h:365
Hostname is fresh and usable.
Definition: resolv.h:63
#define uiplib_ipaddrconv
Convert a textual representation of an IP address to a numerical representation.
Definition: uiplib.h:71
#define PROCESS_WAIT_EVENT()
Wait for an event to be posted to the process.
Definition: process.h:141
#define PROCESS_BEGIN()
Define the beginning of a process.
Definition: process.h:120