Commit 80a5f60e authored by Martin Sustrik's avatar Martin Sustrik

NIC name resolution moved to tcp_address_t

Signed-off-by: 's avatarMartin Sustrik <sustrik@250bpm.com>
parent f0c7edbc
...@@ -47,10 +47,10 @@ ...@@ -47,10 +47,10 @@
#include <sys/sockio.h> #include <sys/sockio.h>
#include <net/if.h> #include <net/if.h>
#include <unistd.h> #include <unistd.h>
#include <stdlib.h>
// On Solaris platform, network interface name can be queried by ioctl. // On Solaris platform, network interface name can be queried by ioctl.
static int resolve_nic_name (struct sockaddr* addr_, char const *interface_, int zmq::tcp_address_t::resolve_nic_name (const char *nic_, bool ipv4only_)
bool ipv4only_)
{ {
// TODO: Unused parameter, IPv6 support not implemented for Solaris. // TODO: Unused parameter, IPv6 support not implemented for Solaris.
(void) ipv4only_; (void) ipv4only_;
...@@ -85,11 +85,11 @@ static int resolve_nic_name (struct sockaddr* addr_, char const *interface_, ...@@ -85,11 +85,11 @@ static int resolve_nic_name (struct sockaddr* addr_, char const *interface_,
lifreq *ifrp = ifc.lifc_req; lifreq *ifrp = ifc.lifc_req;
for (int n = 0; n < (int) (ifc.lifc_len / sizeof (lifreq)); for (int n = 0; n < (int) (ifc.lifc_len / sizeof (lifreq));
n ++, ifrp ++) { n ++, ifrp ++) {
if (!strcmp (interface_, ifrp->lifr_name)) { if (!strcmp (nic_, ifrp->lifr_name)) {
rc = ioctl (fd, SIOCGLIFADDR, (char*) ifrp); rc = ioctl (fd, SIOCGLIFADDR, (char*) ifrp);
zmq_assert (rc != -1); zmq_assert (rc != -1);
if (ifrp->lifr_addr.ss_family == AF_INET) { if (ifrp->lifr_addr.ss_family == AF_INET) {
*(sockaddr_in*) addr_ = *(sockaddr_in*) &ifrp->lifr_addr; address.ipv4 = *(sockaddr_in*) &ifrp->lifr_addr;
found = true; found = true;
break; break;
} }
...@@ -115,8 +115,7 @@ static int resolve_nic_name (struct sockaddr* addr_, char const *interface_, ...@@ -115,8 +115,7 @@ static int resolve_nic_name (struct sockaddr* addr_, char const *interface_,
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <net/if.h> #include <net/if.h>
static int resolve_nic_name (struct sockaddr* addr_, char const *interface_, int zmq::tcp_address_t::resolve_nic_name (const char *nic_, bool ipv4only_)
bool ipv4only_)
{ {
// TODO: Unused parameter, IPv6 support not implemented for AIX or HP/UX. // TODO: Unused parameter, IPv6 support not implemented for AIX or HP/UX.
(void) ipv4only_; (void) ipv4only_;
...@@ -128,7 +127,7 @@ static int resolve_nic_name (struct sockaddr* addr_, char const *interface_, ...@@ -128,7 +127,7 @@ static int resolve_nic_name (struct sockaddr* addr_, char const *interface_,
struct ifreq ifr; struct ifreq ifr;
// Copy interface name for ioctl get. // Copy interface name for ioctl get.
strncpy (ifr.ifr_name, interface_, sizeof (ifr.ifr_name)); strncpy (ifr.ifr_name, nic_, sizeof (ifr.ifr_name));
// Fetch interface address. // Fetch interface address.
int rc = ioctl (sd, SIOCGIFADDR, (caddr_t) &ifr, sizeof (struct ifreq)); int rc = ioctl (sd, SIOCGIFADDR, (caddr_t) &ifr, sizeof (struct ifreq));
...@@ -141,8 +140,8 @@ static int resolve_nic_name (struct sockaddr* addr_, char const *interface_, ...@@ -141,8 +140,8 @@ static int resolve_nic_name (struct sockaddr* addr_, char const *interface_,
return -1; return -1;
} }
memcpy (&((sockaddr_in*) addr_)->sin_addr, memcpy (&address.ipv4.sin_addr, &((sockaddr_in*) &ifr.ifr_addr)->sin_addr,
&((sockaddr_in*) &ifr.ifr_addr)->sin_addr, sizeof (in_addr)); sizeof (in_addr));
return 0; return 0;
} }
...@@ -156,8 +155,7 @@ static int resolve_nic_name (struct sockaddr* addr_, char const *interface_, ...@@ -156,8 +155,7 @@ static int resolve_nic_name (struct sockaddr* addr_, char const *interface_,
// On these platforms, network interface name can be queried // On these platforms, network interface name can be queried
// using getifaddrs function. // using getifaddrs function.
static int resolve_nic_name (struct sockaddr* addr_, char const *interface_, int zmq::tcp_address_t::resolve_nic_name (const char *nic_, bool ipv4only_)
bool ipv4only_)
{ {
// Get the addresses. // Get the addresses.
ifaddrs* ifa = NULL; ifaddrs* ifa = NULL;
...@@ -176,9 +174,9 @@ static int resolve_nic_name (struct sockaddr* addr_, char const *interface_, ...@@ -176,9 +174,9 @@ static int resolve_nic_name (struct sockaddr* addr_, char const *interface_,
if ((family == AF_INET if ((family == AF_INET
|| (!ipv4only_ && family == AF_INET6)) || (!ipv4only_ && family == AF_INET6))
&& !strcmp (interface_, ifp->ifa_name)) && !strcmp (nic_, ifp->ifa_name))
{ {
memcpy (addr_, ifp->ifa_addr, memcpy (&address, ifp->ifa_addr,
(family == AF_INET) ? sizeof (struct sockaddr_in) (family == AF_INET) ? sizeof (struct sockaddr_in)
: sizeof (struct sockaddr_in6)); : sizeof (struct sockaddr_in6));
found = true; found = true;
...@@ -201,12 +199,10 @@ static int resolve_nic_name (struct sockaddr* addr_, char const *interface_, ...@@ -201,12 +199,10 @@ static int resolve_nic_name (struct sockaddr* addr_, char const *interface_,
// On other platforms we assume there are no sane interface names. // On other platforms we assume there are no sane interface names.
// This is true especially of Windows. // This is true especially of Windows.
static int resolve_nic_name (struct sockaddr* addr_, char const *interface_, int zmq::tcp_address_t::resolve_nic_name (const char *nic_, bool ipv4only_)
bool ipv4only_)
{ {
// All unused parameters. // All unused parameters.
(void) addr_; (void) nic_;
(void) interface_;
(void) ipv4only_; (void) ipv4only_;
errno = ENODEV; errno = ENODEV;
...@@ -249,7 +245,7 @@ int zmq::tcp_address_t::resolve_interface (char const *interface_, ...@@ -249,7 +245,7 @@ int zmq::tcp_address_t::resolve_interface (char const *interface_,
} }
// Try to resolve the string as a NIC name. // Try to resolve the string as a NIC name.
int rc = resolve_nic_name (out_addr, interface_, ipv4only_); int rc = resolve_nic_name (interface_, ipv4only_);
if (rc != 0 && errno != ENODEV) if (rc != 0 && errno != ENODEV)
return rc; return rc;
if (rc == 0) { if (rc == 0) {
......
...@@ -56,14 +56,14 @@ namespace zmq ...@@ -56,14 +56,14 @@ namespace zmq
private: private:
int resolve_interface (char const *interface_, bool ipv4only_); int resolve_nic_name (const char *nic_, bool ipv4only_);
int resolve_interface (const char *interface_, bool ipv4only_);
int resolve_hostname (const char *hostname_, bool ipv4only_); int resolve_hostname (const char *hostname_, bool ipv4only_);
union { union {
sockaddr generic; sockaddr generic;
sockaddr_in ipv4; sockaddr_in ipv4;
sockaddr_in6 ipv6; sockaddr_in6 ipv6;
char padding [1024];
} address; } address;
tcp_address_t (const tcp_address_t&); tcp_address_t (const tcp_address_t&);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment