/
ap.c
395 lines (345 loc) · 10.4 KB
/
ap.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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <stdbool.h>
#include <math.h>
#include <assert.h>
#include <stdint.h>
#include <errno.h>
#include <zlib.h>
#include <termios.h>
#include <signal.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include "parse.h"
#include "shared.h"
#define CLIENT_PATH "./client"
typedef struct station_s station_s;
struct station_s
{
pid_t pid;
int pipe[2];
};
sym_table_s station_table;
pthread_mutex_t station_table_lock = PTHREAD_MUTEX_INITIALIZER;
static int shm_mediums;
static int shm_mediumc;
static void process_tasks(void);
static void create_node(char *id, char *ifs);
static void send_message(send_s *send);
static void *process_request(void *);
static void kill_child(station_s *s);
static void kill_childid(char *id);
static void send_ack_cts(char *addr1, int type);
static void deliver_message(char *addr1, char *addr2, char *payload, size_t size);
static void sigUSR1(int sig);
static void sigUSR2(int sig);
int main(int argc, char *argv[])
{
int c, status;
char *src;
buf_s *in;
struct sigaction sa;
sym_record_s *rec, *recb;
pthread_t req_thread;
sigset_t mask;
if(argc > 1) {
src = readfile(argv[1]);
parse(src);
closefile();
}
else {
src = readfile("test");
parse(src);
closefile();
}
name = "ap";
name_stripped = "ap";
name_len = sizeof("ap")-1;
if(access("out/", F_OK)) {
if(errno == ENOENT)
mkdir("out", S_IRWXU);
else {
perror("Directory Access");
exit(EXIT_FAILURE);
}
}
logfile = fopen("out/ap", "w");
if(!logfile) {
perror("Error Creating file for redirection");
exit(EXIT_FAILURE);
}
sa.sa_handler = sigUSR1;
sa.sa_flags = SA_RESTART;
sigemptyset(&sa.sa_mask);
status = sigaction(SIGUSR1, &sa, NULL);
if(status < 0) {
perror("Error installing handler for SIGUSR1");
exit(EXIT_FAILURE);
}
sa.sa_handler = sigALARM;
sa.sa_flags = 0;
sigemptyset(&sa.sa_mask);
status = sigaction(SIGALRM, &sa, NULL);
if(status < 0) {
perror("Error installing handler for SIGTERM");
exit(EXIT_FAILURE);
}
shm_mediums = shmget(SHM_KEY_S, sizeof(*mediums), IPC_CREAT|SHM_R|SHM_W);
if(shm_mediums < 0) {
perror("Failed to set up shared memory segment");
exit(EXIT_FAILURE);
}
mediums = shmat(shm_mediums, NULL, 0);
if(mediums == (medium_s *)-1) {
perror("Failed to attached shared memory segment.");
exit(EXIT_FAILURE);
}
mediums->isbusy = false;
shm_mediumc = shmget(SHM_KEY_C, sizeof(*mediumc), IPC_CREAT|SHM_R|SHM_W);
if(shm_mediumc < 0) {
perror("Failed to set up shared memory segment");
exit(EXIT_FAILURE);
}
mediumc = shmat(shm_mediumc, NULL, 0);
if(mediumc == (medium_s *)-1) {
perror("Failed to attached shared memory segment.");
exit(EXIT_FAILURE);
}
mediumc->isbusy = false;
status = pthread_create(&req_thread, NULL, process_request, NULL);
if(status) {
perror("Failure to set up thread");
exit(EXIT_FAILURE);
}
sigemptyset(&mask);
sigaddset(&mask, SIGUSR2);
status = pthread_sigmask(SIG_BLOCK, &mask, NULL);
if(status) {
perror("Failure to mask SIGUSR2 in parent thread.");
exit(EXIT_FAILURE);
}
process_tasks();
in = buf_init();
/* Get command line input */
printf("> ");
while((c = getchar()) != EOF) {
buf_addc(&in, c);
if(c == '\n') {
buf_addc(&in, '\0');
parse(in->buf);
process_tasks();
buf_reset(&in);
printf("> ");
}
}
buf_free(in);
pthread_mutex_lock(&station_table_lock);
/* kill all children */
for(c = 0; c < SYM_TABLE_SIZE; c++) {
rec = station_table.table[c];
while(rec) {
kill_child(rec->data.ptr);
recb = rec->next;
free(rec);
rec = recb;
}
}
pthread_mutex_unlock(&station_table_lock);
pthread_mutex_destroy(&station_table_lock);
fclose(logfile);
exit(EXIT_SUCCESS);
}
void process_tasks(void)
{
task_s *t;
while((t = task_dequeue())) {
switch(t->func) {
case FNET_NODE:
create_node(*(char **)(t + 1), *((char **)(t + 1) + 1));
break;
case FNET_SEND:
send_message((send_s *)t);
break;
case FNET_KILL:
kill_childid(*(char **)(t + 1));
default:
break;
}
free(t);
}
}
void create_node(char *id, char *ifs)
{
int status;
pid_t pid;
char *argv[5];
char fd_buf[4*sizeof(int)+4];
int fd[2];
station_s *station;
pthread_mutex_lock(&station_table_lock);
if(!sym_lookup(&station_table, id)) {
status = pipe(fd);
if(status < 0) {
perror("Error Creating Pipe");
exit(EXIT_FAILURE);
}
sprintf(fd_buf, "%d.%d", fd[0], fd[1]);
argv[0] = CLIENT_PATH;
argv[1] = id;
argv[2] = ifs;
argv[3] = fd_buf;
argv[4] = NULL;
pid = fork();
if(pid) {
/* wait for SIGUSR1 from child */
pause();
station = alloc(sizeof(*station));
station->pid = pid;
station->pipe[0] = fd[0];
station->pipe[1] = fd[1];
sym_insert(&station_table, id, (sym_data_u){.ptr = station});
}
else if(pid < 0) {
perror("Failed to create station process.");
exit(EXIT_FAILURE);
}
else {
status = execv(CLIENT_PATH, argv);
}
}
pthread_mutex_unlock(&station_table_lock);
}
void send_message(send_s *send)
{
sym_record_s *rec;
station_s *station;
size_t dlen, plen;
pthread_mutex_lock(&station_table_lock);
rec = sym_lookup(&station_table, send->src);
if(rec) {
dlen = strlen(send->dst);
plen = strlen(send->period);
station = rec->data.ptr;
write(station->pipe[1], &send->super.func, sizeof(send->super.func));
write(station->pipe[1], &dlen, sizeof(dlen));
write(station->pipe[1], send->dst, strlen(send->dst));
write(station->pipe[1], &send->size, sizeof(send->size));
write(station->pipe[1], send->payload, send->size);
write(station->pipe[1], &plen, sizeof(plen));
write(station->pipe[1], send->period, plen);
write(station->pipe[1], &send->repeat, sizeof(send->repeat));
kill(station->pid, SIGUSR1);
}
pthread_mutex_unlock(&station_table_lock);
}
void kill_child(station_s *s)
{
close(s->pipe[0]);
close(s->pipe[1]);
kill(s->pid, SIGTERM);
}
void kill_childid(char *id)
{
sym_record_s *rec;
pthread_mutex_lock(&station_table_lock);
rec = sym_lookup(&station_table, id);
if(rec) {
kill_child(rec->data.ptr);
sym_delete(&station_table, id);
}
pthread_mutex_unlock(&station_table_lock);
}
void *process_request(void *arg)
{
ssize_t status;
union {
rts_s rts;
cts_ack_s ctack;
}data;
char *payload;
uint32_t checksum, *checkptr;
while(true) {
status = slowread(mediums, &data, sizeof(data));
mediums->size = 0;
if(status == EINTR) {
set_busy(mediums, false);
logevent("Timed out session");
}
else {
set_busy(mediums, true);
if(data.rts.FC & RTS_SUBTYPE) {
checksum = (uint32_t)crc32(CRC_POLYNOMIAL, (Bytef *)&data.rts, sizeof(data.rts)-sizeof(uint32_t));
if(checksum == data.rts.FCS) {
send_ack_cts(data.rts.addr1, CTS_SUBTYPE);
payload = alloc(data.rts.D+sizeof(uint32_t));
status = slowread(mediums, payload, data.rts.D + sizeof(uint32_t));
mediums->size = 0;
if(status == EINTR) {
logevent("timed out waiting on payload");
}
else {
checksum = (uint32_t)crc32(CRC_POLYNOMIAL, (Bytef *)payload, data.rts.D);
checkptr = (uint32_t *)&payload[data.rts.D];
if(checksum == *checkptr) {
deliver_message(data.rts.addr1, data.rts.addr2, payload, data.rts.D);
send_ack_cts(data.rts.addr1, ACK_SUBTYPE);
}
else {
logevent("Checksum Validation FAiled for payload");
}
}
free(payload);
}
else {
logevent("Checksum Validation Failed for suspected RTS");
}
}
else {
logevent("Unknown traffic type received");
}
set_busy(mediums, false);
}
}
}
void send_ack_cts(char *addr1, int type)
{
cts_ack_s ack_cts;
ack_cts.FC = type;
ack_cts.D = 1;
memcpy(ack_cts.addr1, addr1, sizeof(ack_cts.addr1));
ack_cts.FCS = (uint32_t)crc32(CRC_POLYNOMIAL, (Bytef *)&ack_cts, sizeof(ack_cts)-sizeof(uint32_t));
slowwrite(mediumc, &ack_cts, sizeof(ack_cts));
logevent("Got Valid RTS and Sent ACK");
}
void deliver_message(char *addr1, char *addr2, char *payload, size_t size)
{
sym_record_s *rec;
station_s *station;
funcs_e func = FNET_RECEIVE;
char addbuf[9];
addbuf[0] = '"';
strncpy(&addbuf[1], addr2, 6);
addbuf[strlen(addbuf)] = '"';
addbuf[strlen(addbuf)] = '\0';
pthread_mutex_lock(&station_table_lock);
rec = sym_lookup(&station_table, addbuf);
if(rec) {
station = rec->data.ptr;
write(station->pipe[1], &func, sizeof(func));
write(station->pipe[1], &size, sizeof(size));
write(station->pipe[1], payload, size);
write(station->pipe[1], addr1, 6);
kill(station->pid, SIGUSR1);
logevent("Delivered payload to %.6s", addr2);
}
else
logevent("Unknown Station: %.6s", addr2);
pthread_mutex_unlock(&station_table_lock);
}
void sigUSR1(int sig){}