From: Sharan Santhanam Date: Tue, 12 Jun 2018 22:46:01 +0000 (+0200) Subject: Implement socket() for libvfscore X-Git-Tag: RELEASE-0.3~14 X-Git-Url: http://xenbits.xensource.com/gitweb?a=commitdiff_plain;h=3aceeb1b0f64da7bb20901ded486a93ecaf30c81;p=unikraft%2Flibs%2Flwip.git Implement socket() for libvfscore Signed-off-by: Sharan Santhanam --- diff --git a/socket_glue.c b/socket_glue.c index 21b7569..2148c39 100644 --- a/socket_glue.c +++ b/socket_glue.c @@ -9,6 +9,10 @@ #include #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)