Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_network.c
Go to the documentation of this file.
1/*
2 * Nilorea Library
3 * Copyright (C) 2005-2026 Castagnier Mickael
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
27#include <stdio.h>
28#include <errno.h>
29
30#include "ex_network.h"
31
32#define SERVER 0
33#define CLIENT 1
34
36
38 *netw = NULL;
41int use_udp = 0;
42
43static pthread_t netw_thr;
44
45void usage(void) {
46 fprintf(stderr,
47 " -a 'serveur address name/ip to bind (server mode) (optionnal)\n"
48 " -s 'serveur address name/ip to connect (client mode)\n"
49 " -p 'port': port to use, server or client\n"
50 " -u: use UDP transport instead of TCP\n"
51 " -i 'ipmode': optional, ip version to use. Default support both ipv4 and ipv6. Values: ipv4, ipv6\n"
52 " -n 'nb': optional, number of attempts (default: 3)\n"
53 " -h: show that help file\n"
54 " -v: show the program version\n"
55 " -V 'log level': optional, set the log level. Default: LOG_ERR, values: LOG_INFO, LOG_NOTICE, LOG_ERR, LOG_DEBUG\n");
56}
57
58void process_args(int argc, char** argv, char** address, char** server, char** port, int* nb, int* ip_version, int* udp_mode) {
59 int getoptret = 0,
60 log_level = LOG_ERR; /* default log level */
61
62 /* Arguments optionnels */
63 /* -v version
64 * -V log level
65 * -h help
66 * -a address name/ip to bind to (server mode, NULL if not specified)
67 * -s serveur address name/ip to connect (client mode)
68 * -p port
69 * -u udp mode
70 * -i v4 ip version (default support both ipv4 and ipv6 )
71 * -i v6 ip version ( " " " " " " )
72 */
73 if (argc == 1) {
74 fprintf(stderr, "No arguments given, help:\n");
75 usage();
76 exit(1);
77 }
78 while ((getoptret = getopt(argc, argv, "hvus:V:p:n:i:a:")) != EOF) {
79 switch (getoptret) {
80 case 'i':
81 if (!strcmp("v4", optarg)) {
82 (*ip_version) = NETWORK_IPV4;
83 n_log(LOG_NOTICE, "IPV4 selected");
84 } else if (!strcmp("v6", optarg)) {
85 (*ip_version) = NETWORK_IPV6;
86 n_log(LOG_NOTICE, "IPV6 selected");
87 } else {
88 n_log(LOG_NOTICE, "IPV4/6 selected");
89 }
90 break;
91 case 'v':
92 fprintf(stderr, "Date de compilation : %s a %s.\n", __DATE__, __TIME__);
93 exit(1);
94 case 'V':
95 if (!strncmp("LOG_NULL", optarg, 8)) {
97 } else {
98 if (!strncmp("LOG_NOTICE", optarg, 10)) {
100 } else {
101 if (!strncmp("LOG_INFO", optarg, 8)) {
103 } else {
104 if (!strncmp("LOG_ERR", optarg, 7)) {
106 } else {
107 if (!strncmp("LOG_DEBUG", optarg, 9)) {
109 } else {
110 fprintf(stderr, "%s n'est pas un niveau de log valide.\n", optarg);
111 exit(-1);
112 }
113 }
114 }
115 }
116 }
117 break;
118 case 'u':
119 (*udp_mode) = 1;
120 break;
121 case 's':
122 (*server) = strdup(optarg);
123 break;
124 case 'a':
125 (*address) = strdup(optarg);
126 break;
127 case 'n':
128 (*nb) = atoi(optarg);
129 break;
130 case 'p':
131 (*port) = strdup(optarg);
132 break;
133 default:
134 case '?': {
135 usage();
136 exit(1);
137 }
138 case 'h': {
139 usage();
140 exit(1);
141 }
142 }
143 }
145} /* void process_args( ... ) */
146
147int main(int argc, char** argv) {
148 char* addr = NULL;
149 char* srv = NULL;
150 char* port = NULL;
151
153
154 /* processing args and set log_level */
155 process_args(argc, argv, &addr, &srv, &port, &NB_ATTEMPTS, &ip_mode, &use_udp);
156
157 if (!port) {
158 n_log(LOG_ERR, "No port given. Exiting.");
159 exit(-1);
160 }
161
162 if (srv && addr) {
163 n_log(LOG_ERR, "Please specify only one of the following options: -a (server, addr to bind to) or -s (server on which to connect to)");
164 }
165
166 if (srv) {
167 n_log(LOG_INFO, "Client mode, connecting to %s:%s", srv, port);
168 mode = CLIENT;
169 } else {
170 n_log(LOG_INFO, "Server mode , waiting client on port %s", port);
171 mode = SERVER;
172 }
173
174#ifdef __linux__
175 if (sigchld_handler_installer() == FALSE) {
176 exit(-1);
177 }
178#endif
179
180 /* UDP mode */
181 if (use_udp) {
182 if (mode == SERVER) {
183 n_log(LOG_INFO, "Creating UDP server on %s:%s", _str(addr), _str(port));
184 if (netw_bind_udp(&netw_server, addr, port, ip_mode) == FALSE) {
185 n_log(LOG_ERR, "Fatal error binding UDP socket");
186 netw_unload();
187 exit(-1);
188 }
189 n_log(LOG_NOTICE, "UDP server listening on %s:%s", _str(netw_server->link.ip), port);
190
191 char buf[4096] = "";
192 struct sockaddr_storage client_addr;
193 socklen_t client_len = sizeof(client_addr);
194
195 for (int it = 0; it < NB_ATTEMPTS; it++) {
196 n_log(LOG_INFO, "UDP server: waiting for datagram %d/%d...", it + 1, NB_ATTEMPTS);
197 memset(buf, 0, sizeof(buf));
198 client_len = sizeof(client_addr);
199 ssize_t received = netw_udp_recvfrom(netw_server, buf, sizeof(buf) - 1, (struct sockaddr*)&client_addr, &client_len);
200 if (received > 0) {
201 buf[received] = '\0';
202
203 char client_ip[64] = "";
204 char client_port[16] = "";
205 getnameinfo((struct sockaddr*)&client_addr, client_len, client_ip, sizeof(client_ip), client_port, sizeof(client_port), NI_NUMERICHOST | NI_NUMERICSERV);
206 n_log(LOG_NOTICE, "UDP RECV from %s:%s: %s (%zd bytes)", client_ip, client_port, buf, received);
207
208 /* echo back */
209 ssize_t sent = netw_udp_sendto(netw_server, buf, (uint32_t)received, (struct sockaddr*)&client_addr, client_len);
210 if (sent > 0) {
211 n_log(LOG_NOTICE, "UDP SENT echo to %s: %zd bytes", client_ip, sent);
212 } else {
213 n_log(LOG_ERR, "UDP send error");
214 }
215 } else {
216 n_log(LOG_ERR, "UDP recv error");
217 }
218 }
220 } else if (mode == CLIENT) {
221 for (int it = 1; it <= NB_ATTEMPTS; it++) {
222 n_log(LOG_INFO, "UDP client: connecting to %s:%s (attempt %d/%d)", srv, port, it, NB_ATTEMPTS);
223 if (netw_connect_udp(&netw, srv, port, ip_mode) != TRUE) {
224 n_log(LOG_ERR, "Unable to connect UDP to %s:%s", srv, port);
225 netw_unload();
226 exit(1);
227 }
228 n_log(LOG_NOTICE, "UDP Attempt %d: Connected to %s:%s", it, srv, port);
229
230 char send_buf[256] = "";
231 snprintf(send_buf, sizeof(send_buf), "UDP Hello from client (attempt %d)", it);
232
233 ssize_t sent = send_udp_data(netw, send_buf, (uint32_t)strlen(send_buf));
234 if (sent > 0) {
235 n_log(LOG_NOTICE, "UDP SENT: %s (%zd bytes)", send_buf, sent);
236 } else {
237 n_log(LOG_ERR, "UDP send error");
239 continue;
240 }
241
242 char recv_buf[4096] = "";
243 ssize_t received = recv_udp_data(netw, recv_buf, sizeof(recv_buf) - 1);
244 if (received > 0) {
245 recv_buf[received] = '\0';
246 n_log(LOG_NOTICE, "UDP RECV: %s (%zd bytes)", recv_buf, received);
247 } else {
248 n_log(LOG_ERR, "UDP recv error or timeout");
249 }
251 }
252 }
253 }
254 /* TCP mode */
255 else if (mode == SERVER) {
256 n_log(LOG_INFO, "Creating listening network for %s:%s %d", _str(addr), _str(port), ip_mode);
257 /* create listening network */
258 if (netw_make_listening(&netw_server, addr, port, 10, ip_mode) == FALSE) {
259 n_log(LOG_ERR, "Fatal error with network initialization");
260 netw_unload();
261 exit(-1);
262 }
263
264 int it = 0;
265 for (it = 0; it < NB_ATTEMPTS / 2; it++) {
266 n_log(LOG_INFO, "Blocking on accept...");
267 /* get any accepted client on a network */
269 n_log(LOG_ERR, "Error on accept");
270 } else {
271 /* someone is connected. starting some dialog */
272 int error = 0;
273 int pthread_error = 0;
274 errno = 0;
275 pthread_error = pthread_create(&netw_thr, NULL, &manage_client, (void*)netw);
276 error = errno;
277 if (pthread_error != 0) {
278 n_log(LOG_ERR, "Error creating client management pthread:%d , error: %s", pthread_error, strerror(error));
279 netw_unload();
280 exit(-1);
281 }
282 pthread_join(netw_thr, NULL);
283 }
284 }
285 /* testing with thread pool && non blocking */
286 int error = 0;
288 while (it < NB_ATTEMPTS) {
289 /* get any accepted client on a network */
290 if ((netw = netw_accept_from_ex(netw_server, 0, 0, 0, &error))) {
291 /* someone is connected. starting some dialog */
293 n_log(LOG_ERR, "Error adding client management to thread pool");
294 }
295 it++;
296 } else {
297 n_log(LOG_DEBUG, "Waiting connections...");
298 u_sleep(250000);
299 }
301 }
302 n_log(LOG_NOTICE, "Waiting thread_pool...");
304 n_log(LOG_NOTICE, "Destroying thread_pool...");
306 n_log(LOG_NOTICE, "Setting a 2s wait close timeout to wait for last datas to be received by peer (optional)");
309 } else if (mode == CLIENT) {
310 for (int it = 1; it <= NB_ATTEMPTS; it++) {
311 if (netw_connect(&netw, srv, port, ip_mode) != TRUE) {
312 /* there were some error when trying to connect */
313 n_log(LOG_ERR, "Unable to connect to %s:%s", srv, port);
314 netw_unload();
315 exit(1);
316 }
317 n_log(LOG_NOTICE, "Attempt %d: Connected to %s:%s", it, srv, port);
318
319 /* backgrounding network send / recv */
321
322 N_STR *sended_data = NULL, *recved_data = NULL, *hostname = NULL, *tmpstr = NULL;
323
324 sended_data = char_to_nstr("SENDING DATAS...");
325 send_net_datas(netw, sended_data);
326
327 free_nstr(&sended_data);
328
329 /* let's check for an answer: test each 250000 usec, with
330 * a limit of 1000000 usec */
331 n_log(LOG_INFO, "waiting for datas back from server...");
332 tmpstr = netw_wait_msg(netw, 25000, 10000000);
333
334 if (tmpstr) {
335 get_net_datas(tmpstr, &hostname, &recved_data);
336 n_log(LOG_NOTICE, "RECEIVED DATAS: %s - %s", recved_data ? _nstr(recved_data) : "NULL", hostname ? _nstr(hostname) : "NULL");
337 free_nstr(&tmpstr);
338 free_nstr(&recved_data);
339 free_nstr(&hostname);
340 } else {
341 n_log(LOG_ERR, "Error getting back answer from server");
342 }
343 n_log(LOG_NOTICE, "Closing client connection...");
344 sleep(1);
346 }
347 }
348
349 FreeNoLog(srv);
352
353 netw_unload();
354
355 n_log(LOG_INFO, "Exiting network example");
356
357 exit(0);
358} /* END_OF_MAIN() */
static void usage(void)
void process_args(int argc, char **argv)
Definition ex_common.c:47
int main(void)
THREAD_POOL * thread_pool
Definition ex_fluid.c:77
int getoptret
Definition ex_fluid.c:60
int log_level
Definition ex_fluid.c:61
static NETWORK * netw_server
static int mode
#define SERVER
Definition ex_network.c:32
static pthread_t netw_thr
Definition ex_network.c:43
int NB_ATTEMPTS
Definition ex_network.c:35
int use_udp
Definition ex_network.c:41
NETWORK * netw
Network for server mode, accepting incomming.
Definition ex_network.c:38
int ip_mode
Definition ex_network.c:40
#define CLIENT
Definition ex_network.c:33
void * manage_client(void *ptr)
recv/send datas if any for the client
Definition ex_network.h:120
int get_net_datas(N_STR *str, N_STR **hostname, N_STR **data)
decode data we got from network
Definition ex_network.h:85
int send_net_datas(NETWORK *netw, N_STR *data)
send data to specified network
Definition ex_network.h:32
NETWORK * server
int ip_version
char * addr
char * port
#define FreeNoLog(__ptr)
Free Handler without log.
Definition n_common.h:271
#define _str(__PTR)
define true
Definition n_common.h:192
int sigchld_handler_installer()
install signal SIGCHLD handler to reap zombie processes
Definition n_common.c:266
#define _nstr(__PTR)
N_STR or "NULL" string for logging purposes.
Definition n_common.h:198
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:88
#define LOG_DEBUG
debug-level messages
Definition n_log.h:83
#define LOG_ERR
error conditions
Definition n_log.h:75
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL )
Definition n_log.c:120
#define LOG_NOTICE
normal but significant condition
Definition n_log.h:79
#define LOG_NULL
no log output
Definition n_log.h:45
#define LOG_INFO
informational
Definition n_log.h:81
#define free_nstr(__ptr)
free a N_STR structure and set the pointer to NULL
Definition n_str.h:201
N_STR * char_to_nstr(const char *src)
Convert a char into a N_STR, short version.
Definition n_str.c:254
A box including a string and his lenght.
Definition n_str.h:60
void u_sleep(unsigned int usec)
wrapper around usleep for API consistency
Definition n_time.c:53
char * ip
ip of the connected socket
Definition n_network.h:244
N_SOCKET link
networking socket
Definition n_network.h:326
int netw_bind_udp(NETWORK **netw, char *addr, char *port, int ip_version)
Create a UDP bound socket for receiving datagrams.
Definition n_network.c:2360
#define NETWORK_IPV6
Flag to force IPV6
Definition n_network.h:51
int netw_make_listening(NETWORK **netw, char *addr, char *port, int nbpending, int ip_version)
Make a NETWORK be a Listening network.
Definition n_network.c:2240
ssize_t send_udp_data(void *netw, char *buf, uint32_t n)
send data via UDP on a connected socket
Definition n_network.c:2573
int netw_start_thr_engine(NETWORK *netw)
Start the NETWORK netw Threaded Engine.
Definition n_network.c:3050
#define NETWORK_IPV4
Flag to force IPV4
Definition n_network.h:49
ssize_t netw_udp_sendto(NETWORK *netw, char *buf, uint32_t n, struct sockaddr *dest_addr, socklen_t dest_len)
send data via UDP to a specific destination address
Definition n_network.c:2650
NETWORK * netw_accept_from_ex(NETWORK *from, size_t send_list_limit, size_t recv_list_limit, int blocking, int *retval)
make a normal 'accept' .
Definition n_network.c:2713
#define NETWORK_IPALL
Flag for auto detection by OS of ip version to use.
Definition n_network.h:47
#define NETWORK_WAIT_CLOSE_TIMEOUT
Flag to set network closing wait timeout.
Definition n_network.h:63
int netw_setsockopt(NETWORK *netw, int optname, int value)
Modify common socket options on the given netw.
Definition n_network.c:929
ssize_t recv_udp_data(void *netw, char *buf, uint32_t n)
recv data via UDP from a connected socket
Definition n_network.c:2614
NETWORK * netw_accept_from(NETWORK *from)
make a normal blocking 'accept' .
Definition n_network.c:2894
int netw_close(NETWORK **netw)
Closing a specified Network, destroy queues, free the structure.
Definition n_network.c:2041
N_STR * netw_wait_msg(NETWORK *netw, unsigned int refresh, size_t timeout)
Wait a message from aimed NETWORK.
Definition n_network.c:2998
int netw_connect(NETWORK **netw, char *host, char *port, int ip_version)
Use this to connect a NETWORK to any listening one, unrestricted send/recv lists.
Definition n_network.c:1814
int netw_connect_udp(NETWORK **netw, char *host, char *port, int ip_version)
Connect a UDP socket to a remote host.
Definition n_network.c:2468
ssize_t netw_udp_recvfrom(NETWORK *netw, char *buf, uint32_t n, struct sockaddr *src_addr, socklen_t *src_len)
recv data via UDP and capture the source address
Definition n_network.c:2682
Structure of a NETWORK.
Definition n_network.h:258
THREAD_POOL * new_thread_pool(size_t nbmaxthr, size_t nb_max_waiting)
Create a new pool of nbmaxthr threads.
int add_threaded_process(THREAD_POOL *thread_pool, void *(*func_ptr)(void *param), void *param, int mode)
add a function and params to a thread pool
int refresh_thread_pool(THREAD_POOL *thread_pool)
try to add some waiting DIRECT_PROCs on some free thread slots, else do nothing
int wait_for_threaded_pool(THREAD_POOL *thread_pool)
Wait for the thread pool to become idle (no active threads, empty waiting list), blocking without pol...
int destroy_threaded_pool(THREAD_POOL **pool, unsigned int delay)
delete a thread_pool, exit the threads and free the structs
#define DIRECT_PROC
processing mode for added func, direct start, not queued
Structure of a thread pool.