]> rtime.felk.cvut.cz Git - frescor/fwp.git/commitdiff
fwp_endpoint modification
authorMartin Molnar <molnam1@fel.cvut.cz>
Tue, 29 Jan 2008 01:57:54 +0000 (02:57 +0100)
committerMartin Molnar <molnam1@fel.cvut.cz>
Tue, 29 Jan 2008 01:57:54 +0000 (02:57 +0100)
fwp/libfwp/include/Makefile.omk
fwp/libfwp/include/fwp_conf.h
fwp/libfwp/include/fwp_endpoint.h
fwp/libfwp/src/Makefile.omk
fwp/libfwp/src/fwp_endpoint.c
fwp/libfwp/src/fwp_util.c

index b155965c6e4344c81db50504b01c3583859f28c4..cc717df90edab15318b9ea9dbb9e118d234716c6 100644 (file)
@@ -1,3 +1,3 @@
 include_HEADERS=  fwp_ac.h  fwp_vres.h fwp_msgb.h fwp_msgq.h fwp_msg.h\
                  fwp_conf.h  fwp_util.h fwp_contract.h fwp_ctable.h\
-                 fwp_proto.h
+                 fwp_proto.h fwp_endpoint.h
index daca9aa608a1393eb918f62404fc8056b9211d80..f8bef8aceed5e5a48e726e33d319da2e26c2304e 100644 (file)
@@ -13,6 +13,9 @@
 /* Maximal number of fwp contract in contract table */
 #define FWP_CONTRACT_MAX 50
 
+/* Maximal number of fwp endpoint per application. */
+#define FWP_EPOINT_MAX 50
+
 /* Global variables */
 /*extern struct sockaddr_in fwp_ap;
 extern struct sockaddr_un fwp_client;
index 2e87597502d204e14695781d88fba3cbba710c1d..a2b9ad3636f517a2125cf32c230994d3594feef0 100644 (file)
@@ -1,12 +1,25 @@
 #ifndef _FWP_ENDPOINT_H
 #define _FWP_ENDPOINT_H
 
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+
+#include "fwp_conf.h"
+
+
+/**
+ * Structure of FWP endpoint.
+ *
+ * 
+ */
 struct fwp_endpoint{
-       node;
-       port;
+       int type;
+       int node;
+       int port;
+       struct sockaddr_in addr;
        int sockd;
-       struct sockaddr addr;
-
 }
 
 
index fe0e1ad376e6eec65995b9536a978b2a0ca9d69f..a8aecf98f09a8dfa6390730b7f2b035997ea02e8 100644 (file)
@@ -2,6 +2,6 @@ lib_LIBRARIES = fwp
 CFLAGS = -Wall -D_REENTRANT -g
 #LDFLAGS = -lpthread  -lrt
 fwp_SOURCES = fwp_util.c fwp_ac.c fwp_vres.c fwp_msgb.c fwp_msgq.c fwp_msg.c\
-             fwp_proto.c fwp_ctable.c
+             fwp_proto.c fwp_ctable.c fwp_endpoint.c
 
 #lib_LOADLIBES+= pthread rt
index fc93471a8cdecda3dbe2250cbcfed8d23dc783e9..f438737bb755da5348f6daf0d500ba1b72b1c6d8 100644 (file)
@@ -1,35 +1,84 @@
+#include "fwp_endpoint.h"
 
-struct fwp_endpoint* fwp_endpoint_create(int pf, struct sockaddr *_addr, socklen_t _addrlen)
-{
-       struct fwp_socket* fwpsock;
-       unsigned char *addr;
 
-       
-       
-       
-       fwpsock = (struct fwp_socket*) malloc(sizeof(struct fwp_socket) + 
-                                             _addrlen);
-       if (!fwpsock)
-               return NULL;
-       
-       addr = (unsigned char*) fwpsock + sizeof(struct fwp_socket);
-       memcpy(addr, (void*) _addr, _addrlen);
+enum fwp_endpoint_type_t {
+       FWP_SEND_EPOIN = 0,
+       FWP_RECV_EPOINT = 1,
+};
 
-       fwpsock->addr = (struct sockaddr*) addr;
-       fwpsock->addrlen = _addrlen;
+enum fwp_endpoint_status_t {
+       FWP_EPOINT_CLOSED = 0,
+       FWP_EPOINT_OPENED = 1,
+};
 
-       return fwpsock;
-}
+static struct fwp_endpoint fwp_endpoint_table[FWP_EPOINT_MAX];
+static pthread_mutex_t fwp_endpoint_table_lock = PTHREAD_MUTEX_INITIALIZER; 
 
-inline void fwp_socket_set(struct fwp_socket *fwpsock, struct sockaddr *_addr, 
-                                 socklen_t _addrlen)
+inline int fwp_endpoint_is_valid(int id)
 {      
-       fwpsock->addr = _addr;
-       fwpsock->addrlen = _addrlen;
+       if ((id < 0) || (id >= FWP_EPOINT_MAX))
+               return -EINVAL;
+       if ((fwp_endpoint_table[id].status != FWP_EPOINT_OPENED))
+               return -EPERM;
+       return 0;
+}
+
+void fwp_endpoint_table_init()
+{
+       int id;
+
+       for (id = 0; id < FWP_EPOINT_MAX; id++) 
+               fwp_endpoint_table[id].status = FWP_EPOINT_CLOSED;
 }
 
-void fwp_socket_free(struct fwp_socket *fwpsock)
+struct fwp_endpoint* fwp_endpoint_create(int type, int node, int port)
+/* queueing policy should be the next parameter*/
 {
-       free((void*)fwpsock);
-       fwpsock = NULL;
+       int id,rc;
+       struct fwp_endpoint *epoint;
+       
+       /* Check for validity of the contract */
+
+       /* obtain fwp_vres_table mutex */
+       pthread_mutex_lock(&fwp_epoint_table_lock);
+
+       /* find free vres id */
+       id = 0;
+       while ((id < FWP_EPOINT_MAX) && 
+               (fwp_endpoint_table[id].status != FWP_EPOINT_CLOSED)){ 
+               
+               id++;
+       }
+       if (id == FWP_EPOINT_MAX) {
+               /* release fwp_vres_table mutex */
+               return -ENOMEM;
+       }
+       
+       epoint = &fwp_endpoint_table[id];
+       epoint->status = FWP_EPOINT_OPENED;
+       /* release fwp_vres_table mutex */
+       pthread_mutex_unlock(&fwp_endpoint_table_lock);
+
+       epoint->addr.sin_family = AF_INET;
+       epoint->addr.sin_port = htons(port);
+        if (type == FWP_SEND_EPOINT) {
+               epoint->addr.sin_addr.s_addr = node;
+               return epoint; 
+       }
+
+       epoint->addr.sin_addr.s_addr = INADDR_ANY;
+
+       if ((epoint->sockd = socket(AF_INET, SOCK_DGRAM, 0)) == -1){
+               perror("fwp_endpoint_create - socket error");
+               return (-errno);
+       }
+               
+       if (bind(epoint->sockd, (struct sockaddr*) &epoint->addr, 
+                sizeof(epoint->addr)) == -1) {
+                       
+               perror("fwp_endpoint_create - bind error");
+               return (-errno);
+       }
+    
+
 }
index 49fe29d196fc96e42113f6707d445ecc30ec4081..73cf45a6cd7b0c41f178d73b5fb7b8885dea7a3e 100644 (file)
@@ -78,7 +78,7 @@ int fwp_create_unix_socket(char *path, struct sockaddr_un *addr)
        int sockfd;
 
        if ((sockfd = socket(AF_UNIX, SOCK_DGRAM, 0)) == -1){
-               perror("fwp_init - socket error");
+               perror("fwp_create_unix_socket - socket error");
                return (-errno);
        }
        
@@ -102,7 +102,7 @@ int fwp_create_inet_socket(unsigned int port, struct sockaddr_in *addr)
        int sockfd;
 
        if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1){
-               perror("fwp_open_inet_socket - socket error");
+               perror("fwp_create_inet_socket - socket error");
                return (-errno);
        }
        
@@ -113,7 +113,7 @@ int fwp_create_inet_socket(unsigned int port, struct sockaddr_in *addr)
        if (bind(sockfd, (struct sockaddr*)addr, 
                 sizeof(*addr)) == -1) {
                        
-               perror("fwp_init - bind error");
+               perror("fwp_create_inet_socket - bind error");
                return (-errno);
        }