Interrupt an in-progress connect with netw_set_connect_abort_cb.
Interrupt an in-progress connect with netw_set_connect_abort_cb. A connect to an unresponsive host (a dropped SYN) otherwise blocks until the connect timeout. Registering a connect-abort callback makes the connect wait wake every NETW_CONNECT_ABORT_POLL_MS to poll it, so returning non-zero from the callback aborts the attempt at once. This example dials a TEST-NET-1 (RFC 5737, 192.0.2.0/24, not routed) address, which is guaranteed to have no listener, with a long connect timeout and a callback that asks to abort: the connect returns promptly rather than after the timeout.
- Author
- Castagnier Mickael
- Version
- 1.0
- Date
- 19/07/2026
#define BLACKHOLE_HOST "192.0.2.1"
#define BLACKHOLE_PORT "80"
#define LONG_CONNECT_TIMEOUT_MS 20000
#define ABORT_MAX_MS 3000
(void)ctx;
return 1;
}
int fails = 0;
long elapsed_ms = (long)(elapsed_us / 1000);
if (rc != FALSE) {
fails++;
}
fails++;
}
fails++;
}
elapsed_ms = (long)(
get_usec(&timer) / 1000);
if (rc != FALSE) {
fails++;
}
n_log(
LOG_ERR,
"FAIL: callback polled after being removed");
fails++;
}
if (fails == 0)
else
n_log(
LOG_ERR,
"ex_network_connect_abort: %d check(s) FAILED", fails);
return fails ? 1 : 0;
}
NETWORK * netw
Network for server mode, accepting incomming.
#define ABORT_MAX_MS
An aborted connect must return in well under the connect budget.
static int abort_now(void *ctx)
Connect-abort callback that always asks to abort, counting its polls.
static int g_polls
Count of times the abort callback was polled (proves it ran).
#define BLACKHOLE_PORT
Blackhole port (any).
#define LONG_CONNECT_TIMEOUT_MS
Long connect budget so an un-aborted attempt would clearly out-last an aborted one.
#define BLACKHOLE_HOST
Blackhole target: RFC 5737 documentation range, never has a listener.
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
#define LOG_ERR
error conditions
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL )
#define LOG_INFO
informational
int start_HiTimer(N_TIME *timer)
Initialize or restart from zero any N_TIME HiTimer.
time_t get_usec(N_TIME *timer)
Poll any N_TIME HiTimer, returning usec, and moving currentTime to startTime.
#define NETWORK_IPV4
Flag to force IPV4
int netw_close(NETWORK **netw)
Closing a specified Network, destroy queues, free the structure.
int netw_connect_to(NETWORK **netw, char *host, char *port, int ip_version, int connect_timeout_ms)
Connect a NETWORK with a bounded connection-establishment time.
void netw_set_connect_abort_cb(int(*cb)(void *ctx), void *ctx)
Register a process-wide callback polled while a connect is in progress.