.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Podwrapper::Man 1.24.3 (Pod::Simple 3.45) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" .\" Required to disable full justification in groff 1.23.0. .if n .ds AD l .\" ======================================================================== .\" .IX Title "nbd_connect_uri 3" .TH nbd_connect_uri 3 2026-07-16 libnbd-1.24.3 LIBNBD .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH NAME nbd_connect_uri \- connect to NBD URI .SH SYNOPSIS .IX Header "SYNOPSIS" .Vb 1 \& #include \& \& int nbd_connect_uri ( \& struct nbd_handle *h, const char *uri \& ); .Ve .SH DESCRIPTION .IX Header "DESCRIPTION" Connect (synchronously) to an NBD server and export by specifying the NBD URI. NBD URIs are a standard way to specify a network block device endpoint, using a syntax like \&\f(CW"nbd://example.com"\fR which is convenient, well defined and future proof. .PP This call works by parsing the URI parameter and calling \&\fBnbd_set_export_name\fR\|(3) and \fBnbd_set_tls\fR\|(3) and other calls as needed, followed by \fBnbd_connect_tcp\fR\|(3), \&\fBnbd_connect_unix\fR\|(3) or \fBnbd_connect_vsock\fR\|(3). .PP This call returns when the connection has been made. By default, this proceeds all the way to transmission phase, but \&\fBnbd_set_opt_mode\fR\|(3) can be used for manual control over option negotiation performed before transmission phase. .SS "Example URIs supported" .IX Subsection "Example URIs supported" .ie n .IP """nbd://example.com""" 4 .el .IP \f(CWnbd://example.com\fR 4 .IX Item "nbd://example.com" Connect over TCP, unencrypted, to \f(CW\*(C`example.com\*(C'\fR port 10809. .ie n .IP """nbds://example.com""" 4 .el .IP \f(CWnbds://example.com\fR 4 .IX Item "nbds://example.com" Connect over TCP with TLS, to \f(CW\*(C`example.com\*(C'\fR port 10809. If the server does not support TLS then this will fail. .ie n .IP """nbd+unix:///foo?socket=/tmp/nbd.sock""" 4 .el .IP \f(CWnbd+unix:///foo?socket=/tmp/nbd.sock\fR 4 .IX Item "nbd+unix:///foo?socket=/tmp/nbd.sock" Connect over the Unix domain socket \fI/tmp/nbd.sock\fR to an NBD server running locally. The export name is set to \f(CW\*(C`foo\*(C'\fR (note without any leading \f(CW\*(C`/\*(C'\fR character). .ie n .IP """nbds+unix://alice@/?socket=/tmp/nbd.sock&tls\-certificates=certs""" 4 .el .IP \f(CWnbds+unix://alice@/?socket=/tmp/nbd.sock&tls\-certificates=certs\fR 4 .IX Item "nbds+unix://alice@/?socket=/tmp/nbd.sock&tls-certificates=certs" Connect over a Unix domain socket, enabling TLS and setting the path to a directory containing certificates and keys. .ie n .IP """nbd+vsock:///""" 4 .el .IP \f(CWnbd+vsock:///\fR 4 .IX Item "nbd+vsock:///" In this scenario libnbd is running in a virtual machine. Connect over \f(CW\*(C`AF_VSOCK\*(C'\fR to an NBD server running on the hypervisor. .ie n .IP """nbd+ssh://server/""" 4 .el .IP \f(CWnbd+ssh://server/\fR 4 .IX Item "nbd+ssh://server/" Connect to remote \f(CW\*(C`server\*(C'\fR using Secure Shell, and tunnel NBD to an NBD server listening on port 10809. .SS "NBD URI standard" .IX Subsection "NBD URI standard" https://github.com/NetworkBlockDevice/nbd/blob/master/doc/uri.md documents the NBD URI standard. .PP In the documentation below, \fINon\-standard\fR indicates features supported in libnbd which are not a part of the NBD URI standard, meaning that other NBD URI parsers might not support them or might implement things differently. .SS "URI scheme" .IX Subsection "URI scheme" The scheme is the part before the first \f(CW\*(C`:\*(C'\fR. The following schemes are supported in the current version of libnbd: .ie n .IP """nbd:""" 4 .el .IP \f(CWnbd:\fR 4 .IX Item "nbd:" Connect over TCP without using TLS. .ie n .IP """nbds:""" 4 .el .IP \f(CWnbds:\fR 4 .IX Item "nbds:" Connect over TCP. TLS is required and the connection will fail if the server does not support TLS. .ie n .IP """nbd+unix:""" 4 .el .IP \f(CWnbd+unix:\fR 4 .IX Item "nbd+unix:" .PD 0 .ie n .IP """nbds+unix:""" 4 .el .IP \f(CWnbds+unix:\fR 4 .IX Item "nbds+unix:" .PD Connect over a Unix domain socket, without or with TLS respectively. The \f(CW\*(C`socket\*(C'\fR parameter is required. .ie n .IP """nbd+vsock:""" 4 .el .IP \f(CWnbd+vsock:\fR 4 .IX Item "nbd+vsock:" .PD 0 .ie n .IP """nbds+vsock:""" 4 .el .IP \f(CWnbds+vsock:\fR 4 .IX Item "nbds+vsock:" .PD \&\fINon\-standard\fR .Sp Connect over the \f(CW\*(C`AF_VSOCK\*(C'\fR transport, without or with TLS respectively. You can use \fBnbd_supports_vsock\fR\|(3) to see if this build of libnbd supports \f(CW\*(C`AF_VSOCK\*(C'\fR. .ie n .IP """nbd+ssh:""" 4 .el .IP \f(CWnbd+ssh:\fR 4 .IX Item "nbd+ssh:" .PD 0 .ie n .IP """nbds+ssh:""" 4 .el .IP \f(CWnbds+ssh:\fR 4 .IX Item "nbds+ssh:" .PD \&\fINon\-standard, libnbd ≥ 1.22\fR .Sp Tunnel NBD over a Secure Shell connection. This requires that \fBssh\fR\|(1) is installed locally, and that \fBnc\fR\|(1) (from the nmap project) is installed on the remote server. .SS "URI authority" .IX Subsection "URI authority" The authority part of the URI \f(CW\*(C`[username@][servername][:port]\*(C'\fR is parsed depending on the transport. For TCP it specifies the server to connect to and optional port number. For \f(CW\*(C`+unix\*(C'\fR it should not be present. For \f(CW\*(C`+vsock\*(C'\fR the server name is the numeric CID (eg. \f(CW2\fR to connect to the host), and the optional port number may be present. For \f(CW\*(C`+ssh\*(C'\fR the Secure Shell server and optional port. If the \f(CW\*(C`username\*(C'\fR is present it is used for TLS authentication or the SSH username. .SS "URI export name" .IX Subsection "URI export name" For all transports, an export name may be present, parsed in accordance with the NBD URI specification. Note that the initial \&\f(CW\*(C`/\*(C'\fR character is not part of the export name: .PP .Vb 3 \& URI export name \& nbd://localhost/ "" (empty string) \& nbd://localhost/export "export" .Ve .PP It is possible to override the export name programmatically by using \fBnbd_set_opt_mode\fR\|(3) to enable option mode, then using \fBnbd_set_export_name\fR\|(3) and \fBnbd_opt_go\fR\|(3) as part of subsequent negotiation. .SS "URI query" .IX Subsection "URI query" Finally the query part of the URI can contain: .IP \fBcompress=true\fR 4 .IX Item "compress=true" \&\fINon\-standard, libnbd ≥ 1.24\fR .Sp For SSH transport only, enable compression. The default is false. .ie n .IP "\fBnbd\-port=\fR""PORT""" 4 .el .IP \fBnbd\-port=\fR\f(CWPORT\fR 4 .IX Item "nbd-port=PORT" \&\fINon\-standard, libnbd ≥ 1.24\fR .Sp Override the port number from the authority part of the URI. .Sp For SSH transport, this specifies the port used to connect to the NBD server, but the port in the authority field is used for the SSH connection. .IP \fBsocket=\fR\fISOCKET\fR 4 .IX Item "socket=SOCKET" Specifies the Unix domain socket to connect on. Must be present for the \f(CW\*(C`+unix\*(C'\fR transport, optional for \f(CW\*(C`+ssh\*(C'\fR, and must not be present for the other transports. .IP \fBtls\-certificates=\fR\fIDIR\fR 4 .IX Item "tls-certificates=DIR" \&\fINon\-standard, libnbd ≥ 1.10\fR .Sp Set the certificates directory. See \fBnbd_set_tls_certificates\fR\|(3). Note this is not allowed by default \- see next section. .ie n .IP "\fBtls\-hostname=\fR""SERVER""" 4 .el .IP \fBtls\-hostname=\fR\f(CWSERVER\fR 4 .IX Item "tls-hostname=SERVER" \&\fIlibnbd ≥ 1.22\fR .Sp Set the TLS hostname. See \fBnbd_set_tls_hostname\fR\|(3). .ie n .IP "\fBtls\-priority=\fR""PRIORITY""" 4 .el .IP \fBtls\-priority=\fR\f(CWPRIORITY\fR 4 .IX Item "tls-priority=PRIORITY" \&\fINon\-standard, libnbd ≥ 1.24\fR .Sp Override the TLS priority string. See \fBnbd_set_tls_priority\fR\|(3). This is not allowed by default \- see next section. .IP \fBtls\-psk\-file=\fR\fIPSKFILE\fR 4 .IX Item "tls-psk-file=PSKFILE" \&\fINon\-standard\fR .Sp Set the PSK file. See \fBnbd_set_tls_psk_file\fR\|(3). Note this is not allowed by default \- see next section. .ie n .IP "\fBtls\-username=\fR""USER""" 4 .el .IP \fBtls\-username=\fR\f(CWUSER\fR 4 .IX Item "tls-username=USER" \&\fINon\-standard, libnbd ≥ 1.24\fR .Sp Override the username from the authority part of the URI. .Sp For SSH transport, this specifies the TLS username for connecting to the NBD server, but the user in the authority field is used for the SSH connection. .IP \fBtls\-verify\-peer=false\fR 4 .IX Item "tls-verify-peer=false" Do not verify the server certificate. See \fBnbd_set_tls_verify_peer\fR\|(3). The default is \f(CW\*(C`true\*(C'\fR. .SS "Disabling URI features" .IX Subsection "Disabling URI features" For security reasons you might want to disable certain URI features. Pre\-filtering URIs is error\-prone and should not be attempted. Instead use the libnbd APIs below to control what can appear in URIs. Note you must call these functions on the same handle before calling \fBnbd_connect_uri\fR\|(3) or \&\fBnbd_aio_connect_uri\fR\|(3). .ie n .IP "TCP, Unix domain socket, ""AF_VSOCK"" or SSH transports" 4 .el .IP "TCP, Unix domain socket, \f(CWAF_VSOCK\fR or SSH transports" 4 .IX Item "TCP, Unix domain socket, AF_VSOCK or SSH transports" Default: all allowed .Sp To select which transports are allowed call \&\fBnbd_set_uri_allow_transports\fR\|(3). .IP TLS 4 .IX Item "TLS" Default: both non\-TLS and TLS connections allowed .Sp To force TLS off or on in URIs call \&\fBnbd_set_uri_allow_tls\fR\|(3). .IP "TLS priority" 4 .IX Item "TLS priority" Default: denied .Sp To allow the TLS priority to be set in URIs call \&\fBnbd_set_uri_allow_tls_priority\fR\|(3). .IP "Connect to Unix domain socket in the local filesystem" 4 .IX Item "Connect to Unix domain socket in the local filesystem" Default: allowed .Sp To prevent this you must disable the \f(CW\*(C`+unix\*(C'\fR transport using \fBnbd_set_uri_allow_transports\fR\|(3). .IP "Read from local files" 4 .IX Item "Read from local files" Default: denied .Sp To allow URIs to contain references to local files (eg. for parameters like \f(CW\*(C`tls\-psk\-file\*(C'\fR) call \&\fBnbd_set_uri_allow_local_file\fR\|(3). .SS "Optional features" .IX Subsection "Optional features" This call will fail if libnbd was not compiled with libxml2; you can test whether this is the case with \fBnbd_supports_uri\fR\|(3). .PP Support for URIs that require TLS will fail if libnbd was not compiled with gnutls; you can test whether this is the case with \fBnbd_supports_tls\fR\|(3). .SS "Constructing a URI from an existing connection" .IX Subsection "Constructing a URI from an existing connection" See \fBnbd_get_uri\fR\|(3). .SS "See if a string is an NBD URI" .IX Subsection "See if a string is an NBD URI" See \fBnbd_is_uri\fR\|(3). .SS "Differences from qemu and glib parsing of NBD URIs" .IX Subsection "Differences from qemu and glib parsing of NBD URIs" \&\fBqemu\fR\|(1) also supports NBD URIs and has a separate URI parser. In qemu\ ≤\ 9.0 this was done using their own parser. In qemu\ ≥\ 9.1 this is done using glib \f(CW\*(C`g_uri\*(C'\fR functions. The current (glib\-based) parser does not parse the export name part of the URI in exactly the same way as libnbd, which may cause URIs to work in libnbd but not in qemu or \fIvice versa\fR. Only URIs using exportnames should be affected. For details see https://gitlab.com/qemu\-project/qemu/\-/issues/2584. .SS "Limitations on vsock port numbers" .IX Subsection "Limitations on vsock port numbers" The \fBvsock\fR\|(7) protocol allows 32 bit unsigned ports, reserving ports 0, 1 and 2 for special purposes. In Linux, ports < 1024 are reserved for privileged processes. .PP libxml2 (used to parse the URI) imposes additional restrictions. libxml2 < 2.9 limited port numbers to 99,999,999. libxml2 ≥ 2.9 limits port numbers to ≤ 0x7fff_ffff (31 bits). .PP libnbd ≥ 1.24 allows you to override the port field by adding \&\f(CW\*(C`?nbd\-port=PORT\*(C'\fR to the URI, allowing you to use any vsock port number. .SH "RETURN VALUE" .IX Header "RETURN VALUE" If the call is successful the function returns \f(CW0\fR. .SH ERRORS .IX Header "ERRORS" On error \f(CW\-1\fR is returned. .PP Refer to "ERROR HANDLING" in \fBlibnbd\fR\|(3) for how to get further details of the error. .PP The following parameters must not be NULL: \f(CW\*(C`h\*(C'\fR, \f(CW\*(C`uri\*(C'\fR. For more information see "Non\-NULL parameters" in \fBlibnbd\fR\|(3). .SH "HANDLE STATE" .IX Header "HANDLE STATE" nbd_connect_uri can be called when the handle is in the following state: .PP .Vb 8 \& ┌─────────────────────────────────────┬─────────────────────────┐ \& │ Handle created, before connecting │ ✅ allowed │ \& │ Connecting │ ❌ error │ \& │ Connecting & handshaking (opt_mode) │ ❌ error │ \& │ Connected to the server │ ❌ error │ \& │ Connection shut down │ ❌ error │ \& │ Handle dead │ ❌ error │ \& └─────────────────────────────────────┴─────────────────────────┘ .Ve .SH VERSION .IX Header "VERSION" This function first appeared in libnbd 1.0. .PP If you need to test if this function is available at compile time check if the following macro is defined: .PP .Vb 1 \& #define LIBNBD_HAVE_NBD_CONNECT_URI 1 .Ve .SH EXAMPLE .IX Header "EXAMPLE" This example is also available as \fIexamples/connect\-uri.c\fR in the libnbd source code. .PP .Vb 10 \& /* This example shows how to connect to an NBD \& * server using the server\*(Aqs NBD URI. \& * \& * To test this with a recent version of nbdkit \& * that supports the \*(Aq$uri\*(Aq syntax, do: \& * \& * nbdkit \-U \- random 1M \e \& * \-\-run \*(Aq./connect\-uri $uri\*(Aq \& * \& * To test connecting to a remote NBD server \& * listening on port 10809, do: \& * \& * ./connect\-uri nbd://remote/ \& */ \& \& #include \& #include \& #include \& #include \& #include \& #include \& \& #include \& \& int \& main (int argc, char *argv[]) \& { \& struct nbd_handle *nbd; \& char *s; \& int64_t size; \& \& if (argc != 2) { \& fprintf (stderr, "usage: %s URI\en", \& argv[0]); \& exit (EXIT_FAILURE); \& } \& \& /* Create the libnbd handle. */ \& nbd = nbd_create (); \& if (nbd == NULL) { \& fprintf (stderr, "%s\en", nbd_get_error ()); \& exit (EXIT_FAILURE); \& } \& \& /* Request full information \& * (for nbd_get_canonical_export_name below) \& */ \& #if LIBNBD_HAVE_NBD_SET_FULL_INFO \& if (nbd_set_full_info (nbd, true) == \-1) { \& fprintf (stderr, "%s\en", nbd_get_error ()); \& exit (EXIT_FAILURE); \& } \& #endif \& \& /* Connect to the NBD URI. */ \& printf ("connecting to %s ...\en", argv[1]); \& fflush (stdout); \& if (nbd_connect_uri (nbd, argv[1]) == \-1) { \& fprintf (stderr, "%s\en", nbd_get_error ()); \& exit (EXIT_FAILURE); \& } \& printf ("connected\en"); \& \& /* Print the URI, export name, size and other info. */ \& printf ("requested URI: %s\en", argv[1]); \& s = nbd_get_uri (nbd); \& printf ("generated URI: %s\en", s ? s : "NULL"); \& free (s); \& size = nbd_get_size (nbd); \& if (size == \-1) { \& fprintf (stderr, "%s\en", nbd_get_error ()); \& exit (EXIT_FAILURE); \& } \& printf ("size: %" PRIi64 "\en", size); \& s = nbd_get_export_name (nbd); \& printf ("requested export name: %s\en", s ? s : "NULL"); \& free (s); \& #if LIBNBD_HAVE_NBD_GET_CANONICAL_EXPORT_NAME \& s = nbd_get_canonical_export_name (nbd); \& printf ("canonical export name: %s\en", s ? s : "NULL"); \& free (s); \& #endif \& #if LIBNBD_HAVE_NBD_GET_EXPORT_DESCRIPTION \& s = nbd_get_export_description (nbd); \& printf ("export description: %s\en", s ? s : "NULL"); \& free (s); \& #endif \& \& /* Close the libnbd handle. */ \& nbd_close (nbd); \& \& exit (EXIT_SUCCESS); \& } .Ve .SH "SEE ALSO" .IX Header "SEE ALSO" \&\fBnbd_aio_connect_uri\fR\|(3), \&\fBnbd_connect_tcp\fR\|(3), \&\fBnbd_connect_unix\fR\|(3), \&\fBnbd_connect_uri\fR\|(3), \&\fBnbd_connect_vsock\fR\|(3), \&\fBnbd_create\fR\|(3), \&\fBnbd_get_uri\fR\|(3), \&\fBnbd_is_uri\fR\|(3), \&\fBnbd_opt_go\fR\|(3), \&\fBnbd_set_export_name\fR\|(3), \&\fBnbd_set_opt_mode\fR\|(3), \&\fBnbd_set_tls\fR\|(3), \&\fBnbd_set_tls_certificates\fR\|(3), \&\fBnbd_set_tls_hostname\fR\|(3), \&\fBnbd_set_tls_priority\fR\|(3), \&\fBnbd_set_tls_psk_file\fR\|(3), \&\fBnbd_set_tls_verify_peer\fR\|(3), \&\fBnbd_set_uri_allow_local_file\fR\|(3), \&\fBnbd_set_uri_allow_tls\fR\|(3), \&\fBnbd_set_uri_allow_tls_priority\fR\|(3), \&\fBnbd_set_uri_allow_transports\fR\|(3), \&\fBnbd_supports_tls\fR\|(3), \&\fBnbd_supports_uri\fR\|(3), \&\fBnbd_supports_vsock\fR\|(3), \&\fBlibnbd\fR\|(3), \&\fBnbdkit\-ssh\-plugin\fR\|(1), \&\fBnc\fR\|(1), \&\fBqemu\fR\|(1), \&\fBssh\fR\|(1), \&\fBvsock\fR\|(7), https://github.com/NetworkBlockDevice/nbd/blob/master/doc/uri.md. .SH AUTHORS .IX Header "AUTHORS" Eric Blake .PP Richard W.M. Jones .SH COPYRIGHT .IX Header "COPYRIGHT" Copyright Red Hat .SH LICENSE .IX Header "LICENSE" This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. .PP This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. .PP You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110\-1301 USA