forked from seanmiddleditch/libtelnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtelnet-chatd.c
373 lines (321 loc) · 8.9 KB
/
telnet-chatd.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
/*
* Sean Middleditch
*
* The author or authors of this code dedicate any and all copyright interest
* in this code to the public domain. We make this dedication for the benefit
* of the public at large and to the detriment of our heirs and successors. We
* intend this dedication to be an overt act of relinquishment in perpetuity of
* all present and future rights to this code under copyright law.
*/
#if !defined(_WIN32)
# if !defined(_BSD_SOURCE)
# define _BSD_SOURCE
# endif
# include <sys/socket.h>
# include <netinet/in.h>
# include <arpa/inet.h>
# include <netdb.h>
# include <poll.h>
# include <unistd.h>
# define SOCKET int
#else
# include <winsock2.h>
# include <ws2tcpip.h>
#ifndef _UCRT
# define snprintf _snprintf
#endif
# define poll WSAPoll
# define close closesocket
# define strdup _strdup
# if !defined(ECONNRESET)
# define ECONNRESET WSAECONNRESET
# endif
#endif
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include "libtelnet.h"
#define MAX_USERS 64
#define LINEBUFFER_SIZE 256
static const telnet_telopt_t telopts[] = {
{ TELNET_TELOPT_COMPRESS2, TELNET_WILL, TELNET_DONT },
{ -1, 0, 0 }
};
struct user_t {
char *name;
SOCKET sock;
telnet_t *telnet;
char linebuf[256];
int linepos;
};
static struct user_t users[MAX_USERS];
static void linebuffer_push(char *buffer, size_t size, int *linepos,
char ch, void (*cb)(const char *line, size_t overflow, void *ud),
void *ud) {
/* CRLF -- line terminator */
if (ch == '\n' && *linepos > 0 && buffer[*linepos - 1] == '\r') {
/* NUL terminate (replaces \r in buffer), notify app, clear */
buffer[*linepos - 1] = 0;
cb(buffer, 0, ud);
*linepos = 0;
/* CRNUL -- just a CR */
} else if (ch == 0 && *linepos > 0 && buffer[*linepos - 1] == '\r') {
/* do nothing, the CR is already in the buffer */
/* anything else (including technically invalid CR followed by
* anything besides LF or NUL -- just buffer if we have room
* \r
*/
} else if (*linepos != (int)size) {
buffer[(*linepos)++] = ch;
/* buffer overflow */
} else {
/* terminate (NOTE: eats a byte), notify app, clear buffer */
buffer[size - 1] = 0;
cb(buffer, size - 1, ud);
*linepos = 0;
}
}
static void _message(const char *from, const char *msg) {
int i;
for (i = 0; i != MAX_USERS; ++i) {
if (users[i].sock != -1) {
telnet_printf(users[i].telnet, "%s: %s\n", from, msg);
}
}
}
static void _send(SOCKET sock, const char *buffer, size_t size) {
int rs;
/* ignore on invalid socket */
if (sock == -1)
return;
/* send data */
while (size > 0) {
if ((rs = send(sock, buffer, (int)size, 0)) == -1) {
if (errno != EINTR && errno != ECONNRESET) {
fprintf(stderr, "send() failed: %s\n", strerror(errno));
exit(1);
} else {
return;
}
} else if (rs == 0) {
fprintf(stderr, "send() unexpectedly returned 0\n");
exit(1);
}
/* update pointer and size to see if we've got more to send */
buffer += rs;
size -= rs;
}
}
/* process input line */
static void _online(const char *line, size_t overflow, void *ud) {
struct user_t *user = (struct user_t*)ud;
int i;
(void)overflow;
/* if the user has no name, this is his "login" */
if (user->name == 0) {
/* must not be empty, must be at least 32 chars */
if (strlen(line) == 0 || strlen(line) > 32) {
telnet_printf(user->telnet, "Invalid name.\nEnter name: ");
return;
}
/* must not already be in use */
for (i = 0; i != MAX_USERS; ++i) {
if (users[i].name != 0 && strcmp(users[i].name, line) == 0) {
telnet_printf(user->telnet, "Name in use.\nEnter name: ");
return;
}
}
/* keep name */
user->name = strdup(line);
telnet_printf(user->telnet, "Welcome, %s!\n", line);
return;
}
/* if line is "quit" then, well, quit */
if (strcmp(line, "quit") == 0) {
close(user->sock);
user->sock = -1;
_message(user->name, "** HAS QUIT **");
free(user->name);
user->name = 0;
telnet_free(user->telnet);
return;
}
/* just a message -- send to all users */
_message(user->name, line);
}
static void _input(struct user_t *user, const char *buffer,
size_t size) {
unsigned int i;
for (i = 0; user->sock != -1 && i != size; ++i)
linebuffer_push(user->linebuf, sizeof(user->linebuf), &user->linepos,
(char)buffer[i], _online, user);
}
static void _event_handler(telnet_t *telnet, telnet_event_t *ev,
void *user_data) {
struct user_t *user = (struct user_t*)user_data;
switch (ev->type) {
/* data received */
case TELNET_EV_DATA:
_input(user, ev->data.buffer, ev->data.size);
telnet_negotiate(telnet, TELNET_WONT, TELNET_TELOPT_ECHO);
telnet_negotiate(telnet, TELNET_WILL, TELNET_TELOPT_ECHO);
break;
/* data must be sent */
case TELNET_EV_SEND:
_send(user->sock, ev->data.buffer, ev->data.size);
break;
/* enable compress2 if accepted by client */
case TELNET_EV_DO:
if (ev->neg.telopt == TELNET_TELOPT_COMPRESS2)
telnet_begin_compress2(telnet);
break;
/* error */
case TELNET_EV_ERROR:
close(user->sock);
user->sock = -1;
if (user->name != 0) {
_message(user->name, "** HAS HAD AN ERROR **");
free(user->name);
user->name = 0;
}
telnet_free(user->telnet);
break;
default:
/* ignore */
break;
}
}
int main(int argc, char **argv) {
char buffer[512];
short listen_port;
SOCKET listen_sock;
SOCKET client_sock;
int rs;
int i;
struct sockaddr_in addr;
socklen_t addrlen;
struct pollfd pfd[MAX_USERS + 1];
/* initialize Winsock */
#if defined(_WIN32)
WSADATA wsd;
WSAStartup(MAKEWORD(2, 2), &wsd);
#endif
/* check usage */
if (argc != 2) {
fprintf(stderr, "Usage:\n ./telnet-chatd <port>\n");
return 1;
}
/* initialize data structures */
memset(&pfd, 0, sizeof(pfd));
memset(users, 0, sizeof(users));
for (i = 0; i != MAX_USERS; ++i)
users[i].sock = -1;
/* parse listening port */
listen_port = (short)strtol(argv[1], 0, 10);
/* create listening socket */
if ((listen_sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
fprintf(stderr, "socket() failed: %s\n", strerror(errno));
return 1;
}
/* reuse address option */
rs = 1;
setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR, (char*)&rs, sizeof(rs));
/* bind to listening addr/port */
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = INADDR_ANY;
addr.sin_port = htons(listen_port);
if (bind(listen_sock, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
fprintf(stderr, "bind() failed: %s\n", strerror(errno));
close(listen_sock);
return 1;
}
/* listen for clients */
if (listen(listen_sock, 5) == -1) {
fprintf(stderr, "listen() failed: %s\n", strerror(errno));
close(listen_sock);
return 1;
}
printf("LISTENING ON PORT %d\n", listen_port);
/* initialize listening descriptors */
pfd[MAX_USERS].fd = listen_sock;
pfd[MAX_USERS].events = POLLIN;
/* loop for ever */
for (;;) {
/* prepare for poll */
for (i = 0; i != MAX_USERS; ++i) {
if (users[i].sock != -1) {
pfd[i].fd = users[i].sock;
pfd[i].events = POLLIN;
} else {
pfd[i].fd = -1;
pfd[i].events = 0;
}
}
/* poll */
rs = poll(pfd, MAX_USERS + 1, -1);
if (rs == -1 && errno != EINTR) {
fprintf(stderr, "poll() failed: %s\n", strerror(errno));
close(listen_sock);
return 1;
}
/* new connection */
if (pfd[MAX_USERS].revents & (POLLIN | POLLERR | POLLHUP)) {
/* acept the sock */
addrlen = sizeof(addr);
if ((client_sock = accept(listen_sock, (struct sockaddr *)&addr,
&addrlen)) == -1) {
fprintf(stderr, "accept() failed: %s\n", strerror(errno));
return 1;
}
printf("Connection received.\n");
/* find a free user */
for (i = 0; i != MAX_USERS; ++i)
if (users[i].sock == -1)
break;
if (i == MAX_USERS) {
printf(" rejected (too many users)\n");
_send(client_sock, "Too many users.\r\n", 14);
close(client_sock);
}
/* init, welcome */
users[i].sock = client_sock;
users[i].telnet = telnet_init(telopts, _event_handler, 0,
&users[i]);
telnet_negotiate(users[i].telnet, TELNET_WILL,
TELNET_TELOPT_COMPRESS2);
telnet_printf(users[i].telnet, "Enter name: ");
telnet_negotiate(users[i].telnet, TELNET_WILL, TELNET_TELOPT_ECHO);
}
/* read from client */
for (i = 0; i != MAX_USERS; ++i) {
/* skip users that aren't actually connected */
if (users[i].sock == -1)
continue;
if (pfd[i].revents & (POLLIN | POLLERR | POLLHUP)) {
if ((rs = recv(users[i].sock, buffer, sizeof(buffer), 0)) > 0) {
telnet_recv(users[i].telnet, buffer, rs);
} else if (rs == 0) {
printf("Connection closed.\n");
close(users[i].sock);
users[i].sock = -1;
if (users[i].name != 0) {
_message(users[i].name, "** HAS DISCONNECTED **");
free(users[i].name);
users[i].name = 0;
}
telnet_free(users[i].telnet);
} else if (errno != EINTR) {
fprintf(stderr, "recv(client) failed: %s\n",
strerror(errno));
exit(1);
}
}
}
}
/* not that we can reach this, but GCC will cry if it's not here */
return 0;
}