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