Source-Changes-HG archive

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

[src/trunk]: src/usr.bin/getaddrinfo New utility getaddrinfo(1) to reflect ge...



details:   https://anonhg.NetBSD.org/src/rev/0cf961f433fb
branches:  trunk
changeset: 790247:0cf961f433fb
user:      riastradh <riastradh%NetBSD.org@localhost>
date:      Mon Sep 30 06:19:22 2013 +0000

description:
New utility getaddrinfo(1) to reflect getaddrinfo(3).

Discussed on tech-userlevel back in April:

https://mail-index.netbsd.org/tech-userlevel/2013/04/25/msg007719.html

Not hooked into the build or sets yet.

diffstat:

 usr.bin/getaddrinfo/Makefile      |   20 ++
 usr.bin/getaddrinfo/getaddrinfo.1 |  174 +++++++++++++++++++++
 usr.bin/getaddrinfo/getaddrinfo.c |  308 ++++++++++++++++++++++++++++++++++++++
 usr.bin/getaddrinfo/tables.awk    |   62 +++++++
 4 files changed, 564 insertions(+), 0 deletions(-)

diffs (truncated from 580 to 300 lines):

diff -r 38868d4996eb -r 0cf961f433fb usr.bin/getaddrinfo/Makefile
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/usr.bin/getaddrinfo/Makefile      Mon Sep 30 06:19:22 2013 +0000
@@ -0,0 +1,20 @@
+#      $NetBSD: Makefile,v 1.1 2013/09/30 06:19:22 riastradh Exp $
+
+PROG=  getaddrinfo
+
+DPADD+=        ${LIBUTIL}
+LDADD+=        -lutil
+
+WARNS= 5
+
+SYS_SOCKET_H?= ${NETBSDSRCDIR}/sys/sys/socket.h
+
+CPPFLAGS+=     -I.
+DPSRCS+=       tables.h
+CLEANFILES+=   tables.h
+tables.h: tables.awk ${SYS_SOCKET_H}
+       ${_MKTARGET_CREATE}
+       ${TOOL_AWK} -f ${.ALLSRC} > ${.TARGET}.tmp \
+       && mv -f -- ${.TARGET}.tmp ${.TARGET}
+
+.include <bsd.prog.mk>
diff -r 38868d4996eb -r 0cf961f433fb usr.bin/getaddrinfo/getaddrinfo.1
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/usr.bin/getaddrinfo/getaddrinfo.1 Mon Sep 30 06:19:22 2013 +0000
@@ -0,0 +1,174 @@
+.\"    $NetBSD: getaddrinfo.1,v 1.1 2013/09/30 06:19:22 riastradh Exp $
+.\"
+.\" Copyright (c) 2013 The NetBSD Foundation, Inc.
+.\" All rights reserved.
+.\"
+.\" This documentation is derived from text contributed to The NetBSD
+.\" Foundation by Taylor R. Campbell.
+.\"
+.\" Redistribution and use in source and binary forms, with or without
+.\" modification, are permitted provided that the following conditions
+.\" are met:
+.\" 1. Redistributions of source code must retain the above copyright
+.\"    notice, this list of conditions and the following disclaimer.
+.\" 2. Redistributions in binary form must reproduce the above copyright
+.\"    notice, this list of conditions and the following disclaimer in the
+.\"    documentation and/or other materials provided with the distribution.
+.\"
+.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+.\" POSSIBILITY OF SUCH DAMAGE.
+.\"
+.Dd April 25, 2013
+.Dt GETADDRINFO 1
+.Os
+.Sh NAME
+.Nm getaddrinfo
+.Nd resolve names to socket addresses
+.Sh SYNOPSIS
+.Nm
+.Op Fl cnNP
+.Op Fl f Ar family
+.Op Fl p Ar protocol
+.Op Fl t Ar socktype
+.Op Fl s Ar service
+.Op Ar hostname
+.Sh DESCRIPTION
+The
+.Nm
+utility resolves host and service names to socket addresses as if with
+the
+.Xr getaddrinfo 3
+routine and formats them to standard output.
+.Pp
+The output is a sequence of lines of space-separated fields:
+.Dl socket-type address-family protocol [af-specific data ...]
+.Pp
+For the internet family, the address-family-specific data are the
+internet address and the port number.
+.Pp
+Although the
+.Nm
+utility may query the DNS to give answers, depending on the
+system's
+.Xr nsswitch.conf 5
+configuration, it is not intended to be a general-purpose utility to
+query the DNS; use the
+.Xr dig 1
+utility for that.
+.Pp
+The following options are available:
+.Bl -tag -width Ds
+.It Fl c
+Look up a canonical name as if with the
+.Dv AI_CANONNAME
+flag to
+.Xr getaddrinfo 3
+and print it on the first line before the socket addresses.
+.It Fl f Ar family
+Specify an address family.
+Address families are named like the
+.Dv AF_...
+constants for address family numbers in the
+.Aq Pa sys/socket.h
+header file but without the
+.Dv AF_
+prefix and lowercase.
+For example,
+.Dq inet
+corresponds with
+.Dv AF_INET .
+.It Fl n
+Treat the hostname as a numeric address and do not attempt name
+resolution, as if with the
+.Dv AI_NUMERICHOST
+flag to
+.Xr getaddrinfo 3 .
+.It Fl N
+Treat the service as numeric and do not attempt service name
+resolution, as if with the
+.Dv AI_NUMERICSERV
+flag to
+.Xr getaddrinfo 3 .
+.It Fl s Ar service
+Specify a service to look up.
+If no service is specified, a hostname must be specified.
+.It Fl p Ar protocol
+Specify a protocol.
+Protocols may be numeric, or symbolic as listed in
+.Xr protocols 5 .
+.It Fl P
+Return socket addresses intended for use with
+.Xr bind 2 ,
+as if with the
+.Dv AI_PASSIVE
+flag to
+.Xr getaddrinfo 3 .
+By default, the socket addresses are intended for use with
+.Xr connect 2 ,
+.Xr sendto 2 ,
+or
+.Xr sendmsg 2 .
+.It Fl t Ar socktype
+Specify a socket type.
+Socket types are named like the
+.Dv SOCK_...
+constants for socket type numbers in the
+.Aq Pa sys/socket.h
+header file but without the
+.Dv SOCK_
+prefix and lowercase.
+For example,
+.Dq dgram
+corresponds with
+.Dv SOCK_DGRAM .
+.El
+.Pp
+The
+.Nm
+utility exits 0 on success, and \*[Gt]0 if an error occurs.
+.Sh EXAMPLES
+Look up
+.Dq www.NetBSD.org :
+.Bd -literal -offset indent
+$ getaddrinfo www.NetBSD.org
+dgram inet6 udp 2001:4f8:3:7:2e0:81ff:fe52:9ab6 0
+dgram inet udp 149.20.53.67 0
+stream inet6 tcp 2001:4f8:3:7:2e0:81ff:fe52:9ab6 0
+stream inet tcp 149.20.53.67 0
+.Ed
+The unspecified service manifested as a port number of zero.
+.Pp
+Look up
+.Dq morden.NetBSD.org
+for stream sockets on port 80, and show the canonical name:
+.Bd -literal -offset indent
+$ getaddrinfo -c -t stream -s 80 morden.NetBSD.org
+canonname ftp.NetBSD.org
+stream inet6 tcp 2001:470:1f05:3d::21 80
+stream inet tcp 199.233.217.249 80
+.Ed
+.Sh SEE ALSO
+.Xr dig 1 ,
+.Xr getent 1 ,
+.Xr getaddrinfo 3 ,
+.Xr getnameinfo 3 ,
+.Xr resolver 3 ,
+.Xr hosts 5 ,
+.Xr nsswitch.conf 5 ,
+.Xr protocols 5 ,
+.Xr resolv.conf 5 ,
+.Xr services 5
+.Sh HISTORY
+The
+.Nm
+command first appeared in
+.Nx 7.0 .
diff -r 38868d4996eb -r 0cf961f433fb usr.bin/getaddrinfo/getaddrinfo.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/usr.bin/getaddrinfo/getaddrinfo.c Mon Sep 30 06:19:22 2013 +0000
@@ -0,0 +1,308 @@
+/*     $NetBSD: getaddrinfo.c,v 1.1 2013/09/30 06:19:22 riastradh Exp $        */
+
+/*-
+ * Copyright (c) 2013 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Taylor R. Campbell.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__RCSID("$NetBSD: getaddrinfo.c,v 1.1 2013/09/30 06:19:22 riastradh Exp $");
+
+#include <assert.h>
+#include <err.h>
+#include <errno.h>
+#include <limits.h>
+#include <netdb.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <util.h>
+
+#include "tables.h"
+
+static void    usage(void) __dead;
+static void    printaddrinfo(struct addrinfo *);
+static bool    parse_af(const char *, int *);
+static bool    parse_protocol(const char *, int *);
+static bool    parse_socktype(const char *, int *);
+static bool    parse_numeric_tabular(const char *, int *, const char *const *,
+                   size_t);
+
+int
+main(int argc, char **argv)
+{
+       static const struct addrinfo zero_addrinfo;
+       struct addrinfo hints = zero_addrinfo;
+       struct addrinfo *addrinfo;
+       const char *hostname = NULL, *service = NULL;
+       int ch;
+       int error;
+
+       setprogname(argv[0]);
+
+       hints.ai_family = AF_UNSPEC;
+       hints.ai_socktype = 0;
+       hints.ai_protocol = 0;
+       hints.ai_flags = 0;
+
+       while ((ch = getopt(argc, argv, "cf:nNp:Ps:t:")) != -1) {
+               switch (ch) {
+               case 'c':
+                       hints.ai_flags |= AI_CANONNAME;
+                       break;
+
+               case 'f':
+                       if (!parse_af(optarg, &hints.ai_family)) {
+                               warnx("invalid address family: %s", optarg);
+                               usage();
+                       }
+                       break;
+
+               case 'n':
+                       hints.ai_flags |= AI_NUMERICHOST;
+                       break;
+
+               case 'N':
+                       hints.ai_flags |= AI_NUMERICSERV;
+                       break;
+



Home | Main Index | Thread Index | Old Index