raw: add sendTo
[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 /*
117  * Remove socket from list. Called by sw_close "syscall".
118  */
119
120 static struct sock_list *list_unlink_socket(int s)
121 {
122         struct sock_list *ptr;
123
124         for (ptr = sock_list_head.next; ptr != &sock_list_head; ptr = ptr->next)
125                 if (ptr->s == s) {
126                         ptr->next->prev = ptr->prev;
127                         ptr->prev->next = ptr->next;
128                         ptr->next = ptr;
129                         ptr->prev = ptr;
130                         return ptr;
131                 }
132
133         return NULL;
134 }
135
136 /*
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.
140  *
141  * swift: PROTOCOL is IPPROTO_SWIFT. Ignore TYPE.
142  */
143 int sw_socket(int __domain, int __type, int __protocol)
144 {
145         int s;
146         struct sock_list *list;
147
148         if (__domain != PF_INET || __type != SOCK_RAW || __protocol != IPPROTO_SWIFT) {
149                 errno = EINVAL;
150                 goto sock_err;
151         }
152
153         s = socket(PF_INET, SOCK_RAW, IPPROTO_SWIFT);
154         if (s < 0) {
155                 goto sock_err;
156         }
157
158         list = list_add_socket(s);
159         if (list == NULL) {
160                 errno = ENOMEM;
161                 goto list_add_err;
162         }
163
164         /* Socket is fully open. */
165         list->rw_state = STATE_NO_SHUT;
166
167         return s;
168
169 list_add_err:
170         close(s);
171 sock_err:
172         return -1;
173 }
174
175 /*
176  * Give the socket FD the local address ADDR (which is LEN bytes long).
177  *
178  * swift: ADDR is of type struct sockaddr_sw.
179  */
180 int sw_bind(int __fd, __CONST_SOCKADDR_ARG __addr, socklen_t __len)
181 {
182         struct sock_list *list;
183
184         /* Check whether address is already in use. */
185         list = list_elem_from_address(__addr);
186         if (list != NULL) {
187                 errno = EADDRINUSE;
188                 goto list_elem_err;
189         }
190
191         /* Update __fd entry in socket management list. */
192         list = list_update_socket_address(__fd, __addr);
193         if (list == NULL) {
194                 errno = EBADF;
195                 goto list_update_err;
196         }
197
198         return 0;
199
200 list_update_err:
201 list_elem_err:
202         return -1;
203 }
204
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)
208 {
209         /* TODO */
210
211         return 0;
212 }
213
214 /*
215  * Send N bytes of BUF on socket FD to peer at address ADDR (which is
216  * ADDR_LEN bytes long).  Returns the number sent, or -1 for errors.
217  *
218  * This function is a cancellation point and therefore not marked with
219  * __THROW.
220  */
221 ssize_t sw_sendto(int __fd, __const void *__buf, size_t __n,
222                        int __flags, __CONST_SOCKADDR_ARG __addr,
223                        socklen_t __addr_len)
224 {
225         ssize_t bytes_sent;
226         struct sock_list *list;
227         struct iovec __iov[1];
228         struct msghdr __msgh;
229         
230         list = list_elem_from_socket(__fd);
231         if (list == NULL) {
232                 errno = EBADF;
233                 goto sock_err;
234         }
235
236         if (list->rw_state == STATE_SHUT_WR || list->rw_state == STATE_SHUT_RDWR) {
237                 errno = ENOTCONN;
238                 goto sock_err;  
239         }
240 /*
241         if (list->state == STATE_NOBOUND) {
242                 errno = EDESTADDRREQ;
243                 goto sock_err;
244         }
245  */
246         
247         /* Specify the components of the message in an "iovec".   */
248         __iov[0].iov_base = __buf;
249         __iov[0].iov_len = __n;
250         
251         /* The message header contains parameters for sendmsg.    */
252         __msgh.msg_name = (caddr_t) __addr;
253         __msgh.msg_namelen = sizeof(__addr);
254         __msgh.msg_iov = __iov;
255         __msgh.msg_iovlen = 1;
256         __msgh.msg_control = NULL;            /* irrelevant to AF_INET */
257         __msgh.msg_controllen = 0;            /* irrelevant to AF_INET */
258
259         return sendmsg(__fd, &__msgh, 0);
260
261 sock_err:
262         return -1;
263 }
264
265 /*
266  * Read N bytes into BUF through socket FD.
267  * If ADDR is not NULL, fill in *ADDR_LEN bytes of it with tha address of
268  * the sender, and store the actual size of the address in *ADDR_LEN.
269  * Returns the number of bytes read or -1 for errors.
270  *
271  * This function is a cancellation point and therefore not marked with
272  * __THROW.
273  */
274 ssize_t sw_recvfrom(int __fd, void *__restrict __buf, size_t __n,
275                          int __flags, __SOCKADDR_ARG __addr,
276                          socklen_t *__restrict __addr_len)
277 {
278         ssize_t bytes_recv;
279
280         /* TODO */
281
282         return bytes_recv;
283 }
284
285 /*
286  * Send a message described MESSAGE on socket FD.
287  * Returns the number of bytes sent, or -1 for errors.
288  *
289  * This function is a cancellation point and therefore not marked with
290  * __THROW.
291  */
292 ssize_t sw_sendmsg(int __fd, __const struct msghdr *__message,
293                         int __flags)
294 {
295         ssize_t bytes_sent;
296
297         return sendmsg(__fd, __message, __flags);
298 }
299
300 /*
301  * Receive a message as described by MESSAGE from socket FD.
302  * Returns the number of bytes read or -1 for errors.
303  *
304  * This function is a cancellation point and therefore not marked with
305  * __THROW.
306  */
307 ssize_t sw_recvmsg(int __fd, struct msghdr *__message, int __flags)
308 {
309         ssize_t bytes_recv;
310
311         /* TODO */
312
313         return bytes_recv;
314 }
315
316 /*
317  * Put the current value for socket FD's option OPTNAME at protocol level
318  * LEVEL into OPTVAL (which is *OPTLEN bytes long), and set *OPTLEN to the
319  * value's actual length.  Returns 0 on success, -1 for errors.
320  */
321 int sw_getsockopt(int __fd, int __level, int __optname,
322                        void *__restrict __optval,
323                        socklen_t *__restrict __optlen)
324 {
325         /* Call classical interface of getsockopt(2). */
326         return getsockopt(__fd, __level, __optname, __optval, __optlen);
327 }
328
329 /*
330  * Set socket FD's option OPTNAME at protocol level LEVEL
331  * to *OPTVAL (which is OPTLEN bytes long).
332  * Returns 0 on success, -1 for errors.
333  */
334
335 int sw_setsockopt(int __fd, int __level, int __optname,
336                        __const void *__optval, socklen_t __optlen)
337 {
338         /* Call classical interface of setsockopt(2). */
339         return setsockopt(__fd, __level, __optname, __optval, __optlen);
340 }
341
342 /*
343  * Shut down all or part of the connection open on socket FD.
344  * HOW determines what to shut down:
345  *   SHUT_RD   = No more receptions;
346  *   SHUT_WR   = No more transmissions;
347  *   SHUT_RDWR = No more receptions or transmissions.
348  * Returns 0 on success, -1 for errors.
349  */
350 int sw_shutdown(int __fd, int __how)
351 {
352         struct sock_list *list;
353
354         /* Find socket in management structure. */
355         list = list_elem_from_socket(__fd);
356         if (list == NULL) {
357                 errno = EBADF;
358                 goto list_elem_err;
359         }
360
361         /* Check and update socket state. */
362         if (__how == STATE_SHUT_RDWR)
363                 list->rw_state = STATE_SHUT_RDWR;
364         else if (__how == STATE_SHUT_WR) {
365                 if (list->rw_state == STATE_SHUT_RD)
366                         list->rw_state = STATE_SHUT_RDWR;
367                 else if (list->rw_state == STATE_SHUT_WR) {
368                         errno = ENOTCONN;
369                         goto not_conn_err;
370                 }
371         }
372         else if (__how == STATE_SHUT_RD) {
373                 if (list->rw_state == STATE_SHUT_WR)
374                         list->rw_state = STATE_SHUT_RDWR;
375                 else if (list->rw_state == STATE_SHUT_RD) {
376                         errno = ENOTCONN;
377                         goto not_conn_err;
378                 }
379         }
380
381         /* Remove socket from socket management structure. */
382         if (list->rw_state == STATE_SHUT_RDWR) {
383                 list = list_unlink_socket(__fd);
384                 if (list == NULL) {
385                         errno = EBADF;
386                         goto list_unlink_err;
387                 }
388         }
389
390         /* Call classical interface of shutdown(2). */
391         return shutdown(__fd, __how);
392
393 not_conn_err:
394 list_elem_err:
395 list_unlink_err:
396         return -1;
397 }
398
399 /*
400  * Close file descriptor for socket FD.
401  * Returns 0 on success, -1 for errors.
402  */
403 int sw_close(int __fd)
404 {
405         struct sock_list *list;
406
407         /* Remove socket from socket management structure. */
408         list = list_unlink_socket(__fd);
409         if (list == NULL) {
410                 errno = EBADF;
411                 goto list_unlink_err;
412         }
413
414         /* Call classical interface of close(2). */
415         return close(__fd);
416
417 list_unlink_err:
418         return -1;
419 }