clear struct to get all matching v6 addresses in ifconfig
[platform/upstream/net-tools.git] / lib / inet6.c
1 /*
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).
5  *
6  * Version:     $Id: inet6.c,v 1.8 1999/08/24 16:47:24 ecki Exp $
7  *
8  * Author:      Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
9  *              Copyright 1993 MicroWalt Corporation
10  *
11  * Modified:
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
15  *
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.
21  */
22 #include "config.h"
23
24 #if HAVE_AFINET6
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>
31 #include <ctype.h>
32 #include <errno.h>
33 #include <netdb.h>
34 #include <resolv.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <stdio.h>
38 #include <unistd.h>
39 #include "version.h"
40 #include "net-support.h"
41 #include "pathnames.h"
42 #include "intl.h"
43 #include "util.h"
44
45 extern int h_errno;             /* some netdb.h versions don't export this */
46
47 static int INET6_resolve(char *name, struct sockaddr_in6 *sin6)
48 {
49     struct addrinfo req, *ai;
50     int s;
51
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: %s\n", name, gai_strerror(s));
56         return -1;
57     }
58     memcpy(sin6, ai->ai_addr, sizeof(struct sockaddr_in6));
59
60     freeaddrinfo(ai);
61
62     return (0);
63 }
64
65
66 static int INET6_rresolve(char *name, struct sockaddr_in6 *sin6, int numeric)
67 {
68     int s;
69
70     /* Grmpf. -FvK */
71     if (sin6->sin6_family != AF_INET6) {
72 #ifdef DEBUG
73         fprintf(stderr, _("rresolve: unsupport address family %d !\n"),
74                 sin6->sin6_family);
75 #endif
76         errno = EAFNOSUPPORT;
77         return (-1);
78     }
79     if (numeric & 0x7FFF) {
80         inet_ntop(AF_INET6, &sin6->sin6_addr, name, 80);
81         return (0);
82     }
83     if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
84         if (numeric & 0x8000)
85             strcpy(name, "default");
86         else
87             strcpy(name, "*");
88         return (0);
89     }
90
91     if ((s = getnameinfo((struct sockaddr *) sin6, sizeof(struct sockaddr_in6),
92                          name, 255 /* !! */ , NULL, 0, 0))) {
93         fprintf(stderr, "getnameinfo: %s\n", gai_strerror(s));
94         return -1;
95     }
96     return (0);
97 }
98
99
100 static void INET6_reserror(char *text)
101 {
102     herror(text);
103 }
104
105
106 /* Display an Internet socket address. */
107 static char *INET6_print(unsigned char *ptr)
108 {
109     static char name[80];
110
111     inet_ntop(AF_INET6, (struct in6_addr *) ptr, name, 80);
112     return name;
113 }
114
115
116 /* Display an Internet socket address. */
117 /* dirty! struct sockaddr usually doesn't suffer for inet6 addresses, fst. */
118 static char *INET6_sprint(struct sockaddr *sap, int numeric)
119 {
120     static char buff[128];
121
122     if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
123         return safe_strncpy(buff, _("[NONE SET]"), sizeof(buff));
124     if (INET6_rresolve(buff, (struct sockaddr_in6 *) sap, numeric) != 0)
125         return (NULL);
126     return (buff);
127 }
128
129
130 static int INET6_getsock(char *bufp, struct sockaddr *sap)
131 {
132     struct sockaddr_in6 *sin6;
133
134     sin6 = (struct sockaddr_in6 *) sap;
135     sin6->sin6_family = AF_INET6;
136     sin6->sin6_port = 0;
137
138     if (inet_pton(AF_INET6, bufp, sin6->sin6_addr.s6_addr) <= 0)
139         return (-1);
140
141     return 16;                  /* ?;) */
142 }
143
144 static int INET6_input(int type, char *bufp, struct sockaddr *sap)
145 {
146     switch (type) {
147     case 1:
148         return (INET6_getsock(bufp, sap));
149     default:
150         return (INET6_resolve(bufp, (struct sockaddr_in6 *) sap));
151     }
152 }
153
154
155 struct aftype inet6_aftype =
156 {
157     "inet6", NULL, /*"IPv6", */ AF_INET6, sizeof(struct in6_addr),
158     INET6_print, INET6_sprint, INET6_input, INET6_reserror,
159     INET6_rprint, INET6_rinput, NULL,
160
161     -1,
162     "/proc/net/if_inet6"
163 };
164
165
166 #endif                          /* HAVE_AFINET6 */