diff options
Diffstat (limited to 'src/daemon/daemon.c')
-rw-r--r-- | src/daemon/daemon.c | 184 |
1 files changed, 89 insertions, 95 deletions
diff --git a/src/daemon/daemon.c b/src/daemon/daemon.c index 860d3b1f..b9e2a5a8 100644 --- a/src/daemon/daemon.c +++ b/src/daemon/daemon.c | |||
@@ -1,21 +1,21 @@ | |||
1 | /* | 1 | /* |
2 | This file is part of libmicrohttpd | 2 | This file is part of libmicrohttpd |
3 | (C) 2007, 2008 Daniel Pittman and Christian Grothoff | 3 | (C) 2007, 2008 Daniel Pittman and Christian Grothoff |
4 | 4 | ||
5 | This library is free software; you can redistribute it and/or | 5 | This library is free software; you can redistribute it and/or |
6 | modify it under the terms of the GNU Lesser General Public | 6 | modify it under the terms of the GNU Lesser General Public |
7 | License as published by the Free Software Foundation; either | 7 | License as published by the Free Software Foundation; either |
8 | version 2.1 of the License, or (at your option) any later version. | 8 | version 2.1 of the License, or (at your option) any later version. |
9 | 9 | ||
10 | This library is distributed in the hope that it will be useful, | 10 | This library is distributed in the hope that it will be useful, |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | Lesser General Public License for more details. | 13 | Lesser General Public License for more details. |
14 | 14 | ||
15 | You should have received a copy of the GNU Lesser General Public | 15 | You should have received a copy of the GNU Lesser General Public |
16 | License along with this library; if not, write to the Free Software | 16 | License along with this library; if not, write to the Free Software |
17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
18 | 18 | ||
19 | */ | 19 | */ |
20 | 20 | ||
21 | /** | 21 | /** |
@@ -67,7 +67,7 @@ | |||
67 | 67 | ||
68 | #if HTTPS_SUPPORT | 68 | #if HTTPS_SUPPORT |
69 | /** | 69 | /** |
70 | * Note: code duplication with code in MHD_gnutls_priority.c | 70 | * Note: code duplication with code in MHD_gnutls_priority.c |
71 | * | 71 | * |
72 | * @return 0 | 72 | * @return 0 |
73 | */ | 73 | */ |
@@ -76,11 +76,10 @@ _set_priority (MHD_gtls_priority_st * st, const int *list) | |||
76 | { | 76 | { |
77 | int num = 0; | 77 | int num = 0; |
78 | 78 | ||
79 | while ( (list[num] != 0) && | 79 | while ((list[num] != 0) && (num < MAX_ALGOS)) |
80 | (num < MAX_ALGOS) ) | ||
81 | num++; | 80 | num++; |
82 | st->num_algorithms = num; | 81 | st->num_algorithms = num; |
83 | memcpy(st->priority, list, num * sizeof(int)); | 82 | memcpy (st->priority, list, num * sizeof (int)); |
84 | return 0; | 83 | return 0; |
85 | } | 84 | } |
86 | 85 | ||
@@ -94,12 +93,9 @@ _set_priority (MHD_gtls_priority_st * st, const int *list) | |||
94 | * @return number of bytes actually received | 93 | * @return number of bytes actually received |
95 | */ | 94 | */ |
96 | static ssize_t | 95 | static ssize_t |
97 | recv_tls_adapter (struct MHD_Connection* connection, | 96 | recv_tls_adapter (struct MHD_Connection *connection, void *other, size_t i) |
98 | void *other, | ||
99 | size_t i) | ||
100 | { | 97 | { |
101 | return MHD__gnutls_record_recv(connection->tls_session, | 98 | return MHD__gnutls_record_recv (connection->tls_session, other, i); |
102 | other, i); | ||
103 | } | 99 | } |
104 | 100 | ||
105 | /** | 101 | /** |
@@ -111,12 +107,10 @@ recv_tls_adapter (struct MHD_Connection* connection, | |||
111 | * @return actual number of bytes written | 107 | * @return actual number of bytes written |
112 | */ | 108 | */ |
113 | static ssize_t | 109 | static ssize_t |
114 | send_tls_adapter (struct MHD_Connection* connection, | 110 | send_tls_adapter (struct MHD_Connection *connection, |
115 | const void *other, | 111 | const void *other, size_t i) |
116 | size_t i) | ||
117 | { | 112 | { |
118 | return MHD__gnutls_record_send(connection->tls_session, | 113 | return MHD__gnutls_record_send (connection->tls_session, other, i); |
119 | other, i); | ||
120 | } | 114 | } |
121 | 115 | ||
122 | 116 | ||
@@ -140,8 +134,8 @@ MHD_init_daemon_certificate (struct MHD_Daemon *daemon) | |||
140 | cert.size = strlen (daemon->https_mem_cert); | 134 | cert.size = strlen (daemon->https_mem_cert); |
141 | 135 | ||
142 | return MHD__gnutls_certificate_set_x509_key_mem (daemon->x509_cred, | 136 | return MHD__gnutls_certificate_set_x509_key_mem (daemon->x509_cred, |
143 | &cert, &key, | 137 | &cert, &key, |
144 | GNUTLS_X509_FMT_PEM); | 138 | GNUTLS_X509_FMT_PEM); |
145 | } | 139 | } |
146 | #if HAVE_MESSAGES | 140 | #if HAVE_MESSAGES |
147 | MHD_DLOG (daemon, "You need to specify a certificate and key location\n"); | 141 | MHD_DLOG (daemon, "You need to specify a certificate and key location\n"); |
@@ -150,7 +144,7 @@ MHD_init_daemon_certificate (struct MHD_Daemon *daemon) | |||
150 | } | 144 | } |
151 | 145 | ||
152 | /** | 146 | /** |
153 | * Initialize security aspects of the HTTPS daemon | 147 | * Initialize security aspects of the HTTPS daemon |
154 | * | 148 | * |
155 | * @return 0 on success | 149 | * @return 0 on success |
156 | */ | 150 | */ |
@@ -160,22 +154,24 @@ MHD_TLS_init (struct MHD_Daemon *daemon) | |||
160 | switch (daemon->cred_type) | 154 | switch (daemon->cred_type) |
161 | { | 155 | { |
162 | case MHD_GNUTLS_CRD_ANON: | 156 | case MHD_GNUTLS_CRD_ANON: |
163 | if ( (0 != MHD__gnutls_anon_allocate_server_credentials (&daemon->anon_cred)) || | 157 | if ((0 != |
164 | (0 != MHD__gnutls_dh_params_init (&daemon->dh_params)) ) | 158 | MHD__gnutls_anon_allocate_server_credentials (&daemon->anon_cred)) |
165 | return GNUTLS_E_MEMORY_ERROR; | 159 | || (0 != MHD__gnutls_dh_params_init (&daemon->dh_params))) |
160 | return GNUTLS_E_MEMORY_ERROR; | ||
166 | MHD__gnutls_dh_params_generate2 (daemon->dh_params, 1024); | 161 | MHD__gnutls_dh_params_generate2 (daemon->dh_params, 1024); |
167 | MHD__gnutls_anon_set_server_dh_params (daemon->anon_cred, | 162 | MHD__gnutls_anon_set_server_dh_params (daemon->anon_cred, |
168 | daemon->dh_params); | 163 | daemon->dh_params); |
169 | return 0; | 164 | return 0; |
170 | case MHD_GNUTLS_CRD_CERTIFICATE: | 165 | case MHD_GNUTLS_CRD_CERTIFICATE: |
171 | if (0 != MHD__gnutls_certificate_allocate_credentials (&daemon->x509_cred)) | 166 | if (0 != |
167 | MHD__gnutls_certificate_allocate_credentials (&daemon->x509_cred)) | ||
172 | return GNUTLS_E_MEMORY_ERROR; | 168 | return GNUTLS_E_MEMORY_ERROR; |
173 | return MHD_init_daemon_certificate (daemon); | 169 | return MHD_init_daemon_certificate (daemon); |
174 | default: | 170 | default: |
175 | #if HAVE_MESSAGES | 171 | #if HAVE_MESSAGES |
176 | MHD_DLOG (daemon, | 172 | MHD_DLOG (daemon, |
177 | "Error: invalid credentials type %d specified.\n", | 173 | "Error: invalid credentials type %d specified.\n", |
178 | daemon->cred_type); | 174 | daemon->cred_type); |
179 | #endif | 175 | #endif |
180 | return -1; | 176 | return -1; |
181 | } | 177 | } |
@@ -257,9 +253,9 @@ MHD_handle_connection (void *data) | |||
257 | tv.tv_sec = timeout - (now - con->last_activity); | 253 | tv.tv_sec = timeout - (now - con->last_activity); |
258 | else | 254 | else |
259 | tv.tv_sec = 0; | 255 | tv.tv_sec = 0; |
260 | if ( (con->state == MHD_CONNECTION_NORMAL_BODY_UNREADY) || | 256 | if ((con->state == MHD_CONNECTION_NORMAL_BODY_UNREADY) || |
261 | (con->state == MHD_CONNECTION_CHUNKED_BODY_UNREADY) ) | 257 | (con->state == MHD_CONNECTION_CHUNKED_BODY_UNREADY)) |
262 | timeout = 1; /* do not block */ | 258 | timeout = 1; /* do not block */ |
263 | num_ready = SELECT (max + 1, | 259 | num_ready = SELECT (max + 1, |
264 | &rs, &ws, &es, (timeout != 0) ? &tv : NULL); | 260 | &rs, &ws, &es, (timeout != 0) ? &tv : NULL); |
265 | if (num_ready < 0) | 261 | if (num_ready < 0) |
@@ -304,13 +300,11 @@ MHD_handle_connection (void *data) | |||
304 | * @return number of bytes actually received | 300 | * @return number of bytes actually received |
305 | */ | 301 | */ |
306 | static ssize_t | 302 | static ssize_t |
307 | recv_param_adapter (struct MHD_Connection * connection, | 303 | recv_param_adapter (struct MHD_Connection *connection, void *other, size_t i) |
308 | void *other, | ||
309 | size_t i) | ||
310 | { | 304 | { |
311 | if (connection->socket_fd == -1) | 305 | if (connection->socket_fd == -1) |
312 | return -1; | 306 | return -1; |
313 | return RECV(connection->socket_fd, other, i, MSG_NOSIGNAL); | 307 | return RECV (connection->socket_fd, other, i, MSG_NOSIGNAL); |
314 | } | 308 | } |
315 | 309 | ||
316 | /** | 310 | /** |
@@ -323,12 +317,11 @@ recv_param_adapter (struct MHD_Connection * connection, | |||
323 | */ | 317 | */ |
324 | static ssize_t | 318 | static ssize_t |
325 | send_param_adapter (struct MHD_Connection *connection, | 319 | send_param_adapter (struct MHD_Connection *connection, |
326 | const void *other, | 320 | const void *other, size_t i) |
327 | size_t i) | ||
328 | { | 321 | { |
329 | if (connection->socket_fd == -1) | 322 | if (connection->socket_fd == -1) |
330 | return -1; | 323 | return -1; |
331 | return SEND(connection->socket_fd, other, i, MSG_NOSIGNAL); | 324 | return SEND (connection->socket_fd, other, i, MSG_NOSIGNAL); |
332 | } | 325 | } |
333 | 326 | ||
334 | /** | 327 | /** |
@@ -490,37 +483,41 @@ MHD_accept_connection (struct MHD_Daemon *daemon) | |||
490 | connection->recv_cls = &recv_tls_adapter; | 483 | connection->recv_cls = &recv_tls_adapter; |
491 | connection->send_cls = &send_tls_adapter; | 484 | connection->send_cls = &send_tls_adapter; |
492 | connection->state = MHD_TLS_CONNECTION_INIT; | 485 | connection->state = MHD_TLS_CONNECTION_INIT; |
493 | MHD_set_https_calbacks (connection); | 486 | MHD_set_https_calbacks (connection); |
494 | MHD__gnutls_init (&connection->tls_session, GNUTLS_SERVER); | 487 | MHD__gnutls_init (&connection->tls_session, GNUTLS_SERVER); |
495 | MHD__gnutls_priority_set (connection->tls_session, connection->daemon->priority_cache); | 488 | MHD__gnutls_priority_set (connection->tls_session, |
489 | connection->daemon->priority_cache); | ||
496 | switch (connection->daemon->cred_type) | 490 | switch (connection->daemon->cred_type) |
497 | { | 491 | { |
498 | /* set needed credentials for certificate authentication. */ | 492 | /* set needed credentials for certificate authentication. */ |
499 | case MHD_GNUTLS_CRD_CERTIFICATE: | 493 | case MHD_GNUTLS_CRD_CERTIFICATE: |
500 | MHD__gnutls_credentials_set (connection->tls_session, | 494 | MHD__gnutls_credentials_set (connection->tls_session, |
501 | MHD_GNUTLS_CRD_CERTIFICATE, | 495 | MHD_GNUTLS_CRD_CERTIFICATE, |
502 | connection->daemon->x509_cred); | 496 | connection->daemon->x509_cred); |
503 | break; | 497 | break; |
504 | case MHD_GNUTLS_CRD_ANON: | 498 | case MHD_GNUTLS_CRD_ANON: |
505 | /* set needed credentials for anonymous authentication. */ | 499 | /* set needed credentials for anonymous authentication. */ |
506 | MHD__gnutls_credentials_set (connection->tls_session, MHD_GNUTLS_CRD_ANON, | 500 | MHD__gnutls_credentials_set (connection->tls_session, |
507 | connection->daemon->anon_cred); | 501 | MHD_GNUTLS_CRD_ANON, |
508 | MHD__gnutls_dh_set_prime_bits (connection->tls_session, 1024); | 502 | connection->daemon->anon_cred); |
509 | break; | 503 | MHD__gnutls_dh_set_prime_bits (connection->tls_session, 1024); |
510 | default: | 504 | break; |
505 | default: | ||
511 | #if HAVE_MESSAGES | 506 | #if HAVE_MESSAGES |
512 | MHD_DLOG (connection->daemon, | 507 | MHD_DLOG (connection->daemon, |
513 | "Failed to setup TLS credentials: unknown credential type %d\n", | 508 | "Failed to setup TLS credentials: unknown credential type %d\n", |
514 | connection->daemon->cred_type); | 509 | connection->daemon->cred_type); |
515 | #endif | 510 | #endif |
516 | abort(); | 511 | abort (); |
517 | } | 512 | } |
518 | MHD__gnutls_transport_set_ptr (connection->tls_session, | 513 | MHD__gnutls_transport_set_ptr (connection->tls_session, |
519 | (MHD_gnutls_transport_ptr_t) connection); | 514 | (MHD_gnutls_transport_ptr_t) connection); |
520 | MHD__gnutls_transport_set_pull_function(connection->tls_session, | 515 | MHD__gnutls_transport_set_pull_function (connection->tls_session, |
521 | (MHD_gtls_pull_func) &recv_param_adapter); | 516 | (MHD_gtls_pull_func) & |
522 | MHD__gnutls_transport_set_push_function(connection->tls_session, | 517 | recv_param_adapter); |
523 | (MHD_gtls_push_func) &send_param_adapter); | 518 | MHD__gnutls_transport_set_push_function (connection->tls_session, |
519 | (MHD_gtls_push_func) & | ||
520 | send_param_adapter); | ||
524 | } | 521 | } |
525 | #endif | 522 | #endif |
526 | 523 | ||
@@ -528,8 +525,7 @@ MHD_accept_connection (struct MHD_Daemon *daemon) | |||
528 | if (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) | 525 | if (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) |
529 | { | 526 | { |
530 | res_thread_create = pthread_create (&connection->pid, NULL, | 527 | res_thread_create = pthread_create (&connection->pid, NULL, |
531 | &MHD_handle_connection, | 528 | &MHD_handle_connection, connection); |
532 | connection); | ||
533 | if (res_thread_create != 0) | 529 | if (res_thread_create != 0) |
534 | { | 530 | { |
535 | #if HAVE_MESSAGES | 531 | #if HAVE_MESSAGES |
@@ -542,7 +538,7 @@ MHD_accept_connection (struct MHD_Daemon *daemon) | |||
542 | free (connection); | 538 | free (connection); |
543 | return MHD_NO; | 539 | return MHD_NO; |
544 | } | 540 | } |
545 | } | 541 | } |
546 | connection->next = daemon->connections; | 542 | connection->next = daemon->connections; |
547 | daemon->connections = connection; | 543 | daemon->connections = connection; |
548 | daemon->max_connections--; | 544 | daemon->max_connections--; |
@@ -565,35 +561,34 @@ MHD_cleanup_connections (struct MHD_Daemon *daemon) | |||
565 | prev = NULL; | 561 | prev = NULL; |
566 | while (pos != NULL) | 562 | while (pos != NULL) |
567 | { | 563 | { |
568 | if ( (pos->socket_fd == -1) || | 564 | if ((pos->socket_fd == -1) || |
569 | ( ( (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) && | 565 | (((0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) && |
570 | (daemon->shutdown) && | 566 | (daemon->shutdown) && (pos->socket_fd != -1)))) |
571 | (pos->socket_fd != -1) ) ) ) | 567 | { |
572 | { | ||
573 | if (prev == NULL) | 568 | if (prev == NULL) |
574 | daemon->connections = pos->next; | 569 | daemon->connections = pos->next; |
575 | else | 570 | else |
576 | prev->next = pos->next; | 571 | prev->next = pos->next; |
577 | if (0 != (pos->daemon->options & MHD_USE_THREAD_PER_CONNECTION)) | 572 | if (0 != (pos->daemon->options & MHD_USE_THREAD_PER_CONNECTION)) |
578 | { | 573 | { |
579 | pthread_kill (pos->pid, SIGALRM); | 574 | pthread_kill (pos->pid, SIGALRM); |
580 | pthread_join (pos->pid, &unused); | 575 | pthread_join (pos->pid, &unused); |
581 | } | 576 | } |
582 | MHD_destroy_response (pos->response); | 577 | MHD_destroy_response (pos->response); |
583 | MHD_pool_destroy (pos->pool); | 578 | MHD_pool_destroy (pos->pool); |
584 | #if HTTPS_SUPPORT | 579 | #if HTTPS_SUPPORT |
585 | if (pos->tls_session != NULL) | 580 | if (pos->tls_session != NULL) |
586 | MHD__gnutls_deinit (pos->tls_session); | 581 | MHD__gnutls_deinit (pos->tls_session); |
587 | #endif | 582 | #endif |
588 | free (pos->addr); | 583 | free (pos->addr); |
589 | free (pos); | 584 | free (pos); |
590 | daemon->max_connections++; | 585 | daemon->max_connections++; |
591 | if (prev == NULL) | 586 | if (prev == NULL) |
592 | pos = daemon->connections; | 587 | pos = daemon->connections; |
593 | else | 588 | else |
594 | pos = prev->next; | 589 | pos = prev->next; |
595 | continue; | 590 | continue; |
596 | } | 591 | } |
597 | prev = pos; | 592 | prev = pos; |
598 | pos = pos->next; | 593 | pos = pos->next; |
599 | } | 594 | } |
@@ -946,9 +941,9 @@ MHD_start_daemon_va (unsigned int options, | |||
946 | #else | 941 | #else |
947 | { | 942 | { |
948 | #if HAVE_MESSAGES | 943 | #if HAVE_MESSAGES |
949 | FPRINTF (stderr, "AF_INET6 not supported\n"); | 944 | FPRINTF (stderr, "AF_INET6 not supported\n"); |
950 | #endif | 945 | #endif |
951 | return NULL; | 946 | return NULL; |
952 | } | 947 | } |
953 | #endif | 948 | #endif |
954 | else | 949 | else |
@@ -1026,8 +1021,7 @@ MHD_start_daemon_va (unsigned int options, | |||
1026 | 1021 | ||
1027 | #if HTTPS_SUPPORT | 1022 | #if HTTPS_SUPPORT |
1028 | /* initialize HTTPS daemon certificate aspects & send / recv functions */ | 1023 | /* initialize HTTPS daemon certificate aspects & send / recv functions */ |
1029 | if ( (0 != (options & MHD_USE_SSL)) && | 1024 | if ((0 != (options & MHD_USE_SSL)) && (0 != MHD_TLS_init (retVal))) |
1030 | (0 != MHD_TLS_init (retVal)) ) | ||
1031 | { | 1025 | { |
1032 | #if HAVE_MESSAGES | 1026 | #if HAVE_MESSAGES |
1033 | MHD_DLOG (retVal, "Failed to initialize TLS support\n"); | 1027 | MHD_DLOG (retVal, "Failed to initialize TLS support\n"); |
@@ -1073,7 +1067,7 @@ MHD_stop_daemon (struct MHD_Daemon *daemon) | |||
1073 | #endif | 1067 | #endif |
1074 | #endif | 1068 | #endif |
1075 | CLOSE (fd); | 1069 | CLOSE (fd); |
1076 | if ((0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) || | 1070 | if ((0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) || |
1077 | (0 != (daemon->options & MHD_USE_SELECT_INTERNALLY))) | 1071 | (0 != (daemon->options & MHD_USE_SELECT_INTERNALLY))) |
1078 | { | 1072 | { |
1079 | pthread_kill (daemon->pid, SIGALRM); | 1073 | pthread_kill (daemon->pid, SIGALRM); |
@@ -1128,9 +1122,9 @@ MHD_stop_daemon (struct MHD_Daemon *daemon) | |||
1128 | * @return NULL if this information is not available | 1122 | * @return NULL if this information is not available |
1129 | * (or if the infoType is unknown) | 1123 | * (or if the infoType is unknown) |
1130 | */ | 1124 | */ |
1131 | const union MHD_DaemonInfo *MHD_get_daemon_info (struct MHD_Daemon *daemon, | 1125 | const union MHD_DaemonInfo * |
1132 | enum MHD_DaemonInfoType | 1126 | MHD_get_daemon_info (struct MHD_Daemon *daemon, |
1133 | infoType, ...) | 1127 | enum MHD_DaemonInfoType infoType, ...) |
1134 | { | 1128 | { |
1135 | return NULL; | 1129 | return NULL; |
1136 | } | 1130 | } |