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