sock_t sock_connect_wto(const char *hostname, const int port, const int timeout) { int sock; sock = socket(AF_INET, SOCK_STREAM, 0); if (sock == -1) return -1; if (timeout) { sock_set_blocking (sock, SOCK_NONBLOCK); if (sock_try_connection (sock, hostname, port) < 0) { int ret = sock_connected (sock, timeout); if (ret <= 0) { sock_close (sock); return SOCK_ERROR; } } sock_set_blocking(sock, SOCK_BLOCK); } else { if (sock_try_connection (sock, hostname, port) < 0) { sock_close (sock); sock = SOCK_ERROR; } } return sock; }
static void worker_control_create (worker_t *worker) { if (pipe_create (&worker->wakeup_fd[0]) < 0) { ERROR0 ("pipe failed, descriptor limit?"); abort(); } sock_set_blocking (worker->wakeup_fd[0], 0); sock_set_blocking (worker->wakeup_fd[1], 0); }
static int connection_client_setup (connection_queue_t *node) { int err; err = -ENOENT; if (node->con->con_timeout <= time(NULL)) return err; global_lock(); err = client_create (&node->client, node->con, node->parser); if (err < 0) goto out_fail; if (sock_set_blocking (node->con->sock, 0) || sock_set_nodelay (node->con->sock)) { if (! sock_recoverable(sock_error())) { node->con->error = 1; err = -EINVAL; goto out_fail; } err = -EINPROGRESS; client_send_403 (node->client, "failed to set tcp options on client connection, dropping"); goto out_fail; } global_unlock(); return 0; out_fail: global_unlock(); return err; }
sock_t sock_connect_wto_bind (const char *hostname, int port, const char *bnd, int timeout) { sock_t sock; sock = sock_open (AF_INET, SOCK_STREAM, 0); if (sock == SOCK_ERROR) return SOCK_ERROR; if (bnd) { struct sockaddr_in sa; memset(&sa, 0, sizeof(sa)); sa.sin_family = AF_INET; if (inet_aton (bnd, &sa.sin_addr) == 0 || bind (sock, (struct sockaddr *)&sa, sizeof(sa)) < 0) { sock_close (sock); return SOCK_ERROR; } } if (timeout) { sock_set_blocking (sock, 0); if (sock_try_connection (sock, hostname, port) < 0) { int ret = sock_connected (sock, timeout); if (ret <= 0) { sock_close (sock); return SOCK_ERROR; } } sock_set_blocking(sock, 1); } else { if (sock_try_connection (sock, hostname, port) < 0) { sock_close (sock); sock = SOCK_ERROR; } } return sock; }
int tls_start(tls_t *tls) { sock_set_blocking(tls->sock); tls->lasterror = gnutls_handshake(tls->session); sock_set_nonblocking(tls->sock); return tls->lasterror == GNUTLS_E_SUCCESS; }
sock_t sock_connect_wto(const char *hostname, const int port, const int timeout) { int sock = SOCK_ERROR; struct addrinfo *ai, *head, hints; char service[8]; memset (&hints, 0, sizeof (hints)); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; snprintf (service, sizeof (service), "%u", port); if (getaddrinfo (hostname, service, &hints, &head)) return SOCK_ERROR; ai = head; while (ai) { if ((sock = socket (ai->ai_family, ai->ai_socktype, ai->ai_protocol)) > -1) { if (timeout) { sock_set_blocking (sock, SOCK_NONBLOCK); if (connect (sock, ai->ai_addr, ai->ai_addrlen) < 0) { if (sock_connected (sock, timeout) > 0) { sock_set_blocking(sock, SOCK_BLOCK); break; } } } else { if (connect (sock, ai->ai_addr, ai->ai_addrlen) == 0) break; } sock_close (sock); } sock = SOCK_ERROR; ai = ai->ai_next; } if (head) freeaddrinfo (head); return sock; }
int fserve_client_create(client_t *httpclient, char *path) { fserve_t *client = calloc(1, sizeof(fserve_t)); int bytes; int client_limit; ice_config_t *config = config_get_config(); client_limit = config->client_limit; config_release_config(); client->file = fopen(path, "rb"); if(!client->file) { client_send_404(httpclient, "File not readable"); return -1; } client->client = httpclient; client->offset = 0; client->datasize = 0; client->ready = 0; client->buf = malloc(BUFSIZE); global_lock(); if(global.clients >= client_limit) { httpclient->respcode = 504; bytes = sock_write(httpclient->con->sock, "HTTP/1.0 504 Server Full\r\n" "Content-Type: text/html\r\n\r\n" "<b>Server is full, try again later.</b>\r\n"); if(bytes > 0) httpclient->con->sent_bytes = bytes; fserve_client_destroy(client); global_unlock(); return -1; } global.clients++; global_unlock(); httpclient->respcode = 200; bytes = sock_write(httpclient->con->sock, "HTTP/1.0 200 OK\r\n" "Content-Type: %s\r\n\r\n", fserve_content_type(path)); if(bytes > 0) httpclient->con->sent_bytes = bytes; sock_set_blocking(client->client->con->sock, SOCK_NONBLOCK); sock_set_nodelay(client->client->con->sock); thread_mutex_lock (&pending_lock); client->next = (fserve_t *)pending_list; pending_list = client; thread_mutex_unlock (&pending_lock); return 0; }
static int _start_listening(void) { int i; for(i=0; i < global.server_sockets; i++) { if (sock_listen(global.serversock[i], ICE_LISTEN_QUEUE) == SOCK_ERROR) return 0; sock_set_blocking(global.serversock[i], SOCK_NONBLOCK); } return 1; }
sock_t sock_connect_non_blocking (const char *hostname, unsigned port) { sock_t sock; sock = sock_open (AF_INET, SOCK_STREAM, 0); if (sock == SOCK_ERROR) return SOCK_ERROR; sock_set_blocking (sock, 0); sock_try_connection (sock, hostname, port); return sock; }
int sock_connect_non_blocking (const char *hostname, const unsigned port) { int sock; sock = socket(AF_INET, SOCK_STREAM, 0); if (sock == -1) return -1; sock_set_blocking (sock, SOCK_NONBLOCK); sock_try_connection (sock, hostname, port); return sock; }
int sock_connect(int *fd, const char *host, unsigned short port, int timeout_secs) { int n = 0; socklen_t len = sizeof(n); struct sockaddr_in addr; struct hostent *lphost = NULL; fd_set writefds; struct timeval timeout; memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_addr.s_addr = inet_addr(host); if (addr.sin_addr.s_addr == INADDR_NONE) { lphost = gethostbyname(host); if (lphost) { addr.sin_addr.s_addr = ((struct in_addr *)lphost->h_addr)->s_addr; lphost = NULL; } else return -1; } addr.sin_port = htons(port); if (sock_set_blocking(fd, 0)) return -1; n = connect((*fd), (struct sockaddr *)&addr, sizeof(struct sockaddr_in)); if (n == -1) { if (errno == EINPROGRESS) { FD_ZERO(&writefds); FD_SET((*fd), &writefds); timeout.tv_sec = timeout_secs; timeout.tv_usec = 0; n = select(((*fd) + 1), 0, &writefds, 0, &timeout); if ((n > 0) && FD_ISSET((*fd), &writefds)) { getsockopt((*fd), SOL_SOCKET, SO_ERROR, &n, &len); return (n == 0) ? 0 : -1; } } else return -1; } else return 0; return -1; }
sock_t sock_connect_non_blocking (const char *hostname, unsigned port) { int sock = SOCK_ERROR; struct addrinfo *ai, *head, hints; char service[8]; memset (&hints, 0, sizeof (hints)); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; snprintf (service, sizeof (service), "%u", port); if (getaddrinfo (hostname, service, &hints, &head)) return SOCK_ERROR; ai = head; while (ai) { int type = ai->ai_socktype; if ((sock = sock_open (ai->ai_family, type, ai->ai_protocol)) > -1) { sock_set_cloexec(sock); sock_set_blocking (sock, 0); if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0 && !sock_connect_pending(sock_error())) { sock_close (sock); sock = SOCK_ERROR; } else break; } ai = ai->ai_next; } if (head) freeaddrinfo (head); return sock; }
/* * This is called to handle a brand new connection, in it's own thread. * Nothing is know about the type of the connection. * Assert Class: 3 */ void *handle_connection(void *arg) { connection_t *con = (connection_t *)arg; char line[BUFSIZE] = ""; int res; thread_init(); if (!con) { write_log(LOG_DEFAULT, "handle_connection: got NULL connection"); thread_exit(0); } if (info.reverse_lookups) con->hostname = reverse(con->host); sock_set_blocking(con->sock, SOCK_BLOCK); /* Fill line[] with the user header, ends with \n\n */ if ((res = sock_read_lines(con->sock, line, BUFSIZE)) <= 0) { write_log(LOG_DEFAULT, "Socket error on connection %d", con->id); kick_not_connected(con, "Socket error"); thread_exit(0); } if (ice_strncmp(line, "GET", 3) == 0) { client_login(con, line); } else if (ice_strncmp(line, "SOURCE", 6) == 0) { source_login (con, line); } else { write_400 (con); kick_not_connected(con, "Invalid header"); } thread_exit(0); return NULL; }
static void *_handle_connection(void *arg) { char header[4096]; connection_t *con; http_parser_t *parser; char *rawuri, *uri; client_t *client; while (global.running == ICE_RUNNING) { /* grab a connection and set the socket to blocking */ while ((con = _get_connection())) { /* Handle meta-connections */ if(con->event_number > 0) { switch(con->event_number) { case EVENT_CONFIG_READ: event_config_read(con->event); break; default: ERROR1("Unknown event number: %d", con->event_number); break; } free(con); continue; } stats_event_inc(NULL, "connections"); sock_set_blocking(con->sock, SOCK_BLOCK); /* fill header with the http header */ memset(header, 0, sizeof (header)); if (util_read_header(con->sock, header, sizeof (header)) == 0) { /* either we didn't get a complete header, or we timed out */ connection_close(con); continue; } parser = httpp_create_parser(); httpp_initialize(parser, NULL); if (httpp_parse(parser, header, strlen(header))) { /* handle the connection or something */ if (strcmp("ICE", httpp_getvar(parser, HTTPP_VAR_PROTOCOL)) && strcmp("HTTP", httpp_getvar(parser, HTTPP_VAR_PROTOCOL))) { ERROR0("Bad HTTP protocol detected"); connection_close(con); httpp_destroy(parser); continue; } rawuri = httpp_getvar(parser, HTTPP_VAR_URI); uri = util_normalise_uri(rawuri); if(!uri) { client = client_create(con, parser); client_send_404(client, "The path you requested was invalid"); continue; } if (parser->req_type == httpp_req_source) { _handle_source_request(con, parser, uri); } else if (parser->req_type == httpp_req_stats) { _handle_stats_request(con, parser, uri); } else if (parser->req_type == httpp_req_get) { _handle_get_request(con, parser, uri); } else { ERROR0("Wrong request type from client"); connection_close(con); httpp_destroy(parser); } free(uri); continue; } else if(httpp_parse_icy(parser, header, strlen(header))) { /* TODO: Map incoming icy connections to /icy_0, etc. */ char mount[20]; unsigned i = 0; strcpy(mount, "/"); avl_tree_rlock(global.source_tree); while (source_find_mount (mount) != NULL) { snprintf (mount, sizeof (mount), "/icy_%u", i++); } avl_tree_unlock(global.source_tree); _handle_source_request(con, parser, mount); continue; } else { ERROR0("HTTP request parsing failed"); connection_close(con); httpp_destroy(parser); continue; } } thread_sleep (100000); } DEBUG0 ("Connection thread done"); return NULL; }
static client_t *accept_client (void) { client_t *client = NULL; sock_t sock, serversock = wait_for_serversock (); char addr [200]; if (serversock == SOCK_ERROR) return NULL; sock = sock_accept (serversock, addr, 200); if (sock == SOCK_ERROR) { if (sock_recoverable (sock_error())) return NULL; WARN2 ("accept() failed with error %d: %s", sock_error(), strerror(sock_error())); thread_sleep (500000); return NULL; } do { int i, num; refbuf_t *r; if (sock_set_blocking (sock, 0) || sock_set_nodelay (sock)) { WARN0 ("failed to set tcp options on client connection, dropping"); break; } client = calloc (1, sizeof (client_t)); if (client == NULL || connection_init (&client->connection, sock, addr) < 0) break; client->shared_data = r = refbuf_new (PER_CLIENT_REFBUF_SIZE); r->len = 0; // for building up the request coming in global_lock (); client_register (client); for (i=0; i < global.server_sockets; i++) { if (global.serversock[i] == serversock) { client->server_conn = global.server_conn[i]; client->server_conn->refcount++; if (client->server_conn->ssl && ssl_ok) connection_uses_ssl (&client->connection); if (client->server_conn->shoutcast_compat) client->ops = &shoutcast_source_ops; else client->ops = &http_request_ops; break; } } num = global.clients; global_unlock (); stats_event_args (NULL, "clients", "%d", num); client->flags |= CLIENT_ACTIVE; return client; } while (0); free (client); sock_close (sock); return NULL; }
/* Actually open the connection and do some http parsing, handle any 302 * responses within here. */ static client_t *open_relay_connection (relay_server *relay) { int redirects = 0; char *server_id = NULL; ice_config_t *config; http_parser_t *parser = NULL; connection_t *con=NULL; char *server = strdup (relay->server); char *mount = strdup (relay->mount); int port = relay->port; char *auth_header; char header[4096]; config = config_get_config (); server_id = strdup (config->server_id); config_release_config (); /* build any authentication header before connecting */ if (relay->username && relay->password) { char *esc_authorisation; unsigned len = strlen(relay->username) + strlen(relay->password) + 2; auth_header = malloc (len); snprintf (auth_header, len, "%s:%s", relay->username, relay->password); esc_authorisation = util_base64_encode(auth_header, len); free(auth_header); len = strlen (esc_authorisation) + 24; auth_header = malloc (len); snprintf (auth_header, len, "Authorization: Basic %s\r\n", esc_authorisation); free(esc_authorisation); } else auth_header = strdup (""); while (redirects < 10) { sock_t streamsock; ICECAST_LOG_INFO("connecting to %s:%d", server, port); streamsock = sock_connect_wto_bind (server, port, relay->bind, 10); if (streamsock == SOCK_ERROR) { ICECAST_LOG_WARN("Failed to connect to %s:%d", server, port); break; } con = connection_create (streamsock, -1, strdup (server)); /* At this point we may not know if we are relaying an mp3 or vorbis * stream, but only send the icy-metadata header if the relay details * state so (the typical case). It's harmless in the vorbis case. If * we don't send in this header then relay will not have mp3 metadata. */ sock_write(streamsock, "GET %s HTTP/1.0\r\n" "User-Agent: %s\r\n" "Host: %s\r\n" "%s" "%s" "\r\n", mount, server_id, server, relay->mp3metadata?"Icy-MetaData: 1\r\n":"", auth_header); memset (header, 0, sizeof(header)); if (util_read_header (con->sock, header, 4096, READ_ENTIRE_HEADER) == 0) { ICECAST_LOG_ERROR("Header read failed for %s (%s:%d%s)", relay->localmount, server, port, mount); break; } parser = httpp_create_parser(); httpp_initialize (parser, NULL); if (! httpp_parse_response (parser, header, strlen(header), relay->localmount)) { ICECAST_LOG_ERROR("Error parsing relay request for %s (%s:%d%s)", relay->localmount, server, port, mount); break; } if (strcmp (httpp_getvar (parser, HTTPP_VAR_ERROR_CODE), "302") == 0) { /* better retry the connection again but with different details */ const char *uri, *mountpoint; int len; uri = httpp_getvar (parser, "location"); ICECAST_LOG_INFO("redirect received %s", uri); if (strncmp (uri, "http://", 7) != 0) break; uri += 7; mountpoint = strchr (uri, '/'); free (mount); if (mountpoint) mount = strdup (mountpoint); else mount = strdup ("/"); len = strcspn (uri, ":/"); port = 80; if (uri [len] == ':') port = atoi (uri+len+1); free (server); server = calloc (1, len+1); strncpy (server, uri, len); connection_close (con); httpp_destroy (parser); con = NULL; parser = NULL; } else { client_t *client = NULL; if (httpp_getvar (parser, HTTPP_VAR_ERROR_MESSAGE)) { ICECAST_LOG_ERROR("Error from relay request: %s (%s)", relay->localmount, httpp_getvar(parser, HTTPP_VAR_ERROR_MESSAGE)); break; } global_lock (); if (client_create (&client, con, parser) < 0) { global_unlock (); /* make sure only the client_destory frees these */ con = NULL; parser = NULL; client_destroy (client); break; } global_unlock (); sock_set_blocking (streamsock, 0); client_set_queue (client, NULL); free (server); free (mount); free (server_id); free (auth_header); return client; } redirects++; } /* failed, better clean up */ free (server); free (mount); free (server_id); free (auth_header); if (con) connection_close (con); if (parser) httpp_destroy (parser); return NULL; }
void connection_accept_loop(void) { connection_t *con; ice_config_t *config; config = config_get_config (); get_ssl_certificate (config); config_release_config (); tid = thread_create ("connection thread", _handle_connection, NULL, THREAD_ATTACHED); while (global.running == ICE_RUNNING) { con = _accept_connection(); if (con) { client_queue_t *node; ice_config_t *config; client_t *client = NULL; listener_t *listener; global_lock(); if (client_create (&client, con, NULL) < 0) { global_unlock(); client_send_403 (client, "Icecast connection limit reached"); /* don't be too eager as this is an imposed hard limit */ thread_sleep (400000); continue; } global_unlock(); /* setup client for reading incoming http */ client->refbuf->data [PER_CLIENT_REFBUF_SIZE-1] = '\000'; node = calloc (1, sizeof (client_queue_t)); if (node == NULL) { client_destroy (client); continue; } node->client = client; config = config_get_config(); listener = config_get_listen_sock (config, client->con); if (listener) { if (listener->shoutcast_compat) node->shoutcast = 1; if (listener->ssl && ssl_ok) connection_uses_ssl (client->con); if (listener->shoutcast_mount) node->shoutcast_mount = strdup (listener->shoutcast_mount); } config_release_config(); sock_set_blocking (client->con->sock, SOCK_NONBLOCK); sock_set_nodelay (client->con->sock); _add_request_queue (node); stats_event_inc (NULL, "connections"); } process_request_queue (); } /* Give all the other threads notification to shut down */ thread_cond_broadcast(&global.shutdown_cond); if (tid) thread_join (tid); /* wait for all the sources to shutdown */ thread_rwlock_wlock(&_source_shutdown_rwlock); thread_rwlock_unlock(&_source_shutdown_rwlock); }
/* Actually open the connection and do some http parsing, handle any 302 * responses within here. */ static int open_relay_connection (client_t *client, relay_server *relay, relay_server_master *master) { int redirects = 0; http_parser_t *parser = NULL; connection_t *con = &client->connection; char *server = strdup (master->ip); char *mount = strdup (master->mount); int port = master->port, timeout = master->timeout, ask_for_metadata = relay->mp3metadata; char *auth_header = NULL; if (relay->username && relay->password) { char *esc_authorisation; unsigned len = strlen(relay->username) + strlen(relay->password) + 2; DEBUG2 ("using username %s for %s", relay->username, relay->localmount); auth_header = malloc (len); snprintf (auth_header, len, "%s:%s", relay->username, relay->password); esc_authorisation = util_base64_encode(auth_header); free(auth_header); len = strlen (esc_authorisation) + 24; auth_header = malloc (len); snprintf (auth_header, len, "Authorization: Basic %s\r\n", esc_authorisation); free(esc_authorisation); } while (redirects < 10) { sock_t streamsock; char *bind = NULL; /* policy decision, we assume a source bind even after redirect, possible option */ if (master->bind) bind = strdup (master->bind); if (bind) INFO4 ("connecting to %s:%d for %s, bound to %s", server, port, relay->localmount, bind); else INFO3 ("connecting to %s:%d for %s", server, port, relay->localmount); con->con_time = time (NULL); relay->in_use = master; streamsock = sock_connect_wto_bind (server, port, bind, timeout); free (bind); if (connection_init (con, streamsock, server) < 0) { WARN2 ("Failed to connect to %s:%d", server, port); break; } parser = get_relay_response (con, mount, server, ask_for_metadata, auth_header); if (parser == NULL) { ERROR4 ("Problem trying to start relay on %s (%s:%d%s)", relay->localmount, server, port, mount); break; } if (strcmp (httpp_getvar (parser, HTTPP_VAR_ERROR_CODE), "302") == 0) { /* better retry the connection again but with different details */ const char *uri, *mountpoint; int len; uri = httpp_getvar (parser, "location"); INFO1 ("redirect received %s", uri); if (strncmp (uri, "http://", 7) != 0) break; uri += 7; mountpoint = strchr (uri, '/'); free (mount); if (mountpoint) mount = strdup (mountpoint); else mount = strdup ("/"); len = strcspn (uri, ":/"); port = 80; if (uri [len] == ':') port = atoi (uri+len+1); free (server); server = calloc (1, len+1); strncpy (server, uri, len); connection_close (con); httpp_destroy (parser); parser = NULL; } else { if (httpp_getvar (parser, HTTPP_VAR_ERROR_MESSAGE)) { ERROR3 ("Error from relay request on %s (%s %s)", relay->localmount, master->mount, httpp_getvar(parser, HTTPP_VAR_ERROR_MESSAGE)); client->parser = NULL; break; } sock_set_blocking (streamsock, 0); thread_rwlock_wlock (&relay->source->lock); client->parser = parser; // old parser will be free in the format clear thread_rwlock_unlock (&relay->source->lock); client->connection.discon_time = 0; client->connection.con_time = time (NULL); client_set_queue (client, NULL); free (server); free (mount); free (auth_header); return 0; } redirects++; } /* failed, better clean up */ free (server); free (mount); free (auth_header); if (parser) httpp_destroy (parser); connection_close (con); con->con_time = time (NULL); // sources count needs to drop in such cases if (relay->in_use) relay->in_use->skip = 1; return -1; }
void connection_accept_loop (void) { connection_t *con; ice_config_t *config; int duration = 300; config = config_get_config (); get_ssl_certificate (config); config_release_config (); while (global.running == ICE_RUNNING) { con = _accept_connection (duration); if (con) { client_queue_t *node; ice_config_t *config; client_t *client = NULL; listener_t *listener; global_lock(); if (client_create (&client, con, NULL) < 0) { global_unlock(); client_send_403 (client, "Icecast connection limit reached"); /* don't be too eager as this is an imposed hard limit */ thread_sleep (400000); continue; } /* setup client for reading incoming http */ client->refbuf->data [PER_CLIENT_REFBUF_SIZE-1] = '\000'; if (sock_set_blocking (client->con->sock, 0) || sock_set_nodelay (client->con->sock)) { global_unlock(); WARN0 ("failed to set tcp options on client connection, dropping"); client_destroy (client); continue; } node = calloc (1, sizeof (client_queue_t)); if (node == NULL) { global_unlock(); client_destroy (client); continue; } node->client = client; config = config_get_config(); listener = config_get_listen_sock (config, client->con); if (listener) { if (listener->shoutcast_compat) node->shoutcast = 1; if (listener->ssl && ssl_ok) connection_uses_ssl (client->con); if (listener->shoutcast_mount) node->shoutcast_mount = strdup (listener->shoutcast_mount); } global_unlock(); config_release_config(); _add_request_queue (node); stats_event_inc (NULL, "connections"); duration = 5; } else { if (_req_queue == NULL) duration = 300; /* use longer timeouts when nothing waiting */ } process_request_queue (); } /* Give all the other threads notification to shut down */ thread_cond_broadcast(&global.shutdown_cond); /* wait for all the sources to shutdown */ thread_rwlock_wlock(&_source_shutdown_rwlock); thread_rwlock_unlock(&_source_shutdown_rwlock); }
/* called when listening thread is not checking for incoming connections */ int connection_setup_sockets (ice_config_t *config) { int count = 0; listener_t *listener, **prev; free (banned_ip.filename); banned_ip.filename = NULL; free (allowed_ip.filename); allowed_ip.filename = NULL; global_lock(); if (global.serversock) { for (; count < global.server_sockets; count++) sock_close (global.serversock [count]); free (global.serversock); global.serversock = NULL; } if (config == NULL) { global_unlock(); return 0; } /* setup the banned/allowed IP filenames from the xml */ if (config->banfile) banned_ip.filename = strdup (config->banfile); if (config->allowfile) allowed_ip.filename = strdup (config->allowfile); count = 0; global.serversock = calloc (config->listen_sock_count, sizeof (sock_t)); listener = config->listen_sock; prev = &config->listen_sock; while (listener) { int successful = 0; do { sock_t sock = sock_get_server_socket (listener->port, listener->bind_address); if (sock == SOCK_ERROR) break; if (sock_listen (sock, ICE_LISTEN_QUEUE) == SOCK_ERROR) { sock_close (sock); break; } /* some win32 setups do not do TCP win scaling well, so allow an override */ if (listener->so_sndbuf) sock_set_send_buffer (sock, listener->so_sndbuf); sock_set_blocking (sock, 0); successful = 1; global.serversock [count] = sock; count++; } while(0); if (successful == 0) { if (listener->bind_address) ERROR2 ("Could not create listener socket on port %d bind %s", listener->port, listener->bind_address); else ERROR1 ("Could not create listener socket on port %d", listener->port); /* remove failed connection */ *prev = config_clear_listener (listener); listener = *prev; continue; } if (listener->bind_address) INFO2 ("listener socket on port %d address %s", listener->port, listener->bind_address); else INFO1 ("listener socket on port %d", listener->port); prev = &listener->next; listener = listener->next; } global.server_sockets = count; global_unlock(); if (count == 0) ERROR0 ("No listening sockets established"); return count; }
int fserve_client_create(client_t *httpclient, char *path) { fserve_t *client = calloc(1, sizeof(fserve_t)); int bytes; int client_limit; ice_config_t *config = config_get_config(); struct stat file_buf; char *range = NULL; int64_t new_content_len = 0; int64_t rangenumber = 0; int rangeproblem = 0; int ret = 0; client_limit = config->client_limit; config_release_config(); client->file = fopen(path, "rb"); if(!client->file) { client_send_404(httpclient, "File not readable"); return -1; } client->client = httpclient; client->offset = 0; client->datasize = 0; client->ready = 0; client->content_length = 0; client->buf = malloc(BUFSIZE); if (stat(path, &file_buf) == 0) { client->content_length = (int64_t)file_buf.st_size; } global_lock(); if(global.clients >= client_limit) { global_unlock(); httpclient->respcode = 504; bytes = sock_write(httpclient->con->sock, "HTTP/1.0 504 Server Full\r\n" "Content-Type: text/html\r\n\r\n" "<b>Server is full, try again later.</b>\r\n"); if(bytes > 0) httpclient->con->sent_bytes = bytes; fserve_client_destroy(client); return -1; } global.clients++; global_unlock(); range = httpp_getvar (client->client->parser, "range"); if (range != NULL) { ret = sscanf(range, "bytes=" FORMAT_INT64 "-", &rangenumber); if (ret != 1) { /* format not correct, so lets just assume we start from the beginning */ rangeproblem = 1; } if (rangenumber < 0) { rangeproblem = 1; } if (!rangeproblem) { ret = fseek(client->file, rangenumber, SEEK_SET); if (ret != -1) { new_content_len = client->content_length - rangenumber; if (new_content_len < 0) { rangeproblem = 1; } } else { rangeproblem = 1; } if (!rangeproblem) { /* Date: is required on all HTTP1.1 responses */ char currenttime[50]; time_t now; int strflen; struct tm result; int64_t endpos = rangenumber+new_content_len-1; if (endpos < 0) { endpos = 0; } time(&now); strflen = strftime(currenttime, 50, "%a, %d-%b-%Y %X GMT", gmtime_r(&now, &result)); httpclient->respcode = 206; bytes = sock_write(httpclient->con->sock, "HTTP/1.1 206 Partial Content\r\n" "Date: %s\r\n" "Content-Length: " FORMAT_INT64 "\r\n" "Content-Range: bytes " FORMAT_INT64 \ "-" FORMAT_INT64 "/" FORMAT_INT64 "\r\n" "Content-Type: %s\r\n\r\n", currenttime, new_content_len, rangenumber, endpos, client->content_length, fserve_content_type(path)); if(bytes > 0) httpclient->con->sent_bytes = bytes; } else { httpclient->respcode = 416; bytes = sock_write(httpclient->con->sock, "HTTP/1.0 416 Request Range Not Satisfiable\r\n\r\n"); if(bytes > 0) httpclient->con->sent_bytes = bytes; fserve_client_destroy(client); return -1; } } else { /* If we run into any issues with the ranges we fallback to a normal/non-range request */ httpclient->respcode = 416; bytes = sock_write(httpclient->con->sock, "HTTP/1.0 416 Request Range Not Satisfiable\r\n\r\n"); if(bytes > 0) httpclient->con->sent_bytes = bytes; fserve_client_destroy(client); return -1; } } else { httpclient->respcode = 200; bytes = sock_write(httpclient->con->sock, "HTTP/1.0 200 OK\r\n" "Content-Length: " FORMAT_INT64 "\r\n" "Content-Type: %s\r\n\r\n", client->content_length, fserve_content_type(path)); if(bytes > 0) httpclient->con->sent_bytes = bytes; } sock_set_blocking(client->client->con->sock, SOCK_NONBLOCK); sock_set_nodelay(client->client->con->sock); thread_mutex_lock (&pending_lock); client->next = (fserve_t *)pending_list; pending_list = client; thread_mutex_unlock (&pending_lock); return 0; }
/* issue a connect, but return after the timeout (seconds) is reached. If * timeout is 0 or less then we will wait until the OS gives up on the connect * The socket is returned */ sock_t sock_connect_wto_bind (const char *hostname, int port, const char *bnd, int timeout) { sock_t sock = SOCK_ERROR; struct addrinfo *ai, *head, *b_head=NULL, hints; char service[8]; memset (&hints, 0, sizeof (hints)); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; snprintf (service, sizeof (service), "%u", port); if (getaddrinfo (hostname, service, &hints, &head)) return SOCK_ERROR; ai = head; while (ai) { int type = ai->ai_socktype; if ((sock = sock_open (ai->ai_family, type, ai->ai_protocol)) >= 0) { sock_set_cloexec (sock); if (timeout > 0) sock_set_blocking (sock, 0); if (bnd) { struct addrinfo b_hints; memset (&b_hints, 0, sizeof(b_hints)); b_hints.ai_family = ai->ai_family; b_hints.ai_socktype = ai->ai_socktype; b_hints.ai_protocol = ai->ai_protocol; if (getaddrinfo (bnd, NULL, &b_hints, &b_head) || bind (sock, b_head->ai_addr, b_head->ai_addrlen) < 0) { sock_close (sock); sock = SOCK_ERROR; break; } } if (connect (sock, ai->ai_addr, ai->ai_addrlen) == 0) break; /* loop as the connect maybe async */ while (sock != SOCK_ERROR) { if (sock_recoverable (sock_error())) { int connected = sock_connected (sock, timeout); if (connected == 0) /* try again, interrupted */ continue; if (connected == 1) /* connected */ { if (timeout >= 0) sock_set_blocking(sock, 1); break; } } sock_close (sock); sock = SOCK_ERROR; } if (sock != SOCK_ERROR) break; } ai = ai->ai_next; } if (b_head) freeaddrinfo (b_head); freeaddrinfo (head); return sock; }
/* * Connect to hostname on specified port and return the created socket. * Assert Class: 3 */ SOCKET sock_connect_wto(const char *hostname, const int port, const int timeout) { SOCKET sockfd; struct sockaddr_in sin, server; struct hostent *host; struct hostent hostinfo; char buf[BUFSIZE]; int error; if (!hostname || !hostname[0]) { write_log(LOG_DEFAULT, "ERROR: sock_connect() called with NULL or empty hostname"); return INVALID_SOCKET; } else if (port <= 0) { write_log(LOG_DEFAULT, "ERROR: sock_connect() called with invalid port number"); return INVALID_SOCKET; } sockfd = sock_socket(AF_INET, SOCK_STREAM, 0); if (sockfd == INVALID_SOCKET) { sock_close(sockfd); return INVALID_SOCKET; } if (info.myhostname != NULL) { struct sockaddr_in localsin; memset(&localsin, 0, sizeof (struct sockaddr_in)); xa_debug(2, "DEBUG: Trying to bind to %s", info.myhostname); localsin.sin_addr = localaddr; localsin.sin_family = AF_INET; localsin.sin_port = 0; if (bind (sockfd, (struct sockaddr *) &localsin, sizeof (localsin)) == SOCKET_ERROR) { xa_debug(2, "DEBUG: Unable to bind", info.myhostname); write_log(LOG_DEFAULT, "ERROR: Bind to local address %s failed", info.myhostname); sock_close(sockfd); return INVALID_SOCKET; } } memset(&sin, 0, sizeof (sin)); memset(&server, 0, sizeof (struct sockaddr_in)); if (isdigit((int) hostname[0]) && isdigit((int) hostname[ice_strlen(hostname) - 1])) { if (inet_aton(hostname, (struct in_addr *) &sin.sin_addr) == 0) { write_log(LOG_DEFAULT, "ERROR: Invalid ip number %s", hostname); sock_close(sockfd); return INVALID_SOCKET; } memcpy(&server.sin_addr, &sin.sin_addr, sizeof (sin)); } else { host = ice_gethostbyname(hostname, &hostinfo, buf, BUFSIZE, &error); if (host == NULL) { xa_debug(1, "DEBUG: gethostbyname %s failed", hostname); sock_close(sockfd); ice_clean_hostent(); return INVALID_SOCKET; } memcpy(&server.sin_addr, host->h_addr, host->h_length); ice_clean_hostent(); } server.sin_family = AF_INET; server.sin_port = htons(port); { char buf[50]; makeasciihost(&server.sin_addr, buf); xa_debug(1, "Trying to connect to %s:%d", buf, port); } if (timeout > 0) { fd_set wfds; struct timeval tv; int retval; int val; socklen_t valsize = sizeof (int); xa_debug(3, "DEBUG: sock_connect(): doing a connection w/ timeout"); FD_ZERO(&wfds); FD_SET(sockfd, &wfds); tv.tv_sec = timeout; tv.tv_usec = 0; sock_set_blocking(sockfd, SOCK_NONBLOCK); retval = connect(sockfd, (struct sockaddr *) &server, sizeof (server)); if (retval == 0) { xa_debug(3, "DEBUG: sock_connect(): non blocking connect returned 0!"); sock_set_blocking(sockfd, SOCK_BLOCK); return sockfd; } else { #ifdef _WIN32 if (WSAGetLastError() == WSAEINPROGRESS) { #else if (!is_recoverable(errno)) { #endif xa_debug(3, "DEBUG: sock_connect(): connect didn't return EINPROGRESS!, was: %d", errno); sock_close(sockfd); return SOCKET_ERROR; } } if (select(sockfd + 1, NULL, &wfds, NULL, &tv)) { retval = getsockopt(sockfd, SOL_SOCKET, SO_ERROR, (void *) &val, (socklen_t *) & valsize); if ((retval == 0) && (val == 0)) { sock_set_blocking(sockfd, SOCK_BLOCK); return sockfd; } else { xa_debug(3, "DEBUG: sock_connect(): getsockopt returned %i, val = %i, valsize = %i, errno = %i!", retval, val, valsize, errno); sock_close(sockfd); return SOCKET_ERROR; } } else { xa_debug(3, "DEBUG: sock_connect(): select returned 0"); sock_close(sockfd); return SOCKET_ERROR; } } else { if (connect
static void source_init (source_t *source) { ice_config_t *config = config_get_config(); char *listenurl, *str; int listen_url_size; char *s; /* 6 for max size of port */ listen_url_size = strlen("http://") + strlen(config->hostname) + strlen(":") + 6 + strlen(source->mount) + 1; listenurl = malloc (listen_url_size); memset (listenurl, '\000', listen_url_size); snprintf (listenurl, listen_url_size, "http://%s:%d%s", config->hostname, config->port, source->mount); config_release_config(); do { str = "0"; if (source->yp_prevent) break; if ((str = httpp_getvar(source->parser, "ice-public"))) break; if ((str = httpp_getvar(source->parser, "icy-pub"))) break; /* handle header from icecast v2 release */ if ((str = httpp_getvar(source->parser, "icy-public"))) break; str = "0"; } while (0); source->yp_public = atoi (str); stats_event (source->mount, "public", str); str = httpp_getvar(source->parser, "ice-audio-info"); source->audio_info = util_dict_new(); if (str) { _parse_audio_info (source, str); stats_event (source->mount, "audio_info", str); } stats_event (source->mount, "listenurl", listenurl); if (listenurl) { free(listenurl); } if (source->dumpfilename != NULL) { source->dumpfile = fopen (source->dumpfilename, "ab"); if (source->dumpfile == NULL) { WARN2("Cannot open dump file \"%s\" for appending: %s, disabling.", source->dumpfilename, strerror(errno)); } } /* grab a read lock, to make sure we get a chance to cleanup */ thread_rwlock_rlock (source->shutdown_rwlock); /* start off the statistics */ source->listeners = 0; stats_event_inc (NULL, "sources"); stats_event_inc (NULL, "source_total_connections"); stats_event (source->mount, "listeners", "0"); sock_set_blocking (source->con->sock, SOCK_NONBLOCK); DEBUG0("Source creation complete"); source->last_read = time (NULL); source->running = 1; /* ** Now, if we have a fallback source and override is on, we want ** to steal its clients, because it means we've come back online ** after a failure and they should be gotten back from the waiting ** loop or jingle track or whatever the fallback is used for */ if (source->fallback_override && source->fallback_mount) { source_t *fallback_source; avl_tree_rlock(global.source_tree); fallback_source = source_find_mount(source->fallback_mount); if (fallback_source) source_move_clients (fallback_source, source); avl_tree_unlock(global.source_tree); } if (source->yp_public) { yp_add (source); } else { /* If we are a private server, see if ic*-name and description is provided, and if so, add them to the stats */ if ((s = httpp_getvar(source->parser, "ice-name"))) { stats_event (source->mount, "server_name", s); } if ((s = httpp_getvar(source->parser, "icy-name"))) { stats_event (source->mount, "server_name", s); } if ((s = httpp_getvar(source->parser, "ice-description"))) { stats_event (source->mount, "server_description", s); } } }
void connection_accept_loop(void) { connection_t *con; if (!kitsune_is_updating()) /**DSU control */ tid = thread_create("connection thread", _handle_connection, NULL, THREAD_ATTACHED); while (global.running == ICE_RUNNING) { kitsune_update("connection_accept"); /**DSU updatepoint */ con = _accept_connection(); if (con) { client_queue_t *node; ice_config_t *config; int i; client_t *client = NULL; global_lock(); if (client_create (&client, con, NULL) < 0) { global_unlock(); client_send_404 (client, "Icecast connection limit reached"); continue; } global_unlock(); /* setup client for reading incoming http */ client->refbuf->data [PER_CLIENT_REFBUF_SIZE-1] = '\000'; node = calloc (1, sizeof (client_queue_t)); if (node == NULL) { client_destroy (client); continue; } node->client = client; /* Check for special shoutcast compatability processing */ config = config_get_config(); for (i = 0; i < global.server_sockets; i++) { if (global.serversock[i] == con->serversock) { if (config->listeners[i].shoutcast_compat) node->shoutcast = 1; } } config_release_config(); sock_set_blocking (client->con->sock, SOCK_NONBLOCK); sock_set_nodelay (client->con->sock); _add_request_queue (node); stats_event_inc (NULL, "connections"); } process_request_queue (); } /* Give all the other threads notification to shut down */ thread_cond_broadcast(&global.shutdown_cond); if (tid) thread_join (tid); /* wait for all the sources to shutdown */ thread_rwlock_wlock(&_source_shutdown_rwlock); thread_rwlock_unlock(&_source_shutdown_rwlock); }
static void _handle_get_request(connection_t *con, http_parser_t *parser, char *passed_uri) { char *fullpath; client_t *client; int bytes; struct stat statbuf; source_t *source; int fileserve; char *host; int port; int i; char *serverhost = NULL; int serverport = 0; aliases *alias; ice_config_t *config; int client_limit; int ret; char *uri = passed_uri; config = config_get_config(); fileserve = config->fileserve; host = config->hostname; port = config->port; for(i = 0; i < MAX_LISTEN_SOCKETS; i++) { if(global.serversock[i] == con->serversock) { serverhost = config->listeners[i].bind_address; serverport = config->listeners[i].port; break; } } alias = config->aliases; client_limit = config->client_limit; /* there are several types of HTTP GET clients ** media clients, which are looking for a source (eg, URI = /stream.ogg) ** stats clients, which are looking for /admin/stats.xml ** and directory server authorizers, which are looking for /GUID-xxxxxxxx ** (where xxxxxx is the GUID in question) - this isn't implemented yet. ** we need to handle the latter two before the former, as the latter two ** aren't subject to the limits. */ /* TODO: add GUID-xxxxxx */ /* Handle aliases */ while(alias) { if(strcmp(uri, alias->source) == 0 && (alias->port == -1 || alias->port == serverport) && (alias->bind_address == NULL || (serverhost != NULL && strcmp(alias->bind_address, serverhost) == 0))) { uri = strdup (alias->destination); DEBUG2 ("alias has made %s into %s", passed_uri, uri); break; } alias = alias->next; } config_release_config(); /* make a client */ client = client_create(con, parser); stats_event_inc(NULL, "client_connections"); /* Dispatch all admin requests */ if (strncmp(uri, "/admin/", 7) == 0) { admin_handle_request(client, uri); if (uri != passed_uri) free (uri); return; } /* Here we are parsing the URI request to see ** if the extension is .xsl, if so, then process ** this request as an XSLT request */ fullpath = util_get_path_from_normalised_uri(uri); if (util_check_valid_extension(fullpath) == XSLT_CONTENT) { /* If the file exists, then transform it, otherwise, write a 404 */ if (stat(fullpath, &statbuf) == 0) { DEBUG0("Stats request, sending XSL transformed stats"); client->respcode = 200; bytes = sock_write(client->con->sock, "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n"); if(bytes > 0) client->con->sent_bytes = bytes; stats_transform_xslt(client, fullpath); client_destroy(client); } else { client_send_404(client, "The file you requested could not be found"); } free(fullpath); if (uri != passed_uri) free (uri); return; } else if(fileserve && stat(fullpath, &statbuf) == 0 && #ifdef _WIN32 ((statbuf.st_mode) & _S_IFREG)) #else S_ISREG(statbuf.st_mode)) #endif { fserve_client_create(client, fullpath); free(fullpath); if (uri != passed_uri) free (uri); return; } free(fullpath); if(strcmp(util_get_extension(uri), "m3u") == 0) { char *sourceuri = strdup(uri); char *dot = strrchr(sourceuri, '.'); *dot = 0; client->respcode = 200; bytes = sock_write(client->con->sock, "HTTP/1.0 200 OK\r\n" "Content-Type: audio/x-mpegurl\r\n\r\n" "http://%s:%d%s\r\n", host, port, sourceuri ); if(bytes > 0) client->con->sent_bytes = bytes; client_destroy(client); free(sourceuri); if (uri != passed_uri) free (uri); return; } global_lock(); if (global.clients >= client_limit) { global_unlock(); client_send_404(client, "The server is already full. Try again later."); if (uri != passed_uri) free (uri); return; } global_unlock(); avl_tree_rlock(global.source_tree); source = source_find_mount(uri); if (source) { DEBUG0("Source found for client"); /* The source may not be the requested source - it might have gone * via one or more fallbacks. We only reject it for no-mount if it's * the originally requested source */ if(strcmp(uri, source->mount) == 0 && source->no_mount) { avl_tree_unlock(global.source_tree); client_send_404(client, "This mount is unavailable."); if (uri != passed_uri) free (uri); return; } if (source->running == 0) { avl_tree_unlock(global.source_tree); DEBUG0("inactive source, client dropped"); client_send_404(client, "This mount is unavailable."); if (uri != passed_uri) free (uri); return; } /* Check for any required authentication first */ if(source->authenticator != NULL) { ret = auth_check_client(source, client); if(ret != AUTH_OK) { avl_tree_unlock(global.source_tree); if (ret == AUTH_FORBIDDEN) { INFO1("Client attempted to log multiple times to source " "(\"%s\")", uri); client_send_403(client); } else { /* If not FORBIDDEN, default to 401 */ INFO1("Client attempted to log in to source (\"%s\")with " "incorrect or missing password", uri); client_send_401(client); } if (uri != passed_uri) free (uri); return; } } /* And then check that there's actually room in the server... */ global_lock(); if (global.clients >= client_limit) { global_unlock(); avl_tree_unlock(global.source_tree); client_send_404(client, "The server is already full. Try again later."); if (uri != passed_uri) free (uri); return; } /* Early-out for per-source max listeners. This gets checked again * by the source itself, later. This route gives a useful message to * the client, also. */ else if(source->max_listeners != -1 && source->listeners >= source->max_listeners) { global_unlock(); avl_tree_unlock(global.source_tree); client_send_404(client, "Too many clients on this mountpoint. Try again later."); if (uri != passed_uri) free (uri); return; } global.clients++; global_unlock(); source->format->create_client_data (source, client); source->format->client_send_headers(source->format, source, client); bytes = sock_write(client->con->sock, "\r\n"); if(bytes > 0) client->con->sent_bytes += bytes; sock_set_blocking(client->con->sock, SOCK_NONBLOCK); sock_set_nodelay(client->con->sock); avl_tree_wlock(source->pending_tree); avl_insert(source->pending_tree, (void *)client); avl_tree_unlock(source->pending_tree); } avl_tree_unlock(global.source_tree); if (!source) { DEBUG0("Source not found for client"); client_send_404(client, "The source you requested could not be found."); } if (uri != passed_uri) free (uri); }