diff options
Diffstat (limited to 'src/sp')
| -rw-r--r-- | src/sp/transport.c | 133 | ||||
| -rw-r--r-- | src/sp/transport.h | 31 | ||||
| -rw-r--r-- | src/sp/transport/inproc/inproc.c | 7 | ||||
| -rw-r--r-- | src/sp/transport/ipc/ipc.c | 16 | ||||
| -rw-r--r-- | src/sp/transport/tcp/tcp.c | 17 | ||||
| -rw-r--r-- | src/sp/transport/tls/tls.c | 17 | ||||
| -rw-r--r-- | src/sp/transport/ws/websocket.c | 27 | ||||
| -rw-r--r-- | src/sp/transport/zerotier/zerotier.c | 7 |
8 files changed, 69 insertions, 186 deletions
diff --git a/src/sp/transport.c b/src/sp/transport.c index 7f76af16..dba530e0 100644 --- a/src/sp/transport.c +++ b/src/sp/transport.c @@ -33,132 +33,67 @@ #include <stdio.h> #include <string.h> -// For now the list of transports is hard-wired. Adding new transports -// to the system dynamically is something that might be considered later. -extern nni_sp_tran nni_tcp_tran; -extern nni_sp_tran nni_ipc_tran; +static nni_list sp_tran_list; +static nni_rwlock sp_tran_lk; -typedef struct nni_sp_transport { - nni_sp_tran t_tran; - nni_list_node t_node; -} nni_sp_transport; - -static nni_list nni_sp_tran_list; -static nni_rwlock nni_sp_tran_lk; -static int nni_sp_tran_inited; - -int -nni_sp_tran_register(const nni_sp_tran *tran) +void +nni_sp_tran_register(nni_sp_tran *tran) { - nni_sp_transport *t; - int rv; - - // Its entirely possible that we are called before any sockets - // are opened. Make sure we are initialized. This has to be - // protected by a guard to prevent infinite recursion, since - // nni_init also winds up calling us. - if (!nni_sp_tran_inited) { - nni_init(); - } - - if (tran->tran_version != NNI_TRANSPORT_VERSION) { - return (NNG_ENOTSUP); - } - - nni_rwlock_wrlock(&nni_sp_tran_lk); - // Check to see if the transport is already registered... - NNI_LIST_FOREACH (&nni_sp_tran_list, t) { - if (strcmp(tran->tran_scheme, t->t_tran.tran_scheme) == 0) { - if (tran->tran_init == t->t_tran.tran_init) { - // duplicate. - nni_rwlock_unlock(&nni_sp_tran_lk); - return (0); - } - nni_rwlock_unlock(&nni_sp_tran_lk); - return (NNG_ESTATE); - } - } - if ((t = NNI_ALLOC_STRUCT(t)) == NULL) { - nni_rwlock_unlock(&nni_sp_tran_lk); - return (NNG_ENOMEM); - } - - t->t_tran = *tran; - if ((rv = t->t_tran.tran_init()) != 0) { - nni_rwlock_unlock(&nni_sp_tran_lk); - NNI_FREE_STRUCT(t); - return (rv); + nni_rwlock_wrlock(&sp_tran_lk); + if (!nni_list_node_active(&tran->tran_link)) { + tran->tran_init(); + nni_list_append(&sp_tran_list, tran); } - nni_list_append(&nni_sp_tran_list, t); - nni_rwlock_unlock(&nni_sp_tran_lk); - return (0); + nni_rwlock_unlock(&sp_tran_lk); } nni_sp_tran * nni_sp_tran_find(nni_url *url) { // address is of the form "<scheme>://blah..." - nni_sp_transport *t; + nni_sp_tran *t; - nni_rwlock_rdlock(&nni_sp_tran_lk); - NNI_LIST_FOREACH (&nni_sp_tran_list, t) { - if (strcmp(url->u_scheme, t->t_tran.tran_scheme) == 0) { - nni_rwlock_unlock(&nni_sp_tran_lk); - return (&t->t_tran); + nni_rwlock_rdlock(&sp_tran_lk); + NNI_LIST_FOREACH (&sp_tran_list, t) { + if (strcmp(url->u_scheme, t->tran_scheme) == 0) { + nni_rwlock_unlock(&sp_tran_lk); + return (t); } } - nni_rwlock_unlock(&nni_sp_tran_lk); + nni_rwlock_unlock(&sp_tran_lk); return (NULL); } // nni_sp_tran_sys_init initializes the entire transport subsystem, including // each individual transport. -typedef int (*nni_sp_tran_ctor)(void); +int +nni_sp_tran_sys_init(void) +{ + NNI_LIST_INIT(&sp_tran_list, nni_sp_tran, tran_link); + nni_rwlock_init(&sp_tran_lk); -// These are just the statically compiled in constructors. -// In the future we might want to support dynamic additions. -static nni_sp_tran_ctor nni_sp_tran_ctors[] = { #ifdef NNG_TRANSPORT_INPROC - nng_inproc_register, + nng_inproc_register(); #endif #ifdef NNG_TRANSPORT_IPC - nng_ipc_register, + nng_ipc_register(); #endif #ifdef NNG_TRANSPORT_TCP - nng_tcp_register, + nng_tcp_register(); #endif #ifdef NNG_TRANSPORT_TLS - nng_tls_register, + nng_tls_register(); #endif #ifdef NNG_TRANSPORT_WS - nng_ws_register, + nng_ws_register(); #endif #ifdef NNG_TRANSPORT_WSS - nng_wss_register, + nng_wss_register(); #endif #ifdef NNG_TRANSPORT_ZEROTIER - nng_zt_register, + nng_zt_register(); #endif - NULL, -}; - -int -nni_sp_tran_sys_init(void) -{ - int i; - - nni_sp_tran_inited = 1; - NNI_LIST_INIT(&nni_sp_tran_list, nni_sp_transport, t_node); - nni_rwlock_init(&nni_sp_tran_lk); - - for (i = 0; nni_sp_tran_ctors[i] != NULL; i++) { - int rv; - if ((rv = (nni_sp_tran_ctors[i]) ()) != 0) { - nni_sp_tran_sys_fini(); - return (rv); - } - } return (0); } @@ -167,13 +102,11 @@ nni_sp_tran_sys_init(void) void nni_sp_tran_sys_fini(void) { - nni_sp_transport *t; + nni_sp_tran *t; - while ((t = nni_list_first(&nni_sp_tran_list)) != NULL) { - nni_list_remove(&nni_sp_tran_list, t); - t->t_tran.tran_fini(); - NNI_FREE_STRUCT(t); + while ((t = nni_list_first(&sp_tran_list)) != NULL) { + nni_list_remove(&sp_tran_list, t); + t->tran_fini(); } - nni_rwlock_fini(&nni_sp_tran_lk); - nni_sp_tran_inited = 0; + nni_rwlock_fini(&sp_tran_lk); } diff --git a/src/sp/transport.h b/src/sp/transport.h index ae44ecc4..08f169bc 100644 --- a/src/sp/transport.h +++ b/src/sp/transport.h @@ -14,17 +14,6 @@ #include "core/options.h" -// We quite intentionally use a signature where the upper word is nonzero, -// which ensures that if we get garbage we will reject it. This is more -// likely to mismatch than all zero bytes would. The actual version is -// stored in the lower word; this is not semver -- the numbers are just -// increasing - we doubt it will increase more than a handful of times -// during the life of the project. If we add a new version, please keep -// the old version around -- it may be possible to automatically convert -// older versions in the future. -#define NNI_TRANSPORT_V8 0x54220008 -#define NNI_TRANSPORT_VERSION NNI_TRANSPORT_V8 - // Endpoint operations are called by the socket in a // protocol-independent fashion. The socket makes individual calls, // which are expected to block if appropriate (except for destroy), or @@ -153,10 +142,9 @@ struct nni_sp_pipe_ops { // Transport implementation details. Transports must implement the // interfaces in this file. struct nni_sp_tran { - // tran_version is the version of the transport ops that this - // transport implements. We only bother to version the main - // ops vector. - uint32_t tran_version; + // tran_link is for framework use only - it must initialized + // to zero before registration. + nni_list_node tran_link; // tran_scheme is the transport scheme, such as "tcp" or "inproc". const char *tran_scheme; @@ -170,20 +158,19 @@ struct nni_sp_tran { // tran_pipe links our pipe-specific operations. const nni_sp_pipe_ops *tran_pipe; - // tran_init, if not NULL, is called once during library - // initialization. - int (*tran_init)(void); + // tran_init is called once during library initialization. + void (*tran_init)(void); - // tran_fini, if not NULL, is called during library deinitialization. - // It should release any global resources, close any open files, etc. + // tran_fini is called during library shutdown. + // It should release any global resources. void (*tran_fini)(void); }; // These APIs are used by the framework internally, and not for use by // transport implementations. -extern nni_sp_tran *nni_sp_tran_find(nni_url *url); +extern nni_sp_tran *nni_sp_tran_find(nni_url *); extern int nni_sp_tran_sys_init(void); extern void nni_sp_tran_sys_fini(void); -extern int nni_sp_tran_register(const nni_sp_tran *tran); +extern void nni_sp_tran_register(nni_sp_tran *); #endif // PROTOCOL_SP_TRANSPORT_H diff --git a/src/sp/transport/inproc/inproc.c b/src/sp/transport/inproc/inproc.c index e9ac2e42..a67d6d18 100644 --- a/src/sp/transport/inproc/inproc.c +++ b/src/sp/transport/inproc/inproc.c @@ -67,13 +67,12 @@ struct inproc_ep { // which we use for coordinating rendezvous. static inproc_global nni_inproc; -static int +static void inproc_init(void) { NNI_LIST_INIT(&nni_inproc.servers, inproc_ep, node); nni_mtx_init(&nni_inproc.mx); - return (0); } static void @@ -676,7 +675,6 @@ static nni_sp_listener_ops inproc_listener_ops = { // This is the inproc transport linkage, and should be the only global // symbol in this entire file. struct nni_sp_tran nni_inproc_tran = { - .tran_version = NNI_TRANSPORT_VERSION, .tran_scheme = "inproc", .tran_dialer = &inproc_dialer_ops, .tran_listener = &inproc_listener_ops, @@ -688,5 +686,6 @@ struct nni_sp_tran nni_inproc_tran = { int nng_inproc_register(void) { - return (nni_sp_tran_register(&nni_inproc_tran)); + nni_sp_tran_register(&nni_inproc_tran); + return (0); } diff --git a/src/sp/transport/ipc/ipc.c b/src/sp/transport/ipc/ipc.c index ef597c5d..502943a5 100644 --- a/src/sp/transport/ipc/ipc.c +++ b/src/sp/transport/ipc/ipc.c @@ -93,10 +93,9 @@ static nni_reap_list ipc_pipe_reap_list = { .rl_func = ipc_pipe_fini, }; -static int +static void ipc_tran_init(void) { - return (0); } static void @@ -1119,7 +1118,6 @@ static nni_sp_listener_ops ipc_listener_ops = { }; static nni_sp_tran ipc_tran = { - .tran_version = NNI_TRANSPORT_VERSION, .tran_scheme = "ipc", .tran_dialer = &ipc_dialer_ops, .tran_listener = &ipc_listener_ops, @@ -1130,7 +1128,6 @@ static nni_sp_tran ipc_tran = { #ifdef NNG_PLATFORM_POSIX static nni_sp_tran ipc_tran_unix = { - .tran_version = NNI_TRANSPORT_VERSION, .tran_scheme = "unix", .tran_dialer = &ipc_dialer_ops, .tran_listener = &ipc_listener_ops, @@ -1142,7 +1139,6 @@ static nni_sp_tran ipc_tran_unix = { #ifdef NNG_HAVE_ABSTRACT_SOCKETS static nni_sp_tran ipc_tran_abstract = { - .tran_version = NNI_TRANSPORT_VERSION, .tran_scheme = "abstract", .tran_dialer = &ipc_dialer_ops, .tran_listener = &ipc_listener_ops, @@ -1155,17 +1151,13 @@ static nni_sp_tran ipc_tran_abstract = { int nng_ipc_register(void) { - int rv; - if (((rv = nni_sp_tran_register(&ipc_tran)) != 0) + nni_sp_tran_register(&ipc_tran); #ifdef NNG_PLATFORM_POSIX - || ((rv = nni_sp_tran_register(&ipc_tran_unix)) != 0) + nni_sp_tran_register(&ipc_tran_unix); #endif #ifdef NNG_HAVE_ABSTRACT_SOCKETS - || ((rv = nni_sp_tran_register(&ipc_tran_abstract)) != 0) + nni_sp_tran_register(&ipc_tran_abstract); #endif - ) { - return (rv); - } return (0); } diff --git a/src/sp/transport/tcp/tcp.c b/src/sp/transport/tcp/tcp.c index 0928929a..3aa20f38 100644 --- a/src/sp/transport/tcp/tcp.c +++ b/src/sp/transport/tcp/tcp.c @@ -92,10 +92,9 @@ static nni_reap_list tcptran_pipe_reap_list = { .rl_func = tcptran_pipe_fini, }; -static int +static void tcptran_init(void) { - return (0); } static void @@ -719,7 +718,7 @@ tcptran_url_parse_source(nng_url *url, nng_sockaddr *sa, const nng_url *surl) return (0); } - len = (size_t)(semi - url->u_hostname); + len = (size_t) (semi - url->u_hostname); url->u_hostname = semi + 1; if (strcmp(surl->u_scheme, "tcp") == 0) { @@ -1222,7 +1221,6 @@ static nni_sp_listener_ops tcptran_listener_ops = { }; static nni_sp_tran tcp_tran = { - .tran_version = NNI_TRANSPORT_VERSION, .tran_scheme = "tcp", .tran_dialer = &tcptran_dialer_ops, .tran_listener = &tcptran_listener_ops, @@ -1232,7 +1230,6 @@ static nni_sp_tran tcp_tran = { }; static nni_sp_tran tcp4_tran = { - .tran_version = NNI_TRANSPORT_VERSION, .tran_scheme = "tcp4", .tran_dialer = &tcptran_dialer_ops, .tran_listener = &tcptran_listener_ops, @@ -1242,7 +1239,6 @@ static nni_sp_tran tcp4_tran = { }; static nni_sp_tran tcp6_tran = { - .tran_version = NNI_TRANSPORT_VERSION, .tran_scheme = "tcp6", .tran_dialer = &tcptran_dialer_ops, .tran_listener = &tcptran_listener_ops, @@ -1254,11 +1250,8 @@ static nni_sp_tran tcp6_tran = { int nng_tcp_register(void) { - int rv; - if (((rv = nni_sp_tran_register(&tcp_tran)) != 0) || - ((rv = nni_sp_tran_register(&tcp4_tran)) != 0) || - ((rv = nni_sp_tran_register(&tcp6_tran)) != 0)) { - return (rv); - } + nni_sp_tran_register(&tcp_tran); + nni_sp_tran_register(&tcp4_tran); + nni_sp_tran_register(&tcp6_tran); return (0); } diff --git a/src/sp/transport/tls/tls.c b/src/sp/transport/tls/tls.c index 429440dc..91c0f1c9 100644 --- a/src/sp/transport/tls/tls.c +++ b/src/sp/transport/tls/tls.c @@ -99,10 +99,9 @@ static nni_reap_list tlstran_pipe_reap_list = { .rl_func = tlstran_pipe_fini, }; -static int +static void tlstran_init(void) { - return (0); } static void @@ -688,7 +687,7 @@ tlstran_url_parse_source(nni_url *url, nng_sockaddr *sa, const nni_url *surl) return (0); } - len = (size_t)(semi - url->u_hostname); + len = (size_t) (semi - url->u_hostname); url->u_hostname = semi + 1; if (strcmp(surl->u_scheme, "tls+tcp") == 0) { @@ -1251,7 +1250,6 @@ static nni_sp_listener_ops tlstran_listener_ops = { }; static nni_sp_tran tls_tran = { - .tran_version = NNI_TRANSPORT_VERSION, .tran_scheme = "tls+tcp", .tran_dialer = &tlstran_dialer_ops, .tran_listener = &tlstran_listener_ops, @@ -1261,7 +1259,6 @@ static nni_sp_tran tls_tran = { }; static nni_sp_tran tls4_tran = { - .tran_version = NNI_TRANSPORT_VERSION, .tran_scheme = "tls+tcp4", .tran_dialer = &tlstran_dialer_ops, .tran_listener = &tlstran_listener_ops, @@ -1271,7 +1268,6 @@ static nni_sp_tran tls4_tran = { }; static nni_sp_tran tls6_tran = { - .tran_version = NNI_TRANSPORT_VERSION, .tran_scheme = "tls+tcp6", .tran_dialer = &tlstran_dialer_ops, .tran_listener = &tlstran_listener_ops, @@ -1283,11 +1279,8 @@ static nni_sp_tran tls6_tran = { int nng_tls_register(void) { - int rv; - if (((rv = nni_sp_tran_register(&tls_tran)) != 0) || - ((rv = nni_sp_tran_register(&tls4_tran)) != 0) || - ((rv = nni_sp_tran_register(&tls6_tran)) != 0)) { - return (rv); - } + nni_sp_tran_register(&tls_tran); + nni_sp_tran_register(&tls4_tran); + nni_sp_tran_register(&tls6_tran); return (0); } diff --git a/src/sp/transport/ws/websocket.c b/src/sp/transport/ws/websocket.c index 63a54ce2..a3f2cd26 100644 --- a/src/sp/transport/ws/websocket.c +++ b/src/sp/transport/ws/websocket.c @@ -548,10 +548,9 @@ wstran_listener_init(void **lp, nng_url *url, nni_listener *listener) return (0); } -static int +static void wstran_init(void) { - return (0); } static void @@ -642,7 +641,6 @@ static nni_sp_listener_ops ws_listener_ops = { }; static nni_sp_tran ws_tran = { - .tran_version = NNI_TRANSPORT_VERSION, .tran_scheme = "ws", .tran_dialer = &ws_dialer_ops, .tran_listener = &ws_listener_ops, @@ -652,7 +650,6 @@ static nni_sp_tran ws_tran = { }; static nni_sp_tran ws4_tran = { - .tran_version = NNI_TRANSPORT_VERSION, .tran_scheme = "ws4", .tran_dialer = &ws_dialer_ops, .tran_listener = &ws_listener_ops, @@ -662,7 +659,6 @@ static nni_sp_tran ws4_tran = { }; static nni_sp_tran ws6_tran = { - .tran_version = NNI_TRANSPORT_VERSION, .tran_scheme = "ws6", .tran_dialer = &ws_dialer_ops, .tran_listener = &ws_listener_ops, @@ -674,12 +670,9 @@ static nni_sp_tran ws6_tran = { int nng_ws_register(void) { - int rv; - if (((rv = nni_sp_tran_register(&ws_tran)) != 0) || - ((rv = nni_sp_tran_register(&ws4_tran)) != 0) || - ((rv = nni_sp_tran_register(&ws6_tran)) != 0)) { - return (rv); - } + nni_sp_tran_register(&ws_tran); + nni_sp_tran_register(&ws4_tran); + nni_sp_tran_register(&ws6_tran); return (0); } @@ -687,7 +680,6 @@ nng_ws_register(void) #ifdef NNG_TRANSPORT_WSS static nni_sp_tran wss_tran = { - .tran_version = NNI_TRANSPORT_VERSION, .tran_scheme = "wss", .tran_dialer = &ws_dialer_ops, .tran_listener = &ws_listener_ops, @@ -697,7 +689,6 @@ static nni_sp_tran wss_tran = { }; static nni_sp_tran wss4_tran = { - .tran_version = NNI_TRANSPORT_VERSION, .tran_scheme = "wss4", .tran_dialer = &ws_dialer_ops, .tran_listener = &ws_listener_ops, @@ -707,7 +698,6 @@ static nni_sp_tran wss4_tran = { }; static nni_sp_tran wss6_tran = { - .tran_version = NNI_TRANSPORT_VERSION, .tran_scheme = "wss6", .tran_dialer = &ws_dialer_ops, .tran_listener = &ws_listener_ops, @@ -719,12 +709,9 @@ static nni_sp_tran wss6_tran = { int nng_wss_register(void) { - int rv; - if (((rv = nni_sp_tran_register(&wss_tran)) != 0) || - ((rv = nni_sp_tran_register(&wss4_tran)) != 0) || - ((rv = nni_sp_tran_register(&wss6_tran)) != 0)) { - return (rv); - } + nni_sp_tran_register(&wss_tran); + nni_sp_tran_register(&wss4_tran); + nni_sp_tran_register(&wss6_tran); return (0); } diff --git a/src/sp/transport/zerotier/zerotier.c b/src/sp/transport/zerotier/zerotier.c index 4e752020..15c0fe9f 100644 --- a/src/sp/transport/zerotier/zerotier.c +++ b/src/sp/transport/zerotier/zerotier.c @@ -1579,12 +1579,11 @@ done: return (0); } -static int +static void zt_tran_init(void) { nni_mtx_init(&zt_lk); NNI_LIST_INIT(&zt_nodes, zt_node, zn_link); - return (0); } static void @@ -3233,7 +3232,6 @@ static nni_tran_listener_ops zt_listener_ops = { // This is the ZeroTier transport linkage, and should be the // only global symbol in this entire file. static struct nni_tran zt_tran = { - .tran_version = NNI_TRANSPORT_VERSION, .tran_scheme = "zt", .tran_dialer = &zt_dialer_ops, .tran_listener = &zt_listener_ops, @@ -3245,5 +3243,6 @@ static struct nni_tran zt_tran = { int nng_zt_register(void) { - return (nni_tran_register(&zt_tran)); + nni_tran_register(&zt_tran); + return (0); } |
