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;
211 * Give the socket FD the local address ADDR (which is LEN bytes long).
213 * swift: ADDR is of type struct sockaddr_sw.
215 int sw_bind(int __fd, __CONST_SOCKADDR_ARG __addr, socklen_t __len)
217 struct sock_list *list;
219 /* Check whether address is already in use. */
220 list = list_elem_from_address(__addr);
226 /* Update __fd entry in socket management list. */
227 list = list_update_socket_address(__fd, __addr);
230 goto list_update_err;
240 /* Put the local address of FD into *ADDR and its length in *LEN. */
241 int sw_getsockname(int __fd, __SOCKADDR_ARG __addr,
242 socklen_t *__restrict __len)
244 struct sock_list *list;
246 /* Find socket in management structure. */
247 list = list_elem_from_socket(__fd);
253 memcpy(__addr, &list->addr, sizeof(list->addr));
254 *__len = sizeof(list->addr);
263 * Send N bytes of BUF on socket FD to peer at address ADDR (which is
264 * ADDR_LEN bytes long). Returns the number sent, or -1 for errors.
266 * This function is a cancellation point and therefore not marked with
269 ssize_t sw_sendto(int __fd, __const void *__buf, size_t __n,
270 int __flags, __CONST_SOCKADDR_ARG __addr,
271 socklen_t __addr_len)
274 struct sock_list *list;
275 struct iovec __iov[1];
276 struct msghdr __msgh;
278 list = list_elem_from_socket(__fd);
284 if (list->rw_state == STATE_SHUT_WR || list->rw_state == STATE_SHUT_RDWR) {
289 if (list->state == STATE_NOBOUND) {
290 errno = EDESTADDRREQ;
295 /* Specify the components of the message in an "iovec". */
296 __iov[0].iov_base = __buf;
297 __iov[0].iov_len = __n;
299 /* The message header contains parameters for sendmsg. */
300 __msgh.msg_name = (caddr_t) __addr;
301 __msgh.msg_namelen = sizeof(__addr);
302 __msgh.msg_iov = __iov;
303 __msgh.msg_iovlen = 1;
304 __msgh.msg_control = NULL; /* irrelevant to AF_INET */
305 __msgh.msg_controllen = 0; /* irrelevant to AF_INET */
307 return sendmsg(__fd, &__msgh, 0);
314 * Read N bytes into BUF through socket FD.
315 * If ADDR is not NULL, fill in *ADDR_LEN bytes of it with tha address of
316 * the sender, and store the actual size of the address in *ADDR_LEN.
317 * Returns the number of bytes read or -1 for errors.
319 * This function is a cancellation point and therefore not marked with
322 ssize_t sw_recvfrom(int __fd, void *__restrict __buf, size_t __n,
323 int __flags, __SOCKADDR_ARG __addr,
324 socklen_t *__restrict __addr_len)
334 * Send a message described MESSAGE on socket FD.
335 * Returns the number of bytes sent, or -1 for errors.
337 * This function is a cancellation point and therefore not marked with
340 ssize_t sw_sendmsg(int __fd, __const struct msghdr *__message,
345 return sendmsg(__fd, __message, __flags);
349 * Receive a message as described by MESSAGE from socket FD.
350 * Returns the number of bytes read or -1 for errors.
352 * This function is a cancellation point and therefore not marked with
355 ssize_t sw_recvmsg(int __fd, struct msghdr *__message, int __flags)
365 * Put the current value for socket FD's option OPTNAME at protocol level
366 * LEVEL into OPTVAL (which is *OPTLEN bytes long), and set *OPTLEN to the
367 * value's actual length. Returns 0 on success, -1 for errors.
369 int sw_getsockopt(int __fd, int __level, int __optname,
370 void *__restrict __optval,
371 socklen_t *__restrict __optlen)
373 /* Call classical interface of getsockopt(2). */
374 return getsockopt(__fd, __level, __optname, __optval, __optlen);
378 * Set socket FD's option OPTNAME at protocol level LEVEL
379 * to *OPTVAL (which is OPTLEN bytes long).
380 * Returns 0 on success, -1 for errors.
383 int sw_setsockopt(int __fd, int __level, int __optname,
384 __const void *__optval, socklen_t __optlen)
386 /* Call classical interface of setsockopt(2). */
387 return setsockopt(__fd, __level, __optname, __optval, __optlen);
391 * Shut down all or part of the connection open on socket FD.
392 * HOW determines what to shut down:
393 * SHUT_RD = No more receptions;
394 * SHUT_WR = No more transmissions;
395 * SHUT_RDWR = No more receptions or transmissions.
396 * Returns 0 on success, -1 for errors.
398 int sw_shutdown(int __fd, int __how)
400 struct sock_list *list;
403 /* Find socket in management structure. */
404 list = list_elem_from_socket(__fd);
410 /* Check and update socket state. */
411 if (__how == STATE_SHUT_RDWR)
412 list->rw_state = STATE_SHUT_RDWR;
413 else if (__how == STATE_SHUT_WR) {
414 if (list->rw_state == STATE_SHUT_RD)
415 list->rw_state = STATE_SHUT_RDWR;
416 else if (list->rw_state == STATE_SHUT_WR) {
421 else if (__how == STATE_SHUT_RD) {
422 if (list->rw_state == STATE_SHUT_WR)
423 list->rw_state = STATE_SHUT_RDWR;
424 else if (list->rw_state == STATE_SHUT_RD) {
430 /* Remove socket from socket management structure. */
431 if (list->rw_state == STATE_SHUT_RDWR) {
432 rc = list_remove_socket(__fd);
435 goto list_unlink_err;
439 /* Call classical interface of shutdown(2). */
440 return shutdown(__fd, __how);
449 * Close file descriptor for socket FD.
450 * Returns 0 on success, -1 for errors.
452 int sw_close(int __fd)
456 /* Remove socket from socket management structure. */
457 rc = list_remove_socket(__fd);
460 goto list_unlink_err;
463 /* Call classical interface of close(2). */