summaryrefslogtreecommitdiffstats
path: root/include/net.h
blob: 610ed7737c8d870a566aa7f3cd8bc719dffed73c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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