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