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>
23 #include "swift_types.h"
24 #include "swift_raw.h"
26 /* socket management structure */
29 struct sockaddr_sw addr;
30 struct sock_list *next;
31 struct sock_list *prev;
34 static struct sock_list sock_list_head = {
35 .next = &sock_list_head,
36 .prev = &sock_list_head
40 * Add new socket to list. Called by sw_socket "syscall".
43 static struct sock_list *list_add_socket(int s)
45 struct sock_list *ptr = malloc(sizeof(*ptr));
49 ptr->next = &sock_list_head;
50 ptr->prev = sock_list_head.prev;
51 sock_list_head.prev->next = ptr;
52 sock_list_head.prev = ptr;
58 * Bind socket to given address. Called by sw_bind "syscall".
61 static struct sock_list *list_update_socket_address(int s, struct sockaddr_sw *addr)
63 struct sock_list *ptr;
65 for (ptr = sock_list_head.next; ptr != &sock_list_head; ptr = ptr->next)
67 memcpy(&ptr->addr, addr, sizeof(ptr->addr));
75 * Get list element containing socket s. Called by sw_send* "syscalls".
78 static struct sock_list *list_elem_from_socket(int s)
80 struct sock_list *ptr;
82 for (ptr = sock_list_head.next; ptr != &sock_list_head; ptr = ptr->next)
91 * Remove socket from list. Called by sw_close "syscall".
94 static struct sock_list *list_unlink_socket(int s)
96 struct sock_list *ptr;
98 for (ptr = sock_list_head.next; ptr != &sock_list_head; ptr = ptr->next)
100 ptr->next->prev = ptr->prev;
101 ptr->prev->next = ptr->next;
111 * Create a new socket of type TYPE in domain DOMAIN, using
112 * protocol PROTOCOL. If PROTOCOL is zero, one is chosen automatically.
113 * Returns a file descriptor for the new socket, or -1 for errors.
115 * swift: PROTOCOL is IPPROTO_SWIFT. Ignore TYPE.
117 int sw_socket (int __domain, int __type, int __protocol)
121 s = socket(__domain, SOCK_RAW, IPPROTO_SWIFT);
127 * Give the socket FD the local address ADDR (which is LEN bytes long).
129 * swift: ADDR is of type struct sockaddr_sw.
131 int sw_bind (int __fd, __CONST_SOCKADDR_ARG __addr, socklen_t __len)
138 /* Put the local address of FD into *ADDR and its length in *LEN. */
139 int sw_getsockname (int __fd, __SOCKADDR_ARG __addr,
140 socklen_t *__restrict __len)
148 * Send N bytes of BUF on socket FD to peer at address ADDR (which is
149 * ADDR_LEN bytes long). Returns the number sent, or -1 for errors.
151 * This function is a cancellation point and therefore not marked with
154 ssize_t sw_sendto (int __fd, __const void *__buf, size_t __n,
155 int __flags, __CONST_SOCKADDR_ARG __addr,
156 socklen_t __addr_len)
166 * Read N bytes into BUF through socket FD.
167 * If ADDR is not NULL, fill in *ADDR_LEN bytes of it with tha address of
168 * the sender, and store the actual size of the address in *ADDR_LEN.
169 * Returns the number of bytes read or -1 for errors.
171 * This function is a cancellation point and therefore not marked with
174 ssize_t sw_recvfrom (int __fd, void *__restrict __buf, size_t __n,
175 int __flags, __SOCKADDR_ARG __addr,
176 socklen_t *__restrict __addr_len)
186 * Send a message described MESSAGE on socket FD.
187 * Returns the number of bytes sent, or -1 for errors.
189 * This function is a cancellation point and therefore not marked with
192 ssize_t sw_sendmsg (int __fd, __const struct msghdr *__message,
203 * Receive a message as described by MESSAGE from socket FD.
204 * Returns the number of bytes read or -1 for errors.
206 * This function is a cancellation point and therefore not marked with
209 ssize_t sw_recvmsg (int __fd, struct msghdr *__message, int __flags)
219 * Put the current value for socket FD's option OPTNAME at protocol level
220 * LEVEL into OPTVAL (which is *OPTLEN bytes long), and set *OPTLEN to the
221 * value's actual length. Returns 0 on success, -1 for errors.
223 int sw_getsockopt (int __fd, int __level, int __optname,
224 void *__restrict __optval,
225 socklen_t *__restrict __optlen)
227 /* Call classical interface of getsockopt(2). */
228 return getsockopt(__fd, __level, __optname, __optval, __optlen);
232 * Set socket FD's option OPTNAME at protocol level LEVEL
233 * to *OPTVAL (which is OPTLEN bytes long).
234 * Returns 0 on success, -1 for errors.
237 int sw_setsockopt (int __fd, int __level, int __optname,
238 __const void *__optval, socklen_t __optlen)
240 /* Call classical interface of setsockopt(2). */
241 return setsockopt(__fd, __level, __optname, __optval, __optlen);
245 * Shut down all or part of the connection open on socket FD.
246 * HOW determines what to shut down:
247 * SHUT_RD = No more receptions;
248 * SHUT_WR = No more transmissions;
249 * SHUT_RDWR = No more receptions or transmissions.
250 * Returns 0 on success, -1 for errors.
252 int sw_shutdown (int __fd, int __how)
254 /* Call classical interface of shutdown(2). */
255 return shutdown(__fd, __how);