raw: Add "is bound" check to sw_socket and sw_bind.
[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         /* Socket is not bound. */
203         list->bind_state = STATE_NOTBOUND;
204
205         return s;
206
207 list_add_err:
208         close(s);
209 sock_err:
210         return -1;
211 }
212
213 /*
214  * Give the socket FD the local address ADDR (which is LEN bytes long).
215  *
216  * swift: ADDR is of type struct sockaddr_sw.
217  */
218 int sw_bind(int __fd, __CONST_SOCKADDR_ARG __addr, socklen_t __len)
219 {
220         struct sock_list *list;
221         int rc;
222
223         rc = list_socket_is_bound(__fd);
224         if (rc == 1) {
225                 errno = EINVAL;
226                 goto socket_bound_err;
227         }
228
229         /* Check whether address is already in use. */
230         list = list_elem_from_address(__addr);
231         if (list != NULL) {
232                 errno = EADDRINUSE;
233                 goto list_elem_err;
234         }
235
236         /* Update __fd entry in socket management list. */
237         list = list_update_socket_address(__fd, __addr);
238         if (list == NULL) {
239                 errno = EBADF;
240                 goto list_update_err;
241         }
242
243         return 0;
244
245 socket_bound_err:
246 list_update_err:
247 list_elem_err:
248         return -1;
249 }
250
251 /* Put the local address of FD into *ADDR and its length in *LEN.  */
252 int sw_getsockname(int __fd, __SOCKADDR_ARG __addr,
253                         socklen_t *__restrict __len)
254 {
255         struct sock_list *list;
256
257         /* Find socket in management structure. */
258         list = list_elem_from_socket(__fd);
259         if (list == NULL) {
260                 errno = EBADF;
261                 goto list_elem_err;
262         }
263
264         memcpy(__addr, &list->addr, sizeof(list->addr));
265         *__len = sizeof(list->addr);
266
267         return 0;
268
269 list_elem_err:
270         return -1;
271 }
272
273 /*
274  * Send N bytes of BUF on socket FD to peer at address ADDR (which is
275  * ADDR_LEN bytes long).  Returns the number sent, or -1 for errors.
276  *
277  * This function is a cancellation point and therefore not marked with
278  * __THROW.
279  */
280 ssize_t sw_sendto(int __fd, __const void *__buf, size_t __n,
281                        int __flags, __CONST_SOCKADDR_ARG __addr,
282                        socklen_t __addr_len)
283 {
284         ssize_t bytes_sent;
285         struct sock_list *list;
286         struct iovec __iov[1];
287         struct msghdr __msgh;
288         
289         list = list_elem_from_socket(__fd);
290         if (list == NULL) {
291                 errno = EBADF;
292                 goto sock_err;
293         }
294
295         if (list->rw_state == STATE_SHUT_WR || list->rw_state == STATE_SHUT_RDWR) {
296                 errno = ENOTCONN;
297                 goto sock_err;  
298         }
299 /*
300         if (list->state == STATE_NOBOUND) {
301                 errno = EDESTADDRREQ;
302                 goto sock_err;
303         }
304  */
305         
306         /* Specify the components of the message in an "iovec".   */
307         __iov[0].iov_base = __buf;
308         __iov[0].iov_len = __n;
309         
310         /* The message header contains parameters for sendmsg.    */
311         __msgh.msg_name = (caddr_t) __addr;
312         __msgh.msg_namelen = sizeof(__addr);
313         __msgh.msg_iov = __iov;
314         __msgh.msg_iovlen = 1;
315         __msgh.msg_control = NULL;            /* irrelevant to AF_INET */
316         __msgh.msg_controllen = 0;            /* irrelevant to AF_INET */
317
318         return sendmsg(__fd, &__msgh, 0);
319
320 sock_err:
321         return -1;
322 }
323
324 /*
325  * Read N bytes into BUF through socket FD.
326  * If ADDR is not NULL, fill in *ADDR_LEN bytes of it with tha address of
327  * the sender, and store the actual size of the address in *ADDR_LEN.
328  * Returns the number of bytes read or -1 for errors.
329  *
330  * This function is a cancellation point and therefore not marked with
331  * __THROW.
332  */
333 ssize_t sw_recvfrom(int __fd, void *__restrict __buf, size_t __n,
334                          int __flags, __SOCKADDR_ARG __addr,
335                          socklen_t *__restrict __addr_len)
336 {
337         ssize_t bytes_recv;
338
339         /* TODO */
340
341         return bytes_recv;
342 }
343
344 /*
345  * Send a message described MESSAGE on socket FD.
346  * Returns the number of bytes sent, or -1 for errors.
347  *
348  * This function is a cancellation point and therefore not marked with
349  * __THROW.
350  */
351 ssize_t sw_sendmsg(int __fd, __const struct msghdr *__message,
352                         int __flags)
353 {
354         ssize_t bytes_sent;
355
356         return sendmsg(__fd, __message, __flags);
357 }
358
359 /*
360  * Receive a message as described by MESSAGE from socket FD.
361  * Returns the number of bytes read or -1 for errors.
362  *
363  * This function is a cancellation point and therefore not marked with
364  * __THROW.
365  */
366 ssize_t sw_recvmsg(int __fd, struct msghdr *__message, int __flags)
367 {
368         ssize_t bytes_recv;
369
370         /* TODO */
371
372         return bytes_recv;
373 }
374
375 /*
376  * Put the current value for socket FD's option OPTNAME at protocol level
377  * LEVEL into OPTVAL (which is *OPTLEN bytes long), and set *OPTLEN to the
378  * value's actual length.  Returns 0 on success, -1 for errors.
379  */
380 int sw_getsockopt(int __fd, int __level, int __optname,
381                        void *__restrict __optval,
382                        socklen_t *__restrict __optlen)
383 {
384         /* Call classical interface of getsockopt(2). */
385         return getsockopt(__fd, __level, __optname, __optval, __optlen);
386 }
387
388 /*
389  * Set socket FD's option OPTNAME at protocol level LEVEL
390  * to *OPTVAL (which is OPTLEN bytes long).
391  * Returns 0 on success, -1 for errors.
392  */
393
394 int sw_setsockopt(int __fd, int __level, int __optname,
395                        __const void *__optval, socklen_t __optlen)
396 {
397         /* Call classical interface of setsockopt(2). */
398         return setsockopt(__fd, __level, __optname, __optval, __optlen);
399 }
400
401 /*
402  * Shut down all or part of the connection open on socket FD.
403  * HOW determines what to shut down:
404  *   SHUT_RD   = No more receptions;
405  *   SHUT_WR   = No more transmissions;
406  *   SHUT_RDWR = No more receptions or transmissions.
407  * Returns 0 on success, -1 for errors.
408  */
409 int sw_shutdown(int __fd, int __how)
410 {
411         struct sock_list *list;
412         int rc;
413
414         /* Find socket in management structure. */
415         list = list_elem_from_socket(__fd);
416         if (list == NULL) {
417                 errno = EBADF;
418                 goto list_elem_err;
419         }
420
421         /* Check and update socket state. */
422         if (__how == STATE_SHUT_RDWR)
423                 list->rw_state = STATE_SHUT_RDWR;
424         else if (__how == STATE_SHUT_WR) {
425                 if (list->rw_state == STATE_SHUT_RD)
426                         list->rw_state = STATE_SHUT_RDWR;
427                 else if (list->rw_state == STATE_SHUT_WR) {
428                         errno = ENOTCONN;
429                         goto not_conn_err;
430                 }
431         }
432         else if (__how == STATE_SHUT_RD) {
433                 if (list->rw_state == STATE_SHUT_WR)
434                         list->rw_state = STATE_SHUT_RDWR;
435                 else if (list->rw_state == STATE_SHUT_RD) {
436                         errno = ENOTCONN;
437                         goto not_conn_err;
438                 }
439         }
440
441         /* Remove socket from socket management structure. */
442         if (list->rw_state == STATE_SHUT_RDWR) {
443                 rc = list_remove_socket(__fd);
444                 if (rc < 0) {
445                         errno = EBADF;
446                         goto list_unlink_err;
447                 }
448         }
449
450         /* Call classical interface of shutdown(2). */
451         return shutdown(__fd, __how);
452
453 not_conn_err:
454 list_elem_err:
455 list_unlink_err:
456         return -1;
457 }
458
459 /*
460  * Close file descriptor for socket FD.
461  * Returns 0 on success, -1 for errors.
462  */
463 int sw_close(int __fd)
464 {
465         int rc;
466
467         /* Remove socket from socket management structure. */
468         rc = list_remove_socket(__fd);
469         if (rc < 0) {
470                 errno = EBADF;
471                 goto list_unlink_err;
472         }
473
474         /* Call classical interface of close(2). */
475         return close(__fd);
476
477 list_unlink_err:
478         return -1;
479 }