Skip to content

Commit 97715be

Browse files
committed
TELNET server and client.
1 parent 6363ea1 commit 97715be

File tree

4 files changed

+252
-1
lines changed

4 files changed

+252
-1
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@ src/libncp.a
55
src/ping
66
src/finger
77
src/finser
8+
src/telnet
9+
src/telser
810
test/*.log

src/Makefile

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ CFLAGS=-g -Wall
22

33
NCP=-L. -lncp
44

5-
all: ncp ping finger finser
5+
all: ncp ping finger finser telnet telser
66

77
ncp: ncp.o imp.o
88

@@ -18,3 +18,9 @@ finger: finger.o libncp.a
1818

1919
finser: finser.o libncp.a
2020
$(CC) -o $@ $< $(NCP)
21+
22+
telnet: telnet.o libncp.a
23+
$(CC) -o $@ $< $(NCP)
24+
25+
telser: telser.o libncp.a
26+
$(CC) -o $@ $< $(NCP)

src/telnet.c

Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
#include <stdio.h>
2+
#include <errno.h>
3+
#include <stdlib.h>
4+
#include <unistd.h>
5+
#include <string.h>
6+
#include "ncp.h"
7+
8+
#define IAC 0377
9+
#define DONT 0376
10+
#define DO 0375
11+
#define WONT 0374
12+
#define WILL 0373
13+
#define SB 0372
14+
#define GA 0371
15+
#define EL 0370
16+
#define EC 0367
17+
#define AYT 0366
18+
#define AO 0365
19+
#define IP 0364
20+
#define BRK 0363
21+
#define MARK 0362
22+
#define NOP 0361
23+
#define SE 0360
24+
25+
static void option (unsigned char c)
26+
{
27+
switch (c) {
28+
case 000:
29+
fprintf (stderr, "[Binary Transmission]");
30+
break;
31+
case 001:
32+
fprintf (stderr, "[Echo]");
33+
break;
34+
case 002:
35+
fprintf (stderr, "[Reconnection]");
36+
break;
37+
case 003:
38+
fprintf (stderr, "[Suppress Go Ahead]");
39+
break;
40+
case 004:
41+
fprintf (stderr, "[Approx Message Size Negotiation]");
42+
break;
43+
case 005:
44+
fprintf (stderr, "[Status]");
45+
break;
46+
case 006:
47+
fprintf (stderr, "[Timing Mark]");
48+
break;
49+
case 007:
50+
fprintf (stderr, "[Remote Controlled Trans and Echo]");
51+
break;
52+
case 021:
53+
fprintf (stderr, "[Extended Ascii]");
54+
break;
55+
case 025:
56+
fprintf (stderr, "[Supdup]");
57+
break;
58+
case 026:
59+
fprintf (stderr, "[Supdup Output]");
60+
break;
61+
default:
62+
fprintf (stderr, "[Unknown Option]");
63+
break;
64+
}
65+
}
66+
67+
static int special (unsigned char c, const unsigned char *data)
68+
{
69+
switch (c) {
70+
case IAC:
71+
write (1, &c, 1);
72+
return 0;
73+
case DONT:
74+
fprintf (stderr, "[DONT]");
75+
option (*data);
76+
return 1;
77+
case DO:
78+
fprintf (stderr, "[DO]");
79+
option (*data);
80+
return 1;
81+
case WONT:
82+
fprintf (stderr, "[WONT]");
83+
option (*data);
84+
return 1;
85+
case WILL:
86+
fprintf (stderr, "[WILL]");
87+
option (*data);
88+
return 1;
89+
case SB:
90+
fprintf (stderr, "[SB]");
91+
return 0;
92+
case GA:
93+
fprintf (stderr, "[GA]");
94+
return 0;
95+
case EL:
96+
fprintf (stderr, "[EL]");
97+
return 0;
98+
case EC:
99+
fprintf (stderr, "[EC]");
100+
write (1, "\b \b", 3);
101+
return 0;
102+
case AYT:
103+
fprintf (stderr, "[AYT]");
104+
return 0;
105+
case AO:
106+
fprintf (stderr, "[AO]");
107+
return 0;
108+
case IP:
109+
fprintf (stderr, "[IP]");
110+
return 0;
111+
case BRK:
112+
fprintf (stderr, "[BRK]");
113+
return 0;
114+
case MARK:
115+
fprintf (stderr, "[MARK]");
116+
return 0;
117+
case NOP:
118+
fprintf (stderr, "[NOP]");
119+
return 0;
120+
case SE:
121+
fprintf (stderr, "[SE]");
122+
return 0;
123+
default:
124+
fprintf (stderr, "[Unknown Option]");
125+
return 0;
126+
}
127+
}
128+
129+
static void process (const unsigned char *data, int size)
130+
{
131+
int i;
132+
133+
for (i = 0; i < size; i++) {
134+
switch (data[i]) {
135+
case NUL:
136+
break;
137+
case IAC:
138+
i += special (data[i+1], &data[i+2]);
139+
break;
140+
default:
141+
write (1, &data[i], 1);
142+
break;
143+
}
144+
}
145+
146+
int main (int argc, char **argv)
147+
{
148+
char options[] = {
149+
IAC, DO, 001,
150+
IAC, DO, 003,
151+
IAC, WILL, 003
152+
};
153+
unsigned char reply[1000];
154+
int host, connection, size;
155+
156+
if (argc != 2) {
157+
fprintf (stderr, "Usage: %s host\n", argv[0]);
158+
exit (1);
159+
}
160+
161+
host = atoi (argv[1]);
162+
163+
if (ncp_init (NULL) == -1) {
164+
fprintf (stderr, "NCP initializtion error: %s.\n", strerror (errno));
165+
if (errno == ECONNREFUSED)
166+
fprintf (stderr, "Is the NCP server started?\n");
167+
exit (1);
168+
}
169+
170+
printf ("TELNET to host %03o.\n", host);
171+
172+
switch (ncp_open (host, 23, &connection)) {
173+
case 0:
174+
break;
175+
case -1:
176+
default:
177+
fprintf (stderr, "NCP open error.\n");
178+
exit (1);
179+
case -2:
180+
fprintf (stderr, "Open refused.\n");
181+
exit (1);
182+
}
183+
184+
if (ncp_write (connection, options, strlen (options)) == -1) {
185+
fprintf (stderr, "NCP write error.\n");
186+
exit (1);
187+
}
188+
189+
size = sizeof reply;
190+
if (ncp_read (connection, reply, &size) == -1) {
191+
fprintf (stderr, "NCP read error.\n");
192+
exit (1);
193+
}
194+
195+
process (reply, size);
196+
197+
if (ncp_close (connection) == -1) {
198+
fprintf (stderr, "NCP close error.\n");
199+
exit (1);
200+
}
201+
}

src/telser.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include <stdio.h>
2+
#include <errno.h>
3+
#include <stdlib.h>
4+
#include <unistd.h>
5+
#include <string.h>
6+
#include "ncp.h"
7+
8+
int main (int argc, char **argv)
9+
{
10+
char *command;
11+
char reply[1000];
12+
int host, connection, size;
13+
14+
if (argc != 1) {
15+
fprintf (stderr, "Usage: %s\n", argv[0]);
16+
exit (1);
17+
}
18+
19+
if (ncp_init (NULL) == -1) {
20+
fprintf (stderr, "NCP initializtion error: %s.\n", strerror (errno));
21+
if (errno == ECONNREFUSED)
22+
fprintf (stderr, "Is the NCP server started?\n");
23+
exit (1);
24+
}
25+
26+
if (ncp_listen (23, &host, &connection) == -1) {
27+
fprintf (stderr, "NCP listen error.\n");
28+
exit (1);
29+
}
30+
31+
command = "Welcome to Unix.\r\n";
32+
if (ncp_write (connection, command, strlen (command)) == -1) {
33+
fprintf (stderr, "NCP write error.\n");
34+
exit (1);
35+
}
36+
37+
if (ncp_close (connection) == -1) {
38+
fprintf (stderr, "NCP close error.\n");
39+
exit (1);
40+
}
41+
42+
}

0 commit comments

Comments
 (0)