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;
61 * Bind socket to given address. Called by sw_bind "syscall".
64 static struct sock_list *list_update_socket_address(int s, __CONST_SOCKADDR_ARG addr)
66 struct sock_list *ptr;
68 for (ptr = sock_list_head.next; ptr != &sock_list_head; ptr = ptr->next)
70 memcpy(&ptr->addr, addr, sizeof(ptr->addr));
78 * Get list element containing socket s. Called by sw_send* "syscalls".
81 static struct sock_list *list_elem_from_socket(int s)
83 struct sock_list *ptr;
85 for (ptr = sock_list_head.next; ptr != &sock_list_head; ptr = ptr->next)
94 * Get list element containing address addr. Called by sw_bind "syscall".
97 static struct sock_list *list_elem_from_address(__CONST_SOCKADDR_ARG addr)
99 struct sock_list *ptr;
101 for (ptr = sock_list_head.next; ptr != &sock_list_head; ptr = ptr->next)
102 if (memcmp(&ptr->addr, addr, sizeof(addr)) == 0)
109 * Remove socket from list. Called by sw_close "syscall".
112 static struct sock_list *list_unlink_socket(int s)
114 struct sock_list *ptr;
116 for (ptr = sock_list_head.next; ptr != &sock_list_head; ptr = ptr->next)
118 ptr->next->prev = ptr->prev;
119 ptr->prev->next = ptr->next;
129 * Create a new socket of type TYPE in domain DOMAIN, using
130 * protocol PROTOCOL. If PROTOCOL is zero, one is chosen automatically.
131 * Returns a file descriptor for the new socket, or -1 for errors.
133 * swift: PROTOCOL is IPPROTO_SWIFT. Ignore TYPE.
135 int sw_socket (int __domain, int __type, int __protocol)
138 struct sock_list *list;
140 s = socket(__domain, SOCK_RAW, IPPROTO_SWIFT);
144 list = list_add_socket(s);
157 * Give the socket FD the local address ADDR (which is LEN bytes long).
159 * swift: ADDR is of type struct sockaddr_sw.
161 int sw_bind (int __fd, __CONST_SOCKADDR_ARG __addr, socklen_t __len)
163 struct sock_list *list;
165 /* Check whether address is already in use. */
166 list = list_elem_from_address(__addr);
171 /* Update __fd entry in socket management list. */
172 list = list_update_socket_address(__fd, __addr);
175 goto list_update_err;
185 /* Put the local address of FD into *ADDR and its length in *LEN. */
186 int sw_getsockname (int __fd, __SOCKADDR_ARG __addr,
187 socklen_t *__restrict __len)
195 * Send N bytes of BUF on socket FD to peer at address ADDR (which is
196 * ADDR_LEN bytes long). Returns the number sent, or -1 for errors.
198 * This function is a cancellation point and therefore not marked with
201 ssize_t sw_sendto (int __fd, __const void *__buf, size_t __n,
202 int __flags, __CONST_SOCKADDR_ARG __addr,
203 socklen_t __addr_len)
213 * Read N bytes into BUF through socket FD.
214 * If ADDR is not NULL, fill in *ADDR_LEN bytes of it with tha address of
215 * the sender, and store the actual size of the address in *ADDR_LEN.
216 * Returns the number of bytes read or -1 for errors.
218 * This function is a cancellation point and therefore not marked with
221 ssize_t sw_recvfrom (int __fd, void *__restrict __buf, size_t __n,
222 int __flags, __SOCKADDR_ARG __addr,
223 socklen_t *__restrict __addr_len)
233 * Send a message described MESSAGE on socket FD.
234 * Returns the number of bytes sent, or -1 for errors.
236 * This function is a cancellation point and therefore not marked with
239 ssize_t sw_sendmsg (int __fd, __const struct msghdr *__message,
250 * Receive a message as described by MESSAGE from socket FD.
251 * Returns the number of bytes read or -1 for errors.
253 * This function is a cancellation point and therefore not marked with
256 ssize_t sw_recvmsg (int __fd, struct msghdr *__message, int __flags)
266 * Put the current value for socket FD's option OPTNAME at protocol level
267 * LEVEL into OPTVAL (which is *OPTLEN bytes long), and set *OPTLEN to the
268 * value's actual length. Returns 0 on success, -1 for errors.
270 int sw_getsockopt (int __fd, int __level, int __optname,
271 void *__restrict __optval,
272 socklen_t *__restrict __optlen)
274 /* Call classical interface of getsockopt(2). */
275 return getsockopt(__fd, __level, __optname, __optval, __optlen);
279 * Set socket FD's option OPTNAME at protocol level LEVEL
280 * to *OPTVAL (which is OPTLEN bytes long).
281 * Returns 0 on success, -1 for errors.
284 int sw_setsockopt (int __fd, int __level, int __optname,
285 __const void *__optval, socklen_t __optlen)
287 /* Call classical interface of setsockopt(2). */
288 return setsockopt(__fd, __level, __optname, __optval, __optlen);
292 * Shut down all or part of the connection open on socket FD.
293 * HOW determines what to shut down:
294 * SHUT_RD = No more receptions;
295 * SHUT_WR = No more transmissions;
296 * SHUT_RDWR = No more receptions or transmissions.
297 * Returns 0 on success, -1 for errors.
299 int sw_shutdown (int __fd, int __how)
301 /* Call classical interface of shutdown(2). */
302 return shutdown(__fd, __how);