summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--main.c9
-rw-r--r--tunnel.c31
-rw-r--r--tunnel.h3
3 files changed, 18 insertions, 25 deletions
diff --git a/main.c b/main.c
index e8b0026..800b807 100644
--- a/main.c
+++ b/main.c
@@ -34,9 +34,16 @@ int main(int argc, char *argv[])
if (argc == 1)
errx(EXIT_FAILURE, "bad usage");
+ if (!strcmp(argv[1], "server")) {
+ if (!tunnel_server())
+ errx(EXIT_FAILURE, "start server failed");
+
+ return 0;
+ }
+
int sock = tunnel_client();
if (sock < 0)
- errx(EXIT_FAILURE, "tunnel_client failed");
+ errx(EXIT_FAILURE, "init client failed");
sendcmd(sock, argc - 1, argv + 1);
diff --git a/tunnel.c b/tunnel.c
index 2e0718e..27b71ef 100644
--- a/tunnel.c
+++ b/tunnel.c
@@ -19,10 +19,14 @@ struct socket {
socklen_t salen;
};
+bool tunnel_daemon(int sock);
+
static bool socket_connect(struct socket *sock)
{
- if (connect(sock->fd, sock->sa, sock->salen) < 0)
+ if (connect(sock->fd, sock->sa, sock->salen) < 0) {
+ warn("connect");
return false;
+ }
return true;
}
@@ -101,41 +105,24 @@ bool tunnel_server(void)
defer { socket_free(sock); }
- if (! tunnel_listen(sock))
+ if (!tunnel_listen(sock))
return false;
- if (! tunnel_daemon(sock->fd))
+ if (!tunnel_daemon(sock->fd))
return false;
return true;
}
-bool tunnel_connect(struct socket *sock)
-{
- if (! socket_connect(sock)) {
- if (errno == ENOENT) {
- if (! tunnel_server())
- return false;
- }
-
- if (errno != ENOENT || ! socket_connect(sock)) {
- warn("connect");
- return false;
- }
- }
-
- return true;
-}
-
int tunnel_client(void)
{
struct socket *sock = tunnel_socket();
- if (! sock)
+ if (!sock)
return -1;
defer { socket_free(sock); }
- if (! tunnel_connect(sock))
+ if (!socket_connect(sock))
return -1;
return socket_detach_fd(sock);
diff --git a/tunnel.h b/tunnel.h
index a62f1b8..014a5e5 100644
--- a/tunnel.h
+++ b/tunnel.h
@@ -8,7 +8,6 @@
bool tunnel_alive(bool *alive);
int tunnel_client(void);
-
-bool tunnel_daemon(int sock);
+bool tunnel_server(void);
#endif