From 8fdab6289e5ea6683833583684a672bbfaa63e5d Mon Sep 17 00:00:00 2001 From: reyk Date: Fri, 18 Jul 2008 12:35:27 +0000 Subject: [PATCH] merge host_v6 with relayd's version to use getaddrinfo instead of inet_pton. host_v4, host_v6, and host_dns could be merged into one function using getaddrinfo but i keep it in multiple functions to keep it in sync with the other daemons using this common code. --- usr.sbin/snmpd/parse.y | 36 ++++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/usr.sbin/snmpd/parse.y b/usr.sbin/snmpd/parse.y index 089cebdf5ff..f8186c862e9 100644 --- a/usr.sbin/snmpd/parse.y +++ b/usr.sbin/snmpd/parse.y @@ -1,4 +1,4 @@ -/* $OpenBSD: parse.y,v 1.14 2008/02/27 15:12:10 mpf Exp $ */ +/* $OpenBSD: parse.y,v 1.15 2008/07/18 12:35:27 reyk Exp $ */ /* * Copyright (c) 2007, 2008 Reyk Floeter @@ -862,20 +862,28 @@ host_v4(const char *s) struct address * host_v6(const char *s) { - struct in6_addr ina6; - struct sockaddr_in6 *sin6; - struct address *h; + struct addrinfo hints, *res; + struct sockaddr_in6 *sa_in6; + struct address *h = NULL; - bzero(&ina6, sizeof(ina6)); - if (inet_pton(AF_INET6, s, &ina6) != 1) - return (NULL); - - if ((h = calloc(1, sizeof(*h))) == NULL) - fatal(NULL); - sin6 = (struct sockaddr_in6 *)&h->ss; - sin6->sin6_len = sizeof(struct sockaddr_in6); - sin6->sin6_family = AF_INET6; - memcpy(&sin6->sin6_addr, &ina6, sizeof(ina6)); + bzero(&hints, sizeof(hints)); + hints.ai_family = AF_INET6; + hints.ai_socktype = SOCK_DGRAM; /* dummy */ + hints.ai_flags = AI_NUMERICHOST; + if (getaddrinfo(s, "0", &hints, &res) == 0) { + if ((h = calloc(1, sizeof(*h))) == NULL) + fatal(NULL); + sa_in6 = (struct sockaddr_in6 *)&h->ss; + sa_in6->sin6_len = sizeof(struct sockaddr_in6); + sa_in6->sin6_family = AF_INET6; + memcpy(&sa_in6->sin6_addr, + &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr, + sizeof(sa_in6->sin6_addr)); + sa_in6->sin6_scope_id = + ((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id; + + freeaddrinfo(res); + } return (h); } -- 2.20.1