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