annotate src/c/driver.c @ 737:d049d31a1966

Initial support for blobs and upload
author Adam Chlipala <adamc@hcoop.net>
date Sat, 25 Apr 2009 13:59:11 -0400
parents f2a2be93331c
children 4bb7e1c0550a
rev   line source
adamc@737 1 #define _GNU_SOURCE
adamc@737 2
adamc@116 3 #include <stdio.h>
adamc@116 4 #include <string.h>
adamc@502 5 #include <stdlib.h>
adamc@116 6 #include <sys/types.h>
adamc@116 7 #include <sys/socket.h>
adamc@116 8 #include <netinet/in.h>
adamc@472 9 #include <unistd.h>
adamc@502 10 #include <signal.h>
adamc@116 11
adamc@138 12 #include <pthread.h>
adamc@138 13
adamc@734 14 #include <mhash.h>
adamc@734 15
adamc@244 16 #include "urweb.h"
adamc@117 17
adamc@311 18 int uw_backlog = 10;
adamc@311 19 int uw_bufsize = 1024;
adamc@116 20
adamc@138 21 typedef struct node {
adamc@138 22 int fd;
adamc@138 23 struct node *next;
adamc@138 24 } *node;
adamc@138 25
adamc@138 26 static node front = NULL, back = NULL;
adamc@138 27
adamc@138 28 static int empty() {
adamc@138 29 return front == NULL;
adamc@138 30 }
adamc@138 31
adamc@138 32 static void enqueue(int fd) {
adamc@138 33 node n = malloc(sizeof(struct node));
adamc@138 34
adamc@138 35 n->fd = fd;
adamc@138 36 n->next = NULL;
adamc@138 37 if (back)
adamc@138 38 back->next = n;
adamc@138 39 else
adamc@138 40 front = n;
adamc@138 41 back = n;
adamc@138 42 }
adamc@138 43
adamc@138 44 static int dequeue() {
adamc@138 45 int ret = front->fd;
adamc@138 46
adamc@138 47 front = front->next;
adamc@138 48 if (!front)
adamc@138 49 back = NULL;
adamc@138 50
adamc@138 51 return ret;
adamc@138 52 }
adamc@138 53
adamc@138 54 static pthread_mutex_t queue_mutex = PTHREAD_MUTEX_INITIALIZER;
adamc@138 55 static pthread_cond_t queue_cond = PTHREAD_COND_INITIALIZER;
adamc@138 56
adamc@167 57 #define MAX_RETRIES 5
adamc@167 58
adamc@424 59 static int try_rollback(uw_context ctx) {
adamc@671 60 int r = uw_rollback(ctx);
adamc@424 61
adamc@424 62 if (r) {
adamc@424 63 printf("Error running SQL ROLLBACK\n");
adamc@424 64 uw_reset(ctx);
adamc@424 65 uw_write(ctx, "HTTP/1.1 500 Internal Server Error\n\r");
adamc@424 66 uw_write(ctx, "Content-type: text/plain\r\n\r\n");
adamc@424 67 uw_write(ctx, "Error running SQL ROLLBACK\n");
adamc@424 68 }
adamc@424 69
adamc@424 70 return r;
adamc@424 71 }
adamc@424 72
adamc@698 73 static uw_context new_context() {
adamc@687 74 uw_context ctx = uw_init();
adamc@698 75 int retries_left = MAX_RETRIES;
adamc@698 76
adamc@272 77 while (1) {
adamc@311 78 failure_kind fk = uw_begin_init(ctx);
adamc@272 79
adamc@272 80 if (fk == SUCCESS) {
adamc@272 81 printf("Database connection initialized.\n");
adamc@272 82 break;
adamc@272 83 } else if (fk == BOUNDED_RETRY) {
adamc@272 84 if (retries_left) {
adamc@311 85 printf("Initialization error triggers bounded retry: %s\n", uw_error_message(ctx));
adamc@272 86 --retries_left;
adamc@272 87 } else {
adamc@311 88 printf("Fatal initialization error (out of retries): %s\n", uw_error_message(ctx));
adamc@311 89 uw_free(ctx);
adamc@272 90 return NULL;
adamc@272 91 }
adamc@272 92 } else if (fk == UNLIMITED_RETRY)
adamc@311 93 printf("Initialization error triggers unlimited retry: %s\n", uw_error_message(ctx));
adamc@272 94 else if (fk == FATAL) {
adamc@311 95 printf("Fatal initialization error: %s\n", uw_error_message(ctx));
adamc@311 96 uw_free(ctx);
adamc@272 97 return NULL;
adamc@272 98 } else {
adamc@698 99 printf("Unknown uw_begin_init return code!\n");
adamc@311 100 uw_free(ctx);
adamc@272 101 return NULL;
adamc@272 102 }
adamc@272 103 }
adamc@116 104
adamc@698 105 return ctx;
adamc@698 106 }
adamc@698 107
adamc@734 108 #define KEYSIZE 16
adamc@734 109 #define PASSSIZE 4
adamc@734 110
adamc@734 111 #define HASH_ALGORITHM MHASH_SHA256
adamc@734 112 #define HASH_BLOCKSIZE 32
adamc@734 113 #define KEYGEN_ALGORITHM KEYGEN_MCRYPT
adamc@734 114
adamc@734 115 int uw_hash_blocksize = HASH_BLOCKSIZE;
adamc@734 116
adamc@734 117 static int password[PASSSIZE];
adamc@734 118 static unsigned char private_key[KEYSIZE];
adamc@734 119
adamc@734 120 static void init_crypto() {
adamc@734 121 KEYGEN kg = {{HASH_ALGORITHM, HASH_ALGORITHM}};
adamc@734 122 int i;
adamc@734 123
adamc@734 124 assert(mhash_get_block_size(HASH_ALGORITHM) == HASH_BLOCKSIZE);
adamc@734 125
adamc@734 126 for (i = 0; i < PASSSIZE; ++i)
adamc@734 127 password[i] = rand();
adamc@734 128
adamc@734 129 if (mhash_keygen_ext(KEYGEN_ALGORITHM, kg,
adamc@734 130 private_key, sizeof(private_key),
adamc@734 131 (unsigned char*)password, sizeof(password)) < 0) {
adamc@734 132 printf("Key generation failed\n");
adamc@734 133 exit(1);
adamc@734 134 }
adamc@734 135 }
adamc@734 136
adamc@734 137 void uw_sign(const char *in, char *out) {
adamc@734 138 MHASH td;
adamc@734 139
adamc@734 140 td = mhash_hmac_init(HASH_ALGORITHM, private_key, sizeof(private_key),
adamc@734 141 mhash_get_hash_pblock(HASH_ALGORITHM));
adamc@734 142
adamc@734 143 mhash(td, in, strlen(in));
adamc@734 144 if (mhash_hmac_deinit(td, out) < 0)
adamc@734 145 printf("Signing failed");
adamc@734 146 }
adamc@734 147
adamc@698 148 static void *worker(void *data) {
adamc@698 149 int me = *(int *)data, retries_left = MAX_RETRIES;
adamc@698 150 uw_context ctx = new_context();
adamc@737 151 size_t buf_size = 1;
adamc@737 152 char *buf = malloc(buf_size);
adamc@698 153
adamc@116 154 while (1) {
adamc@737 155 char *back = buf, *s, *post;
adamc@667 156 int sock, dont_close = 0;
adamc@116 157
adamc@138 158 pthread_mutex_lock(&queue_mutex);
adamc@138 159 while (empty())
adamc@138 160 pthread_cond_wait(&queue_cond, &queue_mutex);
adamc@138 161 sock = dequeue();
adamc@138 162 pthread_mutex_unlock(&queue_mutex);
adamc@138 163
adamc@138 164 printf("Handling connection with thread #%d.\n", me);
adamc@138 165
adamc@138 166 while (1) {
adamc@167 167 unsigned retries_left = MAX_RETRIES;
adamc@737 168 int r;
adamc@737 169
adamc@737 170 if (back - buf == buf_size) {
adamc@737 171 char *new_buf;
adamc@737 172 buf_size *= 2;
adamc@737 173 new_buf = realloc(buf, buf_size);
adamc@737 174 back = new_buf + (back - buf);
adamc@737 175 buf = new_buf;
adamc@737 176 }
adamc@737 177
adamc@737 178 r = recv(sock, back, buf_size - (back - buf), 0);
adamc@138 179
adamc@138 180 if (r < 0) {
adamc@138 181 fprintf(stderr, "Recv failed\n");
adamc@138 182 break;
adamc@138 183 }
adamc@138 184
adamc@138 185 if (r == 0) {
adamc@138 186 printf("Connection closed.\n");
adamc@138 187 break;
adamc@138 188 }
adamc@138 189
adamc@730 190 //printf("Received %d bytes.\n", r);
adamc@138 191
adamc@138 192 back += r;
adamc@138 193 *back = 0;
adamc@730 194
adamc@138 195 if (s = strstr(buf, "\r\n\r\n")) {
adamc@424 196 failure_kind fk;
adamc@730 197 int is_post = 0;
adamc@737 198 char *boundary = NULL;
adamc@737 199 size_t boundary_len;
adamc@730 200 char *cmd, *path, *headers, path_copy[uw_bufsize+1], *inputs, *after_headers;
adamc@138 201
adamc@737 202 //printf("All: %s\n", buf);
adamc@737 203
adamc@475 204 s[2] = 0;
adamc@730 205 after_headers = s + 4;
adamc@144 206
adamc@138 207 if (!(s = strstr(buf, "\r\n"))) {
adamc@138 208 fprintf(stderr, "No newline in buf\n");
adamc@138 209 break;
adamc@138 210 }
adamc@138 211
adamc@138 212 *s = 0;
adamc@457 213 headers = s + 2;
adamc@138 214 cmd = s = buf;
adamc@401 215
adamc@737 216 //printf("Read: %s\n", buf);
adamc@138 217
adamc@138 218 if (!strsep(&s, " ")) {
adamc@138 219 fprintf(stderr, "No first space in HTTP command\n");
adamc@138 220 break;
adamc@138 221 }
adamc@138 222
adamc@730 223 uw_set_headers(ctx, headers);
adamc@730 224
adamc@730 225 if (!strcmp(cmd, "POST")) {
adamc@730 226 char *clen_s = uw_Basis_requestHeader(ctx, "Content-length");
adamc@730 227 if (!clen_s) {
adamc@737 228 fprintf(stderr, "No Content-length with POST\n");
adamc@730 229 goto done;
adamc@730 230 }
adamc@730 231 int clen = atoi(clen_s);
adamc@730 232 if (clen < 0) {
adamc@737 233 fprintf(stderr, "Negative Content-length with POST\n");
adamc@730 234 goto done;
adamc@730 235 }
adamc@730 236
adamc@730 237 while (back - after_headers < clen) {
adamc@737 238 if (back - buf == buf_size) {
adamc@737 239 char *new_buf;
adamc@737 240 buf_size *= 2;
adamc@737 241 new_buf = realloc(buf, buf_size);
adamc@737 242 back = new_buf + (back - buf);
adamc@737 243 buf = new_buf;
adamc@737 244 }
adamc@737 245
adamc@737 246 r = recv(sock, back, buf_size - (back - buf), 0);
adamc@730 247
adamc@730 248 if (r < 0) {
adamc@730 249 fprintf(stderr, "Recv failed\n");
adamc@730 250 goto done;
adamc@730 251 }
adamc@730 252
adamc@730 253 if (r == 0) {
adamc@730 254 printf("Connection closed.\n");
adamc@730 255 goto done;
adamc@730 256 }
adamc@730 257
adamc@730 258 back += r;
adamc@730 259 *back = 0;
adamc@730 260 }
adamc@730 261
adamc@730 262 is_post = 1;
adamc@737 263
adamc@737 264 clen_s = uw_Basis_requestHeader(ctx, "Content-type");
adamc@737 265 if (clen_s && !strncasecmp(clen_s, "multipart/form-data", 19)) {
adamc@737 266 if (strncasecmp(clen_s + 19, "; boundary=", 11)) {
adamc@737 267 fprintf(stderr, "Bad multipart boundary spec");
adamc@737 268 break;
adamc@737 269 }
adamc@737 270
adamc@737 271 boundary = clen_s + 28;
adamc@737 272 boundary[0] = '-';
adamc@737 273 boundary[1] = '-';
adamc@737 274 boundary_len = strlen(boundary);
adamc@737 275 }
adamc@730 276 } else if (strcmp(cmd, "GET")) {
adamc@730 277 fprintf(stderr, "Not ready for non-GET/POST command: %s\n", cmd);
adamc@138 278 break;
adamc@138 279 }
adamc@138 280
adamc@138 281 path = s;
adamc@138 282 if (!strsep(&s, " ")) {
adamc@138 283 fprintf(stderr, "No second space in HTTP command\n");
adamc@138 284 break;
adamc@138 285 }
adamc@138 286
adamc@668 287 if (!strcmp(path, "/.msgs")) {
adamc@668 288 char *id = uw_Basis_requestHeader(ctx, "UrWeb-Client");
adamc@668 289 char *pass = uw_Basis_requestHeader(ctx, "UrWeb-Pass");
adamc@668 290
adamc@668 291 if (id && pass) {
adamc@682 292 unsigned idn = atoi(id);
adamc@668 293 uw_client_connect(idn, atoi(pass), sock);
adamc@668 294 dont_close = 1;
adamc@682 295 fprintf(stderr, "Processed request for messages by client %u\n\n", idn);
adamc@668 296 }
adamc@703 297 else {
adamc@703 298 fprintf(stderr, "Missing fields in .msgs request: %s, %s\n\n", id, pass);
adamc@703 299 }
adamc@667 300 break;
adamc@667 301 }
adamc@667 302
adamc@737 303 if (boundary) {
adamc@737 304 char *part = after_headers, *after_sub_headers, *header, *after_header;
adamc@737 305 size_t part_len;
adamc@144 306
adamc@737 307 part = strstr(part, boundary);
adamc@737 308 if (!part) {
adamc@737 309 fprintf(stderr, "Missing first multipart boundary\n");
adamc@737 310 break;
adamc@737 311 }
adamc@737 312 part += boundary_len;
adamc@145 313
adamc@737 314 while (1) {
adamc@737 315 char *name = NULL, *filename = NULL, *type = NULL;
adamc@737 316
adamc@737 317 if (part[0] == '-' && part[1] == '-')
adamc@737 318 break;
adamc@737 319
adamc@737 320 if (*part != '\r') {
adamc@737 321 fprintf(stderr, "No \\r after multipart boundary\n");
adamc@737 322 goto done;
adamc@144 323 }
adamc@737 324 ++part;
adamc@737 325 if (*part != '\n') {
adamc@737 326 fprintf(stderr, "No \\n after multipart boundary\n");
adamc@737 327 goto done;
adamc@737 328 }
adamc@737 329 ++part;
adamc@737 330
adamc@737 331 if (!(after_sub_headers = strstr(part, "\r\n\r\n"))) {
adamc@737 332 fprintf(stderr, "Missing end of headers after multipart boundary\n");
adamc@737 333 goto done;
adamc@737 334 }
adamc@737 335 after_sub_headers[2] = 0;
adamc@737 336 after_sub_headers += 4;
adamc@737 337
adamc@737 338 for (header = part; after_header = strstr(header, "\r\n"); header = after_header + 2) {
adamc@737 339 char *colon, *after_colon;
adamc@737 340
adamc@737 341 *after_header = 0;
adamc@737 342 if (!(colon = strchr(header, ':'))) {
adamc@737 343 fprintf(stderr, "Missing colon in multipart sub-header\n");
adamc@737 344 goto done;
adamc@737 345 }
adamc@737 346 *colon++ = 0;
adamc@737 347 if (*colon++ != ' ') {
adamc@737 348 fprintf(stderr, "No space after colon in multipart sub-header\n");
adamc@737 349 goto done;
adamc@737 350 }
adamc@737 351
adamc@737 352 if (!strcasecmp(header, "Content-Disposition")) {
adamc@737 353 if (strncmp(colon, "form-data; ", 11)) {
adamc@737 354 fprintf(stderr, "Multipart data is not \"form-data\"\n");
adamc@737 355 goto done;
adamc@737 356 }
adamc@737 357
adamc@737 358 for (colon += 11; after_colon = strchr(colon, '='); colon = after_colon) {
adamc@737 359 char *data;
adamc@737 360 after_colon[0] = 0;
adamc@737 361 if (after_colon[1] != '"') {
adamc@737 362 fprintf(stderr, "Disposition setting is missing initial quote\n");
adamc@737 363 goto done;
adamc@737 364 }
adamc@737 365 data = after_colon+2;
adamc@737 366 if (!(after_colon = strchr(data, '"'))) {
adamc@737 367 fprintf(stderr, "Disposition setting is missing final quote\n");
adamc@737 368 goto done;
adamc@737 369 }
adamc@737 370 after_colon[0] = 0;
adamc@737 371 ++after_colon;
adamc@737 372 if (after_colon[0] == ';' && after_colon[1] == ' ')
adamc@737 373 after_colon += 2;
adamc@737 374
adamc@737 375 if (!strcasecmp(colon, "name"))
adamc@737 376 name = data;
adamc@737 377 else if (!strcasecmp(colon, "filename"))
adamc@737 378 filename = data;
adamc@737 379 }
adamc@737 380 } else if (!strcasecmp(header, "Content-Type")) {
adamc@737 381 type = colon;
adamc@737 382 }
adamc@737 383 }
adamc@737 384
adamc@737 385 part = memmem(after_sub_headers, back - after_sub_headers, boundary, boundary_len);
adamc@737 386 if (!part) {
adamc@737 387 fprintf(stderr, "Missing boundary after multipart payload\n");
adamc@737 388 goto done;
adamc@737 389 }
adamc@737 390 part[-2] = 0;
adamc@737 391 part_len = part - after_sub_headers - 2;
adamc@737 392 part[0] = 0;
adamc@737 393 part += boundary_len;
adamc@737 394
adamc@737 395 if (filename) {
adamc@737 396 uw_Basis_file *f = malloc(sizeof(uw_Basis_file));
adamc@737 397 uw_Basis_files fs = { 1, f };
adamc@737 398
adamc@737 399 f->name = filename;
adamc@737 400 f->data.size = part_len;
adamc@737 401 f->data.data = after_sub_headers;
adamc@737 402
adamc@737 403 uw_set_file_input(ctx, name, fs);
adamc@737 404 } else
adamc@737 405 uw_set_input(ctx, name, after_sub_headers);
adamc@737 406 }
adamc@737 407 }
adamc@737 408 else {
adamc@737 409 if (is_post)
adamc@737 410 inputs = after_headers;
adamc@737 411 else if (inputs = strchr(path, '?'))
adamc@737 412 *inputs++ = 0;
adamc@737 413
adamc@737 414 if (inputs) {
adamc@737 415 char *name, *value;
adamc@737 416
adamc@737 417 while (*inputs) {
adamc@737 418 name = inputs;
adamc@737 419 if (inputs = strchr(inputs, '&'))
adamc@737 420 *inputs++ = 0;
adamc@737 421 else
adamc@737 422 inputs = strchr(name, 0);
adamc@737 423
adamc@737 424 if (value = strchr(name, '=')) {
adamc@737 425 *value++ = 0;
adamc@737 426 uw_set_input(ctx, name, value);
adamc@737 427 }
adamc@737 428 else
adamc@737 429 uw_set_input(ctx, name, "");
adamc@737 430 }
adamc@144 431 }
adamc@144 432 }
adamc@144 433
adamc@138 434 printf("Serving URI %s....\n", path);
adamc@138 435
adamc@167 436 while (1) {
adamc@462 437 uw_write_header(ctx, "HTTP/1.1 200 OK\r\n");
adamc@167 438
adamc@400 439 strcpy(path_copy, path);
adamc@458 440 fk = uw_begin(ctx, path_copy);
adamc@167 441 if (fk == SUCCESS) {
adamc@671 442 uw_commit(ctx);
adamc@167 443 break;
adamc@167 444 } else if (fk == BOUNDED_RETRY) {
adamc@167 445 if (retries_left) {
adamc@311 446 printf("Error triggers bounded retry: %s\n", uw_error_message(ctx));
adamc@167 447 --retries_left;
adamc@167 448 }
adamc@167 449 else {
adamc@311 450 printf("Fatal error (out of retries): %s\n", uw_error_message(ctx));
adamc@167 451
adamc@682 452 try_rollback(ctx);
adamc@682 453
adamc@311 454 uw_reset_keep_error_message(ctx);
adamc@464 455 uw_write_header(ctx, "HTTP/1.1 500 Internal Server Error\n\r");
adamc@464 456 uw_write_header(ctx, "Content-type: text/plain\r\n");
adamc@311 457 uw_write(ctx, "Fatal error (out of retries): ");
adamc@311 458 uw_write(ctx, uw_error_message(ctx));
adamc@311 459 uw_write(ctx, "\n");
adamc@424 460
adamc@424 461 break;
adamc@167 462 }
adamc@167 463 } else if (fk == UNLIMITED_RETRY)
adamc@311 464 printf("Error triggers unlimited retry: %s\n", uw_error_message(ctx));
adamc@167 465 else if (fk == FATAL) {
adamc@311 466 printf("Fatal error: %s\n", uw_error_message(ctx));
adamc@167 467
adamc@682 468 try_rollback(ctx);
adamc@682 469
adamc@311 470 uw_reset_keep_error_message(ctx);
adamc@464 471 uw_write_header(ctx, "HTTP/1.1 500 Internal Server Error\r\n");
adamc@548 472 uw_write_header(ctx, "Content-type: text/html\r\n");
adamc@548 473 uw_write(ctx, "<html><head><title>Fatal Error</title></head><body>");
adamc@311 474 uw_write(ctx, "Fatal error: ");
adamc@311 475 uw_write(ctx, uw_error_message(ctx));
adamc@548 476 uw_write(ctx, "\n</body></html>");
adamc@167 477
adamc@167 478 break;
adamc@167 479 } else {
adamc@311 480 printf("Unknown uw_handle return code!\n");
adamc@167 481
adamc@682 482 try_rollback(ctx);
adamc@682 483
adamc@311 484 uw_reset_keep_request(ctx);
adamc@464 485 uw_write_header(ctx, "HTTP/1.1 500 Internal Server Error\n\r");
adamc@464 486 uw_write_header(ctx, "Content-type: text/plain\r\n");
adamc@311 487 uw_write(ctx, "Unknown uw_handle return code!\n");
adamc@167 488
adamc@167 489 break;
adamc@167 490 }
adamc@167 491
adamc@424 492 if (try_rollback(ctx))
adamc@424 493 break;
adamc@682 494
adamc@682 495 uw_reset_keep_request(ctx);
adamc@167 496 }
adamc@138 497
adamc@311 498 uw_send(ctx, sock);
adamc@138 499
adamc@138 500 printf("Done with client.\n\n");
adamc@324 501 uw_memstats(ctx);
adamc@138 502 break;
adamc@138 503 }
adamc@116 504 }
adamc@116 505
adamc@730 506 done:
adamc@667 507 if (!dont_close)
adamc@667 508 close(sock);
adamc@311 509 uw_reset(ctx);
adamc@116 510 }
adamc@116 511 }
adamc@102 512
adamc@667 513 static void *client_pruner(void *data) {
adamc@698 514 uw_context ctx = new_context();
adamc@698 515
adamc@698 516 if (!ctx)
adamc@698 517 exit(1);
adamc@683 518
adamc@667 519 while (1) {
adamc@683 520 uw_prune_clients(ctx);
adamc@667 521 sleep(5);
adamc@667 522 }
adamc@667 523 }
adamc@667 524
adamc@477 525 static void help(char *cmd) {
adamc@477 526 printf("Usage: %s [-p <port>] [-t <thread-count>]\n", cmd);
adamc@477 527 }
adamc@477 528
adamc@502 529 static void sigint(int signum) {
adamc@502 530 printf("Exiting....\n");
adamc@502 531 exit(0);
adamc@502 532 }
adamc@502 533
adamc@687 534 static void initialize() {
adamc@734 535 uw_context ctx;
adamc@700 536 failure_kind fk;
adamc@687 537
adamc@734 538 init_crypto();
adamc@734 539
adamc@734 540 ctx = new_context();
adamc@734 541
adamc@698 542 if (!ctx)
adamc@698 543 exit(1);
adamc@698 544
adamc@700 545 for (fk = uw_initialize(ctx); fk == UNLIMITED_RETRY; fk = uw_initialize(ctx)) {
adamc@700 546 printf("Unlimited retry during init: %s\n", uw_error_message(ctx));
adamc@700 547 uw_db_rollback(ctx);
adamc@700 548 uw_reset(ctx);
adamc@700 549 }
adamc@700 550
adamc@700 551 if (fk != SUCCESS) {
adamc@700 552 printf("Failed to initialize database! %s\n", uw_error_message(ctx));
adamc@687 553 uw_db_rollback(ctx);
adamc@687 554 exit(1);
adamc@687 555 }
adamc@687 556
adamc@687 557 uw_free(ctx);
adamc@687 558 }
adamc@687 559
adamc@138 560 int main(int argc, char *argv[]) {
adamc@116 561 // The skeleton for this function comes from Beej's sockets tutorial.
adamc@138 562 int sockfd; // listen on sock_fd
adamc@116 563 struct sockaddr_in my_addr;
adamc@116 564 struct sockaddr_in their_addr; // connector's address information
adamc@116 565 int sin_size, yes = 1;
adamc@472 566 int uw_port = 8080, nthreads = 1, i, *names, opt;
adamc@502 567
adamc@502 568 signal(SIGINT, sigint);
adamc@505 569 signal(SIGPIPE, SIG_IGN);
adamc@505 570
adamc@477 571 while ((opt = getopt(argc, argv, "hp:t:")) != -1) {
adamc@472 572 switch (opt) {
adamc@472 573 case '?':
adamc@472 574 fprintf(stderr, "Unknown command-line option");
adamc@477 575 help(argv[0]);
adamc@472 576 return 1;
adamc@138 577
adamc@477 578 case 'h':
adamc@477 579 help(argv[0]);
adamc@477 580 return 0;
adamc@477 581
adamc@472 582 case 'p':
adamc@472 583 uw_port = atoi(optarg);
adamc@472 584 if (uw_port <= 0) {
adamc@472 585 fprintf(stderr, "Invalid port number\n");
adamc@477 586 help(argv[0]);
adamc@472 587 return 1;
adamc@472 588 }
adamc@472 589 break;
adamc@472 590
adamc@472 591 case 't':
adamc@472 592 nthreads = atoi(optarg);
adamc@472 593 if (nthreads <= 0) {
adamc@472 594 fprintf(stderr, "Invalid thread count\n");
adamc@477 595 help(argv[0]);
adamc@472 596 return 1;
adamc@472 597 }
adamc@472 598 break;
adamc@472 599
adamc@472 600 default:
adamc@472 601 fprintf(stderr, "Unexpected getopt() behavior\n");
adamc@472 602 return 1;
adamc@472 603 }
adamc@138 604 }
adamc@138 605
adamc@734 606 uw_global_init();
adamc@687 607 initialize();
adamc@687 608
adamc@138 609 names = calloc(nthreads, sizeof(int));
adamc@116 610
adamc@116 611 sockfd = socket(PF_INET, SOCK_STREAM, 0); // do some error checking!
adamc@116 612
adamc@116 613 if (sockfd < 0) {
adamc@116 614 fprintf(stderr, "Listener socket creation failed\n");
adamc@116 615 return 1;
adamc@116 616 }
adamc@116 617
adamc@116 618 if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) < 0) {
adamc@116 619 fprintf(stderr, "Listener socket option setting failed\n");
adamc@116 620 return 1;
adamc@116 621 }
adamc@116 622
adamc@116 623 my_addr.sin_family = AF_INET; // host byte order
adamc@311 624 my_addr.sin_port = htons(uw_port); // short, network byte order
adamc@116 625 my_addr.sin_addr.s_addr = INADDR_ANY; // auto-fill with my IP
adamc@116 626 memset(my_addr.sin_zero, '\0', sizeof my_addr.sin_zero);
adamc@116 627
adamc@116 628 if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof my_addr) < 0) {
adamc@116 629 fprintf(stderr, "Listener socket bind failed\n");
adamc@116 630 return 1;
adamc@116 631 }
adamc@116 632
adamc@311 633 if (listen(sockfd, uw_backlog) < 0) {
adamc@116 634 fprintf(stderr, "Socket listen failed\n");
adamc@116 635 return 1;
adamc@116 636 }
adamc@116 637
adamc@116 638 sin_size = sizeof their_addr;
adamc@116 639
adamc@311 640 printf("Listening on port %d....\n", uw_port);
adamc@116 641
adamc@667 642 {
adamc@667 643 pthread_t thread;
adamc@667 644 int name;
adamc@667 645
adamc@667 646 if (pthread_create(&thread, NULL, client_pruner, &name)) {
adamc@667 647 fprintf(stderr, "Error creating pruner thread\n");
adamc@667 648 return 1;
adamc@667 649 }
adamc@667 650 }
adamc@667 651
adamc@138 652 for (i = 0; i < nthreads; ++i) {
adamc@138 653 pthread_t thread;
adamc@138 654 names[i] = i;
adamc@138 655 if (pthread_create(&thread, NULL, worker, &names[i])) {
adamc@138 656 fprintf(stderr, "Error creating worker thread #%d\n", i);
adamc@138 657 return 1;
adamc@138 658 }
adamc@138 659 }
adamc@138 660
adamc@116 661 while (1) {
adamc@138 662 int new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size);
adamc@116 663
adamc@116 664 if (new_fd < 0) {
adamc@116 665 fprintf(stderr, "Socket accept failed\n");
adamc@116 666 return 1;
adamc@116 667 }
adamc@116 668
adamc@116 669 printf("Accepted connection.\n");
adamc@138 670
adamc@138 671 pthread_mutex_lock(&queue_mutex);
adamc@138 672 enqueue(new_fd);
adamc@139 673 pthread_cond_broadcast(&queue_cond);
adamc@138 674 pthread_mutex_unlock(&queue_mutex);
adamc@116 675 }
adamc@102 676 }