<commit_before>#include <getopt.h>
#include <errno.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>

#include "common.h"
#include "crypto.h"
#include "log.h"

void usage_server(const char *name)
{
	printf("Usage: %s [options]\n", name);
	printf("Options:\n");
	printf("\t-l,--local local\n");
	printf("\t-b,--local-port local port\n");
	printf("\t-k,--password <PASSWORD>");
	printf("\t-m,--method encryption algorithm\n");
	printf("\t-d,--debug print debug information\n");
	printf("\t-v,--verbose print verbose information\n");
	printf("\t-h,--help print this help information\n");
}

/* read plain from remote, encrypt and send to local */
int server_do_remote_read(int sockfd, struct link *ln)
{
	if (do_plain_read(sockfd, ln) == -1)
		goto out;

	if (encrypt(ln) == -1)
		goto out;

	if (do_cipher_send(ln->local_sockfd, ln) == -1)
		goto out;

	sock_info(sockfd, "%s returned successfully", __func__);
	return 0;

out:
	sock_info(sockfd, "%s returned prematurely", __func__);
	return -1;
}

/* read encrypt from local, decrypt and send to remote */
int server_do_local_read(int sockfd, struct link *ln)
{
	struct addrinfo *r_info;

	if (do_cipher_read(sockfd, ln) == -1)
		goto out;

	if (decrypt(ln) == -1)
		goto out;

	if (!(ln->state & LINK_SERVER)) {
		r_info = get_addr(ln);

		if (connect_server(ln, r_info) == -1)
			goto out;
	}

	if (do_plain_send(ln->local_sockfd, ln) == -1)
		goto out;

	sock_info(sockfd, "%s returned successfully", __func__);
	return 0;

out:
	sock_info(sockfd, "%s returned prematurely", __func__);
	return -1;
}

int server_do_pollin(int sockfd, struct link *ln,
		     struct pollfd *clients, int nfds)
{
	if (sockfd == ln->local_sockfd) {
		if (!(ln->state & LINK_SERVER))
			sock_info(sockfd, "%s: connect() in progress",
				  __func__);

		if (ln->state & LINK_CIPHER_PENDING) {
			sock_info(sockfd, "%s: stop due to cipher send pending",
				  __func__);
			goto out;
		} else if (ln->state & LINK_PLAIN_PENDING) {
			sock_info(sockfd, "%s: stop due to plain send pending",
				  __func__);
			goto out;
		}

		if (server_do_local_read(sockfd, ln) == -1)
			goto clean;
		else
			goto out;
	} else {
		if (ln->state & LINK_PLAIN_PENDING) {
			sock_info(sockfd, "%s: stop due to plain send pending",
				  __func__);
			goto out;
		} else if (ln->state & LINK_CIPHER_PENDING) {
			sock_info(sockfd, "%s: stop due to cipher send pending",
				  __func__);
			goto out;
		}

		if (server_do_remote_read(sockfd, ln) == -1)
			goto clean;
		else
			goto out;
	}

out:
	sock_debug(sockfd, "%s succeeded", __func__);
	return 0;
clean:
	sock_info(sockfd, "%s: closed", __func__);
	destroy_link(ln);
	return -1;
}

int server_do_pollout(int sockfd, struct link *ln)
{
	int optval;
	int optlen = sizeof(optval);

	/* write to local */
	if (sockfd == ln->local_sockfd) {
		if (do_cipher_send(sockfd, ln) == -1)
			goto clean;
		else
			goto out;
	} else {
		/* pending connect finished */
		if (!(ln->state & LINK_SERVER)) {
			if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR,
				       &optval, &optlen) == -1) {
				perror("getsockopt");
				return -1;
			}

			if (optval == 0) {
				sock_info(sockfd,
					       "pending connect() finished");
				ln->state |= LINK_SERVER;
				goto out;
			} else {
				perror("pending connect() failed");
				goto clean;
			}

		}

		/* write to server */
		if (do_cipher_send(sockfd, ln) == -1)
			goto clean;
		else
			goto out;
	}

out:
	sock_debug(sockfd, "%s succeeded", __func__);
	return 0;
clean:
	sock_info(sockfd, "%s: closed", __func__);<commit_msg>server: passed echo server test<commit_after>#include <getopt.h>
#include <errno.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>

#include "common.h"
#include "crypto.h"
#include "log.h"

void usage_server(const char *name)
{
	printf("Usage: %s [options]\n", name);
	printf("Options:\n");
	printf("\t-l,--local local\n");
	printf("\t-b,--local-port local port\n");
	printf("\t-k,--password your password\n");
	printf("\t-m,--method encryption algorithm\n");
	printf("\t-d,--debug print debug information\n");
	printf("\t-v,--verbose print verbose information\n");
	printf("\t-h,--help print this help information\n");
}

/* read plain from remote, encrypt and send to local */
int server_do_remote_read(int sockfd, struct link *ln)
{
	if (do_plain_read(sockfd, ln) == -1)
		goto out;

	if (encrypt(ln) == -1)
		goto out;

	if (do_cipher_send(ln->local_sockfd, ln) == -1)
		goto out;

	sock_info(sockfd, "%s returned successfully", __func__);
	return 0;

out:
	sock_info(sockfd, "%s returned prematurely", __func__);
	return -1;
}

/* read encrypt from local, decrypt and send to remote */
int server_do_local_read(int sockfd, struct link *ln)
{
	if (do_cipher_read(sockfd, ln) == -1)
		goto out;

	if (decrypt(ln) == -1)
		goto out;

	if (!(ln->state & LINK_SERVER)) {
		/* xxx: test */
		/* r_info = get_addr(ln); */
		if (getaddrinfo("127.0.0.1", "7", NULL, &ln->server) != 0) {
			pr_warn("getaddrinfo failed: %s\n",
				gai_strerror(errno));
			goto out;
		}

		if (connect_server(ln, ln->server) == -1)
			goto out;
	}

	if (do_plain_send(ln->server_sockfd, ln) == -1)
		goto out;

	sock_info(sockfd, "%s returned successfully", __func__);
	return 0;

out:
	sock_info(sockfd, "%s returned prematurely", __func__);
	return -1;
}

int server_do_pollin(int sockfd, struct link *ln)
{
	if (sockfd == ln->local_sockfd) {
		if (!(ln->state & LINK_SERVER))
			sock_info(sockfd, "%s: connect() in progress",
				  __func__);

		if (ln->state & LINK_CIPHER_PENDING) {
			sock_info(sockfd, "%s: stop due to cipher send pending",
				  __func__);
			goto out;
		} else if (ln->state & LINK_PLAIN_PENDING) {
			sock_info(sockfd, "%s: stop due to plain send pending",
				  __func__);
			goto out;
		}

		if (server_do_local_read(sockfd, ln) == -1)
			goto clean;
		else
			goto out;
	} else {
		if (ln->state & LINK_PLAIN_PENDING) {
			sock_info(sockfd, "%s: stop due to plain send pending",
				  __func__);
			goto out;
		} else if (ln->state & LINK_CIPHER_PENDING) {
			sock_info(sockfd, "%s: stop due to cipher send pending",
				  __func__);
			goto out;
		}

		if (server_do_remote_read(sockfd, ln) == -1)
			goto clean;
		else
			goto out;
	}

out:
	sock_debug(sockfd, "%s succeeded", __func__);
	return 0;
clean:
	sock_info(sockfd, "%s: closed", __func__);
	destroy_link(ln);
	return -1;
}

int server_do_pollout(int sockfd, struct link *ln)
{
	int optval;
	int optlen = sizeof(optval);

	/* write to local */
	if (sockfd == ln->local_sockfd) {
		if (do_cipher_send(sockfd, ln) == -1)
			goto clean;
		else
			goto out;
	} else {
		/* pending connect finished */
		if (!(ln->state & LINK_SERVER)) {
			if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR,
				       &optval, &optlen) == -1) {
				perror("getsockopt");
				return -1;
			}

			if (optval == 0) {
				sock_info(sockfd,
					       "pending connect() finished");
				ln->state |= LINK_SERVER;
				goto out;
			} else {
				perror("pending connect() failed");
				goto clean;
			}

		}

		/* write to server */
		if (do_plain_send(sockfd, ln) == -1)
			goto clean;
		else
			goto out;
	}

out:
	sock_debug(sockfd, "%s succeeded", __func__);
	return 0;
clean:
	sock_info(sockfd, "%s: closed", __func__);
{'fraction_non_alphanumeric': 0.16290146978769734, 'fraction_numerical': 0.006804572672836146, 'mean_word_length': 3.500306184935701, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 46, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 1, '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': '4677846', 'n_tokens_mistral': 3160, 'n_tokens_neox': 2828, 'n_words': 732}