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