[PATCH 55/65] add support to connect to unix domain socket

From: Hiltjo Posthuma <hiltjo_AT_codemadness.org>
Date: Sun, 5 Jul 2015 22:43:11 +0200

useful for tunneling or providing a SSL connection to an IRC server and such
things (relayd, stunnel etc).
---
 ii.c | 52 +++++++++++++++++++++++++++++++++++++++++-----------
 1 file changed, 41 insertions(+), 11 deletions(-)
diff --git a/ii.c b/ii.c
index 231c5a6..8474f84 100644
--- a/ii.c
+++ b/ii.c
_AT_@ -1,4 +1,10 @@
 /* See LICENSE file for license details. */
+#include <sys/select.h>
+#include <sys/socket.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/un.h>
+
 #include <ctype.h>
 #include <errno.h>
 #include <fcntl.h>
_AT_@ -11,12 +17,9 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <sys/select.h>
-#include <sys/socket.h>
-#include <sys/stat.h>
-#include <sys/types.h>
 #include <time.h>
 #include <unistd.h>
+
 #include "util.h"
 
 #define IRC_CHANNEL_MAX   200
_AT_@ -64,6 +67,7 @@ static void      setup(void);
 static void      sighandler(int);
 static int       tcpopen(const char *, const char *);
 static size_t    tokenize(char **, size_t, char *, int);
+static int       udsopen(const char *);
 static void      usage(void);
 
 static int      isrunning = 1;
_AT_@ -77,9 +81,10 @@ static char     msg[IRC_MSG_MAX];  /* message buf used for communication */
 static void
 usage(void)
 {
-	eprintf("ii-" VERSION ", (c) 2005-2014 ii engineers, see LICENSE for details\n"
-		    "usage: %s <-s host> [-i <irc dir>] [-p <port>] "
-		    "[-n <nick>] [-k <password>] [-f <fullname>]\n", argv0);
+	eprintf("ii-" VERSION ", (c) 2005-2015 ii engineers, see LICENSE for details\n"
+	        "usage: %s <-s host> [-i <irc dir>] [-p <port>] "
+	        "[-u <unix-domain-socket>] [-n <nick>] [-k <password>] "
+	        "[-f <fullname>]\n", argv0);
 }
 
 static void *
_AT_@ -318,10 +323,31 @@ loginuser(int ircfd, const char *host, const char *fullname)
 {
 	snprintf(msg, sizeof(msg), "NICK %s\r\nUSER %s localhost %s :%s\r\n",
 	         nick, nick, host, fullname);
+	printf("%s\n", msg);
 	ewritestr(ircfd, msg);
 }
 
 static int
+udsopen(const char *uds)
+{
+	struct sockaddr_un sun;
+	size_t len;
+	int fd;
+
+	fd = socket(AF_UNIX, SOCK_STREAM, 0);
+	if(fd == -1)
+		eprintf("ii: socket:");
+
+	sun.sun_family = AF_UNIX;
+	if(strlcpy(sun.sun_path, uds, sizeof(sun.sun_path)) >= sizeof(sun.sun_path))
+		eprintf("ii: unix domain socket path truncation\n");
+	len = strlen(sun.sun_path) + 1 + sizeof(sun.sun_family);
+	if(connect(fd, (struct sockaddr *)&sun, len) == -1)
+		eprintf("ii: connect:");
+	return fd;
+}
+
+static int
 tcpopen(const char *host, const char *service)
 {
 	struct addrinfo hints, *res = NULL, *rp;
_AT_@ -727,7 +753,8 @@ main(int argc, char *argv[])
 {
 	Channel *c;
 	struct passwd *spw;
-	const char *key = NULL, *fullname = NULL, *host = "", *service = "6667";
+	const char *key = NULL, *fullname = NULL, *host = "";
+	const char *uds = "", *service = "6667";
 	char prefix[PATH_MAX];
 	int ircfd;
 
_AT_@ -754,8 +781,8 @@ main(int argc, char *argv[])
 	case 'k':
 		key = getenv(EARGF(usage()));
 		break;
-	case 'f':
-		fullname = EARGF(usage());
+	case 'u':
+		uds = EARGF(usage());
 		break;
 	default:
 		usage();
_AT_@ -765,7 +792,10 @@ main(int argc, char *argv[])
 	if(!*host || !*nick)
 		usage();
 
-	ircfd = tcpopen(host, service);
+	if(uds[0])
+		ircfd = udsopen(uds);
+	else
+		ircfd = tcpopen(host, service);
 	if(snprintf(ircpath, sizeof(ircpath), "%s/%s", prefix, host) <= 0)
 		eprintf("ii: path to irc directory too long\n");
 	create_dirtree(ircpath);
-- 
2.4.10
--Multipart=_Mon__9_May_2016_17_21_10_+0200_I.6cpFVydhq75aaE
Content-Type: text/x-diff;
 name="0056-cleanup.patch"
Content-Disposition: attachment;
 filename="0056-cleanup.patch"
Content-Transfer-Encoding: 7bit
Received on Mon Sep 17 2001 - 00:00:00 CEST

This archive was generated by hypermail 2.3.0 : Mon May 09 2016 - 17:24:22 CEST