Skip to content

Commit

Permalink
fixes nanomsg#289 nng_sockaddr could just be a union
Browse files Browse the repository at this point in the history
fixes nanomsg#290 sockaddr improvements
  • Loading branch information
gdamore committed Mar 15, 2018
1 parent f4de6b5 commit 85ccb00
Show file tree
Hide file tree
Showing 25 changed files with 241 additions and 243 deletions.
10 changes: 5 additions & 5 deletions docs/man/nng_inproc.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ to slower transports when data must be moved within the same process.
This transport tries hard to avoid copying data, and thus is very
light-weight.

Registration
~~~~~~~~~~~~
=== Registration

The _inproc_ transport is generally built-in to the _nng_ core, so
no extra steps to use it should be necessary.
Expand All @@ -52,7 +51,8 @@ that URI.
=== Socket Address

When using an `nng_sockaddr` structure, the actual structure is of type
`struct nng_sockaddr_inproc`. This type has the following definition:
`struct nng_sockaddr_inproc`.
This type has the following definition:

[source,c]
--------
Expand All @@ -62,9 +62,9 @@ When using an `nng_sockaddr` structure, the actual structure is of type
typedef nng_sockaddr_inproc {
// <2>
uint16_t sa_family; // must be NNG_AF_INPROC
char sa_path[NNG_MAXADDRLEN]; // arbitrary "path"
char sa_name[NNG_MAXADDRLEN]; // arbitrary "name"
//
}
} nng_sockaddr_inproc;
--------
<1> The values of these macros may change, so applications
should avoid depending upon their values and instead use them symbolically.
Expand Down
14 changes: 7 additions & 7 deletions src/core/defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@

// These types are common but have names shared with user space.
// Internal code should use these names when possible.
typedef struct nng_msg nni_msg;
typedef struct nng_sockaddr nni_sockaddr;
typedef struct nng_event nni_event;
typedef struct nng_notify nni_notify;
typedef struct nng_url nni_url;
typedef struct nng_iov nni_iov;
typedef struct nng_aio nni_aio;
typedef nng_msg nni_msg;
typedef nng_sockaddr nni_sockaddr;
typedef nng_url nni_url;
typedef nng_iov nni_iov;
typedef nng_aio nni_aio;
typedef struct nng_event nni_event;
typedef struct nng_notify nni_notify;

// These are our own names.
typedef struct nni_socket nni_sock;
Expand Down
23 changes: 13 additions & 10 deletions src/nng.h
Original file line number Diff line number Diff line change
Expand Up @@ -619,13 +619,18 @@ enum nng_errno_enum {
// sockaddr, but we have our own to cope with our unique families, etc.
// The details of this structure are directly exposed to applications.
// These structures can be obtained via property lookups, etc.
struct nng_sockaddr_inproc {
uint16_t sa_family;
char sa_name[NNG_MAXADDRLEN];
};
typedef struct nng_sockaddr_inproc nng_sockaddr_inproc;

struct nng_sockaddr_path {
uint16_t sa_family;
char sa_path[NNG_MAXADDRLEN];
};
typedef struct nng_sockaddr_path nng_sockaddr_path;
typedef struct nng_sockaddr_path nng_sockaddr_ipc;
typedef struct nng_sockaddr_path nng_sockaddr_inproc;

struct nng_sockaddr_in6 {
uint16_t sa_family;
Expand Down Expand Up @@ -654,15 +659,13 @@ typedef struct nng_sockaddr_in nng_sockaddr_udp;
typedef struct nng_sockaddr_in nng_sockaddr_tcp;
typedef struct nng_sockaddr_zt nng_sockaddr_zt;

typedef struct nng_sockaddr {
union {
uint16_t s_family;
nng_sockaddr_path s_path;
nng_sockaddr_inproc s_inproc;
nng_sockaddr_in6 s_in6;
nng_sockaddr_in s_in;
nng_sockaddr_zt s_zt;
} s_un;
typedef union nng_sockaddr {
uint16_t s_family;
nng_sockaddr_ipc s_ipc;
nng_sockaddr_inproc s_inproc;
nng_sockaddr_in6 s_in6;
nng_sockaddr_in s_in;
nng_sockaddr_zt s_zt;
} nng_sockaddr;

enum nng_sockaddr_family {
Expand Down
3 changes: 1 addition & 2 deletions src/platform/posix/posix_ipc.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ nni_plat_ipc_ep_init(nni_plat_ipc_ep **epp, const nni_sockaddr *sa, int mode)
break;
case NNI_EP_MODE_LISTEN:

if ((rv = nni_plat_ipc_remove_stale(
sa->s_un.s_path.sa_path)) != 0) {
if ((rv = nni_plat_ipc_remove_stale(sa->s_ipc.sa_path)) != 0) {
return (rv);
}

Expand Down
21 changes: 10 additions & 11 deletions src/platform/posix/posix_resolv_gai.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,19 +171,18 @@ nni_posix_resolv_task(void *arg)

switch (probe->ai_addr->sa_family) {
case AF_INET:
rv = 0;
sin = (void *) probe->ai_addr;
sa->s_un.s_in.sa_family = NNG_AF_INET;
sa->s_un.s_in.sa_port = sin->sin_port;
sa->s_un.s_in.sa_addr = sin->sin_addr.s_addr;
rv = 0;
sin = (void *) probe->ai_addr;
sa->s_in.sa_family = NNG_AF_INET;
sa->s_in.sa_port = sin->sin_port;
sa->s_in.sa_addr = sin->sin_addr.s_addr;
break;
case AF_INET6:
rv = 0;
sin6 = (void *) probe->ai_addr;
sa->s_un.s_in6.sa_family = NNG_AF_INET6;
sa->s_un.s_in6.sa_port = sin6->sin6_port;
memcpy(sa->s_un.s_in6.sa_addr, sin6->sin6_addr.s6_addr,
16);
rv = 0;
sin6 = (void *) probe->ai_addr;
sa->s_in6.sa_family = NNG_AF_INET6;
sa->s_in6.sa_port = sin6->sin6_port;
memcpy(sa->s_in6.sa_addr, sin6->sin6_addr.s6_addr, 16);
break;
}
}
Expand Down
14 changes: 7 additions & 7 deletions src/platform/posix/posix_sockaddr.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ nni_posix_nn2sockaddr(void *sa, const nni_sockaddr *na)
if ((sa == NULL) || (na == NULL)) {
return (0);
}
switch (na->s_un.s_family) {
switch (na->s_family) {
case NNG_AF_INET:
sin = (void *) sa;
nsin = &na->s_un.s_in;
nsin = &na->s_in;
memset(sin, 0, sizeof(*sin));
sin->sin_family = PF_INET;
sin->sin_port = nsin->sa_port;
Expand All @@ -48,7 +48,7 @@ nni_posix_nn2sockaddr(void *sa, const nni_sockaddr *na)

case NNG_AF_INET6:
sin6 = (void *) sa;
nsin6 = &na->s_un.s_in6;
nsin6 = &na->s_in6;
memset(sin6, 0, sizeof(*sin6));
#ifdef SIN6_LEN
sin6->sin6_len = sizeof(*sin6);
Expand All @@ -60,7 +60,7 @@ nni_posix_nn2sockaddr(void *sa, const nni_sockaddr *na)

case NNG_AF_IPC:
spath = (void *) sa;
nspath = &na->s_un.s_path;
nspath = &na->s_ipc;
memset(spath, 0, sizeof(*spath));
// Make sure that the path fits!
sz = sizeof(spath->sun_path);
Expand Down Expand Up @@ -89,21 +89,21 @@ nni_posix_sockaddr2nn(nni_sockaddr *na, const void *sa)
switch (((struct sockaddr *) sa)->sa_family) {
case AF_INET:
sin = (void *) sa;
nsin = &na->s_un.s_in;
nsin = &na->s_in;
nsin->sa_family = NNG_AF_INET;
nsin->sa_port = sin->sin_port;
nsin->sa_addr = sin->sin_addr.s_addr;
break;
case AF_INET6:
sin6 = (void *) sa;
nsin6 = &na->s_un.s_in6;
nsin6 = &na->s_in6;
nsin6->sa_family = NNG_AF_INET6;
nsin6->sa_port = sin6->sin6_port;
memcpy(nsin6->sa_addr, sin6->sin6_addr.s6_addr, 16);
break;
case AF_UNIX:
spath = (void *) sa;
nspath = &na->s_un.s_path;
nspath = &na->s_ipc;
nspath->sa_family = NNG_AF_IPC;
(void) snprintf(nspath->sa_path, sizeof(nspath->sa_path), "%s",
spath->sun_path);
Expand Down
14 changes: 7 additions & 7 deletions src/platform/posix/posix_tcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ nni_plat_tcp_ep_init(nni_plat_tcp_ep **epp, const nni_sockaddr *lsa,
return (rv);
}

if ((rsa != NULL) && (rsa->s_un.s_family != NNG_AF_UNSPEC)) {
if ((rsa != NULL) && (rsa->s_family != NNG_AF_UNSPEC)) {
len = nni_posix_nn2sockaddr((void *) &ss, rsa);
nni_posix_epdesc_set_remote(ed, &ss, len);
}
if ((lsa != NULL) && (lsa->s_un.s_family != NNG_AF_UNSPEC)) {
if ((lsa != NULL) && (lsa->s_family != NNG_AF_UNSPEC)) {
len = nni_posix_nn2sockaddr((void *) &ss, lsa);
nni_posix_epdesc_set_local(ed, &ss, len);
}
Expand Down Expand Up @@ -130,15 +130,15 @@ nni_plat_tcp_ntop(const nni_sockaddr *sa, char *ipstr, char *portstr)
const void *ap;
uint16_t port;
int af;
switch (sa->s_un.s_family) {
switch (sa->s_family) {
case NNG_AF_INET:
ap = &sa->s_un.s_in.sa_addr;
port = sa->s_un.s_in.sa_port;
ap = &sa->s_in.sa_addr;
port = sa->s_in.sa_port;
af = AF_INET;
break;
case NNG_AF_INET6:
ap = &sa->s_un.s_in6.sa_addr;
port = sa->s_un.s_in6.sa_port;
ap = &sa->s_in6.sa_addr;
port = sa->s_in6.sa_port;
af = AF_INET6;
break;
default:
Expand Down
2 changes: 1 addition & 1 deletion src/platform/windows/win_ipc.c
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ nni_plat_ipc_ep_init(nni_plat_ipc_ep **epp, const nni_sockaddr *sa, int mode)
const char * path;
nni_plat_ipc_ep *ep;

path = sa->s_un.s_path.sa_path;
path = sa->s_ipc.sa_path;
if (nni_strnlen(path, NNG_MAXADDRLEN) >= NNG_MAXADDRLEN) {
return (NNG_EINVAL);
}
Expand Down
24 changes: 12 additions & 12 deletions src/platform/windows/win_resolv.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//
// Copyright 2017 Garrett D'Amore <[email protected]>
// Copyright 2018 Staysail Systems, Inc. <[email protected]>
// Copyright 2018 Capitar IT Group BV <[email protected]>
//
// This software is supplied under the terms of the MIT License, a
// copy of which should be located in the distribution where this
Expand Down Expand Up @@ -145,19 +146,18 @@ nni_win_resolv_task(void *arg)

switch (probe->ai_addr->sa_family) {
case AF_INET:
rv = 0;
sin = (void *) probe->ai_addr;
sa->s_un.s_in.sa_family = NNG_AF_INET;
sa->s_un.s_in.sa_port = sin->sin_port;
sa->s_un.s_in.sa_addr = sin->sin_addr.s_addr;
rv = 0;
sin = (void *) probe->ai_addr;
sa->s_in.sa_family = NNG_AF_INET;
sa->s_in.sa_port = sin->sin_port;
sa->s_in.sa_addr = sin->sin_addr.s_addr;
break;
case AF_INET6:
rv = 0;
sin6 = (void *) probe->ai_addr;
sa->s_un.s_in6.sa_family = NNG_AF_INET6;
sa->s_un.s_in6.sa_port = sin6->sin6_port;
memcpy(sa->s_un.s_in6.sa_addr, sin6->sin6_addr.s6_addr,
16);
rv = 0;
sin6 = (void *) probe->ai_addr;
sa->s_in6.sa_family = NNG_AF_INET6;
sa->s_in6.sa_port = sin6->sin6_port;
memcpy(sa->s_in6.sa_addr, sin6->sin6_addr.s6_addr, 16);
break;
}
}
Expand Down
30 changes: 15 additions & 15 deletions src/platform/windows/win_sockaddr.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// Copyright 2017 Garrett D'Amore <[email protected]>
// Copyright 2017 Capitar IT Group BV <[email protected]>
// Copyright 2018 Staysail Systems, Inc. <[email protected]>
// Copyright 2018 Capitar IT Group BV <[email protected]>
//
// This software is supplied under the terms of the MIT License, a
// copy of which should be located in the distribution where this
Expand All @@ -23,21 +23,21 @@ nni_win_nn2sockaddr(SOCKADDR_STORAGE *ss, const nni_sockaddr *sa)
if ((ss == NULL) || (sa == NULL)) {
return (-1);
}
switch (sa->s_un.s_family) {
switch (sa->s_family) {
case NNG_AF_INET:
sin = (void *) ss;
memset(sin, 0, sizeof(*sin));
sin->sin_family = PF_INET;
sin->sin_port = sa->s_un.s_in.sa_port;
sin->sin_addr.s_addr = sa->s_un.s_in.sa_addr;
sin->sin_port = sa->s_in.sa_port;
sin->sin_addr.s_addr = sa->s_in.sa_addr;
return (sizeof(*sin));

case NNG_AF_INET6:
sin6 = (void *) ss;
memset(sin6, 0, sizeof(*sin6));
sin6->sin6_family = PF_INET6;
sin6->sin6_port = sa->s_un.s_in6.sa_port;
memcpy(sin6->sin6_addr.s6_addr, sa->s_un.s_in6.sa_addr, 16);
sin6->sin6_port = sa->s_in6.sa_port;
memcpy(sin6->sin6_addr.s6_addr, sa->s_in6.sa_addr, 16);
return (sizeof(*sin6));
}
return (-1);
Expand All @@ -54,17 +54,17 @@ nni_win_sockaddr2nn(nni_sockaddr *sa, const SOCKADDR_STORAGE *ss)
}
switch (ss->ss_family) {
case PF_INET:
sin = (void *) ss;
sa->s_un.s_in.sa_family = NNG_AF_INET;
sa->s_un.s_in.sa_port = sin->sin_port;
sa->s_un.s_in.sa_addr = sin->sin_addr.s_addr;
sin = (void *) ss;
sa->s_in.sa_family = NNG_AF_INET;
sa->s_in.sa_port = sin->sin_port;
sa->s_in.sa_addr = sin->sin_addr.s_addr;
return (0);

case PF_INET6:
sin6 = (void *) ss;
sa->s_un.s_in6.sa_family = NNG_AF_INET6;
sa->s_un.s_in6.sa_port = sin6->sin6_port;
memcpy(sa->s_un.s_in6.sa_addr, sin6->sin6_addr.s6_addr, 16);
sin6 = (void *) ss;
sa->s_in6.sa_family = NNG_AF_INET6;
sa->s_in6.sa_port = sin6->sin6_port;
memcpy(sa->s_in6.sa_addr, sin6->sin6_addr.s6_addr, 16);
return (0);
}
return (-1);
Expand Down
14 changes: 7 additions & 7 deletions src/platform/windows/win_tcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -274,10 +274,10 @@ nni_plat_tcp_ep_init(nni_plat_tcp_ep **epp, const nni_sockaddr *lsa,

ep->s = INVALID_SOCKET;

if ((rsa != NULL) && (rsa->s_un.s_family != NNG_AF_UNSPEC)) {
if ((rsa != NULL) && (rsa->s_family != NNG_AF_UNSPEC)) {
ep->remlen = nni_win_nn2sockaddr(&ep->remaddr, rsa);
}
if ((lsa != NULL) && (lsa->s_un.s_family != NNG_AF_UNSPEC)) {
if ((lsa != NULL) && (lsa->s_family != NNG_AF_UNSPEC)) {
ep->loclen = nni_win_nn2sockaddr(&ep->locaddr, lsa);
}

Expand Down Expand Up @@ -659,15 +659,15 @@ nni_plat_tcp_ntop(const nni_sockaddr *sa, char *ipstr, char *portstr)
const void *ap;
uint16_t port;
int af;
switch (sa->s_un.s_family) {
switch (sa->s_family) {
case NNG_AF_INET:
ap = &sa->s_un.s_in.sa_addr;
port = sa->s_un.s_in.sa_port;
ap = &sa->s_in.sa_addr;
port = sa->s_in.sa_port;
af = AF_INET;
break;
case NNG_AF_INET6:
ap = &sa->s_un.s_in6.sa_addr;
port = sa->s_un.s_in6.sa_port;
ap = &sa->s_in6.sa_addr;
port = sa->s_in6.sa_port;
af = AF_INET6;
break;
default:
Expand Down
5 changes: 2 additions & 3 deletions src/transport/inproc/inproc.c
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,8 @@ nni_inproc_pipe_get_addr(void *arg, void *buf, size_t *szp)
nni_sockaddr sa;

memset(&sa, 0, sizeof(sa));
sa.s_un.s_inproc.sa_family = NNG_AF_INPROC;
nni_strlcpy(sa.s_un.s_inproc.sa_path, p->addr,
sizeof(sa.s_un.s_inproc.sa_path));
sa.s_inproc.sa_family = NNG_AF_INPROC;
nni_strlcpy(sa.s_inproc.sa_name, p->addr, sizeof(sa.s_inproc.sa_name));
return (nni_getopt_sockaddr(&sa, buf, szp));
}

Expand Down
Loading

0 comments on commit 85ccb00

Please sign in to comment.