use [::]:port as wildcard ipv6 address instead of *:port
[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.12 2002/12/10 01:03:09 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 char * fix_v4_address(char *buf, struct in6_addr *in6) 
48
49         if (IN6_IS_ADDR_V4MAPPED(in6->s6_addr)) { 
50                         char *s =strchr(buf, '.'); 
51                         if (s) { 
52                                 while (s > buf && *s != ':')
53                                         --s;
54                                 if (*s == ':') ++s;     
55                                 else s = NULL; 
56                         }       
57                         if (s) return s;
58         } 
59         return buf; 
60
61
62 static int INET6_resolve(char *name, struct sockaddr_in6 *sin6)
63 {
64     struct addrinfo req, *ai;
65     int s;
66
67     memset (&req, '\0', sizeof req);
68     req.ai_family = AF_INET6;
69     if ((s = getaddrinfo(name, NULL, &req, &ai))) {
70         fprintf(stderr, "getaddrinfo: %s: %d\n", name, s);
71         return -1;
72     }
73     memcpy(sin6, ai->ai_addr, sizeof(struct sockaddr_in6));
74
75     freeaddrinfo(ai);
76
77     return (0);
78 }
79
80 #ifndef IN6_IS_ADDR_UNSPECIFIED
81 #define IN6_IS_ADDR_UNSPECIFIED(a) \
82         (((__u32 *) (a))[0] == 0 && ((__u32 *) (a))[1] == 0 && \
83          ((__u32 *) (a))[2] == 0 && ((__u32 *) (a))[3] == 0)
84 #endif
85
86
87 static int INET6_rresolve(char *name, struct sockaddr_in6 *sin6, int numeric)
88 {
89     int s;
90
91     /* Grmpf. -FvK */
92     if (sin6->sin6_family != AF_INET6) {
93 #ifdef DEBUG
94         fprintf(stderr, _("rresolve: unsupport address family %d !\n"),
95                 sin6->sin6_family);
96 #endif
97         errno = EAFNOSUPPORT;
98         return (-1);
99     }
100     if (numeric & 0x7FFF) {
101         inet_ntop( AF_INET6, &sin6->sin6_addr, name, 80);
102         return (0);
103     }
104     if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
105         if (numeric & 0x8000)
106             strcpy(name, "default");
107         else
108             strcpy(name, "[::]");
109         return (0);
110     }
111
112     if ((s = getnameinfo((struct sockaddr *) sin6, sizeof(struct sockaddr_in6),
113                          name, 255 /* !! */ , NULL, 0, 0))) {
114         fputs("getnameinfo failed\n", stderr);
115         return -1;
116     }
117     return (0);
118 }
119
120
121 static void INET6_reserror(char *text)
122 {
123     herror(text);
124 }
125
126
127
128 /* Display an Internet socket address. */
129 static char *INET6_print(unsigned char *ptr)
130 {
131     static char name[80];
132
133     inet_ntop(AF_INET6, (struct in6_addr *) ptr, name, 80);
134         return fix_v4_address(name, (struct in6_addr *)ptr);
135 }
136
137
138 /* Display an Internet socket address. */
139 /* dirty! struct sockaddr usually doesn't suffer for inet6 addresses, fst. */
140 static char *INET6_sprint(struct sockaddr *sap, int numeric)
141 {
142     static char buff[128];
143
144     if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
145         return safe_strncpy(buff, _("[NONE SET]"), sizeof(buff));
146     if (INET6_rresolve(buff, (struct sockaddr_in6 *) sap, numeric) != 0)
147         return safe_strncpy(buff, _("[UNKNOWN]"), sizeof(buff));
148     return (fix_v4_address(buff, &((struct sockaddr_in6 *)sap)->sin6_addr));
149 }
150
151
152 static int INET6_getsock(char *bufp, struct sockaddr *sap)
153 {
154     struct sockaddr_in6 *sin6;
155         char *p;
156
157     sin6 = (struct sockaddr_in6 *) sap;
158     sin6->sin6_family = AF_INET6;
159     sin6->sin6_port = 0;
160
161     if (inet_pton(AF_INET6, bufp, sin6->sin6_addr.s6_addr) <= 0)
162         return (-1);
163         p = fix_v4_address(bufp, &sin6->sin6_addr);
164         if (p != bufp) 
165                 memcpy(bufp, p, strlen(p)+1); 
166     return 16;                  /* ?;) */
167 }
168
169 static int INET6_input(int type, char *bufp, struct sockaddr *sap)
170 {
171     switch (type) {
172     case 1:
173         return (INET6_getsock(bufp, sap));
174     default:
175         return (INET6_resolve(bufp, (struct sockaddr_in6 *) sap));
176     }
177 }
178
179
180 struct aftype inet6_aftype =
181 {
182     "inet6", NULL, /*"IPv6", */ AF_INET6, sizeof(struct in6_addr),
183     INET6_print, INET6_sprint, INET6_input, INET6_reserror,
184     INET6_rprint, INET6_rinput, NULL,
185
186     -1,
187     "/proc/net/if_inet6"
188 };
189
190
191 #endif                          /* HAVE_AFINET6 */