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)
108 * Get list element containing address addr. Called by sw_bind "syscall".
111 static struct sock_list *list_elem_from_address(__CONST_SOCKADDR_ARG addr)
113 struct sock_list *ptr;
115 for (ptr = sock_list_head.next; ptr != &sock_list_head; ptr = ptr->next) {
116 if (ptr->bind_state == STATE_NOTBOUND)
118 if (memcmp(&ptr->addr, addr, sizeof(addr)) == 0)
125 static struct sock_list *list_unlink_socket(int s)
127 struct sock_list *ptr;
129 for (ptr = sock_list_head.next; ptr != &sock_list_head; ptr = ptr->next)
131 ptr->next->prev = ptr->prev;
132 ptr->prev->next = ptr->next;
142 * Remove socket from list. Called by sw_close "syscall".
145 static int list_remove_socket(int s)
147 struct sock_list *ptr;
149 ptr = list_unlink_socket(s);
157 static int list_socket_is_bound(int s)
159 struct sock_list *ptr;
161 for (ptr = sock_list_head.next; ptr != &sock_list_head; ptr = ptr->next)
163 if (ptr->bind_state == STATE_BOUND)
172 * Create a new socket of type TYPE in domain DOMAIN, using
173 * protocol PROTOCOL. If PROTOCOL is zero, one is chosen automatically.
174 * Returns a file descriptor for the new socket, or -1 for errors.
176 * swift: PROTOCOL is IPPROTO_SWIFT. Ignore TYPE.
178 int sw_socket(int __domain, int __type, int __protocol)
181 struct sock_list *list;
183 if (__domain != PF_INET || __type != SOCK_RAW || __protocol != IPPROTO_SWIFT) {
188 s = socket(PF_INET, SOCK_RAW, IPPROTO_SWIFT);
193 list = list_add_socket(s);
199 /* Socket is fully open. */
200 list->rw_state = STATE_NO_SHUT;
202 /* Socket is not bound. */
203 list->bind_state = STATE_NOTBOUND;
214 * Give the socket FD the local address ADDR (which is LEN bytes long).
216 * swift: ADDR is of type struct sockaddr_sw.
218 int sw_bind(int __fd, __CONST_SOCKADDR_ARG __addr, socklen_t __len)
220 struct sock_list *list;
223 rc = list_socket_is_bound(__fd);
226 goto socket_bound_err;
229 /* Check whether address is already in use. */
230 list = list_elem_from_address(__addr);
236 /* Update __fd entry in socket management list. */
237 list = list_update_socket_address(__fd, __addr);
240 goto list_update_err;
251 /* Put the local address of FD into *ADDR and its length in *LEN. */
252 int sw_getsockname(int __fd, __SOCKADDR_ARG __addr,
253 socklen_t *__restrict __len)
255 struct sock_list *list;
257 /* Find socket in management structure. */
258 list = list_elem_from_socket(__fd);
264 memcpy(__addr, &list->addr, sizeof(list->addr));
265 *__len = sizeof(list->addr);
274 * Send N bytes of BUF on socket FD to peer at address ADDR (which is
275 * ADDR_LEN bytes long). Returns the number sent, or -1 for errors.
277 * This function is a cancellation point and therefore not marked with
280 ssize_t sw_sendto(int __fd, __const void *__buf, size_t __n,
281 int __flags, __CONST_SOCKADDR_ARG __addr,
282 socklen_t __addr_len)
285 struct sock_list *list;
286 struct iovec __iov[1];
287 struct msghdr __msgh;
289 list = list_elem_from_socket(__fd);
295 if (list->rw_state == STATE_SHUT_WR || list->rw_state == STATE_SHUT_RDWR) {
300 if (list->state == STATE_NOBOUND) {
301 errno = EDESTADDRREQ;
306 /* Specify the components of the message in an "iovec". */
307 __iov[0].iov_base = __buf;
308 __iov[0].iov_len = __n;
310 /* The message header contains parameters for sendmsg. */
311 __msgh.msg_name = (caddr_t) __addr;
312 __msgh.msg_namelen = sizeof(__addr);
313 __msgh.msg_iov = __iov;
314 __msgh.msg_iovlen = 1;
315 __msgh.msg_control = NULL; /* irrelevant to AF_INET */
316 __msgh.msg_controllen = 0; /* irrelevant to AF_INET */
318 return sendmsg(__fd, &__msgh, 0);
325 * Read N bytes into BUF through socket FD.
326 * If ADDR is not NULL, fill in *ADDR_LEN bytes of it with tha address of
327 * the sender, and store the actual size of the address in *ADDR_LEN.
328 * Returns the number of bytes read or -1 for errors.
330 * This function is a cancellation point and therefore not marked with
333 ssize_t sw_recvfrom(int __fd, void *__restrict __buf, size_t __n,
334 int __flags, __SOCKADDR_ARG __addr,
335 socklen_t *__restrict __addr_len)
345 * Send a message described MESSAGE on socket FD.
346 * Returns the number of bytes sent, or -1 for errors.
348 * This function is a cancellation point and therefore not marked with
351 ssize_t sw_sendmsg(int __fd, __const struct msghdr *__message,
356 return sendmsg(__fd, __message, __flags);
360 * Receive a message as described by MESSAGE from socket FD.
361 * Returns the number of bytes read or -1 for errors.
363 * This function is a cancellation point and therefore not marked with
366 ssize_t sw_recvmsg(int __fd, struct msghdr *__message, int __flags)
376 * Put the current value for socket FD's option OPTNAME at protocol level
377 * LEVEL into OPTVAL (which is *OPTLEN bytes long), and set *OPTLEN to the
378 * value's actual length. Returns 0 on success, -1 for errors.
380 int sw_getsockopt(int __fd, int __level, int __optname,
381 void *__restrict __optval,
382 socklen_t *__restrict __optlen)
384 /* Call classical interface of getsockopt(2). */
385 return getsockopt(__fd, __level, __optname, __optval, __optlen);
389 * Set socket FD's option OPTNAME at protocol level LEVEL
390 * to *OPTVAL (which is OPTLEN bytes long).
391 * Returns 0 on success, -1 for errors.
394 int sw_setsockopt(int __fd, int __level, int __optname,
395 __const void *__optval, socklen_t __optlen)
397 /* Call classical interface of setsockopt(2). */
398 return setsockopt(__fd, __level, __optname, __optval, __optlen);
402 * Shut down all or part of the connection open on socket FD.
403 * HOW determines what to shut down:
404 * SHUT_RD = No more receptions;
405 * SHUT_WR = No more transmissions;
406 * SHUT_RDWR = No more receptions or transmissions.
407 * Returns 0 on success, -1 for errors.
409 int sw_shutdown(int __fd, int __how)
411 struct sock_list *list;
414 /* Find socket in management structure. */
415 list = list_elem_from_socket(__fd);
421 /* Check and update socket state. */
422 if (__how == STATE_SHUT_RDWR)
423 list->rw_state = STATE_SHUT_RDWR;
424 else if (__how == STATE_SHUT_WR) {
425 if (list->rw_state == STATE_SHUT_RD)
426 list->rw_state = STATE_SHUT_RDWR;
427 else if (list->rw_state == STATE_SHUT_WR) {
432 else if (__how == STATE_SHUT_RD) {
433 if (list->rw_state == STATE_SHUT_WR)
434 list->rw_state = STATE_SHUT_RDWR;
435 else if (list->rw_state == STATE_SHUT_RD) {
441 /* Remove socket from socket management structure. */
442 if (list->rw_state == STATE_SHUT_RDWR) {
443 rc = list_remove_socket(__fd);
446 goto list_unlink_err;
450 /* Call classical interface of shutdown(2). */
451 return shutdown(__fd, __how);
460 * Close file descriptor for socket FD.
461 * Returns 0 on success, -1 for errors.
463 int sw_close(int __fd)
467 /* Remove socket from socket management structure. */
468 rc = list_remove_socket(__fd);
471 goto list_unlink_err;
474 /* Call classical interface of close(2). */