Skip to content

Commit 3656ba2

Browse files
authored
feat: connection type callback (#8)
`endpoint_conn_type_cb` to be able to react to connection type changes on a connection to a specific node id
1 parent 2cdc1aa commit 3656ba2

File tree

4 files changed

+342
-1
lines changed

4 files changed

+342
-1
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ tokio = { version = "1.36.0", features = ["rt-multi-thread"] }
3030
tracing = "0.1"
3131
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
3232
url = "2.5.0"
33+
futures-lite = "2.3.0"
3334

3435
[features]
3536
# generate headers

irohnet.h

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ enum EndpointResult {
102102
* It is common to simply log this error and move on.
103103
*/
104104
ENDPOINT_RESULT_INCOMING_ERROR,
105+
/** \brief
106+
* Unable to find connection for the given `NodeId`
107+
*/
108+
ENDPOINT_RESULT_CONNECTION_TYPE_ERROR,
105109
}
106110
#ifndef DOXYGEN
107111
; typedef uint8_t
@@ -513,6 +517,55 @@ typedef struct PublicKey {
513517
uint8_32_array_t key;
514518
} PublicKey_t;
515519

520+
/** <No documentation available> */
521+
/** \remark Has the same ABI as `uint8_t` **/
522+
#ifdef DOXYGEN
523+
typedef
524+
#endif
525+
enum ConnectionType {
526+
/** \brief
527+
* Direct UDP connection
528+
*/
529+
CONNECTION_TYPE_DIRECT = 0,
530+
/** \brief
531+
* Relay connection over relay
532+
*/
533+
CONNECTION_TYPE_RELAY,
534+
/** \brief
535+
* Both a UDP and a relay connection are used.
536+
*
537+
* This is the case if we do have a UDP address, but are missing a recent confirmation that
538+
* the address works.
539+
*/
540+
CONNECTION_TYPE_MIXED,
541+
/** \brief
542+
* We have no verified connection to this PublicKey
543+
*/
544+
CONNECTION_TYPE_NONE,
545+
}
546+
#ifndef DOXYGEN
547+
; typedef uint8_t
548+
#endif
549+
ConnectionType_t;
550+
551+
/** \brief
552+
* Run a callback once you have a direct connection to a peer
553+
*
554+
* Does not block. The provided callback will be called when we have a direct
555+
* connection to the peer associated with the `node_id`, or the timeout has occurred.
556+
*
557+
* To wait indefinitely, provide -1 for the timeout parameter.
558+
*
559+
* `ctx` is passed along to the callback, to allow passing context, it must be thread safe as the callback is
560+
* called from another thread.
561+
*/
562+
void
563+
endpoint_conn_type_cb (
564+
Endpoint_t * ep,
565+
void const * ctx,
566+
PublicKey_t const * node_id,
567+
void (*cb)(void const *, EndpointResult_t, ConnectionType_t));
568+
516569
/** \brief
517570
* Represents a valid URL.
518571
*/

main.c

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,24 @@ run_server (EndpointConfig_t * config, slice_ref_uint8_t alpn_slice, bool json_o
254254
return 0;
255255
}
256256

257+
typedef struct ConnectionStatus {
258+
EndpointResult_t res;
259+
ConnectionType_t conn_type;
260+
} ConnectionStatus;
261+
262+
void
263+
callback(
264+
void const * ctx,
265+
EndpointResult_t res,
266+
ConnectionType_t conn_type
267+
)
268+
{
269+
ConnectionStatus *cs;
270+
cs = (ConnectionStatus *)ctx;
271+
cs->res = res;
272+
cs->conn_type = conn_type;
273+
}
274+
257275
int
258276
run_client (
259277
EndpointConfig_t * config,
@@ -315,6 +333,10 @@ run_client (
315333
return -1;
316334
}
317335

336+
ConnectionStatus *conn_status;
337+
conn_status = malloc(sizeof(ConnectionStatus));
338+
endpoint_conn_type_cb(ep, (const void *)conn_status, &node_addr.node_id, callback);
339+
318340
SendStream_t * send_stream = send_stream_default();
319341
ret = connection_open_uni(&conn, &send_stream);
320342
if (ret != 0) {
@@ -389,6 +411,29 @@ run_client (
389411
printf("received: '%s'\n", recv_str);
390412

391413
fflush(stdout);
414+
// check that we were able to use the conn_type callback
415+
if (conn_status->res != ENDPOINT_RESULT_OK) {
416+
fprintf(stderr, "callback failed to send a connection type\n");
417+
return -1;
418+
} else {
419+
switch (conn_status->conn_type) {
420+
case CONNECTION_TYPE_DIRECT:
421+
printf("had a direct connection\n");
422+
break;
423+
case CONNECTION_TYPE_RELAY:
424+
printf("had a relay connection\n");
425+
break;
426+
case CONNECTION_TYPE_MIXED:
427+
printf("had a mixed connection\n");
428+
break;
429+
case CONNECTION_TYPE_NONE:
430+
fprintf(stderr, "callback reported no connection\n");
431+
return -1;
432+
default:
433+
fprintf(stderr, "unknown connection type reported: %i\n", conn_status->conn_type);
434+
return -1;
435+
}
436+
}
392437

393438
// finish
394439
ret = send_stream_finish(send_stream);
@@ -413,6 +458,7 @@ run_client (
413458
// cleanup
414459
free(recv_str);
415460
free(recv_buffer);
461+
free(conn_status);
416462
recv_stream_free(recv_stream);
417463
return 0;
418464
}

0 commit comments

Comments
 (0)