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>
24 #include "swift_types.h"
25 #include "swift_raw.h"
27 /* socket management structure */
30 struct sockaddr_sw addr;
31 struct sock_list *next;
32 struct sock_list *prev;
35 static struct sock_list sock_list_head = {
36 .next = &sock_list_head,
37 .prev = &sock_list_head
41 * Add new socket to list. Called by sw_socket "syscall".
44 static struct sock_list *list_add_socket(int s)
46 struct sock_list *ptr = malloc(sizeof(*ptr));
50 ptr->next = &sock_list_head;
51 ptr->prev = sock_list_head.prev;
52 sock_list_head.prev->next = ptr;
53 sock_list_head.prev = ptr;
59 * Bind socket to given address. Called by sw_bind "syscall".
62 static struct sock_list *list_update_socket_address(int s, struct sockaddr_sw *addr)
64 struct sock_list *ptr;
66 for (ptr = sock_list_head.next; ptr != &sock_list_head; ptr = ptr->next)
68 memcpy(&ptr->addr, addr, sizeof(ptr->addr));
76 * Get list element containing socket s. Called by sw_send* "syscalls".
79 static struct sock_list *list_elem_from_socket(int s)
81 struct sock_list *ptr;
83 for (ptr = sock_list_head.next; ptr != &sock_list_head; ptr = ptr->next)
92 * Remove socket from list. Called by sw_close "syscall".
95 static struct sock_list *list_unlink_socket(int s)
97 struct sock_list *ptr;
99 for (ptr = sock_list_head.next; ptr != &sock_list_head; ptr = ptr->next)
101 ptr->next->prev = ptr->prev;
102 ptr->prev->next = ptr->next;
112 * Create a new socket of type TYPE in domain DOMAIN, using
113 * protocol PROTOCOL. If PROTOCOL is zero, one is chosen automatically.
114 * Returns a file descriptor for the new socket, or -1 for errors.
116 * swift: PROTOCOL is IPPROTO_SWIFT. Ignore TYPE.
118 int sw_socket (int __domain, int __type, int __protocol)
121 struct sock_list *list;
123 s = socket(__domain, SOCK_RAW, IPPROTO_SWIFT);
127 list = list_add_socket(s);
140 * Give the socket FD the local address ADDR (which is LEN bytes long).
142 * swift: ADDR is of type struct sockaddr_sw.
144 int sw_bind (int __fd, __CONST_SOCKADDR_ARG __addr, socklen_t __len)
151 /* Put the local address of FD into *ADDR and its length in *LEN. */
152 int sw_getsockname (int __fd, __SOCKADDR_ARG __addr,
153 socklen_t *__restrict __len)
161 * Send N bytes of BUF on socket FD to peer at address ADDR (which is
162 * ADDR_LEN bytes long). Returns the number sent, or -1 for errors.
164 * This function is a cancellation point and therefore not marked with
167 ssize_t sw_sendto (int __fd, __const void *__buf, size_t __n,
168 int __flags, __CONST_SOCKADDR_ARG __addr,
169 socklen_t __addr_len)
179 * Read N bytes into BUF through socket FD.
180 * If ADDR is not NULL, fill in *ADDR_LEN bytes of it with tha address of
181 * the sender, and store the actual size of the address in *ADDR_LEN.
182 * Returns the number of bytes read or -1 for errors.
184 * This function is a cancellation point and therefore not marked with
187 ssize_t sw_recvfrom (int __fd, void *__restrict __buf, size_t __n,
188 int __flags, __SOCKADDR_ARG __addr,
189 socklen_t *__restrict __addr_len)
199 * Send a message described MESSAGE on socket FD.
200 * Returns the number of bytes sent, or -1 for errors.
202 * This function is a cancellation point and therefore not marked with
205 ssize_t sw_sendmsg (int __fd, __const struct msghdr *__message,
216 * Receive a message as described by MESSAGE from socket FD.
217 * Returns the number of bytes read or -1 for errors.
219 * This function is a cancellation point and therefore not marked with
222 ssize_t sw_recvmsg (int __fd, struct msghdr *__message, int __flags)
232 * Put the current value for socket FD's option OPTNAME at protocol level
233 * LEVEL into OPTVAL (which is *OPTLEN bytes long), and set *OPTLEN to the
234 * value's actual length. Returns 0 on success, -1 for errors.
236 int sw_getsockopt (int __fd, int __level, int __optname,
237 void *__restrict __optval,
238 socklen_t *__restrict __optlen)
240 /* Call classical interface of getsockopt(2). */
241 return getsockopt(__fd, __level, __optname, __optval, __optlen);
245 * Set socket FD's option OPTNAME at protocol level LEVEL
246 * to *OPTVAL (which is OPTLEN bytes long).
247 * Returns 0 on success, -1 for errors.
250 int sw_setsockopt (int __fd, int __level, int __optname,
251 __const void *__optval, socklen_t __optlen)
253 /* Call classical interface of setsockopt(2). */
254 return setsockopt(__fd, __level, __optname, __optval, __optlen);
258 * Shut down all or part of the connection open on socket FD.
259 * HOW determines what to shut down:
260 * SHUT_RD = No more receptions;
261 * SHUT_WR = No more transmissions;
262 * SHUT_RDWR = No more receptions or transmissions.
263 * Returns 0 on success, -1 for errors.
265 int sw_shutdown (int __fd, int __how)
267 /* Call classical interface of shutdown(2). */
268 return shutdown(__fd, __how);