2 * Copyright (c) 2004, Adam Dunkels.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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
29 * This file is part of the Contiki operating system.
31 * Author: Adam Dunkels <adam@sics.se>
33 * $Id: shell-tcpsend.c,v 1.4 2009/03/17 21:49:44 adamdunkels Exp $
44 #define MIN(a, b) ((a) < (b)? (a) : (b))
47 /*---------------------------------------------------------------------------*/
48 PROCESS(shell_tcpsend_process
, "tcpsend");
49 SHELL_COMMAND(tcpsend_command
,
51 "tcpsend <host> <port>: open a TCP connection",
52 &shell_tcpsend_process
);
53 /*---------------------------------------------------------------------------*/
55 #define MAX_SERVERLEN 16
57 static uip_ipaddr_t serveraddr
;
58 static char server
[MAX_SERVERLEN
+ 1];
60 static struct telnet_state s
;
62 static unsigned char running
;
64 #define MAX_LINELEN 80
66 static char outputline
[MAX_LINELEN
];
67 static uint8_t sending
;
68 /*---------------------------------------------------------------------------*/
70 telnet_text_output(struct telnet_state
*s
, char *text1
, char *text2
)
72 char buf1
[MAX_SERVERLEN
];
75 strncpy(buf1
, text1
, sizeof(buf1
));
77 if(len
< sizeof(buf1
) - 1) {
81 shell_output_str(&tcpsend_command
, buf1
, text2
);
83 /*---------------------------------------------------------------------------*/
85 telnet_newdata(struct telnet_state
*s
, char *data
, u16_t len
)
87 shell_output(&tcpsend_command
, data
, len
, "", 0);
89 /*---------------------------------------------------------------------------*/
91 send_line(struct telnet_state
*s
, char *data
, int len
)
94 strncpy(outputline
, data
, MIN(sizeof(outputline
), len
));
95 telnet_send(s
, data
, len
);
98 shell_output_str(&tcpsend_command
, "Cannot send data, still sending previous data", "");
101 /*---------------------------------------------------------------------------*/
103 telnet_sent(struct telnet_state
*s
)
107 /*---------------------------------------------------------------------------*/
109 telnet_closed(struct telnet_state
*s
)
111 telnet_text_output(s
, server
, "connection closed");
114 /*---------------------------------------------------------------------------*/
116 telnet_aborted(struct telnet_state
*s
)
118 telnet_text_output(s
, server
, "connection aborted");
121 /*---------------------------------------------------------------------------*/
123 telnet_timedout(struct telnet_state
*s
)
125 telnet_text_output(s
, server
, "connection timed out");
128 /*---------------------------------------------------------------------------*/
130 telnet_connected(struct telnet_state
*s
)
132 telnet_text_output(s
, server
, "connected");
134 /*---------------------------------------------------------------------------*/
135 PROCESS_THREAD(shell_tcpsend_process
, ev
, data
)
139 struct shell_input
*input
;
144 next
= strchr(data
, ' ');
146 shell_output_str(&tcpsend_command
,
147 "tcpsend <server> <port>: server as address", "");
152 strncpy(server
, data
, sizeof(server
));
153 port
= shell_strtolong(next
, &dummy
);
157 uiplib_ipaddrconv(server
, (u8_t
*)&serveraddr
);
158 telnet_connect(&s
, &serveraddr
, port
);
160 PROCESS_WAIT_EVENT();
162 if(ev
== shell_event_input
) {
164 if(input
->len1
+ input
->len2
== 0) {
168 if(input
->len1
> 0) {
169 send_line(&s
, input
->data1
, input
->len1
);
171 } else if(ev
== tcpip_event
) {
174 } else if(ev
== resolv_event_found
) {
175 /* Either found a hostname, or not. */
176 if((char *)data
!= NULL
&&
177 resolv_lookup((char *)data
) != NULL
) {
178 uip_ipaddr_copy(serveraddr
, ipaddr
);
179 telnet_connect(&s
, server
, serveraddr
, nick
);
181 shell_output_str(&tcpsend_command
, "Host not found.", "");
189 /*---------------------------------------------------------------------------*/
191 shell_tcpsend_init(void)
193 shell_register_command(&tcpsend_command
);
195 /*---------------------------------------------------------------------------*/