Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_network_connect_abort.c
Go to the documentation of this file.
1/*
2 * Nilorea Library
3 * Copyright (C) 2005-2026 Castagnier Mickael
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14 * implied. See the License for the specific language governing
15 * permissions and limitations under the License.
16 *
17 * SPDX-License-Identifier: Apache-2.0
18 */
19
36#include "nilorea/n_log.h"
37#include "nilorea/n_network.h"
38#include "nilorea/n_time.h"
39
41#define BLACKHOLE_HOST "192.0.2.1"
43#define BLACKHOLE_PORT "80"
45#define LONG_CONNECT_TIMEOUT_MS 20000
47#define ABORT_MAX_MS 3000
48
50static int g_polls = 0;
51
53static int abort_now(void* ctx) {
54 (void)ctx;
55 g_polls++;
56 return 1;
57}
58
59int main(void) {
61
62 int fails = 0;
63
64 /* 1) With an abort callback registered, a connect to an unresponsive host
65 returns quickly instead of blocking for the whole connect timeout. */
67
68 NETWORK* netw = NULL;
69 N_TIME timer;
70 start_HiTimer(&timer);
72 time_t elapsed_us = get_usec(&timer);
73 long elapsed_ms = (long)(elapsed_us / 1000);
74
75 n_log(LOG_INFO, "aborted connect: rc=%d elapsed=%ldms polls=%d", rc, elapsed_ms, g_polls);
76 if (rc != FALSE) {
77 n_log(LOG_ERR, "FAIL: connect to %s should not succeed", BLACKHOLE_HOST);
78 fails++;
79 }
80 if (elapsed_ms >= ABORT_MAX_MS) {
81 n_log(LOG_ERR, "FAIL: aborted connect took %ldms, expected < %dms", elapsed_ms, ABORT_MAX_MS);
82 fails++;
83 }
84 if (g_polls == 0) {
85 n_log(LOG_ERR, "FAIL: abort callback was never polled");
86 fails++;
87 }
88 if (netw)
90
91 /* 2) Removing the callback restores the default: connect blocks up to its
92 (here deliberately short) timeout and then fails. Kept short so the
93 example stays fast; only the return value is asserted, so an environment
94 that fast-fails an unroutable address does not turn this into a flake. */
95 netw_set_connect_abort_cb(NULL, NULL);
96 netw = NULL;
97 g_polls = 0;
98 start_HiTimer(&timer);
100 elapsed_ms = (long)(get_usec(&timer) / 1000);
101 n_log(LOG_INFO, "no-callback connect: rc=%d elapsed=%ldms polls=%d", rc, elapsed_ms, g_polls);
102 if (rc != FALSE) {
103 n_log(LOG_ERR, "FAIL: connect to %s should not succeed", BLACKHOLE_HOST);
104 fails++;
105 }
106 if (g_polls != 0) {
107 n_log(LOG_ERR, "FAIL: callback polled after being removed");
108 fails++;
109 }
110 if (netw)
112
113 if (fails == 0)
114 n_log(LOG_INFO, "ex_network_connect_abort: all checks passed");
115 else
116 n_log(LOG_ERR, "ex_network_connect_abort: %d check(s) FAILED", fails);
117 return fails ? 1 : 0;
118}
int main(void)
NETWORK * netw
Network for server mode, accepting incomming.
Definition ex_network.c:39
#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.
Definition n_log.h:89
#define LOG_ERR
error conditions
Definition n_log.h:76
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL )
Definition n_log.c:121
#define LOG_INFO
informational
Definition n_log.h:82
int start_HiTimer(N_TIME *timer)
Initialize or restart from zero any N_TIME HiTimer.
Definition n_time.c:85
time_t get_usec(N_TIME *timer)
Poll any N_TIME HiTimer, returning usec, and moving currentTime to startTime.
Definition n_time.c:107
Timing Structure.
Definition n_time.h:49
#define NETWORK_IPV4
Flag to force IPV4
Definition n_network.h:50
int netw_close(NETWORK **netw)
Closing a specified Network, destroy queues, free the structure.
Definition n_network.c:2662
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.
Definition n_network.c:2373
void netw_set_connect_abort_cb(int(*cb)(void *ctx), void *ctx)
Register a process-wide callback polled while a connect is in progress.
Definition n_network.c:2172
Structure of a NETWORK.
Definition n_network.h:309
Generic log system.
Network Engine.
Timing utilities.