Skip to content

Commit

Permalink
api: introduce NNG 2.0 compatible nng_dialer_get_url and nng_listener…
Browse files Browse the repository at this point in the history
…_get_url

This is meant to help applications start adopting new interfaces before converting to NNG 2.0
  • Loading branch information
gdamore committed Jan 5, 2025
1 parent 0260bd8 commit 561fc87
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 3 deletions.
5 changes: 5 additions & 0 deletions docs/man/nng_dialer_get.3.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ int nng_dialer_get_string(nng_dialer d, const char *opt, char **strp);
int nng_dialer_get_uint64(nng_dialer d, const char *opt, uint64_t *u64p);
int nng_dialer_get_url(nng_dialer d, const nng_url **url);
----

== DESCRIPTION
Expand Down Expand Up @@ -117,6 +119,9 @@ referenced by _u64p_.
This is typically used for options related to identifiers, network
numbers, and similar.

`nng_dialer_get_url()`::
This function is used to obtain the URL that dialer is configured for.

== RETURN VALUES

These functions returns 0 on success, and non-zero otherwise.
Expand Down
11 changes: 10 additions & 1 deletion docs/man/nng_listener_get.3.adoc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
= nng_listener_get(3)
//
// Copyright 2020 Staysail Systems, Inc. <[email protected]>
// Copyright 2025 Staysail Systems, Inc. <[email protected]>
// Copyright 2018 Capitar IT Group BV <[email protected]>
//
// This document is supplied under the terms of the MIT License, a
Expand Down Expand Up @@ -37,6 +37,8 @@ int nng_listener_get_string(nng_listener l, const char *opt, char **strp);
int nng_listener_get_uint64(nng_listener l, const char *opt, uint64_t *u64p);
int nng_listener_get_url(nng_listener l, const nng_url **url);
----

== DESCRIPTION
Expand Down Expand Up @@ -115,6 +117,13 @@ referenced by _u64p_.
This is typically used for options related to identifiers, network
numbers, and similar.

`nng_listener_get_url()`::
This function is used to obtain the URL that listener is configured for.
This is the actual URL, and may different that what was requested.
For example, when using TCP, if port 0 was requested, but then the listener
was started, a real port will have been chosen by the operating system.
This URL will reflect that final port, so it can be used for dialing.

== RETURN VALUES

These functions return 0 on success, and non-zero otherwise.
Expand Down
4 changes: 4 additions & 0 deletions include/nng/nng.h
Original file line number Diff line number Diff line change
Expand Up @@ -1706,6 +1706,10 @@ NNG_DECL void nng_cv_wake(nng_cv *);
// that no waiter starves forever.
NNG_DECL void nng_cv_wake1(nng_cv *);

// New URL accessors for endpoints - from NNG 2.0.
NNG_DECL int nng_dialer_get_url(nng_dialer, const nng_url **);
NNG_DECL int nng_listener_get_url(nng_listener, const nng_url **);

#ifdef __cplusplus
}
#endif
Expand Down
6 changes: 6 additions & 0 deletions src/core/dialer.c
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,12 @@ nni_dialer_getopt(
return (nni_sock_getopt(d->d_sock, name, valp, szp, t));
}

const nng_url *
nni_dialer_url(nni_dialer *d)

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

View check run for this annotation

Codecov / codecov/patch

src/core/dialer.c#L562

Added line #L562 was not covered by tests
{
return (d->d_url);

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

View check run for this annotation

Codecov / codecov/patch

src/core/dialer.c#L564

Added line #L564 was not covered by tests
}

void
nni_dialer_add_stat(nni_dialer *d, nni_stat_item *item)
{
Expand Down
3 changes: 2 additions & 1 deletion src/core/dialer.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright 2021 Staysail Systems, Inc. <[email protected]>
// Copyright 2025 Staysail Systems, Inc. <[email protected]>
// Copyright 2018 Capitar IT Group BV <[email protected]>
// Copyright 2018 Devolutions <[email protected]>
//
Expand All @@ -25,6 +25,7 @@ extern int nni_dialer_setopt(
nni_dialer *, const char *, const void *, size_t, nni_type);
extern int nni_dialer_getopt(
nni_dialer *, const char *, void *, size_t *, nni_type);
extern const nng_url *nni_dialer_url(nni_dialer *);
extern void nni_dialer_add_stat(nni_dialer *, nni_stat_item *);
extern void nni_dialer_bump_error(nni_dialer *, int);

Expand Down
6 changes: 6 additions & 0 deletions src/core/listener.c
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,12 @@ nni_listener_getopt(
return (nni_sock_getopt(l->l_sock, name, val, szp, t));
}

const nng_url *
nni_listener_url(nni_listener *l)

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

View check run for this annotation

Codecov / codecov/patch

src/core/listener.c#L511

Added line #L511 was not covered by tests
{
return (l->l_url);

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

View check run for this annotation

Codecov / codecov/patch

src/core/listener.c#L513

Added line #L513 was not covered by tests
}

void
nni_listener_add_stat(nni_listener *l, nni_stat_item *item)
{
Expand Down
3 changes: 2 additions & 1 deletion src/core/listener.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright 2021 Staysail Systems, Inc. <[email protected]>
// Copyright 2025 Staysail Systems, Inc. <[email protected]>
// Copyright 2018 Capitar IT Group BV <[email protected]>
// Copyright 2018 Devolutions <[email protected]>
//
Expand All @@ -25,6 +25,7 @@ extern int nni_listener_setopt(
nni_listener *, const char *, const void *, size_t, nni_type);
extern int nni_listener_getopt(
nni_listener *, const char *, void *, size_t *, nni_type);
extern const nng_url *nni_listener_url(nni_listener *);
extern void nni_listener_add_stat(nni_listener *, nni_stat_item *);
extern void nni_listener_bump_error(nni_listener *, int);

Expand Down
27 changes: 27 additions & 0 deletions src/nng.c
Original file line number Diff line number Diff line change
Expand Up @@ -2314,3 +2314,30 @@ nng_udp_multicast_membership(nng_udp *udp, nng_sockaddr *sa, bool join)
return (
nni_plat_udp_multicast_membership((nni_plat_udp *) udp, sa, join));
}


int
nng_dialer_get_url(nng_dialer id, const nng_url **urlp)

Check warning on line 2320 in src/nng.c

View check run for this annotation

Codecov / codecov/patch

src/nng.c#L2320

Added line #L2320 was not covered by tests
{
int rv;
nni_dialer *d;
if ((rv = nni_dialer_find(&d, id.id)) != 0) {
return (rv);

Check warning on line 2325 in src/nng.c

View check run for this annotation

Codecov / codecov/patch

src/nng.c#L2324-L2325

Added lines #L2324 - L2325 were not covered by tests
}
*urlp = nni_dialer_url(d);
nni_dialer_rele(d);
return (0);

Check warning on line 2329 in src/nng.c

View check run for this annotation

Codecov / codecov/patch

src/nng.c#L2327-L2329

Added lines #L2327 - L2329 were not covered by tests
}

int
nng_listener_get_url(nng_listener id, const nng_url **urlp)

Check warning on line 2333 in src/nng.c

View check run for this annotation

Codecov / codecov/patch

src/nng.c#L2333

Added line #L2333 was not covered by tests
{
int rv;
nni_listener *l;
if ((rv = nni_listener_find(&l, id.id)) != 0) {
return (rv);

Check warning on line 2338 in src/nng.c

View check run for this annotation

Codecov / codecov/patch

src/nng.c#L2337-L2338

Added lines #L2337 - L2338 were not covered by tests
}
*urlp = nni_listener_url(l);
nni_listener_rele(l);
return (0);

Check warning on line 2342 in src/nng.c

View check run for this annotation

Codecov / codecov/patch

src/nng.c#L2340-L2342

Added lines #L2340 - L2342 were not covered by tests
}

0 comments on commit 561fc87

Please sign in to comment.