2761b21329597780e7b8149696fecfa4f75c423d
[swifty.git] / src / raw / swift_raw.c
1 /*
2  * swift implementation of syscall API
3  *
4  * Simulates the classic socket syscalls (socket, bind, send, recv).
5  * Implementation uses raw sockets (AF_INET, SOCK_RAW).
6  *
7  * Subsequently, implementation is to be ported into kernel space and
8  * the interface is going to be offered by the Linux syscall API.
9  *
10  * Heavily inspired by GLIBC's <sys/socket.h>
11  * (/usr/include/sys/socket.h).
12  *
13  * 2011, Razvan Deaconescu, razvan.deaconescu@cs.pub.ro
14  */
15
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <sys/types.h>
20 #include <sys/socket.h>
21 #include <netinet/in.h>
22 #include <unistd.h>
23 #include <errno.h>
24
25 #include "swift_types.h"
26 #include "swift_raw.h"
27
28 enum sock_rw_state {
29         STATE_NO_SHUT,
30         STATE_SHUT_RD,
31         STATE_SHUT_WR,
32         STATE_SHUT_RDWR
33 };
34
35 enum sock_bind_state {
36         STATE_NOTBOUND,
37         STATE_BOUND
38 };
39
40 /* socket management structure */
41 struct sock_list {
42         int s;
43         struct sockaddr_sw addr;
44         enum sock_rw_state rw_state;
45         enum sock_bind_state bind_state;
46         struct sock_list *next;
47         struct sock_list *prev;
48 };
49
50 static struct sock_list sock_list_head = {
51         .next = &sock_list_head,
52         .prev = &sock_list_head
53 };
54
55 /*
56  * Add new socket to list. Called by sw_socket "syscall".
57  */
58
59 static struct sock_list *list_add_socket(int s)
60 {
61         struct sock_list *ptr = malloc(sizeof(*ptr));
62         if (ptr == NULL)
63                 return NULL;
64
65         ptr->next = &sock_list_head;
66         ptr->prev = sock_list_head.prev;
67         sock_list_head.prev->next = ptr;
68         sock_list_head.prev = ptr;
69         ptr->s = s;
70
71         return ptr;
72 }
73
74 /*
75  * Bind socket to given address. Called by sw_bind "syscall".
76  */
77
78 static struct sock_list *list_update_socket_address(int s, __CONST_SOCKADDR_ARG addr)
79 {
80         struct sock_list *ptr;
81
82         for (ptr = sock_list_head.next; ptr != &sock_list_head; ptr = ptr->next)
83                 if (ptr->s == s) {
84                         memcpy(&ptr->addr, addr, sizeof(ptr->addr));
85                         return ptr;
86                 }
87
88         return NULL;
89 }
90
91 /*
92  * Get list element containing socket s. Called by sw_send* "syscalls".
93  */
94
95 static struct sock_list *list_elem_from_socket(int s)
96 {
97         struct sock_list *ptr;
98
99         for (ptr = sock_list_head.next; ptr != &sock_list_head; ptr = ptr->next)
100                 if (ptr->s == s) {
101                         return ptr;
102                 }
103
104         return NULL;
105 }
106
107 /*
108  * Get list element containing address addr. Called by sw_bind "syscall".
109  */
110
111 static struct sock_list *list_elem_from_address(__CONST_SOCKADDR_ARG addr)
112 {
113         struct sock_list *ptr;
114
115         for (ptr = sock_list_head.next; ptr != &sock_list_head; ptr = ptr->next) {
116                 if (ptr->bind_state == STATE_NOTBOUND)
117                         continue;
118                 if (memcmp(&ptr->addr, addr, sizeof(addr)) == 0)
119                         return ptr;
120         }
121
122         return NULL;
123 }
124
125 static struct sock_list *list_unlink_socket(int s)
126 {
127         struct sock_list *ptr;
128
129         for (ptr = sock_list_head.next; ptr != &sock_list_head; ptr = ptr->next)
130                 if (ptr->s == s) {
131                         ptr->next->prev = ptr->prev;
132                         ptr->prev->next = ptr->next;
133                         ptr->next = ptr;
134                         ptr->prev = ptr;
135                         return ptr;
136                 }
137
138         return NULL;
139 }
140
141 /*
142  * Remove socket from list. Called by sw_close "syscall".
143  */
144
145 static int list_remove_socket(int s)
146 {
147         struct sock_list *ptr;
148
149         ptr = list_unlink_socket(s);
150         if (ptr == NULL)
151                 return -1;
152
153         free(ptr);
154         return 0;
155 }
156
157 static int list_socket_is_bound(int s)
158 {
159         struct sock_list *ptr;
160
161         for (ptr = sock_list_head.next; ptr != &sock_list_head; ptr = ptr->next)
162                 if (ptr->s == s) {
163                         if (ptr->bind_state == STATE_BOUND)
164                                 return 1;
165                         break;
166                 }
167
168         return 0;
169 }
170
171 /*
172  * Create a new socket of type TYPE in domain DOMAIN, using
173  * protocol PROTOCOL.  If PROTOCOL is zero, one is chosen automatically.
174  * Returns a file descriptor for the new socket, or -1 for errors.
175  *
176  * swift: PROTOCOL is IPPROTO_SWIFT. Ignore TYPE.
177  */
178 int sw_socket(int __domain, int __type, int __protocol)
179 {
180         int s;
181         struct sock_list *list;
182
183         if (__domain != PF_INET || __type != SOCK_RAW || __protocol != IPPROTO_SWIFT) {
184                 errno = EINVAL;
185                 goto sock_err;
186         }
187
188         s = socket(PF_INET, SOCK_RAW, IPPROTO_SWIFT);
189         if (s < 0) {
190                 goto sock_err;
191         }
192
193         list = list_add_socket(s);
194         if (list == NULL) {
195                 errno = ENOMEM;
196                 goto list_add_err;
197         }
198
199         /* Socket is fully open. */
200         list->rw_state = STATE_NO_SHUT;
201
202         return s;
203
204 list_add_err:
205         close(s);
206 sock_err:
207         return -1;
208 }
209
210 /*
211  * Give the socket FD the local address ADDR (which is LEN bytes long).
212  *
213  * swift: ADDR is of type struct sockaddr_sw.
214  */
215 int sw_bind(int __fd, __CONST_SOCKADDR_ARG __addr, socklen_t __len)
216 {
217         struct sock_list *list;
218
219         /* Check whether address is already in use. */
220         list = list_elem_from_address(__addr);
221         if (list != NULL) {
222                 errno = EADDRINUSE;
223                 goto list_elem_err;
224         }
225
226         /* Update __fd entry in socket management list. */
227         list = list_update_socket_address(__fd, __addr);
228         if (list == NULL) {
229                 errno = EBADF;
230                 goto list_update_err;
231         }
232
233         return 0;
234
235 list_update_err:
236 list_elem_err:
237         return -1;
238 }
239
240 /* Put the local address of FD into *ADDR and its length in *LEN.  */
241 int sw_getsockname(int __fd, __SOCKADDR_ARG __addr,
242                         socklen_t *__restrict __len)
243 {
244         struct sock_list *list;
245
246         /* Find socket in management structure. */
247         list = list_elem_from_socket(__fd);
248         if (list == NULL) {
249                 errno = EBADF;
250                 goto list_elem_err;
251         }
252
253         memcpy(__addr, &list->addr, sizeof(list->addr));
254         *__len = sizeof(list->addr);
255
256         return 0;
257
258 list_elem_err:
259         return -1;
260 }
261
262 /*
263  * Send N bytes of BUF on socket FD to peer at address ADDR (which is
264  * ADDR_LEN bytes long).  Returns the number sent, or -1 for errors.
265  *
266  * This function is a cancellation point and therefore not marked with
267  * __THROW.
268  */
269 ssize_t sw_sendto(int __fd, __const void *__buf, size_t __n,
270                        int __flags, __CONST_SOCKADDR_ARG __addr,
271                        socklen_t __addr_len)
272 {
273         ssize_t bytes_sent;
274         struct sock_list *list;
275         struct iovec __iov[1];
276         struct msghdr __msgh;
277         
278         list = list_elem_from_socket(__fd);
279         if (list == NULL) {
280                 errno = EBADF;
281                 goto sock_err;
282         }
283
284         if (list->rw_state == STATE_SHUT_WR || list->rw_state == STATE_SHUT_RDWR) {
285                 errno = ENOTCONN;
286                 goto sock_err;  
287         }
288 /*
289         if (list->state == STATE_NOBOUND) {
290                 errno = EDESTADDRREQ;
291                 goto sock_err;
292         }
293  */
294         
295         /* Specify the components of the message in an "iovec".   */
296         __iov[0].iov_base = __buf;
297         __iov[0].iov_len = __n;
298         
299         /* The message header contains parameters for sendmsg.    */
300         __msgh.msg_name = (caddr_t) __addr;
301         __msgh.msg_namelen = sizeof(__addr);
302         __msgh.msg_iov = __iov;
303         __msgh.msg_iovlen = 1;
304         __msgh.msg_control = NULL;            /* irrelevant to AF_INET */
305         __msgh.msg_controllen = 0;            /* irrelevant to AF_INET */
306
307         return sendmsg(__fd, &__msgh, 0);
308
309 sock_err:
310         return -1;
311 }
312
313 /*
314  * Read N bytes into BUF through socket FD.
315  * If ADDR is not NULL, fill in *ADDR_LEN bytes of it with tha address of
316  * the sender, and store the actual size of the address in *ADDR_LEN.
317  * Returns the number of bytes read or -1 for errors.
318  *
319  * This function is a cancellation point and therefore not marked with
320  * __THROW.
321  */
322 ssize_t sw_recvfrom(int __fd, void *__restrict __buf, size_t __n,
323                          int __flags, __SOCKADDR_ARG __addr,
324                          socklen_t *__restrict __addr_len)
325 {
326         ssize_t bytes_recv;
327
328         /* TODO */
329
330         return bytes_recv;
331 }
332
333 /*
334  * Send a message described MESSAGE on socket FD.
335  * Returns the number of bytes sent, or -1 for errors.
336  *
337  * This function is a cancellation point and therefore not marked with
338  * __THROW.
339  */
340 ssize_t sw_sendmsg(int __fd, __const struct msghdr *__message,
341                         int __flags)
342 {
343         ssize_t bytes_sent;
344
345         return sendmsg(__fd, __message, __flags);
346 }
347
348 /*
349  * Receive a message as described by MESSAGE from socket FD.
350  * Returns the number of bytes read or -1 for errors.
351  *
352  * This function is a cancellation point and therefore not marked with
353  * __THROW.
354  */
355 ssize_t sw_recvmsg(int __fd, struct msghdr *__message, int __flags)
356 {
357         ssize_t bytes_recv;
358
359         /* TODO */
360
361         return bytes_recv;
362 }
363
364 /*
365  * Put the current value for socket FD's option OPTNAME at protocol level
366  * LEVEL into OPTVAL (which is *OPTLEN bytes long), and set *OPTLEN to the
367  * value's actual length.  Returns 0 on success, -1 for errors.
368  */
369 int sw_getsockopt(int __fd, int __level, int __optname,
370                        void *__restrict __optval,
371                        socklen_t *__restrict __optlen)
372 {
373         /* Call classical interface of getsockopt(2). */
374         return getsockopt(__fd, __level, __optname, __optval, __optlen);
375 }
376
377 /*
378  * Set socket FD's option OPTNAME at protocol level LEVEL
379  * to *OPTVAL (which is OPTLEN bytes long).
380  * Returns 0 on success, -1 for errors.
381  */
382
383 int sw_setsockopt(int __fd, int __level, int __optname,
384                        __const void *__optval, socklen_t __optlen)
385 {
386         /* Call classical interface of setsockopt(2). */
387         return setsockopt(__fd, __level, __optname, __optval, __optlen);
388 }
389
390 /*
391  * Shut down all or part of the connection open on socket FD.
392  * HOW determines what to shut down:
393  *   SHUT_RD   = No more receptions;
394  *   SHUT_WR   = No more transmissions;
395  *   SHUT_RDWR = No more receptions or transmissions.
396  * Returns 0 on success, -1 for errors.
397  */
398 int sw_shutdown(int __fd, int __how)
399 {
400         struct sock_list *list;
401         int rc;
402
403         /* Find socket in management structure. */
404         list = list_elem_from_socket(__fd);
405         if (list == NULL) {
406                 errno = EBADF;
407                 goto list_elem_err;
408         }
409
410         /* Check and update socket state. */
411         if (__how == STATE_SHUT_RDWR)
412                 list->rw_state = STATE_SHUT_RDWR;
413         else if (__how == STATE_SHUT_WR) {
414                 if (list->rw_state == STATE_SHUT_RD)
415                         list->rw_state = STATE_SHUT_RDWR;
416                 else if (list->rw_state == STATE_SHUT_WR) {
417                         errno = ENOTCONN;
418                         goto not_conn_err;
419                 }
420         }
421         else if (__how == STATE_SHUT_RD) {
422                 if (list->rw_state == STATE_SHUT_WR)
423                         list->rw_state = STATE_SHUT_RDWR;
424                 else if (list->rw_state == STATE_SHUT_RD) {
425                         errno = ENOTCONN;
426                         goto not_conn_err;
427                 }
428         }
429
430         /* Remove socket from socket management structure. */
431         if (list->rw_state == STATE_SHUT_RDWR) {
432                 rc = list_remove_socket(__fd);
433                 if (rc < 0) {
434                         errno = EBADF;
435                         goto list_unlink_err;
436                 }
437         }
438
439         /* Call classical interface of shutdown(2). */
440         return shutdown(__fd, __how);
441
442 not_conn_err:
443 list_elem_err:
444 list_unlink_err:
445         return -1;
446 }
447
448 /*
449  * Close file descriptor for socket FD.
450  * Returns 0 on success, -1 for errors.
451  */
452 int sw_close(int __fd)
453 {
454         int rc;
455
456         /* Remove socket from socket management structure. */
457         rc = list_remove_socket(__fd);
458         if (rc < 0) {
459                 errno = EBADF;
460                 goto list_unlink_err;
461         }
462
463         /* Call classical interface of close(2). */
464         return close(__fd);
465
466 list_unlink_err:
467         return -1;
468 }