Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gdamore/more opt url #1939

Merged
merged 18 commits into from
Nov 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions docs/ref/migrate/nng1.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,13 @@ The use of `*` to act as a wild card meaning all local interface addresses
is removed. The empty string already performs this function, and unlike
`*` is RFC compliant.

## URL Option Removed

The `NNG_OPT_URL` option has been removed.
It is replaced by the type safe [`nng_dialer_get_url`] and
[`nng_listener_get_url`] functions, which return an [`nng_url`]
structure instead of a string.

## URL Structure Changes

The details of [`nng_url`] have changed significantly, and direct
Expand Down
4 changes: 1 addition & 3 deletions docs/ref/tran/ipc.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ name in the file system where the socket or named pipe should be created.

> [!NOTE]
> When using relative paths on POSIX systems, the address used and returned
> in properties like `NNG_OPT_LOCADDR` and `NNG_OPT_URL` will also be relative.
> in properties like `NNG_OPT_LOCADDR` will also be relative.
> Consequently, they will only be interpreted the same by processes that have
> the same working directory.
> To ensure maximum portability and safety, absolute paths are recommended
Expand Down Expand Up @@ -84,7 +84,6 @@ where supported by the underlying platform.
- [`NNG_OPT_PEER_PID`][NNG_OPT_PEER_PID]
- [`NNG_OPT_PEER_UID`][NNG_OPT_PEER_UID]
- [`NNG_OPT_PEER_ZONEID`][NNG_OPT_PEER_ZONEID]
- [`NNG_OPT_URL`][NNG_OPT_URL]

[NNG_OPT_IPC_PERMISSIONS]: TODO.md
[NNG_OPT_IPC_SECURITY_DESCRIPTOR]: TODO.md
Expand All @@ -94,7 +93,6 @@ where supported by the underlying platform.
[NNG_OPT_PEER_PID]: TODO.md
[NNG_OPT_PEER_UID]: TODO.md
[NNG_OPT_PEER_ZONEID]: TODO.md
[NNG_OPT_URL]: TODO.md
[sockaddr]: TODO.md
[sockaddr_ipc]: TODO.md
[sockaddr_abstract]: TODO.md
1 change: 0 additions & 1 deletion docs/ref/tran/socket.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ Additionally, the following options may be supported on pipes when the platform
[nng_socket_pair]: ../../api/misc.md#create-socket-pair
[NNG_OPT_LOCADDR]: [TODO.md]
[NNG_OPT_REMADDR]: [TODO.md]
[NNG_OPT_URL]: [TODO.md]
[NNG_OPT_PEER_GID]: [TODO.md]
[NNG_OPT_PEER_PID]: [TODO.md]
[NNG_OPT_PEER_UID]: [TODO.md]
Expand Down
2 changes: 0 additions & 2 deletions docs/ref/tran/udp.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ where supported by the underlying platform.

- [`NNG_OPT_LOCADDR`][NNG_OPT_LOCADDR]
- [`NNG_OPT_REMADDR`][NNG_OPT_REMADDR]
- [`NNG_OPT_URL`][NNG_OPT_URL]

TODO: Document other options.

Expand Down Expand Up @@ -109,5 +108,4 @@ TODO: Document the tunables for this.
[nng_sockaddr_in6]: [TODO.md]
[NNG_OPT_LOCADDR]: [TODO.md]
[NNG_OPT_REMADDR]: [TODO.md]
[NNG_OPT_URL]: [TODO.md]
[NNG_OPT_RECVMAXSZ]: [TODO.md]
5 changes: 5 additions & 0 deletions include/nng/nng.h
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ NNG_DECL int nng_pipe_notify(nng_socket, nng_pipe_ev, nng_pipe_cb, void *);
// nn_bind(). The underlying endpoint is returned back to the caller in the
// endpoint pointer, if it is not NULL. The flags are ignored at present.
NNG_DECL int nng_listen(nng_socket, const char *, nng_listener *, int);
NNG_DECL int nng_listen_url(nng_socket, const nng_url *, nng_listener *, int);

// nng_dial creates a dialing endpoint, with no special options, and
// starts it dialing. Dialers have at most one active connection at a time
Expand All @@ -314,12 +315,16 @@ NNG_DECL int nng_listen(nng_socket, const char *, nng_listener *, int);
// case, it will still be reconnected in the background -- only the initial
// connection attempt is normally synchronous.)
NNG_DECL int nng_dial(nng_socket, const char *, nng_dialer *, int);
NNG_DECL int nng_dial_url(nng_socket, const nng_url *url, nng_dialer *, int);

// nng_dialer_create creates a new dialer, that is not yet started.
NNG_DECL int nng_dialer_create(nng_dialer *, nng_socket, const char *);
NNG_DECL int nng_dialer_create_url(nng_dialer *, nng_socket, const nng_url *);

// nng_listener_create creates a new listener, that is not yet started.
NNG_DECL int nng_listener_create(nng_listener *, nng_socket, const char *);
NNG_DECL int nng_listener_create_url(
nng_listener *, nng_socket, const nng_url *);

// nng_dialer_start starts the endpoint dialing. This is only possible if
// the dialer is not already dialing.
Expand Down
92 changes: 77 additions & 15 deletions src/core/dialer.c
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,80 @@
#endif
}

static int
nni_dialer_init(nni_dialer *d, nni_sock *s, nni_sp_tran *tran)
{
int rv;

d->d_closed = false;
d->d_data = NULL;
d->d_ref = 1;
d->d_sock = s;
d->d_tran = tran;
nni_atomic_flag_reset(&d->d_started);

// Make a copy of the endpoint operations. This allows us to
// modify them (to override NULLs for example), and avoids an extra
// dereference on hot paths.
d->d_ops = *tran->tran_dialer;

NNI_LIST_NODE_INIT(&d->d_node);
NNI_LIST_INIT(&d->d_pipes, nni_pipe, p_ep_node);

nni_mtx_init(&d->d_mtx);

nni_aio_init(&d->d_con_aio, dialer_connect_cb, d);
nni_aio_init(&d->d_tmo_aio, dialer_timer_cb, d);

nni_mtx_lock(&dialers_lk);
rv = nni_id_alloc32(&dialers, &d->d_id, d);
nni_mtx_unlock(&dialers_lk);

#ifdef NNG_ENABLE_STATS
dialer_stats_init(d);
#endif

if ((rv != 0) ||
((rv = d->d_ops.d_init(&d->d_data, &d->d_url, d)) != 0) ||
((rv = nni_sock_add_dialer(s, d)) != 0)) {
nni_mtx_lock(&dialers_lk);
nni_id_remove(&dialers, d->d_id);
nni_mtx_unlock(&dialers_lk);

Check warning on line 245 in src/core/dialer.c

View check run for this annotation

Codecov / codecov/patch

src/core/dialer.c#L243-L245

Added lines #L243 - L245 were not covered by tests
#ifdef NNG_ENABLE_STATS
nni_stat_unregister(&d->st_root);

Check warning on line 247 in src/core/dialer.c

View check run for this annotation

Codecov / codecov/patch

src/core/dialer.c#L247

Added line #L247 was not covered by tests
#endif
return (rv);

Check warning on line 249 in src/core/dialer.c

View check run for this annotation

Codecov / codecov/patch

src/core/dialer.c#L249

Added line #L249 was not covered by tests
}

return (0);
}

int
nni_dialer_create_url(nni_dialer **dp, nni_sock *s, const nng_url *url)
{
nni_sp_tran *tran;
nni_dialer *d;
int rv;

if (((tran = nni_sp_tran_find(nng_url_scheme(url))) == NULL) ||
(tran->tran_dialer == NULL)) {
return (NNG_ENOTSUP);

Check warning on line 264 in src/core/dialer.c

View check run for this annotation

Codecov / codecov/patch

src/core/dialer.c#L264

Added line #L264 was not covered by tests
}
if ((d = NNI_ALLOC_STRUCT(d)) == NULL) {
return (NNG_ENOMEM);

Check warning on line 267 in src/core/dialer.c

View check run for this annotation

Codecov / codecov/patch

src/core/dialer.c#L267

Added line #L267 was not covered by tests
}
if ((rv = nni_url_clone_inline(&d->d_url, url)) != 0) {
NNI_FREE_STRUCT(d);
return (rv);

Check warning on line 271 in src/core/dialer.c

View check run for this annotation

Codecov / codecov/patch

src/core/dialer.c#L270-L271

Added lines #L270 - L271 were not covered by tests
}
if ((rv = nni_dialer_init(d, s, tran)) != 0) {
nni_dialer_destroy(d);
return (rv);

Check warning on line 275 in src/core/dialer.c

View check run for this annotation

Codecov / codecov/patch

src/core/dialer.c#L274-L275

Added lines #L274 - L275 were not covered by tests
}
*dp = d;
return (0);
}

// nni_dialer_create creates a dialer on the socket.
// The caller should have a hold on the socket, and on success
// the dialer inherits the callers hold. (If the caller wants
Expand All @@ -216,16 +290,14 @@
nni_dialer *d;
int rv;

if ((d = NNI_ALLOC_STRUCT(d)) == NULL) {
return (NNG_ENOMEM);
}
if (((tran = nni_sp_tran_find(url_str)) == NULL) ||
(tran->tran_dialer == NULL)) {
NNI_FREE_STRUCT(d);
return (NNG_ENOTSUP);
}
if ((d = NNI_ALLOC_STRUCT(d)) == NULL) {
return (NNG_ENOMEM);

Check warning on line 298 in src/core/dialer.c

View check run for this annotation

Codecov / codecov/patch

src/core/dialer.c#L298

Added line #L298 was not covered by tests
}
if ((rv = nni_url_parse_inline(&d->d_url, url_str)) != 0) {
nni_url_fini(&d->d_url);
NNI_FREE_STRUCT(d);
return (rv);
}
Expand Down Expand Up @@ -455,9 +527,6 @@
{
nni_option *o;

if (strcmp(name, NNG_OPT_URL) == 0) {
return (NNG_EREADONLY);
}
if (strcmp(name, NNG_OPT_RECONNMAXT) == 0) {
int rv;
nni_mtx_lock(&d->d_mtx);
Expand Down Expand Up @@ -534,13 +603,6 @@
return (o->o_get(d->d_data, valp, szp, t));
}

// We provide a fallback on the URL, but let the implementation
// override. This allows the URL to be created with wildcards,
// that are resolved later.
if (strcmp(name, NNG_OPT_URL) == 0) {
return (nni_copyout_str(d->d_url.u_rawurl, valp, szp, t));
}

return (nni_sock_getopt(d->d_sock, name, valp, szp, t));
}

Expand Down
15 changes: 8 additions & 7 deletions src/core/dialer.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@
#ifndef CORE_DIALER_H
#define CORE_DIALER_H

extern int nni_dialer_find(nni_dialer **, uint32_t);
extern int nni_dialer_hold(nni_dialer *);
extern void nni_dialer_rele(nni_dialer *);
extern uint32_t nni_dialer_id(nni_dialer *);
extern int nni_dialer_create(nni_dialer **, nni_sock *, const char *);
extern void nni_dialer_close(nni_dialer *);
extern int nni_dialer_start(nni_dialer *, unsigned);
extern int nni_dialer_find(nni_dialer **, uint32_t);
extern int nni_dialer_hold(nni_dialer *);
extern void nni_dialer_rele(nni_dialer *);
extern uint32_t nni_dialer_id(nni_dialer *);
extern int nni_dialer_create(nni_dialer **, nni_sock *, const char *);
extern int nni_dialer_create_url(nni_dialer **, nni_sock *, const nng_url *);
extern void nni_dialer_close(nni_dialer *);
extern int nni_dialer_start(nni_dialer *, unsigned);
extern nni_sock *nni_dialer_sock(nni_dialer *);

extern int nni_dialer_setopt(
Expand Down
93 changes: 59 additions & 34 deletions src/core/listener.c
Original file line number Diff line number Diff line change
Expand Up @@ -192,31 +192,11 @@
#endif
}

// nni_listener_create creates a listener on the socket.
// The caller should have a hold on the socket, and on success
// the listener inherits the callers hold. (If the caller wants
// an additional hold, it should get an extra hold before calling this
// function.)
int
nni_listener_create(nni_listener **lp, nni_sock *s, const char *url_str)
static int
nni_listener_init(nni_listener *l, nni_sock *s, nni_sp_tran *tran)
{
nni_sp_tran *tran;
nni_listener *l;
int rv;
int rv;

if ((l = NNI_ALLOC_STRUCT(l)) == NULL) {
return (NNG_ENOMEM);
}
if (((tran = nni_sp_tran_find(url_str)) == NULL) ||
(tran->tran_listener == NULL)) {
NNI_FREE_STRUCT(l);
return (NNG_ENOTSUP);
}
if ((rv = nni_url_parse_inline(&l->l_url, url_str)) != 0) {
nni_url_fini(&l->l_url);
NNI_FREE_STRUCT(l);
return (rv);
}
l->l_closed = false;
l->l_data = NULL;
l->l_ref = 1;
Expand Down Expand Up @@ -252,6 +232,31 @@
#ifdef NNG_ENABLE_STATS
nni_stat_unregister(&l->st_root);
#endif
return (rv);
}

return (0);
}

int
nni_listener_create_url(nni_listener **lp, nni_sock *s, const nng_url *url)
{
nni_sp_tran *tran;
nni_listener *l;
int rv;

if (((tran = nni_sp_tran_find(nng_url_scheme(url))) == NULL) ||
(tran->tran_listener == NULL)) {
return (NNG_ENOTSUP);

Check warning on line 250 in src/core/listener.c

View check run for this annotation

Codecov / codecov/patch

src/core/listener.c#L250

Added line #L250 was not covered by tests
}
if ((l = NNI_ALLOC_STRUCT(l)) == NULL) {
return (NNG_ENOMEM);

Check warning on line 253 in src/core/listener.c

View check run for this annotation

Codecov / codecov/patch

src/core/listener.c#L253

Added line #L253 was not covered by tests
}
if ((rv = nni_url_clone_inline(&l->l_url, url)) != 0) {
NNI_FREE_STRUCT(l);
return (rv);

Check warning on line 257 in src/core/listener.c

View check run for this annotation

Codecov / codecov/patch

src/core/listener.c#L256-L257

Added lines #L256 - L257 were not covered by tests
}
if ((rv = nni_listener_init(l, s, tran)) != 0) {
nni_listener_destroy(l);
return (rv);
}
Expand All @@ -260,6 +265,37 @@
return (0);
}

// nni_listener_create creates a listener on the socket.
// The caller should have a hold on the socket, and on success
// the listener inherits the callers hold. (If the caller wants
// an additional hold, it should get an extra hold before calling this
// function.)
int
nni_listener_create(nni_listener **lp, nni_sock *s, const char *url_str)
{
nni_sp_tran *tran;
nni_listener *l;
int rv;

if (((tran = nni_sp_tran_find(url_str)) == NULL) ||
(tran->tran_listener == NULL)) {
return (NNG_ENOTSUP);
}
if ((l = NNI_ALLOC_STRUCT(l)) == NULL) {
return (NNG_ENOMEM);

Check warning on line 285 in src/core/listener.c

View check run for this annotation

Codecov / codecov/patch

src/core/listener.c#L285

Added line #L285 was not covered by tests
}
if ((rv = nni_url_parse_inline(&l->l_url, url_str)) != 0) {
NNI_FREE_STRUCT(l);
return (rv);

Check warning on line 289 in src/core/listener.c

View check run for this annotation

Codecov / codecov/patch

src/core/listener.c#L288-L289

Added lines #L288 - L289 were not covered by tests
}
if ((rv = nni_listener_init(l, s, tran)) != 0) {
nni_listener_destroy(l);
return (rv);
}
*lp = l;
return (0);
}

int
nni_listener_find(nni_listener **lp, uint32_t id)
{
Expand Down Expand Up @@ -431,10 +467,6 @@
{
nni_option *o;

if (strcmp(name, NNG_OPT_URL) == 0) {
return (NNG_EREADONLY);
}

if (l->l_ops.l_setopt != NULL) {
int rv = l->l_ops.l_setopt(l->l_data, name, val, sz, t);
if (rv != NNG_ENOTSUP) {
Expand Down Expand Up @@ -479,13 +511,6 @@
return (o->o_get(l->l_data, val, szp, t));
}

// We provide a fallback on the URL, but let the implementation
// override. This allows the URL to be created with wildcards,
// that are resolved later.
if (strcmp(name, NNG_OPT_URL) == 0) {
return (nni_copyout_str(l->l_url.u_rawurl, val, szp, t));
}

return (nni_sock_getopt(l->l_sock, name, val, szp, t));
}

Expand Down
8 changes: 5 additions & 3 deletions src/core/listener.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright 2021 Staysail Systems, Inc. <info@staysail.tech>
// Copyright 2024 Staysail Systems, Inc. <info@staysail.tech>
// Copyright 2018 Capitar IT Group BV <info@capitar.com>
// Copyright 2018 Devolutions <info@devolutions.net>
//
Expand All @@ -17,8 +17,10 @@ extern int nni_listener_hold(nni_listener *);
extern void nni_listener_rele(nni_listener *);
extern uint32_t nni_listener_id(nni_listener *);
extern int nni_listener_create(nni_listener **, nni_sock *, const char *);
extern void nni_listener_close(nni_listener *);
extern int nni_listener_start(nni_listener *, int);
extern int nni_listener_create_url(
nni_listener **, nni_sock *, const nng_url *);
extern void nni_listener_close(nni_listener *);
extern int nni_listener_start(nni_listener *, int);
extern nni_sock *nni_listener_sock(nni_listener *);

extern int nni_listener_setopt(
Expand Down
Loading
Loading