Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_network_proxy.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 "nilorea/n_common.h"
28#include "nilorea/n_log.h"
29#include "nilorea/n_network.h"
30
31#include <stdio.h>
32#include <string.h>
33
34int main(int argc, char* argv[]) {
35 int errors = 0;
36
37 /* parse log level from -V flag if present */
38 int log_level = LOG_INFO;
39 for (int i = 1; i < argc; i++) {
40 if (strcmp(argv[i], "-V") == 0 && i + 1 < argc) {
41 if (strcmp(argv[i + 1], "LOG_DEBUG") == 0)
43 else if (strcmp(argv[i + 1], "LOG_ERR") == 0)
45 else if (strcmp(argv[i + 1], "LOG_NOTICE") == 0)
47 }
48 }
50
51 n_log(LOG_INFO, "ex_network_proxy: proxy URL parser demo");
52
53 /* Test 1: authenticated HTTP proxy */
54 {
55 N_PROXY_CFG* cfg = n_proxy_cfg_parse("http://user:pass@proxy.corp:3128");
56 if (cfg) {
57 n_log(LOG_INFO, " scheme=%s host=%s port=%d user=%s pass=%s",
58 cfg->scheme, cfg->host, cfg->port,
59 cfg->username ? cfg->username : "(null)",
60 cfg->password ? cfg->password : "(null)");
61 if (strcmp(cfg->scheme, "http") != 0) errors++;
62 if (strcmp(cfg->host, "proxy.corp") != 0) errors++;
63 if (cfg->port != 3128) errors++;
64 if (!cfg->username || strcmp(cfg->username, "user") != 0) errors++;
65 if (!cfg->password || strcmp(cfg->password, "pass") != 0) errors++;
66 n_proxy_cfg_free(&cfg);
67 } else {
68 n_log(LOG_ERR, " FAIL: parse returned NULL");
69 errors++;
70 }
71 }
72
73 /* Test 2: unauthenticated SOCKS5 proxy */
74 {
75 N_PROXY_CFG* cfg = n_proxy_cfg_parse("socks5://proxy.corp:1080");
76 if (cfg) {
77 n_log(LOG_INFO, " scheme=%s host=%s port=%d user=%s",
78 cfg->scheme, cfg->host, cfg->port,
79 cfg->username ? cfg->username : "(null)");
80 if (strcmp(cfg->scheme, "socks5") != 0) errors++;
81 if (strcmp(cfg->host, "proxy.corp") != 0) errors++;
82 if (cfg->port != 1080) errors++;
83 if (cfg->username != NULL) errors++;
84 n_proxy_cfg_free(&cfg);
85 } else {
86 n_log(LOG_ERR, " FAIL: parse returned NULL");
87 errors++;
88 }
89 }
90
91 /* Test 3: authenticated HTTPS proxy */
92 {
93 N_PROXY_CFG* cfg = n_proxy_cfg_parse("https://admin:secret@secure-proxy.corp:8443");
94 if (cfg) {
95 n_log(LOG_INFO, " scheme=%s host=%s port=%d user=%s pass=%s",
96 cfg->scheme, cfg->host, cfg->port,
97 cfg->username ? cfg->username : "(null)",
98 cfg->password ? cfg->password : "(null)");
99 if (strcmp(cfg->scheme, "https") != 0) errors++;
100 if (strcmp(cfg->host, "secure-proxy.corp") != 0) errors++;
101 if (cfg->port != 8443) errors++;
102 if (!cfg->username || strcmp(cfg->username, "admin") != 0) errors++;
103 if (!cfg->password || strcmp(cfg->password, "secret") != 0) errors++;
104 n_proxy_cfg_free(&cfg);
105 } else {
106 n_log(LOG_ERR, " FAIL: parse returned NULL");
107 errors++;
108 }
109 }
110
111 /* Test 4: HTTPS proxy with default port */
112 {
113 N_PROXY_CFG* cfg = n_proxy_cfg_parse("https://secure-proxy.corp");
114 if (cfg) {
115 n_log(LOG_INFO, " scheme=%s host=%s port=%d",
116 cfg->scheme, cfg->host, cfg->port);
117 if (strcmp(cfg->scheme, "https") != 0) errors++;
118 if (cfg->port != 3128) errors++;
119 n_proxy_cfg_free(&cfg);
120 } else {
121 n_log(LOG_ERR, " FAIL: parse returned NULL");
122 errors++;
123 }
124 }
125
126 /* Test 5: invalid URL */
127 {
128 N_PROXY_CFG* cfg = n_proxy_cfg_parse("not-a-url");
129 if (cfg) {
130 n_log(LOG_ERR, " FAIL: expected NULL for invalid URL");
131 n_proxy_cfg_free(&cfg);
132 errors++;
133 } else {
134 n_log(LOG_INFO, " invalid URL correctly returned NULL");
135 }
136 }
137
138 n_log(LOG_INFO, "ex_network_proxy: %s (%d error%s)",
139 errors == 0 ? "PASS" : "FAIL", errors, errors == 1 ? "" : "s");
140 return errors > 0 ? 1 : 0;
141}
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.