2299acaecb90145eb7d5cfba5d2ca384a73d5621
[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         s = socket(__domain, SOCK_RAW, IPPROTO_SWIFT);
149         if (s < 0)
150                 goto sock_err;
151
152         list = list_add_socket(s);
153         if (list == NULL)
154                 goto list_add_err;
155
156         /* Socket is fully open. */
157         list->rw_state = STATE_NO_SHUT;
158
159         return s;
160
161 list_add_err:
162         close(s);
163 sock_err:
164         return -1;
165 }
166
167 /*
168  * Give the socket FD the local address ADDR (which is LEN bytes long).
169  *
170  * swift: ADDR is of type struct sockaddr_sw.
171  */
172 int sw_bind(int __fd, __CONST_SOCKADDR_ARG __addr, socklen_t __len)
173 {
174         struct sock_list *list;
175
176         /* Check whether address is already in use. */
177         list = list_elem_from_address(__addr);
178         if (list != NULL) {
179                 errno = EADDRINUSE;
180                 goto list_elem_err;
181         }
182         /* Update __fd entry in socket management list. */
183         list = list_update_socket_address(__fd, __addr);
184         if (list == NULL) {
185                 errno = EBADF;
186                 goto list_update_err;
187         }
188
189         return 0;
190
191 list_update_err:
192 list_elem_err:
193         return -1;
194 }
195
196 /* Put the local address of FD into *ADDR and its length in *LEN.  */
197 int sw_getsockname(int __fd, __SOCKADDR_ARG __addr,
198                         socklen_t *__restrict __len)
199 {
200         /* TODO */
201
202         return 0;
203 }
204
205 /*
206  * Send N bytes of BUF on socket FD to peer at address ADDR (which is
207  * ADDR_LEN bytes long).  Returns the number sent, or -1 for errors.
208  *
209  * This function is a cancellation point and therefore not marked with
210  * __THROW.
211  */
212 ssize_t sw_sendto(int __fd, __const void *__buf, size_t __n,
213                        int __flags, __CONST_SOCKADDR_ARG __addr,
214                        socklen_t __addr_len)
215 {
216         ssize_t bytes_sent;
217
218         /* TODO */
219
220         return bytes_sent;
221 }
222
223 /*
224  * Read N bytes into BUF through socket FD.
225  * If ADDR is not NULL, fill in *ADDR_LEN bytes of it with tha address of
226  * the sender, and store the actual size of the address in *ADDR_LEN.
227  * Returns the number of bytes read or -1 for errors.
228  *
229  * This function is a cancellation point and therefore not marked with
230  * __THROW.
231  */
232 ssize_t sw_recvfrom(int __fd, void *__restrict __buf, size_t __n,
233                          int __flags, __SOCKADDR_ARG __addr,
234                          socklen_t *__restrict __addr_len)
235 {
236         ssize_t bytes_recv;
237
238         /* TODO */
239
240         return bytes_recv;
241 }
242
243 /*
244  * Send a message described MESSAGE on socket FD.
245  * Returns the number of bytes sent, or -1 for errors.
246  *
247  * This function is a cancellation point and therefore not marked with
248  * __THROW.
249  */
250 ssize_t sw_sendmsg(int __fd, __const struct msghdr *__message,
251                         int __flags)
252 {
253         ssize_t bytes_sent;
254
255         /* TODO */
256
257         return bytes_sent;
258 }
259
260 /*
261  * Receive a message as described by MESSAGE from socket FD.
262  * Returns the number of bytes read or -1 for errors.
263  *
264  * This function is a cancellation point and therefore not marked with
265  * __THROW.
266  */
267 ssize_t sw_recvmsg(int __fd, struct msghdr *__message, int __flags)
268 {
269         ssize_t bytes_recv;
270
271         /* TODO */
272
273         return bytes_recv;
274 }
275
276 /*
277  * Put the current value for socket FD's option OPTNAME at protocol level
278  * LEVEL into OPTVAL (which is *OPTLEN bytes long), and set *OPTLEN to the
279  * value's actual length.  Returns 0 on success, -1 for errors.
280  */
281 int sw_getsockopt(int __fd, int __level, int __optname,
282                        void *__restrict __optval,
283                        socklen_t *__restrict __optlen)
284 {
285         /* Call classical interface of getsockopt(2). */
286         return getsockopt(__fd, __level, __optname, __optval, __optlen);
287 }
288
289 /*
290  * Set socket FD's option OPTNAME at protocol level LEVEL
291  * to *OPTVAL (which is OPTLEN bytes long).
292  * Returns 0 on success, -1 for errors.
293  */
294
295 int sw_setsockopt(int __fd, int __level, int __optname,
296                        __const void *__optval, socklen_t __optlen)
297 {
298         /* Call classical interface of setsockopt(2). */
299         return setsockopt(__fd, __level, __optname, __optval, __optlen);
300 }
301
302 /*
303  * Shut down all or part of the connection open on socket FD.
304  * HOW determines what to shut down:
305  *   SHUT_RD   = No more receptions;
306  *   SHUT_WR   = No more transmissions;
307  *   SHUT_RDWR = No more receptions or transmissions.
308  * Returns 0 on success, -1 for errors.
309  */
310 int sw_shutdown(int __fd, int __how)
311 {
312         struct sock_list *list;
313
314         /* Find socket in management structure. */
315         list = list_elem_from_socket(__fd);
316         if (list == NULL) {
317                 errno = EBADF;
318                 goto list_elem_err;
319         }
320
321         /* Check and update socket state. */
322         if (__how == STATE_SHUT_RDWR)
323                 list->rw_state = STATE_SHUT_RDWR;
324         else if (__how == STATE_SHUT_WR) {
325                 if (list->rw_state == STATE_SHUT_RD)
326                         list->rw_state = STATE_SHUT_RDWR;
327                 else if (list->rw_state == STATE_SHUT_WR) {
328                         errno = ENOTCONN;
329                         goto not_conn_err;
330                 }
331         }
332         else if (__how == STATE_SHUT_RD) {
333                 if (list->rw_state == STATE_SHUT_WR)
334                         list->rw_state = STATE_SHUT_RDWR;
335                 else if (list->rw_state == STATE_SHUT_RD) {
336                         errno = ENOTCONN;
337                         goto not_conn_err;
338                 }
339         }
340
341         /* Remove socket from socket management structure. */
342         if (list->rw_state == STATE_SHUT_RDWR) {
343                 list = list_unlink_socket(__fd);
344                 if (list == NULL) {
345                         errno = EBADF;
346                         goto list_unlink_err;
347                 }
348         }
349
350         /* Call classical interface of shutdown(2). */
351         return shutdown(__fd, __how);
352
353 not_conn_err:
354 list_elem_err:
355 list_unlink_err:
356         return -1;
357 }
358
359 /*
360  * Close file descriptor for socket FD.
361  * Returns 0 on success, -1 for errors.
362  */
363 int sw_close(int __fd)
364 {
365         struct sock_list *list;
366
367         /* Remove socket from socket management structure. */
368         list = list_unlink_socket(__fd);
369         if (list == NULL) {
370                 errno = EBADF;
371                 goto list_unlink_err;
372         }
373
374         /* Call classical interface of close(2). */
375         return close(__fd);
376
377 list_unlink_err:
378         return -1;
379 }