-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathprocess_stdin.c
67 lines (61 loc) · 1.38 KB
/
process_stdin.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
#include "main.h"
void print_connection(int fd, const char* prologue, const char* epilogue) {
int peerfd = fdinfo[fd].peerfd;
printf("%s%s:%d -> ",
prologue,
inet_ntoa(fdinfo[fd].address.sin_addr),
ntohs(fdinfo[fd].address.sin_port)
);
printf("%s:%d [%d->%d]",
inet_ntoa(fdinfo[peerfd].address.sin_addr),
ntohs(fdinfo[peerfd].address.sin_port),
fd,
peerfd);
if (fdinfo[fd].total_read || fdinfo[peerfd].total_read) {
printf(" %lld:%lld",
fdinfo[fd].total_read,
fdinfo[peerfd].total_read);
}
printf("%s", epilogue);
}
void list_connections() {
int fd;
for(fd=0; fd<MAXFD; ++fd) {
if(fdinfo[fd].status == 0 || fdinfo[fd].status == '.') {
continue;
}
if(fdinfo[fd].group != 'c') {
continue;
}
print_connection(fd, " ", "\n");
}
}
void process_stdin() {
dpf("processing stdin\n");
char cmd[256]={0};
char arg[256]={0};
scanf("%256s%256s", cmd, arg);
dpf(" c=\"%s\" arg=\"%s\"\n", cmd, arg);
switch(cmd[0]) {
case 'q':
exit(0);
break;
case 'l':
list_connections();
break;
case 'k':
if(!arg[0]) {
printf("k[ill] fd_number\n");
} else {
close_fd(atoi(arg));
}
break;
case 'D':
debug_output = !debug_output;
break;
case '\0':
break;
default:
printf("Commands: quit list kill Debug\n");
}
}