--- embedaddon/miniupnpd/options.c 2012/02/21 23:16:02 1.1.1.1 +++ embedaddon/miniupnpd/options.c 2013/07/22 00:32:35 1.1.1.2 @@ -1,8 +1,8 @@ -/* $Id: options.c,v 1.1.1.1 2012/02/21 23:16:02 misho Exp $ */ +/* $Id: options.c,v 1.1.1.2 2013/07/22 00:32:35 misho Exp $ */ /* MiniUPnP project * http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/ * author: Ryan Wagoner - * (c) 2006 Thomas Bernard + * (c) 2006-2012 Thomas Bernard * This software is subject to the conditions detailed * in the LICENCE file provided within the distribution */ @@ -11,12 +11,15 @@ #include #include #include +#include "config.h" #include "options.h" #include "upnppermissions.h" #include "upnpglobalvars.h" +#ifndef DISABLE_CONFIG_FILE struct option * ary_options = NULL; -int num_options = 0; +static char * string_repo = NULL; +unsigned int num_options = 0; static const struct { enum upnpconfigoptions id; @@ -29,6 +32,7 @@ static const struct { { UPNPBITRATE_UP, "bitrate_up" }, { UPNPBITRATE_DOWN, "bitrate_down" }, { UPNPPRESENTATIONURL, "presentation_url" }, + { UPNPFRIENDLY_NAME, "friendly_name" }, { UPNPNOTIFY_INTERVAL, "notify_interval" }, { UPNPSYSTEM_UPTIME, "system_uptime" }, { UPNPPACKET_LOG, "packet_log" }, @@ -46,6 +50,7 @@ static const struct { #endif { UPNPENABLE, "enable_upnp"}, #ifdef USE_PF + { UPNPANCHOR, "anchor"}, { UPNPQUEUE, "queue"}, { UPNPTAG, "tag"}, #endif @@ -69,8 +74,11 @@ readoptionsfile(const char * fname) char *value; char *t; int linenum = 0; - int i; + unsigned int i; enum upnpconfigoptions id; + size_t string_repo_len = 0; + size_t len; + void *tmp; if(!fname || (strlen(fname) == 0)) return -1; @@ -93,18 +101,19 @@ readoptionsfile(const char * fname) while(fgets(buffer, sizeof(buffer), hfile)) { linenum++; - t = strchr(buffer, '\n'); + t = strchr(buffer, '\n'); if(t) { *t = '\0'; t--; + /* remove spaces at the end of the line */ while((t >= buffer) && isspace(*t)) { *t = '\0'; t--; } } - + /* skip leading whitespaces */ name = buffer; while(isspace(*name)) @@ -116,17 +125,25 @@ readoptionsfile(const char * fname) /* check for UPnP permissions rule */ if(0 == memcmp(name, "allow", 5) || 0 == memcmp(name, "deny", 4)) { - upnppermlist = realloc(upnppermlist, - sizeof(struct upnpperm) * (num_upnpperm+1)); - /* parse the rule */ - if(read_permission_line(upnppermlist + num_upnpperm, name) >= 0) + tmp = realloc(upnppermlist, sizeof(struct upnpperm) * (num_upnpperm+1)); + if(tmp == NULL) { - num_upnpperm++; + fprintf(stderr, "memory allocation error. Permission line in file %s line %d\n", + fname, linenum); } else { - fprintf(stderr, "parsing error file %s line %d : %s\n", - fname, linenum, name); + upnppermlist = tmp; + /* parse the rule */ + if(read_permission_line(upnppermlist + num_upnpperm, name) >= 0) + { + num_upnpperm++; + } + else + { + fprintf(stderr, "parsing error file %s line %d : %s\n", + fname, linenum, name); + } } continue; } @@ -163,22 +180,51 @@ readoptionsfile(const char * fname) if(id == UPNP_INVALID) { - fprintf(stderr, "parsing error file %s line %d : %s=%s\n", + fprintf(stderr, "invalid option in file %s line %d : %s=%s\n", fname, linenum, name, value); } else { - num_options += 1; - ary_options = (struct option *) realloc(ary_options, num_options * sizeof(struct option)); - - ary_options[num_options-1].id = id; - strncpy(ary_options[num_options-1].value, value, MAX_OPTION_VALUE_LEN); + tmp = realloc(ary_options, (num_options + 1) * sizeof(struct option)); + if(tmp == NULL) + { + fprintf(stderr, "memory allocation error. Option in file %s line %d.\n", + fname, linenum); + } + else + { + ary_options = tmp; + len = strlen(value) + 1; /* +1 for terminating '\0' */ + tmp = realloc(string_repo, string_repo_len + len); + if(tmp == NULL) + { + fprintf(stderr, "memory allocation error, Option value in file %s line %d : %s=%s\n", + fname, linenum, name, value); + } + else + { + string_repo = tmp; + memcpy(string_repo + string_repo_len, value, len); + ary_options[num_options].id = id; + /* save the offset instead of the absolute address because realloc() could + * change it */ + ary_options[num_options].value = (const char *)string_repo_len; + num_options += 1; + string_repo_len += len; + } + } } } - + fclose(hfile); - + + for(i = 0; i < num_options; i++) + { + /* add start address of string_repo to get right pointer */ + ary_options[i].value = string_repo + (size_t)ary_options[i].value; + } + return 0; } @@ -191,5 +237,18 @@ freeoptions(void) ary_options = NULL; num_options = 0; } + if(string_repo) + { + free(string_repo); + string_repo = NULL; + } + if(upnppermlist) + { + free(upnppermlist); + upnppermlist = NULL; + num_upnpperm = 0; + } } + +#endif /* DISABLE_CONFIG_FILE */