<commit_before>#include <sstream>
#include <iostream>

#include <cerrno>
#include <cstring>
#include <cstdlib>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/tcp.h>
#include <netinet/udp.h>
#ifdef __linux__
#include <asm-generic/socket.h>
#endif
using namespace std;

#define I_ACKNOWLEDGE_THAT_NATUS_IS_NOT_STABLE
#include <natus/natus.hpp>
using namespace natus;

static Value socket_from_sock(Value& ctx, int sock, int domain, int type, int protocol);

static Value throwexc_eai(const Value& ctx, int error) {
	const char* type = "IOError";
	switch (error) {
	case EAI_SYSTEM:
		return throwException(ctx, errno);
	case EAI_MEMORY:
		return NULL;
	case EAI_ADDRFAMILY:
	case EAI_AGAIN:
	case EAI_BADFLAGS:
	case EAI_FAIL:
	case EAI_FAMILY:
	case EAI_NODATA:
	case EAI_NONAME:
	case EAI_SERVICE:
	case EAI_SOCKTYPE:
	default:
		break;
	}

	return throwException(ctx, type, gai_strerror(error), error);
}

class SocketClass : public Class {
	virtual Class::Flags getFlags() {
		return Class::FlagGet;
	}

	virtual Value get(Value& obj, string key) {
		sockaddr_storage addr;
		socklen_t len = sizeof(sockaddr_storage);
		char name[1024], port[21];
		int status = 0;

		int fd = obj.getPrivate<long>("posix.fd");

		if (key == "remoteAddress" || key == "remotePort")
			status = getpeername(fd, (sockaddr*) &addr, &len);
		else if (key == "localAddress" || key == "localPort")
			status = getsockname(fd, (sockaddr*) &addr, &len);
		else
			return obj.newUndefined().toException(); // Don't intercept on other properties
		if (status < 0)
			return throwException(obj, errno);

		status = getnameinfo((sockaddr*) &addr, len, name, 1024, port, 21, NI_NUMERICHOST | NI_NUMERICSERV);
		if (status < 0)
			return throwexc_eai(obj, status);

		if (key.find("Address") != string::npos)
			return obj.newString(name);
		return obj.newNumber(atoi(port));
	}
};

static Value socket_accept(Value& ths, Value& fnc, Value& arg) {
	int fd = ths.getPrivate<long>("posix.fd");

	int newsock = accept(fd, NULL, NULL);<commit_msg>update socket module to new api<commit_after>#include <sstream>
#include <iostream>

#include <cerrno>
#include <cstring>
#include <cstdlib>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/tcp.h>
#include <netinet/udp.h>
#ifdef __linux__
#include <asm-generic/socket.h>
#endif
using namespace std;

#define I_ACKNOWLEDGE_THAT_NATUS_IS_NOT_STABLE
#include <natus/natus.hpp>
using namespace natus;

static Value socket_from_sock(Value& ctx, int sock, int domain, int type, int protocol);

static Value throwexc_eai(const Value& ctx, int error) {
	const char* type = "IOError";
	switch (error) {
	case EAI_SYSTEM:
		return throwException(ctx, errno);
	case EAI_MEMORY:
		return NULL;
	case EAI_ADDRFAMILY:
	case EAI_AGAIN:
	case EAI_BADFLAGS:
	case EAI_FAIL:
	case EAI_FAMILY:
	case EAI_NODATA:
	case EAI_NONAME:
	case EAI_SERVICE:
	case EAI_SOCKTYPE:
	default:
		break;
	}

	return throwException(ctx, type, gai_strerror(error), error);
}

class SocketClass : public Class {
	virtual Class::Flags getFlags() {
		return Class::FlagGet;
	}

	virtual Value get(Value& obj, Value& key) {
		sockaddr_storage addr;
		socklen_t len = sizeof(sockaddr_storage);
		char name[1024], port[21];
		int status = 0;

		int fd = obj.getPrivate<long>("posix.fd");

		if (key.to<UTF8>() == "remoteAddress" || key.to<UTF8>() == "remotePort")
			status = getpeername(fd, (sockaddr*) &addr, &len);
		else if (key.to<UTF8>() == "localAddress" || key.to<UTF8>() == "localPort")
			status = getsockname(fd, (sockaddr*) &addr, &len);
		else
			return obj.newUndefined().toException(); // Don't intercept on other properties
		if (status < 0)
			return throwException(obj, errno);

		status = getnameinfo((sockaddr*) &addr, len, name, 1024, port, 21, NI_NUMERICHOST | NI_NUMERICSERV);
		if (status < 0)
			return throwexc_eai(obj, status);

		if (key.to<UTF8>().find("Address") != string::npos)
			return obj.newString(name);
		return obj.newNumber(atoi(port));
	}
};

static Value socket_accept(Value& ths, Value& fnc, Value& arg) {
	int fd = ths.getPrivate<long>("posix.fd");

	int newsock = accept(fd, NULL, NULL);
{'fraction_non_alphanumeric': 0.13988023952095807, 'fraction_numerical': 0.008383233532934131, 'mean_word_length': 5.043415340086831, 'pattern_counts': {'":': 0, '<': 40, '<?xml version=': 0, '>': 36, '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': '719341', 'n_tokens_mistral': 1659, 'n_tokens_neox': 1542, 'n_words': 398}