aboutsummaryrefslogtreecommitdiff
path: root/src/transport/plugin_transport_http_server.c
diff options
context:
space:
mode:
authorMatthias Wachs <wachs@net.in.tum.de>2011-09-16 19:44:24 +0000
committerMatthias Wachs <wachs@net.in.tum.de>2011-09-16 19:44:24 +0000
commit426be645faa129f78dc8484f8c596a7cb00549b2 (patch)
tree2f5361b83fc127139cdea3054ca7000919e9fff6 /src/transport/plugin_transport_http_server.c
parent261df49654bbd239d9d164177e1d362ed09f8411 (diff)
downloadgnunet-426be645faa129f78dc8484f8c596a7cb00549b2.tar.gz
gnunet-426be645faa129f78dc8484f8c596a7cb00549b2.zip
sending and receiving implemented
Diffstat (limited to 'src/transport/plugin_transport_http_server.c')
-rw-r--r--src/transport/plugin_transport_http_server.c92
1 files changed, 60 insertions, 32 deletions
diff --git a/src/transport/plugin_transport_http_server.c b/src/transport/plugin_transport_http_server.c
index 98fbfda1c..0c2ab54cf 100644
--- a/src/transport/plugin_transport_http_server.c
+++ b/src/transport/plugin_transport_http_server.c
@@ -37,7 +37,7 @@ server_log (void *arg, const char *fmt, va_list ap)
37 37
38 vsnprintf (text, sizeof (text), fmt, ap); 38 vsnprintf (text, sizeof (text), fmt, ap);
39 va_end (ap); 39 va_end (ap);
40 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "server: %s\n", text); 40 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Server: %s\n", text);
41} 41}
42 42
43struct ServerConnection 43struct ServerConnection
@@ -70,7 +70,7 @@ server_accept_cb (void *cls, const struct sockaddr *addr, socklen_t addr_len)
70 return MHD_YES; 70 return MHD_YES;
71 else 71 else
72 { 72 {
73 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "server: Cannot accept new connections\n"); 73 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Server: Cannot accept new connections\n");
74 return MHD_NO; 74 return MHD_NO;
75 } 75 }
76} 76}
@@ -267,12 +267,12 @@ server_receive_mst_cb (void *cls, void *client,
267 * @return bytes written to buffer 267 * @return bytes written to buffer
268 */ 268 */
269static ssize_t 269static ssize_t
270mhd_send_callback (void *cls, uint64_t pos, char *buf, size_t max) 270server_send_callback (void *cls, uint64_t pos, char *buf, size_t max)
271{ 271{
272 struct Session *s = cls; 272 struct Session *s = cls;
273 struct HTTP_Message *msg; 273 struct HTTP_Message *msg;
274 int bytes_read = 0; 274 int bytes_read = 0;
275 275 //static int c = 0;
276 msg = s->msg_head; 276 msg = s->msg_head;
277 if (msg != NULL) 277 if (msg != NULL)
278 { 278 {
@@ -299,7 +299,8 @@ mhd_send_callback (void *cls, uint64_t pos, char *buf, size_t max)
299 GNUNET_free (msg); 299 GNUNET_free (msg);
300 } 300 }
301 } 301 }
302#if DEBUG_CONNECTIONS 302
303#if VERBOSE_CLIENT
303 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connection %X: MHD has sent %u bytes\n", 304 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connection %X: MHD has sent %u bytes\n",
304 s, bytes_read); 305 s, bytes_read);
305#endif 306#endif
@@ -371,7 +372,7 @@ server_access_cb (void *cls, struct MHD_Connection *mhd_connection,
371 plugin->cur_connections++; 372 plugin->cur_connections++;
372 373
373#if VERBOSE_SERVER 374#if VERBOSE_SERVER
374 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "server: New inbound connection from %s with tag %u\n", GNUNET_h2s_full(&(target.hashPubKey)), tag); 375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Server: New inbound connection from %s with tag %u\n", GNUNET_i2s(&target), tag);
375#endif 376#endif
376 /* find duplicate session */ 377 /* find duplicate session */
377 378
@@ -388,7 +389,7 @@ server_access_cb (void *cls, struct MHD_Connection *mhd_connection,
388 if (t != NULL) 389 if (t != NULL)
389 { 390 {
390#if VERBOSE_SERVER 391#if VERBOSE_SERVER
391 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "server: Duplicate session, dismissing new connection from peer `%s'\n", GNUNET_i2s (&target)); 392 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Server: Duplicate session, dismissing new connection from peer `%s'\n", GNUNET_i2s (&target));
392#endif 393#endif
393 goto error; 394 goto error;
394 } 395 }
@@ -411,13 +412,13 @@ server_access_cb (void *cls, struct MHD_Connection *mhd_connection,
411 goto create; 412 goto create;
412 413
413#if VERBOSE_SERVER 414#if VERBOSE_SERVER
414 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "server: Found existing semi-session for `%s'\n", GNUNET_i2s (&target)); 415 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Server: Found existing semi-session for `%s'\n", GNUNET_i2s (&target));
415#endif 416#endif
416 417
417 if ((direction == _SEND) && (t->server_send != NULL)) 418 if ((direction == _SEND) && (t->server_send != NULL))
418 { 419 {
419#if VERBOSE_SERVER 420#if VERBOSE_SERVER
420 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "server: Duplicate GET session, dismissing new connection from peer `%s'\n", GNUNET_i2s (&target)); 421 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Server: Duplicate GET session, dismissing new connection from peer `%s'\n", GNUNET_i2s (&target));
421#endif 422#endif
422 goto error; 423 goto error;
423 } 424 }
@@ -427,7 +428,7 @@ server_access_cb (void *cls, struct MHD_Connection *mhd_connection,
427 GNUNET_CONTAINER_DLL_remove(plugin->server_semi_head, plugin->server_semi_tail, s); 428 GNUNET_CONTAINER_DLL_remove(plugin->server_semi_head, plugin->server_semi_tail, s);
428 GNUNET_CONTAINER_DLL_insert(plugin->head, plugin->tail, s); 429 GNUNET_CONTAINER_DLL_insert(plugin->head, plugin->tail, s);
429#if VERBOSE_SERVER 430#if VERBOSE_SERVER
430 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "server: Found matching semi-session, merging session for peer `%s'\n", GNUNET_i2s (&target)); 431 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Server: Found matching semi-session, merging session for peer `%s'\n", GNUNET_i2s (&target));
431#endif 432#endif
432 433
433 goto found; 434 goto found;
@@ -435,7 +436,7 @@ server_access_cb (void *cls, struct MHD_Connection *mhd_connection,
435 if ((direction == _RECEIVE) && (t->server_recv != NULL)) 436 if ((direction == _RECEIVE) && (t->server_recv != NULL))
436 { 437 {
437#if VERBOSE_SERVER 438#if VERBOSE_SERVER
438 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "server: Duplicate PUT session, dismissing new connection from peer `%s'\n", GNUNET_i2s (&target)); 439 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Server: Duplicate PUT session, dismissing new connection from peer `%s'\n", GNUNET_i2s (&target));
439#endif 440#endif
440 goto error; 441 goto error;
441 } 442 }
@@ -445,7 +446,7 @@ server_access_cb (void *cls, struct MHD_Connection *mhd_connection,
445 GNUNET_CONTAINER_DLL_remove(plugin->server_semi_head, plugin->server_semi_tail, s); 446 GNUNET_CONTAINER_DLL_remove(plugin->server_semi_head, plugin->server_semi_tail, s);
446 GNUNET_CONTAINER_DLL_insert(plugin->head, plugin->tail, s); 447 GNUNET_CONTAINER_DLL_insert(plugin->head, plugin->tail, s);
447#if VERBOSE_SERVER 448#if VERBOSE_SERVER
448 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "server: Found matching semi-session, merging session for peer `%s'\n", GNUNET_i2s (&target)); 449 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Server: Found matching semi-session, merging session for peer `%s'\n", GNUNET_i2s (&target));
449#endif 450#endif
450 goto found; 451 goto found;
451 } 452 }
@@ -453,7 +454,7 @@ server_access_cb (void *cls, struct MHD_Connection *mhd_connection,
453create: 454create:
454/* create new session */ 455/* create new session */
455#if VERBOSE_SERVER 456#if VERBOSE_SERVER
456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "server: Creating new session for peer `%s' \n", GNUNET_i2s (&target)); 457 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Server: Creating new session for peer `%s' \n", GNUNET_i2s (&target));
457#endif 458#endif
458 459
459 s = create_session(plugin, 460 s = create_session(plugin,
@@ -473,7 +474,7 @@ create:
473 474
474 goto found; 475 goto found;
475error: 476error:
476 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "server: Invalid connection request\n"); 477 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Server: Invalid connection request\n");
477 response = MHD_create_response_from_data (strlen (HTTP_ERROR_RESPONSE),HTTP_ERROR_RESPONSE, MHD_NO, MHD_NO); 478 response = MHD_create_response_from_data (strlen (HTTP_ERROR_RESPONSE),HTTP_ERROR_RESPONSE, MHD_NO, MHD_NO);
478 res = MHD_queue_response (mhd_connection, MHD_HTTP_NOT_FOUND, response); 479 res = MHD_queue_response (mhd_connection, MHD_HTTP_NOT_FOUND, response);
479 MHD_destroy_response (response); 480 MHD_destroy_response (response);
@@ -488,6 +489,10 @@ found:
488 if (direction == _RECEIVE) 489 if (direction == _RECEIVE)
489 s->server_recv = sc; 490 s->server_recv = sc;
490 491
492 int to = (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value / 1000);
493 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Server: Setting Timeout to %u\n", to);
494 //MHD_set_connection_option (mhd_connection, MHD_CONNECTION_OPTION_TIMEOUT, to);
495
491 (*httpSessionCache) = sc; 496 (*httpSessionCache) = sc;
492 } 497 }
493 498
@@ -512,8 +517,7 @@ found:
512 GNUNET_assert (s != NULL); 517 GNUNET_assert (s != NULL);
513 if (sc->direction == _SEND) 518 if (sc->direction == _SEND)
514 { 519 {
515 response = 520 response = MHD_create_response_from_callback (-1, 32 * 1024, &server_send_callback,
516 MHD_create_response_from_callback (-1, 32 * 1024, &mhd_send_callback,
517 s, NULL); 521 s, NULL);
518 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response); 522 res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
519 MHD_destroy_response (response); 523 MHD_destroy_response (response);
@@ -536,8 +540,8 @@ found:
536 { 540 {
537#if VERBOSE_SERVER 541#if VERBOSE_SERVER
538 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name, 542 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
539 "Server: peer `%s' PUT on address `%s' received %u bytes\n", 543 "Server: peer `%s' PUT on address `%s' received %Zu bytes\n",
540 GNUNET_i2s (&s->target), GNUNET_a2s (s->addr, s->addrlen)); 544 GNUNET_i2s (&s->target), GNUNET_a2s (s->addr, s->addrlen), *upload_data_size);
541#endif 545#endif
542 if ((GNUNET_TIME_absolute_get().abs_value < s->delay.abs_value)) 546 if ((GNUNET_TIME_absolute_get().abs_value < s->delay.abs_value))
543 { 547 {
@@ -551,7 +555,7 @@ found:
551 } 555 }
552 res = GNUNET_SERVER_mst_receive (s->msg_tk, s, upload_data, *upload_data_size, GNUNET_NO, GNUNET_NO); 556 res = GNUNET_SERVER_mst_receive (s->msg_tk, s, upload_data, *upload_data_size, GNUNET_NO, GNUNET_NO);
553 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 557 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
554 "Server: Received %u bytes\n", 558 "Server: Received %Zu bytes\n",
555 *upload_data_size); 559 *upload_data_size);
556 (*upload_data_size) = 0; 560 (*upload_data_size) = 0;
557 } 561 }
@@ -573,6 +577,16 @@ found:
573 return res; 577 return res;
574} 578}
575 579
580/**
581 * Function that queries MHD's select sets and
582 * starts the task waiting for them.
583 * @param plugin plugin
584 * @param daemon_handle the MHD daemon handle
585 * @return gnunet task identifier
586 */
587static GNUNET_SCHEDULER_TaskIdentifier
588server_schedule (struct Plugin *plugin, struct MHD_Daemon *daemon_handle);
589
576static void 590static void
577server_disconnect_cb (void *cls, struct MHD_Connection *connection, 591server_disconnect_cb (void *cls, struct MHD_Connection *connection,
578 void **httpSessionCache) 592 void **httpSessionCache)
@@ -592,33 +606,37 @@ server_disconnect_cb (void *cls, struct MHD_Connection *connection,
592 { 606 {
593#if VERBOSE_SERVER 607#if VERBOSE_SERVER
594 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name, 608 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
595 "Server: peer `%s' PUT on address `%s' disconnected\n", 609 "Server: peer `%s' GET on address `%s' disconnected\n",
596 GNUNET_i2s (&s->target), GNUNET_a2s (s->addr, s->addrlen)); 610 GNUNET_i2s (&s->target), GNUNET_a2s (s->addr, s->addrlen));
597#endif 611#endif
598 s->server_send = NULL; 612 s->server_send = NULL;
599 /* FIXME miminimize timeout here */ 613
600 if (s->server_recv != NULL) 614 if (s->server_recv != NULL)
601 { 615 {
602 tc = s->server_recv; 616 tc = s->server_recv;
603 tc->disconnect = GNUNET_YES; 617 tc->disconnect = GNUNET_YES;
618 MHD_set_connection_option (sc->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT, 1);
604 } 619 }
605 } 620 }
606 if (sc->direction == _RECEIVE) 621 if (sc->direction == _RECEIVE)
607 { 622 {
608#if VERBOSE_SERVER 623#if VERBOSE_SERVER
609 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name, 624 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
610 "Server: peer `%s' GET on address `%s' disconnected\n", 625 "Server: peer `%s' PUT on address `%s' disconnected\n",
611 GNUNET_i2s (&s->target), GNUNET_a2s (s->addr, s->addrlen)); 626 GNUNET_i2s (&s->target), GNUNET_a2s (s->addr, s->addrlen));
612#endif 627#endif
613 s->server_recv = NULL; 628 s->server_recv = NULL;
614 //MHD_
615 if (s->server_send != NULL) 629 if (s->server_send != NULL)
616 { 630 {
617 tc = s->server_send; 631 tc = s->server_send;
618 tc->disconnect = GNUNET_YES; 632 tc->disconnect = GNUNET_YES;
633 MHD_set_connection_option (sc->mhd_conn, MHD_CONNECTION_OPTION_TIMEOUT, 1);
619 } 634 }
620 if (s->msg_tk != NULL) 635 if (s->msg_tk != NULL)
636 {
621 GNUNET_SERVER_mst_destroy(s->msg_tk); 637 GNUNET_SERVER_mst_destroy(s->msg_tk);
638 s->msg_tk = NULL;
639 }
622 } 640 }
623 GNUNET_free (sc); 641 GNUNET_free (sc);
624 642
@@ -635,6 +653,19 @@ server_disconnect_cb (void *cls, struct MHD_Connection *connection,
635 } 653 }
636 plugin->cur_connections--; 654 plugin->cur_connections--;
637 655
656 if (plugin->server_v4_task != GNUNET_SCHEDULER_NO_TASK)
657 {
658 GNUNET_SCHEDULER_cancel(plugin->server_v4_task);
659 plugin->server_v4_task = GNUNET_SCHEDULER_NO_TASK;
660 }
661 plugin->server_v4_task = server_schedule (plugin, plugin->server_v4);
662
663 if (plugin->server_v6_task != GNUNET_SCHEDULER_NO_TASK)
664 {
665 GNUNET_SCHEDULER_cancel(plugin->server_v6_task);
666 plugin->server_v6_task = GNUNET_SCHEDULER_NO_TASK;
667 }
668 plugin->server_v6_task = server_schedule (plugin, plugin->server_v6);
638 669
639 if ((s->server_send == NULL) && (s->server_recv == NULL)) 670 if ((s->server_send == NULL) && (s->server_recv == NULL))
640 { 671 {
@@ -643,6 +674,11 @@ server_disconnect_cb (void *cls, struct MHD_Connection *connection,
643 "Server: peer `%s' on address `%s' disconnected\n", 674 "Server: peer `%s' on address `%s' disconnected\n",
644 GNUNET_i2s (&s->target), GNUNET_a2s (s->addr, s->addrlen)); 675 GNUNET_i2s (&s->target), GNUNET_a2s (s->addr, s->addrlen));
645#endif 676#endif
677 if (s->msg_tk != NULL)
678 {
679 GNUNET_SERVER_mst_destroy(s->msg_tk);
680 s->msg_tk = NULL;
681 }
646 682
647 notify_session_end(s->plugin, &s->target, s); 683 notify_session_end(s->plugin, &s->target, s);
648 } 684 }
@@ -681,15 +717,7 @@ server_send (struct Session *s, struct HTTP_Message * msg)
681 return GNUNET_OK; 717 return GNUNET_OK;
682} 718}
683 719
684/** 720
685 * Function that queries MHD's select sets and
686 * starts the task waiting for them.
687 * @param plugin plugin
688 * @param daemon_handle the MHD daemon handle
689 * @return gnunet task identifier
690 */
691static GNUNET_SCHEDULER_TaskIdentifier
692server_schedule (struct Plugin *plugin, struct MHD_Daemon *daemon_handle);
693 721
694/** 722/**
695 * Call MHD IPv4 to process pending requests and then go back 723 * Call MHD IPv4 to process pending requests and then go back