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"
28 /* socket management structure */
31 struct sockaddr_sw addr;
32 struct sock_list *next;
33 struct sock_list *prev;
36 static struct sock_list sock_list_head = {
37 .next = &sock_list_head,
38 .prev = &sock_list_head
42 * Add new socket to list. Called by sw_socket "syscall".
45 static struct sock_list *list_add_socket(int s)
47 struct sock_list *ptr = malloc(sizeof(*ptr));
51 ptr->next = &sock_list_head;
52 ptr->prev = sock_list_head.prev;
53 sock_list_head.prev->next = ptr;
54 sock_list_head.prev = ptr;
60 * Bind socket to given address. Called by sw_bind "syscall".
63 static struct sock_list *list_update_socket_address(int s, struct sockaddr_sw *addr)
65 struct sock_list *ptr;
67 for (ptr = sock_list_head.next; ptr != &sock_list_head; ptr = ptr->next)
69 memcpy(&ptr->addr, addr, sizeof(ptr->addr));
77 * Get list element containing socket s. Called by sw_send* "syscalls".
80 static struct sock_list *list_elem_from_socket(int s)
82 struct sock_list *ptr;
84 for (ptr = sock_list_head.next; ptr != &sock_list_head; ptr = ptr->next)
93 * Get list element containing address addr. Called by sw_bind "syscall".
96 static struct sock_list *list_elem_from_address(const struct sockaddr_sw *addr)
98 struct sock_list *ptr;
100 for (ptr = sock_list_head.next; ptr != &sock_list_head; ptr = ptr->next)
101 if (memcmp(&ptr->addr, addr, sizeof(addr)) == 0)
108 * Remove socket from list. Called by sw_close "syscall".
111 static struct sock_list *list_unlink_socket(int s)
113 struct sock_list *ptr;
115 for (ptr = sock_list_head.next; ptr != &sock_list_head; ptr = ptr->next)
117 ptr->next->prev = ptr->prev;
118 ptr->prev->next = ptr->next;
128 * Create a new socket of type TYPE in domain DOMAIN, using
129 * protocol PROTOCOL. If PROTOCOL is zero, one is chosen automatically.
130 * Returns a file descriptor for the new socket, or -1 for errors.
132 * swift: PROTOCOL is IPPROTO_SWIFT. Ignore TYPE.
134 int sw_socket (int __domain, int __type, int __protocol)
137 struct sock_list *list;
139 s = socket(__domain, SOCK_RAW, IPPROTO_SWIFT);
143 list = list_add_socket(s);
156 * Give the socket FD the local address ADDR (which is LEN bytes long).
158 * swift: ADDR is of type struct sockaddr_sw.
160 int sw_bind (int __fd, __CONST_SOCKADDR_ARG __addr, socklen_t __len)
162 struct sock_list *list;
164 /* Check whether address is already in use. */
165 list = list_elem_from_address(__addr);
170 /* Update __fd entry in socket management list. */
171 list = list_update_socket_address(__fd, __addr);
174 goto list_update_err;
184 /* Put the local address of FD into *ADDR and its length in *LEN. */
185 int sw_getsockname (int __fd, __SOCKADDR_ARG __addr,
186 socklen_t *__restrict __len)
194 * Send N bytes of BUF on socket FD to peer at address ADDR (which is
195 * ADDR_LEN bytes long). Returns the number sent, or -1 for errors.
197 * This function is a cancellation point and therefore not marked with
200 ssize_t sw_sendto (int __fd, __const void *__buf, size_t __n,
201 int __flags, __CONST_SOCKADDR_ARG __addr,
202 socklen_t __addr_len)
212 * Read N bytes into BUF through socket FD.
213 * If ADDR is not NULL, fill in *ADDR_LEN bytes of it with tha address of
214 * the sender, and store the actual size of the address in *ADDR_LEN.
215 * Returns the number of bytes read or -1 for errors.
217 * This function is a cancellation point and therefore not marked with
220 ssize_t sw_recvfrom (int __fd, void *__restrict __buf, size_t __n,
221 int __flags, __SOCKADDR_ARG __addr,
222 socklen_t *__restrict __addr_len)
232 * Send a message described MESSAGE on socket FD.
233 * Returns the number of bytes sent, or -1 for errors.
235 * This function is a cancellation point and therefore not marked with
238 ssize_t sw_sendmsg (int __fd, __const struct msghdr *__message,
249 * Receive a message as described by MESSAGE from socket FD.
250 * Returns the number of bytes read or -1 for errors.
252 * This function is a cancellation point and therefore not marked with
255 ssize_t sw_recvmsg (int __fd, struct msghdr *__message, int __flags)
265 * Put the current value for socket FD's option OPTNAME at protocol level
266 * LEVEL into OPTVAL (which is *OPTLEN bytes long), and set *OPTLEN to the
267 * value's actual length. Returns 0 on success, -1 for errors.
269 int sw_getsockopt (int __fd, int __level, int __optname,
270 void *__restrict __optval,
271 socklen_t *__restrict __optlen)
273 /* Call classical interface of getsockopt(2). */
274 return getsockopt(__fd, __level, __optname, __optval, __optlen);
278 * Set socket FD's option OPTNAME at protocol level LEVEL
279 * to *OPTVAL (which is OPTLEN bytes long).
280 * Returns 0 on success, -1 for errors.
283 int sw_setsockopt (int __fd, int __level, int __optname,
284 __const void *__optval, socklen_t __optlen)
286 /* Call classical interface of setsockopt(2). */
287 return setsockopt(__fd, __level, __optname, __optval, __optlen);
291 * Shut down all or part of the connection open on socket FD.
292 * HOW determines what to shut down:
293 * SHUT_RD = No more receptions;
294 * SHUT_WR = No more transmissions;
295 * SHUT_RDWR = No more receptions or transmissions.
296 * Returns 0 on success, -1 for errors.
298 int sw_shutdown (int __fd, int __how)
300 /* Call classical interface of shutdown(2). */
301 return shutdown(__fd, __how);