.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Podwrapper::Man 1.20.1 (Pod::Simple 3.43) .\" .\" 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 .\" ======================================================================== .\" .IX Title "nbd_connect_uri 3" .TH nbd_connect_uri 3 2024-05-08 libnbd-1.20.1 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. .SS "Supported URI formats" .IX Subsection "Supported URI formats" 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 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. .PP 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. If the \f(CW\*(C`username\*(C'\fR is present it is used for TLS authentication. .PP For all transports, an export name may be present, parsed in accordance with the NBD URI specification. .PP Finally the query part of the URI can contain: .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 and must not be present for the other transports. .IP \fBtls\-certificates=\fR\fIDIR\fR 4 .IX Item "tls-certificates=DIR" Set the certificates directory. See \fBnbd_set_tls_certificates\fR\|(3). Note this is not allowed by default \- see next section. .IP \fBtls\-psk\-file=\fR\fIPSKFILE\fR 4 .IX Item "tls-psk-file=PSKFILE" Set the PSK file. See \fBnbd_set_tls_psk_file\fR\|(3). Note this is not allowed by default \- see next section. .SS "Disable URI features" .IX Subsection "Disable 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 or ""AF_VSOCK"" transports" 4 .el .IP "TCP, Unix domain socket or \f(CWAF_VSOCK\fR transports" 4 .IX Item "TCP, Unix domain socket or AF_VSOCK 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 "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 "Overriding the export name" .IX Subsection "Overriding the export name" It is possible to override the export name portion of a URI 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 "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). .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_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_psk_file\fR\|(3), \&\fBnbd_set_uri_allow_local_file\fR\|(3), \&\fBnbd_set_uri_allow_tls\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), 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