diff options
-rw-r--r-- | src/fd_signaler.cpp | 1 | ||||
-rw-r--r-- | src/ip.cpp | 24 | ||||
-rw-r--r-- | src/ip.hpp | 6 | ||||
-rw-r--r-- | src/tcp_connecter.cpp | 17 | ||||
-rw-r--r-- | src/tcp_connecter.hpp | 2 | ||||
-rw-r--r-- | src/tcp_listener.cpp | 16 |
6 files changed, 35 insertions, 31 deletions
diff --git a/src/fd_signaler.cpp b/src/fd_signaler.cpp index aafd21e..a49dd99 100644 --- a/src/fd_signaler.cpp +++ b/src/fd_signaler.cpp @@ -127,7 +127,6 @@ zmq::fd_signaler_t::fd_signaler_t () memset (&addr, 0, sizeof (addr)); addr.sin_family = AF_INET; - //resolve_ip_hostname (&addr, "127.0.0.1:0"); addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK); addr.sin_port = 0; @@ -245,8 +245,10 @@ static int resolve_nic_name (in_addr* addr_, char const *interface_) #endif -int zmq::resolve_ip_interface (sockaddr_in* addr_, char const *interface_) +int zmq::resolve_ip_interface (sockaddr_storage* addr_, char const *interface_) { + sockaddr_in *addr = (sockaddr_in*) addr_; + // Find the ':' that separates NIC name from port. const char *delimiter = strchr (interface_, ':'); if (!delimiter) { @@ -255,17 +257,17 @@ int zmq::resolve_ip_interface (sockaddr_in* addr_, char const *interface_) } // Clean the structure and fill in protocol family. - memset (addr_, 0, sizeof (sockaddr_in)); - addr_->sin_family = AF_INET; + memset (addr, 0, sizeof (sockaddr_in)); + addr->sin_family = AF_INET; // Resolve the name of the NIC. std::string nic_name (interface_, delimiter - interface_); - if (resolve_nic_name (&addr_->sin_addr, nic_name.c_str ()) != 0) + if (resolve_nic_name (&addr->sin_addr, nic_name.c_str ()) != 0) return -1; // Resolve the port. - addr_->sin_port = htons ((uint16_t) atoi (delimiter + 1)); - if (!addr_->sin_port) { + addr->sin_port = htons ((uint16_t) atoi (delimiter + 1)); + if (!addr->sin_port) { errno = EINVAL; return -1; } @@ -273,8 +275,10 @@ int zmq::resolve_ip_interface (sockaddr_in* addr_, char const *interface_) return 0; } -int zmq::resolve_ip_hostname (sockaddr_in *addr_, const char *hostname_) +int zmq::resolve_ip_hostname (sockaddr_storage *addr_, const char *hostname_) { + sockaddr_in *addr = (sockaddr_in*) addr_; + // Find the ':' that separates hostname name from port. const char *delimiter = strchr (hostname_, ':'); if (!delimiter) { @@ -297,12 +301,12 @@ int zmq::resolve_ip_hostname (sockaddr_in *addr_, const char *hostname_) return -1; } zmq_assert (res->ai_addr->sa_family == AF_INET); - memcpy (addr_, res->ai_addr, sizeof (sockaddr_in)); + memcpy (addr, res->ai_addr, sizeof (sockaddr_in)); freeaddrinfo (res); // Fill in the port number. - addr_->sin_port = htons ((uint16_t) atoi (delimiter + 1)); - if (!addr_->sin_port) { + addr->sin_port = htons ((uint16_t) atoi (delimiter + 1)); + if (!addr->sin_port) { errno = EINVAL; return -1; } @@ -41,15 +41,15 @@ namespace zmq // Resolves network interface name in <nic-name>:<port> format. Symbol "*" // (asterisk) resolves to INADDR_ANY (all network interfaces). - int resolve_ip_interface (sockaddr_in* addr_, char const *interface_); + int resolve_ip_interface (sockaddr_storage *addr_, char const *interface_); // This function resolves a string in <hostname>:<port-number> format. // Hostname can be either the name of the host or its IP address. - int resolve_ip_hostname (sockaddr_in *addr_, const char *hostname_); + int resolve_ip_hostname (sockaddr_storage *addr_, const char *hostname_); #if !defined ZMQ_HAVE_WINDOWS && !defined ZMQ_HAVE_OPENVMS // This function sets up the sockaddr_un structure with the pathname_ - int resolve_local_path( sockaddr_un * addr_, const char* pathname_); + int resolve_local_path (sockaddr_un *addr_, const char* pathname_); #endif } diff --git a/src/tcp_connecter.cpp b/src/tcp_connecter.cpp index c1b8c46..436da88 100644 --- a/src/tcp_connecter.cpp +++ b/src/tcp_connecter.cpp @@ -45,7 +45,7 @@ zmq::tcp_connecter_t::~tcp_connecter_t () int zmq::tcp_connecter_t::set_address (const char *protocol_, const char *addr_) { if (strcmp (protocol_, "tcp") == 0) - return resolve_ip_hostname ((sockaddr_in*) &addr, addr_); + return resolve_ip_hostname (&addr, addr_); errno = EPROTONOSUPPORT; return -1; @@ -56,7 +56,7 @@ int zmq::tcp_connecter_t::open () zmq_assert (s == retired_fd); // Create the socket. - s = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP); + s = socket (addr.ss_family, SOCK_STREAM, IPPROTO_TCP); if (s == INVALID_SOCKET) { wsa_error_to_errno (); return -1; @@ -74,7 +74,7 @@ int zmq::tcp_connecter_t::open () wsa_assert (rc != SOCKET_ERROR); // Connect to the remote peer. - rc = ::connect (s, (sockaddr*) &addr, sizeof (sockaddr_in)); + rc = ::connect (s, (sockaddr*) &addr, sizeof (addr)); // Connect was successfull immediately. if (rc == 0) @@ -153,7 +153,7 @@ zmq::tcp_connecter_t::~tcp_connecter_t () int zmq::tcp_connecter_t::set_address (const char *protocol_, const char *addr_) { if (strcmp (protocol_, "tcp") == 0) - return resolve_ip_hostname ((struct sockaddr_in*)&addr, addr_); + return resolve_ip_hostname (&addr, addr_); else if (strcmp (protocol_, "ipc") == 0) return resolve_local_path (( struct sockaddr_un*)&addr, addr_); @@ -166,10 +166,10 @@ int zmq::tcp_connecter_t::open () zmq_assert (s == retired_fd); struct sockaddr *sa = (struct sockaddr*) &addr; - if (AF_INET == sa->sa_family) { + if (AF_UNIX != sa->sa_family) { // Create the socket. - s = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP); + s = socket (sa->sa_family, SOCK_STREAM, IPPROTO_TCP); if (s == -1) return -1; @@ -193,7 +193,7 @@ int zmq::tcp_connecter_t::open () #endif // Connect to the remote peer. - rc = ::connect (s, (struct sockaddr*) &addr, sizeof (sockaddr_in)); + rc = ::connect (s, (struct sockaddr*) &addr, sizeof (addr)); // Connect was successfull immediately. if (rc == 0) @@ -211,9 +211,10 @@ int zmq::tcp_connecter_t::open () errno = err; return -1; } - else if (AF_UNIX == sa->sa_family) { + else { // Create the socket. + zmq_assert (AF_UNIX == sa->sa_family); s = socket (AF_UNIX, SOCK_STREAM, 0); if (s == -1) return -1; diff --git a/src/tcp_connecter.hpp b/src/tcp_connecter.hpp index cc4434f..23a6e9c 100644 --- a/src/tcp_connecter.hpp +++ b/src/tcp_connecter.hpp @@ -58,7 +58,7 @@ namespace zmq private: // Address to connect to. - sockaddr_in addr; + sockaddr_storage addr; // Underlying socket. fd_t s; diff --git a/src/tcp_listener.cpp b/src/tcp_listener.cpp index 2323e31..c26201c 100644 --- a/src/tcp_listener.cpp +++ b/src/tcp_listener.cpp @@ -50,12 +50,12 @@ int zmq::tcp_listener_t::set_address (const char *protocol_, const char *addr_) } // Convert the interface into sockaddr_in structure. - int rc = resolve_ip_interface ((sockaddr_in*) &addr, addr_); + int rc = resolve_ip_interface (&addr, addr_); if (rc != 0) return rc; // Create a listening socket. - s = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP); + s = socket (addr.ss_family, SOCK_STREAM, IPPROTO_TCP); if (s == INVALID_SOCKET) { wsa_error_to_errno (); return -1; @@ -73,7 +73,7 @@ int zmq::tcp_listener_t::set_address (const char *protocol_, const char *addr_) wsa_assert (rc != SOCKET_ERROR); // Bind the socket to the network interface and port. - rc = bind (s, (struct sockaddr*) &addr, sizeof (sockaddr_in)); + rc = bind (s, (struct sockaddr*) &addr, sizeof (addr)); if (rc == SOCKET_ERROR) { wsa_error_to_errno (); return -1; @@ -157,13 +157,13 @@ int zmq::tcp_listener_t::set_address (const char *protocol_, const char *addr_) { if (strcmp (protocol_, "tcp") == 0 ) { - // Convert the interface into sockaddr_in structure. - int rc = resolve_ip_interface ((struct sockaddr_in*) &addr, addr_); + // Resolve the sockaddr to bind to. + int rc = resolve_ip_interface (&addr, addr_); if (rc != 0) return -1; // Create a listening socket. - s = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP); + s = socket (addr.ss_family, SOCK_STREAM, IPPROTO_TCP); if (s == -1) return -1; @@ -180,7 +180,7 @@ int zmq::tcp_listener_t::set_address (const char *protocol_, const char *addr_) errno_assert (rc != -1); // Bind the socket to the network interface and port. - rc = bind (s, (struct sockaddr*) &addr, sizeof (sockaddr_in)); + rc = bind (s, (struct sockaddr*) &addr, sizeof (addr)); if (rc != 0) { close (); return -1; @@ -305,7 +305,7 @@ zmq::fd_t zmq::tcp_listener_t::accept () errno_assert (rc != -1); struct sockaddr *sa = (struct sockaddr*) &addr; - if (AF_INET == sa->sa_family) { + if (AF_UNIX != sa->sa_family) { // Disable Nagle's algorithm. int flag = 1; |