2 * swift implementation of syscall API
4 * Simulates the classic socket syscalls (socket, bind, send, recv).
5 * Implementation uses raw sockets (AF_INET, SOCK_RAW).
7 * Subsequently, implementation is to be ported into kernel space and
8 * the interface is going to be offered by the Linux syscall API.
10 * Heavily inspired by GLIBC's <sys/socket.h>
11 * (/usr/include/sys/socket.h).
13 * 2011, Razvan Deaconescu, razvan.deaconescu@cs.pub.ro
19 #include <sys/types.h>
20 #include <sys/socket.h>
21 #include <netinet/in.h>
25 #include "swift_types.h"
26 #include "swift_raw.h"
35 enum sock_bind_state {
40 /* socket management structure */
43 struct sockaddr_sw addr;
44 enum sock_rw_state rw_state;
45 enum sock_bind_state bind_state;
46 struct sock_list *next;
47 struct sock_list *prev;
50 static struct sock_list sock_list_head = {
51 .next = &sock_list_head,
52 .prev = &sock_list_head
56 * Add new socket to list. Called by sw_socket "syscall".
59 static struct sock_list *list_add_socket(int s)
61 struct sock_list *ptr = malloc(sizeof(*ptr));
65 ptr->next = &sock_list_head;
66 ptr->prev = sock_list_head.prev;
67 sock_list_head.prev->next = ptr;
68 sock_list_head.prev = ptr;
75 * Bind socket to given address. Called by sw_bind "syscall".
78 static struct sock_list *list_update_socket_address(int s, __CONST_SOCKADDR_ARG addr)
80 struct sock_list *ptr;
82 for (ptr = sock_list_head.next; ptr != &sock_list_head; ptr = ptr->next)
84 memcpy(&ptr->addr, addr, sizeof(ptr->addr));
92 * Get list element containing socket s. Called by sw_send* "syscalls".
95 static struct sock_list *list_elem_from_socket(int s)
97 struct sock_list *ptr;
99 for (ptr = sock_list_head.next; ptr != &sock_list_head; ptr = ptr->next)
107 * Get list element containing address addr. Called by sw_bind "syscall".
110 static struct sock_list *list_elem_from_address(__CONST_SOCKADDR_ARG addr)
112 struct sock_list *ptr;
114 for (ptr = sock_list_head.next; ptr != &sock_list_head; ptr = ptr->next) {
115 if (ptr->bind_state == STATE_NOTBOUND)
117 if (memcmp(&ptr->addr, addr, sizeof(addr)) == 0)
124 static struct sock_list *list_unlink_socket(int s)
126 struct sock_list *ptr;
128 for (ptr = sock_list_head.next; ptr != &sock_list_head; ptr = ptr->next)
130 ptr->next->prev = ptr->prev;
131 ptr->prev->next = ptr->next;
141 * Remove socket from list. Called by sw_close "syscall".
144 static int list_remove_socket(int s)
146 struct sock_list *ptr;
148 ptr = list_unlink_socket(s);
156 static int list_socket_is_bound(int s)
158 struct sock_list *ptr;
160 for (ptr = sock_list_head.next; ptr != &sock_list_head; ptr = ptr->next)
162 if (ptr->bind_state == STATE_BOUND)
171 * Create a new socket of type TYPE in domain DOMAIN, using
172 * protocol PROTOCOL. If PROTOCOL is zero, one is chosen automatically.
173 * Returns a file descriptor for the new socket, or -1 for errors.
175 * swift: PROTOCOL is IPPROTO_SWIFT. Ignore TYPE.
177 int sw_socket(int __domain, int __type, int __protocol)
180 struct sock_list *list;
182 if (__domain != PF_INET || __type != SOCK_DGRAM || __protocol != IPPROTO_SWIFT) {
187 s = socket(PF_INET, SOCK_RAW, IPPROTO_SWIFT);
192 list = list_add_socket(s);
198 /* Socket is fully open. */
199 list->rw_state = STATE_NO_SHUT;
201 /* Socket is not bound. */
202 list->bind_state = STATE_NOTBOUND;
213 * Give the socket FD the local address ADDR (which is LEN bytes long).
215 * swift: ADDR is of type struct sockaddr_sw.
217 int sw_bind(int __fd, __CONST_SOCKADDR_ARG __addr, socklen_t __len)
219 struct sock_list *list;
222 rc = list_socket_is_bound(__fd);
225 goto socket_bound_err;
228 /* Check whether address is already in use. */
229 list = list_elem_from_address(__addr);
235 /* Update __fd entry in socket management list. */
236 list = list_update_socket_address(__fd, __addr);
239 goto list_update_err;
250 /* Put the local address of FD into *ADDR and its length in *LEN. */
251 int sw_getsockname(int __fd, __SOCKADDR_ARG __addr,
252 socklen_t *__restrict __len)
254 struct sock_list *list;
256 /* Find socket in management structure. */
257 list = list_elem_from_socket(__fd);
263 memcpy(__addr, &list->addr, sizeof(list->addr));
264 *__len = sizeof(list->addr);
273 * Send N bytes of BUF on socket FD to peer at address ADDR (which is
274 * ADDR_LEN bytes long). Returns the number sent, or -1 for errors.
276 * This function is a cancellation point and therefore not marked with
279 ssize_t sw_sendto(int __fd, __const void *__buf, size_t __n,
280 int __flags, __CONST_SOCKADDR_ARG __addr,
281 socklen_t __addr_len)
284 struct sock_list *list;
285 struct iovec __iov[1];
286 struct msghdr __msgh;
288 list = list_elem_from_socket(__fd);
294 if (list->rw_state == STATE_SHUT_WR || list->rw_state == STATE_SHUT_RDWR) {
299 if (list->state == STATE_NOBOUND) {
300 errno = EDESTADDRREQ;
305 /* Specify the components of the message in an "iovec". */
306 __iov[0].iov_base = __buf;
307 __iov[0].iov_len = __n;
309 /* The message header contains parameters for sendmsg. */
310 __msgh.msg_name = (caddr_t) __addr;
311 __msgh.msg_namelen = sizeof(__addr);
312 __msgh.msg_iov = __iov;
313 __msgh.msg_iovlen = 1;
314 __msgh.msg_control = NULL; /* irrelevant to AF_INET */
315 __msgh.msg_controllen = 0; /* irrelevant to AF_INET */
317 return sendmsg(__fd, &__msgh, 0);
324 * Read N bytes into BUF through socket FD.
325 * If ADDR is not NULL, fill in *ADDR_LEN bytes of it with tha address of
326 * the sender, and store the actual size of the address in *ADDR_LEN.
327 * Returns the number of bytes read or -1 for errors.
329 * This function is a cancellation point and therefore not marked with
332 ssize_t sw_recvfrom(int __fd, void *__restrict __buf, size_t __n,
333 int __flags, __SOCKADDR_ARG __addr,
334 socklen_t *__restrict __addr_len)
344 * Send a message described MESSAGE on socket FD.
345 * Returns the number of bytes sent, or -1 for errors.
347 * This function is a cancellation point and therefore not marked with
350 ssize_t sw_sendmsg(int __fd, __const struct msghdr *__message,
355 return sendmsg(__fd, __message, __flags);
359 * Receive a message as described by MESSAGE from socket FD.
360 * Returns the number of bytes read or -1 for errors.
362 * This function is a cancellation point and therefore not marked with
365 ssize_t sw_recvmsg(int __fd, struct msghdr *__message, int __flags)
375 * Put the current value for socket FD's option OPTNAME at protocol level
376 * LEVEL into OPTVAL (which is *OPTLEN bytes long), and set *OPTLEN to the
377 * value's actual length. Returns 0 on success, -1 for errors.
379 int sw_getsockopt(int __fd, int __level, int __optname,
380 void *__restrict __optval,
381 socklen_t *__restrict __optlen)
383 /* Call classical interface of getsockopt(2). */
384 return getsockopt(__fd, __level, __optname, __optval, __optlen);
388 * Set socket FD's option OPTNAME at protocol level LEVEL
389 * to *OPTVAL (which is OPTLEN bytes long).
390 * Returns 0 on success, -1 for errors.
393 int sw_setsockopt(int __fd, int __level, int __optname,
394 __const void *__optval, socklen_t __optlen)
396 /* Call classical interface of setsockopt(2). */
397 return setsockopt(__fd, __level, __optname, __optval, __optlen);
401 * Shut down all or part of the connection open on socket FD.
402 * HOW determines what to shut down:
403 * SHUT_RD = No more receptions;
404 * SHUT_WR = No more transmissions;
405 * SHUT_RDWR = No more receptions or transmissions.
406 * Returns 0 on success, -1 for errors.
408 int sw_shutdown(int __fd, int __how)
410 struct sock_list *list;
413 /* Find socket in management structure. */
414 list = list_elem_from_socket(__fd);
420 /* Check and update socket state. */
421 if (__how == STATE_SHUT_RDWR)
422 list->rw_state = STATE_SHUT_RDWR;
423 else if (__how == STATE_SHUT_WR) {
424 if (list->rw_state == STATE_SHUT_RD)
425 list->rw_state = STATE_SHUT_RDWR;
426 else if (list->rw_state == STATE_SHUT_WR) {
431 else if (__how == STATE_SHUT_RD) {
432 if (list->rw_state == STATE_SHUT_WR)
433 list->rw_state = STATE_SHUT_RDWR;
434 else if (list->rw_state == STATE_SHUT_RD) {
440 /* Remove socket from socket management structure. */
441 if (list->rw_state == STATE_SHUT_RDWR) {
442 rc = list_remove_socket(__fd);
445 goto list_unlink_err;
449 /* Call classical interface of shutdown(2). */
450 return shutdown(__fd, __how);
459 * Close file descriptor for socket FD.
460 * Returns 0 on success, -1 for errors.
462 int sw_close(int __fd)
466 /* Remove socket from socket management structure. */
467 rc = list_remove_socket(__fd);
470 goto list_unlink_err;
473 /* Call classical interface of close(2). */