Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.sbin/puffs/mount_9p Support IPv6.



details:   https://anonhg.NetBSD.org/src/rev/782ce6943931
branches:  trunk
changeset: 972897:782ce6943931
user:      uwe <uwe%NetBSD.org@localhost>
date:      Sat Jun 13 13:45:06 2020 +0000

description:
Support IPv6.

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

diffstat:

 usr.sbin/puffs/mount_9p/Makefile    |   7 ++-
 usr.sbin/puffs/mount_9p/mount_9p.8  |  12 +++-
 usr.sbin/puffs/mount_9p/ninepuffs.c |  98 +++++++++++++++++++++++++-----------
 3 files changed, 84 insertions(+), 33 deletions(-)

diffs (231 lines):

diff -r 56ea3b166baa -r 782ce6943931 usr.sbin/puffs/mount_9p/Makefile
--- a/usr.sbin/puffs/mount_9p/Makefile  Sat Jun 13 12:53:42 2020 +0000
+++ b/usr.sbin/puffs/mount_9p/Makefile  Sat Jun 13 13:45:06 2020 +0000
@@ -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 @@
 
 MAN=   mount_9p.8
 
+.if (${USE_INET6} != "no")
+CPPFLAGS += -DINET6
+.endif
+
 .include <bsd.prog.mk>
diff -r 56ea3b166baa -r 782ce6943931 usr.sbin/puffs/mount_9p/mount_9p.8
--- a/usr.sbin/puffs/mount_9p/mount_9p.8        Sat Jun 13 12:53:42 2020 +0000
+++ b/usr.sbin/puffs/mount_9p/mount_9p.8        Sat Jun 13 13:45:06 2020 +0000
@@ -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 @@
 .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
diff -r 56ea3b166baa -r 782ce6943931 usr.sbin/puffs/mount_9p/ninepuffs.c
--- a/usr.sbin/puffs/mount_9p/ninepuffs.c       Sat Jun 13 12:53:42 2020 +0000
+++ b/usr.sbin/puffs/mount_9p/ninepuffs.c       Sat Jun 13 13:45:06 2020 +0000
@@ -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 @@
 
 #include <assert.h>
 #include <err.h>
+#include <errno.h>
 #include <netdb.h>
 #include <pwd.h>
 #include <puffs.h>
@@ -52,7 +53,7 @@
 #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 @@
 }
 
 /*
- * 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;
+       int ret;
 
-       he = gethostbyname2(addr, AF_INET);
-       if (he == NULL) {
-               herror("gethostbyname");
-               exit(1);
+       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));
 
-       opt = 1;
-       ret = setsockopt(s, SOL_SOCKET, SO_NOSIGPIPE, &opt, sizeof(opt));
-       if (ret == -1)
-               err(1, "setsockopt(SO_NOSIGPIPE)");
+       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;
+               }
 
-       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));
+               const int opt = 1;
+               ret = setsockopt(s, SOL_SOCKET, SO_NOSIGPIPE, &opt, sizeof(opt));
+               if (ret < 0) {
+                       cause = "SO_NOSIGPIPE";
+                       continue;
+               }
 
-       if (connect(s, (struct sockaddr *)&mysin, sizeof(mysin)) == -1)
-               err(1, "connect");
+               ret = connect(s, ai->ai_addr, ai->ai_addrlen);
+               if (ret < 0) {
+                       close(s);
+                       s = -1;
+                       cause = "connect";
+                       continue;
+               }
+       }
 
+       if (s < 0)
+               err(EXIT_FAILURE, "%s", cause);
+
+       freeaddrinfo(ai0);
        return s;
 }
 
@@ -121,9 +141,10 @@
        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 @@
 
        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 @@
                        freemntopts(mp);
                        break;
                case 'p':
-                       port = atoi(optarg);
+                       port = optarg;
                        break;
                case 's':
                        detach = 0;
@@ -235,7 +273,7 @@
        }
 
        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]);



Home | Main Index | Thread Index | Old Index