/* return 0 if the passed ip address is not to be handled by icecast, non-zero otherwise */ static int accept_ip_address (char *ip) { void *result; recheck_ip_file (&banned_ip); recheck_ip_file (&allowed_ip); if (banned_ip.contents) { if (avl_get_by_key (banned_ip.contents, ip, &result) == 0) { DEBUG1 ("%s is banned", ip); return 0; } } if (allowed_ip.contents) { if (avl_get_by_key (allowed_ip.contents, ip, &result) == 0) { DEBUG1 ("%s is allowed", ip); return 1; } else { DEBUG1 ("%s is not allowed", ip); return 0; } } return 1; }
void *my_calloc (const char *file, int line, size_t num, size_t size) { alloc_node match, *result; snprintf (match.name, sizeof (match.name), "%s:%d", file, line); avl_tree_wlock (global.alloc_tree); if (avl_get_by_key (global.alloc_tree, &match, (void**)&result) == 0) { allocheader *block = calloc (1, (num*size)+sizeof(allocheader)); result->count++; result->allocated += (num*size); block->info = result; block->len = num*size; avl_tree_unlock (global.alloc_tree); return block+1; } result = calloc (1, sizeof(alloc_node)); if (result) { allocheader *block = calloc (1, (num*size)+sizeof(allocheader)); snprintf (result->name, sizeof (result->name), "%s:%d", file, line); result->count = 1; result->allocated = (num * size); avl_insert (global.alloc_tree, result); block->info = result; block->len = num*size; avl_tree_unlock (global.alloc_tree); return block+1; } avl_tree_unlock (global.alloc_tree); return NULL; }
static int search_cached_pattern (cache_file_contents *cache, const char *line) { int ret = -1; do { recheck_cached_file (cache, cachefile_timecheck); if (cache->wildcards) { struct list_node *entry = cache->wildcards; while (entry) { if (compare_pattern (line, entry->content) == 0) { DEBUG1 ("%s matched pattern", line); return 1; } entry = entry->next; } ret = 0; } if (allowed_ip.contents) { void *result; if (avl_get_by_key (allowed_ip.contents, (char*)line, &result) == 0) return 1; return 0; } } while (0); return ret; }
static fh_node *find_fh (fbinfo *finfo) { char *s = finfo->mount; fh_node fh, *result = NULL; if (finfo->mount == NULL) { ERROR0 ("missing name"); return NULL; } memcpy (&fh.finfo, finfo, sizeof (fbinfo)); if (strncmp (s, "fallback-", 9) == 0) { fh.finfo.flags |= FS_FALLBACK; fh.finfo.mount = s+9; } else if (strncmp (s, "file-", 5) == 0) fh.finfo.mount = s+5; if (avl_get_by_key (fh_cache, &fh, (void**)&result) == 0) { DEBUG2 ("mount %s (%d)", finfo->mount, finfo->flags); return result; } DEBUG2 ("%s (%d) not found in cache", finfo->mount, finfo->flags); return NULL; }
static auth_result htpasswd_auth (auth_client *auth_user) { auth_t *auth = auth_user->client->auth; htpasswd_auth_state *htpasswd = auth->state; client_t *client = auth_user->client; htpasswd_user entry; void *result; if (client->username == NULL || client->password == NULL) return AUTH_FAILED; htpasswd_recheckfile (htpasswd); thread_rwlock_rlock (&htpasswd->file_rwlock); entry.name = client->username; if (avl_get_by_key (htpasswd->users, &entry, &result) == 0) { htpasswd_user *found = result; char *hashed_pw; thread_rwlock_unlock (&htpasswd->file_rwlock); hashed_pw = get_hash (client->password, strlen (client->password)); if (strcmp (found->pass, hashed_pw) == 0) { free (hashed_pw); return AUTH_OK; } free (hashed_pw); DEBUG0 ("incorrect password for client"); return AUTH_FAILED; } DEBUG1 ("no such username: %s", client->username); thread_rwlock_unlock (&htpasswd->file_rwlock); return AUTH_FAILED; }
static char *fserve_content_type(char *path) { char *ext = util_get_extension(path); mime_type exttype = {ext, NULL}; void *result; if (!avl_get_by_key (mimetypes, &exttype, &result)) { mime_type *mime = result; return mime->type; } else { /* Fallbacks for a few basic ones */ if(!strcmp(ext, "ogg")) return "application/ogg"; else if(!strcmp(ext, "mp3")) return "audio/mpeg"; else if(!strcmp(ext, "html")) return "text/html"; else if(!strcmp(ext, "css")) return "text/css"; else if(!strcmp(ext, "txt")) return "text/plain"; else return "application/octet-stream"; } }
static void create_mime_mappings(char *fn) { FILE *mimefile = fopen(fn, "r"); char line[4096]; char *type, *ext, *cur; mime_type *mapping; mimetypes = avl_tree_new(_compare_mappings, NULL); if(!mimefile) return; while(fgets(line, 4096, mimefile)) { line[4095] = 0; if(*line == 0 || *line == '#') continue; type = line; cur = line; while(*cur != ' ' && *cur != '\t' && *cur) cur++; if(*cur == 0) continue; *cur++ = 0; while(1) { while(*cur == ' ' || *cur == '\t') cur++; if(*cur == 0) break; ext = cur; while(*cur != ' ' && *cur != '\t' && *cur != '\n' && *cur) cur++; *cur++ = 0; if(*ext) { void *tmp; /* Add a new extension->type mapping */ mapping = malloc(sizeof(mime_type)); mapping->ext = strdup(ext); mapping->type = strdup(type); if(!avl_get_by_key(mimetypes, mapping, &tmp)) avl_delete(mimetypes, mapping, _delete_mapping); avl_insert(mimetypes, mapping); } } } fclose(mimefile); }
struct name_data *avl_get_data(gint index) { struct name_data data; void *data1; data.index = index; if (avl_get_by_key(competitors_tree, &data, &data1) == 0) { return data1; } //g_print("cannot find name index %d!\n", index); //assert(0); return NULL; }
const char *httpp_get_query_param(http_parser_t *parser, const char *name) { http_var_t var; http_var_t *found; void *fp; fp = &found; var.name = (char *)name; var.value = NULL; if (avl_get_by_key(parser->queryvars, (void *)&var, fp) == 0) return found->value; else return NULL; }
char *httpp_getvar(http_parser_t *parser, char *name) { http_var_t var; http_var_t *found; void *fp; fp = &found; var.name = name; var.value = NULL; if (avl_get_by_key(parser->vars, &var, fp) == 0) return found->value; else return NULL; }
void insert(const cm_data &d) { case_mapping *p; #if 0 /* Check for priority intervals */ if ((d.first <= 'T' && d.last >= 'T') || (d.first <= 0x0410 && d.last >= 0x0410)) { /* Interval contains ASCII or Cyrillic letters */ m_prio.push_back(new cm_data(d)); } #endif if (avl_get_by_key(m_tree, (void*)&d, (void**)&p) != 0) { p = get_mapping(d.first, d.last, d.cm); avl_insert(m_tree, p); } }
/* string returned needs to be free'd */ char *fserve_content_type (const char *path) { char *ext = util_get_extension(path); mime_type exttype = { NULL, NULL }; void *result; char *type; if (ext == NULL) return strdup ("text/html"); exttype.ext = strdup (ext); thread_spin_lock (&pending_lock); if (mimetypes && !avl_get_by_key (mimetypes, &exttype, &result)) { mime_type *mime = result; free (exttype.ext); type = strdup (mime->type); } else { free (exttype.ext); /* Fallbacks for a few basic ones */ if(!strcmp(ext, "ogg")) type = strdup ("application/ogg"); else if(!strcmp(ext, "mp3")) type = strdup ("audio/mpeg"); else if(!strcmp(ext, "html")) type = strdup ("text/html"); else if(!strcmp(ext, "css")) type = strdup ("text/css"); else if(!strcmp(ext, "txt")) type = strdup ("text/plain"); else if(!strcmp(ext, "jpg")) type = strdup ("image/jpeg"); else if(!strcmp(ext, "png")) type = strdup ("image/png"); else if(!strcmp(ext, "m3u")) type = strdup ("audio/x-mpegurl"); else if(!strcmp(ext, "aac")) type = strdup ("audio/aac"); else type = strdup ("application/octet-stream"); } thread_spin_unlock (&pending_lock); return type; }
client_t *source_find_client(source_t *source, int id) { client_t fakeclient; void *result; connection_t fakecon; fakeclient.con = &fakecon; fakeclient.con->id = id; avl_tree_rlock(source->client_tree); if(avl_get_by_key(source->client_tree, &fakeclient, &result) == 0) { avl_tree_unlock(source->client_tree); return result; } avl_tree_unlock(source->client_tree); return NULL; }
void avl_set_data(gint index, gchar *first, gchar *last, gchar *club) { struct name_data *data; void *data1; data = g_malloc(sizeof(*data)); memset(data, 0, sizeof(*data)); data->index = index; data->last = g_strdup(last); data->first = g_strdup(first); data->club = g_strdup(club); if (avl_get_by_key(competitors_tree, data, &data1) == 0) { /* data exists */ avl_delete(competitors_tree, data, free_avl_key); } avl_insert(competitors_tree, data); }
static auth_result htpasswd_adduser (auth_t *auth, const char *username, const char *password) { FILE *passwdfile; char *hashed_password = NULL; htpasswd_auth_state *state = auth->state; htpasswd_user entry; void *result; htpasswd_recheckfile (state); thread_rwlock_wlock (&state->file_rwlock); entry.name = (char*)username; if (avl_get_by_key (state->users, &entry, &result) == 0) { thread_rwlock_unlock (&state->file_rwlock); return AUTH_USEREXISTS; } passwdfile = fopen(state->filename, "ab"); if (passwdfile == NULL) { thread_rwlock_unlock (&state->file_rwlock); WARN2("Failed to open authentication database \"%s\": %s", state->filename, strerror(errno)); return AUTH_FAILED; } hashed_password = get_hash(password, strlen(password)); if (hashed_password) { fprintf(passwdfile, "%s:%s\n", username, hashed_password); free(hashed_password); } fclose(passwdfile); thread_rwlock_unlock (&state->file_rwlock); return AUTH_USERADDED; }
/* check specified ip against internal set of banned IPs * return -1 for no data, 0 for no match and 1 for match */ static int search_banned_ip (char *ip) { recheck_cached_file (&banned_ip, cachefile_timecheck); if (banned_ip.wildcards) { struct list_node *entry = banned_ip.wildcards; while (entry) { if (compare_pattern (ip, entry->content) == 0) return 1; entry = entry->next; } } if (banned_ip.contents) { void *result; ban_entry_removal = NULL; if (avl_get_by_key (banned_ip.contents, ip, &result) == 0) { struct banned_entry *match = result; if (match->a.timeout == 0 || match->a.timeout > cachefile_timecheck) { if (match->a.timeout && cachefile_timecheck + 300 > match->a.timeout) match->a.timeout = cachefile_timecheck + 300; return 1; } avl_delete (banned_ip.contents, ip, cache_treenode_free); } /* we may of seen another one to remove */ if (ban_entry_removal) { INFO1 ("removing %s from ban list for now", &ban_entry_removal->ip[0]); avl_delete (banned_ip.contents, &ban_entry_removal->ip[0], cache_treenode_free); ban_entry_removal = NULL; } } return 0; }
/* string returned needs to be free'd */ char *fserve_content_type (const char *path) { char *ext = util_get_extension(path); mime_type exttype = { NULL, NULL }; void *result; char *type; if (ext == NULL) return strdup ("text/html"); exttype.ext = strdup (ext); thread_spin_lock (&pending_lock); if (mimetypes && !avl_get_by_key (mimetypes, &exttype, &result)) { mime_type *mime = result; type = strdup (mime->type); } else type = strdup ("application/octet-stream"); thread_spin_unlock (&pending_lock); free (exttype.ext); return type; }
/* find/create handle and return it with the structure in a locked state */ static fh_node *open_fh (fbinfo *finfo) { fh_node *fh, *result; if (finfo->mount == NULL) finfo->mount = ""; fh = calloc (1, sizeof (fh_node)); memcpy (&fh->finfo, finfo, sizeof (fbinfo)); if (avl_get_by_key (fh_cache, fh, (void**)&result) == 0) { free (fh); thread_mutex_lock (&result->lock); avl_tree_unlock (fh_cache); if (finfo->flags & FS_FALLBACK) { if (result->finfo.type != finfo->type && finfo->type != FORMAT_TYPE_UNDEFINED) { WARN1 ("format mismatched for %s", finfo->mount); thread_mutex_unlock (&result->lock); return NULL; } result->expire = (time_t)-1; } return result; } // insert new one if (fh->finfo.mount[0]) { char *fullpath= util_get_path_from_normalised_uri (fh->finfo.mount, fh->finfo.flags&FS_USE_ADMIN); char *contenttype = fserve_content_type (fullpath); format_type_t type = format_get_type (contenttype); if (fh->finfo.type == FORMAT_TYPE_UNDEFINED) fh->finfo.type = type; if (finfo->flags & FS_FALLBACK) { if (fh->finfo.type != type && type != FORMAT_TYPE_UNDEFINED && fh->finfo.type != FORMAT_TYPE_UNDEFINED) { avl_tree_unlock (fh_cache); free (contenttype); free (fullpath); free (fh); WARN1 ("format mismatched for %s", finfo->mount); return NULL; } fh->expire = (time_t)-1; INFO2 ("lookup of fallback file \"%s\" (%d)", finfo->mount, finfo->limit); } else INFO1 ("lookup of \"%s\"", finfo->mount); if (file_open (&fh->f, fullpath) < 0) { INFO1 ("Failed to open \"%s\"", fullpath); avl_tree_unlock (fh_cache); free (contenttype); free (fullpath); free (fh); return NULL; } free (fullpath); fh->format = calloc (1, sizeof (format_plugin_t)); fh->format->type = fh->finfo.type; fh->format->contenttype = strdup (contenttype); free (contenttype); if (fh->finfo.type != FORMAT_TYPE_UNDEFINED) { fh->format->mount = strdup (fh->finfo.mount); if (format_get_plugin (fh->format) < 0) { avl_tree_unlock (fh_cache); file_close (&fh->f); free (fh->format); free (fh); return NULL; } } if (fh->finfo.limit) fh->out_bitrate = rate_setup (10000, 1000); } fh->clients = avl_tree_new (client_compare, NULL); thread_mutex_create (&fh->lock); thread_mutex_lock (&fh->lock); avl_insert (fh_cache, fh); avl_tree_unlock (fh_cache); fh->refcount = 0; fh->peak = 0; fh->finfo.mount = strdup (finfo->mount); fh->finfo.fallback = NULL; return fh; }
void fserve_recheck_mime_types (ice_config_t *config) { mime_type *mapping; int i; avl_tree *old_mimetypes = NULL, *new_mimetypes = avl_tree_new(_compare_mappings, NULL); mime_type defaults[] = { { "m3u", "audio/x-mpegurl" }, { "pls", "audio/x-scpls" }, { "xspf", "application/xspf+xml" }, { "ogg", "application/ogg" }, { "mp3", "audio/mpeg" }, { "aac", "audio/aac" }, { "aacp", "audio/aacp" }, { "css", "text/css" }, { "txt", "text/plain" }, { "html", "text/html" }, { "jpg", "image/jpg" }, { "png", "image/png" }, { "gif", "image/gif" }, { NULL, NULL } }; for (i=0; defaults[i].ext; i++) { mapping = malloc (sizeof(mime_type)); mapping->ext = strdup (defaults [i].ext); mapping->type = strdup (defaults [i].type); if (avl_insert (new_mimetypes, mapping) != 0) _delete_mapping (mapping); } do { char *type, *ext, *cur; FILE *mimefile = NULL; char line[4096]; if (config->mimetypes_fn == NULL) { INFO0 ("no mime types file defined, using defaults"); break; } mimefile = fopen (config->mimetypes_fn, "r"); if (mimefile == NULL) { WARN1 ("Cannot open mime types file %s, using defaults", config->mimetypes_fn); break; } while (fgets(line, sizeof line, mimefile)) { line[4095] = 0; if(*line == 0 || *line == '#') continue; type = line; cur = line; while(*cur != ' ' && *cur != '\t' && *cur) cur++; if(*cur == 0) continue; *cur++ = 0; while(1) { while(*cur == ' ' || *cur == '\t') cur++; if(*cur == 0) break; ext = cur; while(*cur != ' ' && *cur != '\t' && *cur != '\n' && *cur) cur++; *cur++ = 0; if(*ext) { void *tmp; /* Add a new extension->type mapping */ mapping = malloc(sizeof(mime_type)); mapping->ext = strdup(ext); mapping->type = strdup(type); if (!avl_get_by_key (new_mimetypes, mapping, &tmp)) avl_delete (new_mimetypes, mapping, _delete_mapping); if (avl_insert (new_mimetypes, mapping) != 0) _delete_mapping (mapping); } } } fclose(mimefile); } while (0); thread_spin_lock (&pending_lock); old_mimetypes = mimetypes; mimetypes = new_mimetypes; thread_spin_unlock (&pending_lock); if (old_mimetypes) avl_tree_free (old_mimetypes, _delete_mapping); }
void fserve_recheck_mime_types(ice_config_t *config) { FILE *mimefile; char line[4096]; char *type, *ext, *cur; mime_type *mapping; avl_tree *new_mimetypes; if (config->mimetypes_fn == NULL) return; mimefile = fopen (config->mimetypes_fn, "r"); if (mimefile == NULL) { ICECAST_LOG_WARN("Cannot open mime types file %s", config->mimetypes_fn); return; } new_mimetypes = avl_tree_new(_compare_mappings, NULL); while(fgets(line, 4096, mimefile)) { line[4095] = 0; if(*line == 0 || *line == '#') continue; type = line; cur = line; while(*cur != ' ' && *cur != '\t' && *cur) cur++; if(*cur == 0) continue; *cur++ = 0; while(1) { while(*cur == ' ' || *cur == '\t') cur++; if(*cur == 0) break; ext = cur; while(*cur != ' ' && *cur != '\t' && *cur != '\n' && *cur) cur++; *cur++ = 0; if(*ext) { void *tmp; /* Add a new extension->type mapping */ mapping = malloc(sizeof(mime_type)); mapping->ext = strdup(ext); mapping->type = strdup(type); if (!avl_get_by_key (new_mimetypes, mapping, &tmp)) avl_delete (new_mimetypes, mapping, _delete_mapping); avl_insert (new_mimetypes, mapping); } } } fclose(mimefile); thread_spin_lock (&pending_lock); if (mimetypes) avl_tree_free (mimetypes, _delete_mapping); mimetypes = new_mimetypes; thread_spin_unlock (&pending_lock); }