upscli_connect(3)

upscli_connect3

Open a connection to a nut upsd data server

Usage

        #include <upsclient.h>
        /* Open a connection to a NUT upsd data server (blocking by default) */
        int upscli_connect(UPSCONN_t *ups, const char *host, uint16_t port, int flags);
        /* Open a connection to a NUT upsd data server with specified timeout */
        int upscli_tryconnect(UPSCONN_t *ups, const char *host, uint16_t port, int flags,
                struct timeval * timeout);

Overview

The upscli_connect() function takes the pointer ups to a UPSCONN_t state structure and opens a TCP connection to the host on the given port.

By default this makes a blocking connection attempt (until the system transport layer times out the underlying select(2) call). A program-wide default timeout for this method can be configured with the upscli_set_default_connect_timeout(3) or upscli_init_default_connect_timeout(3) methods. For operations with explicit operation-specific timeout please use the upscli_tryconnect() method instead.

Note

As part of general initialization, both the upscli_init(3) function or the first call to upscli_connect() function, can call the upscli_init_default_connect_timeout(3) method (if it was never used before): this allows unmodified (legacy) NUT clients to consistently benefit from presence of the NUT_DEFAULT_CONNECT_TIMEOUT environment variable for upscli_connect(3) attempts to be not blocking (as per default).

flags may be either UPSCLI_CONN_TRYSSL to try a SSL connection, or UPSCLI_CONN_REQSSL to require a SSL connection.

Introduced in version 2.7, an additional flag UPSCLI_CONN_CERTVERIF now exists to verify the signature offered during the SSL handshake. This flag should be used in conjunction with upscli_init(3) and/or upscli_add_host_cert(3) calls before connecting in order to define a CA certificate with which to verify.

If SSL mode is required, this function will only return successfully if it is able to establish a SSL connection with the server. Possible reasons for failure include no SSL support on the server, and if upsclient itself hasn’t been compiled with SSL support.

You must call upscli_disconnect(3) when finished with a connection, or your program will slowly leak memory and file descriptors.

Return value

The upscli_connect() function modifies the UPSCONN_t structure and returns 0 on success, or -1 if an error occurs.

Releases

12 releases since Debian 3.1 (Sarge); the text changed 7 times.

  1. 3.1–4.0
  2. 5.0–6.0
  3. 7
  4. 8
  5. 9–11
  6. 12
  7. 13
  8. Arch

From nut 2.8.5-2 in Arch Linux 2026-11-01. Parsed by carta from the page's man source.