1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
|
/*
* libwebsockets - small server side websockets and web server implementation
*
* Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include "libwebsockets.h"
#include "lws-ssh.h"
#include <string.h>
struct per_vhost_data__telnet {
struct lws_context *context;
struct lws_vhost *vhost;
const struct lws_protocols *protocol;
struct per_session_data__telnet *live_pss_list;
const struct lws_ssh_ops *ops;
};
struct per_session_data__telnet {
struct per_session_data__telnet *next;
struct per_vhost_data__telnet *vhd;
uint32_t rx_tail;
void *priv;
uint32_t initial:1;
char state;
uint8_t cmd;
};
enum {
LTS_BINARY_XMIT,
LTS_ECHO,
LTS_SUPPRESS_GA,
LTSC_SUBOPT_END = 240,
LTSC_BREAK = 243,
LTSC_SUBOPT_START = 250,
LTSC_WILL = 251,
LTSC_WONT,
LTSC_DO,
LTSC_DONT,
LTSC_IAC,
LTST_WAIT_IAC = 0,
LTST_GOT_IAC,
LTST_WAIT_OPT,
};
static int
telnet_ld(struct per_session_data__telnet *pss, uint8_t c)
{
switch (pss->state) {
case LTST_WAIT_IAC:
if (c == LTSC_IAC) {
pss->state = LTST_GOT_IAC;
return 0;
}
return 1;
case LTST_GOT_IAC:
pss->state = LTST_WAIT_IAC;
switch (c) {
case LTSC_BREAK:
return 0;
case LTSC_WILL:
case LTSC_WONT:
case LTSC_DO:
case LTSC_DONT:
pss->cmd = c;
pss->state = LTST_WAIT_OPT;
return 0;
case LTSC_IAC:
return 1; /* double IAC */
}
return 0; /* ignore unknown */
case LTST_WAIT_OPT:
lwsl_notice(" tld: cmd %d: opt %d\n", pss->cmd, c);
pss->state = LTST_WAIT_IAC;
return 0;
}
return 0;
}
static uint8_t init[] = {
LTSC_IAC, LTSC_WILL, 3,
LTSC_IAC, LTSC_WILL, 1,
LTSC_IAC, LTSC_DONT, 1,
LTSC_IAC, LTSC_DO, 0
};
static int
lws_callback_raw_telnet(struct lws *wsi, enum lws_callback_reasons reason,
void *user, void *in, size_t len)
{
struct per_session_data__telnet *pss =
(struct per_session_data__telnet *)user, **p;
struct per_vhost_data__telnet *vhd =
(struct per_vhost_data__telnet *)
lws_protocol_vh_priv_get(lws_get_vhost(wsi),
lws_get_protocol(wsi));
const struct lws_protocol_vhost_options *pvo =
(const struct lws_protocol_vhost_options *)in;
int n, m;
uint8_t buf[LWS_PRE + 800], *pu = in;
switch ((int)reason) {
case LWS_CALLBACK_PROTOCOL_INIT:
vhd = lws_protocol_vh_priv_zalloc(lws_get_vhost(wsi),
lws_get_protocol(wsi),
sizeof(struct per_vhost_data__telnet));
vhd->context = lws_get_context(wsi);
vhd->protocol = lws_get_protocol(wsi);
vhd->vhost = lws_get_vhost(wsi);
while (pvo) {
if (!strcmp(pvo->name, "ops"))
vhd->ops = (const struct lws_ssh_ops *)pvo->value;
pvo = pvo->next;
}
if (!vhd->ops) {
lwsl_err("telnet pvo \"ops\" is mandatory\n");
return -1;
}
break;
case LWS_CALLBACK_RAW_ADOPT:
pss->next = vhd->live_pss_list;
vhd->live_pss_list = pss;
pss->vhd = vhd;
pss->state = LTST_WAIT_IAC;
pss->initial = 0;
if (vhd->ops->channel_create)
vhd->ops->channel_create(wsi, &pss->priv);
lws_callback_on_writable(wsi);
break;
case LWS_CALLBACK_RAW_CLOSE:
p = &vhd->live_pss_list;
while (*p) {
if ((*p) == pss) {
if (vhd->ops->channel_destroy)
vhd->ops->channel_destroy(pss->priv);
*p = pss->next;
continue;
}
p = &((*p)->next);
}
break;
case LWS_CALLBACK_RAW_RX:
n = 0;
/* this stuff is coming in telnet line discipline, we
* have to strip IACs and process IAC repeats */
while (len--) {
if (telnet_ld(pss, *pu))
buf[n++] = *pu++;
else
pu++;
if (n > 100 || !len)
pss->vhd->ops->rx(pss->priv, wsi, buf, (uint32_t)n);
}
break;
case LWS_CALLBACK_RAW_WRITEABLE:
n = 0;
if (!pss->initial) {
memcpy(buf + LWS_PRE, init, sizeof(init));
n = sizeof(init);
pss->initial = 1;
} else {
/* bring any waiting tx into second half of buffer
* restrict how much we can send to 1/4 of the buffer,
* because we have to apply telnet line discipline...
* in the worst case of all 0xff, doubling the size
*/
pu = buf + LWS_PRE + 400;
m = (int)pss->vhd->ops->tx(pss->priv, LWS_STDOUT, pu,
(size_t)((int)sizeof(buf) - LWS_PRE - n - 401) / 2);
/*
* apply telnet line discipline and copy into place
* in output buffer
*/
while (m--) {
if (*pu == 0xff)
buf[LWS_PRE + n++] = 0xff;
buf[LWS_PRE + n++] = *pu++;
}
}
if (n > 0) {
m = lws_write(wsi, (unsigned char *)buf + LWS_PRE, (unsigned int)n,
LWS_WRITE_HTTP);
if (m < 0) {
lwsl_err("ERROR %d writing to di socket\n", m);
return -1;
}
}
if (vhd->ops->tx_waiting(&pss->priv))
lws_callback_on_writable(wsi);
break;
case LWS_CALLBACK_SSH_UART_SET_RXFLOW:
/*
* this is sent to set rxflow state on any connections that
* sink on a particular uart. The uart index affected is in len
*
* More than one protocol may sink to the same uart, and the
* protocol may select the uart itself, eg, in the URL used
* to set up the connection.
*/
lws_rx_flow_control(wsi, len & 1);
break;
default:
break;
}
return 0;
}
const struct lws_protocols protocols_telnet[] = {
{
"lws-telnetd-base",
lws_callback_raw_telnet,
sizeof(struct per_session_data__telnet),
1024, 0, NULL, 900
},
{ NULL, NULL, 0, 0, 0, NULL, 0 } /* terminator */
};
|