<commit_before>/**
 * @file io_src_msg_uad.c
 * @date 24 oct. 2012
 * @author <EMAIL>
 * @brief Source for reading / writing fixed length messages
 *
 * Copyright (C) 2012 Parrot S.A.
 */
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif /* _GNU_SOURCE */
#include <sys/socket.h>

#include <fcntl.h>
#include <unistd.h>

#include <errno.h>
#include <string.h>
#include <stdarg.h>
#include <stdio.h>

#include <rs_utils.h>

#include "io_platform.h"
#include "io_mon.h"
#include "io_src_msg_uad.h"
#include "io_utils.h"

/**
 * @def to_src_msg_uad
 * @brief Convert a source to it's message source container
 */
#define to_src_msg_uad(p) rs_container_of(p, struct io_src_msg_uad, src_msg)

/**
 * Performs input
 * @param uad Source
 * @return errno compatible value on error, 0 on success
 */
static int process_in_event(struct io_src_msg_uad *uad)
{
	ssize_t sret;

	sret = io_recvfrom(uad->src_msg.src.fd, uad->src_msg.rcv_buf,
			uad->src_msg.rcv_buf_size, 0, NULL, NULL);
	if (-1 == sret)
		return -errno;

	uad->cb(uad, IO_IN);

	return 0;
}

/**
 * Performs output
 * @param uad Source
 * @return errno compatible value on error, 0 on success
 */
static int process_out_event(struct io_src_msg_uad *uad)
{
	ssize_t sret;

	uad->cb(uad, IO_OUT);

	sret = io_sendto(uad->src_msg.src.fd, uad->src_msg.send_buf,
			uad->src_msg.rcv_buf_size, 0,
			(const struct sockaddr *)&(uad->addr),
			sizeof(uad->addr));
	if (-1 == sret)
		return -errno;

	return 0;
}

/**
 * Performs I/O, after arguments are already verified
 * @param uad Source
 * @param evt Either IO_IN or IO_OUT, not both
 * @return errno compatible value on error, 0 on success
 */
static int process_event(struct io_src_msg_uad *uad, enum io_src_event evt)
{
	/* here evt is either IO_IN or IO_OUT, not both */
	return IO_IN == evt ? process_in_event(uad) : process_out_event(uad);
}

/**
 * Called when the underlying io_src_msg has I/O ready. Performs I/O via
 * recvfrom / sendto
 * @param src Source
 * @param evt Either IO_IN or IO_OUT, not both
 */
static void uad_cb(struct io_src_msg *src, enum io_src_event evt)
{
	struct io_src_msg_uad *uad = to_src_msg_uad(src);

	if (IO_IN != evt && IO_OUT != evt)
		return;

	process_event(uad, evt);
}

static int uad_init_args_are_invalid(struct io_src_msg_uad *uad,
		io_src_msg_uad_cb_t *cb, void *rcv_buf, unsigned len,
		const char *fmt)<commit_msg>[COVERITY 10733 10734] false positive [MIXED_ENUMS]<commit_after>/**
 * @file io_src_msg_uad.c
 * @date 24 oct. 2012
 * @author <EMAIL>
 * @brief Source for reading / writing fixed length messages
 *
 * Copyright (C) 2012 <NAME>.
 */
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif /* _GNU_SOURCE */
#include <sys/socket.h>

#include <fcntl.h>
#include <unistd.h>

#include <errno.h>
#include <string.h>
#include <stdarg.h>
#include <stdio.h>

#include <rs_utils.h>

#include "io_platform.h"
#include "io_mon.h"
#include "io_src_msg_uad.h"
#include "io_utils.h"

/**
 * @def to_src_msg_uad
 * @brief Convert a source to it's message source container
 */
#define to_src_msg_uad(p) rs_container_of(p, struct io_src_msg_uad, src_msg)

/**
 * Performs input
 * @param uad Source
 * @return errno compatible value on error, 0 on success
 */
static int process_in_event(struct io_src_msg_uad *uad)
{
	ssize_t sret;

	sret = io_recvfrom(uad->src_msg.src.fd, uad->src_msg.rcv_buf,
			uad->src_msg.rcv_buf_size, 0, NULL, NULL);
	if (-1 == sret)
		return -errno;

	uad->cb(uad, IO_IN);

	return 0;
}

/**
 * Performs output
 * @param uad Source
 * @return errno compatible value on error, 0 on success
 */
static int process_out_event(struct io_src_msg_uad *uad)
{
	ssize_t sret;

	uad->cb(uad, IO_OUT);

	sret = io_sendto(uad->src_msg.src.fd, uad->src_msg.send_buf,
			uad->src_msg.rcv_buf_size, 0,
			(const struct sockaddr *)&(uad->addr),
			sizeof(uad->addr));
	if (-1 == sret)
		return -errno;

	return 0;
}

/**
 * Performs I/O, after arguments are already verified
 * @param uad Source
 * @param evt Either IO_IN or IO_OUT, not both
 * @return errno compatible value on error, 0 on success
 */
static int process_event(struct io_src_msg_uad *uad, enum io_src_event evt)
{
	/* here evt is either IO_IN or IO_OUT, not both */
	/* coverity[mixed_enums] */
	return IO_IN == evt ? process_in_event(uad) : process_out_event(uad);
}

/**
 * Called when the underlying io_src_msg has I/O ready. Performs I/O via
 * recvfrom / sendto
 * @param src Source
 * @param evt Either IO_IN or IO_OUT, not both
 */
static void uad_cb(struct io_src_msg *src, enum io_src_event evt)
{
	struct io_src_msg_uad *uad = to_src_msg_uad(src);

	/* coverity[mixed_enums] */
	if (IO_IN != evt && IO_OUT != evt)
		return;

	process_event(uad, evt);
}

static int uad_init_args_are_invalid(struct io_src_msg_uad *uad,
		io_src_msg_uad_cb_t *cb, void *rcv_buf, unsigned len,
		const char *fmt)
{'fraction_non_alphanumeric': 0.12712567399419328, 'fraction_numerical': 0.009954375777685608, 'mean_word_length': 4.21969696969697, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 42, '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': '3689990', 'n_tokens_mistral': 2030, 'n_tokens_neox': 1942, 'n_words': 511}