Line data Source code
1 : #ifndef HEADER_fd_src_flamenco_snapshot_fd_snapshot_http_h 2 : #define HEADER_fd_src_flamenco_snapshot_fd_snapshot_http_h 3 : 4 : #include "fd_snapshot_base.h" 5 : #include "fd_snapshot_istream.h" 6 : 7 : /* fd_snapshot_http.h provides APIs for streaming download of Solana 8 : snapshots via HTTP. It is currently hardcoded to use non-blocking 9 : sockets. */ 10 : 11 : /* FD_SNAPSHOT_HTTP_STATE_{...} manage the state machine */ 12 : 13 6 : #define FD_SNAPSHOT_HTTP_STATE_INIT (0) /* start */ 14 6 : #define FD_SNAPSHOT_HTTP_STATE_REQ (1) /* sending request */ 15 9 : #define FD_SNAPSHOT_HTTP_STATE_RESP (2) /* receiving response headers */ 16 3 : #define FD_SNAPSHOT_HTTP_STATE_DL (3) /* downloading response body */ 17 0 : #define FD_SNAPSHOT_HTTP_STATE_DONE (4) /* downloading done */ 18 0 : #define FD_SNAPSHOT_HTTP_STATE_READ (5) /* reading snapshot file */ 19 3 : #define FD_SNAPSHOT_HTTP_STATE_FAIL (-1) /* fatal error */ 20 : 21 : /* Request size limits */ 22 : 23 : #define FD_SNAPSHOT_HTTP_REQ_HDRS_MAX (512UL) 24 : #define FD_SNAPSHOT_HTTP_REQ_PATH_MAX (508UL) 25 3 : #define FD_SNAPSHOT_HTTP_RESP_HDR_CNT (32UL) 26 3 : #define FD_SNAPSHOT_HTTP_RESP_BUF_MAX (1UL<<20) 27 : #define FD_SNAPSHOT_HTTP_FILE_PATH_MAX (4096UL) 28 : 29 : /* FD_SNAPSHOT_HTTP_DEFAULT_HOPS is the number of directs to follow 30 : by default. */ 31 : 32 6 : #define FD_SNAPSHOT_HTTP_DEFAULT_HOPS (4UL) 33 : 34 : /* fd_snapshot_http_t is the snapshot HTTP client class. */ 35 : 36 : FD_PROTOTYPES_BEGIN 37 : 38 : struct fd_snapshot_http { 39 : uint next_ipv4; /* big-endian, see fd_ip4.h */ 40 : ushort next_port; 41 : ushort hops; /* number of redirects still permitted */ 42 : 43 : int socket_fd; 44 : int state; 45 : long req_timeout; 46 : long req_deadline; 47 : 48 : /* HTTP request buffer */ 49 : 50 : union __attribute__((packed)) { 51 : struct __attribute__((packed)) { 52 : char path [ 4+FD_SNAPSHOT_HTTP_REQ_PATH_MAX ]; 53 : char req_hdrs[ FD_SNAPSHOT_HTTP_REQ_HDRS_MAX ]; 54 : }; 55 : char req_buf[ 4+FD_SNAPSHOT_HTTP_REQ_PATH_MAX+FD_SNAPSHOT_HTTP_REQ_HDRS_MAX ]; 56 : }; 57 : 58 : ushort req_tail; /* index of first unsent char */ 59 : ushort req_head; /* index of end of request buf */ 60 : ushort path_off; 61 : ushort _pad; 62 : 63 : /* HTTP response header buffer */ 64 : 65 : uchar resp_buf[ FD_SNAPSHOT_HTTP_RESP_BUF_MAX ]; 66 : uint resp_tail; 67 : uint resp_head; 68 : 69 : /* Name from last redirect */ 70 : 71 : fd_snapshot_name_t * name_out; 72 : fd_snapshot_name_t name_dummy[1]; 73 : 74 : /* Slot number that incremental snapshot should be based off of */ 75 : 76 : ulong base_slot; 77 : 78 : /* value from "content-length:" */ 79 : 80 : ulong content_len; 81 : 82 : /* Total downloaded so far */ 83 : 84 : ulong dl_total; 85 : 86 : /* Total written out so far */ 87 : 88 : ulong write_total; 89 : 90 : /* Snapshot file */ 91 : 92 : char snapshot_path[ FD_SNAPSHOT_HTTP_FILE_PATH_MAX ]; 93 : ulong snapshot_filename_off; 94 : ulong snapshot_filename_max; 95 : int snapshot_fd; 96 : uchar save_snapshot; 97 : }; 98 : 99 : typedef struct fd_snapshot_http fd_snapshot_http_t; 100 : 101 : fd_snapshot_http_t * 102 : fd_snapshot_http_new( void * mem, 103 : const char * dst_str, 104 : uint dst_ipv4, 105 : ushort dst_port, 106 : const char * snapshot_dir, 107 : fd_snapshot_name_t * name_out ); 108 : 109 : void * 110 : fd_snapshot_http_delete( fd_snapshot_http_t * this ); 111 : 112 : /* fd_snapshot_http_set_timeout sets the request timeout of the HTTP 113 : client. Measured in ns from first connection attempt to response 114 : headers received. Resets in case of a redirect. */ 115 : 116 : void 117 : fd_snapshot_http_set_timeout( fd_snapshot_http_t * this, 118 : long req_timeout ); 119 : 120 : /* fd_snapshot_http_set_path sets the path of the next request. Should 121 : start with '/'. */ 122 : 123 : void 124 : fd_snapshot_http_set_path( fd_snapshot_http_t * this, 125 : char const * path, 126 : ulong path_len, 127 : ulong base_slot ); 128 : 129 : int 130 : fd_io_istream_snapshot_http_read( void * _this, 131 : void * dst, 132 : ulong dst_max, 133 : ulong * dst_sz ); 134 : 135 : extern fd_io_istream_vt_t const fd_io_istream_snapshot_http_vt; 136 : 137 : static inline fd_io_istream_obj_t 138 0 : fd_io_istream_snapshot_http_virtual( fd_snapshot_http_t * this ) { 139 0 : return (fd_io_istream_obj_t) { 140 0 : .this = this, 141 0 : .vt = &fd_io_istream_snapshot_http_vt 142 0 : }; 143 0 : } 144 : 145 : FD_PROTOTYPES_END 146 : 147 : #endif /* HEADER_fd_src_flamenco_snapshot_fd_snapshot_http_h */