Module Name:    src
Committed By:   uwe
Date:           Sat Jun 13 13:45:06 UTC 2020

Modified Files:
        src/usr.sbin/puffs/mount_9p: Makefile mount_9p.8 ninepuffs.c

Log Message:
Support IPv6.

Use getaddrinfo(3).  Add -4 and -6 command line options.  Obey USE_INET6.


To generate a diff of this commit:
cvs rdiff -u -r1.3 -r1.4 src/usr.sbin/puffs/mount_9p/Makefile
cvs rdiff -u -r1.13 -r1.14 src/usr.sbin/puffs/mount_9p/mount_9p.8
cvs rdiff -u -r1.29 -r1.30 src/usr.sbin/puffs/mount_9p/ninepuffs.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.sbin/puffs/mount_9p/Makefile
diff -u src/usr.sbin/puffs/mount_9p/Makefile:1.3 src/usr.sbin/puffs/mount_9p/Makefile:1.4
--- src/usr.sbin/puffs/mount_9p/Makefile:1.3	Wed Apr 22 15:23:06 2009
+++ src/usr.sbin/puffs/mount_9p/Makefile	Sat Jun 13 13:45:06 2020
@@ -1,5 +1,6 @@
-#	$NetBSD: Makefile,v 1.3 2009/04/22 15:23:06 lukem Exp $
+#	$NetBSD: Makefile,v 1.4 2020/06/13 13:45:06 uwe Exp $
 #
+.include <bsd.init.mk>		# include early to pick up USE_*
 
 PROG=	mount_9p
 SRCS=	ninepuffs.c ninebuf.c nineproto.c fs.c node.c subr.c
@@ -7,4 +8,8 @@ LDADD+= -lpuffs -lutil
 
 MAN=	mount_9p.8
 
+.if (${USE_INET6} != "no")
+CPPFLAGS += -DINET6
+.endif
+
 .include <bsd.prog.mk>

Index: src/usr.sbin/puffs/mount_9p/mount_9p.8
diff -u src/usr.sbin/puffs/mount_9p/mount_9p.8:1.13 src/usr.sbin/puffs/mount_9p/mount_9p.8:1.14
--- src/usr.sbin/puffs/mount_9p/mount_9p.8:1.13	Sat May 30 00:00:35 2020
+++ src/usr.sbin/puffs/mount_9p/mount_9p.8	Sat Jun 13 13:45:06 2020
@@ -1,4 +1,4 @@
-.\"	$NetBSD: mount_9p.8,v 1.13 2020/05/30 00:00:35 uwe Exp $
+.\"	$NetBSD: mount_9p.8,v 1.14 2020/06/13 13:45:06 uwe Exp $
 .\"
 .\" Copyright (c) 2007 Antti Kantee.  All rights reserved.
 .\"
@@ -31,7 +31,7 @@
 .Nd mount a file server using the 9P resource sharing protocol
 .Sh SYNOPSIS
 .Nm
-.Op Fl su
+.Op Fl 46su
 .Op Fl o Ar options
 .Op Fl p Ar port
 .Oo Ar user Ns Li \&@ Oc Ns Ar host Ns Op Li \&: Ns Ar path
@@ -72,6 +72,14 @@ for more information.
 .Pp
 The following options are available:
 .Bl -tag -width "Fl o Ar options"
+.It Fl 4
+Forces
+.Nm
+to use IPv4 addresses only.
+.It Fl 6
+Forces
+.Nm
+to use IPv6 addresses only.
 .It Fl c
 Interpret
 .Ar special

Index: src/usr.sbin/puffs/mount_9p/ninepuffs.c
diff -u src/usr.sbin/puffs/mount_9p/ninepuffs.c:1.29 src/usr.sbin/puffs/mount_9p/ninepuffs.c:1.30
--- src/usr.sbin/puffs/mount_9p/ninepuffs.c:1.29	Sat May 30 02:53:30 2020
+++ src/usr.sbin/puffs/mount_9p/ninepuffs.c	Sat Jun 13 13:45:06 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: ninepuffs.c,v 1.29 2020/05/30 02:53:30 uwe Exp $	*/
+/*	$NetBSD: ninepuffs.c,v 1.30 2020/06/13 13:45:06 uwe Exp $	*/
 
 /*
  * Copyright (c) 2007  Antti Kantee.  All Rights Reserved.
@@ -31,7 +31,7 @@
 
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: ninepuffs.c,v 1.29 2020/05/30 02:53:30 uwe Exp $");
+__RCSID("$NetBSD: ninepuffs.c,v 1.30 2020/06/13 13:45:06 uwe Exp $");
 #endif /* !lint */
 
 #include <sys/types.h>
@@ -42,6 +42,7 @@ __RCSID("$NetBSD: ninepuffs.c,v 1.29 202
 
 #include <assert.h>
 #include <err.h>
+#include <errno.h>
 #include <netdb.h>
 #include <pwd.h>
 #include <puffs.h>
@@ -52,7 +53,7 @@ __RCSID("$NetBSD: ninepuffs.c,v 1.29 202
 #include "ninepuffs.h"
 #include "nineproto.h"
 
-#define DEFPORT_9P 564
+#define DEFPORT_9P "564" /* "9pfs", but don't depend on it being in services */
 
 __dead static void
 usage(void)
@@ -66,39 +67,58 @@ usage(void)
 }
 
 /*
- * TCPv4 connection to 9P file server, forget IL and IPv6 for now.
+ * TCP connection to 9P file server.
  * Return connected socket or exit with error.
  */
 static int
-serverconnect(const char *addr, unsigned short port)
+serverconnect(const char *hostname, const char *portname, int family)
 {
-	struct sockaddr_in mysin;
-	struct hostent *he;
-	int s, ret, opt;
-
-	he = gethostbyname2(addr, AF_INET);
-	if (he == NULL) {
-		herror("gethostbyname");
-		exit(1);
+	int ret;
+
+	struct addrinfo hints;
+	memset(&hints, 0, sizeof(hints));
+	hints.ai_family = family;
+	hints.ai_socktype = SOCK_STREAM;
+
+	if (portname == NULL) {
+		portname = DEFPORT_9P;
+		hints.ai_flags |= AI_NUMERICSERV;
 	}
 
-	s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
-	if (s == -1)
-		err(1, "socket");
+	struct addrinfo *ai0;
+	ret = getaddrinfo(hostname, portname, &hints, &ai0);
+	if (ret != 0)
+		errx(EXIT_FAILURE, "%s", gai_strerror(ret));
+
+	int s = -1;
+	const char *cause = NULL;
+	for (struct addrinfo *ai = ai0; ai != NULL; ai = ai->ai_next) {
+		s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
+		if (s < 0) {
+			cause = "socket";
+			continue;
+		}
+
+		const int opt = 1;
+		ret = setsockopt(s, SOL_SOCKET, SO_NOSIGPIPE, &opt, sizeof(opt));
+		if (ret < 0) {
+			cause = "SO_NOSIGPIPE";
+			continue;
+		}
 
-	opt = 1;
-	ret = setsockopt(s, SOL_SOCKET, SO_NOSIGPIPE, &opt, sizeof(opt));
-	if (ret == -1)
-		err(1, "setsockopt(SO_NOSIGPIPE)");
-
-	memset(&mysin, 0, sizeof(struct sockaddr_in));
-	mysin.sin_family = AF_INET;
-	mysin.sin_port = htons(port);
-	memcpy(&mysin.sin_addr, he->h_addr, sizeof(struct in_addr));
+		ret = connect(s, ai->ai_addr, ai->ai_addrlen);
+		if (ret < 0) {
+			close(s);
+			s = -1;
+			cause = "connect";
+			continue;
+		}
+	}
 
-	if (connect(s, (struct sockaddr *)&mysin, sizeof(mysin)) == -1)
-		err(1, "connect");
+	if (s < 0)
+		err(EXIT_FAILURE, "%s", cause);
 
+	freeaddrinfo(ai0);
 	return s;
 }
 
@@ -121,9 +141,10 @@ main(int argc, char *argv[])
 	struct puffs_ops *pops;
 	struct puffs_node *pn_root;
 	mntoptparse_t mp;
+	int family;
 	const char *user, *srvhost, *srvpath;
 	char *p;
-	unsigned short port;
+	const char *port;
 	int mntflags, pflags, ch;
 	int detach;
 	int protover;
@@ -136,12 +157,29 @@ main(int argc, char *argv[])
 
 	mntflags = pflags = 0;
 	detach = 1;
-	port = DEFPORT_9P;
+#ifdef INET6
+	family = AF_UNSPEC;
+#else
+	family = AF_INET;
+#endif
+	port = NULL;
 	protover = P9PROTO_VERSION;
 	server = P9P_SERVER_TCP;
 
-	while ((ch = getopt(argc, argv, "co:p:su")) != -1) {
+	while ((ch = getopt(argc, argv, "46co:p:su")) != -1) {
 		switch (ch) {
+		case '4':
+			family = AF_INET;
+			break;
+		case '6':
+#ifdef INET6
+			family = AF_INET6;
+			break;
+#else
+			errno = EPFNOSUPPORT;
+			err(EXIT_FAILURE, "IPv6");
+			/* NOTREACHED */
+#endif
 		case 'c':
 			server = P9P_SERVER_CDEV;
 			break;
@@ -152,7 +190,7 @@ main(int argc, char *argv[])
 			freemntopts(mp);
 			break;
 		case 'p':
-			port = atoi(optarg);
+			port = optarg;
 			break;
 		case 's':
 			detach = 0;
@@ -235,7 +273,7 @@ main(int argc, char *argv[])
 	}
 
 	if (p9p.server == P9P_SERVER_TCP) {
-		p9p.servsock = serverconnect(srvhost, port);
+		p9p.servsock = serverconnect(srvhost, port, family);
 	} else {
 		/* path to a vio9p(4) device, e.g., /dev/vio9p0 */
 		p9p.servsock = open_cdev(argv[0]);

Reply via email to