Debian Codegraph @

src/network.c

raw symbols SCIP
1 /*
2 * HTTPDirFS - HTTP Directory Filesystem
3 *
4 * Copyright (C) 2020-2026 Fufu Fang <[email protected]>
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * In addition, as a special exception, the copyright holders give
20 * permission to link the code of portions of this program with the OpenSSL
21 * library.
22 */
23
24 /**
25 * \file network.c
26 * \brief Network transfer and cURL wrapper implementation
27 */
28
29 #include "network.h"
30
31 #include "config.h"
32 #include "link.h"
33 #include "log.h"
34 #include "memcache.h"
35 #include "util.h"
36
37 #include <errno.h>
38 #include <openssl/crypto.h>
39 #include <stdio.h>
40 #include <string.h>
41 #include <unistd.h>
42
43 /*
44 * ----------------- External variables ----------------------
45 */
46 CURLSH *CURL_SHARE;
47
48 /*
49 * ----------------- Static variable -----------------------
50 */
51 /** \brief curl multi interface handle */
52 static CURLM *curl_multi;
53 /** \brief mutex for transfer functions */
54 static pthread_mutex_t transfer_lock;
55 /** \brief the lock array for cryptographic functions */
56 static pthread_mutex_t *crypto_lockarray;
57 /** \brief mutex for curl share interface itself */
58 static pthread_mutex_t curl_lock;
59
60 /*
61 * -------------------- Functions --------------------------
62 */
63 #pragma GCC diagnostic push
64 #pragma GCC diagnostic ignored "-Wunused-function"
65 /**
66 * \brief OpenSSL 1.02 cryptography callback function
67 * \details Required for OpenSSL 1.02, but not OpenSSL 1.1
68 */
69 static void crypto_lock_callback(int mode, int type, const char *file, int line)
70 {
71 (void)file;
72 (void)line;
73 if (mode & CRYPTO_LOCK) {
74 PTHREAD_MUTEX_LOCK(&(crypto_lockarray[type]));
75 } else {
76 PTHREAD_MUTEX_UNLOCK(&(crypto_lockarray[type]));
77 }
78 }
79
80 /**
81 * \brief OpenSSL 1.02 thread ID function
82 * \details Required for OpenSSL 1.02, but not OpenSSL 1.1
83 */
84 static unsigned long thread_id(void)
85 {
86 unsigned long ret;
87
88 ret = (unsigned long)pthread_self();
89 return ret;
90 }
91
92 #pragma GCC diagnostic pop
93
94 static void crypto_lock_init(void)
95 {
96 int i;
97
98 crypto_lockarray = (pthread_mutex_t *)OPENSSL_malloc(
99 CRYPTO_num_locks() * sizeof(pthread_mutex_t));
100 for (i = 0; i < CRYPTO_num_locks(); i++) {
101 PTHREAD_MUTEX_INIT(&(crypto_lockarray[i]), NULL);
102 }
103
104 CRYPTO_set_id_callback((unsigned long (*)())thread_id);
105 CRYPTO_set_locking_callback((void (*)())crypto_lock_callback);
106 }
107
108 /**
109 * \brief Curl share handle callback function
110 * \details Adapted from:
111 * https://curl.haxx.se/libcurl/c/threaded-shared-conn.html
112 */
113 static void curl_callback_lock(CURL *handle, curl_lock_data data,
114 curl_lock_access access, void *userptr)
115 {
116 (void)access; /* unused */
117 (void)userptr; /* unused */
118 (void)handle; /* unused */
119 (void)data; /* unused */
120 PTHREAD_MUTEX_LOCK(&curl_lock);
121 }
122
123 static void curl_callback_unlock(CURL *handle, curl_lock_data data,
124 void *userptr)
125 {
126 (void)userptr; /* unused */
127 (void)handle; /* unused */
128 (void)data; /* unused */
129 PTHREAD_MUTEX_UNLOCK(&curl_lock);
130 }
131
132 /**
133 * \brief Process a curl message
134 * \details Adapted from:
135 * https://curl.haxx.se/libcurl/c/10-at-a-time.html
136 */
137 static void curl_process_msgs(CURLMsg *curl_msg, int n_running_curl,
138 int n_mesgs)
139 {
140 (void)n_running_curl;
141 (void)n_mesgs;
142 if (curl_msg->msg == CURLMSG_DONE) {
143 TransferStruct *ts;
144 CURL *curl = curl_msg->easy_handle;
145 CURLcode ret
146 = curl_easy_getinfo(curl_msg->easy_handle, CURLINFO_PRIVATE, &ts);
147 if (ret) {
148 lprintf(error, "%s\n", curl_easy_strerror(ret));
149 }
150 ts->transferring = 0;
151 char *url = NULL;
152 ret = curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &url);
153 if (ret) {
154 lprintf(error, "%s\n", curl_easy_strerror(ret));
155 }
156
157 if (!curl_msg->data.result) {
158 /*
159 * Transfer successful, set the file size
160 */
161 if (ts->type == FILESTAT) {
162 Link_set_file_stat(ts->link, curl);
163 }
164 } else {
165 lprintf(error, "%d - %s <%s>\n", curl_msg->data.result,
166 curl_easy_strerror(curl_msg->data.result), url);
167 /*
168 * If the transfer failed, and we are querying the file size,
169 * we must mark the link as invalid so that the link table
170 * fill function can proceed.
171 */
172 if (ts->type == FILESTAT) {
173 ts->link->type = LINK_INVALID;
174 }
175 }
176 curl_multi_remove_handle(curl_multi, curl);
177 /*
178 * clean up the handle, if we are querying the file size
179 */
180 if (ts->type == FILESTAT) {
181 curl_easy_cleanup(curl);
182 FREE(ts);
183 }
184 } else {
185 lprintf(warning, "curl_msg->msg: %d\n", curl_msg->msg);
186 }
187 }
188
189 /**
190 * \details effectively based on
191 * https://curl.haxx.se/libcurl/c/multi-double.html
192 */
193 int curl_multi_perform_once(void)
194 {
195 lprintf(network_lock_debug, "thread %lx: locking transfer_lock;\n",
196 (unsigned long)pthread_self());
197 PTHREAD_MUTEX_LOCK(&transfer_lock);
198
199 /*
200 * Get curl multi interface to perform pending tasks
201 */
202 int n_running_curl = 1;
203 CURLMcode mc = curl_multi_perform(curl_multi, &n_running_curl);
204 if (mc) {
205 lprintf(error, "%s\n", curl_multi_strerror(mc));
206 }
207
208 if (n_running_curl) {
209 mc = curl_multi_poll(curl_multi, NULL, 0, 100, NULL);
210 if (mc) {
211 lprintf(error, "%s\n", curl_multi_strerror(mc));
212 }
213 }
214
215 /*
216 * Process the message queue
217 */
218 int n_mesgs;
219 CURLMsg *curl_msg;
220 while ((curl_msg = curl_multi_info_read(curl_multi, &n_mesgs))) {
221 curl_process_msgs(curl_msg, n_running_curl, n_mesgs);
222 }
223
224 lprintf(network_lock_debug, "thread %lx: unlocking transfer_lock;\n",
225 (unsigned long)pthread_self());
226 PTHREAD_MUTEX_UNLOCK(&transfer_lock);
227
228 return n_running_curl;
229 }
230
231 void NetworkSystem_init(void)
232 {
233 /*
234 * ------- Global related ----------
235 */
236 if (curl_global_init(CURL_GLOBAL_ALL)) {
237 lprintf(fatal, "curl_global_init() failed!\n");
238 }
239
240 /*
241 * -------- Share related ----------
242 */
243 CURL_SHARE = curl_share_init();
244 if (!(CURL_SHARE)) {
245 lprintf(fatal, "curl_share_init() failed!\n");
246 }
247
248 curl_share_setopt(CURL_SHARE, CURLSHOPT_SHARE, CURL_LOCK_DATA_COOKIE);
249 curl_share_setopt(CURL_SHARE, CURLSHOPT_SHARE, CURL_LOCK_DATA_DNS);
250 curl_share_setopt(CURL_SHARE, CURLSHOPT_SHARE, CURL_LOCK_DATA_SSL_SESSION);
251
252 PTHREAD_MUTEX_INIT(&curl_lock, NULL);
253
254 curl_share_setopt(CURL_SHARE, CURLSHOPT_LOCKFUNC, curl_callback_lock);
255 curl_share_setopt(CURL_SHARE, CURLSHOPT_UNLOCKFUNC, curl_callback_unlock);
256
257 /*
258 * ------------- Multi related -----------
259 */
260 curl_multi = curl_multi_init();
261 if (!curl_multi) {
262 lprintf(fatal, "curl_multi_init() failed!\n");
263 }
264 curl_multi_setopt(curl_multi, CURLMOPT_MAX_TOTAL_CONNECTIONS,
265 CONFIG.max_conns);
266 curl_multi_setopt(curl_multi, CURLMOPT_MAX_HOST_CONNECTIONS,
267 CONFIG.max_conns);
268
269 /*
270 * ------------ Initialise locks ---------
271 */
272 PTHREAD_MUTEX_INIT(&transfer_lock, NULL);
273
274 /*
275 * cryptographic lock functions were shamelessly copied from
276 * https://curl.haxx.se/libcurl/c/threaded-ssl.html
277 */
278 crypto_lock_init();
279 }
280
281 void transfer_blocking(CURL *curl)
282 {
283 TransferStruct *ts;
284 CURLcode ret = curl_easy_getinfo(curl, CURLINFO_PRIVATE, &ts);
285 if (ret) {
286 lprintf(error, "%s\n", curl_easy_strerror(ret));
287 }
288
289 lprintf(network_lock_debug, "thread %lx: locking transfer_lock;\n",
290 (unsigned long)pthread_self());
291 PTHREAD_MUTEX_LOCK(&transfer_lock);
292
293 CURLMcode res = curl_multi_add_handle(curl_multi, curl);
294 if (res > 0) {
295 lprintf(error, "%d, %s\n", res, curl_multi_strerror(res));
296 }
297
298 lprintf(network_lock_debug, "thread %lx: unlocking transfer_lock;\n",
299 (unsigned long)pthread_self());
300 PTHREAD_MUTEX_UNLOCK(&transfer_lock);
301
302 while (ts->transferring) {
303 curl_multi_perform_once();
304 }
305 }
306
307 void transfer_nonblocking(CURL *curl)
308 {
309 lprintf(network_lock_debug, "thread %lx: locking transfer_lock;\n",
310 (unsigned long)pthread_self());
311 PTHREAD_MUTEX_LOCK(&transfer_lock);
312
313 CURLMcode res = curl_multi_add_handle(curl_multi, curl);
314 if (res > 0) {
315 lprintf(error, "%s\n", curl_multi_strerror(res));
316 }
317
318 lprintf(network_lock_debug, "thread %lx: unlocking transfer_lock;\n",
319 (unsigned long)pthread_self());
320 PTHREAD_MUTEX_UNLOCK(&transfer_lock);
321 }
322
323 int HTTP_temp_failure(HTTPResponseCode http_resp)
324 {
325 switch (http_resp) {
326 case HTTP_TOO_MANY_REQUESTS:
327 case HTTP_CLOUDFLARE_UNKNOWN_ERROR:
328 case HTTP_CLOUDFLARE_TIMEOUT:
329 return 1;
330 default:
331 return 0;
332 }
333 }