Source-Changes-HG archive

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

[src/trunk]: src/usr.sbin/lpr IPv6 support, using rcmd-family funciton added.



details:   https://anonhg.NetBSD.org/src/rev/89264a0d0402
branches:  trunk
changeset: 481363:89264a0d0402
user:      itojun <itojun%NetBSD.org@localhost>
date:      Thu Jan 27 05:39:50 2000 +0000

description:
IPv6 support, using rcmd-family funciton added.
NetBSD PR: 9050
From: Feico Dillema

diffstat:

 usr.sbin/lpr/common_source/common.c |  128 +++++++++++++-----------
 usr.sbin/lpr/lpd/extern.h           |    6 +-
 usr.sbin/lpr/lpd/lpd.c              |  187 +++++++++++++++++++++++++----------
 3 files changed, 205 insertions(+), 116 deletions(-)

diffs (truncated from 494 to 300 lines):

diff -r cd66cfc9f194 -r 89264a0d0402 usr.sbin/lpr/common_source/common.c
--- a/usr.sbin/lpr/common_source/common.c       Thu Jan 27 05:33:06 2000 +0000
+++ b/usr.sbin/lpr/common_source/common.c       Thu Jan 27 05:39:50 2000 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: common.c,v 1.16 1999/12/05 22:10:57 jdolecek Exp $     */
+/*     $NetBSD: common.c,v 1.17 2000/01/27 05:39:50 itojun Exp $       */
 
 /*
  * Copyright (c) 1983, 1993
@@ -43,7 +43,7 @@
 #if 0
 static char sccsid[] = "@(#)common.c   8.5 (Berkeley) 4/28/95";
 #else
-__RCSID("$NetBSD: common.c,v 1.16 1999/12/05 22:10:57 jdolecek Exp $");
+__RCSID("$NetBSD: common.c,v 1.17 2000/01/27 05:39:50 itojun Exp $");
 #endif
 #endif /* not lint */
 
@@ -128,60 +128,54 @@
        char *rhost;
        int rport;
 {
-       struct hostent *hp;
-       struct servent *sp;
-       struct sockaddr_in sin;
+       struct addrinfo hints, *res, *r;
        u_int timo = 1;
        int s, lport = IPPORT_RESERVED - 1;
-       int err;
+       int error;
 
        /*
         * Get the host address and port number to connect to.
         */
        if (rhost == NULL)
                fatal("no remote host to connect to");
-       memset(&sin, 0, sizeof(sin));
-       if (inet_aton(rhost, &sin.sin_addr) == 1)
-               sin.sin_family = AF_INET;
-       else {
-               hp = gethostbyname(rhost);
-               if (hp == NULL)
-                       fatal("unknown host %s", rhost);
-               memmove(&sin.sin_addr, hp->h_addr, (size_t)hp->h_length);
-               sin.sin_family = hp->h_addrtype;
-       }
-       if (rport == 0) {
-               sp = getservbyname("printer", "tcp");
-               if (sp == NULL)
-                       fatal("printer/tcp: unknown service");
-               sin.sin_port = sp->s_port;
-       } else
-               sin.sin_port = htons(rport);
+       memset(&hints, 0, sizeof(hints));
+       hints.ai_family = PF_UNSPEC;
+       hints.ai_socktype = SOCK_STREAM;
+       error = getaddrinfo(rhost, "printer", &hints, &res);
+       if (error)
+               fatal("printer/tcp: %s", gai_strerror(error));
 
        /*
         * Try connecting to the server.
         */
+       s = -1;
+       for (r = res; r; r = r->ai_next) {
 retry:
-       seteuid(euid);
-       s = rresvport(&lport);
-       seteuid(uid);
-       if (s < 0)
-               return(-1);
-       if (connect(s, (const struct sockaddr *)&sin, sizeof(sin)) < 0) {
-               err = errno;
-               (void)close(s);
-               errno = err;
-               if (errno == EADDRINUSE) {
-                       lport--;
-                       goto retry;
-               }
-               if (errno == ECONNREFUSED && timo <= 16) {
-                       sleep(timo);
-                       timo *= 2;
-                       goto retry;
-               }
-               return(-1);
+               seteuid(euid);
+               s = rresvport_af(&lport, r->ai_family);
+               seteuid(uid);
+               if (s < 0)
+                       return(-1);
+               if (connect(s, r->ai_addr, r->ai_addrlen) < 0) {
+                       error = errno;
+                       (void)close(s);
+                       s = -1;
+                       errno = error;
+                       if (errno == EADDRINUSE) {
+                               lport--;
+                               goto retry;
+                       }
+                       if (errno == ECONNREFUSED && timo <= 16) {
+                               sleep(timo);
+                               timo *= 2;
+                               goto retry;
+                       }
+                       continue;
+               } else
+                       break;
        }
+       if (res)
+               freeaddrinfo(res);
        return(s);
 }
 
@@ -299,45 +293,63 @@
 /*
  * Figure out whether the local machine is the same
  * as the remote machine (RM) entry (if it exists).
+ *
+ * XXX not really the right way to determine.
  */
 char *
 checkremote()
 {
-       char hname[MAXHOSTNAMELEN + 1];
-       struct hostent *hp;
+       char hname[NI_MAXHOST];
+       struct addrinfo hints, *res;
        static char errbuf[128];
+       int error;
 
        remote = 0;     /* assume printer is local */
        if (RM != NULL) {
                /* get the official name of the local host */
                gethostname(hname, sizeof(hname));
                hname[sizeof(hname)-1] = '\0';
-               hp = gethostbyname(hname);
-               if (hp == (struct hostent *) NULL) {
-                   (void)snprintf(errbuf, sizeof(errbuf),
-                       "unable to get official name for local machine %s",
-                       hname);
-                   return errbuf;
+
+               memset(&hints, 0, sizeof(hints));
+               hints.ai_flags = AI_CANONNAME;
+               hints.ai_family = PF_UNSPEC;
+               hints.ai_socktype = SOCK_STREAM;
+               res = NULL;
+               error = getaddrinfo(hname, NULL, &hints, &res);
+               if (error) {
+                       (void)snprintf(errbuf, sizeof(errbuf),
+                           "unable to get official name for local machine %s: "
+                           "%s", hname, gai_strerror(error));
+                       return errbuf;
                } else {
-                       (void)strncpy(hname, hp->h_name, sizeof(hname) - 1);
+                       (void)strncpy(hname, res->ai_canonname,
+                           sizeof(hname) - 1);
                        hname[sizeof(hname) - 1] = '\0';
                }
+               freeaddrinfo(res);
 
                /* get the official name of RM */
-               hp = gethostbyname(RM);
-               if (hp == (struct hostent *) NULL) {
-                   (void)snprintf(errbuf, sizeof(errbuf),
-                       "unable to get official name for remote machine %s",
-                       RM);
-                   return errbuf;
+               memset(&hints, 0, sizeof(hints));
+               hints.ai_flags = AI_CANONNAME;
+               hints.ai_family = PF_UNSPEC;
+               hints.ai_socktype = SOCK_STREAM;
+               res = NULL;
+               error = getaddrinfo(RM, NULL, &hints, &res);
+               if (error) {
+                       (void)snprintf(errbuf, sizeof(errbuf),
+                           "unable to get official name for local machine %s: "
+                           "%s", RM, gai_strerror(error));
+                       return errbuf;
                }
 
                /*
                 * if the two hosts are not the same,
                 * then the printer must be remote.
                 */
-               if (strcasecmp(hname, hp->h_name) != 0)
+               if (strcasecmp(hname, res->ai_canonname) != 0)
                        remote = 1;
+
+               freeaddrinfo(res);
        }
        return NULL;
 }
diff -r cd66cfc9f194 -r 89264a0d0402 usr.sbin/lpr/lpd/extern.h
--- a/usr.sbin/lpr/lpd/extern.h Thu Jan 27 05:33:06 2000 +0000
+++ b/usr.sbin/lpr/lpd/extern.h Thu Jan 27 05:39:50 2000 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: extern.h,v 1.7 1997/10/05 15:12:12 mrg Exp $   */
+/*     $NetBSD: extern.h,v 1.8 2000/01/27 05:39:50 itojun Exp $        */
 
 /*
  * Copyright (c) 1989, 1993
@@ -64,4 +64,6 @@
 void       sttysetlflags __P((struct termios *tp, int flags));
 
 /* XXX from libc/net/rcmd.c */
-int        __ivaliduser __P((FILE *, u_int32_t, const char *, const char *));
+struct sockaddr;
+int        __ivaliduser_sa __P((FILE *, struct sockaddr *, const char *,
+               const char *));
diff -r cd66cfc9f194 -r 89264a0d0402 usr.sbin/lpr/lpd/lpd.c
--- a/usr.sbin/lpr/lpd/lpd.c    Thu Jan 27 05:33:06 2000 +0000
+++ b/usr.sbin/lpr/lpd/lpd.c    Thu Jan 27 05:39:50 2000 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: lpd.c,v 1.19 1999/12/23 02:10:07 mjl Exp $     */
+/*     $NetBSD: lpd.c,v 1.20 2000/01/27 05:39:50 itojun Exp $  */
 
 /*
  * Copyright (c) 1983, 1993, 1994
@@ -45,7 +45,7 @@
 #if 0
 static char sccsid[] = "@(#)lpd.c      8.7 (Berkeley) 5/10/95";
 #else
-__RCSID("$NetBSD: lpd.c,v 1.19 1999/12/23 02:10:07 mjl Exp $");
+__RCSID("$NetBSD: lpd.c,v 1.20 2000/01/27 05:39:50 itojun Exp $");
 #endif
 #endif /* not lint */
 
@@ -116,9 +116,10 @@
 static void       mcleanup __P((int));
 static void       doit __P((void));
 static void       startup __P((void));
-static void       chkhost __P((struct sockaddr_in *));
+static void       chkhost __P((struct sockaddr *));
 static int       ckqueue __P((char *));
 static void      usage __P((void));
+static int       *socksetup __P((int, int));
 
 uid_t  uid, euid;
 int child_count;
@@ -128,10 +129,10 @@
        int argc;
        char **argv;
 {
-       int f, funix, finet, options, fromlen;
+       int f, funix, *finet, options, fromlen;
        fd_set defreadfds;
        struct sockaddr_un un, fromunix;
-       struct sockaddr_in sin, frominet;
+       struct sockaddr_storage frominet;
        int omask, lfd, errs, i;
        int child_max = 32;     /* more then enough to hose the system */
 
@@ -242,31 +243,15 @@
        FD_SET(funix, &defreadfds);
        listen(funix, 5);
        if (!sflag)
-               finet = socket(AF_INET, SOCK_STREAM, 0);
+               finet = socksetup(PF_UNSPEC, options);
        else
-               finet = -1;     /* pretend we couldn't open TCP socket. */
-       if (finet >= 0) {
-               struct servent *sp;
+               finet = NULL;   /* pretend we couldn't open TCP socket. */
 
-               if (options & SO_DEBUG)
-                       if (setsockopt(finet, SOL_SOCKET, SO_DEBUG, 0, 0) < 0) {
-                               syslog(LOG_ERR, "setsockopt (SO_DEBUG): %m");
-                               mcleanup(0);
-                       }
-               sp = getservbyname("printer", "tcp");
-               if (sp == NULL) {
-                       syslog(LOG_ERR, "printer/tcp: unknown service");
-                       mcleanup(0);
+       if (finet) {
+               for (i = 1; i <= *finet; i++) {
+                       FD_SET(finet[i], &defreadfds);
+                       listen(finet[i], 5);
                }
-               memset(&sin, 0, sizeof(sin));
-               sin.sin_family = AF_INET;
-               sin.sin_port = sp->s_port;
-               if (bind(finet, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
-                       syslog(LOG_ERR, "bind: %m");
-                       mcleanup(0);
-               }
-               FD_SET(finet, &defreadfds);
-               listen(finet, 5);
        }
        /*
         * Main loop: accept, do a request, continue.
@@ -302,10 +287,12 @@
                        domain = AF_LOCAL, fromlen = sizeof(fromunix);
                        s = accept(funix,
                            (struct sockaddr *)&fromunix, &fromlen);
-               } else /* if (FD_ISSET(finet, &readfds)) */  {
-                       domain = AF_INET, fromlen = sizeof(frominet);
-                       s = accept(finet,
-                           (struct sockaddr *)&frominet, &fromlen);
+               } else {



Home | Main Index | Thread Index | Old Index