aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorToni Uhlig <matzeton@googlemail.com>2018-05-23 19:28:09 +0200
committerToni Uhlig <matzeton@googlemail.com>2018-05-23 19:28:09 +0200
commitd8a545695458635a45bf179413e8e3824f76cba0 (patch)
tree63c14cdd0d404e4c5d67add1e97c56e9992833e7
parent52a8693b242f6ef1f0c3fa9c7d008728fcfb7f75 (diff)
POTD skeleton #78.
Signed-off-by: Toni Uhlig <matzeton@googlemail.com>
-rw-r--r--src/pevent.c17
-rw-r--r--src/pseccomp.c2
-rw-r--r--src/redirector.c17
-rw-r--r--src/redirector.h2
-rw-r--r--src/socket.c7
5 files changed, 38 insertions, 7 deletions
diff --git a/src/pevent.c b/src/pevent.c
index 3007472..60a954d 100644
--- a/src/pevent.c
+++ b/src/pevent.c
@@ -85,20 +85,23 @@ int event_loop(event_ctx *ctx, on_event_cb on_event, void *user_data)
n = epoll_pwait(ctx->epoll_fd, ctx->events, POTD_MAXEVENTS, -1, &eset);
if (errno == EINTR)
continue;
- if (n < 0)
+ if (n < 0) {
+ ctx->active = 0;
break;
+ }
for (i = 0; i < n; ++i) {
+ ctx->current_event = i;
+
if ((ctx->events[i].events & EPOLLERR) ||
(ctx->events[i].events & EPOLLHUP) ||
+ (ctx->events[i].events & EPOLLRDHUP) ||
(!(ctx->events[i].events & EPOLLIN)))
{
E_STRERR("Event epoll for descriptor %d",
ctx->events[i].data.fd);
ctx->active = 0;
- break;
} else {
- ctx->current_event = i;
if (!on_event(ctx, ctx->events[i].data.fd, user_data))
W2("Event callback failed: [fd: %d , npoll: %d]",
ctx->events[i].data.fd, n);
@@ -139,7 +142,6 @@ event_forward_connection(event_ctx *ctx, int dest_fd, on_data_cb on_data,
siz = read(ev->data.fd, &buf[0], BUFSIZ);
saved_errno = errno;
} else break;
-
if (saved_errno == EAGAIN)
break;
@@ -168,6 +170,7 @@ event_forward_connection(event_ctx *ctx, int dest_fd, on_data_cb on_data,
}
if (has_input) {
+ errno = 0;
siz = write(dest_fd, &buf[0], siz);
switch (siz) {
@@ -189,5 +192,11 @@ event_forward_connection(event_ctx *ctx, int dest_fd, on_data_cb on_data,
}
D2("Connection state: %d", rc);
+ if (rc != CON_OK) {
+ if (shutdown(ev->data.fd, SHUT_RDWR))
+ E_STRERR("Shutdown source socket fd %d", ev->data.fd);
+ if (shutdown(dest_fd, SHUT_RDWR))
+ E_STRERR("Shutdown dest socket fd %d", dest_fd);
+ }
return rc;
}
diff --git a/src/pseccomp.c b/src/pseccomp.c
index 2660e2a..376bbc6 100644
--- a/src/pseccomp.c
+++ b/src/pseccomp.c
@@ -24,7 +24,7 @@ static const int default_allowed_syscalls[] = {
SCMP_SYS(close), SCMP_SYS(wait4),
SCMP_SYS(sigprocmask), SCMP_SYS(tgkill),
SCMP_SYS(clone), SCMP_SYS(execve),
- SCMP_SYS(socket), SCMP_SYS(bind), SCMP_SYS(setsockopt),
+ SCMP_SYS(socket), SCMP_SYS(bind), SCMP_SYS(setsockopt), SCMP_SYS(shutdown),
SCMP_SYS(listen), SCMP_SYS(connect), SCMP_SYS(getsockname),
SCMP_SYS(accept), SCMP_SYS(sendto), SCMP_SYS(recvmsg), SCMP_SYS(recvfrom),
SCMP_SYS(epoll_create1), SCMP_SYS(epoll_ctl), SCMP_SYS(epoll_pwait),
diff --git a/src/redirector.c b/src/redirector.c
index b6b95db..7e589f7 100644
--- a/src/redirector.c
+++ b/src/redirector.c
@@ -57,6 +57,16 @@ int redirector_init_ctx(redirector_ctx **ctx)
return 0;
}
+void redirector_free_ctx(redirector_ctx **rdr_ctx)
+{
+ assert(rdr_ctx && *rdr_ctx);
+
+ socket_close(&(*rdr_ctx)->fwd_ctx.sock);
+ socket_close(&(*rdr_ctx)->sock);
+ free(*rdr_ctx);
+ (*rdr_ctx) = NULL;
+}
+
int redirector_setup(redirector_ctx *ctx,
const char *listen_addr, const char *listen_port,
const char *host, const char *port)
@@ -168,6 +178,8 @@ pid_t redirector_daemonize(event_ctx *ev_ctx, redirector_ctx *rdr_ctx[], size_t
break;
}
D2("Server daemon pid: %d", p);
+ for (i = 0; i < siz; ++i)
+ redirector_free_ctx(&rdr_ctx[i]);
return p;
}
@@ -335,7 +347,10 @@ client_mainloop(void *arg)
ev_cli.client_args = args;
ev_cli.fwd_sock = &fwd;
- event_loop(ev_ctx, client_io, &ev_cli);
+ if (event_loop(ev_ctx, client_io, &ev_cli))
+ E_STRERR("Forward connection data to %s:%s forward fd %d",
+ args->rdr_ctx->fwd_ctx.host_buf,
+ args->rdr_ctx->fwd_ctx.service_buf, fwd.fd);
finish:
event_free(&ev_ctx);
diff --git a/src/redirector.h b/src/redirector.h
index a076f77..d63c8ba 100644
--- a/src/redirector.h
+++ b/src/redirector.h
@@ -15,6 +15,8 @@ typedef struct redirector_ctx {
int redirector_init_ctx(redirector_ctx **rdr_ctx);
+void redirector_free_ctx(redirector_ctx **rdr_ctx);
+
int redirector_setup(redirector_ctx *rdr_ctx,
const char *listen_addr, const char *listen_port,
const char *host, const char *port);
diff --git a/src/socket.c b/src/socket.c
index 1af8e7c..6713d10 100644
--- a/src/socket.c
+++ b/src/socket.c
@@ -123,12 +123,17 @@ int socket_accept_in(const psocket *psock, psocket *client_psock)
&client_psock->addr_len);
if (fd < 0)
return 1;
- if (socket_setopts(fd) || socket_nonblock(psock))
+ if (socket_setopts(fd))
{
close(fd);
return 1;
}
+
client_psock->fd = fd;
+ if (socket_nonblock(client_psock)) {
+ socket_close(client_psock);
+ return 1;
+ }
return 0;
}