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