#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
+#include <unistd.h>
+#include <errno.h>
#include "swift_types.h"
#include "swift_raw.h"
+enum sock_rw_state {
+ STATE_NO_SHUT,
+ STATE_SHUT_RD,
+ STATE_SHUT_WR,
+ STATE_SHUT_RDWR
+};
+
/* socket management structure */
struct sock_list {
int s;
struct sockaddr_sw addr;
+ enum sock_rw_state rw_state;
struct sock_list *next;
struct sock_list *prev;
};
.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));
ptr->prev = sock_list_head.prev;
sock_list_head.prev->next = ptr;
sock_list_head.prev = ptr;
+ ptr->s = s;
return ptr;
}
-static struct sock_list *list_update_socket_address(int s, struct sockaddr_sw *addr)
+/*
+ * Bind socket to given address. Called by sw_bind "syscall".
+ */
+
+static struct sock_list *list_update_socket_address(int s, __CONST_SOCKADDR_ARG addr)
{
struct sock_list *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_SOCKADDR_ARG 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;
*
* 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)
+ goto sock_err;
+
+ list = list_add_socket(s);
+ if (list == NULL)
+ goto list_add_err;
+
+ /* Socket is fully open. */
+ list->rw_state = STATE_NO_SHUT;
+
+ if (__domain != AF_INET || __type != SOCK_RAW || __protocol != IPPROTO_SWIFT) {
+ errno = EINVAL;
+ return -1;
+ }
+
+ s = socket(AF_INET, SOCK_RAW, IPPROTO_SWIFT);
return s;
+
+list_add_err:
+ close(s);
+sock_err:
+ return -1;
}
/*
*
* 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)
{
- /* TODO */
+ struct sock_list *list;
+
+ /* Check whether address is already in use. */
+ list = list_elem_from_address(__addr);
+ if (list != NULL) {
+ errno = EADDRINUSE;
+ goto list_elem_err;
+ }
+ /* Update __fd entry in socket management list. */
+ list = list_update_socket_address(__fd, __addr);
+ if (list == NULL) {
+ errno = EBADF;
+ goto list_update_err;
+ }
return 0;
+
+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 */
* 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;
- /* TODO */
-
return bytes_sent;
}
* 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)
{
* 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);
}
/*
* 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;
* 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)
{
* 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). */
* SHUT_RDWR = No more receptions or transmissions.
* Returns 0 on success, -1 for errors.
*/
-int sw_shutdown (int __fd, int __how)
+int sw_shutdown(int __fd, int __how)
{
+ struct sock_list *list;
+
+ /* Find socket in management structure. */
+ list = list_elem_from_socket(__fd);
+ if (list == NULL) {
+ errno = EBADF;
+ goto list_elem_err;
+ }
+
+ /* Check and update socket state. */
+ if (__how == STATE_SHUT_RDWR)
+ list->rw_state = STATE_SHUT_RDWR;
+ else if (__how == STATE_SHUT_WR) {
+ if (list->rw_state == STATE_SHUT_RD)
+ list->rw_state = STATE_SHUT_RDWR;
+ else if (list->rw_state == STATE_SHUT_WR) {
+ errno = ENOTCONN;
+ goto not_conn_err;
+ }
+ }
+ else if (__how == STATE_SHUT_RD) {
+ if (list->rw_state == STATE_SHUT_WR)
+ list->rw_state = STATE_SHUT_RDWR;
+ else if (list->rw_state == STATE_SHUT_RD) {
+ errno = ENOTCONN;
+ goto not_conn_err;
+ }
+ }
+
+ /* Remove socket from socket management structure. */
+ if (list->rw_state == STATE_SHUT_RDWR) {
+ list = list_unlink_socket(__fd);
+ if (list == NULL) {
+ errno = EBADF;
+ goto list_unlink_err;
+ }
+ }
+
/* Call classical interface of shutdown(2). */
return shutdown(__fd, __how);
+
+not_conn_err:
+list_elem_err:
+list_unlink_err:
+ return -1;
+}
+
+/*
+ * Close file descriptor for socket FD.
+ * Returns 0 on success, -1 for errors.
+ */
+int sw_close(int __fd)
+{
+ struct sock_list *list;
+
+ /* Remove socket from socket management structure. */
+ list = list_unlink_socket(__fd);
+ if (list == NULL) {
+ errno = EBADF;
+ goto list_unlink_err;
+ }
+
+ /* Call classical interface of close(2). */
+ return close(__fd);
+
+list_unlink_err:
+ return -1;
}