Imported Upstream version 3.2.0
[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 int
95 str_to_sockaddr(
96         const char *src,
97         sockaddr_union *dst)
98 {
99     int result;
100
101     g_debug("parsing %s", src);
102     /* try AF_INET first */
103     SU_INIT(dst, AF_INET);
104     if ((result = inet_pton(AF_INET, src, &dst->sin.sin_addr)) == 1)
105         return result;
106
107     /* otherwise try AF_INET6, if supported */
108 #ifdef WORKING_IPV6
109     SU_INIT(dst, AF_INET6);
110     return inet_pton(AF_INET6, src, &dst->sin6.sin6_addr);
111 #else
112     return result;
113 #endif
114 }
115
116 /* Unmap a V4MAPPED IPv6 address into its equivalent IPv4 address.  The location
117  * TMP is used to store the rewritten address, if necessary.  Returns a pointer
118  * to the unmapped address.
119  */
120 #if defined(WORKING_IPV6) && defined(IN6_IS_ADDR_V4MAPPED)
121 static sockaddr_union *
122 unmap_v4mapped(
123     sockaddr_union *sa,
124     sockaddr_union *tmp)
125 {
126     if (SU_GET_FAMILY(sa) == AF_INET6 && IN6_IS_ADDR_V4MAPPED(&sa->sin6.sin6_addr)) {
127         SU_INIT(tmp, AF_INET);
128         SU_SET_PORT(tmp, SU_GET_PORT(sa));
129         /* extract the v4 address from byte 12 of the v6 address */
130         memcpy(&tmp->sin.sin_addr.s_addr,
131                &sa->sin6.sin6_addr.s6_addr[12],
132                sizeof(struct in_addr));
133         return tmp;
134     }
135
136     return sa;
137 }
138 #else
139 /* nothing to do if no IPv6 */
140 #define unmap_v4mapped(sa, tmp) ((void)tmp, sa)
141 #endif
142
143 int
144 cmp_sockaddr(
145     sockaddr_union *ss1,
146     sockaddr_union *ss2,
147     int addr_only)
148 {
149     sockaddr_union tmp1, tmp2;
150
151     /* if addresses are v4mapped, "unmap" them */
152     ss1 = unmap_v4mapped(ss1, &tmp1);
153     ss2 = unmap_v4mapped(ss2, &tmp2);
154
155     if (SU_GET_FAMILY(ss1) == SU_GET_FAMILY(ss2)) {
156         if (addr_only) {
157 #ifdef WORKING_IPV6
158             if(SU_GET_FAMILY(ss1) == AF_INET6)
159                 return memcmp(
160                     &ss1->sin6.sin6_addr,
161                     &ss2->sin6.sin6_addr,
162                     sizeof(ss1->sin6.sin6_addr));
163             else
164 #endif
165                 return memcmp(
166                     &ss1->sin.sin_addr,
167                     &ss2->sin.sin_addr,
168                     sizeof(ss1->sin.sin_addr));
169         } else {
170             return memcmp(ss1, ss2, SS_LEN(ss1));
171         }
172     } else {
173         /* compare families to give a total order */
174         if (SU_GET_FAMILY(ss1) < SU_GET_FAMILY(ss2))
175             return -1;
176         else
177             return 1;
178     }
179 }
180