2 * lib/inet6.c This file contains an implementation of the "INET6"
3 * support functions for the net-tools.
4 * (most of it copied from lib/inet.c 1.26).
6 * Version: $Id: inet6.c,v 1.9 1999/12/11 13:35:57 freitag Exp $
8 * Author: Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
9 * Copyright 1993 MicroWalt Corporation
12 *960808 {0.01} Frank Strauss : adapted for IPv6 support
13 *980701 {0.02} Arnaldo C. Melo: GNU gettext instead of catgets
14 *990824 Bernd Eckenfels: clear members for selecting v6 address
16 * This program is free software; you can redistribute it
17 * and/or modify it under the terms of the GNU General
18 * Public License as published by the Free Software
19 * Foundation; either version 2 of the License, or (at
20 * your option) any later version.
25 #include <asm/types.h>
26 #include <sys/types.h>
27 #include <sys/socket.h>
28 #include <netinet/in.h>
29 #include <arpa/inet.h>
30 #include <arpa/nameser.h>
40 #include "net-support.h"
41 #include "pathnames.h"
45 extern int h_errno; /* some netdb.h versions don't export this */
47 static int INET6_resolve(char *name, struct sockaddr_in6 *sin6)
49 struct addrinfo req, *ai;
52 memset (&req, '\0', sizeof req);
53 req.ai_family = AF_INET6;
54 if ((s = getaddrinfo(name, NULL, &req, &ai))) {
55 fprintf(stderr, "getaddrinfo: %s: %d\n", name, s);
58 memcpy(sin6, ai->ai_addr, sizeof(struct sockaddr_in6));
65 #ifndef IN6_IS_ADDR_UNSPECIFIED
66 #define IN6_IS_ADDR_UNSPECIFIED(a) \
67 (((__u32 *) (a))[0] == 0 && ((__u32 *) (a))[1] == 0 && \
68 ((__u32 *) (a))[2] == 0 && ((__u32 *) (a))[3] == 0)
72 static int INET6_rresolve(char *name, struct sockaddr_in6 *sin6, int numeric)
77 if (sin6->sin6_family != AF_INET6) {
79 fprintf(stderr, _("rresolve: unsupport address family %d !\n"),
85 if (numeric & 0x7FFF) {
86 inet_ntop(AF_INET6, &sin6->sin6_addr, name, 80);
89 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
91 strcpy(name, "default");
97 if ((s = getnameinfo((struct sockaddr *) sin6, sizeof(struct sockaddr_in6),
98 name, 255 /* !! */ , NULL, 0, 0))) {
99 fprintf(stderr, "getnameinfo: %s\n", gai_strerror(s));
106 static void INET6_reserror(char *text)
112 /* Display an Internet socket address. */
113 static char *INET6_print(unsigned char *ptr)
115 static char name[80];
117 inet_ntop(AF_INET6, (struct in6_addr *) ptr, name, 80);
122 /* Display an Internet socket address. */
123 /* dirty! struct sockaddr usually doesn't suffer for inet6 addresses, fst. */
124 static char *INET6_sprint(struct sockaddr *sap, int numeric)
126 static char buff[128];
128 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
129 return safe_strncpy(buff, _("[NONE SET]"), sizeof(buff));
130 if (INET6_rresolve(buff, (struct sockaddr_in6 *) sap, numeric) != 0)
136 static int INET6_getsock(char *bufp, struct sockaddr *sap)
138 struct sockaddr_in6 *sin6;
140 sin6 = (struct sockaddr_in6 *) sap;
141 sin6->sin6_family = AF_INET6;
144 if (inet_pton(AF_INET6, bufp, sin6->sin6_addr.s6_addr) <= 0)
150 static int INET6_input(int type, char *bufp, struct sockaddr *sap)
154 return (INET6_getsock(bufp, sap));
156 return (INET6_resolve(bufp, (struct sockaddr_in6 *) sap));
161 struct aftype inet6_aftype =
163 "inet6", NULL, /*"IPv6", */ AF_INET6, sizeof(struct in6_addr),
164 INET6_print, INET6_sprint, INET6_input, INET6_reserror,
165 INET6_rprint, INET6_rinput, NULL,
172 #endif /* HAVE_AFINET6 */