0
0
mirror of https://github.com/zeromq/libzmq.git synced 2025-01-21 15:12:03 +08:00

Merge pull request #295 from hurtonm/tcp_address_fixes_anc_cleanups

Tcp address fixes anc cleanups
This commit is contained in:
Ian Barber 2012-03-27 01:55:20 -07:00
commit ed65271c52

View File

@ -213,13 +213,13 @@ int zmq::tcp_address_t::resolve_nic_name (const char *nic_, bool ipv4only_)
#endif #endif
int zmq::tcp_address_t::resolve_interface (char const *interface_, int zmq::tcp_address_t::resolve_interface (const char *interface_,
bool ipv4only_) bool ipv4only_)
{ {
// Initialize temporary output pointers with storage address. // Initialize temporary output pointers with storage address.
sockaddr_storage ss; sockaddr_storage ss;
sockaddr *out_addr = (sockaddr *) &ss; sockaddr *out_addr = (sockaddr*) &ss;
socklen_t out_addrlen; size_t out_addrlen;
// Initialise IP-format family/port and populate temporary output pointers // Initialise IP-format family/port and populate temporary output pointers
// with the address. // with the address.
@ -228,20 +228,21 @@ int zmq::tcp_address_t::resolve_interface (char const *interface_,
memset (&ip4_addr, 0, sizeof (ip4_addr)); memset (&ip4_addr, 0, sizeof (ip4_addr));
ip4_addr.sin_family = AF_INET; ip4_addr.sin_family = AF_INET;
ip4_addr.sin_addr.s_addr = htonl (INADDR_ANY); ip4_addr.sin_addr.s_addr = htonl (INADDR_ANY);
out_addrlen = (socklen_t) sizeof (ip4_addr); out_addrlen = sizeof ip4_addr;
memcpy (out_addr, &ip4_addr, out_addrlen); memcpy (out_addr, &ip4_addr, out_addrlen);
} else { }
else {
sockaddr_in6 ip6_addr; sockaddr_in6 ip6_addr;
memset (&ip6_addr, 0, sizeof (ip6_addr)); memset (&ip6_addr, 0, sizeof (ip6_addr));
ip6_addr.sin6_family = AF_INET6; ip6_addr.sin6_family = AF_INET6;
memcpy (&ip6_addr.sin6_addr, &in6addr_any, sizeof (in6addr_any)); memcpy (&ip6_addr.sin6_addr, &in6addr_any, sizeof (in6addr_any));
out_addrlen = (socklen_t) sizeof (ip6_addr); out_addrlen = sizeof ip6_addr;
memcpy (out_addr, &ip6_addr, out_addrlen); memcpy (out_addr, &ip6_addr, out_addrlen);
} }
// * resolves to INADDR_ANY or in6addr_any. // * resolves to INADDR_ANY or in6addr_any.
if (strcmp (interface_, "*") == 0) { if (strcmp (interface_, "*") == 0) {
zmq_assert (out_addrlen <= (socklen_t) sizeof (address)); zmq_assert (out_addrlen <= sizeof address);
memcpy (&address, out_addr, out_addrlen); memcpy (&address, out_addr, out_addrlen);
return 0; return 0;
} }
@ -251,7 +252,7 @@ int zmq::tcp_address_t::resolve_interface (char const *interface_,
if (rc != 0 && errno != ENODEV) if (rc != 0 && errno != ENODEV)
return rc; return rc;
if (rc == 0) { if (rc == 0) {
zmq_assert (out_addrlen <= (socklen_t) sizeof (address)); zmq_assert (out_addrlen <= sizeof address);
memcpy (&address, out_addr, out_addrlen); memcpy (&address, out_addr, out_addrlen);
return 0; return 0;
} }
@ -386,18 +387,18 @@ int zmq::tcp_address_t::resolve (const char *name_, bool local_, bool ipv4only_)
std::string port_str (delimiter + 1); std::string port_str (delimiter + 1);
// Remove square brackets around the address, if any. // Remove square brackets around the address, if any.
if (!addr_str.empty () && addr_str [0] == '[' && if (addr_str.size () >= 2 && addr_str [0] == '[' &&
addr_str [addr_str.size () - 1] == ']') addr_str [addr_str.size () - 1] == ']')
addr_str = addr_str.substr (1, addr_str.size () - 2); addr_str = addr_str.substr (1, addr_str.size () - 2);
uint16_t port; uint16_t port;
// Allow 0 specifically, to detect invalid port error in atoi if not // Allow 0 specifically, to detect invalid port error in atoi if not
if (port_str[0] == '*' || port_str[0] == '0') { if (port_str == "*" || port_str == "0")
// Resolve wildcard to 0 to allow autoselection of port // Resolve wildcard to 0 to allow autoselection of port
port = 0; port = 0;
} else { else {
// Parse the port number (0 is not a valid port). // Parse the port number (0 is not a valid port).
port = (uint16_t) atoi (port_str.c_str()); port = (uint16_t) atoi (port_str.c_str ());
if (port == 0) { if (port == 0) {
errno = EINVAL; errno = EINVAL;
return -1; return -1;