Refactor, factor out getaddrinfo() code duplication to a sep. func.

Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
This commit is contained in:
Joachim Nilsson 2019-11-06 13:35:06 +01:00
parent 0fd87623d5
commit 31d7298516

View File

@ -605,21 +605,28 @@ err:
return -1; return -1;
} }
static int *create_inet_sockets(void) static int nslookup(char *host, char *service, struct addrinfo **ai)
{ {
struct addrinfo hints, *res, *r; struct addrinfo hints;
int error, maxs, *s, *socks;
int on = 1, sockflags;
memset(&hints, 0, sizeof(hints)); memset(&hints, 0, sizeof(hints));
hints.ai_flags = AI_PASSIVE; hints.ai_flags = !host ? AI_PASSIVE : 0;
hints.ai_family = family; hints.ai_family = family;
hints.ai_socktype = SOCK_DGRAM; hints.ai_socktype = SOCK_DGRAM;
error = getaddrinfo(NULL, service, &hints, &res);
if (error) { return getaddrinfo(host, service, &hints, ai);
}
static int *create_inet_sockets(void)
{
struct addrinfo *res, *r;
int err, maxs, *s, *socks;
int on = 1, sockflags;
err = nslookup(NULL, service, &res);
if (err) {
flog(LOG_SYSLOG | LOG_ERR, "network logging disabled (%s/udp " flog(LOG_SYSLOG | LOG_ERR, "network logging disabled (%s/udp "
" service unknown): %s", service, gai_strerror(error)); " service unknown): %s", service, gai_strerror(err));
logerror("see syslogd(8) for details of whether and how to enable it.");
return NULL; return NULL;
} }
@ -1538,7 +1545,7 @@ static int fmt5424(struct buf_msg *buffer, char *fmt, struct iovec *iov, size_t
void fprintlog(struct filed *f, struct buf_msg *buffer) void fprintlog(struct filed *f, struct buf_msg *buffer)
{ {
struct addrinfo hints, *ai; struct addrinfo *ai;
struct buf_msg repeat; struct buf_msg repeat;
struct logtime zero = { 0 }; struct logtime zero = { 0 };
struct iovec iov[20]; struct iovec iov[20];
@ -1626,23 +1633,25 @@ void fprintlog(struct filed *f, struct buf_msg *buffer)
logit(" %s\n", f->f_un.f_forw.f_hname); logit(" %s\n", f->f_un.f_forw.f_hname);
fwd_suspend = time(NULL) - f->f_time; fwd_suspend = time(NULL) - f->f_time;
if (fwd_suspend >= INET_SUSPEND_TIME) { if (fwd_suspend >= INET_SUSPEND_TIME) {
char *host; char *host = f->f_un.f_forw.f_hname;;
logit("Forwarding suspension to unknown over, retrying\n"); logit("Forwarding suspension to %s over, retrying\n", host);
memset(&hints, 0, sizeof(hints)); err = nslookup(host, service, &ai);
hints.ai_family = family;
hints.ai_socktype = SOCK_DGRAM;
host = f->f_un.f_forw.f_hname;
err = getaddrinfo(host, service, &hints, &ai);
if (err) { if (err) {
logit("Failure resolving %s:%s: %s\n", host, service, gai_strerror(err)); logit("Failure resolving %s:%s: %s\n", host, service, gai_strerror(err));
logit("Retries: %d\n", f->f_prevcount); logit("Retries: %d\n", f->f_prevcount);
if (--f->f_prevcount < 0) { if (--f->f_prevcount < 0) {
flog(LOG_SYSLOG | LOG_WARNING, "Still cannot find %s, "
"giving up: %s", host, gai_strerror(err));
logit("Giving up.\n"); logit("Giving up.\n");
f->f_type = F_UNUSED; f->f_type = F_UNUSED;
} else
logit("Left retries: %d\n", f->f_prevcount);
} else { } else {
flog(LOG_SYSLOG | LOG_WARNING, "Still cannot find %s, "
"will try again later: %s", host, gai_strerror(err));
logit("Left retries: %d\n", f->f_prevcount);
}
} else {
flog(LOG_SYSLOG | LOG_NOTICE, "Found %s, resuming operation.", host);
logit("%s found, resuming.\n", host); logit("%s found, resuming.\n", host);
f->f_un.f_forw.f_addr = ai; f->f_un.f_forw.f_addr = ai;
f->f_prevcount = 0; f->f_prevcount = 0;
@ -2436,14 +2445,15 @@ static void cfopts(char *ptr, struct filed *f)
*/ */
static struct filed *cfline(char *line) static struct filed *cfline(char *line)
{ {
struct addrinfo hints, *ai; struct addrinfo *ai;
struct filed *f; struct filed *f;
char buf[MAXLINE]; char buf[MAXLINE];
char xbuf[MAXLINE + 24]; char xbuf[MAXLINE + 24];
char *p, *q, *bp; char *p, *q, *bp;
int ignorepri = 0; int ignorepri = 0;
int singlpri = 0; int singlpri = 0;
int syncfile, pri, i, i2; int syncfile, pri;
int err, i, i2;
logit("cfline(%s)\n", line); logit("cfline(%s)\n", line);
@ -2593,15 +2603,15 @@ static struct filed *cfline(char *line)
strlcpy(f->f_un.f_forw.f_hname, ++p, sizeof(f->f_un.f_forw.f_hname)); strlcpy(f->f_un.f_forw.f_hname, ++p, sizeof(f->f_un.f_forw.f_hname));
logit("forwarding host: '%s'\n", p); /*ASP*/ logit("forwarding host: '%s'\n", p); /*ASP*/
memset(&hints, 0, sizeof(hints));
hints.ai_family = family; err = nslookup(p, service, &ai);
hints.ai_socktype = SOCK_DGRAM; if (err) {
if (getaddrinfo(p, service, &hints, &ai)) { flog(LOG_SYSLOG | LOG_WARNING, "Cannot find %s, "
"will try again later: %s", p, gai_strerror(err));
/* /*
* The host might be unknown due to an * The host might be unknown due to an inaccessible
* inaccessible nameserver (perhaps on the * nameserver (perhaps on the same host). We try to
* same host). We try to get the ip number * get the ip number later, like FORW_SUSP.
* later, like FORW_SUSP.
*/ */
f->f_type = F_FORW_UNKN; f->f_type = F_FORW_UNKN;
f->f_prevcount = INET_RETRY_MAX; f->f_prevcount = INET_RETRY_MAX;