-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathtelnet_server.c.disabled
53 lines (41 loc) · 1.14 KB
/
telnet_server.c.disabled
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
#include <stdio.h>
#include <string.h>
#include <uvm/syscalls.h>
#include <uvm/utils.h>
u64 listen_sock;
void on_new_conn(u64 socket_id)
{
puts("got new connection");
char client_addr[128];
u64 conn_sock = net_accept(socket_id, client_addr, sizeof(client_addr), on_incoming_data);
printf("client address: %s\n", client_addr);
}
void on_incoming_data(u64 socket_id, u64 num_bytes)
{
printf("received %d bytes of incoming data\n", num_bytes);
char read_buf[1024];
memset(read_buf, 0, 1024);
net_read(socket_id, read_buf, 1024 - 1);
// If this is a telnet command (non-printable), ignore it
if (read_buf[0] & 0x80)
{
return;
}
puts(read_buf);
if (strncmp(read_buf, "exit", 4) == 0)
{
puts("got exit command");
char* response = "Goodbye!\n";
net_write(socket_id, response, strlen(response));
net_close(socket_id);
return;
}
char* response = "Hello!\n";
net_write(socket_id, response, strlen(response));
}
void main()
{
puts("starting TCP server");
listen_sock = net_listen("127.0.0.1:9000", on_new_conn);
enable_event_loop();
}