Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_network_proxy.c

Demonstrates n_proxy_cfg_parse / n_proxy_cfg_free and netw_adopt_client_fd.

Demonstrates n_proxy_cfg_parse / n_proxy_cfg_free and netw_adopt_client_fd.

Author
Castagnier Mickael
Version
1.0
Date
27/03/2026
/*
* Nilorea Library
* Copyright (C) 2005-2026 Castagnier Mickael
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "nilorea/n_log.h"
#include <stdio.h>
#include <string.h>
#ifndef __windows__
#include <sys/socket.h>
#include <unistd.h>
#endif
int main(int argc, char* argv[]) {
int errors = 0;
/* parse log level from -V flag if present */
for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], "-V") == 0 && i + 1 < argc) {
if (strcmp(argv[i + 1], "LOG_DEBUG") == 0)
else if (strcmp(argv[i + 1], "LOG_ERR") == 0)
else if (strcmp(argv[i + 1], "LOG_NOTICE") == 0)
}
}
n_log(LOG_INFO, "ex_network_proxy: proxy URL parser demo");
/* Test 1: authenticated HTTP proxy */
{
N_PROXY_CFG* cfg = n_proxy_cfg_parse("http://user:pass@proxy.corp:3128");
if (cfg) {
n_log(LOG_INFO, " scheme=%s host=%s port=%d user=%s pass=%s",
cfg->scheme, cfg->host, cfg->port,
cfg->username ? cfg->username : "(null)",
cfg->password ? cfg->password : "(null)");
if (strcmp(cfg->scheme, "http") != 0) errors++;
if (strcmp(cfg->host, "proxy.corp") != 0) errors++;
if (cfg->port != 3128) errors++;
if (!cfg->username || strcmp(cfg->username, "user") != 0) errors++;
if (!cfg->password || strcmp(cfg->password, "pass") != 0) errors++;
} else {
n_log(LOG_ERR, " FAIL: parse returned NULL");
errors++;
}
}
/* Test 2: unauthenticated SOCKS5 proxy */
{
N_PROXY_CFG* cfg = n_proxy_cfg_parse("socks5://proxy.corp:1080");
if (cfg) {
n_log(LOG_INFO, " scheme=%s host=%s port=%d user=%s",
cfg->scheme, cfg->host, cfg->port,
cfg->username ? cfg->username : "(null)");
if (strcmp(cfg->scheme, "socks5") != 0) errors++;
if (strcmp(cfg->host, "proxy.corp") != 0) errors++;
if (cfg->port != 1080) errors++;
if (cfg->username != NULL) errors++;
} else {
n_log(LOG_ERR, " FAIL: parse returned NULL");
errors++;
}
}
/* Test 3: authenticated HTTPS proxy */
{
N_PROXY_CFG* cfg = n_proxy_cfg_parse("https://admin:secret@secure-proxy.corp:8443");
if (cfg) {
n_log(LOG_INFO, " scheme=%s host=%s port=%d user=%s pass=%s",
cfg->scheme, cfg->host, cfg->port,
cfg->username ? cfg->username : "(null)",
cfg->password ? cfg->password : "(null)");
if (strcmp(cfg->scheme, "https") != 0) errors++;
if (strcmp(cfg->host, "secure-proxy.corp") != 0) errors++;
if (cfg->port != 8443) errors++;
if (!cfg->username || strcmp(cfg->username, "admin") != 0) errors++;
if (!cfg->password || strcmp(cfg->password, "secret") != 0) errors++;
} else {
n_log(LOG_ERR, " FAIL: parse returned NULL");
errors++;
}
}
/* Test 4: HTTPS proxy with default port */
{
N_PROXY_CFG* cfg = n_proxy_cfg_parse("https://secure-proxy.corp");
if (cfg) {
n_log(LOG_INFO, " scheme=%s host=%s port=%d",
cfg->scheme, cfg->host, cfg->port);
if (strcmp(cfg->scheme, "https") != 0) errors++;
if (cfg->port != 3128) errors++;
} else {
n_log(LOG_ERR, " FAIL: parse returned NULL");
errors++;
}
}
/* Test 5: invalid URL */
{
N_PROXY_CFG* cfg = n_proxy_cfg_parse("not-a-url");
if (cfg) {
n_log(LOG_ERR, " FAIL: expected NULL for invalid URL");
errors++;
} else {
n_log(LOG_INFO, " invalid URL correctly returned NULL");
}
}
/* Test 6: adopt an already-connected socket fd into a client NETWORK and
* round-trip raw bytes over its plain send/recv vtable. A socketpair stands
* in for the fd a proxy CONNECT tunnel would return. POSIX-only: Winsock has
* no socketpair()/AF_UNIX equivalent. */
#ifndef __windows__
{
int sv[2];
if (socketpair(AF_UNIX, SOCK_STREAM, 0, sv) != 0) {
n_log(LOG_ERR, " FAIL: socketpair failed");
errors++;
} else {
NETWORK* netw = NULL;
if (netw_adopt_client_fd(&netw, (SOCKET)sv[0], "tunnel.target", "443") == TRUE && netw) {
char rbuf[8];
/* peer -> NETWORK */
if (send(sv[1], "PING", 4, 0) != 4) errors++;
memset(rbuf, 0, sizeof(rbuf));
if (netw->recv_data(netw, rbuf, 4) != 4 || memcmp(rbuf, "PING", 4) != 0) {
n_log(LOG_ERR, " FAIL: adopted recv mismatch");
errors++;
}
/* NETWORK -> peer */
if (netw->send_data(netw, "PONG", 4) != 4) errors++;
memset(rbuf, 0, sizeof(rbuf));
if (recv(sv[1], rbuf, 4, 0) != 4 || memcmp(rbuf, "PONG", 4) != 0) {
n_log(LOG_ERR, " FAIL: adopted send mismatch");
errors++;
}
if (errors == 0)
n_log(LOG_INFO, " adopted fd round-trip OK");
netw_close(&netw); /* closes sv[0] */
} else {
n_log(LOG_ERR, " FAIL: netw_adopt_client_fd returned FALSE");
errors++;
closesocket(sv[0]);
}
closesocket(sv[1]);
}
}
#endif /* !__windows__ */
/* Test 7: netw_adopt_client_fd rejects a non-empty target and a bad fd. */
{
NETWORK* netw = NULL;
if (netw_adopt_client_fd(&netw, INVALID_SOCKET, "h", "1") != FALSE) {
n_log(LOG_ERR, " FAIL: expected FALSE for INVALID_SOCKET");
errors++;
} else {
n_log(LOG_INFO, " invalid fd correctly returned FALSE");
}
}
n_log(LOG_INFO, "ex_network_proxy: %s (%d error%s)",
errors == 0 ? "PASS" : "FAIL", errors, errors == 1 ? "" : "s");
return errors > 0 ? 1 : 0;
}
int main(void)
int log_level
Definition ex_fluid.c:60
NETWORK * netw
Network for server mode, accepting incomming.
Definition ex_network.c:39
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:89
#define LOG_DEBUG
debug-level messages
Definition n_log.h:84
#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_NOTICE
normal but significant condition
Definition n_log.h:80
#define LOG_INFO
informational
Definition n_log.h:82
char * host
proxy hostname
Definition n_network.h:815
char * password
NULL if no auth.
Definition n_network.h:818
int port
proxy port
Definition n_network.h:816
char * scheme
"http", "https", or "socks5"
Definition n_network.h:814
netw_func send_data
send func ptr
Definition n_network.h:359
char * username
NULL if no auth.
Definition n_network.h:817
netw_func recv_data
receive func ptr
Definition n_network.h:361
N_PROXY_CFG * n_proxy_cfg_parse(const char *url)
Parse a proxy URL string into an N_PROXY_CFG struct.
Definition n_network.c:7597
int netw_adopt_client_fd(NETWORK **netw, SOCKET fd, const char *host, const char *port)
Adopt an already-connected socket fd into a fresh client NETWORK.
Definition n_network.c:8040
int SOCKET
default socket declaration
Definition n_network.h:102
void n_proxy_cfg_free(N_PROXY_CFG **cfg)
Free an N_PROXY_CFG created by n_proxy_cfg_parse().
Definition n_network.c:7692
int netw_close(NETWORK **netw)
Closing a specified Network, destroy queues, free the structure.
Definition n_network.c:2662
Parsed proxy URL components.
Definition n_network.h:813
Structure of a NETWORK.
Definition n_network.h:309
Common headers and low-level functions & define.
Generic log system.
Network Engine.