Line data Source code
1 : #define _GNU_SOURCE
2 : #include "fd_sshttp.h"
3 : #include "fd_ssarchive.h"
4 :
5 : #include "../../../waltz/http/picohttpparser.h"
6 : #include "../../../util/log/fd_log.h"
7 : #include "../../../flamenco/types/fd_types_custom.h"
8 :
9 : #include <unistd.h>
10 : #include <errno.h>
11 : #include <stdlib.h>
12 :
13 : #include <sys/socket.h>
14 : #include <netinet/tcp.h>
15 : #include <netinet/in.h>
16 :
17 0 : #define FD_SSHTTP_STATE_INIT (0) /* start */
18 0 : #define FD_SSHTTP_STATE_REQ (1) /* sending request */
19 0 : #define FD_SSHTTP_STATE_RESP (2) /* receiving response headers */
20 0 : #define FD_SSHTTP_STATE_DL (3) /* downloading response body */
21 :
22 : struct fd_sshttp_private {
23 : int state;
24 : long deadline;
25 : int full;
26 :
27 : int hops;
28 :
29 : fd_ip4_port_t addr;
30 : int sockfd;
31 :
32 : char request[ 4096UL ];
33 : ulong request_len;
34 : ulong request_sent;
35 :
36 : ulong response_len;
37 : char response[ USHORT_MAX ];
38 :
39 : char full_snapshot_name[ PATH_MAX ];
40 : char incremental_snapshot_name[ PATH_MAX ];
41 :
42 : ulong content_len;
43 :
44 : ulong magic;
45 : };
46 :
47 : FD_FN_CONST ulong
48 0 : fd_sshttp_align( void ) {
49 0 : return FD_SSHTTP_ALIGN;
50 0 : }
51 :
52 : FD_FN_CONST ulong
53 0 : fd_sshttp_footprint( void ) {
54 0 : ulong l;
55 0 : l = FD_LAYOUT_INIT;
56 0 : l = FD_LAYOUT_APPEND( l, FD_SSHTTP_ALIGN, sizeof(fd_sshttp_t) );
57 0 : return FD_LAYOUT_FINI( l, FD_SSHTTP_ALIGN );
58 0 : }
59 :
60 : void *
61 0 : fd_sshttp_new( void * shmem ) {
62 0 : if( FD_UNLIKELY( !shmem ) ) {
63 0 : FD_LOG_WARNING(( "NULL shmem" ));
64 0 : return NULL;
65 0 : }
66 :
67 0 : if( FD_UNLIKELY( !fd_ulong_is_aligned( (ulong)shmem, fd_sshttp_align() ) ) ) {
68 0 : FD_LOG_WARNING(( "unaligned shmem" ));
69 0 : return NULL;
70 0 : }
71 :
72 0 : FD_SCRATCH_ALLOC_INIT( l, shmem );
73 0 : fd_sshttp_t * sshttp = FD_SCRATCH_ALLOC_APPEND( l, FD_SSHTTP_ALIGN, sizeof(fd_sshttp_t) );
74 :
75 0 : sshttp->state = FD_SSHTTP_STATE_INIT;
76 :
77 0 : FD_COMPILER_MFENCE();
78 0 : FD_VOLATILE( sshttp->magic ) = FD_SSHTTP_MAGIC;
79 0 : FD_COMPILER_MFENCE();
80 :
81 0 : return (void *)sshttp;
82 0 : }
83 :
84 : fd_sshttp_t *
85 0 : fd_sshttp_join( void * shhttp ) {
86 0 : if( FD_UNLIKELY( !shhttp ) ) {
87 0 : FD_LOG_WARNING(( "NULL shhttp" ));
88 0 : return NULL;
89 0 : }
90 :
91 0 : if( FD_UNLIKELY( !fd_ulong_is_aligned( (ulong)shhttp, fd_sshttp_align() ) ) ) {
92 0 : FD_LOG_WARNING(( "misaligned shhttp" ));
93 0 : return NULL;
94 0 : }
95 :
96 0 : fd_sshttp_t * sshttp = (fd_sshttp_t *)shhttp;
97 :
98 0 : if( FD_UNLIKELY( sshttp->magic!=FD_SSHTTP_MAGIC ) ) {
99 0 : FD_LOG_WARNING(( "bad magic" ));
100 0 : return NULL;
101 0 : }
102 :
103 0 : return sshttp;
104 0 : }
105 :
106 : void
107 : fd_sshttp_init( fd_sshttp_t * http,
108 : fd_ip4_port_t addr,
109 : char const * path,
110 : ulong path_len,
111 0 : long now ) {
112 0 : FD_TEST( http->state==FD_SSHTTP_STATE_INIT );
113 :
114 0 : http->hops = 4UL;
115 :
116 0 : http->request_sent = 0UL;
117 0 : FD_TEST( fd_cstr_printf_check( http->request, sizeof(http->request), &http->request_len,
118 0 : "GET %.*s HTTP/1.1\r\n"
119 0 : "User-Agent: Firedancer\r\n"
120 0 : "Accept: */*\r\n"
121 0 : "Accept-Encoding: identity\r\n"
122 0 : "Host: " FD_IP4_ADDR_FMT "\r\n\r\n",
123 0 : (int)path_len, path, FD_IP4_ADDR_FMT_ARGS( addr.addr ) ) );
124 :
125 0 : http->addr = addr;
126 0 : http->sockfd = socket( AF_INET, SOCK_STREAM|SOCK_NONBLOCK, 0 );
127 0 : if( FD_UNLIKELY( -1==http->sockfd ) ) FD_LOG_ERR(( "socket() failed (%d-%s)", errno, fd_io_strerror( errno ) ));
128 :
129 0 : int optval = 1;
130 0 : if( FD_UNLIKELY( -1==setsockopt( http->sockfd, SOL_TCP, TCP_NODELAY, &optval, sizeof(int) ) ) ) {
131 0 : FD_LOG_ERR(( "setsockopt() failed (%d-%s)", errno, fd_io_strerror( errno ) ));
132 0 : }
133 :
134 0 : struct sockaddr_in addr_in = {
135 0 : .sin_family = AF_INET,
136 0 : .sin_port = fd_ushort_bswap( addr.port ),
137 0 : .sin_addr = { .s_addr = addr.addr }
138 0 : };
139 :
140 0 : if( FD_UNLIKELY( -1==connect( http->sockfd, fd_type_pun_const( &addr_in ), sizeof(addr_in) ) ) ) {
141 0 : if( FD_UNLIKELY( errno!=EINPROGRESS ) ) {
142 0 : if( FD_UNLIKELY( -1==close( http->sockfd ) ) ) FD_LOG_ERR(( "close() failed (%d-%s)", errno, fd_io_strerror( errno ) ));
143 0 : return;
144 0 : }
145 0 : }
146 :
147 0 : http->state = FD_SSHTTP_STATE_REQ;
148 0 : http->deadline = now + 500L*1000L*1000L;
149 0 : }
150 :
151 : void
152 0 : fd_sshttp_cancel( fd_sshttp_t * http ) {
153 0 : if( FD_LIKELY( http->state!=FD_SSHTTP_STATE_INIT && -1!=http->sockfd ) ) {
154 0 : if( FD_UNLIKELY( -1==close( http->sockfd ) ) ) FD_LOG_ERR(( "close() failed (%d-%s)", errno, fd_io_strerror( errno ) ));
155 0 : http->sockfd = -1;
156 0 : }
157 0 : http->state = FD_SSHTTP_STATE_INIT;
158 0 : }
159 :
160 : static int
161 : send_request( fd_sshttp_t * http,
162 0 : long now ) {
163 0 : if( FD_UNLIKELY( now>http->deadline ) ) {
164 0 : fd_sshttp_cancel( http );
165 0 : return FD_SSHTTP_ADVANCE_ERROR;
166 0 : }
167 :
168 0 : long sent = send( http->sockfd, http->request+http->request_sent, http->request_len-http->request_sent, 0 );
169 0 : if( FD_UNLIKELY( -1==sent && errno==EAGAIN ) ) return FD_SSHTTP_ADVANCE_AGAIN;
170 0 : else if( FD_UNLIKELY( -1==sent ) ) {
171 0 : fd_sshttp_cancel( http );
172 0 : return FD_SSHTTP_ADVANCE_ERROR;
173 0 : }
174 :
175 0 : http->request_sent += (ulong)sent;
176 0 : if( FD_UNLIKELY( http->request_sent==http->request_len ) ) {
177 0 : http->state = FD_SSHTTP_STATE_RESP;
178 0 : http->response_len = 0UL;
179 0 : http->deadline = now + 500L*1000L*1000L;
180 0 : }
181 :
182 0 : return FD_SSHTTP_ADVANCE_AGAIN;
183 0 : }
184 :
185 : static int
186 : follow_redirect( fd_sshttp_t * http,
187 : struct phr_header * headers,
188 : ulong header_cnt,
189 0 : long now ) {
190 0 : if( FD_UNLIKELY( !http->hops ) ) {
191 0 : FD_LOG_WARNING(( "too many redirects" ));
192 0 : fd_sshttp_cancel( http );
193 0 : return FD_SSHTTP_ADVANCE_ERROR;
194 0 : }
195 :
196 0 : http->hops--;
197 :
198 0 : ulong location_len;
199 0 : char const * location = NULL;
200 :
201 0 : for( ulong i=0UL; i<header_cnt; i++ ) {
202 0 : if( FD_UNLIKELY( !strncasecmp( headers[ i ].name, "location", headers[ i ].name_len ) ) ) {
203 0 : if( FD_UNLIKELY( !headers [ i ].value_len || headers[ i ].value[ 0 ]!='/' ) ) {
204 0 : FD_LOG_WARNING(( "invalid location header `%.*s`", (int)headers[ i ].value_len, headers[ i ].value ));
205 0 : fd_sshttp_cancel( http );
206 0 : return FD_SSHTTP_ADVANCE_ERROR;
207 0 : }
208 :
209 0 : location_len = headers[ i ].value_len;
210 0 : location = headers[ i ].value;
211 :
212 0 : if( FD_UNLIKELY( location_len>=PATH_MAX-1UL ) ) {
213 0 : fd_sshttp_cancel( http );
214 0 : return FD_SSHTTP_ADVANCE_ERROR;
215 0 : }
216 :
217 0 : char snapshot_name[ PATH_MAX ];
218 0 : fd_memcpy( snapshot_name, location+1UL, location_len-1UL );
219 0 : snapshot_name[ location_len-1UL ] = '\0';
220 :
221 0 : ulong full_entry_slot, incremental_entry_slot;
222 0 : uchar decoded_hash[ FD_HASH_FOOTPRINT ];
223 0 : int err = fd_ssarchive_parse_filename( snapshot_name, &full_entry_slot, &incremental_entry_slot, decoded_hash );
224 :
225 0 : if( FD_UNLIKELY( err ) ) {
226 0 : FD_LOG_WARNING(( "unrecognized snapshot file `%s` in redirect location header", snapshot_name ));
227 0 : fd_sshttp_cancel( http );
228 0 : return FD_SSHTTP_ADVANCE_ERROR;
229 0 : }
230 :
231 0 : char encoded_hash[ FD_BASE58_ENCODED_32_SZ ];
232 0 : fd_base58_encode_32( decoded_hash, NULL, encoded_hash );
233 :
234 0 : if( FD_LIKELY( incremental_entry_slot!=ULONG_MAX ) ) {
235 0 : FD_TEST( fd_cstr_printf_check( http->incremental_snapshot_name, PATH_MAX, NULL, "incremental-snapshot-%lu-%lu-%s.tar.zst", full_entry_slot, incremental_entry_slot, encoded_hash ) );
236 0 : } else {
237 0 : FD_TEST( fd_cstr_printf_check( http->full_snapshot_name, PATH_MAX, NULL, "snapshot-%lu-%s.tar.zst", full_entry_slot, encoded_hash ) );
238 0 : }
239 0 : break;
240 0 : }
241 0 : }
242 :
243 0 : if( FD_UNLIKELY( !location ) ) {
244 0 : FD_LOG_WARNING(( "no location header in redirect response" ));
245 0 : fd_sshttp_cancel( http );
246 0 : return FD_SSHTTP_ADVANCE_ERROR;
247 0 : }
248 :
249 0 : if( FD_UNLIKELY( !fd_cstr_printf_check( http->request, sizeof(http->request), &http->request_len,
250 0 : "GET %.*s HTTP/1.1\r\n"
251 0 : "User-Agent: Firedancer\r\n"
252 0 : "Accept: */*\r\n"
253 0 : "Accept-Encoding: identity\r\n"
254 0 : "Host: " FD_IP4_ADDR_FMT "\r\n\r\n",
255 0 : (int)location_len, location, FD_IP4_ADDR_FMT_ARGS( http->addr.addr ) ) ) ) {
256 0 : FD_LOG_WARNING(( "location header too long `%.*s`", (int)location_len, location ));
257 0 : fd_sshttp_cancel( http );
258 0 : return FD_SSHTTP_ADVANCE_ERROR;
259 0 : }
260 :
261 0 : FD_LOG_NOTICE(( "following redirect to http://" FD_IP4_ADDR_FMT ":%hu%.*s",
262 0 : FD_IP4_ADDR_FMT_ARGS( http->addr.addr ), http->addr.port,
263 0 : (int)headers[ 0 ].value_len, headers[ 0 ].value ));
264 :
265 0 : fd_sshttp_cancel( http );
266 0 : fd_sshttp_init( http, http->addr, location, location_len, now );
267 :
268 0 : return FD_SSHTTP_ADVANCE_AGAIN;
269 0 : }
270 :
271 : static int
272 : read_response( fd_sshttp_t * http,
273 : ulong * data_len,
274 : uchar * data,
275 0 : long now ) {
276 0 : if( FD_UNLIKELY( now>http->deadline ) ) {
277 0 : FD_LOG_WARNING(( "timeout reading response" ));
278 0 : fd_sshttp_cancel( http );
279 0 : return FD_SSHTTP_ADVANCE_ERROR;
280 0 : }
281 :
282 0 : long read = recv( http->sockfd, http->response+http->response_len, sizeof(http->response)-http->response_len, 0 );
283 0 : if( FD_UNLIKELY( -1==read && errno==EAGAIN ) ) return 0;
284 0 : else if( FD_UNLIKELY( -1==read ) ) {
285 0 : FD_LOG_WARNING(( "recv() failed (%d-%s)", errno, fd_io_strerror( errno ) ));
286 0 : fd_sshttp_cancel( http );
287 0 : return FD_SSHTTP_ADVANCE_ERROR;
288 0 : }
289 :
290 0 : http->response_len += (ulong)read;
291 :
292 0 : int minor_version;
293 0 : int status;
294 0 : const char * message;
295 0 : ulong message_len;
296 0 : struct phr_header headers[ 128UL ];
297 0 : ulong header_cnt = 128UL;
298 0 : int parsed = phr_parse_response( http->response,
299 0 : http->response_len,
300 0 : &minor_version,
301 0 : &status,
302 0 : &message,
303 0 : &message_len,
304 0 : headers,
305 0 : &header_cnt,
306 0 : http->response_len - (ulong)read );
307 0 : if( FD_UNLIKELY( parsed==-1 ) ) {
308 0 : FD_LOG_WARNING(( "malformed response body" ));
309 0 : fd_sshttp_cancel( http );
310 0 : return FD_SSHTTP_ADVANCE_ERROR;
311 0 : } else if( parsed==-2 ) {
312 0 : return FD_SSHTTP_ADVANCE_AGAIN;
313 0 : }
314 :
315 0 : int is_redirect = (status==301) | (status==303) | (status==304) | (status==307) | (status==308);
316 0 : if( FD_UNLIKELY( is_redirect ) ) {
317 0 : return follow_redirect( http, headers, header_cnt, now );
318 0 : }
319 :
320 0 : if( FD_UNLIKELY( status!=200 ) ) {
321 0 : FD_LOG_WARNING(( "unexpected response status %d", status ));
322 0 : fd_sshttp_cancel( http );
323 0 : return FD_SSHTTP_ADVANCE_ERROR;
324 0 : }
325 :
326 0 : http->content_len = ULONG_MAX;
327 0 : for( ulong i=0UL; i<header_cnt; i++ ) {
328 0 : if( FD_LIKELY( headers[i].name_len!=14UL ) ) continue;
329 0 : if( FD_LIKELY( strncasecmp( headers[i].name, "content-length", 14UL ) ) ) continue;
330 :
331 0 : http->content_len = strtoul( headers[i].value, NULL, 10 );
332 0 : break;
333 0 : }
334 :
335 0 : if( FD_UNLIKELY( http->content_len==ULONG_MAX ) ) {
336 0 : FD_LOG_WARNING(( "no content-length header in response" ));
337 0 : fd_sshttp_cancel( http );
338 0 : return FD_SSHTTP_ADVANCE_ERROR;
339 0 : }
340 :
341 0 : http->state = FD_SSHTTP_STATE_DL;
342 0 : if( FD_UNLIKELY( (ulong)parsed<http->response_len ) ) {
343 0 : FD_TEST( *data_len>=http->response_len-(ulong)parsed );
344 0 : *data_len = http->response_len - (ulong)parsed;
345 0 : fd_memcpy( data, http->response+parsed, *data_len );
346 0 : http->content_len -= *data_len;
347 0 : return FD_SSHTTP_ADVANCE_DATA;
348 0 : } else {
349 0 : FD_TEST( http->response_len==(ulong)parsed );
350 0 : return FD_SSHTTP_ADVANCE_AGAIN;
351 0 : }
352 0 : }
353 :
354 : static int
355 : read_body( fd_sshttp_t * http,
356 : ulong * data_len,
357 0 : uchar * data ) {
358 0 : if( FD_UNLIKELY( !http->content_len ) ) {
359 0 : fd_sshttp_cancel( http );
360 0 : http->state = FD_SSHTTP_STATE_INIT;
361 0 : return FD_SSHTTP_ADVANCE_DONE;
362 0 : }
363 :
364 0 : long read = recv( http->sockfd, data, fd_ulong_min( *data_len, http->content_len ), 0 );
365 0 : if( FD_UNLIKELY( -1==read && errno==EAGAIN ) ) return FD_SSHTTP_ADVANCE_AGAIN;
366 0 : else if( FD_UNLIKELY( -1==read ) ) {
367 0 : fd_sshttp_cancel( http );
368 0 : return FD_SSHTTP_ADVANCE_ERROR;
369 0 : }
370 :
371 0 : if( FD_UNLIKELY( !read ) ) return FD_SSHTTP_ADVANCE_AGAIN;
372 :
373 0 : *data_len = (ulong)read;
374 0 : http->content_len -= (ulong)read;
375 :
376 0 : return FD_SSHTTP_ADVANCE_DATA;
377 0 : }
378 :
379 : void
380 : fd_sshttp_snapshot_names( fd_sshttp_t * http,
381 : char const ** full_snapshot_name,
382 0 : char const ** incremental_snapshot_name ) {
383 0 : *full_snapshot_name = http->full_snapshot_name;
384 0 : *incremental_snapshot_name = http->incremental_snapshot_name;
385 0 : }
386 :
387 : int
388 : fd_sshttp_advance( fd_sshttp_t * http,
389 : ulong * data_len,
390 : uchar * data,
391 0 : long now ) {
392 : /* TODO: Add timeouts ... */
393 :
394 0 : switch( http->state ) {
395 0 : case FD_SSHTTP_STATE_INIT: return FD_SSHTTP_ADVANCE_AGAIN;
396 0 : case FD_SSHTTP_STATE_REQ: return send_request( http, now );
397 0 : case FD_SSHTTP_STATE_RESP: return read_response( http, data_len, data, now );
398 0 : case FD_SSHTTP_STATE_DL: return read_body( http, data_len, data );
399 0 : default: return 0;
400 0 : }
401 0 : }
|