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