From 299b223bba5b68fd9191058bfb94e60f698b3157 Mon Sep 17 00:00:00 2001 From: thorkill Date: Wed, 25 May 2016 18:16:45 +0200 Subject: [PATCH] Added new function config_address2addrinfo This function takes an "Address" config string, splits it into address and port, then calls str2addinfo() on it returning addrinfo. The aim is to refactor the code an allow simple translation of addresses listed in configuration or generated on demand in other places in the code. --- src/netutl.c | 19 +++++++++++++++++++ src/netutl.h | 1 + 2 files changed, 20 insertions(+) diff --git a/src/netutl.c b/src/netutl.c index fab3fc78..43409197 100644 --- a/src/netutl.c +++ b/src/netutl.c @@ -54,6 +54,25 @@ struct addrinfo *str2addrinfo(const char *address, const char *service, int sock return ai; } +/* + Turn a configuration Address string into addrinfo. +*/ +struct addrinfo *config_address2addrinfo(char *address, int socktype) { + struct addrinfo *ai; + char *space, *port; + space = strchr(address, ' '); + if(space) { + port = xstrdup(space + 1); + *space = 0; + } else + port = xstrdup("655"); + + ai = str2addrinfo(address, port, socktype); + free(port); + + return ai; +} + sockaddr_t str2sockaddr(const char *address, const char *port) { struct addrinfo *ai, hint; sockaddr_t result; diff --git a/src/netutl.h b/src/netutl.h index 471cae7f..5b0166a8 100644 --- a/src/netutl.h +++ b/src/netutl.h @@ -26,6 +26,7 @@ extern bool hostnames; extern struct addrinfo *str2addrinfo(const char *, const char *, int) __attribute__ ((__malloc__)); +extern struct addrinfo *config_address2addrinfo(char *, int); extern sockaddr_t str2sockaddr(const char *, const char *); extern void sockaddr2str(const sockaddr_t *, char **, char **); extern char *sockaddr2hostname(const sockaddr_t *) __attribute__ ((__malloc__));