diff options
| author | 2026-01-08 23:23:41 -0600 | |
|---|---|---|
| committer | 2026-01-08 23:23:41 -0600 | |
| commit | ec3534e0c1c0c858d4b35ad06c0718fdbcef1c53 (patch) | |
| tree | 05339e77d71bb7c3168ee9f0fd7471264d1a4fc1 /lib/include/net.h | |
| parent | initial commit (diff) | |
add support for verified files
Diffstat (limited to 'lib/include/net.h')
| -rw-r--r-- | lib/include/net.h | 58 |
1 files changed, 57 insertions, 1 deletions
diff --git a/lib/include/net.h b/lib/include/net.h index d5f1482..5ee7fc6 100644 --- a/lib/include/net.h +++ b/lib/include/net.h @@ -3,6 +3,62 @@ #include "arena.h" -int vl_net_ensure_cached(vl_arena *arena, const char *url, const char *target_path); +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, ...); #endif |
