Skip to content
pi-c Artifact [181b7d4d81]
Login

Artifact [181b7d4d81]

Artifact 181b7d4d810e5cdfe23301480693bc49312f3df527b26368e8970c2db8b98304:


/* One outgoing connection: TCP, then TLS 1.2 (BearSSL) unless the host is the
 * loopback address and TLS was not asked for. The only file that includes
 * BearSSL is net.c, so the TLS library can be replaced there alone.
 * Every wait is bounded by an idle limit and an overall deadline. */
#ifndef PIC_NET_H
#define PIC_NET_H

#include <stddef.h>
#include <signal.h>

/* Set by pic's SIGINT handler (Ctrl-C): network waits give up with
 * "interrupted" and a running command is aborted. */
extern volatile sig_atomic_t pic_interrupted;

typedef struct pic_net pic_net;

/* Loads the trust anchors ($SSL_CERT_FILE, else the system bundle), seeds
 * the TLS random generator and reads the date. NULL with err on failure. */
pic_net *pic_net_new(char *err, size_t cap);
void pic_net_free(pic_net *n);

/* Connects to host:port within idle_seconds of no progress and
 * deadline_seconds overall (the deadline also covers later reads and
 * writes). tls 0 is allowed only for loopback hosts. 0 or -1. */
int pic_net_connect(pic_net *n, const char *host, int port, int tls, int idle_seconds, int deadline_seconds);
/* Sends all n bytes (buffered by TLS until the first read). 0 or -1. */
int pic_net_write(pic_net *n, const void *bytes, size_t len);
/* > 0 bytes read, 0 at a clean end of stream, -1 on error. */
long pic_net_read(pic_net *n, void *bytes, size_t cap);
void pic_net_close(pic_net *n);

/* What failed, for a message. */
const char *pic_net_error(const pic_net *n);
/* The failure was in TLS: handshake, certificate, host name or protocol. */
int pic_net_tls_failed(const pic_net *n);

#endif