Line data Source code
1 : #define _GNU_SOURCE
2 : #include "fd_sshttp_private.h"
3 : #include "fd_ssarchive.h"
4 :
5 : #include "../../../third_party/picohttpparser/picohttpparser.h"
6 : #include "../../../util/log/fd_log.h"
7 : #include "../../../util/fd_util.h"
8 : #include "../../../waltz/http/fd_http.h"
9 : #include "../../../ballet/ed25519/fd_x25519.h"
10 :
11 : FD_STATIC_ASSERT( FD_HASH_FOOTPRINT==32UL, resolved_hash_sz );
12 :
13 : #include <unistd.h>
14 : #include <errno.h>
15 : #include <stdlib.h>
16 :
17 : #include <sys/epoll.h>
18 : #include <sys/socket.h>
19 : #include <sys/random.h>
20 : #include <netinet/in.h>
21 :
22 : _Bool fd_sshttp_fuzz = 0;
23 :
24 : static void
25 0 : fd_sshttp_tls_init( fd_tls_t * tls, fd_sshttp_t * http ) {
26 0 : fd_memset( tls, 0, sizeof(fd_tls_t) );
27 :
28 : /* Seed the CSPRNG that fd_tls draws handshake randomness from */
29 :
30 0 : uchar rng_key[ FD_CHACHA_KEY_SZ ];
31 0 : if( FD_UNLIKELY( !fd_rng_secure( rng_key, sizeof(rng_key) ) ) ) FD_LOG_CRIT(( "fd_rng_secure failed" ));
32 0 : fd_chacha_rng_init( http->rng, rng_key, FD_CHACHA_RNG_ALGO_CHACHA8 );
33 0 : fd_memzero_explicit( rng_key, sizeof(rng_key) );
34 0 : tls->rng = http->rng;
35 :
36 0 : static uchar const alpn[] = { 8, 'h', 't', 't', 'p', '/', '1', '.', '1' };
37 0 : fd_memcpy( tls->alpn, alpn, sizeof(alpn) );
38 0 : tls->alpn_sz = sizeof(alpn);
39 :
40 0 : tls->quic = 0;
41 :
42 0 : tls->ca_store = &http->ca_store;
43 0 : }
44 :
45 : static int
46 0 : http_init_tls( fd_sshttp_t * http ) {
47 0 : ulong hostname_len = strlen( http->hostname );
48 0 : if( FD_UNLIKELY( hostname_len >= sizeof(http->tls.server_name) ) ) {
49 0 : FD_LOG_WARNING(( "hostname too long for SNI: %s", http->hostname ));
50 0 : return -1;
51 0 : }
52 0 : fd_memcpy( http->tls.server_name, http->hostname, hostname_len );
53 0 : http->tls.server_name[ hostname_len ] = '\0';
54 0 : http->tls.server_name_len = (ushort)hostname_len;
55 :
56 : /* Generate a fresh ephemeral X25519 key for this handshake */
57 :
58 0 : if( FD_UNLIKELY( !fd_rng_secure( http->tls.kex_private_key, 32UL ) ) ) FD_LOG_CRIT(( "fd_rng_secure failed" ));
59 0 : fd_x25519_public( http->tls.kex_public_key, http->tls.kex_private_key );
60 :
61 0 : fd_tlsrec_conn_init( &http->tls_conn, &http->tls, 0 );
62 0 : fd_tlsrec_sock_init( http->tls_sock );
63 :
64 0 : return 0;
65 0 : }
66 :
67 : static int
68 : http_connect_tls( fd_sshttp_t * http,
69 0 : long now ) {
70 0 : if( FD_UNLIKELY( now > http->deadline ) ) {
71 0 : FD_LOG_WARNING(( "TLS handshake timeout" ));
72 0 : fd_sshttp_cancel( http );
73 0 : return FD_SSHTTP_ADVANCE_ERROR;
74 0 : }
75 :
76 : /* Flush any buffered outgoing data (e.g., ClientHello from a
77 : previous call where the TCP connect was still in progress). */
78 :
79 0 : int flush = fd_tlsrec_sock_flush( http->tls_sock, http->sockfd );
80 0 : if( flush<0 ) { fd_sshttp_cancel( http ); return FD_SSHTTP_ADVANCE_ERROR; }
81 0 : if( flush>0 ) return FD_SSHTTP_ADVANCE_AGAIN;
82 :
83 : /* Drive the TLS handshake */
84 :
85 0 : ulong tcp_rx_sz;
86 0 : int err = fd_tlsrec_sock_rx( http->tls_sock, &http->tls_conn, http->sockfd, &tcp_rx_sz );
87 0 : if( FD_UNLIKELY( err ) ) {
88 0 : if( err==FD_TLSREC_SOCK_ERR_EOF ) {
89 0 : FD_LOG_WARNING(( "peer " FD_IP4_ADDR_FMT ":%hu closed the connection during TLS handshake",
90 0 : FD_IP4_ADDR_FMT_ARGS( http->addr.addr ), fd_ushort_bswap( http->addr.port ) ));
91 0 : } else {
92 0 : FD_LOG_WARNING(( "TLS handshake failed (%d-%s)", err, fd_tlsrec_sock_strerror( err ) ));
93 0 : }
94 0 : fd_sshttp_cancel( http );
95 0 : return FD_SSHTTP_ADVANCE_ERROR;
96 0 : }
97 :
98 : /* Transition to request state once handshake completes */
99 :
100 0 : if( fd_tlsrec_conn_is_ready( &http->tls_conn ) ) {
101 0 : http->state = FD_SSHTTP_STATE_REQ;
102 0 : http->deadline = now + FD_SSHTTP_DEADLINE_NANOS;
103 0 : return FD_SSHTTP_ADVANCE_AGAIN;
104 0 : }
105 :
106 0 : if( fd_tlsrec_conn_is_failed( &http->tls_conn ) ) {
107 0 : FD_LOG_WARNING(( "TLS handshake failed" ));
108 0 : fd_sshttp_cancel( http );
109 0 : return FD_SSHTTP_ADVANCE_ERROR;
110 0 : }
111 :
112 0 : return FD_SSHTTP_ADVANCE_AGAIN;
113 0 : }
114 :
115 : static long
116 : http_send_tls( fd_sshttp_t * http,
117 : void * buf,
118 0 : ulong bufsz ) {
119 0 : int flush = fd_tlsrec_sock_flush( http->tls_sock, http->sockfd );
120 0 : if( flush<0 ) return FD_SSHTTP_ADVANCE_ERROR;
121 0 : if( flush>0 ) return FD_SSHTTP_ADVANCE_AGAIN;
122 :
123 0 : ulong consumed;
124 0 : if( FD_UNLIKELY( fd_tlsrec_sock_tx( http->tls_sock, &http->tls_conn, http->sockfd, buf, bufsz, &consumed ) ) )
125 0 : return FD_SSHTTP_ADVANCE_ERROR;
126 0 : return (long)consumed;
127 0 : }
128 :
129 : static long
130 : http_recv_tls( fd_sshttp_t * http,
131 : void * buf,
132 0 : ulong bufsz ) {
133 :
134 : /* Drain any buffered plaintext from a previous call */
135 :
136 0 : ulong n = fd_tlsrec_sock_rx_pop( http->tls_sock, buf, bufsz );
137 0 : if( n ) {
138 0 : return (long)n;
139 0 : }
140 :
141 : /* Drain pending ciphertext before generating any new records */
142 :
143 0 : int flush = fd_tlsrec_sock_flush( http->tls_sock, http->sockfd );
144 0 : if( flush<0 ) return FD_SSHTTP_ADVANCE_ERROR;
145 0 : if( flush>0 ) return FD_SSHTTP_ADVANCE_AGAIN;
146 :
147 : /* Read and decrypt */
148 :
149 0 : ulong tcp_rx_sz;
150 0 : int err = fd_tlsrec_sock_rx( http->tls_sock, &http->tls_conn, http->sockfd, &tcp_rx_sz );
151 0 : if( FD_UNLIKELY( err ) ) {
152 0 : if( err==FD_TLSREC_SOCK_ERR_EOF ) {
153 0 : FD_LOG_WARNING(( "peer " FD_IP4_ADDR_FMT ":%hu closed the connection mid-response",
154 0 : FD_IP4_ADDR_FMT_ARGS( http->addr.addr ), fd_ushort_bswap( http->addr.port ) ));
155 0 : } else if( err==FD_TLSREC_SOCK_ERR_RECV ) {
156 0 : FD_LOG_WARNING(( "recv() failed (%d-%s) from " FD_IP4_ADDR_FMT ":%hu", errno, fd_io_strerror( errno ),
157 0 : FD_IP4_ADDR_FMT_ARGS( http->addr.addr ), fd_ushort_bswap( http->addr.port ) ));
158 0 : }
159 0 : return FD_SSHTTP_ADVANCE_ERROR;
160 0 : }
161 0 : if( !tcp_rx_sz ) return FD_SSHTTP_ADVANCE_AGAIN;
162 :
163 0 : n = fd_tlsrec_sock_rx_pop( http->tls_sock, buf, bufsz );
164 0 : if( !n ) return FD_SSHTTP_ADVANCE_AGAIN;
165 0 : return (long)n;
166 0 : }
167 :
168 : static int
169 : setup_redirect_tls( fd_sshttp_t * http,
170 0 : long now ) {
171 0 : fd_sshttp_cancel( http );
172 0 : if( FD_UNLIKELY( fd_sshttp_init( http, http->addr, http->hostname, http->is_https, http->location, http->location_len, ULONG_MAX, now ) ) ) {
173 0 : return FD_SSHTTP_ADVANCE_ERROR;
174 0 : }
175 0 : return FD_SSHTTP_ADVANCE_AGAIN;
176 0 : }
177 :
178 : void
179 0 : fd_sshttp_load_ca_store( fd_sshttp_t * sshttp ) {
180 0 : if( FD_UNLIKELY( fd_x509_ca_store_load_system( &sshttp->ca_store )<0L ) ) {
181 0 : FD_LOG_WARNING(( "No CA certificate bundle found, HTTPS snapshot sources will be refused" ));
182 0 : return;
183 0 : }
184 0 : sshttp->ca_store_loaded = 1;
185 0 : }
186 :
187 : FD_FN_CONST ulong
188 0 : fd_sshttp_align( void ) {
189 0 : return alignof(fd_sshttp_t);
190 0 : }
191 :
192 : FD_FN_CONST ulong
193 0 : fd_sshttp_footprint( void ) {
194 0 : ulong l;
195 0 : l = FD_LAYOUT_INIT;
196 0 : l = FD_LAYOUT_APPEND( l, alignof(fd_sshttp_t), sizeof(fd_sshttp_t) );
197 0 : return FD_LAYOUT_FINI( l, fd_sshttp_align() );
198 0 : }
199 :
200 : void *
201 : fd_sshttp_new( void * shmem,
202 0 : int epoll_fd ) {
203 0 : if( FD_UNLIKELY( !shmem ) ) {
204 0 : FD_LOG_WARNING(( "NULL shmem" ));
205 0 : return NULL;
206 0 : }
207 :
208 0 : if( FD_UNLIKELY( !fd_ulong_is_aligned( (ulong)shmem, fd_sshttp_align() ) ) ) {
209 0 : FD_LOG_WARNING(( "unaligned shmem" ));
210 0 : return NULL;
211 0 : }
212 :
213 0 : if( FD_UNLIKELY( epoll_fd==-1 ) ) {
214 0 : FD_LOG_WARNING(( "invalid epoll_fd" ));
215 0 : return NULL;
216 0 : }
217 :
218 0 : FD_SCRATCH_ALLOC_INIT( l, shmem );
219 0 : fd_sshttp_t * sshttp = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_sshttp_t), sizeof(fd_sshttp_t) );
220 :
221 0 : sshttp->state = FD_SSHTTP_STATE_INIT;
222 0 : sshttp->sockfd = -1;
223 0 : sshttp->epoll_fd = epoll_fd;
224 0 : sshttp->content_len = 0UL;
225 0 : fd_cstr_fini( sshttp->snapshot_name );
226 0 : sshttp->resolved_slot = 0UL;
227 0 : fd_memset( sshttp->resolved_hash, 0, FD_HASH_FOOTPRINT );
228 :
229 : /* Initialize native TLS config */
230 0 : fd_tlsrec_sock_init( sshttp->tls_sock );
231 0 : fd_sshttp_tls_init( &sshttp->tls, sshttp );
232 :
233 : /* CA trust store — loaded via fd_sshttp_load_ca_store() during
234 : privileged_init, before the seccomp sandbox locks down. */
235 0 : sshttp->ca_store_loaded = 0;
236 :
237 0 : FD_COMPILER_MFENCE();
238 0 : sshttp->magic = FD_SSHTTP_MAGIC;
239 0 : FD_COMPILER_MFENCE();
240 :
241 0 : return (void *)sshttp;
242 0 : }
243 :
244 : fd_sshttp_t *
245 0 : fd_sshttp_join( void * shhttp ) {
246 0 : if( FD_UNLIKELY( !shhttp ) ) {
247 0 : FD_LOG_WARNING(( "NULL shhttp" ));
248 0 : return NULL;
249 0 : }
250 :
251 0 : if( FD_UNLIKELY( !fd_ulong_is_aligned( (ulong)shhttp, fd_sshttp_align() ) ) ) {
252 0 : FD_LOG_WARNING(( "misaligned shhttp" ));
253 0 : return NULL;
254 0 : }
255 :
256 0 : fd_sshttp_t * sshttp = (fd_sshttp_t *)shhttp;
257 :
258 0 : if( FD_UNLIKELY( sshttp->magic!=FD_SSHTTP_MAGIC ) ) {
259 0 : FD_LOG_WARNING(( "bad magic" ));
260 0 : return NULL;
261 0 : }
262 :
263 0 : return sshttp;
264 0 : }
265 :
266 : /* http_init_ssl removed — replaced by http_init_tls (native fd_tls) */
267 :
268 : int
269 : fd_sshttp_init( fd_sshttp_t * http,
270 : fd_ip4_port_t addr,
271 : char const * hostname,
272 : int is_https,
273 : char const * path,
274 : ulong path_len,
275 : ulong hops,
276 0 : long now ) {
277 0 : FD_TEST( http->state==FD_SSHTTP_STATE_INIT );
278 :
279 0 : ulong hostname_len = strnlen( hostname, sizeof(http->hostname) );
280 0 : if( FD_UNLIKELY( hostname_len==sizeof(http->hostname) ) ) {
281 0 : FD_LOG_WARNING(( "snapshot hostname too long" ));
282 0 : return -1;
283 0 : }
284 : /* Redirects reinitialize using our own hostname buffer. */
285 0 : memmove( http->hostname, hostname, hostname_len+1UL );
286 0 : http->is_https = is_https;
287 :
288 0 : if( FD_LIKELY( is_https ) ) {
289 0 : if( FD_UNLIKELY( !http->ca_store_loaded ) ) {
290 0 : FD_LOG_WARNING(( "Refusing HTTPS snapshot download from %s: no CA trust store loaded", hostname ));
291 0 : return -1;
292 0 : }
293 0 : if( FD_UNLIKELY( http_init_tls( http ) ) ) return -1;
294 0 : }
295 :
296 0 : if( hops!=ULONG_MAX ) {
297 0 : http->hops = hops;
298 0 : fd_cstr_fini( http->snapshot_name );
299 0 : http->resolved_slot = 0UL;
300 0 : fd_memset( http->resolved_hash, 0, FD_HASH_FOOTPRINT );
301 0 : }
302 0 : http->request_sent = 0UL;
303 0 : int fmt_ok;
304 0 : if( FD_LIKELY( is_https ) ) {
305 0 : fmt_ok = fd_cstr_printf_check( http->request, sizeof(http->request), &http->request_len,
306 0 : "GET %.*s HTTP/1.1\r\n"
307 0 : "User-Agent: Firedancer\r\n"
308 0 : "Accept: */*\r\n"
309 0 : "Accept-Encoding: identity\r\n"
310 0 : "Host: %s\r\n\r\n",
311 0 : (int)path_len, path, hostname );
312 0 : } else {
313 0 : fmt_ok = fd_cstr_printf_check( http->request, sizeof(http->request), &http->request_len,
314 0 : "GET %.*s HTTP/1.1\r\n"
315 0 : "User-Agent: Firedancer\r\n"
316 0 : "Accept: */*\r\n"
317 0 : "Accept-Encoding: identity\r\n"
318 0 : "Host: " FD_IP4_ADDR_FMT "\r\n\r\n",
319 0 : (int)path_len, path, FD_IP4_ADDR_FMT_ARGS( addr.addr ) );
320 0 : }
321 0 : if( FD_UNLIKELY( !fmt_ok ) ) {
322 0 : FD_LOG_WARNING(( "HTTP request too long for %.*s", (int)path_len, path ));
323 0 : return -1;
324 0 : }
325 :
326 0 : http->response_len = 0UL;
327 0 : http->content_len = 0UL;
328 0 : http->content_read = 0UL;
329 :
330 0 : http->addr = addr;
331 0 : http->sockfd = socket( AF_INET, SOCK_STREAM|SOCK_NONBLOCK, 0 );
332 0 : if( FD_UNLIKELY( -1==http->sockfd ) ) {
333 0 : FD_LOG_WARNING(( "socket() failed (%d-%s) for " FD_IP4_ADDR_FMT ":%hu", errno, fd_io_strerror( errno ),
334 0 : FD_IP4_ADDR_FMT_ARGS( http->addr.addr ), fd_ushort_bswap( http->addr.port ) ));
335 0 : return -1;
336 0 : }
337 :
338 0 : struct sockaddr_in addr_in = {
339 0 : .sin_family = AF_INET,
340 0 : .sin_port = addr.port,
341 0 : .sin_addr = { .s_addr = addr.addr }
342 0 : };
343 :
344 0 : if( FD_LIKELY( -1==connect( http->sockfd, fd_type_pun_const( &addr_in ), sizeof(addr_in) ) ) ) {
345 0 : if( FD_UNLIKELY( errno!=EINPROGRESS ) ) {
346 0 : FD_LOG_WARNING(( "connect() failed (%d-%s) to " FD_IP4_ADDR_FMT ":%hu", errno, fd_io_strerror( errno ),
347 0 : FD_IP4_ADDR_FMT_ARGS( http->addr.addr ), fd_ushort_bswap( http->addr.port ) ));
348 0 : if( FD_UNLIKELY( -1==close( http->sockfd ) ) ) FD_LOG_ERR(( "close() failed (%d-%s) for " FD_IP4_ADDR_FMT ":%hu", errno, fd_io_strerror( errno ),
349 0 : FD_IP4_ADDR_FMT_ARGS( http->addr.addr ), fd_ushort_bswap( http->addr.port ) ));
350 0 : http->sockfd = -1;
351 0 : return -1;
352 0 : }
353 0 : }
354 :
355 0 : http->epoll_events = EPOLLIN|EPOLLOUT;
356 0 : struct epoll_event ev = { .events = http->epoll_events, .data.fd = http->sockfd };
357 0 : if( FD_UNLIKELY( -1==epoll_ctl( http->epoll_fd, EPOLL_CTL_ADD, http->sockfd, &ev ) ) ) FD_LOG_ERR(( "epoll_ctl(ADD) failed (%d-%s)", errno, fd_io_strerror( errno ) ));
358 :
359 0 : if( FD_LIKELY( is_https ) ) {
360 0 : http->state = FD_SSHTTP_STATE_CONNECT;
361 0 : http->deadline = now + FD_SSHTTP_DEADLINE_NANOS;
362 0 : } else {
363 0 : http->state = FD_SSHTTP_STATE_REQ;
364 0 : http->deadline = now + FD_SSHTTP_DEADLINE_NANOS;
365 0 : }
366 :
367 0 : return 0;
368 0 : }
369 :
370 : long
371 0 : fd_sshttp_deadline( fd_sshttp_t const * http ) {
372 0 : switch( http->state ) {
373 0 : case FD_SSHTTP_STATE_INIT: return LONG_MAX;
374 0 : case FD_SSHTTP_STATE_REDIRECT:
375 0 : case FD_SSHTTP_STATE_DONE: return 0L; /* non-I/O work pending */
376 0 : case FD_SSHTTP_STATE_DL:
377 : /* Body bytes already buffered (response residual or decrypted TLS
378 : plaintext) don't keep the socket readable: run now. Otherwise
379 : the caller bounds download progress. */
380 0 : if( http->response_len || (http->is_https && fd_tlsrec_sock_rx_avail( http->tls_sock )) ) return 0L;
381 0 : return LONG_MAX;
382 0 : default: return http->deadline;
383 0 : }
384 0 : }
385 :
386 : void
387 0 : fd_sshttp_cancel( fd_sshttp_t * http ) {
388 0 : if( FD_LIKELY( http->state!=FD_SSHTTP_STATE_INIT && -1!=http->sockfd ) ) {
389 0 : if( FD_UNLIKELY( -1==epoll_ctl( http->epoll_fd, EPOLL_CTL_DEL, http->sockfd, NULL ) ) ) FD_LOG_ERR(( "epoll_ctl(DEL) failed (%d-%s)", errno, fd_io_strerror( errno ) ));
390 0 : if( FD_UNLIKELY( -1==close( http->sockfd ) ) ) FD_LOG_ERR(( "close() failed (%d-%s) for " FD_IP4_ADDR_FMT ":%hu", errno, fd_io_strerror( errno ),
391 0 : FD_IP4_ADDR_FMT_ARGS( http->addr.addr ), fd_ushort_bswap( http->addr.port ) ));
392 0 : http->sockfd = -1;
393 0 : }
394 0 : http->state = FD_SSHTTP_STATE_INIT;
395 :
396 : /* Clean up native TLS state */
397 0 : fd_tlsrec_sock_init( http->tls_sock );
398 0 : }
399 :
400 : static long
401 : http_send( fd_sshttp_t * http,
402 : void * buf,
403 0 : ulong bufsz ) {
404 0 : if( FD_LIKELY( http->is_https ) )
405 0 : return http_send_tls( http, buf, bufsz );
406 :
407 0 : long sent = sendto( http->sockfd, buf, bufsz, MSG_NOSIGNAL, NULL, 0 );
408 0 : if( FD_UNLIKELY( -1==sent && errno==EAGAIN ) ) {
409 0 : return FD_SSHTTP_ADVANCE_AGAIN;
410 0 : } else if( FD_UNLIKELY( -1==sent ) ) {
411 0 : FD_LOG_WARNING(( "sendto() failed (%d-%s) to " FD_IP4_ADDR_FMT ":%hu", errno, fd_io_strerror( errno ),
412 0 : FD_IP4_ADDR_FMT_ARGS( http->addr.addr ), fd_ushort_bswap( http->addr.port ) ));
413 0 : fd_sshttp_cancel( http );
414 0 : return FD_SSHTTP_ADVANCE_ERROR;
415 0 : }
416 :
417 0 : return sent;
418 0 : }
419 :
420 : static long
421 : http_recv( fd_sshttp_t * http,
422 : void * buf,
423 0 : ulong bufsz ) {
424 0 : if( FD_LIKELY( http->is_https ) )
425 0 : return http_recv_tls( http, buf, bufsz );
426 :
427 0 : long read = recvfrom( http->sockfd, buf, bufsz, 0, NULL, NULL );
428 0 : if( FD_UNLIKELY( -1==read && errno==EAGAIN ) ) {
429 0 : return FD_SSHTTP_ADVANCE_AGAIN;
430 0 : } else if( FD_UNLIKELY( -1==read ) ) {
431 0 : FD_LOG_WARNING(( "recv() failed (%d-%s) from " FD_IP4_ADDR_FMT ":%hu", errno, fd_io_strerror( errno ),
432 0 : FD_IP4_ADDR_FMT_ARGS( http->addr.addr ), fd_ushort_bswap( http->addr.port ) ));
433 0 : fd_sshttp_cancel( http );
434 0 : return FD_SSHTTP_ADVANCE_ERROR;
435 0 : } else if( FD_UNLIKELY( !read ) ) {
436 0 : FD_LOG_WARNING(( "peer " FD_IP4_ADDR_FMT ":%hu closed the connection mid-response",
437 0 : FD_IP4_ADDR_FMT_ARGS( http->addr.addr ), fd_ushort_bswap( http->addr.port ) ));
438 0 : fd_sshttp_cancel( http );
439 0 : return FD_SSHTTP_ADVANCE_ERROR;
440 0 : }
441 :
442 0 : return read;
443 0 : }
444 :
445 : static int
446 : send_request( fd_sshttp_t * http,
447 0 : long now ) {
448 0 : if( FD_UNLIKELY( now>http->deadline ) ) {
449 0 : FD_LOG_WARNING(( "timeout sending request to " FD_IP4_ADDR_FMT ":%hu",
450 0 : FD_IP4_ADDR_FMT_ARGS( http->addr.addr ), fd_ushort_bswap( http->addr.port ) ));
451 0 : fd_sshttp_cancel( http );
452 0 : return FD_SSHTTP_ADVANCE_ERROR;
453 0 : }
454 :
455 0 : long sent = http_send( http, http->request+http->request_sent, http->request_len-http->request_sent );
456 0 : if( FD_UNLIKELY( sent<=0 ) ) return (int)sent;
457 :
458 0 : http->request_sent += (ulong)sent;
459 0 : if( FD_UNLIKELY( http->request_sent==http->request_len ) ) {
460 0 : http->state = FD_SSHTTP_STATE_RESP;
461 0 : http->response_len = 0UL;
462 0 : http->deadline = now + FD_SSHTTP_DEADLINE_NANOS;
463 0 : }
464 :
465 0 : return FD_SSHTTP_ADVANCE_AGAIN;
466 0 : }
467 :
468 : static int
469 : follow_redirect( fd_sshttp_t * http,
470 : struct phr_header * headers,
471 : ulong header_cnt,
472 0 : long now ) {
473 0 : if( FD_UNLIKELY( !http->hops ) ) {
474 0 : FD_LOG_WARNING(( "too many redirects (remaining %lu) from " FD_IP4_ADDR_FMT ":%hu", http->hops,
475 0 : FD_IP4_ADDR_FMT_ARGS( http->addr.addr ), fd_ushort_bswap( http->addr.port ) ));
476 0 : fd_sshttp_cancel( http );
477 0 : return FD_SSHTTP_ADVANCE_ERROR;
478 0 : }
479 : /* The check above guarantees hops>0. */
480 0 : http->hops--;
481 :
482 0 : ulong location_len = 0UL;
483 0 : char const * location = NULL;
484 :
485 0 : for( ulong i=0UL; i<header_cnt; i++ ) {
486 0 : if( FD_UNLIKELY( headers[ i ].name_len == 8 && !strncasecmp( headers[ i ].name, "location", headers[ i ].name_len ) ) ) {
487 0 : if( FD_UNLIKELY( !headers [ i ].value_len || headers[ i ].value[ 0 ]!='/' ) ) {
488 0 : FD_LOG_WARNING(( "invalid location header `%.*s` from " FD_IP4_ADDR_FMT ":%hu", (int)headers[ i ].value_len, headers[ i ].value,
489 0 : FD_IP4_ADDR_FMT_ARGS( http->addr.addr ), fd_ushort_bswap( http->addr.port ) ));
490 0 : fd_sshttp_cancel( http );
491 0 : return FD_SSHTTP_ADVANCE_ERROR;
492 0 : }
493 :
494 0 : location_len = headers[ i ].value_len;
495 0 : location = headers[ i ].value;
496 :
497 0 : if( FD_UNLIKELY( location_len>=PATH_MAX-1UL ) ) {
498 0 : FD_LOG_WARNING(( "location header too long `%.*s` from " FD_IP4_ADDR_FMT ":%hu", (int)location_len, location,
499 0 : FD_IP4_ADDR_FMT_ARGS( http->addr.addr ), fd_ushort_bswap( http->addr.port ) ));
500 0 : fd_sshttp_cancel( http );
501 0 : return FD_SSHTTP_ADVANCE_ERROR;
502 0 : }
503 :
504 0 : char snapshot_name[ PATH_MAX ];
505 0 : fd_memcpy( snapshot_name, location+1UL, location_len-1UL );
506 0 : snapshot_name[ location_len-1UL ] = '\0';
507 :
508 0 : int is_zstd;
509 0 : ulong full_entry_slot, incremental_entry_slot;
510 0 : uchar decoded_hash[ FD_HASH_FOOTPRINT ];
511 0 : int err = fd_ssarchive_parse_filename( snapshot_name, &full_entry_slot, &incremental_entry_slot, decoded_hash, &is_zstd );
512 :
513 0 : if( FD_UNLIKELY( err || !is_zstd ) ) {
514 0 : FD_LOG_WARNING(( "unrecognized snapshot file `%s` in redirect location header from " FD_IP4_ADDR_FMT ":%hu", snapshot_name,
515 0 : FD_IP4_ADDR_FMT_ARGS( http->addr.addr ), fd_ushort_bswap( http->addr.port ) ));
516 0 : fd_sshttp_cancel( http );
517 0 : return FD_SSHTTP_ADVANCE_ERROR;
518 0 : }
519 :
520 0 : http->resolved_slot = (incremental_entry_slot!=ULONG_MAX)
521 0 : ? incremental_entry_slot : full_entry_slot;
522 0 : fd_memcpy( http->resolved_hash, decoded_hash, FD_HASH_FOOTPRINT );
523 :
524 0 : char encoded_hash[ FD_BASE58_ENCODED_32_SZ ];
525 0 : fd_base58_encode_32( decoded_hash, NULL, encoded_hash );
526 :
527 0 : if( FD_LIKELY( incremental_entry_slot!=ULONG_MAX ) ) {
528 0 : FD_TEST( fd_cstr_printf_check( http->snapshot_name, PATH_MAX, NULL, "incremental-snapshot-%lu-%lu-%s.tar.zst", full_entry_slot, incremental_entry_slot, encoded_hash ) );
529 0 : } else {
530 0 : FD_TEST( fd_cstr_printf_check( http->snapshot_name, PATH_MAX, NULL, "snapshot-%lu-%s.tar.zst", full_entry_slot, encoded_hash ) );
531 0 : }
532 0 : break;
533 0 : }
534 0 : }
535 :
536 0 : if( FD_UNLIKELY( !location_len ) ) {
537 0 : FD_LOG_WARNING(( "no location header in redirect response from " FD_IP4_ADDR_FMT ":%hu",
538 0 : FD_IP4_ADDR_FMT_ARGS( http->addr.addr ), fd_ushort_bswap( http->addr.port ) ));
539 0 : fd_sshttp_cancel( http );
540 0 : return FD_SSHTTP_ADVANCE_ERROR;
541 0 : }
542 :
543 : /* Pre-validate that the redirect request will fit in the request
544 : buffer. The request is rebuilt from scratch by fd_sshttp_init
545 : during the redirect, but the format must match so that a path
546 : accepted here will not overflow in fd_sshttp_init. */
547 0 : int pre_check;
548 0 : if( FD_LIKELY( http->is_https ) ) {
549 0 : pre_check = fd_cstr_printf_check( http->request, sizeof(http->request), &http->request_len,
550 0 : "GET %.*s HTTP/1.1\r\n"
551 0 : "User-Agent: Firedancer\r\n"
552 0 : "Accept: */*\r\n"
553 0 : "Accept-Encoding: identity\r\n"
554 0 : "Host: %s\r\n\r\n",
555 0 : (int)location_len, location, http->hostname );
556 0 : } else {
557 0 : pre_check = fd_cstr_printf_check( http->request, sizeof(http->request), &http->request_len,
558 0 : "GET %.*s HTTP/1.1\r\n"
559 0 : "User-Agent: Firedancer\r\n"
560 0 : "Accept: */*\r\n"
561 0 : "Accept-Encoding: identity\r\n"
562 0 : "Host: " FD_IP4_ADDR_FMT "\r\n\r\n",
563 0 : (int)location_len, location, FD_IP4_ADDR_FMT_ARGS( http->addr.addr ) );
564 0 : }
565 0 : if( FD_UNLIKELY( !pre_check ) ) {
566 0 : FD_LOG_WARNING(( "redirect request too long `%.*s` from " FD_IP4_ADDR_FMT ":%hu", (int)location_len, location,
567 0 : FD_IP4_ADDR_FMT_ARGS( http->addr.addr ), fd_ushort_bswap( http->addr.port ) ));
568 0 : fd_sshttp_cancel( http );
569 0 : return FD_SSHTTP_ADVANCE_ERROR;
570 0 : }
571 :
572 0 : FD_LOG_INFO(( "following redirect to %s://" FD_IP4_ADDR_FMT ":%hu%.*s", http->is_https ? "https" : "http",
573 0 : FD_IP4_ADDR_FMT_ARGS( http->addr.addr ), fd_ushort_bswap( http->addr.port ),
574 0 : (int)location_len, location ));
575 :
576 0 : if( FD_UNLIKELY( http->is_https ) ) {
577 0 : http->state = FD_SSHTTP_STATE_REDIRECT;
578 0 : http->location_len = location_len;
579 0 : FD_TEST( location_len<PATH_MAX-1UL );
580 0 : fd_memcpy( http->location, location, location_len );
581 0 : http->location[ location_len ] = '\0';
582 0 : } else {
583 0 : if( FD_LIKELY( !fd_sshttp_fuzz ) ) {
584 0 : fd_sshttp_cancel( http );
585 0 : if( FD_UNLIKELY( fd_sshttp_init( http, http->addr, http->hostname, http->is_https, location, location_len, ULONG_MAX, now ) ) ) {
586 0 : return FD_SSHTTP_ADVANCE_ERROR;
587 0 : }
588 0 : } else {
589 0 : http->state = FD_SSHTTP_STATE_RESP;
590 0 : http->response_len = 0UL;
591 0 : }
592 0 : }
593 :
594 0 : return FD_SSHTTP_ADVANCE_AGAIN;
595 0 : }
596 :
597 : static int
598 : read_response( fd_sshttp_t * http,
599 : ulong * data_len,
600 : uchar * data,
601 0 : long now ) {
602 0 : if( FD_UNLIKELY( now>http->deadline ) ) {
603 0 : FD_LOG_WARNING(( "timeout reading response from " FD_IP4_ADDR_FMT ":%hu",
604 0 : FD_IP4_ADDR_FMT_ARGS( http->addr.addr ), fd_ushort_bswap( http->addr.port ) ));
605 0 : fd_sshttp_cancel( http );
606 0 : return FD_SSHTTP_ADVANCE_ERROR;
607 0 : }
608 :
609 0 : if( FD_UNLIKELY( http->response_len>=sizeof(http->response) ) ) {
610 0 : FD_LOG_WARNING(( "response headers too large from " FD_IP4_ADDR_FMT ":%hu",
611 0 : FD_IP4_ADDR_FMT_ARGS( http->addr.addr ), fd_ushort_bswap( http->addr.port ) ));
612 0 : fd_sshttp_cancel( http );
613 0 : return FD_SSHTTP_ADVANCE_ERROR;
614 0 : }
615 :
616 0 : long read = http_recv( http, http->response+http->response_len, sizeof(http->response)-http->response_len );
617 0 : if( FD_UNLIKELY( read<=0 ) ) return (int)read;
618 :
619 0 : http->response_len += (ulong)read;
620 :
621 0 : int minor_version;
622 0 : int status;
623 0 : const char * message;
624 0 : ulong message_len;
625 0 : struct phr_header headers[ 128UL ];
626 0 : ulong header_cnt = 128UL;
627 0 : int parsed = phr_parse_response( http->response,
628 0 : http->response_len,
629 0 : &minor_version,
630 0 : &status,
631 0 : &message,
632 0 : &message_len,
633 0 : headers,
634 0 : &header_cnt,
635 0 : http->response_len - (ulong)read );
636 0 : if( FD_UNLIKELY( parsed==-1 ) ) {
637 0 : FD_LOG_WARNING(( "malformed response headers from " FD_IP4_ADDR_FMT ":%hu",
638 0 : FD_IP4_ADDR_FMT_ARGS( http->addr.addr ), fd_ushort_bswap( http->addr.port ) ));
639 0 : fd_sshttp_cancel( http );
640 0 : return FD_SSHTTP_ADVANCE_ERROR;
641 0 : } else if( parsed==-2 ) {
642 0 : return FD_SSHTTP_ADVANCE_AGAIN;
643 0 : }
644 :
645 0 : int is_redirect = (status==301) | (status==302) | (status==303) | (status==307) | (status==308);
646 0 : if( FD_UNLIKELY( is_redirect ) ) {
647 0 : return follow_redirect( http, headers, header_cnt, now );
648 0 : }
649 :
650 0 : if( FD_UNLIKELY( status!=200 ) ) {
651 0 : FD_LOG_WARNING(( "unexpected response status %d %.*s from " FD_IP4_ADDR_FMT ":%hu", status, (int)message_len, message,
652 0 : FD_IP4_ADDR_FMT_ARGS( http->addr.addr ), fd_ushort_bswap( http->addr.port ) ));
653 0 : fd_sshttp_cancel( http );
654 0 : return FD_SSHTTP_ADVANCE_ERROR;
655 0 : }
656 :
657 0 : http->content_read = 0UL;
658 0 : http->content_len = ULONG_MAX;
659 0 : for( ulong i=0UL; i<header_cnt; i++ ) {
660 0 : if( FD_LIKELY( headers[i].name_len!=14UL ) ) continue;
661 0 : if( FD_LIKELY( strncasecmp( headers[i].name, "content-length", 14UL ) ) ) continue;
662 :
663 0 : ulong val = 0UL;
664 0 : if( FD_UNLIKELY( fd_http_parse_content_len( headers[i].value, (ulong)headers[i].value_len, &val ) || val==0UL ) ) {
665 0 : FD_LOG_WARNING(( "invalid content-length in response from " FD_IP4_ADDR_FMT ":%hu", FD_IP4_ADDR_FMT_ARGS( http->addr.addr ), fd_ushort_bswap( http->addr.port ) ));
666 0 : fd_sshttp_cancel( http );
667 0 : return FD_SSHTTP_ADVANCE_ERROR;
668 0 : }
669 0 : http->content_len = val;
670 0 : break;
671 0 : }
672 :
673 0 : if( FD_UNLIKELY( http->content_len==ULONG_MAX ) ) {
674 0 : FD_LOG_WARNING(( "no content-length header in response from " FD_IP4_ADDR_FMT ":%hu",
675 0 : FD_IP4_ADDR_FMT_ARGS( http->addr.addr ), fd_ushort_bswap( http->addr.port ) ));
676 0 : fd_sshttp_cancel( http );
677 0 : return FD_SSHTTP_ADVANCE_ERROR;
678 0 : }
679 :
680 0 : http->state = FD_SSHTTP_STATE_DL;
681 0 : if( FD_UNLIKELY( (ulong)parsed<http->response_len ) ) {
682 : /* Body bytes past the caller's buffer are kept in response, with
683 : response_len repurposed as the residual length, drained by
684 : read_body before it reads the socket again. */
685 0 : ulong leftover = fd_ulong_min( http->response_len - (ulong)parsed, http->content_len );
686 0 : ulong copy_len = fd_ulong_min( leftover, *data_len );
687 0 : fd_memcpy( data, http->response+parsed, copy_len );
688 0 : memmove( http->response, http->response+(ulong)parsed+copy_len, leftover-copy_len );
689 0 : http->response_len = leftover-copy_len;
690 0 : http->content_read += copy_len;
691 0 : *data_len = copy_len;
692 0 : return FD_SSHTTP_ADVANCE_DATA;
693 0 : } else {
694 0 : FD_TEST( http->response_len==(ulong)parsed );
695 0 : http->response_len = 0UL;
696 0 : return FD_SSHTTP_ADVANCE_AGAIN;
697 0 : }
698 0 : }
699 :
700 : static int
701 : read_body( fd_sshttp_t * http,
702 : ulong * data_len,
703 : uchar * data,
704 0 : long now ) {
705 0 : if( FD_UNLIKELY( http->content_read>=http->content_len ) ) {
706 0 : if( FD_UNLIKELY( http->is_https ) ) {
707 0 : http->state = FD_SSHTTP_STATE_DONE;
708 0 : http->deadline = now + FD_SSHTTP_DEADLINE_NANOS;
709 0 : return FD_SSHTTP_ADVANCE_AGAIN;
710 0 : } else {
711 0 : fd_sshttp_cancel( http );
712 0 : http->state = FD_SSHTTP_STATE_INIT;
713 0 : return FD_SSHTTP_ADVANCE_DONE;
714 0 : }
715 0 : }
716 :
717 0 : FD_TEST( http->content_read<http->content_len );
718 :
719 0 : if( FD_UNLIKELY( http->response_len ) ) { /* residual body bytes from read_response */
720 0 : ulong copy_len = fd_ulong_min( http->response_len, *data_len );
721 0 : fd_memcpy( data, http->response, copy_len );
722 0 : memmove( http->response, http->response+copy_len, http->response_len-copy_len );
723 0 : http->response_len -= copy_len;
724 0 : http->content_read += copy_len;
725 0 : *data_len = copy_len;
726 0 : return FD_SSHTTP_ADVANCE_DATA;
727 0 : }
728 :
729 0 : long read = http_recv( http, data, fd_ulong_min( *data_len, http->content_len-http->content_read ) );
730 0 : if( FD_UNLIKELY( read<=0 ) ) return (int)read;
731 :
732 0 : *data_len = (ulong)read;
733 0 : http->content_read += (ulong)read;
734 :
735 0 : return FD_SSHTTP_ADVANCE_DATA;
736 0 : }
737 :
738 : char const *
739 0 : fd_sshttp_snapshot_name( fd_sshttp_t const * http ) {
740 0 : return http->snapshot_name;
741 0 : }
742 :
743 : ulong
744 0 : fd_sshttp_content_len( fd_sshttp_t const * http ) {
745 0 : return http->content_len;
746 0 : }
747 :
748 : ulong
749 0 : fd_sshttp_resolved_slot( fd_sshttp_t const * http ) {
750 0 : return http->resolved_slot;
751 0 : }
752 :
753 : uchar const *
754 0 : fd_sshttp_resolved_hash( fd_sshttp_t const * http ) {
755 0 : return http->resolved_hash;
756 0 : }
757 :
758 : int
759 : fd_sshttp_advance( fd_sshttp_t * http,
760 : ulong * data_len,
761 : uchar * data,
762 : int * downloading,
763 0 : long now ) {
764 0 : *downloading = 0;
765 0 : int res;
766 0 : switch( http->state ) {
767 0 : case FD_SSHTTP_STATE_INIT: return FD_SSHTTP_ADVANCE_AGAIN;
768 0 : case FD_SSHTTP_STATE_CONNECT: res = http_connect_tls( http, now ); break;
769 0 : case FD_SSHTTP_STATE_REDIRECT: res = setup_redirect_tls( http, now ); break;
770 0 : case FD_SSHTTP_STATE_REQ: res = send_request( http, now ); break;
771 0 : case FD_SSHTTP_STATE_RESP: res = read_response( http, data_len, data, now ); break;
772 0 : case FD_SSHTTP_STATE_DL: *downloading = 1; res = read_body( http, data_len, data, now ); break;
773 0 : case FD_SSHTTP_STATE_DONE:
774 0 : fd_sshttp_cancel( http );
775 0 : http->state = FD_SSHTTP_STATE_INIT;
776 0 : return FD_SSHTTP_ADVANCE_DONE;
777 0 : default: return FD_SSHTTP_ADVANCE_ERROR;
778 0 : }
779 :
780 : /* Only wait for writability while there is something to send,
781 : otherwise a connected socket is always ready and the tile spins. */
782 0 : if( FD_LIKELY( http->sockfd!=-1 ) ) {
783 0 : int out = http->state==FD_SSHTTP_STATE_REQ || (http->is_https && fd_tlsrec_sock_tx_pending( http->tls_sock ));
784 0 : uint events = EPOLLIN | (out ? EPOLLOUT : 0U);
785 0 : if( FD_UNLIKELY( events!=http->epoll_events ) ) {
786 0 : struct epoll_event ev = { .events = events, .data.fd = http->sockfd };
787 0 : if( FD_UNLIKELY( -1==epoll_ctl( http->epoll_fd, EPOLL_CTL_MOD, http->sockfd, &ev ) ) ) FD_LOG_ERR(( "epoll_ctl(MOD) failed (%d-%s)", errno, fd_io_strerror( errno ) ));
788 0 : http->epoll_events = events;
789 0 : }
790 0 : }
791 0 : return res;
792 0 : }
|