[BACK]Return to client.c CVS log [TXT][DIR] Up to [local] / acopm / lib / proxy

File: [local] / acopm / lib / proxy / client.c (download)

Revision 1.1.1.1 (vendor branch), Sat May 8 15:42:18 2021 UTC (3 years, 2 months ago) by bountyht
Branch: alphachat, MAIN
CVS Tags: start, HEAD
Changes since 1.1: +0 -0 lines

Initial import

/*
 * Copyright (C) 2017 Aaron M. D. Jones <aaronmdjones@gmail.com>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following
 * conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its
 *    contributors may be used to endorse or promote products derived from
 *    this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 * PARTICULAR PURPOSE ARE DISCLAIMED.
 *
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "acopm-common.h"

#include <assert.h> /* assert() */
#include <errno.h> /* errno */
#include <string.h> /* strlen() */
#include <time.h> /* time_t */

#include <event2/event.h> /* struct event_base */

#include "3rdparty/utlist.h" /* DL_FOREACH_SAFE() */
#include "proxy/client.h" /* decls for own functions */
#include "proxy/internal.h" /* internal decls */
#include "proxy/port.h" /* struct acopm_proxy_port, acopm_proxy_port_free() */
#include "utils/log.h" /* acopm_log_*() */

struct acopm_proxy_client *
acopm_proxy_new(struct event_base *const restrict ev_base)
{
	assert(ev_base != NULL);

	struct acopm_proxy_client *const client = calloc(1, sizeof *client);

	if (! client)
	{
		(void) acopm_log_error("%s: calloc: %s", __func__, strerror(errno));
		return NULL;
	}

	client->ev_base = ev_base;
	client->connect_timeout = ACOPM_PROXY_CONNECT_TIMEOUT_DEF;
	client->data_timeout = ACOPM_PROXY_DATA_TIMEOUT_DEF;

	const char *const defuser = ACOPM_PROXY_USER_DEF;

	if (! acopm_strset_buf(client->username, defuser))
		(void) acopm_log_warning("%s: default username '%s' too long, truncated (BUG)", __func__, defuser);

	client->username_length = strlen(client->username) + 1;

	(void) acopm_log_debug("%s: created client", __func__);
	return client;
}

void
acopm_proxy_set_recvbuf_sz(struct acopm_proxy_client *const restrict client, const size_t recvbuf_sz)
{
	assert(client != NULL);
	assert(recvbuf_sz >= ACOPM_PROXY_RECVBUF_SZ_MIN);
	assert(recvbuf_sz <= ACOPM_PROXY_RECVBUF_SZ_MAX);

	client->recvbuf_sz = recvbuf_sz;
}

void
acopm_proxy_set_sendbuf_sz(struct acopm_proxy_client *const restrict client, const size_t sendbuf_sz)
{
	assert(client != NULL);
	assert(sendbuf_sz >= ACOPM_PROXY_SENDBUF_SZ_MIN);
	assert(sendbuf_sz <= ACOPM_PROXY_SENDBUF_SZ_MAX);

	client->sendbuf_sz = sendbuf_sz;
}

void
acopm_proxy_set_connect_timeout(struct acopm_proxy_client *const restrict client, time_t timeout)
{
	assert(client != NULL);
	assert(timeout >= ACOPM_PROXY_CONNECT_TIMEOUT_MIN);
	assert(timeout <= ACOPM_PROXY_CONNECT_TIMEOUT_MAX);

	client->connect_timeout = timeout;
}

void
acopm_proxy_set_data_timeout(struct acopm_proxy_client *const restrict client, time_t timeout)
{
	assert(client != NULL);
	assert(timeout >= ACOPM_PROXY_DATA_TIMEOUT_MIN);
	assert(timeout <= ACOPM_PROXY_DATA_TIMEOUT_MAX);

	client->data_timeout = timeout;
}

void
acopm_proxy_set_username(struct acopm_proxy_client *const restrict client, const char *const restrict username)
{
	assert(client != NULL);
	assert(username != NULL);
	assert(*username != 0x00);

	if (! acopm_strset_buf(client->username, username))
		(void) acopm_log_warning("%s: username '%s' too long, truncated", __func__, username);

	client->username_length = strlen(client->username) + 1;
}

void
acopm_proxy_free(struct acopm_proxy_client *const restrict client)
{
	assert(client != NULL);

	(void) acopm_log_debug("%s: freeing client", __func__);

	if (client->ports)
	{
		struct acopm_proxy_port *port, *tmp;
		DL_FOREACH_SAFE(client->ports, port, tmp)
		{
			if (! client->ports)
				break;

			(void) acopm_proxy_port_free(port);
		}
	}

	(void) free(client);
}

const char *
acopm_proxy_type_str(const enum acopm_proxy_type type)
{
	switch (type)
	{
		case ACOPM_PROXY_TYPE_NONE:
			break;
		case ACOPM_PROXY_TYPE_PORT:
			return "(open port)";
		case ACOPM_PROXY_TYPE_SOCKS4:
			return "SOCKS4";
		case ACOPM_PROXY_TYPE_SOCKS5:
			return "SOCKS5";
		case ACOPM_PROXY_TYPE_HTTP_CONNECT:
			return "HTTP";

#ifdef BUILD_WITH_MBEDTLS
		case ACOPM_PROXY_TYPE_HTTPS_CONNECT:
			return "HTTPS";
#endif

	}

	abort();
	return NULL;
}