e88a973c4b3ee701aa6cba02375eec19a3717568
[swifty.git] / src / 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_raw.h"
24
25 /*
26  * Create a new socket of type TYPE in domain DOMAIN, using
27  * protocol PROTOCOL.  If PROTOCOL is zero, one is chosen automatically.
28  * Returns a file descriptor for the new socket, or -1 for errors.
29  *
30  * swift: protocol is IPPROTO_SWIFT.
31  */
32 int sw_socket (int __domain, int __type, int __protocol)
33 {
34         int s;
35
36         /* TODO */
37
38         return s;
39 }
40
41 /*
42  * Give the socket FD the local address ADDR (which is LEN bytes long).
43  *
44  * swift: ADDR is of type struct sockaddr_sw.
45  */
46 int sw_bind (int __fd, __CONST_SOCKADDR_ARG __addr, socklen_t __len)
47 {
48         /* TODO */
49
50         return 0;
51 }
52
53 /* Put the local address of FD into *ADDR and its length in *LEN.  */
54 int sw_getsockname (int __fd, __SOCKADDR_ARG __addr,
55                         socklen_t *__restrict __len)
56 {
57         /* TODO */
58
59         return 0;
60 }
61
62 /*
63  * Send N bytes of BUF on socket FD to peer at address ADDR (which is
64  * ADDR_LEN bytes long).  Returns the number sent, or -1 for errors.
65  *
66  * This function is a cancellation point and therefore not marked with
67  * __THROW.
68  */
69 ssize_t sw_sendto (int __fd, __const void *__buf, size_t __n,
70                        int __flags, __CONST_SOCKADDR_ARG __addr,
71                        socklen_t __addr_len)
72 {
73         ssize_t bytes_sent;
74
75         /* TODO */
76
77         return bytes_sent;
78 }
79
80 /*
81  * Read N bytes into BUF through socket FD.
82  * If ADDR is not NULL, fill in *ADDR_LEN bytes of it with tha address of
83  * the sender, and store the actual size of the address in *ADDR_LEN.
84  * Returns the number of bytes read or -1 for errors.
85  *
86  * This function is a cancellation point and therefore not marked with
87  * __THROW.
88  */
89 ssize_t sw_recvfrom (int __fd, void *__restrict __buf, size_t __n,
90                          int __flags, __SOCKADDR_ARG __addr,
91                          socklen_t *__restrict __addr_len)
92 {
93         ssize_t bytes_recv;
94
95         /* TODO */
96
97         return bytes_recv;
98 }
99
100 /*
101  * Send a message described MESSAGE on socket FD.
102  * Returns the number of bytes sent, or -1 for errors.
103  *
104  * This function is a cancellation point and therefore not marked with
105  * __THROW.
106  */
107 ssize_t sw_sendmsg (int __fd, __const struct msghdr *__message,
108                         int __flags)
109 {
110         ssize_t bytes_sent;
111
112         /* TODO */
113
114         return bytes_sent;
115 }
116
117 /*
118  * Receive a message as described by MESSAGE from socket FD.
119  * Returns the number of bytes read or -1 for errors.
120  *
121  * This function is a cancellation point and therefore not marked with
122  * __THROW.
123  */
124 ssize_t sw_recvmsg (int __fd, struct msghdr *__message, int __flags)
125 {
126         ssize_t bytes_recv;
127
128         /* TODO */
129
130         return bytes_recv;
131 }
132
133 /*
134  * Put the current value for socket FD's option OPTNAME at protocol level
135  * LEVEL into OPTVAL (which is *OPTLEN bytes long), and set *OPTLEN to the
136  * value's actual length.  Returns 0 on success, -1 for errors.
137  */
138 int sw_getsockopt (int __fd, int __level, int __optname,
139                        void *__restrict __optval,
140                        socklen_t *__restrict __optlen)
141 {
142         /* Call classical interface of getsockopt(2). */
143         return getsockopt(__fd, __level, __optname, __optval, __optlen);
144 }
145
146 /*
147  * Set socket FD's option OPTNAME at protocol level LEVEL
148  * to *OPTVAL (which is OPTLEN bytes long).
149  * Returns 0 on success, -1 for errors.
150  */
151
152 int sw_setsockopt (int __fd, int __level, int __optname,
153                        __const void *__optval, socklen_t __optlen)
154 {
155         /* Call classical interface of setsockopt(2). */
156         return setsockopt(__fd, __level, __optname, __optval, __optlen);
157 }
158
159 /*
160  * Shut down all or part of the connection open on socket FD.
161  * HOW determines what to shut down:
162  *   SHUT_RD   = No more receptions;
163  *   SHUT_WR   = No more transmissions;
164  *   SHUT_RDWR = No more receptions or transmissions.
165  * Returns 0 on success, -1 for errors.
166  */
167 int sw_shutdown (int __fd, int __how)
168 {
169         /* Call classical interface of shutdown(2). */
170         return shutdown(__fd, __how);
171 }