From b90cd8d221d55975cc357db0d6c0b95b781107dd Mon Sep 17 00:00:00 2001 From: reyk Date: Fri, 18 Jul 2008 12:30:06 +0000 Subject: [PATCH] merge log_host with relayd's version using getnameinfo and rename it to print_host. --- usr.sbin/snmpd/log.c | 20 +++++++++----------- usr.sbin/snmpd/snmpd.h | 4 ++-- usr.sbin/snmpd/snmpe.c | 8 ++++---- 3 files changed, 15 insertions(+), 17 deletions(-) diff --git a/usr.sbin/snmpd/log.c b/usr.sbin/snmpd/log.c index f1d773c35f0..7603cb5cfef 100644 --- a/usr.sbin/snmpd/log.c +++ b/usr.sbin/snmpd/log.c @@ -1,4 +1,4 @@ -/* $OpenBSD: log.c,v 1.1 2007/12/05 09:22:44 reyk Exp $ */ +/* $OpenBSD: log.c,v 1.2 2008/07/18 12:30:06 reyk Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer @@ -36,6 +36,7 @@ #include #include #include +#include #include @@ -168,15 +169,12 @@ fatalx(const char *emsg) } const char * -log_host(struct sockaddr_storage *ss, char *buf, size_t len) +print_host(struct sockaddr_storage *ss, char *buf, size_t len) { - int af = ss->ss_family; - void *ptr; - - bzero(buf, len); - if (af == AF_INET) - ptr = &((struct sockaddr_in *)ss)->sin_addr; - else - ptr = &((struct sockaddr_in6 *)ss)->sin6_addr; - return (inet_ntop(af, ptr, buf, len)); + if (getnameinfo((struct sockaddr *)ss, ss->ss_len, + buf, len, NULL, 0, NI_NUMERICHOST) != 0) { + buf[0] = '\0'; + return (NULL); + } + return (buf); } diff --git a/usr.sbin/snmpd/snmpd.h b/usr.sbin/snmpd/snmpd.h index 3a0fe23babe..16969ebf1dd 100644 --- a/usr.sbin/snmpd/snmpd.h +++ b/usr.sbin/snmpd/snmpd.h @@ -1,4 +1,4 @@ -/* $OpenBSD: snmpd.h,v 1.19 2008/06/29 16:00:22 ragge Exp $ */ +/* $OpenBSD: snmpd.h,v 1.20 2008/07/18 12:30:06 reyk Exp $ */ /* * Copyright (c) 2007, 2008 Reyk Floeter @@ -353,7 +353,7 @@ void log_info(const char *, ...); void log_debug(const char *, ...); __dead void fatal(const char *); __dead void fatalx(const char *); -const char *log_host(struct sockaddr_storage *, char *, size_t); +const char *print_host(struct sockaddr_storage *, char *, size_t); /* buffer.c */ struct buf *buf_open(size_t); diff --git a/usr.sbin/snmpd/snmpe.c b/usr.sbin/snmpd/snmpe.c index 3c0d871754e..da02d59e7c3 100644 --- a/usr.sbin/snmpd/snmpe.c +++ b/usr.sbin/snmpd/snmpe.c @@ -1,4 +1,4 @@ -/* $OpenBSD: snmpe.c,v 1.18 2008/03/12 14:11:52 reyk Exp $ */ +/* $OpenBSD: snmpe.c,v 1.19 2008/07/18 12:30:06 reyk Exp $ */ /* * Copyright (c) 2007, 2008 Reyk Floeter @@ -234,7 +234,7 @@ snmpe_bind(struct address *addr) if (bind(s, (struct sockaddr *)&addr->ss, addr->ss.ss_len) == -1) goto bad; - if (log_host(&addr->ss, buf, sizeof(buf)) == NULL) + if (print_host(&addr->ss, buf, sizeof(buf)) == NULL) goto bad; log_info("snmpe_bind: binding to address %s:%d", buf, addr->port); @@ -582,7 +582,7 @@ snmpe_parse(struct sockaddr_storage *ss, msg->sm_error = errval; msg->sm_errorindex = erridx; - log_host(ss, host, sizeof(host)); + print_host(ss, host, sizeof(host)); log_debug("snmpe_parse: %s: SNMPv%d '%s' context %d request %lld", host, msg->sm_version + 1, msg->sm_community, msg->sm_context, msg->sm_request); @@ -691,7 +691,7 @@ snmpe_parse(struct sockaddr_storage *ss, parsefail: stats->snmp_inasnparseerrs++; fail: - log_host(ss, host, sizeof(host)); + print_host(ss, host, sizeof(host)); log_debug("snmpe_parse: %s: %s", host, errstr); return (-1); } -- 2.20.1