#include "fwp_endpoint.h"
#include "fwp_msgb.h"
+#include <errno.h>
#include <pthread.h>
return (epointd->status == FWP_EPOINT_BOUND);
}
+/**
+ * Initializes endpoint table.
+ *
+ * @param max_endpoints For how many endpoints allocate the table.
+ *
+ * @return Zero on success, -1 on error (errno contains the code).
+ */
int fwp_endpoint_table_init(unsigned int max_endpoints)
{
int table_size = max_endpoints * sizeof(fwp_endpoint_t);
fwp_endpoint_table.entry = (fwp_endpoint_t*) malloc(table_size);
if (!fwp_endpoint_table.entry)
- return -ENOMEM;
+ return -1; /* errno is set to ENOMEM by malloc() */
memset(fwp_endpoint_table.entry, 0, table_size);
fwp_endpoint_table.max_endpoints = max_endpoints;
return 0;
if (i == max_endpoints) {
pthread_mutex_unlock(&fwp_endpoint_table.lock);
+ errno = ENOBUFS;
return NULL;
}
*
* \param[in] epointd Endpoint descriptor
* \return On success 0 is returned.
- * On error, negative error value is returned.
+ * On error, negative error value is returned and errno is set appropriately.
*/
int fwp_endpoint_destroy(fwp_endpoint_d_t epointd)
{
fwp_endpoint_t *epoint = epointd;
- if (!fwp_endpoint_is_valid(epointd))
- return -EINVAL;
+ if (!fwp_endpoint_is_valid(epointd)) {
+ errno = EINVAL;
+ return -1;
+ }
epoint->status = FWP_EPOINT_FREE;
if (epoint->sockd > 0)
* \param[in] attr Endpoint attributes
* \param[out] epointdp Pointer to the descriptor of newly created endpoint
*
- * \return On success returns descriptor of endpoint.
- * On error, negative error code is returned.
+ * \return Zero on success, -1 on error and sets errno appropriately.
*
*/
int fwp_send_endpoint_create(unsigned int node, unsigned int port,
epoint = fwp_endpoint_alloc();
if (!epoint) {
- return -ENOMEM;
+ errno = ENOMEM;
+ return -1;
}
epoint->type = FWP_SEND_EPOINT;
if (epoint->attr.reliability == FWP_EPOINT_RELIABLE) {
epoint->sockd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
+ if (epoint->sockd < 0) {
+ goto err;
+ }
} else {
epoint->sockd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
-
+ if (epoint->sockd < 0) {
+ goto err;
+ }
+
/* Enable broadcasts */
unsigned int yes = 1;
if (setsockopt(epoint->sockd,SOL_SOCKET, SO_BROADCAST,
&yes, sizeof(yes)) == -1) {
- perror("setsockopt(SO_BROADCAST)");
- return (-errno);
+ FWP_DEBUG("setsockopt(SO_BROADCAST): %s", strerror(errno));
+ goto err;;
}
}
- if (epoint->sockd < 0) {
- perror("Unable to open socket");
- goto err;
- }
-
if (connect(epoint->sockd,(struct sockaddr*) &epoint->peer.addr,
epoint->peer.addrlen)) {
- perror("Connect error");
goto err;
}
return 0;
err:
fwp_endpoint_destroy(epoint);
- return (-errno);
+ return -1;
}
/**
* \param[in] attr Endpoint attributes
* \param[out] epointdp Pointer to the descriptor of newly created endpoint
*
- * \return On success returns descriptor of endpoint.
- * On error, negative error code is returned.
+ * \return Zero on success, -1 on error and errno is set.
*/
int fwp_receive_endpoint_create(/*unsigned int node,*/ unsigned int port,
fwp_endpoint_attr_t *attr,
epoint = fwp_endpoint_alloc();
if (!epoint) {
- return -ENOMEM;
+ errno = ENOMEM;
+ return -1;
}
epoint->type = FWP_RECV_EPOINT;
if (epoint->attr.reliability == FWP_EPOINT_RELIABLE) {
if ((epoint->sockd = socket(PF_INET, SOCK_STREAM,
IPPROTO_TCP)) < 0) {
- perror("Unable to open socket");
+ FWP_ERROR("Unable to open socket: %s", strerror(errno));
goto err;
}
int yes = 1;
if (setsockopt(epoint->sockd,SOL_SOCKET, SO_REUSEADDR,
&yes, sizeof(yes)) == -1) {
- perror("setsockopt(SO_REUSEADDR)");
- return (-errno);
+ FWP_ERROR("setsockopt(SO_REUSEADDR): %s", strerror(errno));
+ goto err;
}
if (bind(epoint->sockd, (struct sockaddr*) &epoint->peer.addr,
epoint->peer.addrlen) == -1) {
- perror("Bind error");
+ FWP_ERROR("Bind error: %s", strerror(errno));
/* TODO: remove all error messages from all libraries */
goto err;
}
} else {
if ((epoint->sockd = socket(PF_INET, SOCK_DGRAM,
IPPROTO_UDP)) < 0) {
- perror("Unable to open socket");
+ FWP_ERROR("Unable to open socket: %s", strerror(errno));
goto err;
}
if (bind(epoint->sockd, (struct sockaddr*) &epoint->peer.addr,
epoint->peer.addrlen) == -1) {
- perror("Bind error");
+ FWP_ERROR("Bind error: %s", strerror(errno));
goto err;
}
}
/*if (setsockopt(epoint->sockd, SOL_SOCKET, SO_RCVBUF,
&rcvbuf_size, sizeof(rcvbuf_size)) == -1) {
- perror("Unable to set socket buffer size");
+ FWP_ERROR("Unable to set socket buffer size: %s", strerror(errno));
return -1;
}else {
FWP_DEBUG("Receive endpoint buffer size is set.\n");
*epointdp = epoint;
return 0;
-err:
+err:
fwp_endpoint_destroy(epoint);
- return (-errno);
+ return -1;
}
/**
* \param[in] vres_id identifier of vres
* \param[in] epoint_id send endpoint identifier
*
- * \return On success returns 0. On error, negative error code is returned
+ * \return On success returns 0. On error, -1 and errno is set appropriately.
*/
int fwp_send_endpoint_bind(fwp_endpoint_d_t epointd, fwp_vres_d_t vresd)
{
pthread_mutex_lock(&fwp_endpoint_table.lock);
if ((!fwp_endpoint_is_valid(epointd))||
(epoint->type != FWP_SEND_EPOINT)) {
- rv = -EINVAL;
+ errno = EINVAL;
goto err;
}
if (epoint->status == FWP_EPOINT_BOUND) { /* already bound*/
- rv = -EPERM;
+ errno = EPERM;
goto err;
}
- if ((rv = _fwp_vres_bind(vresd, epoint->sockd)) < 0) {
+ if ((rv = _fwp_vres_bind(vresd, epoint->sockd)) < 0) {
goto err;
}
* Unbinds send endpoint from vres
*
* \param[in] epointd Send endpoint descriptor
- * \return On success returns 0. On error, negative error code is returned
+ * \return On success returns 0. On error, -1 is returned and errno is set appropriately.
*
*/
int fwp_send_endpoint_unbind(fwp_endpoint_d_t epointd)
fwp_endpoint_t *epoint = epointd;
int rv = 0;
- if (!fwp_endpoint_is_valid(epointd))
- return -EINVAL;
- if (epoint->status != FWP_EPOINT_BOUND)
- return -EPERM;
+ if (!fwp_endpoint_is_valid(epointd)) {
+ errno = EINVAL;
+ return -1;
+ }
+ if (epoint->status != FWP_EPOINT_BOUND) {
+ errno = EPERM;
+ return -1;
+ }
/* unlink epoint-vres mutually */
if ((rv = _fwp_vres_unbind(epoint->vresd)) < 0)
*
* \return
* On success, it returns number of received bytes.
- * On error, negative error code is returned,
+ * On error, -1 is returned and errno is set appropriately.
*
*/
ssize_t fwp_recv(fwp_endpoint_d_t epointd, void *buffer, size_t buffer_size,
fd_set fdset;
int csockd, i;
- if (!fwp_endpoint_is_valid(epointd))
- return -EINVAL;
+ if (!fwp_endpoint_is_valid(epointd)) {
+ errno = EINVAL;
+ return -1;
+ }
if (epoint->attr.reliability == FWP_EPOINT_BESTEFFORT) {
- _fwp_recvfrom(len, epoint->sockd, buffer, buffer_size, 0,
- peer->addr, &peer->addrlen);
+ len = _fwp_recvfrom(epoint->sockd, buffer, buffer_size, 0, peer);
return len;
}
if (select(FD_SETSIZE, &fdset, (fd_set *)0,
(fd_set *)0, NULL) < 0) {
- perror("Error in select");
- return (-errno);
+ FWP_ERROR("Error in select: %s", strerror(errno));
+ return -1;
}
if (FD_ISSET(epoint->sockd, &fdset)) { /* is it listen socket? */
/* Check client TCP sockets */
for (i = 0; i < epoint->nr_connections; i++) {
if (FD_ISSET(epoint->c_sockd[i], &fdset)) {
- _fwp_recvfrom(len, epoint->c_sockd[i], buffer, buffer_size,
- 0, peer->addr, &peer->addrlen);
-
+ len = _fwp_recvfrom(epoint->c_sockd[i], buffer, buffer_size,
+ 0, peer);
+ if (len < 0) /* Error */
+ return len;
FWP_DEBUG("Received tcp data\n");
if (len)
return len;
*
* \return
* On success, it returns zero.
- * On error, negative error code is returned,
+ * On error, -1 is returned and errno is set appropriately.
*
*/
int fwp_send(fwp_endpoint_d_t epointd, void *msg, size_t size, int flags)
struct fwp_msgb *msgb;
if (!fwp_endpoint_is_valid(epointd)){
- return -EINVAL;
+ errno = EINVAL;
+ return -1;
}
if (!fwp_endpoint_is_bound(epointd)){
- return -EPERM;
+ errno = EPERM;
+ return -1;
}
/*if (flags && MSG_DONTWAIT)
msgb = fwp_msgb_alloc(buffer_size);
else {*/
- if (!(msgb = fwp_msgb_alloc(size)))
- return -ENOMEM;
+ if (!(msgb = fwp_msgb_alloc(size))) {
+ errno = ENOMEM;
+ return -1;
+ }
msgb->peer = &epoint->peer;
/*msgb->data = msg;*/
*
* \param msgb Pointer to msgb
* \param len The lenght data
- * \return Previous tail pointer
+ * \return Previous tail pointer on NULL in case of lack of space in the buffer.
*
*/
inline unsigned char* fwp_msgb_pull(struct fwp_msgb *msgb, unsigned int len)
#include "fwp_msgq.h"
+#include <errno.h>
/**
*Initialize message queue
* @param[in] msgq Message queue
*`@param[in] msgb Message buffer which stores a message
* @return
- * 0 it there are no errors
+ * Zero on success, -1 or error.
**/
int fwp_msgq_enqueue(struct fwp_msgq *msgq, struct fwp_msgb *msgb)
{
/*release queue mutex*/
pthread_mutex_unlock(&msgq->lock);
+ errno = ENOBUFS;
return -1;
/* }
* if (msgq->qr_policy == OLDEST)
static struct sched_param param;
if ((maxpri = sched_get_priority_max(SCHED_FIFO)) == -1) {
- perror("fwp_set_rt_prio - sched_get_priority_max call failed");
- return -errno;
+ FWP_ERROR("sched_get_priority_max call failed: %s", strerror(errno));
+ return -1;
}
if ((minpri = sched_get_priority_min(SCHED_FIFO)) == -1) {
- perror("fwp_set_rt_prio - sched_get_priority_min call failed");
- return -errno;
+ FWP_ERROR("sched_get_priority_min call failed: %s", strerror(errno));
+ return -1;
}
if (priority > maxpri) {
- fprintf(stderr,"fwp_set_rt_prio - priority parameter %d is"
- " greater than the maximal allowed priority %d.\n",
- priority, maxpri);
- return -EINVAL;
+ FWP_ERROR("parameter %d is greater than the maximal allowed priority %d.\n",
+ priority, maxpri);
+ errno = EINVAL;
+ return -1;
}
if (priority < minpri) {
- fprintf(stderr,"fwp_set_rt_prio - priority parameter %d is"
- " lower than the minimal allowed priority %d.\n",
- priority, minpri);
- return -EINVAL;
+ FWP_ERROR(stderr,"priority parameter %d is lower than the minimal allowed priority %d.\n",
+ priority, minpri);
+ errno = EINVAL;
+ return -1;
}
param.sched_priority = priority;
if (sched_setscheduler(0, SCHED_FIFO, ¶m) == -1) {
- perror("fwp_set_rt_prio - sched_setscheduler call failed");
- return -errno;
+ FWP_ERROR("sched_setscheduler call failed: %s", strerror(errno));
+ return -1;
}
return 0;
int sockfd;
if ((sockfd = socket(AF_UNIX, SOCK_DGRAM, 0)) == -1){
- perror("fwp_create_unix_socket - socket error");
- return (-errno);
+ FWP_ERROR("socket error: %s", strerror(errno));
+ return (-1);
}
bzero(addr, sizeof(addr));
if (bind(sockfd, (struct sockaddr*)addr,
sizeof(*addr)) == -1) {
- perror("fwp_open_unix_socket - bind error");
- return (-errno);
+ FWP_ERROR("fwp_open_unix_socket - bind error: %s", strerror(errno));
+ return (-1);
}
return sockfd;
int sockfd;
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1){
- perror("fwp_create_inet_socket - socket error");
- return (-errno);
+ FWP_ERROR("socket error: %s", strerror(errno));
+ return (-1);
}
addr->sin_family = AF_INET;
if (bind(sockfd, (struct sockaddr*)addr,
sizeof(*addr)) == -1) {
- perror("fwp_create_inet_socket - bind error");
- return (-errno);
+ FWP_ERROR("bind error: %s", strerror(errno));
+ return (-1);
}
return sockfd;
sigaddset(&sigset, SIGTERM);
ret = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
if (ret != 0) {
- perror("pthread_sigmask failed");
+ FWP_ERROR("pthread_sigmask failed: %s", strerror(errno));
exit(1);
}
}
tos = ac_to_tos[ac_id];
if (setsockopt(sockd, SOL_IP, IP_TOS, &tos, sizeof(tos)) == -1) {
- perror("Root permission needed to set AC");
- //return (-errno);
+ FWP_ERROR("setsockopt: %s", strerror(errno));
+ return (-1);
}
return 0;
int sockd;
unsigned int tos;
- if ((ac_id < 0)||(ac_id >= FWP_AC_NUM))
- return -EINVAL;
+ if ((ac_id < 0)||(ac_id >= FWP_AC_NUM)) {
+ errno = EINVAL;
+ return -1;
+ }
if ((sockd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
- perror("Unable to open socket for AC");
- return (-errno);
+ FWP_ERROR("Unable to open socket for AC: %s", strerror(errno));
+ return (-1);
}
tos = ac_to_tos[ac_id];
if (setsockopt(sockd, SOL_IP, IP_TOS, &tos, sizeof(tos)) == -1) {
- perror("Root permission needed to set AC");
- /* let pass
- * close(sockfd);
- *return (-errno);*/
+ int e = errno;
+ FWP_ERROR("setsockopt(IP_TOS): %s", strerror(errno));
+ close(sockfd);
+ errno = e;
+ return -1;
}
return sockd;
{
/*_fwp_sendto(ac_sockd, msgb->data, msgb->len, 0,
msgb->peer->addr, msgb->peer->addrlen);*/
- _fwp_send(ac_sockd, msgb->data, msgb->len, 0);
- return 0;
+ return _fwp_send(ac_sockd, msgb->data, msgb->len, 0);
}
static inline void fwp_vres_free(fwp_vres_t *vres)
unsigned int table_size = max_vres * sizeof(fwp_vres_t);
fwp_vres_table.entry = (fwp_vres_t*) malloc(table_size);
- if (!fwp_vres_table.entry)
- return -ENOMEM;
+ if (!fwp_vres_table.entry)
+ return -1; /* Errno is set by malloc */
memset((void*) fwp_vres_table.entry, 0, table_size);
fwp_vres_table.max_vres = max_vres;
if (i == max_vres) {
pthread_mutex_unlock(&fwp_vres_table.lock);
+ errno = ENOBUFS;
return NULL;
}
int rv;
if (!fwp_vres_is_valid(vresd)) {
- return -EINVAL;
+ errno = EINVAL;
+ return -1;
}
/* copy vres paramters into vres structure */
memcpy(&vres->params, params, sizeof(struct fwp_vres_params));
vres = fwp_vres_alloc();
if (!vres) {
- return -ENOMEM;
+ return -1;
}
/* copy vres paramters into vres structure */
memcpy(&vres->params, params, sizeof(struct fwp_vres_params));
return 0;
err:
fwp_vres_free(vres);
- return rv;
+ errno = rv;
+ return -1;
}
/**
{
fwp_vres_t *vres = vresd;
- if (!fwp_vres_is_valid(vresd))
- return -EINVAL;
+ if (!fwp_vres_is_valid(vresd)) {
+ errno = EINVAL;
+ return -1;
+ }
vres->status = FWP_VRES_FREE;
pthread_cancel(vres->tx_thread);
if (vres->status == FWP_VRES_BOUND) {
return fwp_msgq_enqueue(&vres->tx_queue, msgb);
- } else
- return -EPERM;
+ } else {
+ errno = EPERM; /* TODO: Use more appropriate error than EPERM. */
+ return -1;
+ }
}
/*int _fwp_vres_bind(fwp_vres_d_t vresd, fwp_endpoint_t *epoint)*/
pthread_mutex_lock(&fwp_vres_table.lock);
if (!fwp_vres_is_valid(vresd)) {
- rv = -EINVAL;
+ errno = EINVAL;
+ rv = -1;
goto err;
}
if (vres->status != FWP_VRES_UNBOUND) { /*if already bounded */
- rv = -EPERM;
+ errno = EPERM;
+ rv = -1;
goto err;
}
fwp_vres_t *vres = vresd;
if (!fwp_vres_is_valid(vresd)) {
- return -EINVAL;
+ errno = EINVAL;
+ return -1;
}
vres->status = FWP_VRES_UNBOUND;
/* TODO: consider what to do with pending messages */
*
* \return
* If successful, the function returns zero and vres descriptor is
- * stored in vresdp parameter, otherwise, an error number shall be
- * returned to indicate the error. If the contract is not negotiated EAGAIN
+ * stored in vresdp parameter, otherwise -1 is returned and errno is set appropriately. If the contract is not negotiated EAGAIN
* error is returned.
*/
/*int fwp_contract_negotiate(fwp_contract_d_t contractd, void *vresdp) */
int fwp_contract_negotiate(fwp_contract_d_t contractd, fwp_vres_d_t *vresdp)
{
- fwp_contract_reserve(contractd);
- if (!fwp_contract_is_reserved(contractd)) {
- return -EAGAIN;
+ int ret;
+ ret = fwp_contract_reserve(contractd);
+ if (ret < 0)
+ return ret;
+ if (!fwp_contract_is_reserved(contractd)) {
+ errno = EAGAIN;
+ return -1;
}
fwp_contract_commit(contractd, vresdp);
{
fwp_contract_data_t *contdata = contractd;
- if (contdata->status != FWP_CONT_NOTNEGOTIATED)
- return -EPERM;
+ if (contdata->status != FWP_CONT_NOTNEGOTIATED) {
+ errno = EINVAL;
+ return -1;
+ }
fwp_contract_data_delete(contdata);
return 0;
}
fwp_msgb_t *msgb;
fwp_msg_type_t msg_type;
fwp_participant_id_t participant_id;
+ int ret;
/*contdata = fwp_contract_table_find(&fwp_participant_this->contract_table,
contract_id);*/
fwp_msg_contract_in(msgb->tail, &contdata->contract);
fwp_msgb_put(msgb, sizeof(struct fwp_msg_contract));
- fwp_mngt_send(FWP_MSG_RESERVE, msgb,
- fwp_participant_this, fwp_participant_mngr);
- fwp_mngt_recv(&msg_type, &participant_id, msgb);
-
+ ret = fwp_mngt_send(FWP_MSG_RESERVE, msgb,
+ fwp_participant_this, fwp_participant_mngr);
+ if (ret < 0)
+ return ret;
+ ret = fwp_mngt_recv(&msg_type, &participant_id, msgb);
+ if (ret < 0)
+ return ret;
fwp_msg_contracthdr_out(msgb->data, &contdata->id, &contdata->status);
fwp_msgb_pull(msgb, sizeof(struct fwp_msg_contracthdr));
* \param[out] vresd Descriptor of vres
*
* \return On success, returns zero.
- * On error, returns negative error code.
+ * On error, returns -1 and sets errno appropriately.
*
*/
int fwp_contract_commit(fwp_contract_d_t contractd, fwp_vres_d_t *vresdp)
{
fwp_contract_data_t *contdata = contractd;
fwp_msgb_t *msgb;
+ int ret;
/* Send COMMIT to manager */
msgb = fwp_msgb_alloc(sizeof(struct fwp_msg_header) +
fwp_msgb_put(msgb, sizeof(struct fwp_msg_contracthdr));
FWP_DEBUG("Commit contract id =%d\n", contdata->id);
- fwp_mngt_send(FWP_MSG_COMMIT, msgb,
- fwp_participant_this, fwp_participant_mngr);
+ ret = fwp_mngt_send(FWP_MSG_COMMIT, msgb,
+ fwp_participant_this, fwp_participant_mngr);
+ if (ret < 0)
+ return ret;
contdata->status = FWP_CONT_NEGOTIATED;
/* Set parameters of vres
* and activate it if needed */
- fwp_vres_set_params(contdata->vresd, &contdata->vres_params);
+ ret = fwp_vres_set_params(contdata->vresd, &contdata->vres_params);
+ if (ret < 0)
+ return ret;
*vresdp = contdata->vresd;
- /*TODO: error handling */
return 0;
}
fwp_contract_data_t *contdata = contractd;
fwp_msgb_t *msgb;
- if (contdata->status != FWP_CONT_NEGOTIATED)
- return -EPERM;
+ if (contdata->status != FWP_CONT_NEGOTIATED) {
+ errno = EINVAL;
+ return -1;
+ }
/* Send CANCEL to manager */
msgb = fwp_msgb_alloc(sizeof(struct fwp_msg_header) +
int fwp_mngt_send(fwp_msg_type_t type,fwp_msgb_t *msgb,
fwp_participant_t *source, fwp_participant_t *dest)
{
+ int ret;
+
fwp_msgb_push(msgb, sizeof(struct fwp_msg_header));
fwp_msg_header_in(msgb->data, type, msgb->len, source->id);
- fwp_send(dest->epointd, msgb->data, msgb->len, 0);
+ ret = fwp_send(dest->epointd, msgb->data, msgb->len, 0);
- return 0;
+ return ret;
}
/**
fwp_msgb_reset_data(msgb);
size = fwp_recv(fwp_participant_this->epointd, msgb->data,
msgb->buffer_size, 0);
+ if (size < 0)
+ return size;
fwp_msgb_put(msgb, size);
fwp_msg_header_out(msgb->data, type, participant_id);