]> xenbits.xensource.com Git - unikraft/libs/lwip.git/commitdiff
Implement socket() for libvfscore
authorSharan Santhanam <sharan.santhanam@neclab.eu>
Tue, 12 Jun 2018 22:46:01 +0000 (00:46 +0200)
committerSimon Kuenzer <simon.kuenzer@neclab.eu>
Fri, 15 Jun 2018 01:21:07 +0000 (03:21 +0200)
Signed-off-by: Sharan Santhanam <sharan.santhanam@neclab.eu>
socket_glue.c

index 21b7569be6f5b440a11453a36379b2ce45ce86e2..2148c396d427b89d68ea4f4d5afcf4ff359c512b 100644 (file)
@@ -9,6 +9,10 @@
 #include <lwip/sockets.h>
 
 #define  NET_LIB_NAME          "lwip-socket"
+#define  SOCK_NET_SET_ERRNO(errcode) \
+                       do {\
+                               errno = -(errcode);\
+                       } while(0)
 
 
 struct sock_net_file {
@@ -16,6 +20,41 @@ struct sock_net_file {
        int sock_fd;
 };
 
+static int sock_fd_alloc(struct vfscore_fops *fops, int sock_fd)
+{
+       int ret = 0;
+       int vfs_fd;
+       struct sock_net_file *file = NULL;
+
+       /* Allocate file descriptor */
+       vfs_fd = vfscore_alloc_fd();
+       if (vfs_fd < 0) {
+               ret = -ENFILE;
+               uk_printd(DLVL_ERR, "failed to allocate socket fd\n");
+               goto EXIT;
+       }
+
+       file = uk_malloc(uk_alloc_get_default(), sizeof(*file));
+       if (!file) {
+               ret = -ENOMEM;
+               uk_printd(DLVL_ERR, "failed to allocate socket fd - no mem\n");
+               goto UK_MEM_ALLOC_ERR;
+       }
+       file->vfscore_file.fops = fops;
+       file->sock_fd = sock_fd;
+       uk_printd(DLVL_EXTRA, NET_LIB_NAME":allocated socket %d (%x)\n",
+                       file->vfscore_file.fd, file->sock_fd);
+       /* Storing the information within the vfs structure */
+       vfscore_install_fd(vfs_fd, &file->vfscore_file);
+       ret = vfs_fd;
+EXIT:
+       return ret;
+
+UK_MEM_ALLOC_ERR:
+       vfscore_put_fd(vfs_fd);
+       goto EXIT;
+}
+
 static int sock_net_close(struct vfscore_file *vfscore_file)
 {
        int    ret = 0;
@@ -45,7 +84,35 @@ static struct vfscore_fops sock_net_fops = {
 int socket(int domain, int type, int protocol)
 {
        int ret = 0;
-       return ret;
+       int vfs_fd = 0xff;
+       int sock_fd = 0;
+
+       /* Create lwip_socket */
+       sock_fd = lwip_socket(domain, type, protocol);
+       if(0 > sock_fd) {
+               uk_printd(DLVL_ERR, "failed to create socket %d\n", errno);
+               ret = -1;
+               goto EXIT;
+       }
+
+       /* Allocate the file descriptor */
+       vfs_fd = sock_fd_alloc(&sock_net_fops, sock_fd);
+       if(0 > vfs_fd) {
+               uk_printd(DLVL_ERR, "failed to allocate descriptor %d\n", errno);
+               ret = -1;
+               /* Setting the errno */
+               SOCK_NET_SET_ERRNO(vfs_fd);
+               goto LWIP_SOCKET_CLEANUP;
+       }
+
+       /* Returning the file descriptor to the user */
+       ret = vfs_fd;
+EXIT:
+       return ret;
+LWIP_SOCKET_CLEANUP:
+       /* Cleanup the lwip socket */
+       lwip_close(sock_fd);
+       goto EXIT;
 }
 
 int accept(int s, struct sockaddr *addr, socklen_t *addrlen)