c5e058fddcce3bc0bebbd6069295d89681506609
[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.5 1998/11/17 15:16:49 freitag 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  *
15  *              This program is free software; you can redistribute it
16  *              and/or  modify it under  the terms of  the GNU General
17  *              Public  License as  published  by  the  Free  Software
18  *              Foundation;  either  version 2 of the License, or  (at
19  *              your option) any later version.
20  */
21 #include "config.h"
22
23 #if HAVE_AFINET6
24 #include <asm/types.h>
25 #include <sys/types.h>
26 #include <sys/socket.h>
27 #include <netinet/in.h>
28 #include <arpa/inet.h>
29 #include <arpa/nameser.h>
30 #include <ctype.h>
31 #include <errno.h>
32 #include <netdb.h>
33 #include <resolv.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <stdio.h>
37 #include <unistd.h>
38 #include "version.h"
39 #include "net-support.h"
40 #include "pathnames.h"
41 #include "intl.h"
42
43 extern int h_errno;             /* some netdb.h versions don't export this */
44
45 static int INET6_resolve(char *name, struct sockaddr_in6 *sin6)
46 {
47     struct addrinfo req, *ai;
48     int s;
49
50     req.ai_family = AF_INET6;
51     if ((s = getaddrinfo(name, NULL, &req, &ai))) {
52         fprintf(stderr, "getaddrinfo: %s: %s\n", name, gai_strerror(s));
53         return -1;
54     }
55     memcpy(sin6, ai->ai_addr, sizeof(struct sockaddr_in6));
56
57     freeaddrinfo(ai);
58
59     return (0);
60 }
61
62
63 static int INET6_rresolve(char *name, struct sockaddr_in6 *sin6, int numeric)
64 {
65     int s;
66
67     /* Grmpf. -FvK */
68     if (sin6->sin6_family != AF_INET6) {
69 #ifdef DEBUG
70         fprintf(stderr, _("rresolve: unsupport address family %d !\n"),
71                 sin6->sin6_family);
72 #endif
73         errno = EAFNOSUPPORT;
74         return (-1);
75     }
76     if (numeric & 0x7FFF) {
77         inet_ntop(AF_INET6, &sin6->sin6_addr, name, 80);
78         return (0);
79     }
80     if ((s = getnameinfo((struct sockaddr *) sin6, sizeof(struct sockaddr_in6),
81                          name, 255 /* !! */ , NULL, 0, 0))) {
82         fprintf(stderr, "getnameinfo: %s\n", gai_strerror(s));
83         return -1;
84     }
85     return (0);
86 }
87
88
89 static void INET6_reserror(char *text)
90 {
91     herror(text);
92 }
93
94
95 /* Display an Internet socket address. */
96 static char *INET6_print(unsigned char *ptr)
97 {
98     static char name[80];
99
100     inet_ntop(AF_INET6, (struct in6_addr *) ptr, name, 80);
101     return name;
102 }
103
104
105 /* Display an Internet socket address. */
106 /* dirty! struct sockaddr usually doesn't suffer for inet6 addresses, fst. */
107 static char *INET6_sprint(struct sockaddr *sap, int numeric)
108 {
109     static char buff[128];
110
111     if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
112         return safe_strncpy(buff, _("[NONE SET]"), sizeof(buff));
113     if (INET6_rresolve(buff, (struct sockaddr_in6 *) sap, numeric) != 0)
114         return (NULL);
115     return (buff);
116 }
117
118
119 static int INET6_getsock(char *bufp, struct sockaddr *sap)
120 {
121     struct sockaddr_in6 *sin6;
122
123     sin6 = (struct sockaddr_in6 *) sap;
124     sin6->sin6_family = AF_INET6;
125     sin6->sin6_port = 0;
126
127     if (inet_pton(AF_INET6, bufp, sin6->sin6_addr.s6_addr) <= 0)
128         return (-1);
129
130     return 16;                  /* ?;) */
131 }
132
133 static int INET6_input(int type, char *bufp, struct sockaddr *sap)
134 {
135     switch (type) {
136     case 1:
137         return (INET6_getsock(bufp, sap));
138     default:
139         return (INET6_resolve(bufp, (struct sockaddr_in6 *) sap));
140     }
141 }
142
143
144 struct aftype inet6_aftype =
145 {
146     "inet6", NULL, /*"IPv6", */ AF_INET6, sizeof(struct in6_addr),
147     INET6_print, INET6_sprint, INET6_input, INET6_reserror,
148     INET6_rprint, INET6_rinput, NULL,
149
150     -1,
151     "/proc/net/if_inet6"
152 };
153
154
155 #endif                          /* HAVE_AFINET6 */