Tero Marttila's Ipv6 patch - part I.

This commit is contained in:
Martin Sustrik 2010-01-22 13:13:52 +01:00
parent 6edec4fe93
commit 34d65e22c5
6 changed files with 35 additions and 31 deletions

View File

@ -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;

View File

@ -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;
}

View File

@ -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
}

View File

@ -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;

View File

@ -58,7 +58,7 @@ namespace zmq
private:
// Address to connect to.
sockaddr_in addr;
sockaddr_storage addr;
// Underlying socket.
fd_t s;

View File

@ -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;