@@ -179,6 +179,24 @@ run_server (MagicEndpointConfig_t * config, slice_ref_uint8_t alpn_slice, bool j
179179 return 0 ;
180180}
181181
182+ typedef struct ConnectionStatus {
183+ MagicEndpointResult_t res ;
184+ ConnectionType_t conn_type ;
185+ } ConnectionStatus ;
186+
187+ void
188+ callback (
189+ void const * ctx ,
190+ MagicEndpointResult_t res ,
191+ ConnectionType_t conn_type
192+ )
193+ {
194+ ConnectionStatus * cs ;
195+ cs = (ConnectionStatus * )ctx ;
196+ cs -> res = res ;
197+ cs -> conn_type = conn_type ;
198+ }
199+
182200int
183201run_client (
184202 MagicEndpointConfig_t * config ,
@@ -238,6 +256,10 @@ run_client (
238256 return -1 ;
239257 }
240258
259+ ConnectionStatus * conn_status ;
260+ conn_status = malloc (sizeof (ConnectionStatus ));
261+ magic_endpoint_conn_type_cb (ep , (const void * )conn_status , & node_addr .node_id , callback );
262+
241263 SendStream_t * send_stream = send_stream_default ();
242264 ret = connection_open_uni (& conn , & send_stream );
243265 if (ret != 0 ) {
@@ -307,6 +329,30 @@ run_client (
307329 recv_str [read ] = '\0' ;
308330 printf ("received: '%s'\n" , recv_str );
309331
332+ // check that we were able to use the conn_type callback
333+ if (conn_status -> res != MAGIC_ENDPOINT_RESULT_OK ) {
334+ fprintf (stderr , "callback failed to send a connection type\n" );
335+ return -1 ;
336+ } else {
337+ switch (conn_status -> conn_type ) {
338+ case CONNECTION_TYPE_DIRECT :
339+ printf ("had a direct connection\n" );
340+ break ;
341+ case CONNECTION_TYPE_RELAY :
342+ printf ("had a relay connection\n" );
343+ break ;
344+ case CONNECTION_TYPE_MIXED :
345+ printf ("had a mixed connection\n" );
346+ break ;
347+ case CONNECTION_TYPE_NONE :
348+ fprintf (stderr , "callback reported no connection\n" );
349+ return -1 ;
350+ default :
351+ fprintf (stderr , "unknown connection type reported: %i\n" , conn_status -> conn_type );
352+ return -1 ;
353+ }
354+ }
355+
310356 // finish
311357 ret = send_stream_finish (send_stream );
312358 if (ret != 0 ) {
@@ -317,6 +363,7 @@ run_client (
317363 // cleanup
318364 free (recv_str );
319365 free (recv_buffer );
366+ free (conn_status );
320367 recv_stream_free (recv_stream );
321368 connection_free (conn );
322369
0 commit comments