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