Skip to content
pi-c Documentation
Login

Documentation

/* 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;
/* 1 while pic_run is at a step of its own, between requests and tool calls
 * (not inside a network or tool wait, whose idle limits a slow command
 * handler would eat into); pic_side_idle, when set, is called there. */
extern int pic_side_safe;
extern void (*pic_side_idle)(void);

/* A second descriptor pic watches whenever it waits (the --commands lines on
 * stdin), and what runs when it is readable; -1 when there is none. Waits
 * happen outside any transaction, so pic_side_ready may journal. */
extern int pic_side_fd;
extern void (*pic_side_ready)(void);
/* Waits up to ms (0: only looks) for the side descriptor and serves it. */
void pic_side_poll(int ms);

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