<commit_before>/**
 * @file
 * @brief Implements socket interface function for kernel mode.
 *
 * @date 13.01.10
 * @author <NAME>
 * @author <NAME>
 */

#include <errno.h>
#include <net/socket.h>
#include <sys/socket.h>
#include <stddef.h>
#include <string.h>
#include <fcntl.h>
#include <net/netdevice.h>

#include <util/math.h>

#include <net/sock.h>
#include <util/sys_log.h>
#include <net/ksocket.h>
#include <net/socket_registry.h>
#include <embox/net/family.h>

#include <framework/mod/options.h>
#include <hal/ipl.h>
#include <mem/misc/pool.h>

#define MODOPS_AMOUNT_SOCKET OPTION_GET(NUMBER, amount_socket)

POOL_DEF(socket_pool, struct socket, MODOPS_AMOUNT_SOCKET);

static struct socket * socket_alloc(void) {
	ipl_t sp;
	struct socket *sock;

	sp = ipl_save();
	{
		sock = pool_alloc(&socket_pool);
	}
	ipl_restore(sp);

	return sock;
}

static void socket_free(struct socket *sock) {
	ipl_t sp;

	sp = ipl_save();
	{
		pool_free(&socket_pool, sock);
	}
	ipl_restore(sp);
}

static int ksocket_ext(int family, int type, int protocol,
		struct sock *sk, struct socket **out_sock) {
	int ret;
	struct socket *new_sock;

	assert(out_sock != NULL);

	new_sock = socket_alloc();
	if (new_sock == NULL) {
		return -ENOMEM;
	}

	if (sk == NULL) {
		ret = sock_create(family, type, protocol, &sk);
		if (ret != 0) {
			socket_free(new_sock);
			return ret;
		}
	}

	new_sock->sk = sk;
	new_sock->desc_data = NULL;
	new_sock->socket_node = NULL;

	ret = sr_add_socket_to_registry(new_sock);
	if (ret != 0) {
		sock_close(sk);
		return ret;
	}
	sk_set_connection_state(new_sock, UNCONNECTED);

	*out_sock = new_sock;

	return 0;
}

int ksocket(int family, int type, int protocol,
		struct socket **out_sock) {
	if (out_sock == NULL) {
		return -EINVAL;
	}<commit_msg>ksocket sk_socket initialization of sock<commit_after>/**
 * @file
 * @brief Implements socket interface function for kernel mode.
 *
 * @date 13.01.10
 * @author <NAME>
 * @author <NAME>
 */

#include <errno.h>
#include <net/socket.h>
#include <sys/socket.h>
#include <stddef.h>
#include <string.h>
#include <fcntl.h>
#include <net/netdevice.h>

#include <util/math.h>

#include <net/sock.h>
#include <util/sys_log.h>
#include <net/ksocket.h>
#include <net/socket_registry.h>
#include <embox/net/family.h>

#include <framework/mod/options.h>
#include <hal/ipl.h>
#include <mem/misc/pool.h>

#define MODOPS_AMOUNT_SOCKET OPTION_GET(NUMBER, amount_socket)

POOL_DEF(socket_pool, struct socket, MODOPS_AMOUNT_SOCKET);

static struct socket * socket_alloc(void) {
	ipl_t sp;
	struct socket *sock;

	sp = ipl_save();
	{
		sock = pool_alloc(&socket_pool);
	}
	ipl_restore(sp);

	return sock;
}

static void socket_free(struct socket *sock) {
	ipl_t sp;

	sp = ipl_save();
	{
		pool_free(&socket_pool, sock);
	}
	ipl_restore(sp);
}

static int ksocket_ext(int family, int type, int protocol,
		struct sock *sk, struct socket **out_sock) {
	int ret;
	struct socket *new_sock;

	assert(out_sock != NULL);

	new_sock = socket_alloc();
	if (new_sock == NULL) {
		return -ENOMEM;
	}

	if (sk == NULL) {
		ret = sock_create(family, type, protocol, &sk);
		if (ret != 0) {
			socket_free(new_sock);
			return ret;
		}
	}

	sk->sk_socket = new_sock;

	new_sock->sk = sk;
	new_sock->desc_data = NULL;
	new_sock->socket_node = NULL;

	ret = sr_add_socket_to_registry(new_sock);
	if (ret != 0) {
		socket_free(new_sock);
		sock_close(sk);
		return ret;
	}
	sk_set_connection_state(new_sock, UNCONNECTED);

	*out_sock = new_sock;

	return 0;
}

int ksocket(int family, int type, int protocol,
		struct socket **out_sock) {
	if (out_sock == NULL) {
		return -EINVAL;
	}
{'fraction_non_alphanumeric': 0.14668508287292817, 'fraction_numerical': 0.004972375690607734, 'mean_word_length': 4.34070796460177, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 46, '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': '6167502', 'n_tokens_mistral': 1516, 'n_tokens_neox': 1430, 'n_words': 329}