2
Copyright (c) 2009-2011 250bpm s.r.o.
3
Copyright (c) 2007-2009 iMatix Corporation
4
Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file
6
This file is part of 0MQ.
8
0MQ is free software; you can redistribute it and/or modify it under
9
the terms of the GNU Lesser General Public License as published by
10
the Free Software Foundation; either version 3 of the License, or
11
(at your option) any later version.
13
0MQ is distributed in the hope that it will be useful,
14
but WITHOUT ANY WARRANTY; without even the implied warranty of
15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
GNU Lesser General Public License for more details.
18
You should have received a copy of the GNU Lesser General Public License
19
along with this program. If not, see <http://www.gnu.org/licenses/>.
25
#include "tcp_address.hpp"
26
#include "platform.hpp"
31
#ifdef ZMQ_HAVE_WINDOWS
32
#include "windows.hpp"
34
#include <sys/types.h>
35
#include <arpa/inet.h>
36
#include <netinet/tcp.h>
40
// Some platforms (notably Darwin/OSX and NetBSD) do not define all AI_
41
// flags for getaddrinfo(). This can be worked around safely by defining
44
#define AI_ADDRCONFIG 0
47
#if defined ZMQ_HAVE_SOLARIS
49
#include <sys/sockio.h>
54
// On Solaris platform, network interface name can be queried by ioctl.
55
int zmq::tcp_address_t::resolve_nic_name (const char *nic_, bool ipv4only_)
57
// TODO: Unused parameter, IPv6 support not implemented for Solaris.
61
int fd = open_socket (AF_INET, SOCK_DGRAM, 0);
62
zmq_assert (fd != -1);
64
// Retrieve number of interfaces.
66
ifn.lifn_family = AF_INET;
68
int rc = ioctl (fd, SIOCGLIFNUM, (char*) &ifn);
69
zmq_assert (rc != -1);
71
// Allocate memory to get interface names.
72
size_t ifr_size = sizeof (struct lifreq) * ifn.lifn_count;
73
char *ifr = (char*) malloc (ifr_size);
76
// Retrieve interface names.
78
ifc.lifc_family = AF_INET;
80
ifc.lifc_len = ifr_size;
82
rc = ioctl (fd, SIOCGLIFCONF, (char*) &ifc);
83
zmq_assert (rc != -1);
85
// Find the interface with the specified name and AF_INET family.
87
lifreq *ifrp = ifc.lifc_req;
88
for (int n = 0; n < (int) (ifc.lifc_len / sizeof (lifreq));
90
if (!strcmp (nic_, ifrp->lifr_name)) {
91
rc = ioctl (fd, SIOCGLIFADDR, (char*) ifrp);
92
zmq_assert (rc != -1);
93
if (ifrp->lifr_addr.ss_family == AF_INET) {
94
address.ipv4 = *(sockaddr_in*) &ifrp->lifr_addr;
113
#elif defined ZMQ_HAVE_AIX || defined ZMQ_HAVE_HPUX || defined ZMQ_HAVE_ANDROID
115
#include <sys/types.h>
117
#include <sys/ioctl.h>
120
int zmq::tcp_address_t::resolve_nic_name (const char *nic_, bool ipv4only_)
122
// TODO: Unused parameter, IPv6 support not implemented for AIX or HP/UX.
126
int sd = open_socket (AF_INET, SOCK_DGRAM, 0);
127
zmq_assert (sd != -1);
131
// Copy interface name for ioctl get.
132
strncpy (ifr.ifr_name, nic_, sizeof (ifr.ifr_name));
134
// Fetch interface address.
135
int rc = ioctl (sd, SIOCGIFADDR, (caddr_t) &ifr, sizeof (struct ifreq));
145
memcpy (&address.ipv4.sin_addr, &((sockaddr_in*) &ifr.ifr_addr)->sin_addr,
151
#elif ((defined ZMQ_HAVE_LINUX || defined ZMQ_HAVE_FREEBSD ||\
152
defined ZMQ_HAVE_OSX || defined ZMQ_HAVE_OPENBSD ||\
153
defined ZMQ_HAVE_QNXNTO || defined ZMQ_HAVE_NETBSD)\
154
&& defined ZMQ_HAVE_IFADDRS)
158
// On these platforms, network interface name can be queried
159
// using getifaddrs function.
160
int zmq::tcp_address_t::resolve_nic_name (const char *nic_, bool ipv4only_)
162
// Get the addresses.
164
int rc = getifaddrs (&ifa);
165
zmq_assert (rc == 0);
166
zmq_assert (ifa != NULL);
168
// Find the corresponding network interface.
170
for (ifaddrs *ifp = ifa; ifp != NULL ;ifp = ifp->ifa_next)
172
if (ifp->ifa_addr == NULL)
175
int family = ifp->ifa_addr->sa_family;
177
if ((family == AF_INET
178
|| (!ipv4only_ && family == AF_INET6))
179
&& !strcmp (nic_, ifp->ifa_name))
181
memcpy (&address, ifp->ifa_addr,
182
(family == AF_INET) ? sizeof (struct sockaddr_in)
183
: sizeof (struct sockaddr_in6));
202
// On other platforms we assume there are no sane interface names.
203
// This is true especially of Windows.
204
int zmq::tcp_address_t::resolve_nic_name (const char *nic_, bool ipv4only_)
206
// All unused parameters.
216
int zmq::tcp_address_t::resolve_interface (char const *interface_,
219
// Initialize temporary output pointers with storage address.
221
sockaddr *out_addr = (sockaddr *) &ss;
222
socklen_t out_addrlen;
224
// Initialise IP-format family/port and populate temporary output pointers
227
sockaddr_in ip4_addr;
228
memset (&ip4_addr, 0, sizeof (ip4_addr));
229
ip4_addr.sin_family = AF_INET;
230
ip4_addr.sin_addr.s_addr = htonl (INADDR_ANY);
231
out_addrlen = (socklen_t) sizeof (ip4_addr);
232
memcpy (out_addr, &ip4_addr, out_addrlen);
234
sockaddr_in6 ip6_addr;
235
memset (&ip6_addr, 0, sizeof (ip6_addr));
236
ip6_addr.sin6_family = AF_INET6;
237
memcpy (&ip6_addr.sin6_addr, &in6addr_any, sizeof (in6addr_any));
238
out_addrlen = (socklen_t) sizeof (ip6_addr);
239
memcpy (out_addr, &ip6_addr, out_addrlen);
242
// * resolves to INADDR_ANY or in6addr_any.
243
if (strcmp (interface_, "*") == 0) {
244
zmq_assert (out_addrlen <= (socklen_t) sizeof (address));
245
memcpy (&address, out_addr, out_addrlen);
249
// Try to resolve the string as a NIC name.
250
int rc = resolve_nic_name (interface_, ipv4only_);
251
if (rc != 0 && errno != ENODEV)
254
zmq_assert (out_addrlen <= (socklen_t) sizeof (address));
255
memcpy (&address, out_addr, out_addrlen);
259
// There's no such interface name. Assume literal address.
260
#if defined ZMQ_HAVE_OPENVMS && defined __ia64
261
__addrinfo64 *res = NULL;
264
addrinfo *res = NULL;
267
memset (&req, 0, sizeof (req));
269
// Choose IPv4 or IPv6 protocol family. Note that IPv6 allows for
270
// IPv4-in-IPv6 addresses.
271
req.ai_family = ipv4only_ ? AF_INET : AF_INET6;
273
// Arbitrary, not used in the output, but avoids duplicate results.
274
req.ai_socktype = SOCK_STREAM;
276
// Restrict hostname/service to literals to avoid any DNS lookups or
277
// service-name irregularity due to indeterminate socktype.
278
req.ai_flags = AI_PASSIVE | AI_NUMERICHOST;
280
#ifndef ZMQ_HAVE_WINDOWS
281
// Windows by default maps IPv4 addresses into IPv6. In this API we only
282
// require IPv4-mapped addresses when no native IPv6 interfaces are
283
// available (~AI_ALL). This saves an additional DNS roundtrip for IPv4
285
if (req.ai_family == AF_INET6)
286
req.ai_flags |= AI_V4MAPPED;
289
// Resolve the literal address. Some of the error info is lost in case
290
// of error, however, there's no way to report EAI errors via errno.
291
rc = getaddrinfo (interface_, NULL, &req, &res);
297
// Use the first result.
298
zmq_assert ((size_t) (res->ai_addrlen) <= sizeof (address));
299
memcpy (&address, res->ai_addr, res->ai_addrlen);
301
// Cleanup getaddrinfo after copying the possibly referenced result.
308
int zmq::tcp_address_t::resolve_hostname (const char *hostname_, bool ipv4only_)
311
#if defined ZMQ_HAVE_OPENVMS && defined __ia64 && __INITIAL_POINTER_SIZE == 64
316
memset (&req, 0, sizeof (req));
318
// Choose IPv4 or IPv6 protocol family. Note that IPv6 allows for
319
// IPv4-in-IPv6 addresses.
320
req.ai_family = ipv4only_ ? AF_INET : AF_INET6;
322
// Need to choose one to avoid duplicate results from getaddrinfo() - this
323
// doesn't really matter, since it's not included in the addr-output.
324
req.ai_socktype = SOCK_STREAM;
326
#ifndef ZMQ_HAVE_WINDOWS
327
// Windows by default maps IPv4 addresses into IPv6. In this API we only
328
// require IPv4-mapped addresses when no native IPv6 interfaces are
329
// available. This saves an additional DNS roundtrip for IPv4 addresses.
330
if (req.ai_family == AF_INET6)
331
req.ai_flags |= AI_V4MAPPED;
334
// Resolve host name. Some of the error info is lost in case of error,
335
// however, there's no way to report EAI errors via errno.
336
#if defined ZMQ_HAVE_OPENVMS && defined __ia64 && __INITIAL_POINTER_SIZE == 64
341
int rc = getaddrinfo (hostname_, NULL, &req, &res);
354
// Copy first result to output addr with hostname and service.
355
zmq_assert ((size_t) (res->ai_addrlen) <= sizeof (address));
356
memcpy (&address, res->ai_addr, res->ai_addrlen);
363
zmq::tcp_address_t::tcp_address_t ()
365
memset (&address, 0, sizeof (address));
368
zmq::tcp_address_t::~tcp_address_t ()
372
int zmq::tcp_address_t::resolve (const char *name_, bool local_, bool ipv4only_)
374
// Find the ':' at end that separates address from the port number.
375
const char *delimiter = strrchr (name_, ':');
381
// Separate the address/port.
382
std::string addr_str (name_, delimiter - name_);
383
std::string port_str (delimiter + 1);
385
// Remove square brackets around the address, if any.
386
if (!addr_str.empty () && addr_str [0] == '[' &&
387
addr_str [addr_str.size () - 1] == ']')
388
addr_str = addr_str.substr (1, addr_str.size () - 2);
390
// Parse the port number (0 is not a valid port).
391
uint16_t port = (uint16_t) atoi (port_str.c_str());
397
// Resolve the IP address.
400
rc = resolve_interface (addr_str.c_str (), ipv4only_);
402
rc = resolve_hostname (addr_str.c_str (), ipv4only_);
406
// Set the port into the address structure.
407
if (address.generic.sa_family == AF_INET6)
408
address.ipv6.sin6_port = htons (port);
410
address.ipv4.sin_port = htons (port);
415
sockaddr *zmq::tcp_address_t::addr ()
417
return &address.generic;
420
socklen_t zmq::tcp_address_t::addrlen ()
422
if (address.generic.sa_family == AF_INET6)
423
return (socklen_t) sizeof (address.ipv6);
425
return (socklen_t) sizeof (address.ipv4);
428
#if defined ZMQ_HAVE_WINDOWS
429
unsigned short zmq::tcp_address_t::family ()
431
sa_family_t zmq::tcp_address_t::family ()
434
return address.generic.sa_family;