2 * Copyright 2016 Jakub Klama <jceel@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted providing that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
22 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
23 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 * POSSIBILITY OF SUCH DAMAGE.
34 #include <sys/types.h>
36 # include "../apple_endian.h"
38 # include <sys/endian.h>
40 #include <sys/socket.h>
41 #include <sys/event.h>
45 #include "../lib9p_impl.h"
49 struct l9p_socket_softc
51 struct l9p_connection *ls_conn;
52 struct sockaddr ls_sockaddr;
58 static int l9p_socket_readmsg(struct l9p_socket_softc *, void **, size_t *);
59 static int l9p_socket_get_response_buffer(struct l9p_request *,
60 struct iovec *, size_t *, void *);
61 static int l9p_socket_send_response(struct l9p_request *, const struct iovec *,
62 const size_t, const size_t, void *);
63 static void l9p_socket_drop_response(struct l9p_request *, const struct iovec *,
65 static void *l9p_socket_thread(void *);
66 static ssize_t xread(int, void *, size_t);
67 static ssize_t xwrite(int, void *, size_t);
70 l9p_start_server(struct l9p_server *server, const char *host, const char *port)
72 struct addrinfo *res, *res0, hints;
74 struct kevent event[2];
75 int err, kq, i, val, evs, nsockets = 0;
78 memset(&hints, 0, sizeof(hints));
79 hints.ai_family = PF_UNSPEC;
80 hints.ai_socktype = SOCK_STREAM;
81 err = getaddrinfo(host, port, &hints, &res0);
86 for (res = res0; res; res = res->ai_next) {
87 int s = socket(res->ai_family, res->ai_socktype,
91 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
96 if (bind(s, res->ai_addr, res->ai_addrlen) < 0) {
101 sockets[nsockets] = s;
102 EV_SET(&kev[nsockets++], s, EVFILT_READ, EV_ADD | EV_ENABLE, 0,
108 L9P_LOG(L9P_ERROR, "bind(): %s", strerror(errno));
114 if (kevent(kq, kev, nsockets, NULL, 0, NULL) < 0) {
115 L9P_LOG(L9P_ERROR, "kevent(): %s", strerror(errno));
120 evs = kevent(kq, NULL, 0, event, nsockets, NULL);
125 L9P_LOG(L9P_ERROR, "kevent(): %s", strerror(errno));
129 for (i = 0; i < evs; i++) {
130 struct sockaddr client_addr;
131 socklen_t client_addr_len = sizeof(client_addr);
132 int news = accept((int)event[i].ident, &client_addr,
136 L9P_LOG(L9P_WARNING, "accept(): %s",
141 l9p_socket_accept(server, news, &client_addr,
149 l9p_socket_accept(struct l9p_server *server, int conn_fd,
150 struct sockaddr *client_addr, socklen_t client_addr_len)
152 struct l9p_socket_softc *sc;
153 struct l9p_connection *conn;
154 char host[NI_MAXHOST + 1];
155 char serv[NI_MAXSERV + 1];
158 err = getnameinfo(client_addr, client_addr_len, host, NI_MAXHOST, serv,
159 NI_MAXSERV, NI_NUMERICHOST | NI_NUMERICSERV);
162 L9P_LOG(L9P_WARNING, "cannot look up client name: %s",
165 L9P_LOG(L9P_INFO, "new connection from %s:%s", host, serv);
168 if (l9p_connection_init(server, &conn) != 0) {
169 L9P_LOG(L9P_ERROR, "cannot create new connection");
173 sc = l9p_calloc(1, sizeof(*sc));
178 * Fill in transport handler functions and aux argument.
180 conn->lc_lt.lt_aux = sc;
181 conn->lc_lt.lt_get_response_buffer = l9p_socket_get_response_buffer;
182 conn->lc_lt.lt_send_response = l9p_socket_send_response;
183 conn->lc_lt.lt_drop_response = l9p_socket_drop_response;
185 err = pthread_create(&sc->ls_thread, NULL, l9p_socket_thread, sc);
188 "pthread_create (for connection from %s:%s): error %s",
189 host, serv, strerror(err));
190 l9p_connection_close(sc->ls_conn);
196 l9p_socket_thread(void *arg)
198 struct l9p_socket_softc *sc = (struct l9p_socket_softc *)arg;
204 if (l9p_socket_readmsg(sc, &buf, &length) != 0)
208 iov.iov_len = length;
209 l9p_connection_recv(sc->ls_conn, &iov, 1, NULL);
213 L9P_LOG(L9P_INFO, "connection closed");
214 l9p_connection_close(sc->ls_conn);
220 l9p_socket_readmsg(struct l9p_socket_softc *sc, void **buf, size_t *size)
230 buffer = l9p_malloc(sizeof(uint32_t));
232 ret = xread(fd, buffer, sizeof(uint32_t));
234 L9P_LOG(L9P_ERROR, "read(): %s", strerror(errno));
238 if (ret != sizeof(uint32_t)) {
240 L9P_LOG(L9P_DEBUG, "%p: EOF", (void *)sc->ls_conn);
243 "short read: %zd bytes of %zd expected",
244 ret, sizeof(uint32_t));
248 msize = le32toh(*(uint32_t *)buffer);
249 toread = msize - sizeof(uint32_t);
250 buffer = l9p_realloc(buffer, msize);
252 ret = xread(fd, (char *)buffer + sizeof(uint32_t), toread);
254 L9P_LOG(L9P_ERROR, "read(): %s", strerror(errno));
258 if (ret != (ssize_t)toread) {
259 L9P_LOG(L9P_ERROR, "short read: %zd bytes of %zd expected",
266 L9P_LOG(L9P_INFO, "%p: read complete message, buf=%p size=%d",
267 (void *)sc->ls_conn, buffer, msize);
273 l9p_socket_get_response_buffer(struct l9p_request *req, struct iovec *iov,
274 size_t *niovp, void *arg __unused)
276 size_t size = req->lr_conn->lc_msize;
279 buf = l9p_malloc(size);
280 iov[0].iov_base = buf;
281 iov[0].iov_len = size;
288 l9p_socket_send_response(struct l9p_request *req __unused,
289 const struct iovec *iov, const size_t niov __unused, const size_t iolen,
292 struct l9p_socket_softc *sc = (struct l9p_socket_softc *)arg;
294 assert(sc->ls_fd >= 0);
296 L9P_LOG(L9P_DEBUG, "%p: sending reply, buf=%p, size=%d", arg,
297 iov[0].iov_base, iolen);
299 if (xwrite(sc->ls_fd, iov[0].iov_base, iolen) != (int)iolen) {
300 L9P_LOG(L9P_ERROR, "short write: %s", strerror(errno));
304 free(iov[0].iov_base);
309 l9p_socket_drop_response(struct l9p_request *req __unused,
310 const struct iovec *iov, size_t niov __unused, void *arg __unused)
313 L9P_LOG(L9P_DEBUG, "%p: drop buf=%p", arg, iov[0].iov_base);
314 free(iov[0].iov_base);
318 xread(int fd, void *buf, size_t count)
323 while (done < count) {
324 ret = read(fd, (char *)buf + done, count - done);
333 return ((ssize_t)done);
338 return ((ssize_t)done);
342 xwrite(int fd, void *buf, size_t count)
347 while (done < count) {
348 ret = write(fd, (char *)buf + done, count - done);
357 return ((ssize_t)done);
362 return ((ssize_t)done);