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>
25 #include "include/swift_types.h"
26 #include "include/swift_raw.h"
27 #include "include/swift_list.h"
30 * Create a new socket of type TYPE in domain DOMAIN, using
31 * protocol PROTOCOL. If PROTOCOL is zero, one is chosen automatically.
32 * Returns a file descriptor for the new socket, or -1 for errors.
34 * swift: PROTOCOL is IPPROTO_SWIFT. Ignore TYPE.
36 int sw_socket(int __domain, int __type, int __protocol)
39 struct sock_list *list;
41 if (__domain != PF_INET || __type != SOCK_DGRAM || __protocol != IPPROTO_SWIFT) {
46 s = socket(PF_INET, SOCK_RAW, IPPROTO_SWIFT);
51 list = list_add_socket(s);
57 /* Socket is not bound. */
58 list->bind_state = STATE_NOTBOUND;
69 * Give the socket FD the local address ADDR (which is LEN bytes long).
71 * swift: ADDR is of type struct sockaddr_sw.
73 int sw_bind(int __fd, __CONST_SOCKADDR_ARG __addr, socklen_t __len)
75 struct sock_list *list;
78 rc = list_socket_is_bound(__fd);
81 goto socket_bound_err;
84 /* Check whether address is already in use. */
85 list = list_elem_from_address(__addr);
91 /* Update __fd entry in socket management list. */
92 list = list_update_socket_address(__fd, __addr);
106 /* Put the local address of FD into *ADDR and its length in *LEN. */
107 int sw_getsockname(int __fd, __SOCKADDR_ARG __addr,
108 socklen_t *__restrict __len)
110 struct sock_list *list;
112 /* Find socket in management structure. */
113 list = list_elem_from_socket(__fd);
119 memcpy(__addr, &list->addr, sizeof(list->addr));
120 *__len = sizeof(list->addr);
129 * Send N bytes of BUF on socket FD to peer at address ADDR (which is
130 * ADDR_LEN bytes long). Returns the number sent, or -1 for errors.
132 * This function is a cancellation point and therefore not marked with
135 ssize_t sw_sendto(int __fd, __const void *__buf, size_t __n,
136 int __flags, __CONST_SOCKADDR_ARG __addr,
137 socklen_t __addr_len)
140 struct sock_list *list;
141 struct iovec __iov[1];
142 struct msghdr __msgh;
143 struct sockaddr_sw *__sw_addr = (struct sockaddr_sw *) __addr;
145 list = list_elem_from_socket(__fd);
146 if (list != NULL && list->bind_state == STATE_NOTBOUND) {
147 errno = EAFNOSUPPORT;
152 char str[INET_ADDRSTRLEN];
154 inet_ntop(AF_INET, &(__sw_addr->sin_addr), str, INET_ADDRSTRLEN);
155 printf("=== ADDR: %s ===\n", str);
158 /* Specify the components of the message in an "iovec". */
159 __iov[0].iov_base = (void *) __buf;
160 __iov[0].iov_len = __n;
162 /* The message header contains parameters for sendmsg. */
163 __msgh.msg_name = (caddr_t) __addr;
164 __msgh.msg_namelen = sizeof(__addr);
165 __msgh.msg_iov = __iov;
166 __msgh.msg_iovlen = 1;
167 __msgh.msg_control = NULL; /* irrelevant to AF_INET */
168 __msgh.msg_controllen = 0; /* irrelevant to AF_INET */
170 return sendmsg(__fd, &__msgh, 0);
177 * Read N bytes into BUF through socket FD.
178 * If ADDR is not NULL, fill in *ADDR_LEN bytes of it with tha address of
179 * the sender, and store the actual size of the address in *ADDR_LEN.
180 * Returns the number of bytes read or -1 for errors.
182 * This function is a cancellation point and therefore not marked with
185 ssize_t sw_recvfrom(int __fd, void *__restrict __buf, size_t __n,
186 int __flags, __SOCKADDR_ARG __addr,
187 socklen_t *__restrict __addr_len)
190 struct sock_list *list;
191 struct iovec __iov[1];
192 struct msghdr __msgh;
193 struct sockaddr_sw *__sw_addr = (struct sockaddr_sw *) __addr;
195 list = list_elem_from_socket(__fd);
196 if (list != NULL && list->bind_state == STATE_NOTBOUND) {
197 errno = EAFNOSUPPORT;
203 return recvmsg(__fd, &__msgh, 0);
210 * Send a message described MESSAGE on socket FD.
211 * Returns the number of bytes sent, or -1 for errors.
213 * This function is a cancellation point and therefore not marked with
216 ssize_t sw_sendmsg(int __fd, __const struct msghdr *__message,
223 return sendmsg(__fd, __message, __flags);
227 * Receive a message as described by MESSAGE from socket FD.
228 * Returns the number of bytes read or -1 for errors.
230 * This function is a cancellation point and therefore not marked with
233 ssize_t sw_recvmsg(int __fd, struct msghdr *__message, int __flags)
239 return recvmsg(__fd, __message, __flags);
243 * Put the current value for socket FD's option OPTNAME at protocol level
244 * LEVEL into OPTVAL (which is *OPTLEN bytes long), and set *OPTLEN to the
245 * value's actual length. Returns 0 on success, -1 for errors.
247 int sw_getsockopt(int __fd, int __level, int __optname,
248 void *__restrict __optval,
249 socklen_t *__restrict __optlen)
251 /* Call classical interface of getsockopt(2). */
252 return getsockopt(__fd, __level, __optname, __optval, __optlen);
256 * Set socket FD's option OPTNAME at protocol level LEVEL
257 * to *OPTVAL (which is OPTLEN bytes long).
258 * Returns 0 on success, -1 for errors.
260 int sw_setsockopt(int __fd, int __level, int __optname,
261 __const void *__optval, socklen_t __optlen)
263 /* Call classical interface of setsockopt(2). */
264 return setsockopt(__fd, __level, __optname, __optval, __optlen);
268 * Close file descriptor for socket FD.
269 * Returns 0 on success, -1 for errors.
271 int sw_close(int __fd)
275 /* Remove socket from socket management structure. */
276 rc = list_remove_socket(__fd);
279 goto list_unlink_err;
282 /* Call classical interface of close(2). */