From 475f4c8abfdd9c88b585105c48839f51d7523d57 Mon Sep 17 00:00:00 2001 From: Garrett D'Amore Date: Tue, 31 Oct 2017 00:47:21 -0700 Subject: fixes #137 Remove public access to numeric protocols --- src/core/protocol.h | 25 +++++++++++++++++++++ src/nng.c | 30 ------------------------- src/nng.h | 51 ------------------------------------------- src/nng_compat.c | 27 ++++++++++++----------- src/protocol/bus/bus.c | 4 ++-- src/protocol/pair/pair_v0.c | 4 ++-- src/protocol/pair/pair_v1.c | 4 ++-- src/protocol/pipeline/pull.c | 4 ++-- src/protocol/pipeline/push.c | 6 ++--- src/protocol/pubsub/pub.c | 6 ++--- src/protocol/pubsub/sub.c | 4 ++-- src/protocol/reqrep/rep.c | 4 ++-- src/protocol/reqrep/req.c | 6 ++--- src/protocol/survey/respond.c | 4 ++-- src/protocol/survey/survey.c | 4 ++-- 15 files changed, 64 insertions(+), 119 deletions(-) (limited to 'src') diff --git a/src/core/protocol.h b/src/core/protocol.h index 253452d8..5db259f0 100644 --- a/src/core/protocol.h +++ b/src/core/protocol.h @@ -138,6 +138,31 @@ struct nni_proto { // not been initialized yet, this routine will do so. extern int nni_proto_open(nng_socket *, const nni_proto *); +// Protocol numbers. These are to be used with nng_socket_create(). +// These values are used on the wire, so must not be changed. The major +// number of the protocol is shifted left by 4 bits, and a subprotocol is +// assigned in the lower 4 bits. +// +// There are gaps in the list, which are obsolete or unsupported protocols. +// Protocol numbers are never more than 16 bits. Also, there will never be +// a valid protocol numbered 0 (NNG_PROTO_NONE). +#define NNI_PROTO(major, minor) (((major) *16) + (minor)) +enum nng_proto_enum { + NNI_PROTO_NONE = NNI_PROTO(0, 0), + NNI_PROTO_PAIR_V0 = NNI_PROTO(1, 0), + NNI_PROTO_PAIR_V1 = NNI_PROTO(1, 1), + NNI_PROTO_PUB_V0 = NNI_PROTO(2, 0), + NNI_PROTO_SUB_V0 = NNI_PROTO(2, 1), + NNI_PROTO_REQ_V0 = NNI_PROTO(3, 0), + NNI_PROTO_REP_V0 = NNI_PROTO(3, 1), + NNI_PROTO_PUSH_V0 = NNI_PROTO(5, 0), + NNI_PROTO_PULL_V0 = NNI_PROTO(5, 1), + NNI_PROTO_SURVEYOR_V0 = NNI_PROTO(6, 2), + NNI_PROTO_RESPONDENT_V0 = NNI_PROTO(6, 3), + NNI_PROTO_BUS_V0 = NNI_PROTO(7, 0), + NNI_PROTO_STAR_V0 = NNI_PROTO(100, 0), +}; + extern int nni_proto_sys_init(void); extern void nni_proto_sys_fini(void); diff --git a/src/nng.c b/src/nng.c index 3f868b30..db4a5c7c 100644 --- a/src/nng.c +++ b/src/nng.c @@ -51,36 +51,6 @@ nng_closeall(void) nni_sock_closeall(); } -uint16_t -nng_protocol(nng_socket sid) -{ - int rv; - uint16_t pnum; - nni_sock *sock; - - if ((rv = nni_sock_find(&sock, sid)) != 0) { - return (rv); - } - pnum = nni_sock_proto(sock); - nni_sock_rele(sock); - return (pnum); -} - -uint16_t -nng_peer(nng_socket sid) -{ - int rv; - uint16_t pnum; - nni_sock *sock; - - if ((rv = nni_sock_find(&sock, sid)) != 0) { - return (rv); - } - pnum = nni_sock_peer(sock); - nni_sock_rele(sock); - return (pnum); -} - void * nng_alloc(size_t sz) { diff --git a/src/nng.h b/src/nng.h index 9e147df3..c39ba9ee 100644 --- a/src/nng.h +++ b/src/nng.h @@ -77,12 +77,6 @@ NNG_DECL int nng_close(nng_socket); // a library; it will affect all sockets. NNG_DECL void nng_closeall(void); -// nng_protocol returns the protocol number of the socket. -NNG_DECL uint16_t nng_protocol(nng_socket); - -// nng_peer returns the protocol number for the socket's peer. -NNG_DECL uint16_t nng_peer(nng_socket); - // nng_setopt sets an option for a specific socket. NNG_DECL int nng_setopt(nng_socket, const char *, const void *, size_t); NNG_DECL int nng_setopt_int(nng_socket, const char *, int); @@ -326,13 +320,6 @@ NNG_DECL void nng_msg_set_pipe(nng_msg *, nng_pipe); NNG_DECL nng_pipe nng_msg_get_pipe(const nng_msg *); NNG_DECL int nng_msg_getopt(nng_msg *, int, void *, size_t *); -// Lookup an option by name. This returns either the option value, -// or -1 if the option name is unknown. -NNG_DECL int nng_option_lookup(const char *); - -// Lookup an option name by id. Returns NULL if not known. -NNG_DECL const char *nng_option_name(int); - // Pipe API. Generally pipes are only "observable" to applications, but // we do permit an application to close a pipe. This can be useful, for // example during a connection notification, to disconnect a pipe that @@ -350,44 +337,6 @@ enum nng_flag_enum { NNG_FLAG_NONBLOCK = 2, // Non-blocking operations. }; -// Protocol numbers. These are to be used with nng_socket_create(). -// These values are used on the wire, so must not be changed. The major -// number of the protocol is shifted left by 4 bits, and a subprotocol is -// assigned in the lower 4 bits. -// -// There are gaps in the list, which are obsolete or unsupported protocols. -// Protocol numbers are never more than 16 bits. Also, there will never be -// a valid protocol numbered 0 (NNG_PROTO_NONE). -#define NNG_PROTO(major, minor) (((major) *16) + (minor)) -enum nng_proto_enum { - NNG_PROTO_NONE = NNG_PROTO(0, 0), - NNG_PROTO_PAIR_V0 = NNG_PROTO(1, 0), - NNG_PROTO_PAIR_V1 = NNG_PROTO(1, 1), - NNG_PROTO_PUB_V0 = NNG_PROTO(2, 0), - NNG_PROTO_SUB_V0 = NNG_PROTO(2, 1), - NNG_PROTO_REQ_V0 = NNG_PROTO(3, 0), - NNG_PROTO_REP_V0 = NNG_PROTO(3, 1), - NNG_PROTO_PUSH_V0 = NNG_PROTO(5, 0), - NNG_PROTO_PULL_V0 = NNG_PROTO(5, 1), - NNG_PROTO_SURVEYOR_V0 = NNG_PROTO(6, 2), - NNG_PROTO_RESPONDENT_V0 = NNG_PROTO(6, 3), - NNG_PROTO_BUS_V0 = NNG_PROTO(7, 0), - NNG_PROTO_STAR_V0 = NNG_PROTO(100, 0), - - // "Default" names. Use the explicit version to guarantee - // backwards compatibility. - NNG_PROTO_BUS = NNG_PROTO_BUS_V0, - NNG_PROTO_PAIR = NNG_PROTO_PAIR_V1, - NNG_PROTO_SUB = NNG_PROTO_SUB_V0, - NNG_PROTO_PUB = NNG_PROTO_PUB_V0, - NNG_PROTO_REQ = NNG_PROTO_REQ_V0, - NNG_PROTO_REP = NNG_PROTO_REP_V0, - NNG_PROTO_PUSH = NNG_PROTO_PUSH_V0, - NNG_PROTO_PULL = NNG_PROTO_PULL_V0, - NNG_PROTO_SURVEYOR = NNG_PROTO_SURVEYOR_V0, - NNG_PROTO_RESPONDENT = NNG_PROTO_RESPONDENT_V0, -}; - // Builtin protocol socket constructors. NNG_DECL int nng_bus0_open(nng_socket *); NNG_DECL int nng_pair0_open(nng_socket *); diff --git a/src/nng_compat.c b/src/nng_compat.c index e9a3e3a3..f8a7ceb0 100644 --- a/src/nng_compat.c +++ b/src/nng_compat.c @@ -92,18 +92,19 @@ static const struct { uint16_t p_id; int (*p_open)(nng_socket *); } nn_protocols[] = { - { NNG_PROTO_BUS_V0, nng_bus0_open }, - { NNG_PROTO_PAIR_V0, nng_pair0_open }, - { NNG_PROTO_PAIR_V0, nng_pair1_open }, - { NNG_PROTO_PUSH_V0, nng_push0_open }, - { NNG_PROTO_PULL_V0, nng_pull0_open }, - { NNG_PROTO_PUB_V0, nng_pub0_open }, - { NNG_PROTO_SUB_V0, nng_sub0_open }, - { NNG_PROTO_REQ_V0, nng_req0_open }, - { NNG_PROTO_REP_V0, nng_rep0_open }, - { NNG_PROTO_SURVEYOR_V0, nng_surveyor0_open }, - { NNG_PROTO_RESPONDENT_V0, nng_respondent0_open }, - { NNG_PROTO_NONE, NULL }, + // clang-format off + { NN_BUS, nng_bus0_open }, + { NN_PAIR, nng_pair0_open }, + { NN_PUSH, nng_push0_open }, + { NN_PULL, nng_pull0_open }, + { NN_PUB, nng_pub0_open }, + { NN_SUB, nng_sub0_open }, + { NN_REQ, nng_req0_open }, + { NN_REP, nng_rep0_open }, + { NN_SURVEYOR, nng_surveyor0_open }, + { NN_RESPONDENT, nng_respondent0_open }, + { 0, NULL }, + // clang-format on }; int @@ -118,7 +119,7 @@ nn_socket(int domain, int protocol) return (-1); } - for (i = 0; nn_protocols[i].p_id != NNG_PROTO_NONE; i++) { + for (i = 0; nn_protocols[i].p_id != 0; i++) { if (nn_protocols[i].p_id == protocol) { break; } diff --git a/src/protocol/bus/bus.c b/src/protocol/bus/bus.c index 07f91602..6ec0066b 100644 --- a/src/protocol/bus/bus.c +++ b/src/protocol/bus/bus.c @@ -387,8 +387,8 @@ static nni_proto_sock_ops bus_sock_ops = { static nni_proto bus_proto = { .proto_version = NNI_PROTOCOL_VERSION, - .proto_self = { NNG_PROTO_BUS_V0, "bus" }, - .proto_peer = { NNG_PROTO_BUS_V0, "bus" }, + .proto_self = { NNI_PROTO_BUS_V0, "bus" }, + .proto_peer = { NNI_PROTO_BUS_V0, "bus" }, .proto_flags = NNI_PROTO_FLAG_SNDRCV, .proto_sock_ops = &bus_sock_ops, .proto_pipe_ops = &bus_pipe_ops, diff --git a/src/protocol/pair/pair_v0.c b/src/protocol/pair/pair_v0.c index 29f3b59c..93cd1497 100644 --- a/src/protocol/pair/pair_v0.c +++ b/src/protocol/pair/pair_v0.c @@ -287,8 +287,8 @@ static nni_proto_sock_ops pair0_sock_ops = { // Legacy protocol (v0) static nni_proto pair0_proto = { .proto_version = NNI_PROTOCOL_VERSION, - .proto_self = { NNG_PROTO_PAIR_V0, "pair" }, - .proto_peer = { NNG_PROTO_PAIR_V0, "pair" }, + .proto_self = { NNI_PROTO_PAIR_V0, "pair" }, + .proto_peer = { NNI_PROTO_PAIR_V0, "pair" }, .proto_flags = NNI_PROTO_FLAG_SNDRCV, .proto_sock_ops = &pair0_sock_ops, .proto_pipe_ops = &pair0_pipe_ops, diff --git a/src/protocol/pair/pair_v1.c b/src/protocol/pair/pair_v1.c index 86ee97eb..e14d06d5 100644 --- a/src/protocol/pair/pair_v1.c +++ b/src/protocol/pair/pair_v1.c @@ -499,8 +499,8 @@ static nni_proto_sock_ops pair1_sock_ops = { static nni_proto pair1_proto = { .proto_version = NNI_PROTOCOL_VERSION, - .proto_self = { NNG_PROTO_PAIR_V1, "pair1" }, - .proto_peer = { NNG_PROTO_PAIR_V1, "pair1" }, + .proto_self = { NNI_PROTO_PAIR_V1, "pair1" }, + .proto_peer = { NNI_PROTO_PAIR_V1, "pair1" }, .proto_flags = NNI_PROTO_FLAG_SNDRCV, .proto_sock_ops = &pair1_sock_ops, .proto_pipe_ops = &pair1_pipe_ops, diff --git a/src/protocol/pipeline/pull.c b/src/protocol/pipeline/pull.c index 267352c5..9685f0a1 100644 --- a/src/protocol/pipeline/pull.c +++ b/src/protocol/pipeline/pull.c @@ -228,8 +228,8 @@ static nni_proto_sock_ops pull_sock_ops = { static nni_proto pull_proto = { .proto_version = NNI_PROTOCOL_VERSION, - .proto_self = { NNG_PROTO_PULL_V0, "pull" }, - .proto_peer = { NNG_PROTO_PUSH_V0, "push" }, + .proto_self = { NNI_PROTO_PULL_V0, "pull" }, + .proto_peer = { NNI_PROTO_PUSH_V0, "push" }, .proto_flags = NNI_PROTO_FLAG_RCV, .proto_pipe_ops = &pull_pipe_ops, .proto_sock_ops = &pull_sock_ops, diff --git a/src/protocol/pipeline/push.c b/src/protocol/pipeline/push.c index 995ac56d..9ff74558 100644 --- a/src/protocol/pipeline/push.c +++ b/src/protocol/pipeline/push.c @@ -114,7 +114,7 @@ push_pipe_start(void *arg) push_pipe *p = arg; push_sock *s = p->push; - if (nni_pipe_peer(p->pipe) != NNG_PROTO_PULL) { + if (nni_pipe_peer(p->pipe) != NNI_PROTO_PULL_V0) { return (NNG_EPROTO); } @@ -245,8 +245,8 @@ static nni_proto_sock_ops push_sock_ops = { static nni_proto push_proto = { .proto_version = NNI_PROTOCOL_VERSION, - .proto_self = { NNG_PROTO_PUSH_V0, "push" }, - .proto_peer = { NNG_PROTO_PULL_V0, "pull" }, + .proto_self = { NNI_PROTO_PUSH_V0, "push" }, + .proto_peer = { NNI_PROTO_PULL_V0, "pull" }, .proto_flags = NNI_PROTO_FLAG_SND, .proto_pipe_ops = &push_pipe_ops, .proto_sock_ops = &push_sock_ops, diff --git a/src/protocol/pubsub/pub.c b/src/protocol/pubsub/pub.c index 29863322..9e5cd67f 100644 --- a/src/protocol/pubsub/pub.c +++ b/src/protocol/pubsub/pub.c @@ -142,7 +142,7 @@ pub_pipe_start(void *arg) pub_pipe *p = arg; pub_sock *s = p->pub; - if (nni_pipe_peer(p->pipe) != NNG_PROTO_SUB) { + if (nni_pipe_peer(p->pipe) != NNI_PROTO_SUB_V0) { return (NNG_EPROTO); } nni_mtx_lock(&s->mtx); @@ -321,8 +321,8 @@ static nni_proto_sock_ops pub_sock_ops = { static nni_proto pub_proto = { .proto_version = NNI_PROTOCOL_VERSION, - .proto_self = { NNG_PROTO_PUB_V0, "pub" }, - .proto_peer = { NNG_PROTO_SUB_V0, "sub" }, + .proto_self = { NNI_PROTO_PUB_V0, "pub" }, + .proto_peer = { NNI_PROTO_SUB_V0, "sub" }, .proto_flags = NNI_PROTO_FLAG_SND, .proto_sock_ops = &pub_sock_ops, .proto_pipe_ops = &pub_pipe_ops, diff --git a/src/protocol/pubsub/sub.c b/src/protocol/pubsub/sub.c index d87b42ec..555d528e 100644 --- a/src/protocol/pubsub/sub.c +++ b/src/protocol/pubsub/sub.c @@ -384,8 +384,8 @@ static nni_proto_sock_ops sub_sock_ops = { static nni_proto sub_proto = { .proto_version = NNI_PROTOCOL_VERSION, - .proto_self = { NNG_PROTO_SUB_V0, "sub" }, - .proto_peer = { NNG_PROTO_PUB_V0, "pub" }, + .proto_self = { NNI_PROTO_SUB_V0, "sub" }, + .proto_peer = { NNI_PROTO_PUB_V0, "pub" }, .proto_flags = NNI_PROTO_FLAG_RCV, .proto_sock_ops = &sub_sock_ops, .proto_pipe_ops = &sub_pipe_ops, diff --git a/src/protocol/reqrep/rep.c b/src/protocol/reqrep/rep.c index 5d924e32..100e739d 100644 --- a/src/protocol/reqrep/rep.c +++ b/src/protocol/reqrep/rep.c @@ -492,8 +492,8 @@ static nni_proto_sock_ops rep_sock_ops = { static nni_proto nni_rep_proto = { .proto_version = NNI_PROTOCOL_VERSION, - .proto_self = { NNG_PROTO_REP_V0, "rep" }, - .proto_peer = { NNG_PROTO_REQ_V0, "req" }, + .proto_self = { NNI_PROTO_REP_V0, "rep" }, + .proto_peer = { NNI_PROTO_REQ_V0, "req" }, .proto_flags = NNI_PROTO_FLAG_SNDRCV, .proto_sock_ops = &rep_sock_ops, .proto_pipe_ops = &rep_pipe_ops, diff --git a/src/protocol/reqrep/req.c b/src/protocol/reqrep/req.c index 1ab49f3e..bead1ec4 100644 --- a/src/protocol/reqrep/req.c +++ b/src/protocol/reqrep/req.c @@ -185,7 +185,7 @@ req_pipe_start(void *arg) req_pipe *p = arg; req_sock *s = p->req; - if (nni_pipe_peer(p->pipe) != NNG_PROTO_REP) { + if (nni_pipe_peer(p->pipe) != NNI_PROTO_REP_V0) { return (NNG_EPROTO); } @@ -654,8 +654,8 @@ static nni_proto_sock_ops req_sock_ops = { static nni_proto req_proto = { .proto_version = NNI_PROTOCOL_VERSION, - .proto_self = { NNG_PROTO_REQ_V0, "req" }, - .proto_peer = { NNG_PROTO_REP_V0, "rep" }, + .proto_self = { NNI_PROTO_REQ_V0, "req" }, + .proto_peer = { NNI_PROTO_REP_V0, "rep" }, .proto_flags = NNI_PROTO_FLAG_SNDRCV, .proto_sock_ops = &req_sock_ops, .proto_pipe_ops = &req_pipe_ops, diff --git a/src/protocol/survey/respond.c b/src/protocol/survey/respond.c index dbce0751..94730be6 100644 --- a/src/protocol/survey/respond.c +++ b/src/protocol/survey/respond.c @@ -486,8 +486,8 @@ static nni_proto_sock_ops resp_sock_ops = { static nni_proto resp_proto = { .proto_version = NNI_PROTOCOL_VERSION, - .proto_self = { NNG_PROTO_RESPONDENT_V0, "respondent" }, - .proto_peer = { NNG_PROTO_SURVEYOR_V0, "surveyor" }, + .proto_self = { NNI_PROTO_RESPONDENT_V0, "respondent" }, + .proto_peer = { NNI_PROTO_SURVEYOR_V0, "surveyor" }, .proto_flags = NNI_PROTO_FLAG_SNDRCV, .proto_sock_ops = &resp_sock_ops, .proto_pipe_ops = &resp_pipe_ops, diff --git a/src/protocol/survey/survey.c b/src/protocol/survey/survey.c index f44cd63a..9dcd6664 100644 --- a/src/protocol/survey/survey.c +++ b/src/protocol/survey/survey.c @@ -461,8 +461,8 @@ static nni_proto_sock_ops surv_sock_ops = { static nni_proto surv_proto = { .proto_version = NNI_PROTOCOL_VERSION, - .proto_self = { NNG_PROTO_SURVEYOR_V0, "surveyor" }, - .proto_peer = { NNG_PROTO_RESPONDENT_V0, "respondent" }, + .proto_self = { NNI_PROTO_SURVEYOR_V0, "surveyor" }, + .proto_peer = { NNI_PROTO_RESPONDENT_V0, "respondent" }, .proto_flags = NNI_PROTO_FLAG_SNDRCV, .proto_sock_ops = &surv_sock_ops, .proto_pipe_ops = &surv_pipe_ops, -- cgit v1.2.3-70-g09d2