73eb5f119268e34ade9f2e11acc078a84471283a
[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 /* socket management structure */
29 struct sock_list {
30         int s;
31         struct sockaddr_sw addr;
32         struct sock_list *next;
33         struct sock_list *prev;
34 };
35
36 static struct sock_list sock_list_head = {
37         .next = &sock_list_head,
38         .prev = &sock_list_head
39 };
40
41 /*
42  * Add new socket to list. Called by sw_socket "syscall".
43  */
44
45 static struct sock_list *list_add_socket(int s)
46 {
47         struct sock_list *ptr = malloc(sizeof(*ptr));
48         if (ptr == NULL)
49                 return NULL;
50
51         ptr->next = &sock_list_head;
52         ptr->prev = sock_list_head.prev;
53         sock_list_head.prev->next = ptr;
54         sock_list_head.prev = ptr;
55         ptr->s = s;
56
57         return ptr;
58 }
59
60 /*
61  * Bind socket to given address. Called by sw_bind "syscall".
62  */
63
64 static struct sock_list *list_update_socket_address(int s, __CONST_SOCKADDR_ARG addr)
65 {
66         struct sock_list *ptr;
67
68         for (ptr = sock_list_head.next; ptr != &sock_list_head; ptr = ptr->next)
69                 if (ptr->s == s) {
70                         memcpy(&ptr->addr, addr, sizeof(ptr->addr));
71                         return ptr;
72                 }
73
74         return NULL;
75 }
76
77 /*
78  * Get list element containing socket s. Called by sw_send* "syscalls".
79  */
80
81 static struct sock_list *list_elem_from_socket(int s)
82 {
83         struct sock_list *ptr;
84
85         for (ptr = sock_list_head.next; ptr != &sock_list_head; ptr = ptr->next)
86                 if (ptr->s == s) {
87                         return ptr;
88                 }
89
90         return NULL;
91 }
92
93 /*
94  * Get list element containing address addr. Called by sw_bind "syscall".
95  */
96
97 static struct sock_list *list_elem_from_address(__CONST_SOCKADDR_ARG addr)
98 {
99         struct sock_list *ptr;
100
101         for (ptr = sock_list_head.next; ptr != &sock_list_head; ptr = ptr->next)
102                 if (memcmp(&ptr->addr, addr, sizeof(addr)) == 0)
103                         return ptr;
104
105         return NULL;
106 }
107
108 /*
109  * Remove socket from list. Called by sw_close "syscall".
110  */
111
112 static struct sock_list *list_unlink_socket(int s)
113 {
114         struct sock_list *ptr;
115
116         for (ptr = sock_list_head.next; ptr != &sock_list_head; ptr = ptr->next)
117                 if (ptr->s == s) {
118                         ptr->next->prev = ptr->prev;
119                         ptr->prev->next = ptr->next;
120                         ptr->next = ptr;
121                         ptr->prev = ptr;
122                         return ptr;
123                 }
124
125         return NULL;
126 }
127
128 /*
129  * Create a new socket of type TYPE in domain DOMAIN, using
130  * protocol PROTOCOL.  If PROTOCOL is zero, one is chosen automatically.
131  * Returns a file descriptor for the new socket, or -1 for errors.
132  *
133  * swift: PROTOCOL is IPPROTO_SWIFT. Ignore TYPE.
134  */
135 int sw_socket (int __domain, int __type, int __protocol)
136 {
137         int s;
138         struct sock_list *list;
139
140         s = socket(__domain, SOCK_RAW, IPPROTO_SWIFT);
141         if (s < 0)
142                 goto sock_err;
143
144         list = list_add_socket(s);
145         if (list == NULL)
146                 goto list_add_err;
147
148         return s;
149
150 list_add_err:
151         close(s);
152 sock_err:
153         return -1;
154 }
155
156 /*
157  * Give the socket FD the local address ADDR (which is LEN bytes long).
158  *
159  * swift: ADDR is of type struct sockaddr_sw.
160  */
161 int sw_bind (int __fd, __CONST_SOCKADDR_ARG __addr, socklen_t __len)
162 {
163         struct sock_list *list;
164
165         /* Check whether address is already in use. */
166         list = list_elem_from_address(__addr);
167         if (list != NULL) {
168                 errno = EADDRINUSE;
169                 goto list_elem_err;
170         }
171         /* Update __fd entry in socket management list. */
172         list = list_update_socket_address(__fd, __addr);
173         if (list == NULL) {
174                 errno = EBADF;
175                 goto list_update_err;
176         }
177
178         return 0;
179
180 list_update_err:
181 list_elem_err:
182         return -1;
183 }
184
185 /* Put the local address of FD into *ADDR and its length in *LEN.  */
186 int sw_getsockname (int __fd, __SOCKADDR_ARG __addr,
187                         socklen_t *__restrict __len)
188 {
189         /* TODO */
190
191         return 0;
192 }
193
194 /*
195  * Send N bytes of BUF on socket FD to peer at address ADDR (which is
196  * ADDR_LEN bytes long).  Returns the number sent, or -1 for errors.
197  *
198  * This function is a cancellation point and therefore not marked with
199  * __THROW.
200  */
201 ssize_t sw_sendto (int __fd, __const void *__buf, size_t __n,
202                        int __flags, __CONST_SOCKADDR_ARG __addr,
203                        socklen_t __addr_len)
204 {
205         ssize_t bytes_sent;
206
207         /* TODO */
208
209         return bytes_sent;
210 }
211
212 /*
213  * Read N bytes into BUF through socket FD.
214  * If ADDR is not NULL, fill in *ADDR_LEN bytes of it with tha address of
215  * the sender, and store the actual size of the address in *ADDR_LEN.
216  * Returns the number of bytes read or -1 for errors.
217  *
218  * This function is a cancellation point and therefore not marked with
219  * __THROW.
220  */
221 ssize_t sw_recvfrom (int __fd, void *__restrict __buf, size_t __n,
222                          int __flags, __SOCKADDR_ARG __addr,
223                          socklen_t *__restrict __addr_len)
224 {
225         ssize_t bytes_recv;
226
227         /* TODO */
228
229         return bytes_recv;
230 }
231
232 /*
233  * Send a message described MESSAGE on socket FD.
234  * Returns the number of bytes sent, or -1 for errors.
235  *
236  * This function is a cancellation point and therefore not marked with
237  * __THROW.
238  */
239 ssize_t sw_sendmsg (int __fd, __const struct msghdr *__message,
240                         int __flags)
241 {
242         ssize_t bytes_sent;
243
244         /* TODO */
245
246         return bytes_sent;
247 }
248
249 /*
250  * Receive a message as described by MESSAGE from socket FD.
251  * Returns the number of bytes read or -1 for errors.
252  *
253  * This function is a cancellation point and therefore not marked with
254  * __THROW.
255  */
256 ssize_t sw_recvmsg (int __fd, struct msghdr *__message, int __flags)
257 {
258         ssize_t bytes_recv;
259
260         /* TODO */
261
262         return bytes_recv;
263 }
264
265 /*
266  * Put the current value for socket FD's option OPTNAME at protocol level
267  * LEVEL into OPTVAL (which is *OPTLEN bytes long), and set *OPTLEN to the
268  * value's actual length.  Returns 0 on success, -1 for errors.
269  */
270 int sw_getsockopt (int __fd, int __level, int __optname,
271                        void *__restrict __optval,
272                        socklen_t *__restrict __optlen)
273 {
274         /* Call classical interface of getsockopt(2). */
275         return getsockopt(__fd, __level, __optname, __optval, __optlen);
276 }
277
278 /*
279  * Set socket FD's option OPTNAME at protocol level LEVEL
280  * to *OPTVAL (which is OPTLEN bytes long).
281  * Returns 0 on success, -1 for errors.
282  */
283
284 int sw_setsockopt (int __fd, int __level, int __optname,
285                        __const void *__optval, socklen_t __optlen)
286 {
287         /* Call classical interface of setsockopt(2). */
288         return setsockopt(__fd, __level, __optname, __optval, __optlen);
289 }
290
291 /*
292  * Shut down all or part of the connection open on socket FD.
293  * HOW determines what to shut down:
294  *   SHUT_RD   = No more receptions;
295  *   SHUT_WR   = No more transmissions;
296  *   SHUT_RDWR = No more receptions or transmissions.
297  * Returns 0 on success, -1 for errors.
298  */
299 int sw_shutdown (int __fd, int __how)
300 {
301         /* Call classical interface of shutdown(2). */
302         return shutdown(__fd, __how);
303 }