Imported Upstream version 3.3.3
[debian/amanda] / common-src / sockaddr-util.c
1 /*
2  * Copyright (c) 2007-2012 Zmanda, Inc.  All Rights Reserved.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17  *
18  * Contact information: Zmanda Inc, 465 S. Mathilda Ave., Suite 300
19  * Sunnyvale, CA 94086, USA, or: http://www.zmanda.com
20  *
21  * Author: Dustin J. Mitchell <dustin@zmanda.com>
22  */
23 /*
24  * Utility routines for handling sockaddrs
25  */
26
27 #include "amanda.h"
28 #include "sockaddr-util.h"
29
30 void
31 dump_sockaddr(
32     sockaddr_union *sa)
33 {
34 #ifdef WORKING_IPV6
35     char ipstr[INET6_ADDRSTRLEN];
36 #else
37     char ipstr[INET_ADDRSTRLEN];
38 #endif
39     int port;
40
41     port = SU_GET_PORT(sa);
42 #ifdef WORKING_IPV6
43     if (SU_GET_FAMILY(sa) == AF_INET6) {
44         inet_ntop(AF_INET6, &sa->sin6.sin6_addr, ipstr, sizeof(ipstr));
45         dbprintf("(sockaddr_in6 *)%p = { %d, %d, %s }\n",
46                  sa,
47                  SU_GET_FAMILY(sa),
48                  port,
49                  ipstr);
50     } else
51 #endif
52     {
53         inet_ntop(AF_INET, &sa->sin.sin_addr.s_addr, ipstr, sizeof(ipstr));
54         dbprintf("(sockaddr_in *)%p = { %d, %d, %s }\n",
55                  sa,
56                  SU_GET_FAMILY(sa),
57                  port,
58                  ipstr);
59     }
60 }
61
62
63 #ifdef WORKING_IPV6
64 static char mystr_sockaddr[INET6_ADDRSTRLEN + 20];
65 #else
66 static char mystr_sockaddr[INET_ADDRSTRLEN + 20];
67 #endif
68
69 char *
70 str_sockaddr(
71     sockaddr_union *sa)
72 {
73 #ifdef WORKING_IPV6
74     char ipstr[INET6_ADDRSTRLEN];
75 #else
76     char ipstr[INET_ADDRSTRLEN];
77 #endif
78     int port;
79
80     port = SU_GET_PORT(sa);
81 #ifdef WORKING_IPV6
82     if ( SU_GET_FAMILY(sa) == AF_INET6) {
83         inet_ntop(AF_INET6, &sa->sin6.sin6_addr, ipstr, sizeof(ipstr));
84     } else
85 #endif
86     {
87         inet_ntop(AF_INET, &sa->sin.sin_addr.s_addr, ipstr, sizeof(ipstr));
88     }
89     g_snprintf(mystr_sockaddr,sizeof(mystr_sockaddr),"%s:%d", ipstr, port);
90     mystr_sockaddr[sizeof(mystr_sockaddr)-1] = '\0';
91
92     return mystr_sockaddr;
93 }
94
95 char *
96 str_sockaddr_no_port(
97     sockaddr_union *sa)
98 {
99 #ifdef WORKING_IPV6
100     char ipstr[INET6_ADDRSTRLEN];
101 #else
102     char ipstr[INET_ADDRSTRLEN];
103 #endif
104
105 #ifdef WORKING_IPV6
106     if ( SU_GET_FAMILY(sa) == AF_INET6) {
107         inet_ntop(AF_INET6, &sa->sin6.sin6_addr, ipstr, sizeof(ipstr));
108     } else
109 #endif
110     {
111         inet_ntop(AF_INET, &sa->sin.sin_addr.s_addr, ipstr, sizeof(ipstr));
112     }
113     g_snprintf(mystr_sockaddr,sizeof(mystr_sockaddr),"%s", ipstr);
114     mystr_sockaddr[sizeof(mystr_sockaddr)-1] = '\0';
115
116     return mystr_sockaddr;
117 }
118
119 int
120 str_to_sockaddr(
121         const char *src,
122         sockaddr_union *dst)
123 {
124     int result;
125
126     g_debug("parsing %s", src);
127     /* try AF_INET first */
128     SU_INIT(dst, AF_INET);
129     if ((result = inet_pton(AF_INET, src, &dst->sin.sin_addr)) == 1)
130         return result;
131
132     /* otherwise try AF_INET6, if supported */
133 #ifdef WORKING_IPV6
134     SU_INIT(dst, AF_INET6);
135     return inet_pton(AF_INET6, src, &dst->sin6.sin6_addr);
136 #else
137     return result;
138 #endif
139 }
140
141 /* Unmap a V4MAPPED IPv6 address into its equivalent IPv4 address.  The location
142  * TMP is used to store the rewritten address, if necessary.  Returns a pointer
143  * to the unmapped address.
144  */
145 #if defined(WORKING_IPV6) && defined(IN6_IS_ADDR_V4MAPPED)
146 static sockaddr_union *
147 unmap_v4mapped(
148     sockaddr_union *sa,
149     sockaddr_union *tmp)
150 {
151     if (SU_GET_FAMILY(sa) == AF_INET6 && IN6_IS_ADDR_V4MAPPED(&sa->sin6.sin6_addr)) {
152         SU_INIT(tmp, AF_INET);
153         SU_SET_PORT(tmp, SU_GET_PORT(sa));
154         /* extract the v4 address from byte 12 of the v6 address */
155         memcpy(&tmp->sin.sin_addr.s_addr,
156                &sa->sin6.sin6_addr.s6_addr[12],
157                sizeof(struct in_addr));
158         return tmp;
159     }
160
161     return sa;
162 }
163 #else
164 /* nothing to do if no IPv6 */
165 #define unmap_v4mapped(sa, tmp) ((void)tmp, sa)
166 #endif
167
168 int
169 cmp_sockaddr(
170     sockaddr_union *ss1,
171     sockaddr_union *ss2,
172     int addr_only)
173 {
174     sockaddr_union tmp1, tmp2;
175
176     /* if addresses are v4mapped, "unmap" them */
177     ss1 = unmap_v4mapped(ss1, &tmp1);
178     ss2 = unmap_v4mapped(ss2, &tmp2);
179
180     if (SU_GET_FAMILY(ss1) == SU_GET_FAMILY(ss2)) {
181         if (addr_only) {
182 #ifdef WORKING_IPV6
183             if(SU_GET_FAMILY(ss1) == AF_INET6)
184                 return memcmp(
185                     &ss1->sin6.sin6_addr,
186                     &ss2->sin6.sin6_addr,
187                     sizeof(ss1->sin6.sin6_addr));
188             else
189 #endif
190                 return memcmp(
191                     &ss1->sin.sin_addr,
192                     &ss2->sin.sin_addr,
193                     sizeof(ss1->sin.sin_addr));
194         } else {
195             return memcmp(ss1, ss2, SS_LEN(ss1));
196         }
197     } else {
198         /* compare families to give a total order */
199         if (SU_GET_FAMILY(ss1) < SU_GET_FAMILY(ss2))
200             return -1;
201         else
202             return 1;
203     }
204 }
205