-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
351 lines (289 loc) · 8.2 KB
/
main.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
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <err.h>
#include <errno.h>
#include <pthread.h>
#include <sqlite3.h>
#define POLL_LENGTH 4
#define PORT 8080
#define IP4 "127.0.0.1"
#define SHOWLOG 1
#define SHOWERR 1
#define SHOWWARN 1
#if SHOWERR
#define MERR(status, args_format, arg1, arg2, arg3) \
{ \
err(status, args_format, arg1, arg2, arg3); \
fflush(NULL); \
}
#else
#define MERR(status, args_format, arg1, arg2, arg3) {err(status, "");}
#endif
#if SHOWLOG
#define MLOG(args_format, arg1, arg2, arg3, arg4) { \
printf(args_format, arg1, arg2, arg3, arg4); \
fflush(NULL); \
}
#else
#define MLOG(args_format, arg1, arg2, arg3, arg4) {;}
#endif
#if SHOWWARN
#define MWARN(args_format, arg1, arg2, arg3, arg4) { \
warn(args_format, arg1, arg2, arg3, arg4); \
fflush(NULL); \
}
#else
#define MWARN(args_format, arg1, arg2, arg3, arg4) {;}
#endif
#define GET_SLASH "GET / HTTP/1.1\r\n"
#define GET_IMAGE "GET /store_front.jpeg HTTP/1.1\r\n"
/* SQLite query buffer */
char *query;
struct conn_info
{
int fd;
pthread_t thread_id;
struct sockaddr_in peer_addr;
socklen_t socklen;
};
void* conn_handler(void *arg);
int main()
{
int sfd, ret;
struct sockaddr_in addr;
pthread_attr_t attr;
struct conn_info *ci;
pthread_mutex_t mutex;
sqlite3 *db_conn_stat;
sfd = socket(AF_INET, SOCK_STREAM, 0);
if (sfd == -1)
{
MERR(EXIT_FAILURE, "%d: %s: %s", __LINE__, __func__, strerror(errno));
}
else
{
MLOG("%s: %d: Socket created and file descriptor is: %d\n",
__FILE__, __LINE__, sfd, NULL);
}
memset(&addr, '\0', sizeof(struct sockaddr_in));
addr.sin_family = AF_INET;
addr.sin_port = htons(PORT);
if (!inet_pton(AF_INET, IP4, &addr.sin_addr))
{
MERR(EXIT_FAILURE, "%s: %d: Wrong ipv4 format.\n", __FILE__, __LINE__,
NULL);
}
if (bind(sfd, (struct sockaddr *) &addr, sizeof(struct sockaddr)) == -1)
{
MERR(EXIT_FAILURE, "%s: %d: %s", __FILE__, __LINE__, strerror(errno));
}
else
{
MLOG("%s: %d: Socket binded to %s ip and port is %d\n",
__FILE__, __LINE__, IP4, PORT);
}
if (listen(sfd, POLL_LENGTH) == -1)
{
MERR(EXIT_FAILURE, "%s: %d: %s", __FILE__, __LINE__, strerror(errno));
}
else
{
MLOG("%s: %d: Listening... .\n", __FILE__, __LINE__, NULL, NULL);
}
ret = pthread_attr_init(&attr);
if (ret)
{
MERR(EXIT_FAILURE, "%s: %d: error number: %d", __FILE__, __LINE__, ret);
}
/* Open/Create sqlite connections statistics database */
ret = sqlite3_open_v2("./db_conn_stat", &db_conn_stat, SQLITE_OPEN_READWRITE,
NULL);
if (ret == SQLITE_CANTOPEN)
{
MLOG("%s: %d: sqlite: %s, creating database ... \n", __FILE__, __LINE__,
sqlite3_errstr(ret), NULL);
ret = sqlite3_open_v2("./db_conn_stat", &db_conn_stat,
SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
if (ret != SQLITE_OK)
{
MERR(EXIT_FAILURE, "%s: %d: sqlite: %s\n", __FILE__, __LINE__,
sqlite3_errstr(ret));
}
MLOG("%s: %d: sqlite: database created.\n", __FILE__, __LINE__, NULL,
NULL);
ret = asprintf(&query, "CREATE TABLE conn_stat(\
IP TEXT PRIMARY KEY NOT NULL,\
VIS_COUNT INT DEFAULT 0)");
if (ret == -1)
{
MERR(EXIT_FAILURE, "%s: %d: Could not allocate memory for query.\n",
__FILE__, __LINE__, NULL);
}
ret = sqlite3_exec(db_conn_stat, query, NULL, 0, NULL);
free(query);
query = NULL;
if (ret != SQLITE_OK)
{
MERR(EXIT_FAILURE, "%s: %d: Could not create conn_stat table: %s\n",
__FILE__, __LINE__, sqlite3_errstr(ret));
}
MLOG("%s: %d: sqlite: table conn_stat created.\n", __FILE__, __LINE__,
NULL, NULL);
}
pthread_mutex_init(&mutex, NULL);
while (1)
{
ci = (struct conn_info*) malloc(sizeof(struct conn_info));
if (ci == NULL)
{
MWARN("%s: %d: malloc couldn't allocate memmory for ci struct!\n",
__FILE__, __LINE__, NULL, NULL);
sleep(5);
continue;
}
MLOG("\n%s: %d: Waiting for a new connection ...\n", __FILE__, __LINE__,
NULL, NULL);
ci->socklen = sizeof(ci->peer_addr);
ci->fd = accept(sfd, (struct sockaddr *) &ci->peer_addr, &ci->socklen);
if (ci->fd == -1)
{
MWARN("\n%s: %d: Accept() returned -1\n", __FILE__, __LINE__, NULL,
NULL);
continue;
}
MLOG("\n%s: %d: New connection from %s address with fd %d.\n",
__FILE__, __LINE__, inet_ntoa(ci->peer_addr.sin_addr), ci->fd);
ret = asprintf(&query, "INSERT INTO conn_stat (ip, vis_count) VALUES \
('%s', 1) ON CONFLICT(ip) DO UPDATE SET vis_count=vis_count+1;",
inet_ntoa(ci->peer_addr.sin_addr));
if (ret == -1)
{
MERR(EXIT_FAILURE, "%s: %d: Could not allocate memory for query.\n",
__FILE__, __LINE__, NULL);
}
ret = sqlite3_exec(db_conn_stat, query, NULL, 0, NULL);
free(query);
query = NULL;
if (ret != SQLITE_OK)
{
MERR(EXIT_FAILURE, "%s: %d: Could update table conn_stat: %s\n",
__FILE__, __LINE__, sqlite3_errstr(ret));
}
pthread_mutex_lock(&mutex);
if (!pthread_create(&ci->thread_id, &attr, &conn_handler, ci))
{
MLOG("\n%s: %d: New thread created for %s address (%d fd).\n",
__FILE__, __LINE__, inet_ntoa(ci->peer_addr.sin_addr), ci->fd);
}
else
{
MERR(EXIT_FAILURE, "%s: %d: %s", __FILE__, __LINE__, strerror(errno));
}
pthread_mutex_unlock(&mutex);
}
}
void* conn_handler(void *arg)
{
int ret, fd;
char buffer[1000];
int size, cnt;
struct conn_info *ci = (struct conn_info *) arg;
char *uszbuf1, *uszbuf2;
char status_line[100], filename[100];
MLOG("\n%s: %d: Handling the connection with %s address and file %d.\n",
__FILE__, __LINE__, inet_ntoa(ci->peer_addr.sin_addr), ci->fd);
size = 0;
while ((ret = recv(ci->fd, buffer, sizeof(buffer), 0)) != 0)
{
if ( ret < 0 )
{
MERR(EXIT_FAILURE, "%s: %d: %s", __FILE__, __LINE__, strerror(errno));
}
size += ret;
uszbuf1 = realloc(uszbuf1, size);
if (uszbuf1 == NULL)
{
MERR(EXIT_FAILURE, "%s: %d: %s", __FILE__, __LINE__, strerror(errno));
}
memcpy(uszbuf1+size-ret, buffer, ret);
if (strstr(uszbuf1, "\r\n\r\n") != NULL)
break;
}
// MLOG("\n%s\n", uszbuf1, NULL, NULL, NULL);
if (strncmp(uszbuf1, GET_SLASH, sizeof(GET_SLASH)-1) == 0)
{
strcpy(status_line, "HTTP/1.1 200 OK");
strcpy(filename, "index.html");
}
else if (strncmp(uszbuf1, GET_IMAGE, sizeof(GET_IMAGE)-1) == 0)
{
strcpy(status_line, "HTTP/1.1 200 OK");
strcpy(filename, "store_front.jpeg");
}
else
{
strcpy(status_line, "HTTP/1.1 404 NOT FOUND");
strcpy(filename, "404.html");
}
free(uszbuf1);
uszbuf1 = NULL;
fd = open(filename, O_RDONLY);
if (fd == -1)
{
MERR(EXIT_FAILURE, "%s: %d: %s", __FILE__, __LINE__, strerror(errno));
}
size = 0;
while ((ret = read(fd, buffer, sizeof(buffer))) != 0)
{
if (ret < 0)
{
MERR(EXIT_FAILURE, "%s: %d: %s", __FILE__, __LINE__, strerror(errno));
}
size += ret;
uszbuf1 = realloc(uszbuf1, size);
if (uszbuf1 == NULL)
{
MERR(EXIT_FAILURE, "%s: %d: %s", __FILE__, __LINE__, strerror(errno));
}
memcpy(uszbuf1+size-ret, buffer, ret);
}
close(fd);
ret = asprintf(&uszbuf2, "%s\r\nContent-Length: %d\r\n\r\n", status_line,
size);
size += ret;
uszbuf2 = realloc(uszbuf2, size);
memcpy(uszbuf2+ret, uszbuf1, size-ret);
if (size == -1)
{
MERR(EXIT_FAILURE, "%s: %d: Could not allocate memory for query.\n",
__FILE__, __LINE__, NULL);
}
// MLOG("\n%s\n", uszbuf2, NULL, NULL, NULL);
cnt = 0;
while ((ret = send(ci->fd, uszbuf2+cnt, size, 0)) != 0)
{
if (ret <= 0)
{
MERR(EXIT_FAILURE, "%s: %d: %s", __FILE__, __LINE__, strerror(errno));
}
size -= ret;
cnt += ret;
}
MLOG("\n%s: %d: Connection handled and data sent.\n", __FILE__, __LINE__,
NULL, NULL);
free(uszbuf1);
free(uszbuf2);
uszbuf1 = NULL;
uszbuf2 = NULL;
close(ci->fd);
free(ci);
}