Skip to content

Commit 76be190

Browse files
committed
Clean up net_hdrs
- Fix alignment of ip4_hdr and udp_hdr - Rename net_hdrs to ip4_udp_hdrs - Use strongly typed syntax for ip4_udp_hdrs to avoid pointer bugs
1 parent 6116d7b commit 76be190

File tree

9 files changed

+149
-149
lines changed

9 files changed

+149
-149
lines changed

src/disco/shred/fd_shred_tile.c

+17-21
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,8 @@ typedef struct {
145145

146146
int skip_frag;
147147

148-
fd_net_hdrs_t data_shred_net_hdr [1];
149-
fd_net_hdrs_t parity_shred_net_hdr[1];
148+
fd_ip4_udp_hdrs_t data_shred_net_hdr [1];
149+
fd_ip4_udp_hdrs_t parity_shred_net_hdr[1];
150150

151151
fd_wksp_t * shred_store_wksp;
152152

@@ -499,28 +499,24 @@ send_shred( fd_shred_ctx_t * ctx,
499499
uchar * packet = fd_chunk_to_laddr( ctx->net_out_mem, ctx->net_out_chunk );
500500

501501
int is_data = fd_shred_type( shred->variant )==FD_SHRED_TYPE_MERKLE_DATA;
502-
fd_net_hdrs_t * tmpl = fd_ptr_if( is_data, (fd_net_hdrs_t *)ctx->data_shred_net_hdr,
503-
(fd_net_hdrs_t *)ctx->parity_shred_net_hdr );
504-
fd_memcpy( packet, tmpl, sizeof(fd_net_hdrs_t) );
502+
fd_ip4_udp_hdrs_t * hdr = (fd_ip4_udp_hdrs_t *)packet;
503+
*hdr = *( is_data ? ctx->data_shred_net_hdr : ctx->parity_shred_net_hdr );
505504

506-
fd_net_hdrs_t * hdr = (fd_net_hdrs_t *)packet;
505+
fd_ip4_hdr_t * ip4 = hdr->ip4;
506+
ip4->daddr = dest->ip4;
507+
ip4->net_id = fd_ushort_bswap( ctx->net_id++ );
508+
ip4->check = 0U;
509+
ip4->check = fd_ip4_hdr_check_fast( ip4 );
507510

508-
hdr->ip4->daddr = dest->ip4;
509-
hdr->ip4->net_id = fd_ushort_bswap( ctx->net_id++ );
510-
hdr->ip4->check = 0U;
511-
hdr->ip4->check = fd_ip4_hdr_check_fast( packet+14 );
512-
513-
hdr->udp->net_dport = fd_ushort_bswap( dest->port );
511+
hdr->udp->net_dport = fd_ushort_bswap( dest->port );
514512

515513
ulong shred_sz = fd_ulong_if( is_data, FD_SHRED_MIN_SZ, FD_SHRED_MAX_SZ );
516-
fd_memcpy( packet+sizeof(fd_net_hdrs_t), shred, shred_sz );
517-
518-
ulong pkt_sz = shred_sz + sizeof(fd_net_hdrs_t);
514+
fd_memcpy( packet+sizeof(fd_ip4_udp_hdrs_t), shred, shred_sz );
519515

520-
ulong tspub = fd_frag_meta_ts_comp( fd_tickcount() );
521-
ulong sig = fd_disco_netmux_sig( dest->ip4, dest->port, dest->ip4, DST_PROTO_OUTGOING, FD_NETMUX_SIG_MIN_HDR_SZ );
522-
fd_mcache_publish( ctx->net_out_mcache, ctx->net_out_depth, ctx->net_out_seq, sig, ctx->net_out_chunk,
523-
pkt_sz, 0UL, tsorig, tspub );
516+
ulong pkt_sz = shred_sz + sizeof(fd_ip4_udp_hdrs_t);
517+
ulong tspub = fd_frag_meta_ts_comp( fd_tickcount() );
518+
ulong sig = fd_disco_netmux_sig( dest->ip4, dest->port, dest->ip4, DST_PROTO_OUTGOING, sizeof(fd_ip4_udp_hdrs_t) );
519+
fd_mcache_publish( ctx->net_out_mcache, ctx->net_out_depth, ctx->net_out_seq, sig, ctx->net_out_chunk, pkt_sz, 0UL, tsorig, tspub );
524520
ctx->net_out_seq = fd_seq_inc( ctx->net_out_seq, 1UL );
525521
ctx->net_out_chunk = fd_dcache_compact_next( ctx->net_out_chunk, pkt_sz, ctx->net_out_chunk0, ctx->net_out_wmark );
526522
}
@@ -835,8 +831,8 @@ unprivileged_init( fd_topo_t * topo,
835831

836832
ctx->net_id = (ushort)0;
837833

838-
fd_net_create_packet_header_template( ctx->data_shred_net_hdr, FD_SHRED_MIN_SZ, 0, tile->shred.shred_listen_port );
839-
fd_net_create_packet_header_template( ctx->parity_shred_net_hdr, FD_SHRED_MAX_SZ, 0, tile->shred.shred_listen_port );
834+
fd_ip4_udp_hdr_init( ctx->data_shred_net_hdr, FD_SHRED_MIN_SZ, 0, tile->shred.shred_listen_port );
835+
fd_ip4_udp_hdr_init( ctx->parity_shred_net_hdr, FD_SHRED_MAX_SZ, 0, tile->shred.shred_listen_port );
840836

841837
for( ulong i=0UL; i<tile->in_cnt; i++ ) {
842838
fd_topo_link_t const * link = &topo->links[ tile->in_link_id[ i ] ];

src/discof/eqvoc/fd_eqvoc_tile.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ during_frag( fd_eqvoc_tile_ctx_t * ctx,
152152

153153
uchar * packet = fd_chunk_to_laddr( ctx->shred_net_in_mem, chunk );
154154
// memcpy( packet + sizeof(fd_net_hdrs_t), packet, sizeof(fd_shred_t) );
155-
fd_shred_t * shred = (fd_shred_t *)(packet + sizeof(fd_net_hdrs_t));
155+
fd_shred_t * shred = (fd_shred_t *)( packet + fd_disco_netmux_sig_hdr_sz( sig ) );
156156
memcpy( &ctx->shred, shred, sizeof(fd_shred_t) );
157157
}
158158
}

src/discof/gossip/fd_gossip_tile.c

+22-24
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ struct fd_gossip_tile_ctx {
200200
uchar gossip_buffer[ FD_NET_MTU ];
201201

202202
ushort net_id;
203-
fd_net_hdrs_t hdr[1];
203+
fd_ip4_udp_hdrs_t hdr[1];
204204

205205
fd_keyguard_client_t keyguard_client[1];
206206

@@ -250,28 +250,26 @@ send_packet( fd_gossip_tile_ctx_t * ctx,
250250
ulong tsorig ) {
251251
uchar * packet = fd_chunk_to_laddr( ctx->net_out_mem, ctx->net_out_chunk );
252252

253-
fd_memcpy( packet, ctx->hdr, sizeof(fd_net_hdrs_t) );
254-
fd_net_hdrs_t * hdr = (fd_net_hdrs_t *)packet;
255-
256-
hdr->udp->net_dport = dst_port;
257-
258-
memset( hdr->eth->dst, 0U, 6UL );
259-
memcpy( hdr->ip4->daddr_c, &dst_ip_addr, 4UL );
260-
hdr->ip4->net_id = fd_ushort_bswap( ctx->net_id++ );
261-
hdr->ip4->check = 0U;
262-
hdr->ip4->net_tot_len = fd_ushort_bswap( (ushort)(payload_sz + sizeof(fd_ip4_hdr_t)+sizeof(fd_udp_hdr_t)) );
263-
hdr->ip4->check = fd_ip4_hdr_check_fast( hdr->buf+14 );
264-
265-
ulong packet_sz = payload_sz + sizeof(fd_net_hdrs_t);
266-
fd_memcpy( packet+sizeof(fd_net_hdrs_t), payload, payload_sz );
267-
hdr->udp->net_len = fd_ushort_bswap( (ushort)(payload_sz + sizeof(fd_udp_hdr_t)) );
268-
hdr->udp->check = fd_ip4_udp_check( hdr->ip4->saddr,
269-
hdr->ip4->daddr,
270-
(fd_udp_hdr_t const *)(hdr->buf + 34),
271-
packet + sizeof(fd_net_hdrs_t) );
272-
273-
ulong tspub = fd_frag_meta_ts_comp( fd_tickcount() );
274-
ulong sig = fd_disco_netmux_sig( dst_ip_addr, dst_port, dst_ip_addr, DST_PROTO_OUTGOING, FD_NETMUX_SIG_MIN_HDR_SZ );
253+
fd_ip4_udp_hdrs_t * hdr = (fd_ip4_udp_hdrs_t *)packet;
254+
*hdr = *ctx->hdr;
255+
256+
fd_ip4_hdr_t * ip4 = hdr->ip4;
257+
ip4->daddr = dst_ip_addr;
258+
ip4->net_id = fd_ushort_bswap( ctx->net_id++ );
259+
ip4->check = 0U;
260+
ip4->net_tot_len = fd_ushort_bswap( (ushort)(payload_sz + sizeof(fd_ip4_hdr_t)+sizeof(fd_udp_hdr_t)) );
261+
ip4->check = fd_ip4_hdr_check_fast( ip4 );
262+
263+
fd_udp_hdr_t * udp = hdr->udp;
264+
udp->net_dport = dst_port;
265+
udp->net_len = fd_ushort_bswap( (ushort)(payload_sz + sizeof(fd_udp_hdr_t)) );
266+
fd_memcpy( packet+sizeof(fd_ip4_udp_hdrs_t), payload, payload_sz );
267+
udp->check = fd_ip4_udp_check( ip4->saddr, ip4->daddr, udp,
268+
packet + sizeof(fd_ip4_udp_hdrs_t) );
269+
270+
ulong tspub = fd_frag_meta_ts_comp( fd_tickcount() );
271+
ulong sig = fd_disco_netmux_sig( dst_ip_addr, dst_port, dst_ip_addr, DST_PROTO_OUTGOING, sizeof(fd_ip4_udp_hdrs_t) );
272+
ulong packet_sz = payload_sz + sizeof(fd_ip4_udp_hdrs_t);
275273
fd_stem_publish( ctx->stem, 0UL, sig, ctx->net_out_chunk, packet_sz, 0UL, tsorig, tspub );
276274
ctx->net_out_chunk = fd_dcache_compact_next( ctx->net_out_chunk, packet_sz, ctx->net_out_chunk0, ctx->net_out_wmark );
277275
}
@@ -907,7 +905,7 @@ unprivileged_init( fd_topo_t * topo,
907905

908906
ctx->net_id = (ushort)0;
909907

910-
fd_net_create_packet_header_template( ctx->hdr, FD_NET_MTU, ctx->gossip_my_addr.addr, ctx->gossip_listen_port );
908+
fd_ip4_udp_hdr_init( ctx->hdr, FD_NET_MTU, ctx->gossip_my_addr.addr, ctx->gossip_listen_port );
911909

912910
ctx->last_shred_dest_push_time = 0;
913911
ctx->restart_last_push_time = 0;

src/discof/repair/fd_repair_tile.c

+24-23
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ struct fd_repair_tile_ctx {
9191
ushort net_id;
9292
/* Includes Ethernet, IP, UDP headers */
9393
uchar buffer[ MAX_BUFFER_SIZE ];
94-
fd_net_hdrs_t intake_hdr[1];
95-
fd_net_hdrs_t serve_hdr[1];
94+
fd_ip4_udp_hdrs_t intake_hdr[1];
95+
fd_ip4_udp_hdrs_t serve_hdr [1];
9696

9797
fd_stake_ci_t * stake_ci;
9898

@@ -148,28 +148,29 @@ send_packet( fd_repair_tile_ctx_t * ctx,
148148
ulong payload_sz,
149149
ulong tsorig ) {
150150
uchar * packet = fd_chunk_to_laddr( ctx->net_out_mem, ctx->net_out_chunk );
151-
152-
fd_memcpy( packet, ( is_intake ? ctx->intake_hdr : ctx->serve_hdr ), sizeof(fd_net_hdrs_t) );
153-
fd_net_hdrs_t * hdr = (fd_net_hdrs_t *)packet;
154-
155-
hdr->ip4->saddr = src_ip_addr;
156-
hdr->ip4->daddr = dst_ip_addr;
157-
hdr->ip4->net_id = fd_ushort_bswap( ctx->net_id++ );
158-
hdr->ip4->check = 0U;
159-
hdr->ip4->net_tot_len = fd_ushort_bswap( (ushort)(payload_sz + sizeof(fd_ip4_hdr_t)+sizeof(fd_udp_hdr_t)) );
160-
hdr->ip4->check = fd_ip4_hdr_check_fast( hdr->buf+14 );
161-
162-
ulong packet_sz = payload_sz + sizeof(fd_net_hdrs_t);
163-
fd_memcpy( packet+sizeof(fd_net_hdrs_t), payload, payload_sz );
164-
hdr->udp->net_dport = dst_port;
165-
hdr->udp->net_len = fd_ushort_bswap( (ushort)(payload_sz + sizeof(fd_udp_hdr_t)) );
151+
fd_ip4_udp_hdrs_t * hdr = (fd_ip4_udp_hdrs_t *)packet;
152+
*hdr = *(is_intake ? ctx->intake_hdr : ctx->serve_hdr);
153+
154+
fd_ip4_hdr_t * ip4 = hdr->ip4;
155+
ip4->saddr = src_ip_addr;
156+
ip4->daddr = dst_ip_addr;
157+
ip4->net_id = fd_ushort_bswap( ctx->net_id++ );
158+
ip4->check = 0U;
159+
ip4->net_tot_len = fd_ushort_bswap( (ushort)(payload_sz + sizeof(fd_ip4_hdr_t)+sizeof(fd_udp_hdr_t)) );
160+
ip4->check = fd_ip4_hdr_check_fast( hdr->ip4 );
161+
162+
fd_udp_hdr_t * udp = hdr->udp;
163+
udp->net_dport = dst_port;
164+
udp->net_len = fd_ushort_bswap( (ushort)(payload_sz + sizeof(fd_udp_hdr_t)) );
165+
fd_memcpy( packet+sizeof(fd_ip4_udp_hdrs_t), payload, payload_sz );
166166
hdr->udp->check = fd_ip4_udp_check( hdr->ip4->saddr,
167167
hdr->ip4->daddr,
168-
(fd_udp_hdr_t const *)(hdr->buf + 34),
169-
packet + sizeof(fd_net_hdrs_t) );
168+
hdr->udp,
169+
packet + sizeof(fd_ip4_udp_hdrs_t) );
170170

171-
ulong tspub = fd_frag_meta_ts_comp( fd_tickcount() );
172-
ulong sig = fd_disco_netmux_sig( dst_ip_addr, dst_port, dst_ip_addr, DST_PROTO_OUTGOING, FD_NETMUX_SIG_MIN_HDR_SZ );
171+
ulong tspub = fd_frag_meta_ts_comp( fd_tickcount() );
172+
ulong sig = fd_disco_netmux_sig( dst_ip_addr, dst_port, dst_ip_addr, DST_PROTO_OUTGOING, sizeof(fd_ip4_udp_hdrs_t) );
173+
ulong packet_sz = payload_sz + sizeof(fd_ip4_udp_hdrs_t);
173174
fd_mcache_publish( ctx->net_out_mcache, ctx->net_out_depth, ctx->net_out_seq, sig, ctx->net_out_chunk, packet_sz, 0UL, tsorig, tspub );
174175
ctx->net_out_seq = fd_seq_inc( ctx->net_out_seq, 1UL );
175176
ctx->net_out_chunk = fd_dcache_compact_next( ctx->net_out_chunk, packet_sz, ctx->net_out_chunk0, ctx->net_out_wmark );
@@ -535,8 +536,8 @@ unprivileged_init( fd_topo_t * topo,
535536

536537
ctx->net_id = (ushort)0;
537538

538-
fd_net_create_packet_header_template( ctx->intake_hdr, FD_REPAIR_MAX_PACKET_SIZE, 0, ctx->repair_intake_listen_port );
539-
fd_net_create_packet_header_template( ctx->serve_hdr, FD_REPAIR_MAX_PACKET_SIZE, 0, ctx->repair_serve_listen_port );
539+
fd_ip4_udp_hdr_init( ctx->intake_hdr, FD_REPAIR_MAX_PACKET_SIZE, 0, ctx->repair_intake_listen_port );
540+
fd_ip4_udp_hdr_init( ctx->serve_hdr, FD_REPAIR_MAX_PACKET_SIZE, 0, ctx->repair_serve_listen_port );
540541

541542
/* Keyguard setup */
542543
fd_topo_link_t * sign_in = &topo->links[ tile->in_link_id[ SIGN_IN_IDX ] ];

src/discof/sender/fd_sender_tile.c

+22-22
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ struct fd_sender_tile_ctx {
4848
uchar txn_buf[ sizeof(fd_txn_p_t) ] __attribute__((aligned(alignof(fd_txn_p_t))));
4949

5050
fd_gossip_peer_addr_t tpu_serve_addr;
51-
fd_net_hdrs_t packet_hdr[ 1 ];
51+
fd_ip4_udp_hdrs_t packet_hdr[1];
5252
ushort net_id;
5353

5454
ulong stake_in_idx;
@@ -141,26 +141,26 @@ send_packet( fd_sender_tile_ctx_t * ctx,
141141
ulong tsorig ) {
142142
uchar * packet = fd_chunk_to_laddr( ctx->net_out_mem, ctx->net_out_chunk );
143143

144-
fd_memcpy( packet, ctx->packet_hdr, sizeof(fd_net_hdrs_t) );
145-
fd_net_hdrs_t * hdr = (fd_net_hdrs_t *)packet;
146-
147-
hdr->ip4->daddr = dst_ip_addr;
148-
hdr->ip4->net_id = fd_ushort_bswap( ctx->net_id++ );
149-
hdr->ip4->check = 0U;
150-
hdr->ip4->net_tot_len = fd_ushort_bswap( (ushort)(payload_sz + sizeof(fd_ip4_hdr_t)+sizeof(fd_udp_hdr_t)) );
151-
hdr->ip4->check = fd_ip4_hdr_check_fast( hdr->buf+14 );
152-
153-
ulong packet_sz = payload_sz + sizeof(fd_net_hdrs_t);
154-
fd_memcpy( packet+sizeof(fd_net_hdrs_t), payload, payload_sz );
155-
hdr->udp->net_dport = fd_ushort_bswap( dst_port );
156-
hdr->udp->net_len = fd_ushort_bswap( (ushort)(payload_sz + sizeof(fd_udp_hdr_t)) );
157-
hdr->udp->check = fd_ip4_udp_check( hdr->ip4->saddr,
158-
hdr->ip4->daddr,
159-
(fd_udp_hdr_t const *)hdr->buf+34,
160-
packet + sizeof(fd_net_hdrs_t) );
161-
162-
ulong tspub = fd_frag_meta_ts_comp( fd_tickcount() );
163-
ulong sig = fd_disco_netmux_sig( dst_ip_addr, dst_port, dst_ip_addr, DST_PROTO_OUTGOING, FD_NETMUX_SIG_MIN_HDR_SZ );
144+
fd_ip4_udp_hdrs_t * hdr = (fd_ip4_udp_hdrs_t *)packet;
145+
*hdr = *ctx->packet_hdr;
146+
147+
fd_ip4_hdr_t * ip4 = hdr->ip4;
148+
ip4->daddr = dst_ip_addr;
149+
ip4->net_id = fd_ushort_bswap( ctx->net_id++ );
150+
ip4->check = 0U;
151+
ip4->net_tot_len = fd_ushort_bswap( (ushort)(payload_sz + sizeof(fd_ip4_hdr_t)+sizeof(fd_udp_hdr_t)) );
152+
ip4->check = fd_ip4_hdr_check_fast( ip4 );
153+
154+
fd_udp_hdr_t * udp = hdr->udp;
155+
udp->net_dport = fd_ushort_bswap( dst_port );
156+
udp->net_len = fd_ushort_bswap( (ushort)(payload_sz + sizeof(fd_udp_hdr_t)) );
157+
fd_memcpy( packet+sizeof(fd_ip4_udp_hdrs_t), payload, payload_sz );
158+
udp->check = fd_ip4_udp_check( ip4->saddr, ip4->daddr, udp,
159+
packet+sizeof(fd_ip4_udp_hdrs_t) );
160+
161+
ulong tspub = fd_frag_meta_ts_comp( fd_tickcount() );
162+
ulong sig = fd_disco_netmux_sig( dst_ip_addr, dst_port, dst_ip_addr, DST_PROTO_OUTGOING, sizeof(fd_ip4_udp_hdrs_t) );
163+
ulong packet_sz = payload_sz + sizeof(fd_ip4_udp_hdrs_t);
164164
fd_mcache_publish( ctx->net_out_mcache, ctx->net_out_depth, ctx->net_out_seq, sig, ctx->net_out_chunk, packet_sz, 0UL, tsorig, tspub );
165165
ctx->net_out_seq = fd_seq_inc( ctx->net_out_seq, 1UL );
166166
ctx->net_out_chunk = fd_dcache_compact_next( ctx->net_out_chunk, packet_sz, ctx->net_out_chunk0, ctx->net_out_wmark );
@@ -356,7 +356,7 @@ unprivileged_init( fd_topo_t * topo,
356356

357357
ctx->tpu_serve_addr.addr = tile->sender.ip_addr;
358358
ctx->tpu_serve_addr.port = fd_ushort_bswap( tile->sender.tpu_listen_port );
359-
fd_net_create_packet_header_template( ctx->packet_hdr, FD_TXN_MTU, ctx->tpu_serve_addr.addr, ctx->tpu_serve_addr.port );
359+
fd_ip4_udp_hdr_init( ctx->packet_hdr, FD_TXN_MTU, ctx->tpu_serve_addr.addr, ctx->tpu_serve_addr.port );
360360

361361
ulong poh_slot_obj_id = fd_pod_query_ulong( topo->props, "poh_slot", ULONG_MAX );
362362
FD_TEST( poh_slot_obj_id!=ULONG_MAX );

src/util/net/fd_ip4.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,12 @@ union fd_ip4_hdr {
4343
uchar ttl; /* Frag time to live */
4444
uchar protocol; /* Type of payload */
4545
ushort check; /* Header checksum ("invariant" order) */
46-
union { /* Address of sender, technically net order but all APIs below work with this directly */
47-
uchar saddr_c[4];
46+
union __attribute__((packed)) {
47+
uchar saddr_c[4]; /* Address of sender, technically net order but all APIs below work with this directly */
4848
uint saddr;
4949
};
50-
union { /* Address of destination, technically net order but all APIs below work with this directly */
51-
uchar daddr_c[4];
50+
union __attribute__((packed)) {
51+
uchar daddr_c[4]; /* Address of destination, technically net order but all APIs below work with this directly */
5252
uint daddr;
5353
};
5454
/* Up to 40 bytes of options here */

src/util/net/fd_net_headers.h

+41-33
Original file line numberDiff line numberDiff line change
@@ -4,46 +4,54 @@
44
#include "fd_udp.h"
55
#include "fd_eth.h"
66

7-
typedef union {
8-
struct __attribute__((packed)) {
7+
/* fd_ip4_udp_hdrs is useful to construct Ethernet+IPv4+UDP network
8+
headers. Assumes that the IPv4 header has no options (IHL=5). */
9+
10+
union fd_ip4_udp_hdrs {
11+
uchar uc[ 42 ];
12+
struct {
913
fd_eth_hdr_t eth[1];
1014
fd_ip4_hdr_t ip4[1];
1115
fd_udp_hdr_t udp[1];
1216
};
13-
uchar buf[ 42 ];
14-
} fd_net_hdrs_t;
17+
};
18+
19+
typedef union fd_ip4_udp_hdrs fd_ip4_udp_hdrs_t;
1520

1621
FD_PROTOTYPES_BEGIN
1722

18-
/* Helper method to populate a header template
19-
containing ethernet, UDP and IP headers with
20-
default values. Used for pre-staging headers. */
21-
22-
static inline fd_net_hdrs_t *
23-
fd_net_create_packet_header_template( fd_net_hdrs_t * pkt,
24-
ulong payload_sz,
25-
uint src_ip,
26-
ushort src_port ) {
27-
memset( pkt->eth->dst, 0, 6UL );
28-
memset( pkt->eth->src, 0, 6UL );
29-
pkt->eth->net_type = fd_ushort_bswap( FD_ETH_HDR_TYPE_IP );
30-
31-
pkt->ip4->verihl = FD_IP4_VERIHL( 4U, 5U );
32-
pkt->ip4->tos = (uchar)0;
33-
pkt->ip4->net_tot_len = fd_ushort_bswap( (ushort)(payload_sz + sizeof(fd_ip4_hdr_t)+sizeof(fd_udp_hdr_t)) );
34-
pkt->ip4->net_frag_off = fd_ushort_bswap( FD_IP4_HDR_FRAG_OFF_DF );
35-
pkt->ip4->ttl = (uchar)64;
36-
pkt->ip4->protocol = FD_IP4_HDR_PROTOCOL_UDP;
37-
pkt->ip4->check = 0U;
38-
memcpy( pkt->ip4->saddr_c, &src_ip, 4UL );
39-
memset( pkt->ip4->daddr_c, 0, 4UL );
40-
41-
pkt->udp->net_sport = fd_ushort_bswap( src_port );
42-
pkt->udp->net_dport = (ushort)0;
43-
pkt->udp->net_len = fd_ushort_bswap( (ushort)(payload_sz + sizeof(fd_udp_hdr_t)) );
44-
pkt->udp->check = (ushort)0;
45-
46-
return pkt;
23+
/* Helper method to populate a header template containing Ethernet,
24+
IPv4 (no options), and UDP headers. Note that IPv4 and UDP header
25+
checksums are set to 0. */
26+
27+
static inline fd_ip4_udp_hdrs_t *
28+
fd_ip4_udp_hdr_init( fd_ip4_udp_hdrs_t * hdrs,
29+
ulong payload_sz,
30+
uint src_ip,
31+
ushort src_port ) {
32+
fd_eth_hdr_t * eth = hdrs->eth;
33+
memset( eth->dst, 0, 6UL );
34+
memset( eth->src, 0, 6UL );
35+
eth->net_type = fd_ushort_bswap( FD_ETH_HDR_TYPE_IP );
36+
37+
fd_ip4_hdr_t * ip4 = hdrs->ip4;
38+
ip4->verihl = FD_IP4_VERIHL( 4U, 5U );
39+
ip4->tos = (uchar)0;
40+
ip4->net_tot_len = fd_ushort_bswap( (ushort)(payload_sz + sizeof(fd_ip4_hdr_t)+sizeof(fd_udp_hdr_t)) );
41+
ip4->net_frag_off = fd_ushort_bswap( FD_IP4_HDR_FRAG_OFF_DF );
42+
ip4->ttl = (uchar)64;
43+
ip4->protocol = FD_IP4_HDR_PROTOCOL_UDP;
44+
ip4->check = 0U;
45+
ip4->saddr = src_ip;
46+
ip4->daddr = 0;
47+
48+
fd_udp_hdr_t * udp = hdrs->udp;
49+
udp->net_sport = fd_ushort_bswap( src_port );
50+
udp->net_dport = (ushort)0;
51+
udp->net_len = fd_ushort_bswap( (ushort)(payload_sz + sizeof(fd_udp_hdr_t)) );
52+
udp->check = (ushort)0;
53+
54+
return hdrs;
4755
}
4856

4957
FD_PROTOTYPES_END

0 commit comments

Comments
 (0)