From 0566f0804b7e48a1070d0d3a5d0f6817b4003a05 Mon Sep 17 00:00:00 2001 From: bigfoot547 Date: Fri, 16 Jan 2026 16:54:39 -0600 Subject: move include directory --- include/net.h | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 include/net.h (limited to 'include/net.h') diff --git a/include/net.h b/include/net.h new file mode 100644 index 0000000..610ed77 --- /dev/null +++ b/include/net.h @@ -0,0 +1,93 @@ +#ifndef VL_NET_H_INCLUDED +#define VL_NET_H_INCLUDED + +#include "arena.h" +#include "sha1.h" + +enum { + /* The operation completed successfully */ + NET_OK = 0, + + /* There was some kind of error relating to I/O on the local system */ + NET_EIO = -1, + + /* There was some kind of network error downloading the file. */ + NET_ENETWORK = -2, + + /* The server gave us a bad status code while downloading the file. */ + NET_ESTATUS = -3, + + /* An unrecoverable integrity error occurred. */ + NET_EINTEGRITY = -4, + + /* An unspecified error occurred. */ + NET_EUNSPEC = -5, + + /* An unspecified (remote) error occurred. */ + NET_EREMOTEUNSPEC = -6 +}; + +enum { + VERIFY_SIZE = 1u, + VERIFY_SHA1 = 2u +}; + +/* Ensures the latest version of the file available at 'url' is downloaded to 'target_path'. + * Will avoid downloading the file if it hasn't changed. + * + * Never returns NET_EINTEGRITY. */ +int vl_net_ensure_cached(vl_arena *scratch, const char *url, const char *target_path); + +/* Will verify that the file at 'target_path' hasn't been tampered with since being updated + * by vl_net_ensure_cached. + * + * Returns NET_EIO if the file doesn't exist or couldn't be read. + * Returns NET_EINTEGRITY if the file on disk doesn't match. */ +int vl_net_verify_cached(vl_arena *scratch, const char *target_path); + +/* Downloads a file for which you know the integrity information (SHA1 and/or size) from 'url' to + * 'target_path'. Will not download the file if the file already exists with correct SHA1 and size + * (as specified). + * + * Pass variadic arguments in the following order: + * - size_t size (if VERIFY_SIZE is set) + * - const uint8_t *hash (if VERIFY_SHA1 is set) + * + * Returns NET_EINTEGRITY if the file at 'url' doesn't match the provided integrity information. */ +int vl_net_ensure_verified(const char *url, const char *target_path, unsigned flags, ...); + +/* See vl_net_ensure_verified for information on the variadic argument list. + * + * Returns NET_EINTEGRITY if the file at 'target_path' doesn't match the provided integrity information. + * Returns NET_EIO if the downloaded file is not readable or could not be verified due to an I/O issue. */ +int vl_net_verify(const char *target_path, unsigned flags, ...); + +enum { + /* The job hasn't been started yet. */ + STATUS_WAITING = 0u, + + /* The job is in flight. */ + STATUS_RUNNING = 1u, + + /* The job is finished without errors. */ + STATUS_COMPLETE = 2u, + + /* The job is finished with errors. */ + STATUS_ERROR = 3u, + + /* The job finished successfully, but the downloaded file failed integrity checks. */ + STATUS_INTEGRITY = 4u +}; + +struct vl_download_job { + const char *url; + const char *opath; + size_t expect_len; + vl_sha1 expect_hash; + unsigned verify_flags; + unsigned status; +}; + +int vl_net_download_all(struct vl_download_job *jobs, size_t njobs, size_t simult); + +#endif -- cgit v1.2.3-70-g09d2