<commit_before>
/* Module:          socket.c
 *
 * Description:     This module contains functions for low level socket
 *                  operations (connecting/reading/writing to the backend)
 *
 * Classes:         SocketClass (Functions prefix: "SOCK_")
 *
 * API functions:   none
 *
 * Comments:        See "notice.txt" for copyright and license information.
 *
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "socket.h"

#ifndef WIN32
#ifndef VMS
#include "sys/un.h"
#endif
#include <stdlib.h>
#include <string.h>	/* for memset */
#endif

extern GLOBAL_VALUES globals;

#ifndef BOOL
#define BOOL	int
#endif
#ifndef TRUE
#define TRUE	(BOOL)1
#endif
#ifndef FALSE
#define FALSE	(BOOL)0
#endif


void
SOCK_clear_error(SocketClass *self)
{
	self->errornumber = 0; 
	self->errormsg = NULL; 
}

SocketClass * 
SOCK_Constructor()
{
SocketClass *rv;

    rv = (SocketClass *) malloc(sizeof(SocketClass));

    if (rv != NULL) {
		rv->socket = (SOCKETFD) -1;
		rv->buffer_filled_in = 0;
		rv->buffer_filled_out = 0;
		rv->buffer_read_in = 0;

		rv->buffer_in = (unsigned char *) malloc(globals.socket_buffersize);
		if ( ! rv->buffer_in)
			return NULL;

		rv->buffer_out = (unsigned char *) malloc(globals.socket_buffersize);
		if ( ! rv->buffer_out)
			return NULL;
		
        rv->errormsg = NULL;
        rv->errornumber = 0;

		rv->reverse = FALSE;
    } 
    return rv;

}

void
SOCK_Destructor(SocketClass *self)
{
	if (self->socket != -1) {
		SOCK_put_char(self, 'X');
		SOCK_flush_output(self);
		closesocket(self->socket);
	}

	if (self->buffer_in)
		free(self->buffer_in);

	if (self->buffer_out)
		free(self->buffer_out);

	free(self);

}

static char 
SOCK_connect_to_ip(SocketClass *self, unsigned short port, char *hostname)
{
struct hostent *host;
struct sockaddr_in sadr;
unsigned long iaddr;

	if (self->socket != -1) {
		self->errornumber = SOCKET_ALREADY_CONNECTED;
		self->errormsg = "Socket is already connected";
		return 0;
	}

	memset((char *)&sadr, 0, sizeof(sadr));

	/*	If it is a valid IP address, use it.
		Otherwise use hostname lookup. 
	*/
	iaddr = inet_addr(hostname);
	if (iaddr == INADDR_NONE) {
		host = gethostbyname(hostname);
		if (host == NULL) {
			self->errornumber = SOCKET_HOST_NOT_FOUND;
			self->errormsg = "Could not resolve hostname.";
			return 0;
		}
		memcpy(&(sadr.sin_addr), host->h_addr, host->h_length);
	}
	else<commit_msg>Postgres driver: Use struct in_addr<commit_after>
/* Module:          socket.c
 *
 * Description:     This module contains functions for low level socket
 *                  operations (connecting/reading/writing to the backend)
 *
 * Classes:         SocketClass (Functions prefix: "SOCK_")
 *
 * API functions:   none
 *
 * Comments:        See "notice.txt" for copyright and license information.
 *
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "socket.h"

#ifndef WIN32
#ifndef VMS
#include "sys/un.h"
#endif
#include <stdlib.h>
#include <string.h>	/* for memset */
#endif

extern GLOBAL_VALUES globals;

#ifndef BOOL
#define BOOL	int
#endif
#ifndef TRUE
#define TRUE	(BOOL)1
#endif
#ifndef FALSE
#define FALSE	(BOOL)0
#endif


void
SOCK_clear_error(SocketClass *self)
{
	self->errornumber = 0; 
	self->errormsg = NULL; 
}

SocketClass * 
SOCK_Constructor()
{
SocketClass *rv;

    rv = (SocketClass *) malloc(sizeof(SocketClass));

    if (rv != NULL) {
		rv->socket = (SOCKETFD) -1;
		rv->buffer_filled_in = 0;
		rv->buffer_filled_out = 0;
		rv->buffer_read_in = 0;

		rv->buffer_in = (unsigned char *) malloc(globals.socket_buffersize);
		if ( ! rv->buffer_in)
			return NULL;

		rv->buffer_out = (unsigned char *) malloc(globals.socket_buffersize);
		if ( ! rv->buffer_out)
			return NULL;
		
        rv->errormsg = NULL;
        rv->errornumber = 0;

		rv->reverse = FALSE;
    } 
    return rv;

}

void
SOCK_Destructor(SocketClass *self)
{
	if (self->socket != -1) {
		SOCK_put_char(self, 'X');
		SOCK_flush_output(self);
		closesocket(self->socket);
	}

	if (self->buffer_in)
		free(self->buffer_in);

	if (self->buffer_out)
		free(self->buffer_out);

	free(self);

}

static char 
SOCK_connect_to_ip(SocketClass *self, unsigned short port, char *hostname)
{
struct hostent *host;
struct sockaddr_in sadr;
struct in_addr iaddr;

	if (self->socket != -1) {
		self->errornumber = SOCKET_ALREADY_CONNECTED;
		self->errormsg = "Socket is already connected";
		return 0;
	}

	memset((char *)&sadr, 0, sizeof(sadr));

	/*	If it is a valid IP address, use it.
		Otherwise use hostname lookup. 
	*/
	iaddr.s_addr = inet_addr(hostname);
	if (iaddr.s_addr == INADDR_NONE) {
		host = gethostbyname(hostname);
		if (host == NULL) {
			self->errornumber = SOCKET_HOST_NOT_FOUND;
			self->errormsg = "Could not resolve hostname.";
			return 0;
		}
		memcpy(&(sadr.sin_addr), host->h_addr, host->h_length);
	}
	else
{'fraction_non_alphanumeric': 0.13461937357394732, 'fraction_numerical': 0.006222775357809583, 'mean_word_length': 3.5706161137440757, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 59, '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': '4981452', 'n_tokens_mistral': 1885, 'n_tokens_neox': 1756, 'n_words': 427}