+int get_config_bool(config_t *cfg, int *result)
+{
+cp
+ if(!cfg)
+ return 0;
+
+ if(!strcasecmp(cfg->value, "yes"))
+ {
+ *result = 1;
+ return 1;
+ }
+ else if(!strcasecmp(cfg->value, "no"))
+ {
+ *result = 0;
+ return 1;
+ }
+
+ syslog(LOG_ERR, _("\"yes\" or \"no\" expected for configuration variable %s in %s line %d"),
+ cfg->variable, cfg->file, cfg->line);
+
+ return 0;
+}
+
+int get_config_int(config_t *cfg, int *result)
+{
+cp
+ if(!cfg)
+ return 0;
+
+ if(sscanf(cfg->value, "%d", result) == 1)
+ return 1;
+
+ syslog(LOG_ERR, _("Integer expected for configuration variable %s in %s line %d"),
+ cfg->variable, cfg->file, cfg->line);
+ return 0;
+}
+
+int get_config_string(config_t *cfg, char **result)
+{
+cp
+ if(!cfg)
+ return 0;
+
+ *result = xstrdup(cfg->value);
+ return 1;
+}
+
+int get_config_address(config_t *cfg, struct addrinfo **result)
+{
+ struct addrinfo *ai;
+cp
+ if(!cfg)
+ return 0;
+
+ ai = str2addrinfo(cfg->value, NULL, 0);
+
+ if(ai)
+ {
+ *result = ai;
+ return 1;
+ }
+
+ syslog(LOG_ERR, _("Hostname or IP address expected for configuration variable %s in %s line %d"),
+ cfg->variable, cfg->file, cfg->line);
+ return 0;
+}
+
+int get_config_port(config_t *cfg, port_t *result)
+{
+cp
+ if(!cfg)
+ return 0;
+
+ if(sscanf(cfg->value, "%hu", result) == 1)
+ {
+ *result = htons(*result);
+ return 1;
+ }
+
+ syslog(LOG_ERR, _("Port number expected for configuration variable %s in %s line %d"),
+ cfg->variable, cfg->file, cfg->line);
+ return 0;
+}
+
+int get_config_subnet(config_t *cfg, subnet_t **result)
+{
+ subnet_t *subnet;
+cp
+ if(!cfg)
+ return 0;
+
+ subnet = str2net(cfg->value);
+
+ if(!subnet)
+ {
+ syslog(LOG_ERR, _("Subnet expected for configuration variable %s in %s line %d"),
+ cfg->variable, cfg->file, cfg->line);
+ return 0;
+ }
+
+ /* Teach newbies what subnets are... */
+
+ if(((subnet->type == SUBNET_IPV4) && maskcheck(&subnet->net.ipv4.address, subnet->net.ipv4.prefixlength, sizeof(ipv4_t)))
+ || ((subnet->type == SUBNET_IPV6) && maskcheck(&subnet->net.ipv6.address, subnet->net.ipv6.prefixlength, sizeof(ipv6_t))))
+ {
+ syslog(LOG_ERR, _("Network address and prefix length do not match for configuration variable %s in %s line %d"),
+ cfg->variable, cfg->file, cfg->line);
+ free(subnet);
+ return 0;
+ }
+
+ *result = subnet;
+
+ return 1;
+}
+
+/*
+ Read exactly one line and strip the trailing newline if any. If the
+ file was on EOF, return NULL. Otherwise, return all the data in a
+ dynamically allocated buffer.
+
+ If line is non-NULL, it will be used as an initial buffer, to avoid
+ unnecessary mallocing each time this function is called. If buf is
+ given, and buf needs to be expanded, the var pointed to by buflen
+ will be increased.
+*/
+char *readline(FILE *fp, char **buf, size_t *buflen)
+{
+ char *newline = NULL;
+ char *p;
+ char *line; /* The array that contains everything that has been read
+ so far */
+ char *idx; /* Read into this pointer, which points to an offset
+ within line */
+ size_t size, newsize; /* The size of the current array pointed to by
+ line */
+ size_t maxlen; /* Maximum number of characters that may be read with
+ fgets. This is newsize - oldsize. */
+
+ if(feof(fp))
+ return NULL;
+
+ if((buf != NULL) && (buflen != NULL))
+ {
+ size = *buflen;
+ line = *buf;
+ }
+ else
+ {
+ size = 100;
+ line = xmalloc(size);
+ }
+
+ maxlen = size;
+ idx = line;
+ *idx = 0;
+ for(;;)
+ {
+ errno = 0;
+ p = fgets(idx, maxlen, fp);
+ if(p == NULL) /* EOF or error */
+ {
+ if(feof(fp))
+ break;
+
+ /* otherwise: error; let the calling function print an error
+ message if applicable */
+ free(line);
+ return NULL;
+ }
+
+ newline = strchr(p, '\n');
+ if(newline == NULL)
+ /* We haven't yet read everything to the end of the line */
+ {
+ newsize = size << 1;
+ line = xrealloc(line, newsize);
+ idx = &line[size - 1];
+ maxlen = newsize - size + 1;
+ size = newsize;
+ }
+ else
+ {
+ *newline = '\0'; /* kill newline */
+ break; /* yay */
+ }
+ }
+
+ if((buf != NULL) && (buflen != NULL))
+ {
+ *buflen = size;
+ *buf = line;
+ }
+ return line;
+}
+