+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 */