From 7d3d2de4657a080275a705fbf3bd21a484cb3e4b Mon Sep 17 00:00:00 2001 From: David Lamparter Date: Sun, 4 Oct 2009 16:21:49 +0200 Subject: lib: add sockopt helper for setting IPV6_V6ONLY and use it getaddrinfo returns a list of socket parameters for listening. it will contain both IPv4 and IPv6 listening sockets. unless we use IPV6_V6ONLY on the IPv6 ones, only the socket listed first will work. if the IPv4 one came first, the IPv6 one would get an "Address in use" error. this functionality was already present for bgpd and its listening sockets. as it is needed for vtys as well, make it a common helper. --- lib/sockunion.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'lib/sockunion.c') diff --git a/lib/sockunion.c b/lib/sockunion.c index 6a40f332..d1f6d80b 100644 --- a/lib/sockunion.c +++ b/lib/sockunion.c @@ -527,6 +527,30 @@ sockopt_ttl (int family, int sock, int ttl) return 0; } +int +sockopt_v6only (int family, int sock) +{ + int ret, on = 1; + +#ifdef HAVE_IPV6 +#ifdef IPV6_V6ONLY + if (family == AF_INET6) + { + ret = setsockopt (sock, IPPROTO_IPV6, IPV6_V6ONLY, + (void *) &on, sizeof (int)); + if (ret < 0) + { + zlog (NULL, LOG_WARNING, "can't set sockopt IPV6_V6ONLY " + "to socket %d", sock); + return -1; + } + return 0; + } +#endif /* IPV6_V6ONLY */ +#endif /* HAVE_IPV6 */ + return 0; +} + /* If same family and same prefix return 1. */ int sockunion_same (union sockunion *su1, union sockunion *su2) -- cgit v1.2.1