adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 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. |
adamdunkels | 06f897e | 2004-06-06 05:59:20 +0000 | [diff] [blame] | 13 | * 3. The name of the author may not be used to endorse or promote |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 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 | * |
adamdunkels | f2f8cb2 | 2004-07-04 11:35:07 +0000 | [diff] [blame] | 31 | * $Id: telnet.c,v 1.5 2004/07/04 11:35:08 adamdunkels Exp $ |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 32 | * |
| 33 | */ |
| 34 | |
adamdunkels | f2f8cb2 | 2004-07-04 11:35:07 +0000 | [diff] [blame] | 35 | #include "tcpip.h" |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 36 | |
| 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 | /*-----------------------------------------------------------------------------------*/ |
| 46 | unsigned char |
| 47 | telnet_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 | /*-----------------------------------------------------------------------------------*/ |
| 58 | unsigned char |
| 59 | telnet_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 | /*-----------------------------------------------------------------------------------*/ |
| 68 | unsigned char |
| 69 | telnet_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 | /*-----------------------------------------------------------------------------------*/ |
| 78 | static void |
| 79 | acked(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 | /*-----------------------------------------------------------------------------------*/ |
| 91 | static void |
| 92 | senddata(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 | /*-----------------------------------------------------------------------------------*/ |
adamdunkels | f2f8cb2 | 2004-07-04 11:35:07 +0000 | [diff] [blame] | 106 | void |
| 107 | telnet_app(void *ts) |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 108 | { |
adamdunkels | 2e41542 | 2003-04-10 09:04:49 +0000 | [diff] [blame] | 109 | struct telnet_state *s = (struct telnet_state *)ts; |
| 110 | |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 111 | 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 | /*-----------------------------------------------------------------------------------*/ |