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