summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlorian Obser <florian@cvs.openbsd.org>2014-03-18 10:11:01 +0000
committerFlorian Obser <florian@cvs.openbsd.org>2014-03-18 10:11:01 +0000
commit19d345e24d500e76992da177c7a663ee98850685 (patch)
tree60a2f5d55f252ed93c70f26345cf8562ac628245
parent19839346538235d48c540ab06facd6544656a2dd (diff)
Sync inetname from traceroute6 and thereby make it AF independent.
While there drop the !nflag check as it's never called with nflag set, simplify the code a bit and adapt style from traceroute. OK benno
-rw-r--r--usr.sbin/traceroute/traceroute.c43
-rw-r--r--usr.sbin/traceroute6/traceroute6.c29
2 files changed, 33 insertions, 39 deletions
diff --git a/usr.sbin/traceroute/traceroute.c b/usr.sbin/traceroute/traceroute.c
index 6c823f2fe48..34c49ec13e3 100644
--- a/usr.sbin/traceroute/traceroute.c
+++ b/usr.sbin/traceroute/traceroute.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: traceroute.c,v 1.92 2014/03/18 10:10:17 florian Exp $ */
+/* $OpenBSD: traceroute.c,v 1.93 2014/03/18 10:11:00 florian Exp $ */
/* $NetBSD: traceroute.c,v 1.10 1995/05/21 15:50:45 mycroft Exp $ */
/*-
@@ -262,7 +262,7 @@ int packet_ok(u_char *, int, struct sockaddr_in *, int, int);
void dump_packet(void);
void print_exthdr(u_char *, int);
void print(u_char *, int, struct sockaddr_in *);
-char *inetname(struct in_addr);
+const char *inetname(struct sockaddr*);
void print_asn(struct sockaddr_storage *);
u_short in_cksum(u_short *, int);
char *pr_type(u_int8_t);
@@ -1119,7 +1119,7 @@ print(u_char *buf, int cc, struct sockaddr_in *from)
if (nflag)
printf(" %s", inet_ntoa(from->sin_addr));
else
- printf(" %s (%s)", inetname(from->sin_addr),
+ printf(" %s (%s)", inetname((struct sockaddr*)from),
inet_ntoa(from->sin_addr));
if (Aflag)
print_asn((struct sockaddr_storage *)from);
@@ -1165,32 +1165,33 @@ in_cksum(u_short *addr, int len)
/*
* Construct an Internet address representation.
*/
-char *
-inetname(struct in_addr in)
+const char *
+inetname(struct sockaddr *sa)
{
- static char domain[MAXHOSTNAMELEN], line[MAXHOSTNAMELEN];
+ static char line[NI_MAXHOST], domain[MAXHOSTNAMELEN + 1];
static int first = 1;
- struct hostent *hp;
char *cp;
if (first) {
first = 0;
- if (gethostname(domain, sizeof domain) == 0 &&
- (cp = strchr(domain, '.')) != NULL) {
- strlcpy(domain, cp + 1, sizeof(domain));
- }
+ if (gethostname(domain, sizeof(domain)) == 0 &&
+ (cp = strchr(domain, '.')) != NULL)
+ (void) strlcpy(domain, cp + 1, sizeof(domain));
+ else
+ domain[0] = 0;
}
- if (in.s_addr != INADDR_ANY) {
- hp = gethostbyaddr((char *)&in, sizeof(in), AF_INET);
- if (hp != NULL) {
- if ((cp = strchr(hp->h_name, '.')) != NULL &&
- strcmp(cp + 1, domain) == 0)
- *cp = '\0';
- strlcpy(line, hp->h_name, sizeof(line));
- return (line);
- }
+ if (getnameinfo(sa, sa->sa_len, line, sizeof(line), NULL, 0,
+ NI_NAMEREQD) == 0) {
+ if ((cp = strchr(line, '.')) != NULL && strcmp(cp + 1,
+ domain) == 0)
+ *cp = '\0';
+ return (line);
}
- return (inet_ntoa(in));
+
+ if (getnameinfo(sa, sa->sa_len, line, sizeof(line), NULL, 0,
+ NI_NUMERICHOST) != 0)
+ return ("invalid");
+ return (line);
}
void
diff --git a/usr.sbin/traceroute6/traceroute6.c b/usr.sbin/traceroute6/traceroute6.c
index 1c9c4d9f000..9bf9411a1ea 100644
--- a/usr.sbin/traceroute6/traceroute6.c
+++ b/usr.sbin/traceroute6/traceroute6.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: traceroute6.c,v 1.65 2014/03/18 10:10:17 florian Exp $ */
+/* $OpenBSD: traceroute6.c,v 1.66 2014/03/18 10:11:00 florian Exp $ */
/* $KAME: traceroute6.c,v 1.63 2002/10/24 12:53:25 itojun Exp $ */
/*
@@ -1124,8 +1124,6 @@ print(struct msghdr *mhdr, int cc)
/*
* Construct an Internet address representation.
- * If the nflag has been supplied, give
- * numeric value, otherwise try for symbolic name.
*/
const char *
inetname(struct sockaddr *sa)
@@ -1134,31 +1132,26 @@ inetname(struct sockaddr *sa)
static int first = 1;
char *cp;
- if (first && !nflag) {
+ if (first) {
first = 0;
if (gethostname(domain, sizeof(domain)) == 0 &&
- (cp = strchr(domain, '.')))
+ (cp = strchr(domain, '.')) != NULL)
(void) strlcpy(domain, cp + 1, sizeof(domain));
else
domain[0] = 0;
}
- cp = NULL;
- if (!nflag) {
- if (getnameinfo(sa, sa->sa_len, line, sizeof(line), NULL, 0,
- NI_NAMEREQD) == 0) {
- if ((cp = strchr(line, '.')) &&
- !strcmp(cp + 1, domain))
- *cp = 0;
- cp = line;
- }
+ if (getnameinfo(sa, sa->sa_len, line, sizeof(line), NULL, 0,
+ NI_NAMEREQD) == 0) {
+ if ((cp = strchr(line, '.')) != NULL && strcmp(cp + 1,
+ domain) == 0)
+ *cp = '\0';
+ return (line);
}
- if (cp)
- return cp;
if (getnameinfo(sa, sa->sa_len, line, sizeof(line), NULL, 0,
NI_NUMERICHOST) != 0)
- strlcpy(line, "invalid", sizeof(line));
- return line;
+ return ("invalid");
+ return (line);
}
void