blob: df7bf2f739b0fe921486c4792c0c8340c1122a8f [file] [log] [blame]
adamdunkelsca9ddcb2003-03-19 14:13:31 +00001/*
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.
adamdunkels06f897e2004-06-06 05:59:20 +000013 * 3. The name of the author may not be used to endorse or promote
adamdunkelsca9ddcb2003-03-19 14:13:31 +000014 * 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 *
adamdunkelsf2f8cb22004-07-04 11:35:07 +000031 * $Id: telnet.c,v 1.5 2004/07/04 11:35:08 adamdunkels Exp $
adamdunkelsca9ddcb2003-03-19 14:13:31 +000032 *
33 */
34
adamdunkelsf2f8cb22004-07-04 11:35:07 +000035#include "tcpip.h"
adamdunkelsca9ddcb2003-03-19 14:13:31 +000036
37#include "telnet.h"
38
39#ifndef NULL
40#define NULL (void *)0
41#endif /* NULL */
42
43#define FLAG_CLOSE 1
44#define FLAG_ABORT 2
45/*-----------------------------------------------------------------------------------*/
46unsigned char
47telnet_send(struct telnet_state *s, char *text, u16_t len)
48{
49 if(s->text != NULL) {
50 return 1;
51 }
52 s->text = text;
53 s->textlen = len;
54 s->sentlen = 0;
55 return 0;
56}
57/*-----------------------------------------------------------------------------------*/
58unsigned char
59telnet_close(struct telnet_state *s)
60{
61 s->flags = FLAG_CLOSE;
62 if(s->text != NULL) {
63 return 1;
64 }
65 return 0;
66}
67/*-----------------------------------------------------------------------------------*/
68unsigned char
69telnet_abort(struct telnet_state *s)
70{
71 s->flags = FLAG_ABORT;
72 if(s->text != NULL) {
73 return 1;
74 }
75 return 0;
76}
77/*-----------------------------------------------------------------------------------*/
78static void
79acked(struct telnet_state *s)
80{
81 s->textlen -= s->sentlen;
82 if(s->textlen == 0) {
83 s->text = NULL;
84 telnet_sent(s);
85 } else {
86 s->text += s->sentlen;
87 }
88 s->sentlen = 0;
89}
90/*-----------------------------------------------------------------------------------*/
91static void
92senddata(struct telnet_state *s)
93{
94 if(s->text == NULL) {
95 uip_send(s->text, 0);
96 return;
97 }
98 if(s->textlen > uip_mss()) {
99 s->sentlen = uip_mss();
100 } else {
101 s->sentlen = s->textlen;
102 }
103 uip_send(s->text, s->sentlen);
104}
105/*-----------------------------------------------------------------------------------*/
adamdunkelsf2f8cb22004-07-04 11:35:07 +0000106void
107telnet_app(void *ts)
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000108{
adamdunkels2e415422003-04-10 09:04:49 +0000109 struct telnet_state *s = (struct telnet_state *)ts;
110
adamdunkelsca9ddcb2003-03-19 14:13:31 +0000111 if(uip_connected()) {
112 s->flags = 0;
113 telnet_connected(s);
114 senddata(s);
115 return;
116 }
117
118 if(uip_closed()) {
119 telnet_closed(s);
120 }
121
122 if(uip_aborted()) {
123 telnet_aborted(s);
124 }
125 if(uip_timedout()) {
126 telnet_timedout(s);
127 }
128
129
130 if(s->flags & FLAG_CLOSE) {
131 uip_close();
132 return;
133 }
134 if(s->flags & FLAG_ABORT) {
135 uip_abort();
136 return;
137 }
138 if(uip_acked()) {
139 acked(s);
140 }
141 if(uip_newdata()) {
142 telnet_newdata(s, (char *)uip_appdata, uip_datalen());
143 }
144 if(uip_rexmit() ||
145 uip_newdata() ||
146 uip_acked()) {
147 senddata(s);
148 } else if(uip_poll()) {
149 senddata(s);
150 }
151}
152/*-----------------------------------------------------------------------------------*/