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
|
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <ctype.h>
#include <stdbool.h>
#include "util.h"
/*
* CONNECT MSG: "connect NAME"
* SEND MSG: "msg XXXXX CONTENTS TIME"
* VALID MSG: 0
* INVALID MSG: 1
*/
typedef struct client {
char *name;
int fd;
bool connected;
} client;
typedef struct msg {
client *sender;
char *contents;
time_t time;
} msg;
FILE *fout;
int VALID = 0;
int INVALID = 1;
static int
init_daemon(int port) {
int s = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in addr = {AF_INET, htons(port), INADDR_ANY};
if (bind(s, (struct sockaddr *)&addr, sizeof(addr)))
error("Failed to bind");
int flags = 10;
if (setsockopt(s, SOL_TCP, TCP_KEEPIDLE, (void *)&flags, sizeof(flags)))
error("Couldn't set socket opts");
flags = 5;
if (setsockopt(s, SOL_TCP, TCP_KEEPCNT, (void *)&flags, sizeof(flags)))
error("Couldn't set socket opts");
flags = 5;
if (setsockopt(s, SOL_TCP, TCP_KEEPINTVL, (void *)&flags, sizeof(flags)))
error("Couldn't set socket opts");
listen(s, 10);
return s;
}
static void
stop_daemon(int s) {
close(s);
}
static client *
accept_client(int s) {
client *c = malloc(sizeof(client));
c->fd = accept(s, NULL, NULL);
char buf[256] = {0};
char *Pbuf = buf;
int n = recv(c->fd, buf, 256, 0);
if (n <= 0) {
warn("Client hungup");
close(c->fd);
return NULL;
}
if (n <= 8) {
warn("Malformed connect msg");
close(c->fd);
return NULL;
}
if (memcmp(buf, "connect ", 8) != 0) {
warn("Malformed connect msg");
close(c->fd);
return NULL;
}
Pbuf += 8;
c->name = strdup(Pbuf);
c->connected = true;
return c;
}
static void
close_client(client *c) {
close(c->fd);
free(c->name);
free(c);
}
static msg*
recv_msg(client *c) {
char buf[11] = {0};
char *Pbuf = buf + 4;
int n = recv(c->fd, buf, 10, 0);
if (n <= 0) {
warn("Client hungup");
close(c->fd);
c->connected = false;
return NULL;
}
if (n < 10) {
warn("Malformed msg msg");
send(c->fd, &INVALID, sizeof(int), 0);
return NULL;
}
if (memcmp(buf, "msg ", 4) != 0) {
warn("Malformed msg msg");
send(c->fd, &INVALID, sizeof(int), 0);
return NULL;
}
for (int i = 4; buf[i] != 0 && i < 9; i++) {
if (!isdigit(buf[i])) {
warn("Malformed msg msg");
send(c->fd, &INVALID, sizeof(int), 0);
return NULL;
}
}
int len = atoi(Pbuf);
char *contents = malloc(len);
memset(contents, 0, len);
n = recv(c->fd, contents, len, 0);
if (n <= 0) {
warn("Client hungup");
c->connected = false;
close(c->fd);
return NULL;
}
msg *m = malloc(sizeof(msg));
m->sender = c;
m->contents = contents;
time_t t;
n = recv(c->fd, &t, sizeof(time_t), 0);
if (n <= 0) {
warn("Client hungup");
c->connected = false;
close(c->fd);
return NULL;
}
m->time = t;
send(c->fd, &VALID, sizeof(int), 0);
return m;
}
static void
free_msg(msg *m) {
free(m->contents);
free(m);
}
static void
msg_loop(client *c) {
msg *m;
while (c->connected && (m = recv_msg(c))) {
fprintf(fout, "%lld:%s:%s\n", m->time, m->sender->name, m->contents);
free_msg(m);
}
close_client(c);
exit(0);
}
int
main(int argc, char **argv) {
char *out;
if (argc <= 1)
out = "/dev/stdout";
else
out = argv[1];
if (!(fout = fopen(out, "w")))
error("Couldn't open outfile");
int s = init_daemon(1543);
pid_t pid;
for (;;) {
client *c = accept_client(s);
if ((pid = fork()) == 0)
msg_loop(c);
else
continue;
}
stop_daemon(s);
}
|