diff options
-rw-r--r-- | src/util/network.c | 2 | ||||
-rw-r--r-- | src/util/server.c | 110 | ||||
-rw-r--r-- | src/util/test_client.c | 22 | ||||
-rw-r--r-- | src/util/test_server.c | 10 | ||||
-rw-r--r-- | src/util/test_server_with_client.c | 2 |
5 files changed, 133 insertions, 13 deletions
diff --git a/src/util/network.c b/src/util/network.c index aae9d6872..00b2fac02 100644 --- a/src/util/network.c +++ b/src/util/network.c | |||
@@ -40,7 +40,7 @@ | |||
40 | #include "gnunet_network_lib.h" | 40 | #include "gnunet_network_lib.h" |
41 | #include "gnunet_scheduler_lib.h" | 41 | #include "gnunet_scheduler_lib.h" |
42 | 42 | ||
43 | #define DEBUG_NETWORK GNUNET_NO | 43 | #define DEBUG_NETWORK GNUNET_YES |
44 | 44 | ||
45 | /** | 45 | /** |
46 | * List of address families to give as hints to | 46 | * List of address families to give as hints to |
diff --git a/src/util/server.c b/src/util/server.c index 282e036e8..ca4a5f0dd 100644 --- a/src/util/server.c +++ b/src/util/server.c | |||
@@ -34,6 +34,8 @@ | |||
34 | #include "gnunet_server_lib.h" | 34 | #include "gnunet_server_lib.h" |
35 | #include "gnunet_time_lib.h" | 35 | #include "gnunet_time_lib.h" |
36 | 36 | ||
37 | #define DEBUG_SERVER GNUNET_YES | ||
38 | |||
37 | /** | 39 | /** |
38 | * List of arrays of message handlers. | 40 | * List of arrays of message handlers. |
39 | */ | 41 | */ |
@@ -274,6 +276,10 @@ destroy_server (struct GNUNET_SERVER_Handle *server) | |||
274 | struct HandlerList *hpos; | 276 | struct HandlerList *hpos; |
275 | struct NotifyList *npos; | 277 | struct NotifyList *npos; |
276 | 278 | ||
279 | #if DEBUG_SERVER | ||
280 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
281 | "Server shutting down.\n"); | ||
282 | #endif | ||
277 | GNUNET_assert (server->listen_socket == -1); | 283 | GNUNET_assert (server->listen_socket == -1); |
278 | GNUNET_break (0 == CLOSE (server->shutpipe[0])); | 284 | GNUNET_break (0 == CLOSE (server->shutpipe[0])); |
279 | GNUNET_break (0 == CLOSE (server->shutpipe[1])); | 285 | GNUNET_break (0 == CLOSE (server->shutpipe[1])); |
@@ -330,6 +336,10 @@ process_listen_socket (void *cls, | |||
330 | server->maxbuf); | 336 | server->maxbuf); |
331 | if (sock != NULL) | 337 | if (sock != NULL) |
332 | { | 338 | { |
339 | #if DEBUG_SERVER | ||
340 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
341 | "Server accepted incoming connection.\n"); | ||
342 | #endif | ||
333 | client = GNUNET_SERVER_connect_socket (server, sock); | 343 | client = GNUNET_SERVER_connect_socket (server, sock); |
334 | /* decrement reference count, we don't keep "client" alive */ | 344 | /* decrement reference count, we don't keep "client" alive */ |
335 | GNUNET_SERVER_client_drop (client); | 345 | GNUNET_SERVER_client_drop (client); |
@@ -405,6 +415,11 @@ open_listen_socket (const struct sockaddr *serverAddr, socklen_t socklen) | |||
405 | GNUNET_break (0 == CLOSE (fd)); | 415 | GNUNET_break (0 == CLOSE (fd)); |
406 | return -1; | 416 | return -1; |
407 | } | 417 | } |
418 | #if DEBUG_SERVER | ||
419 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
420 | "Server starts to listen on port %u.\n", | ||
421 | port); | ||
422 | #endif | ||
408 | return fd; | 423 | return fd; |
409 | } | 424 | } |
410 | 425 | ||
@@ -551,6 +566,12 @@ GNUNET_SERVER_inject (struct GNUNET_SERVER_Handle *server, | |||
551 | 566 | ||
552 | type = ntohs (message->type); | 567 | type = ntohs (message->type); |
553 | size = ntohs (message->size); | 568 | size = ntohs (message->size); |
569 | #if DEBUG_SERVER | ||
570 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
571 | "Server schedules transmission of %u-byte message of type %u to client.\n", | ||
572 | size, | ||
573 | type); | ||
574 | #endif | ||
554 | pos = server->handlers; | 575 | pos = server->handlers; |
555 | found = GNUNET_NO; | 576 | found = GNUNET_NO; |
556 | while (pos != NULL) | 577 | while (pos != NULL) |
@@ -633,6 +654,13 @@ shutdown_incoming_processing (struct GNUNET_SERVER_Client *client) | |||
633 | } | 654 | } |
634 | 655 | ||
635 | 656 | ||
657 | /** | ||
658 | * Go over the contents of the client buffer; as long as full messages | ||
659 | * are available, pass them on for processing. Update the buffer | ||
660 | * accordingly. Handles fatal errors by shutting down the connection. | ||
661 | * | ||
662 | * @param client identifies which client receive buffer to process | ||
663 | */ | ||
636 | static void | 664 | static void |
637 | process_client_buffer (struct GNUNET_SERVER_Client *client) | 665 | process_client_buffer (struct GNUNET_SERVER_Client *client) |
638 | { | 666 | { |
@@ -642,13 +670,33 @@ process_client_buffer (struct GNUNET_SERVER_Client *client) | |||
642 | 670 | ||
643 | client->in_process_client_buffer = GNUNET_YES; | 671 | client->in_process_client_buffer = GNUNET_YES; |
644 | server = client->server; | 672 | server = client->server; |
673 | #if DEBUG_SERVER | ||
674 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
675 | "Private buffer contains %u bytes; client is %s and we are %s\n", | ||
676 | client->receive_pos, | ||
677 | client->suspended ? "suspended" : "up", | ||
678 | client->shutdown_now ? "in shutdown" : "running"); | ||
679 | #endif | ||
645 | while ((client->receive_pos >= sizeof (struct GNUNET_MessageHeader)) && | 680 | while ((client->receive_pos >= sizeof (struct GNUNET_MessageHeader)) && |
646 | (0 == client->suspended) && (GNUNET_YES != client->shutdown_now)) | 681 | (0 == client->suspended) && (GNUNET_YES != client->shutdown_now)) |
647 | { | 682 | { |
648 | hdr = (const struct GNUNET_MessageHeader *) &client->incoming_buffer; | 683 | hdr = (const struct GNUNET_MessageHeader *) &client->incoming_buffer; |
649 | msize = ntohs (hdr->size); | 684 | msize = ntohs (hdr->size); |
650 | if (msize > client->receive_pos) | 685 | if (msize > client->receive_pos) |
651 | break; | 686 | { |
687 | #if DEBUG_SERVER | ||
688 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
689 | "Total message size is %u, we only have %u bytes; need more data\n", | ||
690 | msize, | ||
691 | client->receive_pos); | ||
692 | #endif | ||
693 | break; | ||
694 | } | ||
695 | #if DEBUG_SERVER | ||
696 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
697 | "Passing %u bytes to callback for processing\n", | ||
698 | msize); | ||
699 | #endif | ||
652 | if ((msize < sizeof (struct GNUNET_MessageHeader)) || | 700 | if ((msize < sizeof (struct GNUNET_MessageHeader)) || |
653 | (GNUNET_OK != GNUNET_SERVER_inject (server, client, hdr))) | 701 | (GNUNET_OK != GNUNET_SERVER_inject (server, client, hdr))) |
654 | { | 702 | { |
@@ -671,12 +719,21 @@ process_client_buffer (struct GNUNET_SERVER_Client *client) | |||
671 | 719 | ||
672 | /** | 720 | /** |
673 | * We are receiving an incoming message. Process it. | 721 | * We are receiving an incoming message. Process it. |
722 | * | ||
723 | * @param cls our closure (handle for the client) | ||
724 | * @param buf buffer with data received from network | ||
725 | * @param available number of bytes available in buf | ||
726 | * @param addr address of the sender | ||
727 | * @param addrlen length of addr | ||
728 | * @param errCode code indicating errors receiving, 0 for success | ||
674 | */ | 729 | */ |
675 | static void | 730 | static void |
676 | process_incoming (void *cls, | 731 | process_incoming (void *cls, |
677 | const void *buf, | 732 | const void *buf, |
678 | size_t available, | 733 | size_t available, |
679 | const struct sockaddr *addr, socklen_t addrlen, int errCode) | 734 | const struct sockaddr *addr, |
735 | socklen_t addrlen, | ||
736 | int errCode) | ||
680 | { | 737 | { |
681 | struct GNUNET_SERVER_Client *client = cls; | 738 | struct GNUNET_SERVER_Client *client = cls; |
682 | struct GNUNET_SERVER_Handle *server = client->server; | 739 | struct GNUNET_SERVER_Handle *server = client->server; |
@@ -695,6 +752,12 @@ process_incoming (void *cls, | |||
695 | shutdown_incoming_processing (client); | 752 | shutdown_incoming_processing (client); |
696 | return; | 753 | return; |
697 | } | 754 | } |
755 | #if DEBUG_SERVER | ||
756 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
757 | "Server receives %u bytes from `%s'.\n", | ||
758 | available, | ||
759 | GNUNET_a2s(addr, addrlen)); | ||
760 | #endif | ||
698 | GNUNET_SERVER_client_keep (client); | 761 | GNUNET_SERVER_client_keep (client); |
699 | client->last_activity = GNUNET_TIME_absolute_get (); | 762 | client->last_activity = GNUNET_TIME_absolute_get (); |
700 | /* process data (if available) */ | 763 | /* process data (if available) */ |
@@ -703,6 +766,11 @@ process_incoming (void *cls, | |||
703 | maxcpy = available; | 766 | maxcpy = available; |
704 | if (maxcpy > sizeof (client->incoming_buffer) - client->receive_pos) | 767 | if (maxcpy > sizeof (client->incoming_buffer) - client->receive_pos) |
705 | maxcpy = sizeof (client->incoming_buffer) - client->receive_pos; | 768 | maxcpy = sizeof (client->incoming_buffer) - client->receive_pos; |
769 | #if DEBUG_SERVER | ||
770 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
771 | "Can copy %u bytes to private buffer\n", | ||
772 | maxcpy); | ||
773 | #endif | ||
706 | memcpy (&client->incoming_buffer[client->receive_pos], cbuf, maxcpy); | 774 | memcpy (&client->incoming_buffer[client->receive_pos], cbuf, maxcpy); |
707 | client->receive_pos += maxcpy; | 775 | client->receive_pos += maxcpy; |
708 | cbuf += maxcpy; | 776 | cbuf += maxcpy; |
@@ -711,6 +779,13 @@ process_incoming (void *cls, | |||
711 | { | 779 | { |
712 | if (available > 0) | 780 | if (available > 0) |
713 | { | 781 | { |
782 | #if DEBUG_SERVER | ||
783 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
784 | "Client has suspended processing; copying %u bytes to side buffer to be used later.\n", | ||
785 | available); | ||
786 | #endif | ||
787 | GNUNET_assert (client->side_buf_size == 0); | ||
788 | GNUNET_assert (client->side_buf == NULL); | ||
714 | client->side_buf_size = available; | 789 | client->side_buf_size = available; |
715 | client->side_buf = GNUNET_malloc (available); | 790 | client->side_buf = GNUNET_malloc (available); |
716 | memcpy (client->side_buf, cbuf, available); | 791 | memcpy (client->side_buf, cbuf, available); |
@@ -718,6 +793,10 @@ process_incoming (void *cls, | |||
718 | } | 793 | } |
719 | break; /* do not run next client iteration! */ | 794 | break; /* do not run next client iteration! */ |
720 | } | 795 | } |
796 | #if DEBUG_SERVER | ||
797 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
798 | "Now processing messages in private buffer\n"); | ||
799 | #endif | ||
721 | process_client_buffer (client); | 800 | process_client_buffer (client); |
722 | } | 801 | } |
723 | GNUNET_assert (available == 0); | 802 | GNUNET_assert (available == 0); |
@@ -736,6 +815,9 @@ process_incoming (void *cls, | |||
736 | } | 815 | } |
737 | 816 | ||
738 | 817 | ||
818 | /** | ||
819 | * FIXME: document. | ||
820 | */ | ||
739 | static void | 821 | static void |
740 | restart_processing (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | 822 | restart_processing (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) |
741 | { | 823 | { |
@@ -768,6 +850,17 @@ add_client (struct GNUNET_SERVER_Handle *server, | |||
768 | &process_incoming, client); | 850 | &process_incoming, client); |
769 | } | 851 | } |
770 | 852 | ||
853 | |||
854 | /** | ||
855 | * Create a request for receiving data from a socket. | ||
856 | * | ||
857 | * @param cls identifies the socket to receive from | ||
858 | * @param max how much data to read at most | ||
859 | * @param timeout when should this operation time out | ||
860 | * @param receiver function to call for processing | ||
861 | * @param receiver_cls closure for receiver | ||
862 | * @return task identifier that can be used to cancel the operation | ||
863 | */ | ||
771 | static GNUNET_SCHEDULER_TaskIdentifier | 864 | static GNUNET_SCHEDULER_TaskIdentifier |
772 | sock_receive (void *cls, | 865 | sock_receive (void *cls, |
773 | size_t max, | 866 | size_t max, |
@@ -777,6 +870,13 @@ sock_receive (void *cls, | |||
777 | return GNUNET_NETWORK_receive (cls, max, timeout, receiver, receiver_cls); | 870 | return GNUNET_NETWORK_receive (cls, max, timeout, receiver, receiver_cls); |
778 | } | 871 | } |
779 | 872 | ||
873 | |||
874 | /** | ||
875 | * Wrapper to cancel receiving from a socket. | ||
876 | * | ||
877 | * @param cls handle to the GNUNET_NETWORK_SocketHandle to cancel | ||
878 | * @param tc task ID that was returned by GNUNET_NETWORK_receive | ||
879 | */ | ||
780 | static void | 880 | static void |
781 | sock_receive_cancel (void *cls, GNUNET_SCHEDULER_TaskIdentifier ti) | 881 | sock_receive_cancel (void *cls, GNUNET_SCHEDULER_TaskIdentifier ti) |
782 | { | 882 | { |
@@ -784,6 +884,9 @@ sock_receive_cancel (void *cls, GNUNET_SCHEDULER_TaskIdentifier ti) | |||
784 | } | 884 | } |
785 | 885 | ||
786 | 886 | ||
887 | /** | ||
888 | * FIXME: document. | ||
889 | */ | ||
787 | static void * | 890 | static void * |
788 | sock_notify_transmit_ready (void *cls, | 891 | sock_notify_transmit_ready (void *cls, |
789 | size_t size, | 892 | size_t size, |
@@ -796,6 +899,9 @@ sock_notify_transmit_ready (void *cls, | |||
796 | } | 899 | } |
797 | 900 | ||
798 | 901 | ||
902 | /** | ||
903 | * FIXME: document. | ||
904 | */ | ||
799 | static void | 905 | static void |
800 | sock_notify_transmit_ready_cancel (void *cls, void *h) | 906 | sock_notify_transmit_ready_cancel (void *cls, void *h) |
801 | { | 907 | { |
diff --git a/src/util/test_client.c b/src/util/test_client.c index 3545b9fa9..a1a268b14 100644 --- a/src/util/test_client.c +++ b/src/util/test_client.c | |||
@@ -61,6 +61,8 @@ copy_msg (void *cls, size_t size, void *buf) | |||
61 | GNUNET_SERVER_receive_done (ctx->client, GNUNET_OK); | 61 | GNUNET_SERVER_receive_done (ctx->client, GNUNET_OK); |
62 | GNUNET_free (cpy); | 62 | GNUNET_free (cpy); |
63 | GNUNET_free (ctx); | 63 | GNUNET_free (ctx); |
64 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
65 | "Message bounced back to client\n"); | ||
64 | return sizeof (struct GNUNET_MessageHeader); | 66 | return sizeof (struct GNUNET_MessageHeader); |
65 | } | 67 | } |
66 | 68 | ||
@@ -76,6 +78,8 @@ echo_cb (void *cls, | |||
76 | struct CopyContext *cc; | 78 | struct CopyContext *cc; |
77 | struct GNUNET_MessageHeader *cpy; | 79 | struct GNUNET_MessageHeader *cpy; |
78 | 80 | ||
81 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
82 | "Receiving message from client, bouncing back\n"); | ||
79 | GNUNET_assert (sizeof (struct GNUNET_MessageHeader) == | 83 | GNUNET_assert (sizeof (struct GNUNET_MessageHeader) == |
80 | ntohs (message->size)); | 84 | ntohs (message->size)); |
81 | cc = GNUNET_malloc (sizeof (struct CopyContext)); | 85 | cc = GNUNET_malloc (sizeof (struct CopyContext)); |
@@ -104,9 +108,11 @@ recv_bounce (void *cls, const struct GNUNET_MessageHeader *got) | |||
104 | struct GNUNET_MessageHeader msg; | 108 | struct GNUNET_MessageHeader msg; |
105 | 109 | ||
106 | GNUNET_assert (got != NULL); /* timeout */ | 110 | GNUNET_assert (got != NULL); /* timeout */ |
111 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
112 | "Receiving bounce, checking content\n"); | ||
107 | msg.type = htons (MY_TYPE); | 113 | msg.type = htons (MY_TYPE); |
108 | msg.size = htons (sizeof (msg)); | 114 | msg.size = htons (sizeof (struct GNUNET_MessageHeader)); |
109 | GNUNET_assert (0 == memcmp (got, &msg, sizeof (msg))); | 115 | GNUNET_assert (0 == memcmp (got, &msg, sizeof (struct GNUNET_MessageHeader))); |
110 | GNUNET_CLIENT_disconnect (client); | 116 | GNUNET_CLIENT_disconnect (client); |
111 | client = NULL; | 117 | client = NULL; |
112 | GNUNET_SERVER_destroy (server); | 118 | GNUNET_SERVER_destroy (server); |
@@ -121,7 +127,9 @@ make_msg (void *cls, size_t size, void *buf) | |||
121 | struct GNUNET_MessageHeader *msg = buf; | 127 | struct GNUNET_MessageHeader *msg = buf; |
122 | GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader)); | 128 | GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader)); |
123 | msg->type = htons (MY_TYPE); | 129 | msg->type = htons (MY_TYPE); |
124 | msg->size = htons (sizeof (msg)); | 130 | msg->size = htons (sizeof (struct GNUNET_MessageHeader)); |
131 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
132 | "Creating message for transmission\n"); | ||
125 | return sizeof (struct GNUNET_MessageHeader); | 133 | return sizeof (struct GNUNET_MessageHeader); |
126 | } | 134 | } |
127 | 135 | ||
@@ -185,7 +193,13 @@ main (int argc, char *argv[]) | |||
185 | { | 193 | { |
186 | int ret = 0; | 194 | int ret = 0; |
187 | 195 | ||
188 | GNUNET_log_setup ("test_client", "WARNING", NULL); | 196 | GNUNET_log_setup ("test_client", |
197 | #if VERBOSE | ||
198 | "DEBUG", | ||
199 | #else | ||
200 | "WARNING", | ||
201 | #endif | ||
202 | NULL); | ||
189 | ret += check (); | 203 | ret += check (); |
190 | 204 | ||
191 | return ret; | 205 | return ret; |
diff --git a/src/util/test_server.c b/src/util/test_server.c index 475d411db..c26d64270 100644 --- a/src/util/test_server.c +++ b/src/util/test_server.c | |||
@@ -83,8 +83,8 @@ my_receive (void *cls, | |||
83 | case 1: | 83 | case 1: |
84 | *ok = 2; /* report success */ | 84 | *ok = 2; /* report success */ |
85 | msg.type = htons (MY_TYPE2); | 85 | msg.type = htons (MY_TYPE2); |
86 | msg.size = htons (sizeof (msg)); | 86 | msg.size = htons (sizeof (struct GNUNET_MessageHeader)); |
87 | receiver (receiver_cls, &msg, sizeof (msg), NULL, 0, 0); | 87 | receiver (receiver_cls, &msg, sizeof (struct GNUNET_MessageHeader), NULL, 0, 0); |
88 | break; | 88 | break; |
89 | case 3: | 89 | case 3: |
90 | /* called after first receive instantly | 90 | /* called after first receive instantly |
@@ -130,7 +130,7 @@ my_transmit_ready_cb (void *cls, | |||
130 | GNUNET_assert (size == sizeof (struct GNUNET_MessageHeader)); | 130 | GNUNET_assert (size == sizeof (struct GNUNET_MessageHeader)); |
131 | notify (notify_cls, size, buf); | 131 | notify (notify_cls, size, buf); |
132 | msg.type = htons (MY_TYPE); | 132 | msg.type = htons (MY_TYPE); |
133 | msg.size = htons (sizeof (msg)); | 133 | msg.size = htons (sizeof (struct GNUNET_MessageHeader)); |
134 | GNUNET_assert (0 == memcmp (&msg, buf, size)); | 134 | GNUNET_assert (0 == memcmp (&msg, buf, size)); |
135 | *ok = 5; /* report success */ | 135 | *ok = 5; /* report success */ |
136 | return &non_null_addr; | 136 | return &non_null_addr; |
@@ -250,9 +250,9 @@ task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | |||
250 | handlers[1].callback_cls = cls; | 250 | handlers[1].callback_cls = cls; |
251 | GNUNET_SERVER_add_handlers (server, handlers); | 251 | GNUNET_SERVER_add_handlers (server, handlers); |
252 | msg.type = htons (MY_TYPE); | 252 | msg.type = htons (MY_TYPE); |
253 | msg.size = htons (sizeof (msg)); | 253 | msg.size = htons (sizeof (struct GNUNET_MessageHeader)); |
254 | GNUNET_SERVER_inject (server, NULL, &msg); | 254 | GNUNET_SERVER_inject (server, NULL, &msg); |
255 | memset (&msg, 0, sizeof (msg)); | 255 | memset (&msg, 0, sizeof (struct GNUNET_MessageHeader)); |
256 | } | 256 | } |
257 | 257 | ||
258 | 258 | ||
diff --git a/src/util/test_server_with_client.c b/src/util/test_server_with_client.c index 22cf2cded..cccb2652d 100644 --- a/src/util/test_server_with_client.c +++ b/src/util/test_server_with_client.c | |||
@@ -30,7 +30,7 @@ | |||
30 | #include "gnunet_server_lib.h" | 30 | #include "gnunet_server_lib.h" |
31 | #include "gnunet_time_lib.h" | 31 | #include "gnunet_time_lib.h" |
32 | 32 | ||
33 | #define VERBOSE GNUNET_NO | 33 | #define VERBOSE GNUNET_YES |
34 | 34 | ||
35 | #define PORT 22335 | 35 | #define PORT 22335 |
36 | 36 | ||