]> rtime.felk.cvut.cz Git - frescor/fwp.git/blobdiff - fwp/lib/core/fwp_endpoint.c
Fixing bugs related to tcp tranfer. Still not working
[frescor/fwp.git] / fwp / lib / core / fwp_endpoint.c
index bc9e7a74fdbe675cc0f5648f317152df89e18ea6..79c53f7a977267f65575b29373e89104d4c84cff 100644 (file)
@@ -50,6 +50,18 @@ static fwp_endpoint_t* fwp_endpoint_alloc()
        return (fwp_endpoint_t*) calloc(1,sizeof(fwp_endpoint_t));
 }
 
+/**
+ * Allocates endpoint
+ *
+ * \return On success returns endpoint structure. 
+ * On error, NULL is returned. 
+ *
+ */
+static inline void fwp_endpoint_free(fwp_endpoint_t *endpoint)
+{
+       free(endpoint);
+}
+
 /**
  * Destroy endpoint
  *
@@ -59,12 +71,10 @@ static fwp_endpoint_t* fwp_endpoint_alloc()
  */
 int fwp_endpoint_destroy(fwp_endpoint_d_t epointd)
 {
-       fwp_endpoint_t *epoint;
-
-/*     epoint = (fwp_endpoint_t*) endpoint->endpoint_protocol_info.send.body;
-       if (epoint->sockd > 0) 
-               close(epoint->sockd);*/
+       if (epointd->sockd > 0) 
+               close(epointd->sockd);
        
+       fwp_endpoint_free(epointd);     
        return 0;
 }
 
@@ -206,7 +216,7 @@ int fwp_receive_endpoint_create(unsigned int port,
        else
                fwp_epoint->attr = fwp_epoint_attr_default;
 
-       addr = (struct sockaddr_in *)&(fwp_epoint->peer.addr);
+       addr = (struct sockaddr_in *) &(fwp_epoint->peer.addr);
        addr->sin_family = AF_INET;
        /* TODO: set listen interface, maybe through config struct*/
        addr->sin_addr.s_addr = INADDR_ANY;
@@ -234,7 +244,10 @@ int fwp_receive_endpoint_create(unsigned int port,
                        goto err;
                }
                
-               listen(fwp_epoint->sockd, fwp_epoint->attr.max_connections); 
+               if (listen(fwp_epoint->sockd, fwp_epoint->attr.max_connections)){
+                       perror("Error on listen call\n");
+                       goto err;
+               }
                
                FD_ZERO(&fwp_epoint->fdset);
                /*add listen socket */
@@ -245,8 +258,8 @@ int fwp_receive_endpoint_create(unsigned int port,
                bzero(fwp_epoint->c_sockd, fwp_epoint->attr.max_connections);
                fwp_epoint->nr_connections = 0;
 
-               FWP_DEBUG("Receive endpoint\n");
-
+               FWP_DEBUG("Reliable receive endpoint port=%d created.\n", 
+                               fwp_epoint->port); 
        } else {
                if ((fwp_epoint->sockd = socket(PF_INET, SOCK_DGRAM, 
                                                IPPROTO_UDP)) < 0) {
@@ -261,6 +274,8 @@ int fwp_receive_endpoint_create(unsigned int port,
                        FWP_ERROR("Bind error: %s", strerror(errno));
                        goto err;
                }
+               FWP_DEBUG("Best-Effort receive endpoint port=%d created.\n", 
+                               fwp_epoint->port); 
        }
                
        /*if (setsockopt(epoint->sockd, SOL_SOCKET, SO_RCVBUF, 
@@ -276,12 +291,11 @@ int fwp_receive_endpoint_create(unsigned int port,
        getsockname(fwp_epoint->sockd, (struct sockaddr*)&fwp_epoint->peer.addr, 
                        &fwp_epoint->peer.addrlen);
        
-       FWP_DEBUG("Receive endpoint port=%d created.\n", fwp_epoint->port); 
        *epoint = fwp_epoint;   
        return 0;
 err:
        fwp_endpoint_destroy(fwp_epoint);
-       return -1;
+       return errno;
 }
 
 /**
@@ -292,7 +306,7 @@ err:
  *
  * \return On success returns 0. On error, -1 and errno is set appropriately.
  */
-int fwp_send_endpoint_bind(fwp_vres_d_t vresd, fwp_endpoint_t *epoint)
+int fwp_send_endpoint_bind(fwp_endpoint_t *epoint, fwp_vres_d_t vresd)
 {
        int rv ;
        fwp_endpoint_t *fwp_epoint = epoint;
@@ -314,10 +328,10 @@ int fwp_send_endpoint_bind(fwp_vres_d_t vresd, fwp_endpoint_t *epoint)
  * \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)
+int fwp_send_endpoint_unbind(fwp_endpoint_t *epoint)
 {
        int rv = 0;
-       fwp_endpoint_t *fwp_epoint = epointd;
+       fwp_endpoint_t *fwp_epoint = epoint;
 
        /* unlink epoint-vres mutually */
        if ((rv = fwp_vres_unbind(fwp_epoint->vresd)) < 0) 
@@ -326,19 +340,25 @@ int fwp_send_endpoint_unbind(fwp_endpoint_d_t epointd)
        return 0;
 }
 
-int fwp_receive_endpoint_accept(fwp_endpoint_d_t epointd)
+static int fwp_receive_endpoint_accept(fwp_endpoint_t *fwp_epoint)
 {
        int csockd;
-       fwp_endpoint_t *fwp_epoint = epointd;
+//     fwp_endpoint_t *fwp_epoint = epointd;
        fwp_sockaddr_t  peer;
        int i;
 
        if (fwp_epoint->nr_connections == fwp_epoint->attr.max_connections)
                return -1;
 
+       peer.addrlen = sizeof(struct sockaddr_in);
        csockd = accept(fwp_epoint->sockd, (struct sockaddr*)peer.addr,
                        &peer.addrlen);
-               
+       
+       if (csockd < 0) {
+               perror("Error on accept\n");
+               return errno;   
+       }               
+
        FWP_DEBUG("New connection accepted\n");
        /* find free place */           
        i = 0;
@@ -348,7 +368,9 @@ int fwp_receive_endpoint_accept(fwp_endpoint_d_t epointd)
        FWP_DEBUG("Index = %d\n", i);
        fwp_epoint->nr_connections++;
                
+       FWP_DEBUG("before\n");
        FD_SET(csockd, &fwp_epoint->fdset);
+       FWP_DEBUG("SET fdset\n");
        return 0;       
 } 
 
@@ -378,6 +400,7 @@ int fwp_recv_conn(fwp_endpoint_d_t epointd, void *buffer,
                        continue;       
                }       
                        
+               peer->addrlen = sizeof(struct sockaddr_in);
                len = _fwp_recvfrom(fwp_epoint->c_sockd[i], buffer, 
                                        buffer_size,0, peer);
 
@@ -443,7 +466,8 @@ ssize_t fwp_recv(fwp_endpoint_t *endpoint,
                }
        
                if (FD_ISSET(fwp_epoint->sockd, &fdset)) { /* is it listen socket? */
-                       fwp_receive_endpoint_accept(endpoint);
+                       fwp_receive_endpoint_accept(fwp_epoint);
+                       FWP_DEBUG("After accepted\n");
                        continue;
                }