/* 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> #include <sys/socket.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; /* 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); /* A third: the inbox's wake (inbox.c), readable when a message may be * waiting for the running conversation; -1 when there is none. Served the * same way, outside any transaction. */ extern int pic_watch_fd; extern void (*pic_watch_ready)(void); /* Waits up to ms (0: only looks) for the side and watch descriptors and serves them. */ void pic_side_poll(int ms); /* Whether pic_side_poll has a descriptor to wait on. */ int pic_side_any(void); 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); /* Web mode, for a fetch of a URL the model names: plain http is allowed to any host, and a * connection is made only to a public address, the one checked being the one connected to (so a * name that resolves to a private address, or to one after a redirect, is refused). Off by * default. LOOPBACK_PORT, when above 0, is the one loopback port a test may reach. */ void pic_net_web(pic_net *n, int on, int loopback_port); /* Whether SA (an IPv4 or IPv6 address) is a public one: not private, loopback, link-local, * carrier-grade NAT, multicast, documentation or reserved, nor an IPv6 form of one (mapped, * NAT64, 6to4). */ int pic_net_addr_public(const struct sockaddr *sa); /* The last connect was refused for its address, not for a failure of the network. */ int pic_net_refused(const 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); /* The last read failed because the server closed TLS without ending it * (no close_notify): a sized reply may still be whole. */ int pic_net_unclean_close(const pic_net *n); #endif