Contiki 3.x
telnet.c
1 /*
2  * Copyright (c) 2002, 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. The name of the author may not be used to endorse or promote
14  * products derived from this software without specific prior
15  * written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
18  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  * This file is part of the uIP TCP/IP stack.
30  *
31  *
32  */
33 
34 #include "contiki-net.h"
35 
36 #include "telnet.h"
37 
38 #ifndef NULL
39 #define NULL (void *)0
40 #endif /* NULL */
41 
42 #define FLAG_CLOSE 1
43 #define FLAG_ABORT 2
44 /*-----------------------------------------------------------------------------------*/
45 unsigned char
46 telnet_send(struct telnet_state *s, char *text, uint16_t len)
47 {
48  if(s->text != NULL) {
49  return 1;
50  }
51  s->text = text;
52  s->textlen = len;
53  s->sentlen = 0;
54  return 0;
55 }
56 /*-----------------------------------------------------------------------------------*/
57 unsigned char
58 telnet_close(struct telnet_state *s)
59 {
60  s->flags = FLAG_CLOSE;
61  if(s->text != NULL) {
62  return 1;
63  }
64  return 0;
65 }
66 /*-----------------------------------------------------------------------------------*/
67 unsigned char
68 telnet_abort(struct telnet_state *s)
69 {
70  s->flags = FLAG_ABORT;
71  if(s->text != NULL) {
72  return 1;
73  }
74  return 0;
75 }
76 /*-----------------------------------------------------------------------------------*/
77 static void
78 acked(struct telnet_state *s)
79 {
80  s->textlen -= s->sentlen;
81  if(s->textlen == 0) {
82  s->text = NULL;
83  telnet_sent(s);
84  } else {
85  s->text += s->sentlen;
86  }
87  s->sentlen = 0;
88 }
89 /*-----------------------------------------------------------------------------------*/
90 static void
91 senddata(struct telnet_state *s)
92 {
93  if(s->text == NULL) {
94  uip_send(s->text, 0);
95  return;
96  }
97  if(s->textlen > uip_mss()) {
98  s->sentlen = uip_mss();
99  } else {
100  s->sentlen = s->textlen;
101  }
102  uip_send(s->text, s->sentlen);
103 }
104 /*-----------------------------------------------------------------------------------*/
105 struct telnet_state *
106 telnet_connect(struct telnet_state *s, uip_ipaddr_t *addr, uint16_t port)
107 {
108  struct uip_conn *conn;
109 
110  conn = tcp_connect(addr, uip_htons(port), s);
111  if(conn == NULL) {
112  return NULL;
113  }
114  return s;
115 }
116 /*-----------------------------------------------------------------------------------*/
117 void
118 telnet_app(void *ts)
119 {
120  struct telnet_state *s = (struct telnet_state *)ts;
121 
122  if(uip_connected()) {
123  s->flags = 0;
124  telnet_connected(s);
125  senddata(s);
126  return;
127  }
128 
129  if(uip_closed()) {
130  telnet_closed(s);
131  }
132 
133  if(uip_aborted()) {
134  telnet_aborted(s);
135  }
136  if(uip_timedout()) {
137  telnet_timedout(s);
138  }
139 
140  if(s->flags & FLAG_CLOSE) {
141  uip_close();
142  return;
143  }
144  if(s->flags & FLAG_ABORT) {
145  uip_abort();
146  return;
147  }
148  if(uip_acked()) {
149  acked(s);
150  }
151  if(uip_newdata()) {
152  telnet_newdata(s, (char *)uip_appdata, uip_datalen());
153  }
154  if(uip_rexmit() ||
155  uip_newdata() ||
156  uip_acked()) {
157  senddata(s);
158  } else if(uip_poll()) {
159  senddata(s);
160  }
161 }
162 /*-----------------------------------------------------------------------------------*/
Representation of a uIP TCP connection.
Definition: uip.h:1336
CCIF struct uip_conn * tcp_connect(uip_ipaddr_t *ripaddr, uint16_t port, void *appstate)
Open a TCP connection to the specified IP address and port.
#define uip_aborted()
Has the connection been aborted by the other end?
Definition: uip.h:781
CCIF void uip_send(const void *data, int len)
Send data on the current connection.
Definition: uip6.c:2310
#define uip_newdata()
Is new incoming data available?
Definition: uip.h:738
#define uip_mss()
Get the current maximum segment size that can be sent on the current connection.
Definition: uip.h:838
#define NULL
The null pointer.
#define uip_poll()
Is the connection being polled by uIP?
Definition: uip.h:817
#define uip_acked()
Has previously sent data been acknowledged?
Definition: uip.h:749
#define uip_connected()
Has the connection just been connected?
Definition: uip.h:761
#define uip_abort()
Abort the current connection.
Definition: uip.h:682
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
#define uip_close()
Close the current connection.
Definition: uip.h:671
#define uip_datalen()
The length of any incoming data that is currently available (if available) in the uip_appdata buffer...
Definition: uip.h:651
#define uip_timedout()
Has the connection timed out?
Definition: uip.h:791
#define uip_closed()
Has the connection been closed by the other end?
Definition: uip.h:771
uip_appdata
Pointer to the application data in the packet buffer.
Definition: tcp_loader.c:74
#define uip_rexmit()
Do we need to retransmit previously data?
Definition: uip.h:803