Reference

libnng(3)

nanomsg next generation library

NAME

libnng - nanomsg next generation library

SYNOPSIS

cc [flags] files -lnng [libraries]

DESCRIPTION

NNG provides a common messaging framework intended to solve common communication problems in distributed applications.

It provides a C language API.

Common Functions

The following common functions exist in libnng.

nng_allocallocate memory
nng_freefree memory
nng_strdupduplicate string
nng_strerrorreturn an error description
nng_strfreefree string
nng_versionreport library version

Socket Functions

The following functions operate on sockets.

nng_closeclose socket
nng_dialcreate and start dialer
nng_getoptget socket option
nng_listencreate and start listener
nng_recvreceive data
nng_sendsend data
nng_setoptset socket option (DEPRECATED)
nng_socket_getget socket option
nng_socket_idget numeric socket identifier
nng_socket_rawis the socket raw
nng_socket_proto_idget the socket protocol identifier
nng_socket_proto_nameget the socket protocol name
nng_socket_setset socket option
nng_sub_subscribemanage subscriptions

Connection Management

The following functions are used with either listeners, or dialers. Listeners accept incoming connection requests, and dialers make them.

nng_dialcreate and start dialer
nng_dialer_closeclose dialer
nng_dialer_createcreate dialer
nng_dialer_getget dialer option
nng_dialer_getoptget dialer option (DEPRECATED)
nng_dialer_idget numeric dialer identifier
nng_dialer_setset dialer option
nng_dialer_setoptset dialer option (DEPRECATED)
nng_dialer_startstart dialer
nng_listencreate and start listener
nng_listener_closeclose listener
nng_listener_createcreate listener
nng_listener_getget listener option
nng_listener_getoptget listener option (DEPRECATED)
nng_listener_idget numeric listener identifier
nng_listener_setset listener option
nng_listener_setoptset listener option (DEPRECATED)
nng_listener_startstart listener
nng_pipe_closeclose pipe
nng_pipe_dialerreturn dialer that created pipe
nng_pipe_getget pipe option
nng_pipe_getoptget pipe option (DEPRECATED)
nng_pipe_idget numeric pipe identifier
nng_pipe_listenerreturn listener that created pipe
nng_pipe_notifyregister pipe notification callback
nng_pipe_socketreturn owning socket for pipe

Message Handling Functions

Applications desiring to use the richest part of libnng will want to use the message API, where a message structure is passed between functions. This API provides the most power support for zero-copy.

Messages are divided into a header and body, where the body generally carries user-payload and the header carries protocol specific header information. Most applications will only interact with the body.

nng_msg_allocallocate a message
nng_msg_appendappend to message body
nng_msg_bodyreturn message body
nng_msg_capacityreturn capacity allocated for message body
nng_msg_chopremove data from end of message body
nng_msg_clearclear message body
nng_msg_dupduplicate a message
nng_msg_freefree a message
nng_msg_get_pipeget pipe for message
nng_msg_insertprepend to message body
nng_msg_lenreturn the message body length
nng_msg_reallocreallocate a message
nng_msg_reservereserve storage for message body
nng_msg_set_pipeset pipe for message
nng_msg_trimremove data from start of message body
nng_recvmsgreceive a message
nng_sendmsgsend a message

Message Header Handling

Tip

Few applications will need these functions, as message headers are only used to carry protocol-specific content. However, applications which use raw mode may need to access the header of messages.

nng_msg_headerreturn message header
nng_msg_header_appendappend to message header
nng_msg_header_chopremove data from end of message header
nng_msg_header_clearclear message header
nng_msg_header_insertprepend to message header
nng_msg_header_lenreturn the message header length
nng_msg_header_trimremove data from start of message header

Asynchronous Operations

Most applications will interact with NNG synchronously; that is that functions such as nng_send will block the calling thread until the operation has completed.

Note

Synchronous operations which send messages may return before the message has actually been received, or even transmitted. Instead, These functions return as soon as the message was successfully queued for delivery.

Asynchronous operations behave differently. These operations are initiated by the calling thread, but control returns immediately to the calling thread. When the operation is subsequently completed (regardless of whether this was successful or not), then a user supplied function is executed.

A context structure, an nng_aio, is allocated and associated with each asynchronous operation. Only a single asynchronous operation may be associated with an nng_aio at any time.

The following functions are used in the asynchronous model:

nng_aio_abortabort asynchronous I/O operation
nng_aio_allocallocate asynchronous I/O handle
nng_aio_beginbegin asynchronous I/O operation
nng_aio_busytest if asynchronous I/O is busy
nng_aio_cancelcancel asynchronous I/O operation
nng_aio_countreturn number of bytes transferred
nng_aio_deferdefer asynchronous I/O operation
nng_aio_finishfinish asynchronous I/O operation
nng_aio_freefree asynchronous I/O handle
nng_aio_get_inputreturn input parameter
nng_aio_get_msgget message from an asynchronous receive
nng_aio_get_outputreturn output result
nng_aio_reapreap asynchronous I/O handle
nng_aio_resultreturn result of asynchronous operation
nng_aio_set_inputset input parameter
nng_aio_set_iovset scatter/gather vector
nng_aio_set_msgset message for an asynchronous send
nng_aio_set_outputset output result
nng_aio_set_timeoutset asynchronous I/O timeout
nng_aio_stopstop asynchronous I/O operation
nng_aio_waitwait for asynchronous I/O operation
nng_recv_aioreceive message asynchronously
nng_send_aiosend message asynchronously
nng_sleep_aiosleep asynchronously

Protocols

The following functions are used to construct a socket with a specific protocol:

nng_bus_openopen a bus socket
nng_pair_openopen a pair socket
nng_pub_openopen a pub socket
nng_pull_openopen a pull socket
nng_push_openopen a push socket
nng_rep_openopen a rep socket
nng_req_openopen a req socket
nng_respondent_openopen a respondent socket
nng_sub_openopen a sub socket
nng_surveyor_openopen a surveyor socket

Transports

The following functions are used to register a transport for use. This is no longer necessary, and applications should not call these functions anymore.

nng_inproc_registerregister inproc transport (DEPRECATED)
nng_ipc_registerregister IPC transport (DEPRECATED)
nng_tcp_registerregister TCP transport (DEPRECATED)
nng_tls_registerregister TLS transport (DEPRECATED)
nng_ws_registerregister WebSocket transport (DEPRECATED)
nng_wss_registerregister WebSocket Secure transport (DEPRECATED)
nng_zt_registerregister ZeroTier transport (DEPRECATED)

Protocol Contexts

The following functions are useful to separate the protocol processing from a socket object, into a separate context. This can allow multiple contexts to be created on a single socket for concurrent applications.

nng_ctx_closeclose context
nng_ctx_getget context option
nng_ctx_getoptget context option (DEPRECATED)
nng_ctx_idget numeric context identifier
nng_ctx_opencreate context
nng_ctx_recvreceive message using context asynchronously
nng_ctx_recvmsgreceive a message using context
nng_ctx_sendsend message using context asynchronously
nng_ctx_sendmsgsend a message using context
nng_ctx_setset context option
nng_ctx_setoptset context option (DEPRECATED)

Devices, Relays

The following function family is used to create forwarders or relayers that route messages from one socket to another.

nng_devicemessage forwarding device

Statistics

The following functions provide access to statistics which can be used to observe program behaviors and as an aid in troubleshooting.

nng_stat_boolget statistic Boolean value
nng_stat_childget child statistic
nng_stat_nameget statistic description
nng_stat_findfind statistic by name
nng_stat_find_dialerfind dialer statistics
nng_stat_find_listenerfind listener statistics
nng_stat_find_socketfind socket statistics
nng_stat_nameget statistic name
nng_stat_nextget next statistic
nng_stat_stringget statistic string value
nng_stat_timestampget statistic timestamp
nng_stat_typeget statistic type
nng_stat_unitget statistic unit
nng_stat_valueget statistic numeric value
nng_stats_freefree statistics
nng_stats_getget statistics

URL Object

Common functionality is supplied for parsing and handling universal resource locators (URLS).

nng_url_cloneclone URL structure
nng_url_freefree URL structure
nng_url_parsecreate URL structure from string

Logging Support

Common functionality for message logging.

nng_loglog a message
nng_log_set_facilityset log facility
nng_log_set_levelset log level
nng_log_set_loggerset logging handler

Supplemental API

These supplemental functions are not intrinsic to building network applications with NNG, but they are made available as a convenience to aid in creating portable applications.

nng_clockget time
nng_cv_allocallocate condition variable
nng_cv_freefree condition variable
nng_cv_untilwait for condition or timeout
nng_cv_waitwait for condition
nng_cv_wakewake all waiters
nng_cv_wake1wake one waiter
nng_id_mapidentifier based mapping table
nng_msleepsleep for milliseconds
nng_mtx_allocallocate mutex
nng_mtx_freefree mutex
nng_mtx_locklock mutex
nng_mtx_unlockunlock mutex
nng_opts_parseparse command line options
nng_randomget random number
nng_socket_paircreate connected pair of BSD sockets
nng_thread_createcreate thread
nng_thread_destroyreap thread
nng_thread_set_nameset thread name

Byte Streams

These functions are available for use with byte streams. They are considered low-level, for uses where the higher level functions using Scalability Protocols are inappropriate.

Byte streams, represented by nng_stream objects, correspond to underlying connections such as TCP connections or named pipes. They are created by either nng_stream_dialer or nng_stream_listener objects.

nng_stream_closeclose byte stream
nng_stream_dialer_allocallocate byte stream dialer
nng_stream_dialer_closeclose byte stream dialer
nng_stream_dialer_dialinitiate outgoing byte stream
nng_stream_dialer_freefree byte stream dialer
nng_stream_dialer_getget option from byte stream dialer
nng_stream_dialer_setset option on byte stream dialer
nng_stream_freefree byte stream
nng_stream_getget option from byte stream
nng_stream_listener_acceptaccept incoming byte stream
nng_stream_listener_allocallocate byte stream listener
nng_stream_listener_closeclose byte stream listener
nng_stream_listener_freefree byte stream listener
nng_stream_listener_getget option from byte stream listener
nng_stream_listener_listenbind byte stream listener to address
nng_stream_listener_setset option on byte stream listener
nng_stream_recvreceive from byte stream
nng_stream_sendsend to byte stream
nng_stream_setset option on byte stream

HTTP Support

The library may be configured with support for HTTP, and this will be the case if WebSocket support is configured as well. In this case, it is possible to access functionality to support the creation of HTTP (and HTTP/S if TLS support is present) servers and clients.

Common HTTP Functions

The following functions are used to work with HTTP requests, responses, and connections.

nng_http_conn_closeclose HTTP connection
nng_http_conn_readread from HTTP connection
nng_http_conn_read_allread all from HTTP connection
nng_http_conn_read_reqread HTTP request
nng_http_conn_read_resread HTTP response
nng_http_conn_writewrite to HTTP connection
nng_http_conn_write_allwrite all to HTTP connection
nng_http_conn_write_reqwrite HTTP request
nng_http_conn_write_reswrite HTTP response
nng_http_req_add_headeradd HTTP request header
nng_http_req_allocallocate HTTP request structure
nng_http_req_copy_datacopy HTTP request body
nng_http_req_del_headerdelete HTTP request header
nng_http_req_freefree HTTP request structure
nng_http_req_get_dataget HTTP request body
nng_http_req_get_headerreturn HTTP request header
nng_http_req_get_methodreturn HTTP request method
nng_http_req_get_urireturn HTTP request URI
nng_http_req_get_versionreturn HTTP request protocol version
nng_http_req_resetreset HTTP request structure
nng_http_req_set_dataset HTTP request body
nng_http_req_set_headerset HTTP request header
nng_http_req_set_methodset HTTP request method
nng_http_req_set_uriset HTTP request URI
nng_http_req_set_versionset HTTP request protocol version
nng_http_res_add_headeradd HTTP response header
nng_http_res_allocallocate HTTP response structure
nng_http_res_alloc_errorallocate HTTP error response
nng_http_res_copy_datacopy HTTP response body
nng_http_res_del_headerdelete HTTP response header
nng_http_res_freefree HTTP response structure
nng_http_res_get_dataget HTTP response body
nng_http_res_get_headerreturn HTTP response header
nng_http_res_get_reasonreturn HTTP response reason
nng_http_res_get_statusreturn HTTP response status
nng_http_res_get_versionreturn HTTP response protocol version
nng_http_res_resetreset HTTP response structure
nng_http_res_set_dataset HTTP response body
nng_http_res_set_headerset HTTP response header
nng_http_res_set_reasonset HTTP response reason
nng_http_res_set_statusset HTTP response status
nng_http_res_set_versionset HTTP response protocol version

HTTP Client Functions

These functions are intended for use with HTTP client applications.

nng_http_client_allocallocate HTTP client
nng_http_client_connectestablish HTTP client connection
nng_http_client_freefree HTTP client
nng_http_client_get_tlsget HTTP client TLS configuration
nng_http_client_set_tlsset HTTP client TLS configuration
nng_http_client_transactperform one HTTP transaction
nng_http_conn_transactperform one HTTP transaction on connection

HTTP Server Functions

These functions are intended for use with HTTP server applications.

nng_http_handler_allocallocate HTTP server handler
nng_http_handler_collect_bodyset HTTP handler to collect request body
nng_http_handler_freefree HTTP server handler
nng_http_handler_get_datareturn extra data for HTTP handler
nng_http_handler_set_dataset extra data for HTTP handler
nng_http_handler_set_hostset host for HTTP handler
nng_http_handler_set_methodset HTTP handler method
nng_http_handler_set_treeset HTTP handler to match trees
nng_http_hijackhijack HTTP server connection
nng_http_server_add_handleradd HTTP server handler
nng_http_server_del_handlerdelete HTTP server handler
nng_http_server_get_addrget HTTP server address
nng_http_server_get_tlsget HTTP server TLS configuration
nng_http_server_holdget and hold HTTP server instance
nng_http_server_releaserelease HTTP server instance
nng_http_server_set_error_fileset custom HTTP error file
nng_http_server_set_error_pageset custom HTTP error page
nng_http_server_set_tlsset HTTP server TLS configuration
nng_http_server_res_erroruse HTTP server error page
nng_http_server_startstart HTTP server
nng_http_server_stopstop HTTP server

TLS Configuration Objects

The following functions are used to manipulate transport layer security (TLS) configuration objects. Most of these functions will not be used even by TLS applications.

Note

These functions will only be present if the library has been built with TLS support.

nng_tls_config_allocallocate TLS configuration
nng_tls_config_auth_modeset authentication mode
nng_tls_config_ca_chainset certificate authority chain
nng_tls_config_ca_fileload certificate authority from file
nng_tls_config_cert_key_fileload own certificate and key from file
nng_tls_config_pskset pre-shared key and identity
nng_tls_config_own_certset own certificate and key
nng_tls_config_freefree TLS configuration
nng_tls_config_server_nameset remote server name

SEE ALSO

nng_compat(3compat), nng(7)