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 /* socket management structure */
38 struct sockaddr_sw addr;
39 enum sock_rw_state rw_state;
40 struct sock_list *next;
41 struct sock_list *prev;
44 static struct sock_list sock_list_head = {
45 .next = &sock_list_head,
46 .prev = &sock_list_head
50 * Add new socket to list. Called by sw_socket "syscall".
53 static struct sock_list *list_add_socket(int s)
55 struct sock_list *ptr = malloc(sizeof(*ptr));
59 ptr->next = &sock_list_head;
60 ptr->prev = sock_list_head.prev;
61 sock_list_head.prev->next = ptr;
62 sock_list_head.prev = ptr;
69 * Bind socket to given address. Called by sw_bind "syscall".
72 static struct sock_list *list_update_socket_address(int s, __CONST_SOCKADDR_ARG addr)
74 struct sock_list *ptr;
76 for (ptr = sock_list_head.next; ptr != &sock_list_head; ptr = ptr->next)
78 memcpy(&ptr->addr, addr, sizeof(ptr->addr));
86 * Get list element containing socket s. Called by sw_send* "syscalls".
89 static struct sock_list *list_elem_from_socket(int s)
91 struct sock_list *ptr;
93 for (ptr = sock_list_head.next; ptr != &sock_list_head; ptr = ptr->next)
102 * Get list element containing address addr. Called by sw_bind "syscall".
105 static struct sock_list *list_elem_from_address(__CONST_SOCKADDR_ARG addr)
107 struct sock_list *ptr;
109 for (ptr = sock_list_head.next; ptr != &sock_list_head; ptr = ptr->next)
110 if (memcmp(&ptr->addr, addr, sizeof(addr)) == 0)
117 * Remove socket from list. Called by sw_close "syscall".
120 static struct sock_list *list_unlink_socket(int s)
122 struct sock_list *ptr;
124 for (ptr = sock_list_head.next; ptr != &sock_list_head; ptr = ptr->next)
126 ptr->next->prev = ptr->prev;
127 ptr->prev->next = ptr->next;
137 * Create a new socket of type TYPE in domain DOMAIN, using
138 * protocol PROTOCOL. If PROTOCOL is zero, one is chosen automatically.
139 * Returns a file descriptor for the new socket, or -1 for errors.
141 * swift: PROTOCOL is IPPROTO_SWIFT. Ignore TYPE.
143 int sw_socket(int __domain, int __type, int __protocol)
146 struct sock_list *list;
148 if (__domain != PF_INET || __type != SOCK_RAW || __protocol != IPPROTO_SWIFT) {
153 s = socket(PF_INET, SOCK_RAW, IPPROTO_SWIFT);
158 list = list_add_socket(s);
164 /* Socket is fully open. */
165 list->rw_state = STATE_NO_SHUT;
176 * Give the socket FD the local address ADDR (which is LEN bytes long).
178 * swift: ADDR is of type struct sockaddr_sw.
180 int sw_bind(int __fd, __CONST_SOCKADDR_ARG __addr, socklen_t __len)
182 struct sock_list *list;
184 /* Check whether address is already in use. */
185 list = list_elem_from_address(__addr);
191 /* Update __fd entry in socket management list. */
192 list = list_update_socket_address(__fd, __addr);
195 goto list_update_err;
205 /* Put the local address of FD into *ADDR and its length in *LEN. */
206 int sw_getsockname(int __fd, __SOCKADDR_ARG __addr,
207 socklen_t *__restrict __len)
209 struct sock_list *list;
211 /* Find socket in management structure. */
212 list = list_elem_from_socket(__fd);
218 memcpy(__addr, &list->addr, sizeof(list->addr));
219 *__len = sizeof(list->addr);
228 * Send N bytes of BUF on socket FD to peer at address ADDR (which is
229 * ADDR_LEN bytes long). Returns the number sent, or -1 for errors.
231 * This function is a cancellation point and therefore not marked with
234 ssize_t sw_sendto(int __fd, __const void *__buf, size_t __n,
235 int __flags, __CONST_SOCKADDR_ARG __addr,
236 socklen_t __addr_len)
239 struct sock_list *list;
240 struct iovec __iov[1];
241 struct msghdr __msgh;
243 list = list_elem_from_socket(__fd);
249 if (list->rw_state == STATE_SHUT_WR || list->rw_state == STATE_SHUT_RDWR) {
254 if (list->state == STATE_NOBOUND) {
255 errno = EDESTADDRREQ;
260 /* Specify the components of the message in an "iovec". */
261 __iov[0].iov_base = __buf;
262 __iov[0].iov_len = __n;
264 /* The message header contains parameters for sendmsg. */
265 __msgh.msg_name = (caddr_t) __addr;
266 __msgh.msg_namelen = sizeof(__addr);
267 __msgh.msg_iov = __iov;
268 __msgh.msg_iovlen = 1;
269 __msgh.msg_control = NULL; /* irrelevant to AF_INET */
270 __msgh.msg_controllen = 0; /* irrelevant to AF_INET */
272 return sendmsg(__fd, &__msgh, 0);
279 * Read N bytes into BUF through socket FD.
280 * If ADDR is not NULL, fill in *ADDR_LEN bytes of it with tha address of
281 * the sender, and store the actual size of the address in *ADDR_LEN.
282 * Returns the number of bytes read or -1 for errors.
284 * This function is a cancellation point and therefore not marked with
287 ssize_t sw_recvfrom(int __fd, void *__restrict __buf, size_t __n,
288 int __flags, __SOCKADDR_ARG __addr,
289 socklen_t *__restrict __addr_len)
299 * Send a message described MESSAGE on socket FD.
300 * Returns the number of bytes sent, or -1 for errors.
302 * This function is a cancellation point and therefore not marked with
305 ssize_t sw_sendmsg(int __fd, __const struct msghdr *__message,
310 return sendmsg(__fd, __message, __flags);
314 * Receive a message as described by MESSAGE from socket FD.
315 * Returns the number of bytes read or -1 for errors.
317 * This function is a cancellation point and therefore not marked with
320 ssize_t sw_recvmsg(int __fd, struct msghdr *__message, int __flags)
330 * Put the current value for socket FD's option OPTNAME at protocol level
331 * LEVEL into OPTVAL (which is *OPTLEN bytes long), and set *OPTLEN to the
332 * value's actual length. Returns 0 on success, -1 for errors.
334 int sw_getsockopt(int __fd, int __level, int __optname,
335 void *__restrict __optval,
336 socklen_t *__restrict __optlen)
338 /* Call classical interface of getsockopt(2). */
339 return getsockopt(__fd, __level, __optname, __optval, __optlen);
343 * Set socket FD's option OPTNAME at protocol level LEVEL
344 * to *OPTVAL (which is OPTLEN bytes long).
345 * Returns 0 on success, -1 for errors.
348 int sw_setsockopt(int __fd, int __level, int __optname,
349 __const void *__optval, socklen_t __optlen)
351 /* Call classical interface of setsockopt(2). */
352 return setsockopt(__fd, __level, __optname, __optval, __optlen);
356 * Shut down all or part of the connection open on socket FD.
357 * HOW determines what to shut down:
358 * SHUT_RD = No more receptions;
359 * SHUT_WR = No more transmissions;
360 * SHUT_RDWR = No more receptions or transmissions.
361 * Returns 0 on success, -1 for errors.
363 int sw_shutdown(int __fd, int __how)
365 struct sock_list *list;
367 /* Find socket in management structure. */
368 list = list_elem_from_socket(__fd);
374 /* Check and update socket state. */
375 if (__how == STATE_SHUT_RDWR)
376 list->rw_state = STATE_SHUT_RDWR;
377 else if (__how == STATE_SHUT_WR) {
378 if (list->rw_state == STATE_SHUT_RD)
379 list->rw_state = STATE_SHUT_RDWR;
380 else if (list->rw_state == STATE_SHUT_WR) {
385 else if (__how == STATE_SHUT_RD) {
386 if (list->rw_state == STATE_SHUT_WR)
387 list->rw_state = STATE_SHUT_RDWR;
388 else if (list->rw_state == STATE_SHUT_RD) {
394 /* Remove socket from socket management structure. */
395 if (list->rw_state == STATE_SHUT_RDWR) {
396 list = list_unlink_socket(__fd);
399 goto list_unlink_err;
403 /* Call classical interface of shutdown(2). */
404 return shutdown(__fd, __how);
413 * Close file descriptor for socket FD.
414 * Returns 0 on success, -1 for errors.
416 int sw_close(int __fd)
418 struct sock_list *list;
420 /* Remove socket from socket management structure. */
421 list = list_unlink_socket(__fd);
424 goto list_unlink_err;
427 /* Call classical interface of close(2). */