12f8fb578588452c31c899659308210973168fce
[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 "include/swift_types.h"
26 #include "include/swift_raw.h"
27 #include "include/swift_list.h"
28
29 /*
30  * Create a new socket of type TYPE in domain DOMAIN, using
31  * protocol PROTOCOL.  If PROTOCOL is zero, one is chosen automatically.
32  * Returns a file descriptor for the new socket, or -1 for errors.
33  *
34  * swift: PROTOCOL is IPPROTO_SWIFT. Ignore TYPE.
35  */
36 int sw_socket(int __domain, int __type, int __protocol)
37 {
38         int s;
39         struct sock_list *list;
40
41         if (__domain != PF_INET || __type != SOCK_DGRAM || __protocol != IPPROTO_SWIFT) {
42                 errno = EINVAL;
43                 goto sock_err;
44         }
45
46         s = socket(PF_INET, SOCK_RAW, IPPROTO_SWIFT);
47         if (s < 0) {
48                 goto sock_err;
49         }
50
51         list = list_add_socket(s);
52         if (list == NULL) {
53                 errno = ENOMEM;
54                 goto list_add_err;
55         }
56
57         /* Socket is not bound. */
58         list->bind_state = STATE_NOTBOUND;
59
60         return s;
61
62 list_add_err:
63         close(s);
64 sock_err:
65         return -1;
66 }
67
68 /*
69  * Give the socket FD the local address ADDR (which is LEN bytes long).
70  *
71  * swift: ADDR is of type struct sockaddr_sw.
72  */
73 int sw_bind(int __fd, __CONST_SOCKADDR_ARG __addr, socklen_t __len)
74 {
75         struct sock_list *list;
76         int rc;
77
78         rc = list_socket_is_bound(__fd);
79         if (rc == 1) {
80                 errno = EINVAL;
81                 goto socket_bound_err;
82         }
83
84         /* Check whether address is already in use. */
85         list = list_elem_from_address(__addr);
86         if (list != NULL) {
87                 errno = EADDRINUSE;
88                 goto list_elem_err;
89         }
90
91         /* Update __fd entry in socket management list. */
92         list = list_update_socket_address(__fd, __addr);
93         if (list == NULL) {
94                 errno = EBADF;
95                 goto list_update_err;
96         }
97
98         return 0;
99
100 socket_bound_err:
101 list_update_err:
102 list_elem_err:
103         return -1;
104 }
105
106 /* Put the local address of FD into *ADDR and its length in *LEN.  */
107 int sw_getsockname(int __fd, __SOCKADDR_ARG __addr,
108                         socklen_t *__restrict __len)
109 {
110         struct sock_list *list;
111
112         /* Find socket in management structure. */
113         list = list_elem_from_socket(__fd);
114         if (list == NULL) {
115                 errno = EBADF;
116                 goto list_elem_err;
117         }
118
119         memcpy(__addr, &list->addr, sizeof(list->addr));
120         *__len = sizeof(list->addr);
121
122         return 0;
123
124 list_elem_err:
125         return -1;
126 }
127
128 /*
129  * Send N bytes of BUF on socket FD to peer at address ADDR (which is
130  * ADDR_LEN bytes long).  Returns the number sent, or -1 for errors.
131  *
132  * This function is a cancellation point and therefore not marked with
133  * __THROW.
134  */
135 ssize_t sw_sendto(int __fd, __const void *__buf, size_t __n,
136                        int __flags, __CONST_SOCKADDR_ARG __addr,
137                        socklen_t __addr_len)
138 {
139         ssize_t bytes_sent;
140         struct sock_list *list;
141         struct iovec __iov[1];
142         struct msghdr __msgh;
143         struct sockaddr_sw *__sw_addr = (struct sockaddr_sw *) __addr;
144
145         list = list_elem_from_socket(__fd);
146         if (list != NULL && list->bind_state == STATE_NOTBOUND) {
147                 errno = EAFNOSUPPORT;
148                 goto sock_err;
149         }
150  
151         printf("=== ADDR: %s ===\n", ntohl(__sw_addr->sin_addr.s_addr));
152
153         /* Specify the components of the message in an "iovec".   */
154         __iov[0].iov_base = (void *) __buf;
155         __iov[0].iov_len = __n;
156         
157         /* The message header contains parameters for sendmsg.    */
158         __msgh.msg_name = (caddr_t) __addr;
159         __msgh.msg_namelen = sizeof(__addr);
160         __msgh.msg_iov = __iov;
161         __msgh.msg_iovlen = 1;
162         __msgh.msg_control = NULL;            /* irrelevant to AF_INET */
163         __msgh.msg_controllen = 0;            /* irrelevant to AF_INET */
164
165         return sendmsg(__fd, &__msgh, 0);
166
167 sock_err:
168         return -1;
169 }
170
171 /*
172  * Read N bytes into BUF through socket FD.
173  * If ADDR is not NULL, fill in *ADDR_LEN bytes of it with tha address of
174  * the sender, and store the actual size of the address in *ADDR_LEN.
175  * Returns the number of bytes read or -1 for errors.
176  *
177  * This function is a cancellation point and therefore not marked with
178  * __THROW.
179  */
180 ssize_t sw_recvfrom(int __fd, void *__restrict __buf, size_t __n,
181                          int __flags, __SOCKADDR_ARG __addr,
182                          socklen_t *__restrict __addr_len)
183 {
184         ssize_t bytes_recv;
185
186         /* TODO */
187
188         return bytes_recv;
189 }
190
191 /*
192  * Send a message described MESSAGE on socket FD.
193  * Returns the number of bytes sent, or -1 for errors.
194  *
195  * This function is a cancellation point and therefore not marked with
196  * __THROW.
197  */
198 ssize_t sw_sendmsg(int __fd, __const struct msghdr *__message,
199                         int __flags)
200 {
201         ssize_t bytes_sent;
202
203         return sendmsg(__fd, __message, __flags);
204 }
205
206 /*
207  * Receive a message as described by MESSAGE from socket FD.
208  * Returns the number of bytes read or -1 for errors.
209  *
210  * This function is a cancellation point and therefore not marked with
211  * __THROW.
212  */
213 ssize_t sw_recvmsg(int __fd, struct msghdr *__message, int __flags)
214 {
215         ssize_t bytes_recv;
216
217         /* TODO */
218
219         return bytes_recv;
220 }
221
222 /*
223  * Put the current value for socket FD's option OPTNAME at protocol level
224  * LEVEL into OPTVAL (which is *OPTLEN bytes long), and set *OPTLEN to the
225  * value's actual length.  Returns 0 on success, -1 for errors.
226  */
227 int sw_getsockopt(int __fd, int __level, int __optname,
228                        void *__restrict __optval,
229                        socklen_t *__restrict __optlen)
230 {
231         /* Call classical interface of getsockopt(2). */
232         return getsockopt(__fd, __level, __optname, __optval, __optlen);
233 }
234
235 /*
236  * Set socket FD's option OPTNAME at protocol level LEVEL
237  * to *OPTVAL (which is OPTLEN bytes long).
238  * Returns 0 on success, -1 for errors.
239  */
240 int sw_setsockopt(int __fd, int __level, int __optname,
241                        __const void *__optval, socklen_t __optlen)
242 {
243         /* Call classical interface of setsockopt(2). */
244         return setsockopt(__fd, __level, __optname, __optval, __optlen);
245 }
246
247 /*
248  * Close file descriptor for socket FD.
249  * Returns 0 on success, -1 for errors.
250  */
251 int sw_close(int __fd)
252 {
253         int rc;
254
255         /* Remove socket from socket management structure. */
256         rc = list_remove_socket(__fd);
257         if (rc < 0) {
258                 errno = EBADF;
259                 goto list_unlink_err;
260         }
261
262         /* Call classical interface of close(2). */
263         return close(__fd);
264
265 list_unlink_err:
266         return -1;
267 }