annotate src/c/driver.c @ 740:b302b6e35f93

Add MIME type to file
author Adam Chlipala <adamc@hcoop.net>
date Sat, 25 Apr 2009 14:47:16 -0400
parents 4bb7e1c0550a
children f7e2026dd5ae
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@740 396 uw_Basis_file f = {filename, type, {part_len, after_sub_headers}};
adamc@737 397
adamc@739 398 uw_set_file_input(ctx, name, f);
adamc@737 399 } else
adamc@737 400 uw_set_input(ctx, name, after_sub_headers);
adamc@737 401 }
adamc@737 402 }
adamc@737 403 else {
adamc@737 404 if (is_post)
adamc@737 405 inputs = after_headers;
adamc@737 406 else if (inputs = strchr(path, '?'))
adamc@737 407 *inputs++ = 0;
adamc@737 408
adamc@737 409 if (inputs) {
adamc@737 410 char *name, *value;
adamc@737 411
adamc@737 412 while (*inputs) {
adamc@737 413 name = inputs;
adamc@737 414 if (inputs = strchr(inputs, '&'))
adamc@737 415 *inputs++ = 0;
adamc@737 416 else
adamc@737 417 inputs = strchr(name, 0);
adamc@737 418
adamc@737 419 if (value = strchr(name, '=')) {
adamc@737 420 *value++ = 0;
adamc@737 421 uw_set_input(ctx, name, value);
adamc@737 422 }
adamc@737 423 else
adamc@737 424 uw_set_input(ctx, name, "");
adamc@737 425 }
adamc@144 426 }
adamc@144 427 }
adamc@144 428
adamc@138 429 printf("Serving URI %s....\n", path);
adamc@138 430
adamc@167 431 while (1) {
adamc@462 432 uw_write_header(ctx, "HTTP/1.1 200 OK\r\n");
adamc@167 433
adamc@400 434 strcpy(path_copy, path);
adamc@458 435 fk = uw_begin(ctx, path_copy);
adamc@167 436 if (fk == SUCCESS) {
adamc@671 437 uw_commit(ctx);
adamc@167 438 break;
adamc@167 439 } else if (fk == BOUNDED_RETRY) {
adamc@167 440 if (retries_left) {
adamc@311 441 printf("Error triggers bounded retry: %s\n", uw_error_message(ctx));
adamc@167 442 --retries_left;
adamc@167 443 }
adamc@167 444 else {
adamc@311 445 printf("Fatal error (out of retries): %s\n", uw_error_message(ctx));
adamc@167 446
adamc@682 447 try_rollback(ctx);
adamc@682 448
adamc@311 449 uw_reset_keep_error_message(ctx);
adamc@464 450 uw_write_header(ctx, "HTTP/1.1 500 Internal Server Error\n\r");
adamc@464 451 uw_write_header(ctx, "Content-type: text/plain\r\n");
adamc@311 452 uw_write(ctx, "Fatal error (out of retries): ");
adamc@311 453 uw_write(ctx, uw_error_message(ctx));
adamc@311 454 uw_write(ctx, "\n");
adamc@424 455
adamc@424 456 break;
adamc@167 457 }
adamc@167 458 } else if (fk == UNLIMITED_RETRY)
adamc@311 459 printf("Error triggers unlimited retry: %s\n", uw_error_message(ctx));
adamc@167 460 else if (fk == FATAL) {
adamc@311 461 printf("Fatal error: %s\n", uw_error_message(ctx));
adamc@167 462
adamc@682 463 try_rollback(ctx);
adamc@682 464
adamc@311 465 uw_reset_keep_error_message(ctx);
adamc@464 466 uw_write_header(ctx, "HTTP/1.1 500 Internal Server Error\r\n");
adamc@548 467 uw_write_header(ctx, "Content-type: text/html\r\n");
adamc@548 468 uw_write(ctx, "<html><head><title>Fatal Error</title></head><body>");
adamc@311 469 uw_write(ctx, "Fatal error: ");
adamc@311 470 uw_write(ctx, uw_error_message(ctx));
adamc@548 471 uw_write(ctx, "\n</body></html>");
adamc@167 472
adamc@167 473 break;
adamc@167 474 } else {
adamc@311 475 printf("Unknown uw_handle return code!\n");
adamc@167 476
adamc@682 477 try_rollback(ctx);
adamc@682 478
adamc@311 479 uw_reset_keep_request(ctx);
adamc@464 480 uw_write_header(ctx, "HTTP/1.1 500 Internal Server Error\n\r");
adamc@464 481 uw_write_header(ctx, "Content-type: text/plain\r\n");
adamc@311 482 uw_write(ctx, "Unknown uw_handle return code!\n");
adamc@167 483
adamc@167 484 break;
adamc@167 485 }
adamc@167 486
adamc@424 487 if (try_rollback(ctx))
adamc@424 488 break;
adamc@682 489
adamc@682 490 uw_reset_keep_request(ctx);
adamc@167 491 }
adamc@138 492
adamc@311 493 uw_send(ctx, sock);
adamc@138 494
adamc@138 495 printf("Done with client.\n\n");
adamc@324 496 uw_memstats(ctx);
adamc@138 497 break;
adamc@138 498 }
adamc@116 499 }
adamc@116 500
adamc@730 501 done:
adamc@667 502 if (!dont_close)
adamc@667 503 close(sock);
adamc@311 504 uw_reset(ctx);
adamc@116 505 }
adamc@116 506 }
adamc@102 507
adamc@667 508 static void *client_pruner(void *data) {
adamc@698 509 uw_context ctx = new_context();
adamc@698 510
adamc@698 511 if (!ctx)
adamc@698 512 exit(1);
adamc@683 513
adamc@667 514 while (1) {
adamc@683 515 uw_prune_clients(ctx);
adamc@667 516 sleep(5);
adamc@667 517 }
adamc@667 518 }
adamc@667 519
adamc@477 520 static void help(char *cmd) {
adamc@477 521 printf("Usage: %s [-p <port>] [-t <thread-count>]\n", cmd);
adamc@477 522 }
adamc@477 523
adamc@502 524 static void sigint(int signum) {
adamc@502 525 printf("Exiting....\n");
adamc@502 526 exit(0);
adamc@502 527 }
adamc@502 528
adamc@687 529 static void initialize() {
adamc@734 530 uw_context ctx;
adamc@700 531 failure_kind fk;
adamc@687 532
adamc@734 533 init_crypto();
adamc@734 534
adamc@734 535 ctx = new_context();
adamc@734 536
adamc@698 537 if (!ctx)
adamc@698 538 exit(1);
adamc@698 539
adamc@700 540 for (fk = uw_initialize(ctx); fk == UNLIMITED_RETRY; fk = uw_initialize(ctx)) {
adamc@700 541 printf("Unlimited retry during init: %s\n", uw_error_message(ctx));
adamc@700 542 uw_db_rollback(ctx);
adamc@700 543 uw_reset(ctx);
adamc@700 544 }
adamc@700 545
adamc@700 546 if (fk != SUCCESS) {
adamc@700 547 printf("Failed to initialize database! %s\n", uw_error_message(ctx));
adamc@687 548 uw_db_rollback(ctx);
adamc@687 549 exit(1);
adamc@687 550 }
adamc@687 551
adamc@687 552 uw_free(ctx);
adamc@687 553 }
adamc@687 554
adamc@138 555 int main(int argc, char *argv[]) {
adamc@116 556 // The skeleton for this function comes from Beej's sockets tutorial.
adamc@138 557 int sockfd; // listen on sock_fd
adamc@116 558 struct sockaddr_in my_addr;
adamc@116 559 struct sockaddr_in their_addr; // connector's address information
adamc@116 560 int sin_size, yes = 1;
adamc@472 561 int uw_port = 8080, nthreads = 1, i, *names, opt;
adamc@502 562
adamc@502 563 signal(SIGINT, sigint);
adamc@505 564 signal(SIGPIPE, SIG_IGN);
adamc@505 565
adamc@477 566 while ((opt = getopt(argc, argv, "hp:t:")) != -1) {
adamc@472 567 switch (opt) {
adamc@472 568 case '?':
adamc@472 569 fprintf(stderr, "Unknown command-line option");
adamc@477 570 help(argv[0]);
adamc@472 571 return 1;
adamc@138 572
adamc@477 573 case 'h':
adamc@477 574 help(argv[0]);
adamc@477 575 return 0;
adamc@477 576
adamc@472 577 case 'p':
adamc@472 578 uw_port = atoi(optarg);
adamc@472 579 if (uw_port <= 0) {
adamc@472 580 fprintf(stderr, "Invalid port number\n");
adamc@477 581 help(argv[0]);
adamc@472 582 return 1;
adamc@472 583 }
adamc@472 584 break;
adamc@472 585
adamc@472 586 case 't':
adamc@472 587 nthreads = atoi(optarg);
adamc@472 588 if (nthreads <= 0) {
adamc@472 589 fprintf(stderr, "Invalid thread count\n");
adamc@477 590 help(argv[0]);
adamc@472 591 return 1;
adamc@472 592 }
adamc@472 593 break;
adamc@472 594
adamc@472 595 default:
adamc@472 596 fprintf(stderr, "Unexpected getopt() behavior\n");
adamc@472 597 return 1;
adamc@472 598 }
adamc@138 599 }
adamc@138 600
adamc@734 601 uw_global_init();
adamc@687 602 initialize();
adamc@687 603
adamc@138 604 names = calloc(nthreads, sizeof(int));
adamc@116 605
adamc@116 606 sockfd = socket(PF_INET, SOCK_STREAM, 0); // do some error checking!
adamc@116 607
adamc@116 608 if (sockfd < 0) {
adamc@116 609 fprintf(stderr, "Listener socket creation failed\n");
adamc@116 610 return 1;
adamc@116 611 }
adamc@116 612
adamc@116 613 if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) < 0) {
adamc@116 614 fprintf(stderr, "Listener socket option setting failed\n");
adamc@116 615 return 1;
adamc@116 616 }
adamc@116 617
adamc@116 618 my_addr.sin_family = AF_INET; // host byte order
adamc@311 619 my_addr.sin_port = htons(uw_port); // short, network byte order
adamc@116 620 my_addr.sin_addr.s_addr = INADDR_ANY; // auto-fill with my IP
adamc@116 621 memset(my_addr.sin_zero, '\0', sizeof my_addr.sin_zero);
adamc@116 622
adamc@116 623 if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof my_addr) < 0) {
adamc@116 624 fprintf(stderr, "Listener socket bind failed\n");
adamc@116 625 return 1;
adamc@116 626 }
adamc@116 627
adamc@311 628 if (listen(sockfd, uw_backlog) < 0) {
adamc@116 629 fprintf(stderr, "Socket listen failed\n");
adamc@116 630 return 1;
adamc@116 631 }
adamc@116 632
adamc@116 633 sin_size = sizeof their_addr;
adamc@116 634
adamc@311 635 printf("Listening on port %d....\n", uw_port);
adamc@116 636
adamc@667 637 {
adamc@667 638 pthread_t thread;
adamc@667 639 int name;
adamc@667 640
adamc@667 641 if (pthread_create(&thread, NULL, client_pruner, &name)) {
adamc@667 642 fprintf(stderr, "Error creating pruner thread\n");
adamc@667 643 return 1;
adamc@667 644 }
adamc@667 645 }
adamc@667 646
adamc@138 647 for (i = 0; i < nthreads; ++i) {
adamc@138 648 pthread_t thread;
adamc@138 649 names[i] = i;
adamc@138 650 if (pthread_create(&thread, NULL, worker, &names[i])) {
adamc@138 651 fprintf(stderr, "Error creating worker thread #%d\n", i);
adamc@138 652 return 1;
adamc@138 653 }
adamc@138 654 }
adamc@138 655
adamc@116 656 while (1) {
adamc@138 657 int new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size);
adamc@116 658
adamc@116 659 if (new_fd < 0) {
adamc@116 660 fprintf(stderr, "Socket accept failed\n");
adamc@116 661 return 1;
adamc@116 662 }
adamc@116 663
adamc@116 664 printf("Accepted connection.\n");
adamc@138 665
adamc@138 666 pthread_mutex_lock(&queue_mutex);
adamc@138 667 enqueue(new_fd);
adamc@139 668 pthread_cond_broadcast(&queue_cond);
adamc@138 669 pthread_mutex_unlock(&queue_mutex);
adamc@116 670 }
adamc@102 671 }