<commit_before>/*-
 * Copyright (c) 2008-2011 Varnish Software AS
 * All rights reserved.
 *
 * Author: <NAME> <<EMAIL>>
 *
 * 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.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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 "config.h"

#include <sys/types.h>
#include <sys/socket.h>

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "vtc.h"

#include "vsa.h"
#include "vss.h"
#include "vtcp.h"

struct client {
	unsigned		magic;
#define CLIENT_MAGIC		0x6242397c
	char			*name;
	struct vtclog		*vl;
	VTAILQ_ENTRY(client)	list;

	char			*spec;

	char			connect[256];

	char			*proxy_spec;
	int			proxy_version;

	unsigned		repeat;

	unsigned		running;
	pthread_t		tp;
};

static VTAILQ_HEAD(, client)	clients =
    VTAILQ_HEAD_INITIALIZER(clients);

/**********************************************************************
 * Send the proxy header
 */

static int __match_proto__(vss_resolved_f)
proxy_cb(void *priv, const struct suckaddr *sa)
{
	struct suckaddr **addr = priv;
	*addr = VSA_Clone(sa);
	return (1);
}

static void
client_proxy(struct vtclog *vl, int fd, int version, const char *spec)
{
	struct suckaddr *sac, *sas;
	const char *err;
	char *p, *p2;
	int error;

	p = strdup(spec);
	AN(p);
	p2 = strchr(p, ' ');
	AN(p2);
	*p2++ = '\0';

	error = VSS_resolver(p, NULL, proxy_cb, &sac, &err);
	if (err != NULL)
		vtc_log(vl, 0, "Could not resolve client address: %s", err);
	assert(error == 1);
	error = VSS_resolver(p2, NULL, proxy_cb, &sas, &err);
	if (err != NULL)
		vtc_log(vl, 0, "Could not resolve server address: %s", err);
	assert(error == 1);
	if (vtc_send_proxy(fd, version, sac, sas))
		vtc_log(vl, 0, "Write failed: %s", strerror(errno));
	free(p);
	free(sac);
	free(sas);
}

/**********************************************************************
 * Client thread
 */

static void *
client_thread(void *priv)
{
	struct client *c;
	struct vtclog *vl;
	int fd;
	unsigned u;
	struct vsb *vsb;
	char *p;
	char mabuf[32], mpbuf[32];
	const char *err;

	CAST_OBJ_NOTNULL(c, priv, CLIENT_MAGIC);
	AN(*c->connect);

	vl = vtc_logopen(c->name);

	p = strdup(c->connect);
	AN(p);
	vsb = macro_expand(vl, p);
	AN(vsb);

	if (c->repeat == 0)
		c->repeat = 1;
	if (c->repeat != 1)
		vtc_log(vl, 2, "Started (%u iterations)", c->repeat);
	for (u = 0; u < c->repeat; u++) {
		vtc_log(vl, 3, "Connect to %s", VSB_data(vsb));
		fd = VTCP_open(VSB_data(vsb), NULL, 10., &err);
		if (fd < 0)
			vtc_log(c->vl, 0, "Failed to open %s: %s",
			    VSB_data(vsb), err);
		assert(fd >= 0);
		VTCP_blocking(fd);
		VTCP_myname(fd, mabuf, sizeof mabuf, mpbuf, sizeof mpbuf);
		vtc_log(vl, 3, "connected fd %d from %s %s to %s",
		    fd, mabuf, mpbuf, VSB_data(vsb));<commit_msg>Fix unchecked return<commit_after>/*-
 * Copyright (c) 2008-2011 Varnish Software AS
 * All rights reserved.
 *
 * Author: <NAME> <<EMAIL>>
 *
 * 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.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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 "config.h"

#include <sys/types.h>
#include <sys/socket.h>

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "vtc.h"

#include "vsa.h"
#include "vss.h"
#include "vtcp.h"

struct client {
	unsigned		magic;
#define CLIENT_MAGIC		0x6242397c
	char			*name;
	struct vtclog		*vl;
	VTAILQ_ENTRY(client)	list;

	char			*spec;

	char			connect[256];

	char			*proxy_spec;
	int			proxy_version;

	unsigned		repeat;

	unsigned		running;
	pthread_t		tp;
};

static VTAILQ_HEAD(, client)	clients =
    VTAILQ_HEAD_INITIALIZER(clients);

/**********************************************************************
 * Send the proxy header
 */

static int __match_proto__(vss_resolved_f)
proxy_cb(void *priv, const struct suckaddr *sa)
{
	struct suckaddr **addr = priv;
	*addr = VSA_Clone(sa);
	return (1);
}

static void
client_proxy(struct vtclog *vl, int fd, int version, const char *spec)
{
	struct suckaddr *sac, *sas;
	const char *err;
	char *p, *p2;
	int error;

	p = strdup(spec);
	AN(p);
	p2 = strchr(p, ' ');
	AN(p2);
	*p2++ = '\0';

	error = VSS_resolver(p, NULL, proxy_cb, &sac, &err);
	if (err != NULL)
		vtc_log(vl, 0, "Could not resolve client address: %s", err);
	assert(error == 1);
	error = VSS_resolver(p2, NULL, proxy_cb, &sas, &err);
	if (err != NULL)
		vtc_log(vl, 0, "Could not resolve server address: %s", err);
	assert(error == 1);
	if (vtc_send_proxy(fd, version, sac, sas))
		vtc_log(vl, 0, "Write failed: %s", strerror(errno));
	free(p);
	free(sac);
	free(sas);
}

/**********************************************************************
 * Client thread
 */

static void *
client_thread(void *priv)
{
	struct client *c;
	struct vtclog *vl;
	int fd;
	unsigned u;
	struct vsb *vsb;
	char *p;
	char mabuf[32], mpbuf[32];
	const char *err;

	CAST_OBJ_NOTNULL(c, priv, CLIENT_MAGIC);
	AN(*c->connect);

	vl = vtc_logopen(c->name);

	p = strdup(c->connect);
	AN(p);
	vsb = macro_expand(vl, p);
	AN(vsb);

	if (c->repeat == 0)
		c->repeat = 1;
	if (c->repeat != 1)
		vtc_log(vl, 2, "Started (%u iterations)", c->repeat);
	for (u = 0; u < c->repeat; u++) {
		vtc_log(vl, 3, "Connect to %s", VSB_data(vsb));
		fd = VTCP_open(VSB_data(vsb), NULL, 10., &err);
		if (fd < 0)
			vtc_log(c->vl, 0, "Failed to open %s: %s",
			    VSB_data(vsb), err);
		assert(fd >= 0);
		/* VTCP_blocking does its own checks, trust it */
		(void)VTCP_blocking(fd);
		VTCP_myname(fd, mabuf, sizeof mabuf, mpbuf, sizeof mpbuf);
		vtc_log(vl, 3, "connected fd %d from %s %s to %s",
		    fd, mabuf, mpbuf, VSB_data(vsb));
{'fraction_non_alphanumeric': 0.16025885039969548, 'fraction_numerical': 0.012434970181449055, 'mean_word_length': 4.247669773635153, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1435345', 'n_tokens_mistral': 3040, 'n_tokens_neox': 2773, 'n_words': 936}