2 * swift implementation of syscall API
4 * Simulates the classic socket syscalls (socket, bind, send, recv).
5 * Implementation uses raw sockets (AF_INET, SOCK_RAW).
7 * Subsequently, implementation is to be ported into kernel space and
8 * the interface is going to be offered by the Linux syscall API.
10 * Heavily inspired by GLIBC's <sys/socket.h>
11 * (/usr/include/sys/socket.h).
13 * 2011, Razvan Deaconescu, razvan.deaconescu@cs.pub.ro
19 #include <sys/types.h>
20 #include <sys/socket.h>
21 #include <netinet/in.h>
22 #include <arpa/inet.h>
26 #include "swift_types.h"
27 #include "swift_raw.h"
28 #include "swift_list.h"
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.
37 * swift: PROTOCOL is IPPROTO_SWIFT. Ignore TYPE.
39 int sw_socket(int __domain, int __type, int __protocol)
42 struct sock_list *list;
44 if (__domain != PF_INET || __type != SOCK_DGRAM || __protocol != IPPROTO_SWIFT) {
49 s = socket(PF_INET, SOCK_RAW, IPPROTO_SWIFT);
54 list = list_add_socket(s);
60 /* Socket is not bound. */
61 list->bind_state = STATE_NOTBOUND;
72 * Give the socket FD the local address ADDR (which is LEN bytes long).
74 * swift: ADDR is of type struct sockaddr_sw.
76 int sw_bind(int __fd, __CONST_SOCKADDR_ARG __addr, socklen_t __len)
78 struct sock_list *list;
81 rc = list_socket_is_bound(__fd);
84 goto socket_bound_err;
87 /* Check whether address is already in use. */
88 list = list_elem_from_address(__addr);
94 /* Update __fd entry in socket management list. */
95 list = list_update_socket_address(__fd, __addr);
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)
113 struct sock_list *list;
115 /* Find socket in management structure. */
116 list = list_elem_from_socket(__fd);
122 memcpy(__addr, &list->addr, sizeof(list->addr));
123 *__len = sizeof(list->addr);
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.
135 * This function is a cancellation point and therefore not marked with
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)
143 struct sock_list *list;
144 struct iovec __iov[1];
145 struct msghdr __msgh;
146 struct sockaddr_sw *__sw_addr = (struct sockaddr_sw *) __addr;
148 list = list_elem_from_socket(__fd);
149 if (list != NULL && list->bind_state == STATE_NOTBOUND) {
150 errno = EAFNOSUPPORT;
155 char str[INET_ADDRSTRLEN];
157 inet_ntop(AF_INET, &(__sw_addr->sin_addr), str, INET_ADDRSTRLEN);
158 printf("=== ADDR: %s ===\n", str);
161 /* Specify the components of the message in an "iovec". */
162 __iov[0].iov_base = (void *) __buf;
163 __iov[0].iov_len = __n;
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 */
173 return sendmsg(__fd, &__msgh, 0);
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.
185 * This function is a cancellation point and therefore not marked with
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)
193 struct sock_list *list;
194 struct iovec __iov[1];
195 struct msghdr __msgh;
196 struct sockaddr_sw *__sw_addr = (struct sockaddr_sw *) __addr;
198 list = list_elem_from_socket(__fd);
199 if (list != NULL && list->bind_state == STATE_NOTBOUND) {
200 errno = EAFNOSUPPORT;
206 return recvmsg(__fd, &__msgh, 0);
213 * Send a message described MESSAGE on socket FD.
214 * Returns the number of bytes sent, or -1 for errors.
216 * This function is a cancellation point and therefore not marked with
219 ssize_t sw_sendmsg(int __fd, __const struct msghdr *__message,
226 return sendmsg(__fd, __message, __flags);
230 * Receive a message as described by MESSAGE from socket FD.
231 * Returns the number of bytes read or -1 for errors.
233 * This function is a cancellation point and therefore not marked with
236 ssize_t sw_recvmsg(int __fd, struct msghdr *__message, int __flags)
242 return recvmsg(__fd, __message, __flags);
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.
250 int sw_getsockopt(int __fd, int __level, int __optname,
251 void *__restrict __optval,
252 socklen_t *__restrict __optlen)
254 /* Call classical interface of getsockopt(2). */
255 return getsockopt(__fd, __level, __optname, __optval, __optlen);
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.
263 int sw_setsockopt(int __fd, int __level, int __optname,
264 __const void *__optval, socklen_t __optlen)
266 /* Call classical interface of setsockopt(2). */
267 return setsockopt(__fd, __level, __optname, __optval, __optlen);
271 * Close file descriptor for socket FD.
272 * Returns 0 on success, -1 for errors.
274 int sw_close(int __fd)
278 /* Remove socket from socket management structure. */
279 rc = list_remove_socket(__fd);
282 goto list_unlink_err;
285 /* Call classical interface of close(2). */