Skip to content

Commit

Permalink
fixes #287 remove NNG_OPT_DOMAIN, NNG_OPT_PROTOCOL, and NNG_OPT_TRANS…
Browse files Browse the repository at this point in the history
…PORT

While here we documented that certain options are not supported in the
compatibility layer.
  • Loading branch information
gdamore committed Mar 18, 2018
1 parent 48bda13 commit 2e6f766
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 31 deletions.
45 changes: 45 additions & 0 deletions docs/man/nng_compat.3compat.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,51 @@ supplied here later.
In the meantime it can be found online at the
http://nanomsg.org[nanomsg site].

There are a few caveats, that should be kept in mind.

NOTE: Socket numbers can be quite large.
The legacy _libnanomsg_ attempted to reuse socket numbers, like
file descriptors in UNIX systems.
The _nng_ library avoids this to prevent accidental reuse or
collision after a descriptor is closed.
Consequently, socket numbers can become quite large, and should
probably not be used for array indices.

NOTE: The following options (`nn_getsockopt`) are unsupported:
`NN_PROTOCOL`, `NN_SNDPRIO`, `NN_RCVPRIO`, `NN_IPV4ONLY`.
Some of these will probably be added back in the future when
the relevant support is added to _nng_.

NOTE: Statistics (`nn_get_statistic`) are unsupported.
The plan is to support statistics in the native _nng_ API, but
we think there is no need for this in a compatibility layer.
Hence, this function returns `ENOTSUP`.

NOTE: Some transports can support longer URLs than legacy _libnanomsg_ can.
It is a good idea to use short pathnames in URLs if interoperability
is a concern.

NOTE: Some transports are unusable from this mode.
In particular, this legacy API offers no way to configure
TLS parameters that are required for use.

NOTE: ABI versioning is not supported.
We don't offer the `NN_VERSION_` macros. Sorry.

NOTE: Runtime symbol information is not implemented.
Specifically, there is no `nn_symbol()` function yet.
(This may be addressed later if there is a need.)

IMPORTANT: The `nn_term()` function is destructive and should be avoided.
This function closes down all sockets, and really there is no good
reason to ever use it.
Removal from existing code is advised.
(Keep track of sockets and close them explicitly if necessary.)

IMPORTANT: It *is* possible at present to intermix sockets between the new and
the old APIs, but this is not a guaranteed feature, and should only
be used temporarily to facilitate transitioning code to the new APIs.

// === Common Functions
//
// The following common functions exist in _libnng_.
Expand Down
21 changes: 20 additions & 1 deletion src/compat/nanomsg/nn.c
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,6 @@ static const struct {
{ NN_SOL_SOCKET, NN_MAXTTL, NNG_OPT_MAXTTL },
{ NN_SOL_SOCKET, NN_RCVTIMEO, NNG_OPT_RECVTIMEO },
{ NN_SOL_SOCKET, NN_SNDTIMEO, NNG_OPT_SENDTIMEO },
{ NN_SOL_SOCKET, NN_DOMAIN, NNG_OPT_DOMAIN },
{ NN_SOL_SOCKET, NN_SOCKET_NAME, NNG_OPT_SOCKNAME },
{ NN_REQ, NN_REQ_RESEND_IVL, NNG_OPT_REQ_RESENDTIME },
{ NN_SUB, NN_SUB_SUBSCRIBE, NNG_OPT_SUB_SUBSCRIBE },
Expand All @@ -622,6 +621,22 @@ static const struct {
// XXX: IPV4ONLY, SNDPRIO, RCVPRIO
};

static int
nn_getdomain(int s, void *valp, size_t *szp)
{
int i;
int rv;

if ((rv = nng_getopt_int((nng_socket) s, NNG_OPT_RAW, &i)) != 0) {
nn_seterror(rv);
return (-1);
}
i = i ? AF_SP_RAW : AF_SP;
memcpy(valp, &i, *szp < sizeof(int) ? *szp : sizeof(int));
*szp = sizeof(int);
return (0);
}

int
nn_getsockopt(int s, int nnlevel, int nnopt, void *valp, size_t *szp)
{
Expand All @@ -637,6 +652,10 @@ nn_getsockopt(int s, int nnlevel, int nnopt, void *valp, size_t *szp)
}

if (name == NULL) {
if (nnlevel == NN_SOL_SOCKET && nnopt == NN_DOMAIN) {
return (nn_getdomain(s, valp, szp));
}

errno = ENOPROTOOPT;
return (-1);
}
Expand Down
17 changes: 0 additions & 17 deletions src/core/socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ struct nni_socket {
nni_mtx s_mx;
nni_cv s_cv;
nni_cv s_close_cv;
int s_raw;

uint64_t s_id;
uint32_t s_flags;
Expand Down Expand Up @@ -249,12 +248,6 @@ nni_sock_setopt_sockname(nni_sock *s, const void *buf, size_t sz)
return (0);
}

static int
nni_sock_getopt_domain(nni_sock *s, void *buf, size_t *szp)
{
return (nni_getopt_int(s->s_raw + 1, buf, szp));
}

static const nni_socket_option nni_sock_options[] = {
{
.so_name = NNG_OPT_RECVTIMEO,
Expand Down Expand Up @@ -301,11 +294,6 @@ static const nni_socket_option nni_sock_options[] = {
.so_getopt = nni_sock_getopt_sockname,
.so_setopt = nni_sock_setopt_sockname,
},
{
.so_name = NNG_OPT_DOMAIN,
.so_getopt = nni_sock_getopt_domain,
.so_setopt = NULL,
},
// terminate list
{ NULL, NULL, NULL },
};
Expand Down Expand Up @@ -886,11 +874,6 @@ nni_sock_setopt(nni_sock *s, const char *name, const void *val, size_t size)
return (NNG_EREADONLY);
}
rv = pso->pso_setopt(s->s_data, val, size);
if ((rv == 0) && (strcmp(name, NNG_OPT_RAW) == 0) &&
(size >= sizeof(int))) {
// Save the raw option -- we use this for the DOMAIN.
memcpy(&s->s_raw, val, sizeof(int));
}
nni_mtx_unlock(&s->s_mx);
return (rv);
}
Expand Down
3 changes: 0 additions & 3 deletions src/nng.h
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,6 @@ enum nng_flag_enum {

// Options.
#define NNG_OPT_SOCKNAME "socket-name"
#define NNG_OPT_DOMAIN "compat:domain" // legacy compat only
#define NNG_OPT_RAW "raw"
#define NNG_OPT_LINGER "linger"
#define NNG_OPT_RECVBUF "recv-buffer"
Expand All @@ -421,8 +420,6 @@ enum nng_flag_enum {
#define NNG_OPT_REMADDR "remote-address"
#define NNG_OPT_URL "url"
#define NNG_OPT_MAXTTL "ttl-max"
#define NNG_OPT_PROTOCOL "protocol"
#define NNG_OPT_TRANSPORT "transport"
#define NNG_OPT_RECVMAXSZ "recv-size-max"
#define NNG_OPT_RECONNMINT "reconnect-time-min"
#define NNG_OPT_RECONNMAXT "reconnect-time-max"
Expand Down
16 changes: 6 additions & 10 deletions tests/sock.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,6 @@ TestMain("Socket Operations", {
NNG_EREADONLY);
So(nng_setopt(s1, NNG_OPT_LOCADDR, "a", 1) ==
NNG_EREADONLY);
So(nng_setopt_int(s1, NNG_OPT_DOMAIN, 3) ==
NNG_EREADONLY);
});

Convey("Sockname option works", {
Expand Down Expand Up @@ -121,15 +119,13 @@ TestMain("Socket Operations", {
So(strcmp(name, "hello") == 0);
});

Convey("Domain option works", {
int dom;
So(nng_getopt_int(s1, NNG_OPT_DOMAIN, &dom) ==
0);
So(dom == 1); // NN_AF_SP
Convey("RAW option works", {
int raw;
So(nng_getopt_int(s1, NNG_OPT_RAW, &raw) == 0);
So(raw == 0);
So(nng_setopt_int(s1, NNG_OPT_RAW, 1) == 0);
So(nng_getopt_int(s1, NNG_OPT_DOMAIN, &dom) ==
0);
So(dom == 2); // NN_AF_SP_RAW
So(nng_getopt_int(s1, NNG_OPT_RAW, &raw) == 0);
So(raw == 1);
});

Convey("URL option works", {
Expand Down

0 comments on commit 2e6f766

Please sign in to comment.