X-Git-Url: http://p2p-next.cs.pub.ro/gitweb/?a=blobdiff_plain;f=src%2Fraw%2Fswift_raw.c;h=f4850f19140301d71c9513995795cb99ed7d8b71;hb=2092aaa0762a3bc3feb98a1d5571f210e9eabefa;hp=9761c1f13269d6cc22902ffe75a63e23b85189d8;hpb=aaa8dd0eff439c132771502cbe71e49770caed2c;p=swifty.git diff --git a/src/raw/swift_raw.c b/src/raw/swift_raw.c index 9761c1f..f4850f1 100644 --- a/src/raw/swift_raw.c +++ b/src/raw/swift_raw.c @@ -1,5 +1,5 @@ /* - * swift implementation of syscall API> + * swift implementation of syscall API * * Simulates the classic socket syscalls (socket, bind, send, recv). * Implementation uses raw sockets (AF_INET, SOCK_RAW). @@ -24,105 +24,7 @@ #include "swift_types.h" #include "swift_raw.h" - -/* socket management structure */ -struct sock_list { - int s; - struct sockaddr_sw addr; - struct sock_list *next; - struct sock_list *prev; -}; - -static struct sock_list sock_list_head = { - .next = &sock_list_head, - .prev = &sock_list_head -}; - -/* - * Add new socket to list. Called by sw_socket "syscall". - */ - -static struct sock_list *list_add_socket(int s) -{ - struct sock_list *ptr = malloc(sizeof(*ptr)); - if (ptr == NULL) - return NULL; - - ptr->next = &sock_list_head; - ptr->prev = sock_list_head.prev; - sock_list_head.prev->next = ptr; - sock_list_head.prev = ptr; - - return ptr; -} - -/* - * Bind socket to given address. Called by sw_bind "syscall". - */ - -static struct sock_list *list_update_socket_address(int s, struct sockaddr_sw *addr) -{ - struct sock_list *ptr; - - for (ptr = sock_list_head.next; ptr != &sock_list_head; ptr = ptr->next) - if (ptr->s == s) { - memcpy(&ptr->addr, addr, sizeof(ptr->addr)); - return ptr; - } - - return NULL; -} - -/* - * Get list element containing socket s. Called by sw_send* "syscalls". - */ - -static struct sock_list *list_elem_from_socket(int s) -{ - struct sock_list *ptr; - - for (ptr = sock_list_head.next; ptr != &sock_list_head; ptr = ptr->next) - if (ptr->s == s) { - return ptr; - } - - return NULL; -} - -/* - * Get list element containing address addr. Called by sw_bind "syscall". - */ - -static struct sock_list *list_elem_from_address(const struct sockaddr_sw *addr) -{ - struct sock_list *ptr; - - for (ptr = sock_list_head.next; ptr != &sock_list_head; ptr = ptr->next) - if (memcmp(&ptr->addr, addr, sizeof(addr)) == 0) - return ptr; - - return NULL; -} - -/* - * Remove socket from list. Called by sw_close "syscall". - */ - -static struct sock_list *list_unlink_socket(int s) -{ - struct sock_list *ptr; - - for (ptr = sock_list_head.next; ptr != &sock_list_head; ptr = ptr->next) - if (ptr->s == s) { - ptr->next->prev = ptr->prev; - ptr->prev->next = ptr->next; - ptr->next = ptr; - ptr->prev = ptr; - return ptr; - } - - return NULL; -} +#include "swift_list.h" /* * Create a new socket of type TYPE in domain DOMAIN, using @@ -131,18 +33,29 @@ static struct sock_list *list_unlink_socket(int s) * * swift: PROTOCOL is IPPROTO_SWIFT. Ignore TYPE. */ -int sw_socket (int __domain, int __type, int __protocol) +int sw_socket(int __domain, int __type, int __protocol) { int s; struct sock_list *list; - s = socket(__domain, SOCK_RAW, IPPROTO_SWIFT); - if (s < 0) + if (__domain != PF_INET || __type != SOCK_DGRAM || __protocol != IPPROTO_SWIFT) { + errno = EINVAL; goto sock_err; + } + + s = socket(PF_INET, SOCK_RAW, IPPROTO_SWIFT); + if (s < 0) { + goto sock_err; + } list = list_add_socket(s); - if (list == NULL) + if (list == NULL) { + errno = ENOMEM; goto list_add_err; + } + + /* Socket is not bound. */ + list->bind_state = STATE_NOTBOUND; return s; @@ -157,9 +70,16 @@ sock_err: * * swift: ADDR is of type struct sockaddr_sw. */ -int sw_bind (int __fd, __CONST_SOCKADDR_ARG __addr, socklen_t __len) +int sw_bind(int __fd, __CONST_SOCKADDR_ARG __addr, socklen_t __len) { struct sock_list *list; + int rc; + + rc = list_socket_is_bound(__fd); + if (rc == 1) { + errno = EINVAL; + goto socket_bound_err; + } /* Check whether address is already in use. */ list = list_elem_from_address(__addr); @@ -167,6 +87,7 @@ int sw_bind (int __fd, __CONST_SOCKADDR_ARG __addr, socklen_t __len) errno = EADDRINUSE; goto list_elem_err; } + /* Update __fd entry in socket management list. */ list = list_update_socket_address(__fd, __addr); if (list == NULL) { @@ -176,18 +97,32 @@ int sw_bind (int __fd, __CONST_SOCKADDR_ARG __addr, socklen_t __len) return 0; +socket_bound_err: list_update_err: list_elem_err: return -1; } /* Put the local address of FD into *ADDR and its length in *LEN. */ -int sw_getsockname (int __fd, __SOCKADDR_ARG __addr, +int sw_getsockname(int __fd, __SOCKADDR_ARG __addr, socklen_t *__restrict __len) { - /* TODO */ + struct sock_list *list; + + /* Find socket in management structure. */ + list = list_elem_from_socket(__fd); + if (list == NULL) { + errno = EBADF; + goto list_elem_err; + } + + memcpy(__addr, &list->addr, sizeof(list->addr)); + *__len = sizeof(list->addr); return 0; + +list_elem_err: + return -1; } /* @@ -197,15 +132,44 @@ int sw_getsockname (int __fd, __SOCKADDR_ARG __addr, * This function is a cancellation point and therefore not marked with * __THROW. */ -ssize_t sw_sendto (int __fd, __const void *__buf, size_t __n, +ssize_t sw_sendto(int __fd, __const void *__buf, size_t __n, int __flags, __CONST_SOCKADDR_ARG __addr, socklen_t __addr_len) { ssize_t bytes_sent; + struct sock_list *list; + struct iovec __iov[1]; + struct msghdr __msgh; + + list = list_elem_from_socket(__fd); + if (list == NULL) { + errno = EBADF; + goto sock_err; + } - /* TODO */ +/* + if (list->state == STATE_NOBOUND) { + errno = EDESTADDRREQ; + goto sock_err; + } + */ + + /* Specify the components of the message in an "iovec". */ + __iov[0].iov_base = (void *) __buf; + __iov[0].iov_len = __n; + + /* The message header contains parameters for sendmsg. */ + __msgh.msg_name = (caddr_t) __addr; + __msgh.msg_namelen = sizeof(__addr); + __msgh.msg_iov = __iov; + __msgh.msg_iovlen = 1; + __msgh.msg_control = NULL; /* irrelevant to AF_INET */ + __msgh.msg_controllen = 0; /* irrelevant to AF_INET */ + + return sendmsg(__fd, &__msgh, 0); - return bytes_sent; +sock_err: + return -1; } /* @@ -217,7 +181,7 @@ ssize_t sw_sendto (int __fd, __const void *__buf, size_t __n, * This function is a cancellation point and therefore not marked with * __THROW. */ -ssize_t sw_recvfrom (int __fd, void *__restrict __buf, size_t __n, +ssize_t sw_recvfrom(int __fd, void *__restrict __buf, size_t __n, int __flags, __SOCKADDR_ARG __addr, socklen_t *__restrict __addr_len) { @@ -235,14 +199,12 @@ ssize_t sw_recvfrom (int __fd, void *__restrict __buf, size_t __n, * This function is a cancellation point and therefore not marked with * __THROW. */ -ssize_t sw_sendmsg (int __fd, __const struct msghdr *__message, +ssize_t sw_sendmsg(int __fd, __const struct msghdr *__message, int __flags) { ssize_t bytes_sent; - /* TODO */ - - return bytes_sent; + return sendmsg(__fd, __message, __flags); } /* @@ -252,7 +214,7 @@ ssize_t sw_sendmsg (int __fd, __const struct msghdr *__message, * This function is a cancellation point and therefore not marked with * __THROW. */ -ssize_t sw_recvmsg (int __fd, struct msghdr *__message, int __flags) +ssize_t sw_recvmsg(int __fd, struct msghdr *__message, int __flags) { ssize_t bytes_recv; @@ -266,7 +228,7 @@ ssize_t sw_recvmsg (int __fd, struct msghdr *__message, int __flags) * LEVEL into OPTVAL (which is *OPTLEN bytes long), and set *OPTLEN to the * value's actual length. Returns 0 on success, -1 for errors. */ -int sw_getsockopt (int __fd, int __level, int __optname, +int sw_getsockopt(int __fd, int __level, int __optname, void *__restrict __optval, socklen_t *__restrict __optlen) { @@ -279,8 +241,7 @@ int sw_getsockopt (int __fd, int __level, int __optname, * to *OPTVAL (which is OPTLEN bytes long). * Returns 0 on success, -1 for errors. */ - -int sw_setsockopt (int __fd, int __level, int __optname, +int sw_setsockopt(int __fd, int __level, int __optname, __const void *__optval, socklen_t __optlen) { /* Call classical interface of setsockopt(2). */ @@ -288,15 +249,23 @@ int sw_setsockopt (int __fd, int __level, int __optname, } /* - * Shut down all or part of the connection open on socket FD. - * HOW determines what to shut down: - * SHUT_RD = No more receptions; - * SHUT_WR = No more transmissions; - * SHUT_RDWR = No more receptions or transmissions. + * Close file descriptor for socket FD. * Returns 0 on success, -1 for errors. */ -int sw_shutdown (int __fd, int __how) +int sw_close(int __fd) { - /* Call classical interface of shutdown(2). */ - return shutdown(__fd, __how); + int rc; + + /* Remove socket from socket management structure. */ + rc = list_remove_socket(__fd); + if (rc < 0) { + errno = EBADF; + goto list_unlink_err; + } + + /* Call classical interface of close(2). */ + return close(__fd); + +list_unlink_err: + return -1; }