typedef
struct fwp_endpoint_table {
- unsigned int nr_endpoints;
+ unsigned int max_endpoints;
fwp_endpoint_t *entry;
pthread_mutex_t lock;
} fwp_endpoint_table_t;
/* Global variable - endpoint table */
static fwp_endpoint_table_t fwp_endpoint_table = {
- .nr_endpoints = 0,
+ .max_endpoints = 0,
.entry = NULL,
.lock = PTHREAD_MUTEX_INITIALIZER,
};
-int fwp_endpoint_table_init(unsigned int nr_endpoints)
+static inline int fwp_endpoint_is_valid(fwp_endpoint_d_t epointd)
{
- int table_size = nr_endpoints * sizeof(fwp_endpoint_t);
+ int id = epointd - fwp_endpoint_table.entry;
+
+ if ((id < 0) || (id > fwp_endpoint_table.max_endpoints - 1)||
+ (epointd->status == FWP_EPOINT_FREE))
+ return 0;
+
+ return 1;
+}
+
+int fwp_endpoint_is_bound(fwp_endpoint_d_t epointd)
+{
+ return (epointd->status == FWP_EPOINT_BOUND);
+}
+
+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;
memset(fwp_endpoint_table.entry, 0, table_size);
- fwp_endpoint_table.nr_endpoints = nr_endpoints;
+ fwp_endpoint_table.max_endpoints = max_endpoints;
return 0;
}
static fwp_endpoint_t* fwp_endpoint_alloc()
{
- int i, nr_endpoints;
+ int i, max_endpoints;
/* find free vres id */
pthread_mutex_lock(&fwp_endpoint_table.lock);
i = 0;
- nr_endpoints = fwp_endpoint_table.nr_endpoints;
- while ((i < nr_endpoints) &&
+ max_endpoints = fwp_endpoint_table.max_endpoints;
+ while ((i < max_endpoints) &&
(fwp_endpoint_table.entry[i].status != FWP_EPOINT_FREE))
i++;
- if (i == nr_endpoints) {
+ if (i == max_endpoints) {
pthread_mutex_unlock(&fwp_endpoint_table.lock);
return NULL;
}
return (&fwp_endpoint_table.entry[i]);
}
-void fwp_endpoint_destroy(fwp_endpoint_d_t epointd)
+int fwp_endpoint_destroy(fwp_endpoint_d_t epointd)
{
fwp_endpoint_t *epoint = epointd;
+ if (!fwp_endpoint_is_valid(epointd))
+ return -EINVAL;
+
epoint->status = FWP_EPOINT_FREE;
if (epoint->sockd > 0)
close(epoint->sockd);
+ return 0;
}
int fwp_endpoint_get_params(fwp_endpoint_d_t epointd, unsigned int *node,
int fwp_send_endpoint_bind(fwp_endpoint_d_t epointd, fwp_vres_d_t vresd)
{
fwp_endpoint_t *epoint = epointd;
-
- if (epoint->type != FWP_SEND_EPOINT) {
- return (-EINVAL);
- }
-
+ int rv = 0;
+
/* link epoint-vres mutually */
pthread_mutex_lock(&fwp_endpoint_table.lock);
- if (_fwp_vres_bind(vresd, epoint->sockd) < 0) {
- pthread_mutex_unlock(&fwp_endpoint_table.lock);
- return -EPERM;
+ if ((!fwp_endpoint_is_valid(epointd))||
+ (epoint->type != FWP_SEND_EPOINT)) {
+ rv = -EINVAL;
+ goto err;
}
-
- if (epoint->type == FWP_EPOINT_BOUND) { /* if send endpoint is already bound */
- fwp_send_endpoint_unbind(epoint);
+ if (epoint->status == FWP_EPOINT_BOUND) { /* already bound*/
+ rv = -EPERM;
+ goto err;
+ }
+ if ((rv = _fwp_vres_bind(vresd, epoint->sockd)) < 0) {
+ goto err;
}
+ /* if send endpoint is already bound
+ if (epoint->type == FWP_EPOINT_BOUND) {
+ fwp_send_endpoint_unbind(epoint);
+ }*/
epoint->vresd = vresd;
epoint->status = FWP_EPOINT_BOUND;
+err:
pthread_mutex_unlock(&fwp_endpoint_table.lock);
- return 0;
+ return rv;
}
/**
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;
+
/* unlink epoint-vres mutually */
- _fwp_vres_unbind(epoint->vresd);
+ if ((rv = _fwp_vres_unbind(epoint->vresd)) < 0)
+ return rv;
epoint->status = FWP_EPOINT_UNBOUND;
return 0;
fd_set fdset;
int csockd, i;
+ if (!fwp_endpoint_is_valid(epointd))
+ return -EINVAL;
+
if (epoint->attr.reliability == FWP_EPOINT_BESTEFFORT) {
_fwp_recvfrom(len, epoint->sockd, buffer, buffer_size, 0,
peer->addr, &peer->addrlen);
goto next_recv;
}
}
+
+ return -EPERM;
}
/**
struct fwp_endpoint *epoint = epointd;
struct fwp_msgb *msgb;
- /* TODO: Validity test of epointd */
- if (epoint->status != FWP_EPOINT_BOUND) {
+ if (!fwp_endpoint_is_valid(epointd)){
+ return -EINVAL;
+ }
+ if (!fwp_endpoint_is_bound(epointd)){
return -EPERM;
}
perror("Root permission needed to set AC");
//return (-errno);
}
-
+
return 0;
}
{
int id = vresd - fwp_vres_table.entry;
- if ((id < 0) || (id > fwp_vres_table.max_vres - 1))
+ if ((id < 0) || (id > fwp_vres_table.max_vres - 1) ||
+ (vresd->status == FWP_VRES_FREE))
return 0;
return 1;
/*inline int fwp_vres_get(fwp_vres_id_t vres_id, fwp_vres_t **vres )
{
- if ((vres_id < 0) || (vres_id > fwp_vres_table.nr_vres - 1))
+ 3if ((vres_id < 0) || (vres_id > fwp_vres_table.nr_vres - 1))
return -EINVAL;
*vres = &fwp_vres_table.entry[vres_id];
return 0;
}
-
-inline int fwp_vres_getid(fwp_vres_t *vres, fwp_vres_id_t *vres_id)
-{
- fwp_vres_id_t id;
-
- id = vres - fwp_vres_table.entry;
- if ((id < 0) || (id > fwp_vres_table.nr_vres - 1))
- return -EINVAL;
- *vres_id = id;
- return 0;
-}*/
+*/
int fwp_vres_table_init(unsigned int max_vres)
{
fwp_vres_t *vres = vresd;
int rv;
- if (!fwp_vres_is_valid(vresd))
+ if (!fwp_vres_is_valid(vresd)) {
return -EINVAL;
-
+ }
/* copy vres paramters into vres structure */
memcpy(&vres->params, params, sizeof(struct fwp_vres_params));
if (!fwp_vres_is_valid(vresd))
return -EINVAL;
- if (vres->status == FWP_VRES_FREE)
- return -EPERM;
-
- vres->status = FWP_VRES_INACTIVE;
+ vres->status = FWP_VRES_FREE;
pthread_cancel(vres->tx_thread);
FWP_DEBUG("Vres vparam_id=%d destroyed.\n", vres->params.id);
int rv = 0;
pthread_mutex_lock(&fwp_vres_table.lock);
- if (vres->status == FWP_VRES_BOUND) /*if other endpoint is assigned to vres*/
+ if (!fwp_vres_is_valid(vresd)) {
+ rv = -EINVAL;
+ goto err;
+ }
+
+ if (vres->status != FWP_VRES_UNBOUND) { /*if already bounded */
rv = -EPERM;
- else {
- vres->ac_sockd = sockd;
- fwp_vres_set_ac(vres->ac_sockd,vres->params.ac_id);
- vres->status = FWP_VRES_BOUND;
+ goto err;
}
+
+ vres->ac_sockd = sockd;
+ fwp_vres_set_ac(vres->ac_sockd,vres->params.ac_id);
+ vres->status = FWP_VRES_BOUND;
+err:
pthread_mutex_unlock(&fwp_vres_table.lock);
return rv;
}
{
fwp_vres_t *vres = vresd;
+ if (!fwp_vres_is_valid(vresd)) {
+ return -EINVAL;
+ }
vres->status = FWP_VRES_UNBOUND;
/* TODO: consider what to do with pending messages */
/* fwp_vres_free_msgb(vres->tx_queue); */