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