-
Notifications
You must be signed in to change notification settings - Fork 22
/
telnet.c
1655 lines (1408 loc) · 34.5 KB
/
telnet.c
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*-
* Copyright (c) 1997 Dave Richards <dave@synergy.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* and exceptions are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the authors may not be used to endorse or promote products
* derived from this software without specific prior written permission.
* 4. The code can not be used by Gary Random, Random Communications, Inc.,
* the employees of Random Communications, Inc. or its subsidiaries,
* including Defiance MUD, without prior written permission from the
* authors.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/telnet.h>
#include <netdb.h>
#include "config.h"
#include "patchlevel.h"
#include "lint.h"
#include "main.h"
#include "interpret.h"
#include "simulate.h"
#include "nqueue.h"
#include "ndesc.h"
#include "net.h"
#include "telnet.h"
#include "comm.h"
#include "object.h"
#include "backend.h"
#ifndef EPROTO
#define EPROTO EPROTOTYPE
#endif
/*
* Telnet Server
*/
/*
* Queue Sizes.
*/
#define TELNET_CANQ_SIZE (1024 * 8)
#define TELNET_RAWQ_SIZE (1024 * 4)
#define TELNET_OPTQ_SIZE (1024)
#define TELNET_OUTQ_SIZE (32*1024)
/*
* Output Queue Flow Control Parameters.
*/
#define TELNET_OUTQ_LOWAT (TELNET_OUTQ_SIZE/2)
#define TELNET_OUTQ_HIWAT (TELNET_OUTQ_SIZE - 256)
/*
* The following parameter specifies the minimum number of bytes that must
* exist in the output queue to allow raw queue processing to continue.
* It is set to the maximum number of bytes ever sent in response to a
* TELNET command. N.B. Do not change this unless you know what you
* are doing!
*/
#define TELNET_OUTQ_REQUIRED 12
/*
* The folowing parameters are used to set the kernel socket buffer
* size for the TELNET connection. By default, BSD sets both send
* and receive buffers to 4k. It makes more sense to increase the
* send size and reduce the receive size for a MUD.
*/
#define TELNET_RCVBUF_SIZE 2048
#define TELNET_SNDBUF_SIZE TELNET_OUTQ_SIZE
/*
* ASCII Definitions.
*/
#define NUL 0
#define BEL 7
#define BS 8
#define LF 10
#define CR 13
#define DEL 127
#define TRUNCATED "*** Truncated. ***\r\n"
static void telnet_interactive(void *vp);
static void telnet_input(telnet_t *tp);
static void telnet_readbytes(ndesc_t *nd, telnet_t *tp);
static void telnet_send_sb(telnet_t *tp, u_char opt, u_char *data);
/*
* Allocate a Telnet control block.
*/
static telnet_t *
telnet_alloc(void)
{
int i;
telnet_t *tp;
tp = xalloc(sizeof (telnet_t));
tp->t_flags = 0;
tp->t_state = TS_DATA;
tp->t_nd = NULL;
tp->t_rawq = nq_alloc(TELNET_RAWQ_SIZE);
tp->t_canq = nq_alloc(TELNET_CANQ_SIZE);
tp->t_optq = NULL;
tp->t_outq = nq_alloc(TELNET_OUTQ_SIZE);
tp->t_ip = NULL;
tp->t_rblen = 0;
tp->t_sblen = 0;
tp->task = NULL;
for (i = 0; i < OP_SIZE; i++)
{
tp->t_optb[i].o_us = OS_NO;
tp->t_optb[i].o_usq = OQ_EMPTY;
tp->t_optb[i].o_him = OS_NO;
tp->t_optb[i].o_himq = OQ_EMPTY;
}
return tp;
}
/*
* Free a Telnet Control Block.
*/
static void
telnet_free(telnet_t *tp)
{
nq_free(tp->t_rawq);
nq_free(tp->t_canq);
if (tp->t_optq != NULL)
nq_free(tp->t_optq);
nq_free(tp->t_outq);
if (tp->task)
remove_task(tp->task);
tp->task = NULL;
free(tp);
}
/*
* Flush the output queue.
*/
static void
telnet_flush(telnet_t *tp)
{
if (!nq_empty(tp->t_outq))
nq_send(tp->t_outq, nd_fd(tp->t_nd), &tp->t_sblen);
}
/*
* Close a Telnet session and free the associated resources.
*/
static void
telnet_shutdown(ndesc_t *nd, telnet_t *tp)
{
if (tp != NULL)
{
telnet_flush(tp);
telnet_free(tp);
}
(void)close(nd_fd(nd));
nd_detach(nd);
}
/*
* Associate a Telnet control block with an interactive object.
*/
static void
telnet_attach(telnet_t *tp, struct interactive *ip)
{
tp->t_flags |= TF_ATTACH;
tp->t_ip = ip;
}
/*
* Disassociate a Telnet control block from an interactive object.
*/
void
telnet_detach(telnet_t *tp)
{
if ((tp->t_flags & TF_ATTACH) == 0)
return;
tp->t_flags &= ~TF_ATTACH;
tp->t_ip = NULL;
if (!tp->task)
tp->task = create_task(telnet_interactive, tp);
}
/*
* Re-enable the output queue (if blocked).
*/
static void
telnet_enabw(telnet_t *tp)
{
nd_enable(tp->t_nd, ND_W);
}
/*
* Append a character string to the output queue, encapsulating it
* appropriately.
* Returns 1 in case the message is truncated.
*/
int
telnet_output(telnet_t *tp, u_char *cp)
{
int len;
u_char c, *bp, buf[TELNET_OUTQ_SIZE + 3];
if (tp->t_flags & TF_OVFLOUTQ)
return 1;
bp = buf;
while (bp < &buf[TELNET_OUTQ_SIZE] && *cp != '\0')
{
c = *cp++;
switch (c)
{
case LF:
*bp++ = CR;
break;
case IAC:
*bp++ = IAC;
break;
}
*bp++ = c;
}
*bp = '\0';
len = bp - buf;
if (len == 0)
return 0;
if (nq_len(tp->t_outq) + len >= TELNET_OUTQ_HIWAT)
{
telnet_flush(tp);
if (nq_len(tp->t_outq) + len >= TELNET_OUTQ_HIWAT)
{
tp->t_flags |= TF_OVFLOUTQ;
buf[TELNET_OUTQ_HIWAT - nq_len(tp->t_outq)] = '\0';
nq_puts(tp->t_outq, buf);
nq_puts(tp->t_outq, (u_char *)TRUNCATED);
telnet_enabw(tp);
return 1;
}
}
nq_puts(tp->t_outq, buf);
telnet_enabw(tp);
return 0;
}
/*
* Send a GMCP SB message if GMCP has been enabled on the connection.
* Returns 1 if gmcp is not enabled on the connection.
*/
int
telnet_output_gmcp(telnet_t *tp, u_char *cp)
{
if (tp->t_flags & TF_GMCP)
{
telnet_send_sb(tp, TELOPT_GMCP, cp);
return 0;
}
return 1;
}
int
telnet_output_mssp(telnet_t *tp, mssp_t *vars[], size_t count)
{
nqueue_t *nq;
nq = tp->t_outq;
nq_putc(nq, IAC);
nq_putc(nq, SB);
nq_putc(nq, TELOPT_MSSP);
for (size_t i = 0; i < count; i++) {
nq_putc(nq, MSSP_VAR);
nq_puts(nq, vars[i]->name);
for (size_t k = 0; k < vars[i]->size; k++) {
nq_putc(nq, MSSP_VAL);
nq_puts(nq, vars[i]->values[k]);
}
}
nq_putc(nq, IAC);
nq_putc(nq, SE);
telnet_enabw(tp);
return 1;
}
/*
* Notify an interactive object that the Telnet session has been disconnected.
*/
static void
telnet_disconnect(telnet_t *tp)
{
if ((tp->t_flags & TF_ATTACH) == 0)
return;
tp->t_flags |= TF_DISCONNECT;
if (!tp->task)
tp->task = create_task(telnet_interactive, tp);
}
/*
* Append a character to the canonical queue.
*/
static void
telnet_canq_putc(telnet_t *tp, u_char c)
{
if (tp->t_flags & (TF_OVFLCANQ | TF_SYNCH))
return;
if (!nq_full(tp->t_canq))
{
nq_putc(tp->t_canq, c);
}
else
{
tp->t_flags |= TF_OVFLCANQ;
if (nq_avail(tp->t_outq) > 0)
{
nq_putc(tp->t_outq, BEL);
telnet_enabw(tp);
}
}
}
/*
* Append a character to the option queue.
*/
static void
telnet_optq_putc(telnet_t *tp, u_char c)
{
if (tp->t_flags & TF_OVFLOPTQ)
return;
if (!nq_full(tp->t_optq))
nq_putc(tp->t_optq, c);
else
tp->t_flags |= TF_OVFLOPTQ;
}
/*
* Send the contents of the canonical queue to the interactive object.
*/
static void
telnet_eol(telnet_t *tp)
{
if (tp->t_flags & TF_SYNCH)
return;
if (nq_full(tp->t_canq))
{
tp->t_flags &= ~TF_OVFLCANQ;
}
else
{
nq_putc(tp->t_canq, '\0');
}
tp->t_flags |= TF_INPUT;
}
static void
telnet_interactive(void *vp)
{
telnet_t *tp = vp;
char *cp;
if (!(tp->t_flags & TF_ATTACH)) {
tp->task = NULL;
telnet_shutdown(tp->t_nd, tp);
return;
}
if (tp->t_flags & TF_DISCONNECT) {
tp->t_flags &= ~TF_DISCONNECT;
if (tp->t_ip)
remove_interactive(tp->t_ip, 1);
}
if (!(tp->t_flags & TF_ATTACH)) {
tp->task = NULL;
telnet_shutdown(tp->t_nd, tp);
return;
}
if (tp->t_flags & TF_OVFLOUTQ) {
tp->task = NULL;
return;
}
if (tp->t_flags & TF_INPUT) {
tp->t_flags &= ~TF_INPUT;
cp = (char *)nq_rptr(tp->t_canq);
if (nq_full(tp->t_canq) &&
(nq_size(tp->t_canq) > (strlen(TRUNCATED) + 1))) {
cp += nq_size(tp->t_canq) - (strlen(TRUNCATED) + 1);
strcpy(cp, TRUNCATED);
cp = (char *)nq_rptr(tp->t_canq);
}
interactive_input(tp->t_ip, cp);
nq_init(tp->t_canq);
}
if (!(tp->t_flags & TF_ATTACH)) {
tp->task = NULL;
telnet_shutdown(tp->t_nd, tp);
return;
}
tp->t_flags &= ~TF_GA;
telnet_readbytes(tp->t_nd, tp);
telnet_input(tp);
if (!(tp->t_flags & TF_ATTACH)) {
tp->task = NULL;
telnet_shutdown(tp->t_nd, tp);
return;
}
if (tp->t_flags & (TF_INPUT|TF_DISCONNECT)) {
reschedule_task(tp->task);
return;
}
tp->task = NULL;
nd_enable(tp->t_nd, ND_R);
}
/*
* Process a Data Mark.
*/
static void
telnet_dm(telnet_t *tp)
{
if ((tp->t_flags & TF_URGENT) == 0)
tp->t_flags &= ~TF_SYNCH;
}
/*
* Process an Are You There.
*/
static void
telnet_ayt(telnet_t *tp)
{
char version[24];
snprintf(version, sizeof(version), "[%6.6s%02d]\r\n", GAME_VERSION, PATCH_LEVEL);
nq_puts(tp->t_outq, (u_char *)version);
telnet_enabw(tp);
}
/*
* Process an Erase Character/Backspace/Delete.
*/
static INLINE void
telnet_ec(telnet_t *tp)
{
if (tp->t_flags & (TF_OVFLCANQ | TF_SYNCH))
return;
if (!nq_empty(tp->t_canq))
nq_unputc(tp->t_canq);
}
/*
* Process an Erase Line.
*/
static void
telnet_el(telnet_t *tp)
{
if (tp->t_flags & (TF_OVFLCANQ | TF_SYNCH))
return;
nq_init(tp->t_canq);
}
/*
* Process a Subnegotiate Begin.
*/
static void
telnet_sb(telnet_t *tp, u_char opt)
{
if (tp->t_optq == NULL)
tp->t_optq = nq_alloc(TELNET_OPTQ_SIZE);
tp->t_opt = opt;
}
/*
* Process a Subnegotiate End.
*/
static void
telnet_se(telnet_t *tp)
{
if (!nq_full(tp->t_optq) && !nq_empty(tp->t_optq))
{
if (tp->t_opt == TELOPT_GMCP) {
nq_putc(tp->t_optq, '\0');
gmcp_input(tp->t_ip, (char *)nq_rptr(tp->t_optq));
}
}
tp->t_flags &= ~TF_OVFLOPTQ;
nq_init(tp->t_optq);
}
/*
* Return the option block pointer for the specified option. If we have no
* interest in ever negotiating the specified option, return NULL; i.e. it's
* not one we know about, or we do but we'd never want to enable it.
*/
static opt_t *
telnet_get_optp(telnet_t *tp, u_char opt)
{
switch (opt)
{
case TELOPT_ECHO:
return &tp->t_optb[OP_ECHO];
case TELOPT_SGA:
return &tp->t_optb[OP_SGA];
case TELOPT_CDM:
return &tp->t_optb[OP_CDM];
case TELOPT_GMCP:
return &tp->t_optb[OP_GMCP];
case TELOPT_MSSP:
return &tp->t_optb[OP_MSSP];
case TELOPT_CHARSET:
return &tp->t_optb[OP_CHARSET];
default:
return NULL;
}
}
/*
* Enable an option in the local-to-remote direction?
*/
static int
telnet_lenabp(telnet_t *tp, u_char opt)
{
switch (opt)
{
case TELOPT_ECHO:
return 1;
case TELOPT_SGA:
return 1;
case TELOPT_CDM:
return 1;
default:
return 0;
}
}
/*
* Enable an option in the remote-to-local direction?
*/
static int
telnet_renabp(telnet_t *tp, u_char opt)
{
switch (opt)
{
case TELOPT_SGA:
return 1;
default:
return 0;
}
}
/*
* Send IAC GA.
*/
static void
telnet_send_ga(telnet_t *tp)
{
nqueue_t *nq;
nq = tp->t_outq;
nq_putc(nq, IAC);
nq_putc(nq, GA);
telnet_enabw(tp);
}
/*
* Send IAC WILL <option>.
*/
static void
telnet_send_will(telnet_t *tp, u_char opt)
{
nqueue_t *nq;
nq = tp->t_outq;
nq_putc(nq, IAC);
nq_putc(nq, WILL);
nq_putc(nq, opt);
telnet_enabw(tp);
}
/*
* Send IAC WONT <option>.
*/
static void
telnet_send_wont(telnet_t *tp, u_char opt)
{
nqueue_t *nq;
nq = tp->t_outq;
nq_putc(nq, IAC);
nq_putc(nq, WONT);
nq_putc(nq, opt);
telnet_enabw(tp);
}
/*
* Send IAC DO <option>.
*/
static void
telnet_send_do(telnet_t *tp, u_char opt)
{
nqueue_t *nq;
nq = tp->t_outq;
nq_putc(nq, IAC);
nq_putc(nq, DO);
nq_putc(nq, opt);
telnet_enabw(tp);
}
/*
* Send IAC DONT <option>.
*/
static void
telnet_send_dont(telnet_t *tp, u_char opt)
{
nqueue_t *nq;
nq = tp->t_outq;
nq_putc(nq, IAC);
nq_putc(nq, DONT);
nq_putc(nq, opt);
telnet_enabw(tp);
}
/*
* Send IAC SB subnegotiation sequence
*/
static void
telnet_send_sb(telnet_t *tp, u_char opt, u_char *data)
{
nqueue_t *nq;
nq = tp->t_outq;
nq_putc(nq, IAC);
nq_putc(nq, SB);
nq_putc(nq, opt);
nq_puts(nq, data);
nq_putc(nq, IAC);
nq_putc(nq, SE);
telnet_enabw(tp);
}
/*
* Acknowledge enabling an option in the local-to-remote direction.
*/
static void
telnet_ack_lenab(telnet_t *tp, u_char opt)
{
switch (opt)
{
case TELOPT_SGA:
tp->t_flags |= TF_SGA;
break;
case TELOPT_GMCP:
tp->t_flags |= TF_GMCP;
break;
case TELOPT_MSSP:
break;
}
}
/*
* Acknowledge disabling an option in the local-to-remote direction.
*/
static void
telnet_ack_ldisab(telnet_t *tp, u_char opt)
{
switch (opt)
{
case TELOPT_SGA:
tp->t_flags &= ~TF_SGA;
break;
}
}
/*
* Acknowledge enabling an option in the remote-to-local direction.
*/
static void
telnet_ack_renab(telnet_t *tp, u_char opt)
{
}
/*
* Acknowledge disabling an option in the remote-to-local direction.
*/
static void
telnet_ack_rdisab(telnet_t *tp, u_char opt)
{
}
/*
* Negotiate enabling an option in the local-to-remote direction.
*/
static void
telnet_neg_lenab(telnet_t *tp, u_char opt)
{
opt_t *op;
op = telnet_get_optp(tp, opt);
if (op == NULL)
return;
switch (op->o_us)
{
case OS_NO:
op->o_us = OS_WANTYES;
telnet_send_will(tp, opt);
break;
case OS_YES:
break;
case OS_WANTNO:
switch (op->o_usq)
{
case OQ_EMPTY:
op->o_usq = OQ_OPPOSITE;
break;
case OQ_OPPOSITE:
break;
}
break;
case OS_WANTYES:
switch (op->o_usq)
{
case OQ_EMPTY:
break;
case OQ_OPPOSITE:
op->o_usq = OQ_EMPTY;
break;
}
break;
}
}
/*
* Negotiate disabling an option in the local-to-remote direction.
*/
static void
telnet_neg_ldisab(telnet_t *tp, u_char opt)
{
opt_t *op;
op = telnet_get_optp(tp, opt);
if (op == NULL)
return;
switch (op->o_us)
{
case OS_NO:
break;
case OS_YES:
op->o_us = OS_WANTNO;
telnet_send_wont(tp, opt);
break;
case OS_WANTNO:
switch (op->o_usq)
{
case OQ_EMPTY:
break;
case OQ_OPPOSITE:
op->o_usq = OQ_EMPTY;
break;
}
break;
case OS_WANTYES:
switch (op->o_usq)
{
case OQ_EMPTY:
op->o_usq = OQ_OPPOSITE;
break;
case OQ_OPPOSITE:
break;
}
break;
}
}
/*
* Enable Remote Echo.
*/
void
telnet_enable_echo(telnet_t *tp)
{
if (nq_avail(tp->t_outq) < 3)
return;
telnet_neg_lenab(tp, TELOPT_ECHO);
}
/*
* Disable Remote Echo.
*/
void
telnet_disable_echo(telnet_t *tp)
{
if (nq_avail(tp->t_outq) < 3)
return;
telnet_neg_ldisab(tp, TELOPT_ECHO);
}
/*
* Enable GMCP
*/
void
telnet_enable_gmcp(telnet_t *tp)
{
if (nq_avail(tp->t_outq) < 3)
return;
telnet_neg_lenab(tp, TELOPT_GMCP);
}
/*
* Disable GMCP
*/
void
telnet_disable_gmcp(telnet_t *tp)
{
if (nq_avail(tp->t_outq) < 3)
return;
telnet_neg_ldisab(tp, TELOPT_GMCP);
}
/*
* Enable MSSP
*/
void
telnet_enable_mssp(telnet_t *tp)
{
if (nq_avail(tp->t_outq) < 3)
return;
telnet_neg_lenab(tp, TELOPT_MSSP);
}
/*
* Disable MSSP
*/
void
telnet_disable_mssp(telnet_t *tp)
{
if (nq_avail(tp->t_outq) < 3)
return;
telnet_neg_ldisab(tp, TELOPT_MSSP);
}
/*
* Enable charset negotiation
*
* The way we do this is actually not a proper negotiation yet.
* What we do is send a single charset and hope the client accept it.
*
* This is done primarily to get mudlet to config the connection correctly.
* This should be replaced with the ability to actually convert charsets.
*/
void
telnet_enable_charset(telnet_t *tp)
{
if (nq_avail(tp->t_outq) < 3)
return;
if (NULL == default_charset)
return;
telnet_neg_lenab(tp, TELOPT_CHARSET);
}
void
telnet_output_charset(telnet_t *tp)
{
nqueue_t *nq;
nq = tp->t_outq;
if (nq_avail(nq) < (8 + strlen(default_charset)))
return;
nq_putc(nq, IAC);
nq_putc(nq, SB);
nq_putc(nq, TELOPT_CHARSET);
nq_putc(nq, CHARSET_REQUEST);
nq_putc(nq, ';');
nq_puts(nq, (u_char *)default_charset);
nq_putc(nq, IAC);
nq_putc(nq, SE);
telnet_enabw(tp);
}
/*
* Process IAC WILL <option>.
*/
static void
telnet_will(telnet_t *tp, u_char opt)
{
opt_t *op;
op = telnet_get_optp(tp, opt);
if (op == NULL)
{
telnet_send_dont(tp, opt);
return;
}
switch (op->o_him)
{
case OS_NO:
if (telnet_renabp(tp, opt))
{
op->o_him = OS_YES;
telnet_send_do(tp, opt);
telnet_ack_renab(tp, opt);
}
else
{
telnet_send_dont(tp, opt);
}
break;
case OS_YES: