Lines Matching refs:service

75 static bool fb_setup_socket (FB_SERVICE *service, FB_SOCKETID which) {  argument
85 … (fb_encrypted_socket (which) ? service->options.https_port : service->options.http_port) :
86 service->options.line_port;
91 if ((service->socket [which] = socket (ipv6 ? PF_INET6 : PF_INET, SOCK_STREAM, 0)) >= 0) {
97 if ((service->socket [which] = socket (PF_INET, SOCK_STREAM, 0)) >= 0) {
103 service->address [which].ip6.sin6_family = PF_INET6;
104 service->address [which].ip6.sin6_addr = in6addr_any;
105 service->address [which].ip6.sin6_port = htons (port);
110 service->address [which].ip4.sin_family = PF_INET;
111 service->address [which].ip4.sin_addr.s_addr = INADDR_ANY;
112 service->address [which].ip4.sin_port = htons (port);
114 if (setsockopt(service->socket [which], SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
120 if (bind (service->socket [which],
121 (struct sockaddr*) &(service->address [which]),
122 … ipv6 ? sizeof (service->address [which].ip6) : sizeof (service->address [which].ip4)) >= 0) {
124 if (bind (service->socket [which],
125 … (struct sockaddr*) &(service->address [which]), sizeof (service->address [which].ip4)) >= 0) {
127 if (listen (service->socket [which], service->options.queue_size) >= 0) {
128 if (fb_register (service->socket [which], FB_SOCKTYPE_SERVICE, service)) {
137 close (service->socket [which]);
141 service->socket [which] = 0;
167 FB_SERVICE *service; local
168 if ((service = calloc (1, sizeof (*service)))) {
170 service->options = *options;
171 service->options.name = options->name ? strdup (options->name) : NULL;
172 service->options.greeting = strdup (options->greeting ? options->greeting
180 service->options.serve_directory = canonicalize_file_name(servedir);
182 service->options.serve_directory = realpath (servedir, NULL);
184 service->options.serve_directory = strdup (servedir);
186 if (!service->options.serve_directory) {
193 service->options.locale_directory = langdir ? strdup (langdir) : NULL;
194 if ((service->options.name || !options->name) &&
195 (service->options.serve_directory || !servedir) &&
196 (service->options.locale_directory || !langdir) &&
197 service->options.greeting) {
198 service->type = FB_SOCKTYPE_SERVICE;
199 fb_mutex_init (&service->mutex);
202 fb_mutex_lock (&service->mutex);
205 successes += fb_setup_socket (service, i);
209 service->state = FB_SOCKET_STATE_OPEN;
212 service->next_child = options->parent->next_child;
213 options->parent->next_child = service;
218 fb_mutex_unlock (&service->mutex);
220 return (service);
222 fb_mutex_unlock (&service->mutex);
223 fb_mutex_destroy (&service->mutex);
226 free (service->options.name);
227 free (service->options.greeting);
228 free (service->options.serve_directory);
229 free (service->options.locale_directory);
230 free (service);
245 void fb_destroy_service (FB_SERVICE *service) { argument
248 assert (service);
249 assert (service->connection_count == 0);
250 while (service->connection_count > 0) {
251 fb_destroy_connection (service->connections [0]);
255 if (service->socket [id]) {
256 fb_unregister (service->socket [id]);
257 close (service->socket [id]);
260 if (service->options.parent) {
262 FB_SERVICE *node = service->options.parent;
263 while (node->next_child != service) {
270 FB_SERVICE *child = service->next_child;
278 free (service->options.greeting);
279 free (service->options.serve_directory);
280 free (service->options.locale_directory);
281 free (service->options.name);
282 free (service->connections); /* Per man page, freeing null is okay */
283 free (service);
291 void fb_close_service (FB_SERVICE *service) { argument
292 fb_mutex_lock (&service->mutex);
293 assert (service);
294 assert (service->state == FB_SOCKET_STATE_OPEN);
295 service->state = FB_SOCKET_STATE_CLOSING;
296 for (unsigned i = 0; i < service->connection_count; i++) {
297 fb_close_connection (service->connections[i]);
301 if (service->socket [id]) {
302 fb_set_readable (service->socket [id], false);
305 fb_mutex_unlock (&service->mutex);
306 if (service->connection_count == 0) {
308 fb_schedule_reap (service);
318 bool fb_validate_connection (FB_SERVICE *service, FB_CONNECTION *connection) { argument
332 bool allowed = hosts_ctl (service->options.name ? service->options.name : STRING_UNKNOWN,
353 static FB_CONNECTION *fb_new_connection (FB_SERVICE *service) { argument
355 assert (service);
356 …assert (service->connection_count <= service->connections_size); /* If this is off, something alre…
357 if (!fb_expandcalloc ((void **) &service->connections, &service->connections_size,
358 service->connection_count + 1, sizeof (FB_CONNECTION *))) {
366 connection->service = service;
369 if (service->options.context_size == 0) {
372 connection->context = calloc (1, service->options.context_size);
388 FB_CONNECTION *fb_accept_connection (FB_SERVICE *service, FB_SOCKETID id) { argument
389 assert (service);
390 fb_mutex_lock(&service->mutex);
391 FB_CONNECTION *connection = fb_new_connection (service);
394 fb_mutex_unlock (&service->mutex);
409 connection->http || service->options.greeting_mode == FB_GREETING_REQUIRE ||
410 service->options.greeting_mode == FB_GREETING_FALLBACK ?
414 if ((connection->socket = accept (service->socket [id],
417 service->connections [service->connection_count++] = connection;
419 … if (fb_validate_connection (service, connection) && connection->transport->init (connection)) {
420 fb_mutex_unlock (&service->mutex);
427 fb_mutex_unlock (&service->mutex);
440 static void fb_remove_connection_from_service (FB_SERVICE *service, FB_CONNECTION *connection) { argument
443 for (i = 0; i < service->connection_count && service->connections [i] != connection; i++)
445 assert (i < service->connection_count); /* If we didn't find it, there's a bug */
446 service->connection_count--;
448 for (j = i; j < service->connection_count; j++) {
449 service->connections [j] = service->connections [j+1];
457 bool fb_transfer (FB_CONNECTION *connection, FB_SERVICE *service) { argument
458 if (service == connection->service) {
461 fb_mutex_lock (&service->mutex);
462 fb_mutex_lock (&connection->service->mutex);
464 assert (service);
467 if (!fb_expandcalloc ((void **) &service->connections, &service->connections_size,
468 service->connection_count + 1, sizeof (FB_CONNECTION *))) {
474 fb_remove_connection_from_service (connection->service, connection);
477 service->connections [service->connection_count++] = connection;
478 connection->service = service;
479 fb_mutex_unlock (&connection->service->mutex);
480 fb_mutex_unlock (&service->mutex);
493 FB_SERVICE *service = connection->service->options.parent; local
494 if (!service) {
495 service = connection->service;
497 while (service) {
498 if (strcasecmp (service->options.name, name) == 0) {
499 return fb_transfer(connection, service);
501 service = service->next_child;
515 fb_mutex_lock (&connection->service->mutex);
516 fb_remove_connection_from_service (connection->service, connection);
517 fb_mutex_unlock (&connection->service->mutex);
564 FB_EVENT *fb_accept_file (FB_SERVICE *service, char *filename) { argument
565 assert (service);
571 event.service = service;
573 fb_mutex_lock(&service->mutex);
574 event.connection = fb_new_connection (service);
585 service->connections [service->connection_count++] = event.connection;
587 fb_mutex_unlock(&service->mutex);
604 fb_mutex_unlock(&service->mutex);
612 FB_EVENT *fb_loopback_socket (FB_SERVICE *service) { argument
613 assert (service);
618 event.service = service;
620 fb_mutex_lock(&service->mutex);
621 event.connection = fb_new_connection (service);
634 service->connections [service->connection_count++] = event.connection;
635 fb_mutex_unlock(&service->mutex);
649 fb_mutex_unlock(&service->mutex);
659 FB_ITERATOR *fb_new_iterator (FB_SERVICE *service) { argument
660 assert (service);
663 it->service = service;
664 it->iteration = service->connection_count;
665 fb_mutex_lock(&service->mutex);
680 assert (it->service);
684 if (it->service->connections [it->iteration]->state < FB_SOCKET_STATE_OPEN) {
689 event.service = it->service;
690 event.connection = it->service->connections [it->iteration];
704 fb_mutex_unlock(&it->service->mutex);