@@ -102,6 +102,28 @@ static void *sequence__thread_core(void *temp)
102102 // Get human-friendly sequence ID (id + 1).
103103 int seq_num = ti -> seq_cnt + 1 ;
104104
105+ // Initialize socket FD.
106+ int sock_fd ;
107+
108+ // Create AF_XDP socket and check.
109+ xsk_socket_info_t * xsk = tech_afxdp__sock_setup (ti -> device , ti -> id , ti -> cmd .verbose );
110+
111+ if (xsk == NULL )
112+ {
113+ fprintf (stderr , "[%d] Error setting up AF_XDP socket on thread.\n" , seq_num );
114+
115+ goto thread_exit ;
116+ }
117+
118+ sock_fd = tech_afxdp__sock_fd (xsk );
119+
120+ if (sock_fd < 0 )
121+ {
122+ fprintf (stderr , "[%d] Error setting up AF_XDP socket on thread.\n" , seq_num );
123+
124+ goto thread_exit ;
125+ }
126+
105127 // Let's parse some config values before creating the socket so we know what we're doing.
106128 u8 protocol = IPPROTO_UDP ;
107129 u8 src_mac [ETH_ALEN ] = {0 };
@@ -116,11 +138,16 @@ static void *sequence__thread_core(void *temp)
116138 {
117139 fprintf (stderr , "[%d] Failed to allocate buffers\n" , seq_num );
118140
119- free (data_len );
120- free (pckt_len );
121- free (buffer );
141+ if (data_len != NULL )
142+ free (data_len );
143+
144+ if (pckt_len != NULL )
145+ free (pckt_len );
146+
147+ if (buffer != NULL )
148+ free (buffer );
122149
123- pthread_exit ( NULL ) ;
150+ goto thread_exit ;
124151 }
125152
126153 // Payloads.
@@ -137,7 +164,7 @@ static void *sequence__thread_core(void *temp)
137164 {
138165 fprintf (stderr , "[%d] Failed to intialize payloads array due to allocation error.\n" , seq_num );
139166
140- pthread_exit ( NULL ) ;
167+ goto thread_exit ;
141168 }
142169
143170 // Let's first start off by checking if the source MAC address is set within the config.
@@ -162,29 +189,6 @@ static void *sequence__thread_core(void *temp)
162189 protocol = IPPROTO_ICMP ;
163190 }
164191
165- // Initialize socket FD.
166- int sock_fd ;
167-
168- // Create AF_XDP socket and check.
169- xsk_socket_info_t * xsk = tech_afxdp__sock_setup (ti -> device , ti -> id , ti -> cmd .verbose );
170-
171- sock_fd = tech_afxdp__sock_fd (xsk );
172-
173- if (sock_fd < 0 )
174- {
175- fprintf (stderr , "[%d] Error setting up AF_XDP socket on thread.\n" , seq_num );
176-
177- // Attempt to cleanup socket.
178- tech_afxdp__sock_cleanup (xsk );
179-
180- // Attempt to close the socket.
181- close (sock_fd );
182-
183- pthread_exit (NULL );
184-
185- return NULL ;
186- }
187-
188192 // Check if source MAC address is set properly. If not, let's get the MAC address of the interface we're sending packets out of.
189193 if (src_mac [0 ] == 0 && src_mac [1 ] == 0 && src_mac [2 ] == 0 && src_mac [3 ] == 0 && src_mac [4 ] == 0 && src_mac [5 ] == 0 )
190194 {
@@ -930,6 +934,17 @@ static void *sequence__thread_core(void *temp)
930934 // Free payloads.
931935 free (payloads );
932936
937+ pthread_exit (NULL );
938+
939+ thread_exit :
940+ // Cleanup AF_XDP socket.
941+ tech_afxdp__sock_cleanup (xsk );
942+
943+ if (sock_fd > -1 )
944+ {
945+ close (sock_fd );
946+ }
947+
933948 pthread_exit (NULL );
934949}
935950
0 commit comments