<commit_before>/* appserver.c */



#include <sys/socket.h>
#include <netinet/ip.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>

#include "array.h"
#include "string.h"
#include "appserver.h"


/* AppServerClient */
/* types */
typedef enum _AppServerClientState
{
	ASCS_NEW,
	ASCS_LOGGED
} AppServerClientState;

typedef struct _AppServerClient
{
	AppServerClientState state;
	int fd;
	uint32_t addr; /* FIXME uint8_t[4] instead? */
	uint16_t port;
} AppServerClient;


/* functions */
AppServerClient * appserverclient_new(int fd, uint32_t addr, uint16_t port)
{
	AppServerClient * asc;

	if((asc = malloc(sizeof(AppServerClient))) == NULL)
		return NULL;
	asc->state = ASCS_NEW;
	asc->fd = fd;
	asc->addr = addr;
	asc->port = port;
	return asc;
}

void appserverclient_delete(AppServerClient * appserverclient)
{
	if(close(appserverclient->fd) != 0)
		perror("close"); /* FIXME find a way to properly report error */
	free(appserverclient);
}


/* AppServer */
/* private */
/* types */
ARRAY(AppServerClient *, AppServerClient);
struct _AppServer
{
	Event * event;
	uint16_t port;
	AppServerClientArray * clients;
};


/* functions */
static int _appserver_accept(int fd, AppServer * appserver)
{
	struct sockaddr_in sa;
	int sa_size = sizeof(struct sockaddr_in);
	int newfd;
	AppServerClient * asc;

	/* FIXME append client to the clients list with the appropriate state */
	if((newfd = accept(fd, (struct sockaddr *)&sa, &sa_size)) == -1)
		return 1;
	if((asc = appserverclient_new(newfd, sa.sin_addr.s_addr, sa.sin_port))
			== NULL)
		return 1;
	array_append(appserver->clients, asc);
	return 0;
}


/* public */
/* functions */
/* appserver_new */
static int _new_interface(AppServer * appserver, const char * app);
static int _new_server(AppServer * appserver, int options);
AppServer * appserver_new(const char * app, int options)
{
	AppServer * appserver;
	Event * event;

	if((event = event_new()) == NULL)
		return NULL;
	if((appserver = appserver_new_event(app, options, event)) == NULL)
	{
		event_delete(event);
		return NULL;
	}
	return appserver;
}


/* appserver_new_event */
AppServer * appserver_new_event(const char * app, int options, Event * event)
{
	AppServer * appserver;

	if((appserver = malloc(sizeof(AppServer))) == NULL)
		return NULL;
	appserver->event = event;
	if(_new_interface(appserver, app) != 0
			|| _new_server(appserver, options) != 0)
	{
		free(appserver);<commit_msg>No longer segfaults on accept(), w00t<commit_after>/* appserver.c */



#include <sys/socket.h>
#include <netinet/ip.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>

#include "array.h"
#include "string.h"
#include "appserver.h"


/* AppServerClient */
/* types */
typedef enum _AppServerClientState
{
	ASCS_NEW,
	ASCS_LOGGED
} AppServerClientState;

typedef struct _AppServerClient
{
	AppServerClientState state;
	int fd;
	uint32_t addr; /* FIXME uint8_t[4] instead? */
	uint16_t port;
} AppServerClient;


/* functions */
AppServerClient * appserverclient_new(int fd, uint32_t addr, uint16_t port)
{
	AppServerClient * asc;

	if((asc = malloc(sizeof(AppServerClient))) == NULL)
		return NULL;
	asc->state = ASCS_NEW;
	asc->fd = fd;
	asc->addr = addr;
	asc->port = port;
	return asc;
}

void appserverclient_delete(AppServerClient * appserverclient)
{
	if(close(appserverclient->fd) != 0)
		perror("close"); /* FIXME find a way to properly report error */
	free(appserverclient);
}


/* AppServer */
/* private */
/* types */
ARRAY(AppServerClient *, AppServerClient);
struct _AppServer
{
	Event * event;
	uint16_t port;
	AppServerClientArray * clients;
};


/* functions */
static int _appserver_accept(int fd, AppServer * appserver)
{
	struct sockaddr_in sa;
	int sa_size = sizeof(struct sockaddr_in);
	int newfd;
	AppServerClient * asc;

#ifdef DEBUG
	fprintf(stderr, "%s%d%s%p%s", "_appserver_accept(", fd, ", ", appserver,
			")\n");
#endif
	/* FIXME append client to the clients list with the appropriate state */
	if((newfd = accept(fd, (struct sockaddr *)&sa, &sa_size)) == -1)
		return 1;
	if((asc = appserverclient_new(newfd, sa.sin_addr.s_addr, sa.sin_port))
			== NULL)
		return 1;
	array_append(appserver->clients, asc);
	return 0;
}


/* public */
/* functions */
/* appserver_new */
static int _new_interface(AppServer * appserver, const char * app);
static int _new_server(AppServer * appserver, int options);
AppServer * appserver_new(const char * app, int options)
{
	AppServer * appserver;
	Event * event;

	if((event = event_new()) == NULL)
		return NULL;
	if((appserver = appserver_new_event(app, options, event)) == NULL)
	{
		event_delete(event);
		return NULL;
	}
	return appserver;
}


/* appserver_new_event */
AppServer * appserver_new_event(const char * app, int options, Event * event)
{
	AppServer * appserver;

	if((appserver = malloc(sizeof(AppServer))) == NULL)
		return NULL;
	appserver->event = event;
	if((appserver->clients = AppServerClientarray_new()) == NULL
			|| _new_interface(appserver, app) != 0
			|| _new_server(appserver, options) != 0)
	{
		free(appserver);
{'fraction_non_alphanumeric': 0.13988508024569052, 'fraction_numerical': 0.007925500297206262, 'mean_word_length': 4.621380846325167, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 28, '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': '2920284', 'n_tokens_mistral': 1871, 'n_tokens_neox': 1785, 'n_words': 434}