1 /*
2  * Copyright (c) 2007-2013 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
dump_sockaddr(sockaddr_union * sa)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 *
str_sockaddr(sockaddr_union * sa)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 *
str_sockaddr_no_port(sockaddr_union * sa)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 char *
str_sockaddr_r(sockaddr_union * sa,char * strsockaddr,socklen_t size)120 str_sockaddr_r(
121     sockaddr_union *sa,
122     char *strsockaddr,
123     socklen_t size)
124 {
125 #ifdef WORKING_IPV6
126     char ipstr[INET6_ADDRSTRLEN];
127 #else
128     char ipstr[INET_ADDRSTRLEN];
129 #endif
130     int port;
131 
132     port = SU_GET_PORT(sa);
133 #ifdef WORKING_IPV6
134     if ( SU_GET_FAMILY(sa) == AF_INET6) {
135 	inet_ntop(AF_INET6, &sa->sin6.sin6_addr, ipstr, sizeof(ipstr));
136     } else
137 #endif
138     {
139 	inet_ntop(AF_INET, &sa->sin.sin_addr.s_addr, ipstr, sizeof(ipstr));
140     }
141     g_snprintf(strsockaddr, size, "%s:%d", ipstr, port);
142 
143     return strsockaddr;
144 }
145 
146 char *
str_sockaddr_no_port_r(sockaddr_union * sa,char * strsockaddr,socklen_t size)147 str_sockaddr_no_port_r(
148     sockaddr_union *sa,
149     char *strsockaddr,
150     socklen_t size)
151 {
152 #ifdef WORKING_IPV6
153     char ipstr[INET6_ADDRSTRLEN];
154 #else
155     char ipstr[INET_ADDRSTRLEN];
156 #endif
157 
158 #ifdef WORKING_IPV6
159     if ( SU_GET_FAMILY(sa) == AF_INET6) {
160 	inet_ntop(AF_INET6, &sa->sin6.sin6_addr, ipstr, sizeof(ipstr));
161     } else
162 #endif
163     {
164 	inet_ntop(AF_INET, &sa->sin.sin_addr.s_addr, ipstr, sizeof(ipstr));
165     }
166     g_snprintf(strsockaddr, size, "%s", ipstr);
167 
168     return strsockaddr;
169 }
170 
171 int
str_to_sockaddr(const char * src,sockaddr_union * dst)172 str_to_sockaddr(
173 	const char *src,
174 	sockaddr_union *dst)
175 {
176     int result;
177 
178     g_debug("parsing %s", src);
179     /* try AF_INET first */
180     SU_INIT(dst, AF_INET);
181     if ((result = inet_pton(AF_INET, src, &dst->sin.sin_addr)) == 1)
182 	return result;
183 
184     /* otherwise try AF_INET6, if supported */
185 #ifdef WORKING_IPV6
186     SU_INIT(dst, AF_INET6);
187     return inet_pton(AF_INET6, src, &dst->sin6.sin6_addr);
188 #else
189     return result;
190 #endif
191 }
192 
193 /* Unmap a V4MAPPED IPv6 address into its equivalent IPv4 address.  The location
194  * TMP is used to store the rewritten address, if necessary.  Returns a pointer
195  * to the unmapped address.
196  */
197 #if defined(WORKING_IPV6) && defined(IN6_IS_ADDR_V4MAPPED)
198 static sockaddr_union *
unmap_v4mapped(sockaddr_union * sa,sockaddr_union * tmp)199 unmap_v4mapped(
200     sockaddr_union *sa,
201     sockaddr_union *tmp)
202 {
203     if (SU_GET_FAMILY(sa) == AF_INET6 && IN6_IS_ADDR_V4MAPPED(&sa->sin6.sin6_addr)) {
204 	SU_INIT(tmp, AF_INET);
205 	SU_SET_PORT(tmp, SU_GET_PORT(sa));
206 	/* extract the v4 address from byte 12 of the v6 address */
207 	memcpy(&tmp->sin.sin_addr.s_addr,
208 	       &sa->sin6.sin6_addr.s6_addr[12],
209 	       sizeof(struct in_addr));
210 	return tmp;
211     }
212 
213     return sa;
214 }
215 #else
216 /* nothing to do if no IPv6 */
217 #define unmap_v4mapped(sa, tmp) ((void)tmp, sa)
218 #endif
219 
220 int
cmp_sockaddr(sockaddr_union * ss1,sockaddr_union * ss2,int addr_only)221 cmp_sockaddr(
222     sockaddr_union *ss1,
223     sockaddr_union *ss2,
224     int addr_only)
225 {
226     sockaddr_union tmp1, tmp2;
227 
228     /* if addresses are v4mapped, "unmap" them */
229     ss1 = unmap_v4mapped(ss1, &tmp1);
230     ss2 = unmap_v4mapped(ss2, &tmp2);
231 
232     if (SU_GET_FAMILY(ss1) == SU_GET_FAMILY(ss2)) {
233         if (addr_only) {
234 #ifdef WORKING_IPV6
235             if(SU_GET_FAMILY(ss1) == AF_INET6)
236                 return memcmp(
237                     &ss1->sin6.sin6_addr,
238                     &ss2->sin6.sin6_addr,
239                     sizeof(ss1->sin6.sin6_addr));
240             else
241 #endif
242                 return memcmp(
243                     &ss1->sin.sin_addr,
244                     &ss2->sin.sin_addr,
245                     sizeof(ss1->sin.sin_addr));
246         } else {
247             return memcmp(ss1, ss2, SS_LEN(ss1));
248         }
249     } else {
250         /* compare families to give a total order */
251         if (SU_GET_FAMILY(ss1) < SU_GET_FAMILY(ss2))
252             return -1;
253         else
254             return 1;
255     }
256 }
257 
258