]> rtime.felk.cvut.cz Git - frescor/fwp.git/commitdiff
Endpoint and vres validity checking
authorMartin Molnar <molnar@sum.(none)>
Tue, 17 Jun 2008 14:39:17 +0000 (16:39 +0200)
committerMartin Molnar <molnar@sum.(none)>
Tue, 17 Jun 2008 14:39:17 +0000 (16:39 +0200)
fwp/lib/core/fwp_endpoint.c
fwp/lib/core/fwp_endpoint.h
fwp/lib/core/fwp_vres.c
fwp/lib/core/fwp_vres.h
fwp/tests/fwp_mngrtest/fwp_mngrtest.c

index 0ff0f406a1b8d1043d51b57b1d96432d53eb716c..ea787b92cc1f720c813533a15f558c43f058782d 100644 (file)
@@ -49,43 +49,59 @@ struct fwp_endpoint{
 
 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;
        }
@@ -95,13 +111,17 @@ static fwp_endpoint_t* fwp_endpoint_alloc()
        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, 
@@ -311,27 +331,33 @@ err:
 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;
 }
 
 /**
@@ -344,9 +370,16 @@ int fwp_send_endpoint_bind(fwp_endpoint_d_t epointd, fwp_vres_d_t vresd)
 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;
@@ -373,6 +406,9 @@ 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 (epoint->attr.reliability == FWP_EPOINT_BESTEFFORT) {        
                _fwp_recvfrom(len, epoint->sockd, buffer, buffer_size, 0, 
                        peer->addr, &peer->addrlen);
@@ -431,6 +467,8 @@ next_recv:
                        goto next_recv;
                }
        }
+
+       return -EPERM;
 }
 
 /**
@@ -450,8 +488,10 @@ int fwp_send(fwp_endpoint_d_t epointd, void *msg, size_t size, int flags)
        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;
        }
 
index 8d1016de9a0820ce1374d23b70ca968d85c0625e..551962aaa39d0ac263132d824e6ec09effd73e77 100644 (file)
@@ -38,7 +38,7 @@ int fwp_send_endpoint_create(unsigned int node, unsigned int port,
 int fwp_receive_endpoint_create(/*unsigned int node,*/ unsigned int port,
                                fwp_endpoint_attr_t *attr, 
                                fwp_endpoint_d_t *epointdp);
-void fwp_endpoint_destroy(fwp_endpoint_d_t epointd);
+int fwp_endpoint_destroy(fwp_endpoint_d_t epointd);
 
 int fwp_send_endpoint_bind(fwp_endpoint_d_t epointd, fwp_vres_d_t vresd);
 int fwp_send_endpoint_unbind(fwp_endpoint_d_t epointd);
index 93d12d150fd23b7d73258bf94b4065a273301dbc..0da3d4a23eb0008d86e2878906a9e1f7a6acff3c 100644 (file)
@@ -58,7 +58,7 @@ static inline int fwp_vres_set_ac(int sockd, fwp_ac_t ac_id)
                perror("Root permission needed to set AC");
                //return (-errno);
        }
-
+       
        return 0;
 }
 
@@ -105,7 +105,8 @@ static inline int fwp_vres_is_valid(fwp_vres_d_t vresd)
 {
        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; 
@@ -113,22 +114,12 @@ static inline int fwp_vres_is_valid(fwp_vres_d_t vresd)
 
 /*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)
 {
@@ -180,9 +171,9 @@ int fwp_vres_set_params(fwp_vres_d_t vresd, fwp_vres_params_t *params)
        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));
        
@@ -273,10 +264,7 @@ int fwp_vres_destroy(fwp_vres_d_t vresd)
        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);   
@@ -381,13 +369,20 @@ int _fwp_vres_bind(fwp_vres_d_t vresd, int sockd)
        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;
 }
@@ -396,6 +391,9 @@ int _fwp_vres_unbind(fwp_vres_d_t vresd)
 {
        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); */
index a5274e2da446ab6668d478a4d1da5d2eccec5d12..4763b9c499f662d5cc7592ab650d729a97d7b0a7 100644 (file)
@@ -41,7 +41,7 @@ struct fwp_vres_params {
 
 /* TODO: This functions must not be provided to user. Consider to move this functions
  *to separate include file*/
-int fwp_vres_table_init(unsigned int nr_vres);
+int fwp_vres_table_init(unsigned int max_vres);
 
 fwp_vres_d_t fwp_vres_alloc();
 fwp_vres_id_t fwp_vres_get_id(fwp_vres_d_t vresd);
index 5ec1cca1cf15cc9a60ff174387fd3366a3c4967b..c77811cafab43cbfbc1ee48b05044d2f30184179 100644 (file)
@@ -96,14 +96,16 @@ int main()
        }
        printf("Send endpoint 1 created\n");
        
-       fwp_send_endpoint_bind(sepoint_d1, vres_d1);
+       if ((fwp_send_endpoint_bind(sepoint_d1, vres_d1)) != 0){
+               printf("Bind error\n");
+               return -1;
+       }
 
        fwp_send(sepoint_d1, msg1, sizeof(msg1), 0);
        printf("Sent msg 1\n");
        fwp_send(sepoint_d1, msg2, sizeof(msg2), 0);
        printf("Sent msg 2\n");
 
-
        printf("Test PASSED!\n");
        
        scanf("Press key");