@@ -174,57 +174,43 @@ main(int argc,
174174 continue ;
175175 }
176176
177+ ret = recvfrom (listenfd , NULL , 0 , MSG_PEEK ,
178+ (struct sockaddr * )& cliaddr , & cliLen );
179+ if (ret < 0 )
180+ continue ;
181+
182+ printf ("Received a packet from %s:%d\n" ,
183+ inet_ntoa (cliaddr .sin_addr ), ntohs (cliaddr .sin_port ));
184+
177185 memset (& args [i ], 0 , sizeof (thread_args_t ));
186+ args [i ].activefd = listenfd ;
187+ listenfd = new_udp_listen_socket ();
188+ /* avoid messages from other peers */
189+ ret = connect (args [i ].activefd , (const struct sockaddr * )& cliaddr , cliLen );
190+ if (ret != 0 ) {
191+ printf ("error: connect returned: %d\n" , ret );
192+ break ;
193+ }
178194
179195 args [i ].ssl = wolfSSL_new (ctx );
180196 if (args [i ].ssl == NULL ) {
181197 printf ("error: wolfSSL_new returned null\n" );
182198 break ;
183199 }
184200
185- /* set the session ssl to client connection port */
186- ret = wolfSSL_set_fd (args [i ].ssl , listenfd );
187- if (ret != SSL_SUCCESS ) {
188- printf ("error: wolfSSL_set_fd returned %d\n" , ret );
189- break ;
190- }
191-
192- ret = wolfSSL_accept (args [i ].ssl );
201+ ret = wolfSSL_set_fd (args [i ].ssl , args [i ].activefd );
193202 if (ret != SSL_SUCCESS ) {
194- printf ("error: wolfSSL_accept returned %d\n" , ret );
203+ printf ("error: wolfSSL_set_dtls_fd_connected: %d\n" , ret );
195204 break ;
196205 }
197206
198- ret = wolfSSL_dtls_get_peer (args [i ].ssl , & cliaddr , & cliLen );
207+ ret = wolfSSL_dtls_set_peer (args [i ].ssl , & cliaddr , cliLen );
199208 if (ret != WOLFSSL_SUCCESS ) {
200- printf ("error: wolfSSL_dtls_get_peer failed \n" );
209+ printf ("error: wolfSSL_dtls_set_peer: %d \n" , ret );
201210 break ;
202211 }
203212
204213 args [i ].peer_port = ntohs (cliaddr .sin_port );
205-
206- printf ("info: new dtls session: %p, %d\n" , (void * ) args [i ].ssl ,
207- args [i ].peer_port );
208-
209- /* Open new UDP socket. */
210- args [i ].activefd = new_udp_listen_socket ();
211- if (args [i ].activefd <= 0 ) {
212- break ;
213- }
214-
215- ret = connect (args [i ].activefd , (const struct sockaddr * )& cliaddr ,
216- cliLen );
217- if (ret != 0 ) {
218- printf ("error: connect returned: %d\n" , ret );
219- break ;
220- }
221-
222- ret = wolfSSL_set_dtls_fd_connected (args [i ].ssl , args [i ].activefd );
223- if (ret != SSL_SUCCESS ) {
224- printf ("error: wolfSSL_set_dtls_fd_connected: %d\n" , ret );
225- break ;
226- }
227-
228214 ret = pthread_create (& threads [i ], NULL , server_work , & args [i ]);
229215
230216 if (ret == 0 ) {
@@ -319,6 +305,19 @@ server_work(void * args)
319305 int n_bytes = 0 ;
320306 char recv_msg [MSGLEN ];
321307 char send_msg [MSGLEN ];
308+ int ret ;
309+
310+ ret = wolfSSL_accept (thread_args -> ssl );
311+ if (ret != SSL_SUCCESS )
312+ {
313+ printf ("error: wolfSSL_accept returned %d\n" , ret );
314+ pthread_exit (NULL );
315+ /* we should never reach here */
316+ return NULL ;
317+ }
318+
319+ printf ("info: new dtls session: %p, %d\n" , (void * )thread_args -> ssl ,
320+ thread_args -> peer_port );
322321
323322 for (size_t i = 0 ; i < 4 ; ++ i ) {
324323 if (stop_server ) {
@@ -374,14 +373,21 @@ server_work(void * args)
374373static void
375374safer_shutdown (thread_args_t * args )
376375{
376+ int ret ;
377+
377378 if (args == NULL ) {
378379 printf ("error: safer_shutdown with null args\n" );
379380 return ;
380381 }
381382
382383 if (args -> ssl != NULL ) {
383384 printf ("info: closed dtls session: %p\n" , (void * ) args -> ssl );
384- wolfSSL_shutdown (args -> ssl );
385+ ret = wolfSSL_shutdown (args -> ssl );
386+
387+ /* bidirectional shutdown */
388+ if (ret != WOLFSSL_SUCCESS )
389+ ret = wolfSSL_shutdown (args -> ssl );
390+
385391 wolfSSL_free (args -> ssl );
386392 args -> ssl = NULL ;
387393 }
0 commit comments