aboutsummaryrefslogtreecommitdiff
path: root/src/platform/posix/posix_socket.c
diff options
context:
space:
mode:
authorGarrett D'Amore <garrett@damore.org>2017-06-29 22:44:03 -0700
committerGarrett D'Amore <garrett@damore.org>2017-06-29 22:44:03 -0700
commitc4c4e96a56863bfd365df18495d5cf6ab35cc880 (patch)
tree287066eddaffaf44785119fb8b80af644fb5759b /src/platform/posix/posix_socket.c
parent21378fe3372227a0e947d0c5bd4bdbcb54e82182 (diff)
downloadnng-c4c4e96a56863bfd365df18495d5cf6ab35cc880.tar.gz
nng-c4c4e96a56863bfd365df18495d5cf6ab35cc880.tar.bz2
nng-c4c4e96a56863bfd365df18495d5cf6ab35cc880.zip
Fixes for IPC: don't try to disable Nagle, and use SUN_LEN properly.
Diffstat (limited to 'src/platform/posix/posix_socket.c')
-rw-r--r--src/platform/posix/posix_socket.c39
1 files changed, 21 insertions, 18 deletions
diff --git a/src/platform/posix/posix_socket.c b/src/platform/posix/posix_socket.c
index 6aa17978..9dd3a274 100644
--- a/src/platform/posix/posix_socket.c
+++ b/src/platform/posix/posix_socket.c
@@ -46,6 +46,7 @@ struct nni_posix_sock {
int devnull; // for shutting down accept()
char * unlink; // path to unlink at unbind
nni_posix_pipedesc * pd;
+ int tcpnodelay;
};
int
@@ -97,17 +98,7 @@ nni_posix_to_sockaddr(struct sockaddr_storage *ss, const nni_sockaddr *sa)
sun->sun_family = PF_UNIX;
(void) snprintf(sun->sun_path, sizeof (sun->sun_path), "%s",
sa->s_un.s_path.sa_path);
- // Some systems (Linux!) have sun_len, while others do not.
- // The lack of a length field means systems without it cannot
- // use abstract sockets.
-#ifdef SUN_LEN
- sun->sun_len = SUN_LEN(sun);
- return (sun->sun_len);
-
-#else
return (sizeof (*sun));
-
-#endif
}
return (-1);
}
@@ -169,7 +160,7 @@ nni_posix_sock_aio_recv(nni_posix_sock *s, nni_aio *aio)
static void
-nni_posix_sock_setopts_fd(int fd)
+nni_posix_sock_setopts_fd(int fd, int tcpnodelay)
{
int one;
@@ -190,8 +181,11 @@ nni_posix_sock_setopts_fd(int fd)
// It's unclear whether this is safe for UNIX domain sockets. It
// *should* be.
- one = 1;
- (void) setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof (one));
+ if (tcpnodelay) {
+ one = 1;
+ (void) setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one,
+ sizeof (one));
+ }
}
@@ -258,15 +252,19 @@ nni_posix_sock_listen(nni_posix_sock *s, const nni_sockaddr *saddr)
if ((fd = socket(ss.ss_family, NNI_STREAM_SOCKTYPE, 0)) < 0) {
return (nni_plat_errno(errno));
}
+ if ((saddr->s_un.s_family == NNG_AF_INET) ||
+ (saddr->s_un.s_family == NNG_AF_INET6)) {
+ s->tcpnodelay = 1;
+ }
- nni_posix_sock_setopts_fd(fd);
+ nni_posix_sock_setopts_fd(fd, s->tcpnodelay);
// UNIX DOMAIN SOCKETS -- these have names in the file namespace.
// We are going to check to see if there was a name already there.
// If there was, and nothing is listening (ECONNREFUSED), then we
// will just try to cleanup the old socket. Note that this is not
// perfect in all scenarios, so use this with caution.
- if ((ss.ss_family == AF_UNIX) &&
+ if ((saddr->s_un.s_family == NNG_AF_IPC) &&
(saddr->s_un.s_path.sa_path[0] != 0)) {
int chkfd;
if ((chkfd = socket(AF_UNIX, NNI_STREAM_SOCKTYPE, 0)) < 0) {
@@ -276,7 +274,7 @@ nni_posix_sock_listen(nni_posix_sock *s, const nni_sockaddr *saddr)
// Nonblocking; we don't want to wait for remote server.
(void) fcntl(chkfd, F_SETFL, O_NONBLOCK);
- if (connect(chkfd, (struct sockaddr *) &ss, sizeof (ss)) < 0) {
+ if (connect(chkfd, (struct sockaddr *) &ss, len) < 0) {
if (errno == ECONNREFUSED) {
(void) unlink(saddr->s_un.s_path.sa_path);
}
@@ -435,7 +433,7 @@ nni_posix_sock_accept_sync(nni_posix_sock *s, nni_posix_sock *server)
}
}
- nni_posix_sock_setopts_fd(fd);
+ nni_posix_sock_setopts_fd(fd, s->tcpnodelay);
if ((rv = nni_posix_pipedesc_init(&s->pd, fd)) != 0) {
close(fd);
@@ -464,6 +462,11 @@ nni_posix_sock_connect_sync(nni_posix_sock *s, const nni_sockaddr *addr,
return (nni_plat_errno(errno));
}
+ if ((addr->s_un.s_family == NNG_AF_INET) ||
+ (addr->s_un.s_family == NNG_AF_INET6)) {
+ s->tcpnodelay = 1;
+ }
+
if (bindaddr != NULL) {
if (bindaddr->s_un.s_family != addr->s_un.s_family) {
return (NNG_EINVAL);
@@ -478,7 +481,7 @@ nni_posix_sock_connect_sync(nni_posix_sock *s, const nni_sockaddr *addr,
}
}
- nni_posix_sock_setopts_fd(fd);
+ nni_posix_sock_setopts_fd(fd, s->tcpnodelay);
if (connect(fd, (struct sockaddr *) &ss, len) != 0) {
rv = nni_plat_errno(errno);