0
0
mirror of https://github.com/zeromq/libzmq.git synced 2025-01-22 07:29:31 +08:00
libzmq/src/ip.cpp

340 lines
9.3 KiB
C++
Raw Normal View History

2009-07-29 12:07:54 +02:00
/*
Copyright (c) 2007-2011 iMatix Corporation
Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file
2009-07-29 12:07:54 +02:00
This file is part of 0MQ.
0MQ is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by
2009-07-29 12:07:54 +02:00
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
0MQ is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
2009-07-29 12:07:54 +02:00
You should have received a copy of the GNU Lesser General Public License
2009-07-29 12:07:54 +02:00
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <string.h>
#include <stdlib.h>
#include <string>
#include "../include/zmq.h"
2009-07-29 12:07:54 +02:00
#include "ip.hpp"
#include "platform.hpp"
#include "err.hpp"
#include "stdint.hpp"
2009-08-03 11:30:13 +02:00
#if defined ZMQ_HAVE_SOLARIS
2009-07-29 12:07:54 +02:00
#include <sys/sockio.h>
#include <net/if.h>
#include <unistd.h>
// On Solaris platform, network interface name can be queried by ioctl.
static int resolve_nic_name (in_addr* addr_, char const *interface_)
{
// Create a socket.
int fd = socket (AF_INET, SOCK_DGRAM, 0);
2009-08-03 11:30:13 +02:00
zmq_assert (fd != -1);
2009-07-29 12:07:54 +02:00
// Retrieve number of interfaces.
lifnum ifn;
ifn.lifn_family = AF_UNSPEC;
ifn.lifn_flags = 0;
int rc = ioctl (fd, SIOCGLIFNUM, (char*) &ifn);
2009-08-03 11:30:13 +02:00
zmq_assert (rc != -1);
2009-07-29 12:07:54 +02:00
// Allocate memory to get interface names.
size_t ifr_size = sizeof (struct lifreq) * ifn.lifn_count;
char *ifr = (char*) malloc (ifr_size);
alloc_assert (ifr);
2009-07-29 12:07:54 +02:00
// Retrieve interface names.
lifconf ifc;
ifc.lifc_family = AF_UNSPEC;
ifc.lifc_flags = 0;
ifc.lifc_len = ifr_size;
ifc.lifc_buf = ifr;
rc = ioctl (fd, SIOCGLIFCONF, (char*) &ifc);
2009-08-03 11:30:13 +02:00
zmq_assert (rc != -1);
2009-07-29 12:07:54 +02:00
// Find the interface with the specified name and AF_INET family.
bool found = false;
lifreq *ifrp = ifc.lifc_req;
for (int n = 0; n < (int) (ifc.lifc_len / sizeof (lifreq));
n ++, ifrp ++) {
if (!strcmp (interface_, ifrp->lifr_name)) {
rc = ioctl (fd, SIOCGLIFADDR, (char*) ifrp);
2009-08-03 11:30:13 +02:00
zmq_assert (rc != -1);
2009-07-29 12:07:54 +02:00
if (ifrp->lifr_addr.ss_family == AF_INET) {
*addr_ = ((sockaddr_in*) &ifrp->lifr_addr)->sin_addr;
found = true;
break;
}
}
}
// Clean-up.
free (ifr);
close (fd);
if (!found) {
2010-01-22 16:32:48 +01:00
errno = ENODEV;
return -1;
2009-07-29 12:07:54 +02:00
}
return 0;
}
2009-08-03 11:30:13 +02:00
#elif defined ZMQ_HAVE_AIX || ZMQ_HAVE_HPUX
2009-07-29 12:07:54 +02:00
#include <sys/types.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <net/if.h>
static int resolve_nic_name (in_addr* addr_, char const *interface_)
{
// Create a socket.
int sd = socket (AF_INET, SOCK_DGRAM, 0);
2009-08-03 11:30:13 +02:00
zmq_assert (sd != -1);
2009-07-29 12:07:54 +02:00
struct ifreq ifr;
// Copy interface name for ioctl get.
2010-02-09 19:23:15 +01:00
strncpy (ifr.ifr_name, interface_, sizeof (ifr.ifr_name));
2009-07-29 12:07:54 +02:00
// Fetch interface address.
int rc = ioctl (sd, SIOCGIFADDR, (caddr_t) &ifr, sizeof (struct ifreq));
// Clean up.
close (sd);
2010-01-22 16:32:48 +01:00
if (rc == -1) {
errno = ENODEV;
2009-07-29 12:07:54 +02:00
return -1;
}
2010-01-22 16:32:48 +01:00
struct sockaddr *sa = (struct sockaddr *) &ifr.ifr_addr;
*addr_ = ((sockaddr_in*)sa)->sin_addr;
return 0;
2009-07-29 12:07:54 +02:00
}
2009-08-03 11:30:13 +02:00
#elif ((defined ZMQ_HAVE_LINUX || defined ZMQ_HAVE_FREEBSD ||\
defined ZMQ_HAVE_OSX || defined ZMQ_HAVE_OPENBSD ||\
2010-02-18 19:38:15 +01:00
defined ZMQ_HAVE_QNXNTO || defined ZMQ_HAVE_NETBSD)\
&& defined ZMQ_HAVE_IFADDRS)
2009-07-29 12:07:54 +02:00
#include <ifaddrs.h>
// On these platforms, network interface name can be queried
// using getifaddrs function.
static int resolve_nic_name (in_addr* addr_, char const *interface_)
{
// Get the addresses.
ifaddrs* ifa = NULL;
int rc = getifaddrs (&ifa);
2009-08-03 11:30:13 +02:00
zmq_assert (rc == 0);
zmq_assert (ifa != NULL);
2009-07-29 12:07:54 +02:00
// Find the corresponding network interface.
bool found = false;
for (ifaddrs *ifp = ifa; ifp != NULL ;ifp = ifp->ifa_next)
if (ifp->ifa_addr && ifp->ifa_addr->sa_family == AF_INET
&& !strcmp (interface_, ifp->ifa_name))
{
*addr_ = ((sockaddr_in*) ifp->ifa_addr)->sin_addr;
found = true;
break;
2009-07-29 12:07:54 +02:00
}
// Clean-up;
freeifaddrs (ifa);
if (!found) {
2010-01-22 16:32:48 +01:00
errno = ENODEV;
return -1;
2009-07-29 12:07:54 +02:00
}
return 0;
}
#else
2010-01-22 16:32:48 +01:00
// On other platforms we assume there are no sane interface names.
// This is true especially of Windows.
2009-07-29 12:07:54 +02:00
static int resolve_nic_name (in_addr* addr_, char const *interface_)
{
2010-01-22 16:32:48 +01:00
errno = ENODEV;
return -1;
2009-07-29 12:07:54 +02:00
}
#endif
2010-01-23 08:19:30 +01:00
int zmq::resolve_ip_interface (sockaddr_storage* addr_, socklen_t *addr_len_,
char const *interface_)
2009-07-29 12:07:54 +02:00
{
2010-01-24 08:19:02 +01:00
// Find the ':' at end that separates NIC name from service.
2010-01-22 16:32:48 +01:00
const char *delimiter = strrchr (interface_, ':');
2009-07-29 12:07:54 +02:00
if (!delimiter) {
errno = EINVAL;
return -1;
}
2010-01-23 08:19:30 +01:00
// Separate the name/port.
2010-01-23 09:08:31 +01:00
std::string iface (interface_, delimiter - interface_);
2010-01-22 16:32:48 +01:00
std::string service (delimiter + 1);
2009-07-29 12:07:54 +02:00
2010-01-22 16:32:48 +01:00
// Initialize the output parameter.
memset (addr_, 0, sizeof (*addr_));
2009-07-29 12:07:54 +02:00
2010-01-22 16:32:48 +01:00
// Initialise IPv4-format family/port.
sockaddr_in ip4_addr;
memset (&ip4_addr, 0, sizeof (ip4_addr));
2010-01-22 16:32:48 +01:00
ip4_addr.sin_family = AF_INET;
ip4_addr.sin_port = htons ((uint16_t) atoi (service.c_str()));
// Initialize temporary output pointers with ip4_addr
sockaddr *out_addr = (sockaddr *) &ip4_addr;
size_t out_addrlen = sizeof (ip4_addr);
// 0 is not a valid port.
if (!ip4_addr.sin_port) {
2009-07-29 12:07:54 +02:00
errno = EINVAL;
return -1;
2009-07-29 12:07:54 +02:00
}
2010-01-22 16:32:48 +01:00
// * resolves to INADDR_ANY.
2010-01-23 09:08:31 +01:00
if (iface.compare("*") == 0) {
2010-01-22 16:32:48 +01:00
ip4_addr.sin_addr.s_addr = htonl (INADDR_ANY);
zmq_assert (out_addrlen <= sizeof (*addr_));
memcpy (addr_, out_addr, out_addrlen);
2010-01-23 08:19:30 +01:00
*addr_len_ = out_addrlen;
2010-01-22 16:32:48 +01:00
return 0;
}
// Try to resolve the string as a NIC name.
2010-01-23 09:08:31 +01:00
int rc = resolve_nic_name (&ip4_addr.sin_addr, iface.c_str());
2010-01-22 16:32:48 +01:00
if (rc != 0 && errno != ENODEV)
return rc;
if (rc == 0) {
zmq_assert (out_addrlen <= sizeof (*addr_));
memcpy (addr_, out_addr, out_addrlen);
2010-01-23 08:19:30 +01:00
*addr_len_ = out_addrlen;
2010-01-22 16:32:48 +01:00
return 0;
}
2010-01-24 08:19:02 +01:00
// There's no such interface name. Assume literal address.
#if defined ZMQ_HAVE_OPENVMS && defined __ia64
__addrinfo64 *res = NULL;
__addrinfo64 req;
#else
2010-01-24 08:19:02 +01:00
addrinfo *res = NULL;
addrinfo req;
#endif
2010-01-24 08:19:02 +01:00
memset (&req, 0, sizeof (req));
// We only support IPv4 addresses for now.
req.ai_family = AF_INET;
2010-01-24 08:19:02 +01:00
// Arbitrary, not used in the output, but avoids duplicate results.
req.ai_socktype = SOCK_STREAM;
// Restrict hostname/service to literals to avoid any DNS lookups or
// service-name irregularity due to indeterminate socktype.
req.ai_flags = AI_PASSIVE | AI_NUMERICHOST | AI_NUMERICSERV;
2010-01-24 08:19:02 +01:00
// Resolve the literal address. Some of the error info is lost in case
// of error, however, there's no way to report EAI errors via errno.
rc = getaddrinfo (iface.c_str(), service.c_str(), &req, &res);
if (rc) {
2010-01-22 16:32:48 +01:00
errno = ENODEV;
return -1;
}
2010-01-24 08:19:02 +01:00
// Use the first result.
2010-02-24 16:19:14 +01:00
zmq_assert ((size_t) (res->ai_addrlen) <= sizeof (*addr_));
2010-01-24 08:38:18 +01:00
memcpy (addr_, res->ai_addr, res->ai_addrlen);
*addr_len_ = res->ai_addrlen;
2010-01-24 08:19:02 +01:00
// Cleanup getaddrinfo after copying the possibly referenced result.
if (res)
freeaddrinfo (res);
2009-07-29 12:07:54 +02:00
return 0;
}
2010-01-23 08:19:30 +01:00
int zmq::resolve_ip_hostname (sockaddr_storage *addr_, socklen_t *addr_len_,
const char *hostname_)
2009-07-29 12:07:54 +02:00
{
2010-01-23 11:14:30 +01:00
// Find the ':' that separates hostname name from service.
2009-07-29 12:07:54 +02:00
const char *delimiter = strchr (hostname_, ':');
if (!delimiter) {
errno = EINVAL;
return -1;
}
2010-01-23 11:14:30 +01:00
// Separate the hostname and service.
2009-07-29 12:07:54 +02:00
std::string hostname (hostname_, delimiter - hostname_);
2010-01-23 11:14:30 +01:00
std::string service (delimiter + 1);
2009-07-29 12:07:54 +02:00
2010-01-24 08:19:02 +01:00
// Set up the query.
2009-07-29 12:07:54 +02:00
addrinfo req;
memset (&req, 0, sizeof (req));
2010-01-23 08:19:30 +01:00
// We only support IPv4 addresses for now.
req.ai_family = AF_INET;
2010-01-23 11:14:30 +01:00
// Need to choose one to avoid duplicate results from getaddrinfo() - this
// doesn't really matter, since it's not included in the addr-output.
req.ai_socktype = SOCK_STREAM;
2010-07-21 17:33:40 +02:00
// Avoid named services due to unclear socktype.
req.ai_flags = AI_NUMERICSERV;
2010-01-23 11:14:30 +01:00
// Resolve host name. Some of the error info is lost in case of error,
// however, there's no way to report EAI errors via errno.
2009-07-29 12:07:54 +02:00
addrinfo *res;
2010-01-23 11:14:30 +01:00
int rc = getaddrinfo (hostname.c_str (), service.c_str (), &req, &res);
2009-07-29 12:07:54 +02:00
if (rc) {
errno = EINVAL;
return -1;
}
2010-01-23 11:14:30 +01:00
// Copy first result to output addr with hostname and service.
2010-02-24 16:19:14 +01:00
zmq_assert ((size_t) (res->ai_addrlen) <= sizeof (*addr_));
2010-01-23 11:14:30 +01:00
memcpy (addr_, res->ai_addr, res->ai_addrlen);
*addr_len_ = res->ai_addrlen;
2009-07-29 12:07:54 +02:00
freeaddrinfo (res);
return 0;
}
2010-01-15 14:11:39 +01:00
2010-01-23 08:19:30 +01:00
int zmq::resolve_local_path (sockaddr_storage *addr_, socklen_t *addr_len_,
const char *path_)
2010-01-15 14:11:39 +01:00
{
#if defined ZMQ_HAVE_WINDOWS || defined ZMQ_HAVE_OPENVMS
errno = EPROTONOSUPPORT;
return -1;
#else
2010-01-23 08:19:30 +01:00
sockaddr_un *un = (sockaddr_un*) addr_;
if (strlen (path_) >= sizeof (un->sun_path))
2010-01-15 14:11:39 +01:00
{
errno = ENAMETOOLONG;
return -1;
}
2010-01-23 08:19:30 +01:00
strcpy (un->sun_path, path_);
un->sun_family = AF_UNIX;
*addr_len_ = sizeof (sockaddr_un);
2010-01-15 14:11:39 +01:00
return 0;
#endif
}
2010-01-15 14:11:39 +01:00