File: init.c

package info (click to toggle)
wget2 2.2.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 22,248 kB
  • sloc: ansic: 121,144; sh: 11,559; makefile: 878; xml: 182; sed: 16
file content (289 lines) | stat: -rw-r--r-- 7,816 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
 * Copyright (c) 2013 Tim Ruehsen
 * Copyright (c) 2015-2024 Free Software Foundation, Inc.
 *
 * This file is part of libwget.
 *
 * Libwget is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Libwget 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with libwget.  If not, see <https://www.gnu.org/licenses/>.
 *
 *
 * Memory allocation routines
 *
 * Changelog
 * 18.01.2013  Tim Ruehsen  created
 *
 */

#include <config.h>

#include <stdarg.h>

#include <wget.h>
#include "private.h"

static struct config {
	char
		*cookie_file;
	wget_cookie_db
		*cookie_db;
	bool
		cookies_enabled,
		keep_session_cookies;
} config = {
	.cookies_enabled = 0
};

static wget_dns_cache *dns_cache;

static int global_initialized;
static wget_thread_mutex _mutex;
static bool initialized;

static void global_exit(void)
{
	if (initialized) {
		wget_thread_mutex_destroy(&_mutex);
		initialized = false;
	}
}

INITIALIZER(global_init)
{
	if (!initialized) {
		wget_thread_mutex_init(&_mutex);
		initialized = true;
		atexit(global_exit);
	}
}

/**
 * Global library initialization, allocating/preparing all resources.
 *
 * This function is not thread-safe on systems without automatic library
 * initialization.
 */
void wget_global_init(int first_key, ...)
{
	va_list args;
	int key, rc, caching;
	const char *psl_file = NULL;
	wget_logger_func *func; // intermediate var to satisfy MSVC

	// just in case that automatic initializers didn't work,
	// e.g. maybe a static build
	global_init();

	wget_thread_mutex_lock(_mutex);

	if (global_initialized++) {
		wget_thread_mutex_unlock(_mutex);
		return;
	}

	wget_console_init();
	wget_random_init();
	wget_http_init();

	va_start (args, first_key);
	for (key = first_key; key; key = va_arg(args, int)) {
		switch (key) {
		case WGET_DEBUG_STREAM:
			wget_logger_set_stream(wget_get_logger(WGET_LOGGER_DEBUG), va_arg(args, FILE *));
			break;
		case WGET_DEBUG_FUNC:
			func = va_arg(args, wget_logger_func *);
			wget_logger_set_func(wget_get_logger(WGET_LOGGER_DEBUG), func);
			break;
		case WGET_DEBUG_FILE:
			wget_logger_set_file(wget_get_logger(WGET_LOGGER_DEBUG), va_arg(args, const char *));
			break;
		case WGET_ERROR_STREAM:
			wget_logger_set_stream(wget_get_logger(WGET_LOGGER_ERROR), va_arg(args, FILE *));
			break;
		case WGET_ERROR_FUNC:
			func = va_arg(args, wget_logger_func *);
			wget_logger_set_func(wget_get_logger(WGET_LOGGER_ERROR), func);
			break;
		case WGET_ERROR_FILE:
			wget_logger_set_file(wget_get_logger(WGET_LOGGER_ERROR), va_arg(args, const char *));
			break;
		case WGET_INFO_STREAM:
			wget_logger_set_stream(wget_get_logger(WGET_LOGGER_INFO), va_arg(args, FILE *));
			break;
		case WGET_INFO_FUNC:
			func = va_arg(args, wget_logger_func *);
			wget_logger_set_func(wget_get_logger(WGET_LOGGER_INFO), func);
			break;
		case WGET_INFO_FILE:
			wget_logger_set_file(wget_get_logger(WGET_LOGGER_INFO), va_arg(args, const char *));
			break;
		case WGET_DNS_CACHING:
			caching = va_arg(args, int);
			if (caching) {
				if ((rc = wget_dns_cache_init(&dns_cache)) == WGET_E_SUCCESS)
					wget_dns_set_cache(NULL, dns_cache);
				else
					wget_error_printf(_("Failed to init DNS cache (%d)"), rc);
			}
			break;
		case WGET_TCP_FASTFORWARD:
			wget_tcp_set_tcp_fastopen(NULL, va_arg(args, int) != 0);
			break;
		case WGET_COOKIE_SUFFIXES:
			psl_file = va_arg(args, const char *);
			config.cookies_enabled = 1;
			break;
		case WGET_COOKIES_ENABLED:
			config.cookies_enabled = va_arg(args, int) != 0;
			break;
		case WGET_COOKIE_FILE:
			// load cookie-store
			config.cookies_enabled = 1;
			config.cookie_file = va_arg(args, char *);
			break;
		case WGET_COOKIE_KEEPSESSIONCOOKIES:
			config.keep_session_cookies = va_arg(args, int) != 0;
			break;
		case WGET_BIND_ADDRESS:
			wget_tcp_set_bind_address(NULL, va_arg(args, const char *));
			break;
		case WGET_NET_FAMILY_EXCLUSIVE:
			wget_tcp_set_family(NULL, va_arg(args, int));
			break;
		case WGET_NET_FAMILY_PREFERRED:
			wget_tcp_set_preferred_family(NULL, va_arg(args, int));
			break;
		case WGET_BIND_INTERFACE:
			wget_tcp_set_bind_interface(NULL, va_arg(args, const char *));
			break;
		default:
			wget_thread_mutex_unlock(_mutex);
			wget_error_printf(_("%s: Unknown option %d"), __func__, key);
			va_end(args);
			return;
		}
	}
	va_end(args);

	if (config.cookies_enabled && config.cookie_file) {
		config.cookie_db = wget_cookie_db_init(NULL);
		wget_cookie_set_keep_session_cookies(config.cookie_db, config.keep_session_cookies);
		wget_cookie_db_load(config.cookie_db, config.cookie_file);
		wget_cookie_db_load_psl(config.cookie_db, psl_file);
	}

	rc = wget_net_init();

	wget_thread_mutex_unlock(_mutex);

	if (rc)
		wget_error_printf_exit(_("%s: Failed to init networking (%d)"), __func__, rc);
}

/**
 * Global library deinitialization, free'ing all allocated resources.
 *
 * This function is not thread-safe.
 */
void wget_global_deinit(void)
{
	int rc = 0;

	// we need to lock a static mutex here

	if (global_initialized == 1) {
		// free resources here
		if (config.cookie_db && config.cookies_enabled && config.cookie_file) {
			wget_cookie_db_save(config.cookie_db, config.cookie_file);
			wget_cookie_db_free(&config.cookie_db);
		}
		wget_tcp_set_bind_address(NULL, NULL);

		wget_dns_cache_free(&dns_cache);

		rc = wget_net_deinit();
		wget_ssl_deinit();
		wget_http_set_http_proxy(NULL, NULL);
		wget_http_set_https_proxy(NULL, NULL);
		wget_http_set_no_proxy(NULL, NULL);
	}

	if (global_initialized > 0) global_initialized--;

	global_exit();

	// we need to unlock a static mutex here

	if (rc)
		wget_error_printf(_("%s: Failed to deinit networking (%d)"), __func__, rc);

	wget_console_deinit();
}

int wget_global_get_int(int key)
{
	switch (key) {
	case WGET_COOKIES_ENABLED:
		return config.cookies_enabled;
	case WGET_COOKIE_KEEPSESSIONCOOKIES:
		return config.keep_session_cookies;
	case WGET_NET_FAMILY_EXCLUSIVE:
		return wget_tcp_get_family(NULL);
	case WGET_NET_FAMILY_PREFERRED:
		return wget_tcp_get_preferred_family(NULL);
	default:
		wget_error_printf(_("%s: Unknown option %d"), __func__, key);
		return 0;
	}
}

const void *wget_global_get_ptr(int key)
{
	switch (key) {
	case WGET_DEBUG_STREAM:
		return wget_logger_get_stream(wget_get_logger(WGET_LOGGER_DEBUG));
	case WGET_DEBUG_FILE:
		return wget_logger_get_file(wget_get_logger(WGET_LOGGER_DEBUG));
	case WGET_ERROR_STREAM:
		return wget_logger_get_stream(wget_get_logger(WGET_LOGGER_ERROR));
	case WGET_ERROR_FILE:
		return wget_logger_get_file(wget_get_logger(WGET_LOGGER_ERROR));
	case WGET_INFO_STREAM:
		return wget_logger_get_stream(wget_get_logger(WGET_LOGGER_INFO));
	case WGET_INFO_FILE:
		return wget_logger_get_file(wget_get_logger(WGET_LOGGER_INFO));
	case WGET_COOKIE_FILE:
		return config.cookie_file;
	case WGET_COOKIE_DB:
		return config.cookie_db;
	default:
		wget_error_printf(_("%s: Unknown option %d"), __func__, key);
		return NULL;
	}
}

wget_global_func *wget_global_get_func(int key)
{
	switch (key) {
	case WGET_DEBUG_FUNC:
		return wget_logger_get_func(wget_get_logger(WGET_LOGGER_DEBUG));
	case WGET_ERROR_FUNC:
		return wget_logger_get_func(wget_get_logger(WGET_LOGGER_ERROR));
	case WGET_INFO_FUNC:
		return wget_logger_get_func(wget_get_logger(WGET_LOGGER_INFO));
	default:
		wget_error_printf(_("%s: Unknown option %d"), __func__, key);
		return NULL;
	}
}