aboutsummaryrefslogtreecommitdiff
path: root/src/util/server.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2009-11-01 20:47:52 +0000
committerChristian Grothoff <christian@grothoff.org>2009-11-01 20:47:52 +0000
commit652e89b59ed2207c2c12172fdabcd6e659995c81 (patch)
treef054c819d483c1056e18c1099afd4c7fcd2582a0 /src/util/server.c
parent5e4113e83368849500792e57946c3d8dd9e548d8 (diff)
downloadgnunet-652e89b59ed2207c2c12172fdabcd6e659995c81.tar.gz
gnunet-652e89b59ed2207c2c12172fdabcd6e659995c81.zip
fixing bio testcase and a bug in bio.c, also indenting
Diffstat (limited to 'src/util/server.c')
-rw-r--r--src/util/server.c136
1 files changed, 63 insertions, 73 deletions
diff --git a/src/util/server.c b/src/util/server.c
index 646299718..42fa8c46c 100644
--- a/src/util/server.c
+++ b/src/util/server.c
@@ -272,13 +272,11 @@ destroy_server (struct GNUNET_SERVER_Handle *server)
272 struct NotifyList *npos; 272 struct NotifyList *npos;
273 273
274#if DEBUG_SERVER 274#if DEBUG_SERVER
275 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 275 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Server shutting down.\n");
276 "Server shutting down.\n");
277#endif 276#endif
278 GNUNET_assert (server->listen_socket == NULL); 277 GNUNET_assert (server->listen_socket == NULL);
279 if (GNUNET_OK != GNUNET_DISK_pipe_close (server->shutpipe)) 278 if (GNUNET_OK != GNUNET_DISK_pipe_close (server->shutpipe))
280 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, 279 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "pipe-close");
281 "pipe-close");
282 while (server->clients != NULL) 280 while (server->clients != NULL)
283 { 281 {
284 pos = server->clients; 282 pos = server->clients;
@@ -318,26 +316,29 @@ process_listen_socket (void *cls,
318 { 316 {
319 /* shutdown was initiated */ 317 /* shutdown was initiated */
320 GNUNET_assert (server->listen_socket != NULL); 318 GNUNET_assert (server->listen_socket != NULL);
321 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (server->listen_socket)); 319 GNUNET_break (GNUNET_OK ==
320 GNUNET_NETWORK_socket_close (server->listen_socket));
322 server->listen_socket = NULL; 321 server->listen_socket = NULL;
323 if (server->do_shutdown) 322 if (server->do_shutdown)
324 destroy_server (server); 323 destroy_server (server);
325 return; 324 return;
326 } 325 }
327 shutpipe = GNUNET_DISK_pipe_handle (server->shutpipe, 326 shutpipe = GNUNET_DISK_pipe_handle (server->shutpipe,
328 GNUNET_DISK_PIPE_END_READ); 327 GNUNET_DISK_PIPE_END_READ);
329 GNUNET_assert (GNUNET_NETWORK_fdset_isset (tc->read_ready, server->listen_socket)); 328 GNUNET_assert (GNUNET_NETWORK_fdset_isset
330 GNUNET_assert (!GNUNET_NETWORK_fdset_handle_isset (tc->read_ready, shutpipe)); 329 (tc->read_ready, server->listen_socket));
331 sock = GNUNET_CONNECTION_create_from_accept (tc->sched, 330 GNUNET_assert (!GNUNET_NETWORK_fdset_handle_isset
332 server->access, 331 (tc->read_ready, shutpipe));
333 server->access_cls, 332 sock =
334 server->listen_socket, 333 GNUNET_CONNECTION_create_from_accept (tc->sched, server->access,
335 server->maxbuf); 334 server->access_cls,
335 server->listen_socket,
336 server->maxbuf);
336 if (sock != NULL) 337 if (sock != NULL)
337 { 338 {
338#if DEBUG_SERVER 339#if DEBUG_SERVER
339 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 340 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
340 "Server accepted incoming connection.\n"); 341 "Server accepted incoming connection.\n");
341#endif 342#endif
342 client = GNUNET_SERVER_connect_socket (server, sock); 343 client = GNUNET_SERVER_connect_socket (server, sock);
343 /* decrement reference count, we don't keep "client" alive */ 344 /* decrement reference count, we don't keep "client" alive */
@@ -352,8 +353,7 @@ process_listen_socket (void *cls,
352 GNUNET_SCHEDULER_PRIORITY_HIGH, 353 GNUNET_SCHEDULER_PRIORITY_HIGH,
353 GNUNET_SCHEDULER_NO_TASK, 354 GNUNET_SCHEDULER_NO_TASK,
354 GNUNET_TIME_UNIT_FOREVER_REL, 355 GNUNET_TIME_UNIT_FOREVER_REL,
355 r, NULL, 356 r, NULL, &process_listen_socket, server);
356 &process_listen_socket, server);
357 GNUNET_NETWORK_fdset_destroy (r); 357 GNUNET_NETWORK_fdset_destroy (r);
358} 358}
359 359
@@ -388,7 +388,8 @@ open_listen_socket (const struct sockaddr *serverAddr, socklen_t socklen)
388 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket"); 388 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
389 return NULL; 389 return NULL;
390 } 390 }
391 if (GNUNET_NETWORK_socket_setsockopt (sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)) != GNUNET_OK) 391 if (GNUNET_NETWORK_socket_setsockopt
392 (sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)) != GNUNET_OK)
392 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 393 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
393 "setsockopt"); 394 "setsockopt");
394 /* bind the socket */ 395 /* bind the socket */
@@ -409,9 +410,8 @@ open_listen_socket (const struct sockaddr *serverAddr, socklen_t socklen)
409 return NULL; 410 return NULL;
410 } 411 }
411#if DEBUG_SERVER 412#if DEBUG_SERVER
412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 413 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
413 "Server starts to listen on port %u.\n", 414 "Server starts to listen on port %u.\n", port);
414 port);
415#endif 415#endif
416 return sock; 416 return sock;
417} 417}
@@ -472,15 +472,14 @@ GNUNET_SERVER_create (struct GNUNET_SCHEDULER_Handle *sched,
472 { 472 {
473 r = GNUNET_NETWORK_fdset_create (); 473 r = GNUNET_NETWORK_fdset_create ();
474 GNUNET_NETWORK_fdset_set (r, ret->listen_socket); 474 GNUNET_NETWORK_fdset_set (r, ret->listen_socket);
475 GNUNET_NETWORK_fdset_handle_set (r, GNUNET_DISK_pipe_handle (ret->shutpipe, 475 GNUNET_NETWORK_fdset_handle_set (r,
476 GNUNET_DISK_PIPE_END_READ)); 476 GNUNET_DISK_pipe_handle (ret->shutpipe,
477 GNUNET_SCHEDULER_add_select (sched, 477 GNUNET_DISK_PIPE_END_READ));
478 GNUNET_YES, 478 GNUNET_SCHEDULER_add_select (sched, GNUNET_YES,
479 GNUNET_SCHEDULER_PRIORITY_HIGH, 479 GNUNET_SCHEDULER_PRIORITY_HIGH,
480 GNUNET_SCHEDULER_NO_TASK, 480 GNUNET_SCHEDULER_NO_TASK,
481 GNUNET_TIME_UNIT_FOREVER_REL, 481 GNUNET_TIME_UNIT_FOREVER_REL, r, NULL,
482 r, 482 &process_listen_socket, ret);
483 NULL, &process_listen_socket, ret);
484 GNUNET_NETWORK_fdset_destroy (r); 483 GNUNET_NETWORK_fdset_destroy (r);
485 } 484 }
486 return ret; 485 return ret;
@@ -500,10 +499,11 @@ GNUNET_SERVER_destroy (struct GNUNET_SERVER_Handle *s)
500 if (s->listen_socket == NULL) 499 if (s->listen_socket == NULL)
501 destroy_server (s); 500 destroy_server (s);
502 else 501 else
503 GNUNET_break (1 == GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle (s->shutpipe, 502 GNUNET_break (1 ==
504 GNUNET_DISK_PIPE_END_WRITE), 503 GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle
505 &c, 504 (s->shutpipe,
506 sizeof(c))); 505 GNUNET_DISK_PIPE_END_WRITE), &c,
506 sizeof (c)));
507} 507}
508 508
509 509
@@ -565,9 +565,8 @@ GNUNET_SERVER_inject (struct GNUNET_SERVER_Handle *server,
565 size = ntohs (message->size); 565 size = ntohs (message->size);
566#if DEBUG_SERVER 566#if DEBUG_SERVER
567 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 567 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
568 "Server schedules transmission of %u-byte message of type %u to client.\n", 568 "Server schedules transmission of %u-byte message of type %u to client.\n",
569 size, 569 size, type);
570 type);
571#endif 570#endif
572 pos = server->handlers; 571 pos = server->handlers;
573 found = GNUNET_NO; 572 found = GNUNET_NO;
@@ -668,10 +667,10 @@ process_client_buffer (struct GNUNET_SERVER_Client *client)
668 server = client->server; 667 server = client->server;
669#if DEBUG_SERVER 668#if DEBUG_SERVER
670 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 669 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
671 "Private buffer contains %u bytes; client is %s and we are %s\n", 670 "Private buffer contains %u bytes; client is %s and we are %s\n",
672 client->receive_pos, 671 client->receive_pos,
673 client->suspended ? "suspended" : "up", 672 client->suspended ? "suspended" : "up",
674 client->shutdown_now ? "in shutdown" : "running"); 673 client->shutdown_now ? "in shutdown" : "running");
675#endif 674#endif
676 while ((client->receive_pos >= sizeof (struct GNUNET_MessageHeader)) && 675 while ((client->receive_pos >= sizeof (struct GNUNET_MessageHeader)) &&
677 (0 == client->suspended) && (GNUNET_YES != client->shutdown_now)) 676 (0 == client->suspended) && (GNUNET_YES != client->shutdown_now))
@@ -679,19 +678,17 @@ process_client_buffer (struct GNUNET_SERVER_Client *client)
679 hdr = (const struct GNUNET_MessageHeader *) &client->incoming_buffer; 678 hdr = (const struct GNUNET_MessageHeader *) &client->incoming_buffer;
680 msize = ntohs (hdr->size); 679 msize = ntohs (hdr->size);
681 if (msize > client->receive_pos) 680 if (msize > client->receive_pos)
682 { 681 {
683#if DEBUG_SERVER 682#if DEBUG_SERVER
684 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 683 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
685 "Total message size is %u, we only have %u bytes; need more data\n", 684 "Total message size is %u, we only have %u bytes; need more data\n",
686 msize, 685 msize, client->receive_pos);
687 client->receive_pos);
688#endif 686#endif
689 break; 687 break;
690 } 688 }
691#if DEBUG_SERVER 689#if DEBUG_SERVER
692 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 690 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
693 "Passing %u bytes to callback for processing\n", 691 "Passing %u bytes to callback for processing\n", msize);
694 msize);
695#endif 692#endif
696 if ((msize < sizeof (struct GNUNET_MessageHeader)) || 693 if ((msize < sizeof (struct GNUNET_MessageHeader)) ||
697 (GNUNET_OK != GNUNET_SERVER_inject (server, client, hdr))) 694 (GNUNET_OK != GNUNET_SERVER_inject (server, client, hdr)))
@@ -727,9 +724,7 @@ static void
727process_incoming (void *cls, 724process_incoming (void *cls,
728 const void *buf, 725 const void *buf,
729 size_t available, 726 size_t available,
730 const struct sockaddr *addr, 727 const struct sockaddr *addr, socklen_t addrlen, int errCode)
731 socklen_t addrlen,
732 int errCode)
733{ 728{
734 struct GNUNET_SERVER_Client *client = cls; 729 struct GNUNET_SERVER_Client *client = cls;
735 struct GNUNET_SERVER_Handle *server = client->server; 730 struct GNUNET_SERVER_Handle *server = client->server;
@@ -749,9 +744,8 @@ process_incoming (void *cls,
749 } 744 }
750#if DEBUG_SERVER 745#if DEBUG_SERVER
751 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 746 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
752 "Server receives %u bytes from `%s'.\n", 747 "Server receives %u bytes from `%s'.\n",
753 available, 748 available, GNUNET_a2s (addr, addrlen));
754 GNUNET_a2s(addr, addrlen));
755#endif 749#endif
756 GNUNET_SERVER_client_keep (client); 750 GNUNET_SERVER_client_keep (client);
757 client->last_activity = GNUNET_TIME_absolute_get (); 751 client->last_activity = GNUNET_TIME_absolute_get ();
@@ -763,8 +757,7 @@ process_incoming (void *cls,
763 maxcpy = sizeof (client->incoming_buffer) - client->receive_pos; 757 maxcpy = sizeof (client->incoming_buffer) - client->receive_pos;
764#if DEBUG_SERVER 758#if DEBUG_SERVER
765 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 759 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
766 "Can copy %u bytes to private buffer\n", 760 "Can copy %u bytes to private buffer\n", maxcpy);
767 maxcpy);
768#endif 761#endif
769 memcpy (&client->incoming_buffer[client->receive_pos], cbuf, maxcpy); 762 memcpy (&client->incoming_buffer[client->receive_pos], cbuf, maxcpy);
770 client->receive_pos += maxcpy; 763 client->receive_pos += maxcpy;
@@ -775,12 +768,12 @@ process_incoming (void *cls,
775 if (available > 0) 768 if (available > 0)
776 { 769 {
777#if DEBUG_SERVER 770#if DEBUG_SERVER
778 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 771 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
779 "Client has suspended processing; copying %u bytes to side buffer to be used later.\n", 772 "Client has suspended processing; copying %u bytes to side buffer to be used later.\n",
780 available); 773 available);
781#endif 774#endif
782 GNUNET_assert (client->side_buf_size == 0); 775 GNUNET_assert (client->side_buf_size == 0);
783 GNUNET_assert (client->side_buf == NULL); 776 GNUNET_assert (client->side_buf == NULL);
784 client->side_buf_size = available; 777 client->side_buf_size = available;
785 client->side_buf = GNUNET_malloc (available); 778 client->side_buf = GNUNET_malloc (available);
786 memcpy (client->side_buf, cbuf, available); 779 memcpy (client->side_buf, cbuf, available);
@@ -790,7 +783,7 @@ process_incoming (void *cls,
790 } 783 }
791#if DEBUG_SERVER 784#if DEBUG_SERVER
792 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 785 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
793 "Now processing messages in private buffer\n"); 786 "Now processing messages in private buffer\n");
794#endif 787#endif
795 process_client_buffer (client); 788 process_client_buffer (client);
796 } 789 }
@@ -800,9 +793,8 @@ process_incoming (void *cls,
800 { 793 {
801 /* Finally, keep receiving! */ 794 /* Finally, keep receiving! */
802 client->receive (client->client_closure, 795 client->receive (client->client_closure,
803 GNUNET_SERVER_MAX_MESSAGE_SIZE, 796 GNUNET_SERVER_MAX_MESSAGE_SIZE,
804 server->idle_timeout, 797 server->idle_timeout, &process_incoming, client);
805 &process_incoming, client);
806 } 798 }
807 if (GNUNET_YES == client->shutdown_now) 799 if (GNUNET_YES == client->shutdown_now)
808 shutdown_incoming_processing (client); 800 shutdown_incoming_processing (client);
@@ -821,9 +813,8 @@ restart_processing (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
821 process_client_buffer (client); 813 process_client_buffer (client);
822 if (0 == client->suspended) 814 if (0 == client->suspended)
823 client->receive (client->client_closure, 815 client->receive (client->client_closure,
824 GNUNET_SERVER_MAX_MESSAGE_SIZE, 816 GNUNET_SERVER_MAX_MESSAGE_SIZE,
825 client->server->idle_timeout, 817 client->server->idle_timeout, &process_incoming, client);
826 &process_incoming, client);
827} 818}
828 819
829 820
@@ -840,9 +831,8 @@ add_client (struct GNUNET_SERVER_Handle *server,
840 client->next = server->clients; 831 client->next = server->clients;
841 server->clients = client; 832 server->clients = client;
842 client->receive (client->client_closure, 833 client->receive (client->client_closure,
843 GNUNET_SERVER_MAX_MESSAGE_SIZE, 834 GNUNET_SERVER_MAX_MESSAGE_SIZE,
844 server->idle_timeout, 835 server->idle_timeout, &process_incoming, client);
845 &process_incoming, client);
846} 836}
847 837
848 838
@@ -888,7 +878,7 @@ sock_notify_transmit_ready (void *cls,
888 void *notify_cls) 878 void *notify_cls)
889{ 879{
890 return GNUNET_CONNECTION_notify_transmit_ready (cls, size, timeout, notify, 880 return GNUNET_CONNECTION_notify_transmit_ready (cls, size, timeout, notify,
891 notify_cls); 881 notify_cls);
892} 882}
893 883
894 884
@@ -1058,7 +1048,7 @@ GNUNET_SERVER_client_get_address (struct GNUNET_SERVER_Client *client,
1058 if (client->receive != &sock_receive) 1048 if (client->receive != &sock_receive)
1059 return GNUNET_SYSERR; /* not a network client */ 1049 return GNUNET_SYSERR; /* not a network client */
1060 return GNUNET_CONNECTION_get_address (client->client_closure, 1050 return GNUNET_CONNECTION_get_address (client->client_closure,
1061 addr, addrlen); 1051 addr, addrlen);
1062} 1052}
1063 1053
1064 1054