-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoverlord.c
279 lines (213 loc) · 6.43 KB
/
overlord.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
#include "util.h"
#include "overlist.h"
#define NUM_GROUPS 10
#define LOGIC_EXIT 0
#define LOGIC_DEFAULT 1
#define LOGIC_ORDER 2
#define LOGIC_MANAGE 3
#define WAIT_DEFAULT 0
#define WAIT_CONNECT 1
#define WAIT_NETWORK 2
#define WAIT_COMMAND 3
char *input;
char message[MAX_MESSAGE_LENGTH];
int sock;
client_list *underlings;
client_list *groups[NUM_GROUPS];
volatile sig_atomic_t logicState;
volatile sig_atomic_t waitState;
sigjmp_buf sigjmps[WAIT_COMMAND];
void cleanup() {
if (input) {
free(input);
}
if (underlings) {
freeClientList(underlings);
}
if (sock >= 0) {
close(sock);
}
}
static void sighandler(int signo) {
if (signo == SIGINT) {
if (waitState == WAIT_COMMAND) {
cleanup();
exit(1);
} else if (waitState == WAIT_NETWORK) {
//printf("Writing sigint\n");
char data = 3;
write(sock, &data, MAX_MESSAGE_LENGTH);
siglongjmp(sigjmps[WAIT_DEFAULT], 1);
}
siglongjmp(sigjmps[waitState], 1);
}
}
int serverSocket(int port) {
int sock = socket(AF_INET, SOCK_STREAM, 0);
if (checkError(sock, "socket") < 0) {
// error handling
return -1;
}
int enable = 1;
int sockoptStatus = setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(enable));
if (checkError(sockoptStatus, "setsockopt") < 0) {
return -1;
}
sockoptStatus = setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, &enable, sizeof(enable));
if (checkError(sockoptStatus, "setsockopt") < 0) {
return -1;
}
struct sockaddr_in sock_struct;
sock_struct.sin_family = AF_INET;
sock_struct.sin_addr.s_addr = INADDR_ANY;
sock_struct.sin_port = htons(port);
int bindStatus = bind(sock, (struct sockaddr *) &sock_struct, sizeof(sock_struct));
if (checkError(bindStatus, "bind") < 0) {
// error handling
return -1;
}
return sock;
}
int serverConnect(int sock) {
int listenStatus = listen(sock, 1);
if (checkError(listenStatus, "listen") < 0) {
return -1;
}
if (sigsetjmp(sigjmps[WAIT_CONNECT], 1) != 0) {
return -1;
}
struct sockaddr_in sock_struct;
socklen_t sock_struct_len = sizeof(sock_struct);
int connection = accept(sock, (struct sockaddr *) &sock_struct, &sock_struct_len);
if (checkError(connection, "accept") < 0) {
return -1;
}
printf("Connected to %s\n", inet_ntoa(sock_struct.sin_addr));
return connection;
}
int main() {
input = NULL;
sock = -1;
printf("Welcome, overlord.\n");
printf("Type help for available commands.\n");
underlings = newClientList();
logicState = LOGIC_DEFAULT;
waitState = WAIT_DEFAULT;
signal(SIGINT, sighandler);
int index = -1;
while (sigsetjmp(sigjmps[WAIT_DEFAULT], 1) != 0);
while (logicState) {
switch (logicState) {
case LOGIC_DEFAULT:
waitState = WAIT_COMMAND;
prompt(&input, "[DEFAULT]> ", 1);
waitState = WAIT_DEFAULT;
if (!input || equals(input, "exit")) {
logicState = LOGIC_EXIT;
} else if (equals(input, "help")) {
printf("Type 'manage' to manage underlings.\n");
printf("Type 'order' to give orders to underlings.\n");
printf("Type 'exit' to exit.\n");
} else if (equals(input, "order")) {
logicState = LOGIC_ORDER;
} else if (equals(input, "manage")) {
logicState = LOGIC_MANAGE;
}
break;
case LOGIC_ORDER:
if (!input) {
logicState = LOGIC_DEFAULT;
break;
}
if (underlings->size == 0) {
printf("No underlings available.\n");
} else if (index < 0 || index >= underlings->size) {
printClientList(underlings);
prompt(&input, "Enter underling ID: ", 0);
sscanf(input, "%d", &index);
if (index < 0 || index >= underlings->size) {
printf("Invalid ID!\n");
}
} else {
client_node *underling = underlings->list[index];
printf("[OVERLORD] ");
prompt(&input, underling->prefix, 0);
if (equals(input, "exit")) {
logicState = LOGIC_DEFAULT;
index = -1;
} else {
strncpy(message, input, MAX_MESSAGE_LENGTH);
// write command
write(underling->sock, message, MAX_MESSAGE_LENGTH);
waitState = WAIT_NETWORK;
// read command output
readMessage(underling->sock);
// print command output
printf("%s", message);
// read next prompt
readMessage(underling->sock);
strcpy(underling->prefix, message);
}
}
break;
case LOGIC_MANAGE:
waitState = WAIT_COMMAND;
prompt(&input, "[MANAGE]> ", 1);
waitState = WAIT_DEFAULT;
if (!input || equals(input, "back")) {
logicState = LOGIC_DEFAULT;
} else if (equals(input, "help")) {
printf("Type 'back' or Control-C to return to the default state.\n");
printf("Type 'add' to add underlings.\n");
printf("Type 'view' to see current underlings.\n");
printf("Type 'remove' to remove underlings.\n");
} else if (equals(input, "add")) {
sock = -1;
int port = -1;
while (sock < 0) {
waitState = WAIT_DEFAULT;
prompt(&input, "Enter port to listen on (default 5001): ", 0);
if (!input) {
exit(1);
}
if (*input) {
sscanf(input, "%d", &port);
if (port < 0) {
printf("Invalid port!\n");
continue;
}
} else {
port = PORT;
}
sock = serverSocket(port);
}
waitState = WAIT_CONNECT;
printf("Waiting for underling to connect...\n");
int connection = serverConnect(sock);
close(sock);
if (connection >= 0) {
printf("Underling connected.\n");
readMessage(connection);
prompt(&input, "Enter a description for this underling: ", 0);
addClientToList(underlings, connection, input, message);
}
} else if (equals(input, "remove")) {
printClientList(underlings);
int id = -1;
prompt(&input, "Enter ID to remove: ", 0);
sscanf(input, "%d", &id);
if (id < 0 || id > underlings->size) {
printf("Invalid ID!\n");
} else {
removeClientFromList(underlings, id);
}
} else if (equals(input, "view")) {
printClientList(underlings);
}
break;
default:
break;
}
}
return 0;
}