Migrating from NNG 1.x
There are some incompatibities from NNG 1.x, and applications must make certain changes for NNG 2.0. This guide should help with this migration.
Nanomsg Compatibility
Applications using the legacy libnanomsg API will have to be updated to native NNG interfaces.
See the Migrating From libnanomsg chapter for details.
Library Initialization
It is now required for applications to initialize the library explicitly before using it.
This is done using the nng_init function.
Renamed Functions
The nng_close function has been renamed to nng_socket_close to make it clearer that
the object being closed is a socket. A compatible nng_close macro is available by defining NNG1_TRANSITION
in your compilation environment.
Removed Protocol Aliases
The following macro aliases are removed, unless NNG1_TRANSITION is defined in your compilation environment.
nng_bus_opennng_pair_opennng_pub_opennng_pull_opennng_push_opennng_rep_opennng_req_opennng_respondent_opennng_sub_opennng_surveyor_open
Just add either 0 or 1 (in the case of PAIRv1) to get the protocol desired. (Forcing the version number to
be supplied should avoid surprises later as new versions of protocols are added.)
Additionally, the header files for protocols are now empty, as all of their content has been moved to nng/nng.h.
Please remove #include references to protocol headers as we anticipate removing them in the future.
New AIO Error Code NNG_ESTOPPED
When an operation fails with NNG_ESTOPPED, it means that the associated [nni_aio] object has
been permanently stopped and must not be reused. Applications must watch for this error code, and
not resubmit an operation that returns it. This is particularly important for callbacks that automatically
resubmit operations. Failure to observe this rule will lead to an infinite loop
as any further operations on the object will fail immediately with NNG_ESTOPPED.
AIO Provider API changes
The API used for providers for asynchronous I/O operations has changed slightly.
- The
nng_aio_beginfunction is removed. However a newnng_aio_resetfunction should be called instead, before performing any other operations on an aio object. (This simply clears certain fields.) - The
nng_aio_deferfunction is replaced, with a verynng_aio_startfunction. However, this function has slightly different semantics. It will automatically call the callback if the operation cannot be scheduled. - Be aware of the new
NNG_ESTOPPEDerror code, for operations on a handle that is being torn down by the consumer.
Transport Specific Functions
Transports have not needed to be registered for a long time now, and the functions for doing so have been removed. These functions can be simply removed from your application:
nng_inproc_registernng_ipc_registernng_tls_registernng_tcp_registernng_ws_registernng_wss_registernng_zt_register
Additionally, the header files containing these functions have been removed, such as
nng/transport/ipc/ipc.h. Simply remove #include references to those files.
The NNG_OPT_WSS_REQUEST_HEADERS and NNG_OPT_WSS_RESPONSE_HEADERS aliases for
NNG_OPT_WS_OPT_WS_REQUEST_HEADERS and NNG_OPT_WS_RESPONSE_HEADERS have been removed.
Just convert any use of them to NNG_OPT_WS_REQUEST_HEADERS or
NNG_OPT_WS_RESPONSE_HEADERS as appropriate.
TLS Configuration
The support for configuring TLS via NNG_OPT_TLS_CONFIG, NNG_TLS_AUTH_MODE, NNG_OPT_TLS_CA_FILE,
NNG_OPT_TLS_SERVER_NAME, and similar has been removed.
Instead configuration must be performed by allocating
a nng_tls_config object, and then setting fields on it using the appropriate functions,
after which it may be configured on a listener or dialer using the nng_listener_set_tls
or nng_dialer_set_tls functions.
Likewise, when using the streams API, use the nng_stream_listener_set_tls or
nng_stream_dialer_set_tls functions.
Note that the declarations needed for TLS configuration are now available in <nng/nng.h>,
rather than the supplemental header.
Old TLS Versions Removed
Support for very old TLS versions 1.0 and 1.1 is removed.
Further, the NNG_TLS_1_0 and NNG_TLS_1_1 constants are also removed.
Applications should use NNG_TLS_1_2 or even NNG_TLS_1_3 instead.
Only One TLS Key/Cert Per Configuration
The ability to configure multiple keys and certificates for a given TLS configuration object is removed.
(The nng_tls_config_own_cert will return NNG_EBUSY if it has already been called for the configuration.)
The intended purpose was to support alternative cryptographic algorithms, but this is not necessary, was never
used, and was error prone.
Support for Local Addresses in Dial URLs Removed
NNG 1.x had an undocumented ability to specify the local address to bind
to when dialing, by using the local address in front of the destination
address separated by a semicolon. This was provided for legacy libnanomsg
compatilibility, and is no longer offered. The correct way to specify a
local address is by setting NNG_OPT_LOCADDR on the dialer.
Option Functions
The previously deprecated nng_pipe_getopt_xxx family of functions is removed.
Applications should use nng_pipe_get and related functions instead.
The socket option function families for nng_getopt and nng_setopt have been removed as well.
In this case, use the nng_socket_get and nng_socket_set functions as appropriate.
The _getopt and _setopt functions for contexts, listeners, and dialers are no longer
present. Simply changing _getopt to _get or _setopt to _set in the function name
should be sufficient in most cases.
The following functions served no useful purpose (after other changes described in this document), and are thus removed:
nng_ctx_get_stringnng_ctx_set_stringnng_dialer_get_ptrnng_dialer_set_ptrnng_listener_get_ptrnng_listener_set_ptrnng_socket_get_ptrnng_socket_set_ptrnng_socket_get_stringnng_socket_set_stringnng_stream_get_ptrnng_stream_set_ptrnng_stream_dialer_get_ptrnng_stream_dialer_set_ptrnng_stream_listener_get_ptrnng_stream_listener_set_ptrnng_ctx_get_ptr(not documented)nng_ctx_set_ptr(not documented)
Untyped Option Functions Removed
The following functions are removed. To access options, use a proper typed access function,
such as one ending in a suffix like _bool (to access a bool typed option).
nng_ctx_getnng_ctx_setnng_dialer_getnng_dialer_setnng_listener_getnng_listener_setnng_pipe_getnng_socket_getnng_socket_setnng_stream_getnng_stream_setnng_stream_dialer_getnng_stream_dialer_setnng_stream_listener_getnng_stream_listener_set
Stream Options
The ability to set options on streams after they have been created is no longer present.
(It turns out that this was not very useful.) All functions nng_stream_set_xxx are removed.
For tuning the NNG_OPT_TCP_NODELAY or similar properties, set the option on the listener
or dialer that creates the stream instead.
Transport Options
A number of transport options can no longer be set on the socket. Instead these
options must be set on the endpoint (dialer or listener) using the appropriate
nng_dialer_set or nng_listener_set option. This likely means that it is necessary
to allocate and configure the endpoint before attaching it to the socket. This will
also afford a much more fine-grained level of control over transport options.
The following options are copied from the socket when creating a dialer or listener, but afterwards will not be changed on the dialer or listener if the socket changes. It is recommended to set them properly on the socket before creating dialers or listeners, or set them explicitly on the dialer or listener directly:
NNG_OPT_RECONNMINTNNG_OPT_RECONNMAXTNNG_OPT_RECVMAXSZ
The latter option is a hint for transports and intended to facilitate early detection (and possibly avoidance of extra allocations) of oversize messages, before bringing them into the socket itself.
Socket Options
The NNG_OPT_PROTO, NNG_OPT_PROTONAME, NNG_OPT_PEER, and NNG_OPT_PEERNAME options
have been replaced by functions instead of options.
Use nng_socket_proto_id, nng_socket_peer_id, nng_socket_proto_name, and nng_socket_peer_name instead.
Note that the new functions provide a reference to a static string, and thus do not require
allocation, and the returned strings should not be freed. Also the IDs are provided as uint16_t,
matching the actual wire protocol values, instead of int.
The NNG_OPT_RAW option has aso been replaced by a function, nng_socket_raw.
The NNG_OPT_SENDFD and NNG_OPT_RECVFD options have been replaced by
nng_socket_get_send_poll_fd and nng_socket_get_recv_poll_fd respectively.
The NNG_OPT_SOCKNAME function is removed. This was provided for application use, and never used internally by NNG.
Applications should keep track of this information separately.
Subscriptions
The NNG_OPT_SUB_SUBSCRIBE and NNG_OPT_SUB_UNSUBCRIBE options have been replaced by
the following functions: nng_sub0_socket_subscribe, nng_sub0_socket_unsubscribe,
nng_sub0_ctx_subscribe and nng_sub0_ctx_unsubscribe. These functions, like the options
they replace, are only applicable to SUB sockets.
Statistics Use Constified Pointers
A number of the statistics functions take, or return, const nng_stat * instead
of plain nng_stat *. The ABI has not changed, but it may be necessary to declare
certain methods variables const to avoid warnings about misuse of const.
Wildcards Not Valid in URLs
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
access of the structure is no longer permitted. Intead new
accessors functions are provided:
u_schemeis replaced bynng_url_scheme.u_portis replaced bynng_url_port, but this returns auint16_t.u_hostnameis replaced bynng_url_hostname.u_pathis replaced bynng_url_path.u_queryis replaced bynng_url_query.u_fragmentis replaced bynng_url_fragment.u_userinfois replaced bynng_url_userinfo.u_requriis removed - it can be easily formulated from the other fields.u_hostis removed - usenng_url_hostnameandnng_url_portto construct if neededu_rawurlis removed - a “cooked” URL can be obtained from the newnng_url_sprintffunction.
HTTP API
A few limits on string lengths of certain values are now applied, which allows us to preallocate values and eliminate certain unreasonable error paths. If values longer than these are supplied in certain APIs they may be silently truncated to the limit:
- Hostnames are limited per RFC 1035 to 253 characters (not including terminating “.” or zero byte.)
- HTTP Method names are limited to 32 bytes (the longest IANA registered method is currently 18 bytes, used for WebDAV.)
- The fixed part of URI pathnames used with HTTP handlers is limited to 1024 bytes. (Longer URIs may be accepted
by using [
nng_http_handler_set_tree] and matching a parent of the directory component.)
The following API calls have changed so that they are void returns, and cannot fail.
They may silently truncate data.
- [
nng_http_req_set_method] - [
nng_http_res_set_status] - [
nng_http_handler_collect_body] - [
nng_http_handler_set_data] - [
nng_http_handler_set_host] - [
nng_http_handler_set_method] - [
nng_http_handler_set_tree] - [
nng_http_handler_set_tree_exclusive]
The HTTP handler objects may not be modified once in use. Previously this would fail with NNG_EBUSY.
These checks are removed now, but debug builds will assert if an application tries to do so.
Security Descriptors (Windows Only)
The NNG_OPT_IPC_SECURITY_DESCRIPTOR option is removed, and replaced
with the functions [nng_listener_get_security_descriptor] and
[nng_stream_listener_get_security_descriptor].
Security descriptor support is only relevant to Windows, and is presently only supported for IPC when Named Pipes are used. Planned future changes to switch to UNIX domain sockets may eliminate support for security descriptors altogether in NNG.
ZeroTier Support Removed
The Layer 2 special ZeroTier transport has been removed. It is possible to use NNG with ZeroTier using TCP/IP, and a future update is planned to provided coexistence between ZeroTier & the native stack’s TCP/IP using lwIP.