aboutsummaryrefslogtreecommitdiff
path: root/src/telnet.c
blob: 038c54ca575cff1b8d9ee3acf7ed919b4edc207e (plain)
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
/*
 *   NetCosm - a MUD server
 *   Copyright (C) 2016 Franklin Wei
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "netcosm.h"

int telnet_handle_command(const unsigned char *buf)
{
    bool cmd = false;

    while(*buf)
    {
        unsigned char c = *buf;

        const struct telnet_cmd {
            int val;
            const char *name;
        } commands[] = {
            {  IAC,     "IAC"     },
            {  DONT,    "DONT"    },
            {  DO,      "DO"      },
            {  WONT,    "WONT"    },
            {  WILL,    "WILL"    },
            {  GA,      "GA"      },
            {  AYT,     "AYT"     },
            {  NOP,     "NOP"     },
            {  SB,      "SB"      },
            {  SE,      "SE"      },
            {  ECHO,    "ECHO"    },
            {  SGA,     "SGA"     },
            {  STATUS,  "STATUS"  },
            {  NAWS,    "NAWS"    }
        };

        for(unsigned int i = 0; i < ARRAYLEN(commands); ++i)
        {
            if(c == commands[i].val)
            {
                debugf("%s ", commands[i].name);
                cmd = true;
                goto found;
            }
        }
        switch(c)
        {
        case IP:
            return TELNET_EXIT;
        default:
            break;
        }
        debugf("???: %d ", c);
    found:

        ++buf;
    }

    if(cmd)
        debugf("\n");

    return TELNET_OK;
}

void telnet_echo_off(void)
{
    const unsigned char seq[] = {
        IAC, DONT, ECHO,
        IAC, WILL, ECHO,
    };
    out_raw(seq, ARRAYLEN(seq));
}

void telnet_echo_on(void)
{
    const unsigned char seq[] = {
        IAC, DO, ECHO,
        IAC, WONT, ECHO,
    };
    out_raw(seq, ARRAYLEN(seq));
}

void telnet_init(void)
{
    const unsigned char init_seq[] = {
        IAC, WONT, SGA,
        IAC, DONT, SGA,
        IAC, WONT, NAWS,
        IAC, DONT, NAWS,
        IAC, WONT, STATUS,
        IAC, DONT, STATUS,
        IAC, DO,   ECHO,
        IAC, WONT, ECHO,
        IAC, DONT, LINEMODE,
    };
    out_raw(init_seq, ARRAYLEN(init_seq));
}