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

Demonstrates n_proxy_cfg_parse and n_proxy_cfg_free.

Demonstrates n_proxy_cfg_parse and n_proxy_cfg_free.

Author
Castagnier Mickael
Version
1.0
Date
27/03/2026
/*
* Nilorea Library
* Copyright (C) 2005-2026 Castagnier Mickael
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "nilorea/n_log.h"
#include <stdio.h>
#include <string.h>
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");
}
}
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:61
#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_INFO
informational
Definition n_log.h:81
char * host
proxy hostname
Definition n_network.h:556
char * password
NULL if no auth.
Definition n_network.h:559
int port
proxy port
Definition n_network.h:557
char * scheme
"http", "https", or "socks5"
Definition n_network.h:555
char * username
NULL if no auth.
Definition n_network.h:558
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:5745
void n_proxy_cfg_free(N_PROXY_CFG **cfg)
Free an N_PROXY_CFG created by n_proxy_cfg_parse().
Definition n_network.c:5840
Parsed proxy URL components.
Definition n_network.h:554
Common headers and low-level functions & define.
Generic log system.
Network Engine.