Imported Upstream version 3.1.0
[debian/amanda] / common-src / sockaddr-util.c
1 /*
2  * Copyright (c) 2007,2008 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 "sockaddr-util.h"
27
28 void
29 dump_sockaddr(
30     sockaddr_union *sa)
31 {
32 #ifdef WORKING_IPV6
33     char ipstr[INET6_ADDRSTRLEN];
34 #else
35     char ipstr[INET_ADDRSTRLEN];
36 #endif
37     int port;
38
39     port = SU_GET_PORT(sa);
40 #ifdef WORKING_IPV6
41     if (SU_GET_FAMILY(sa) == AF_INET6) {
42         inet_ntop(AF_INET6, &sa->sin6.sin6_addr, ipstr, sizeof(ipstr));
43         dbprintf("(sockaddr_in6 *)%p = { %d, %d, %s }\n",
44                  sa,
45                  SU_GET_FAMILY(sa),
46                  port,
47                  ipstr);
48     } else
49 #endif
50     {
51         inet_ntop(AF_INET, &sa->sin.sin_addr.s_addr, ipstr, sizeof(ipstr));
52         dbprintf("(sockaddr_in *)%p = { %d, %d, %s }\n",
53                  sa,
54                  SU_GET_FAMILY(sa),
55                  port,
56                  ipstr);
57     }
58 }
59
60
61 #ifdef WORKING_IPV6
62 static char mystr_sockaddr[INET6_ADDRSTRLEN + 20];
63 #else
64 static char mystr_sockaddr[INET_ADDRSTRLEN + 20];
65 #endif
66
67 char *
68 str_sockaddr(
69     sockaddr_union *sa)
70 {
71 #ifdef WORKING_IPV6
72     char ipstr[INET6_ADDRSTRLEN];
73 #else
74     char ipstr[INET_ADDRSTRLEN];
75 #endif
76     int port;
77
78     port = SU_GET_PORT(sa);
79 #ifdef WORKING_IPV6
80     if ( SU_GET_FAMILY(sa) == AF_INET6) {
81         inet_ntop(AF_INET6, &sa->sin6.sin6_addr, ipstr, sizeof(ipstr));
82     } else
83 #endif
84     {
85         inet_ntop(AF_INET, &sa->sin.sin_addr.s_addr, ipstr, sizeof(ipstr));
86     }
87     g_snprintf(mystr_sockaddr,sizeof(mystr_sockaddr),"%s.%d", ipstr, port);
88     mystr_sockaddr[sizeof(mystr_sockaddr)-1] = '\0';
89
90     return mystr_sockaddr;
91 }
92
93 /* Unmap a V4MAPPED IPv6 address into its equivalent IPv4 address.  The location
94  * TMP is used to store the rewritten address, if necessary.  Returns a pointer
95  * to the unmapped address.
96  */
97 #if defined(WORKING_IPV6) && defined(IN6_IS_ADDR_V4MAPPED)
98 static sockaddr_union *
99 unmap_v4mapped(
100     sockaddr_union *sa,
101     sockaddr_union *tmp)
102 {
103     if (SU_GET_FAMILY(sa) == AF_INET6 && IN6_IS_ADDR_V4MAPPED(&sa->sin6.sin6_addr)) {
104         SU_INIT(tmp, AF_INET);
105         SU_SET_PORT(tmp, SU_GET_PORT(sa));
106         /* extract the v4 address from byte 12 of the v6 address */
107         memcpy(&tmp->sin.sin_addr.s_addr,
108                &sa->sin6.sin6_addr.s6_addr[12],
109                sizeof(struct in_addr));
110         return tmp;
111     }
112
113     return sa;
114 }
115 #else
116 /* nothing to do if no IPv6 */
117 #define unmap_v4mapped(sa, tmp) ((void)tmp, sa)
118 #endif
119
120 int
121 cmp_sockaddr(
122     sockaddr_union *ss1,
123     sockaddr_union *ss2,
124     int addr_only)
125 {
126     sockaddr_union tmp1, tmp2;
127
128     /* if addresses are v4mapped, "unmap" them */
129     ss1 = unmap_v4mapped(ss1, &tmp1);
130     ss2 = unmap_v4mapped(ss2, &tmp2);
131
132     if (SU_GET_FAMILY(ss1) == SU_GET_FAMILY(ss2)) {
133         if (addr_only) {
134 #ifdef WORKING_IPV6
135             if(SU_GET_FAMILY(ss1) == AF_INET6)
136                 return memcmp(
137                     &ss1->sin6.sin6_addr,
138                     &ss2->sin6.sin6_addr,
139                     sizeof(ss1->sin6.sin6_addr));
140             else
141 #endif
142                 return memcmp(
143                     &ss1->sin.sin_addr,
144                     &ss2->sin.sin_addr,
145                     sizeof(ss1->sin.sin_addr));
146         } else {
147             return memcmp(ss1, ss2, SS_LEN(ss1));
148         }
149     } else {
150         /* compare families to give a total order */
151         if (SU_GET_FAMILY(ss1) < SU_GET_FAMILY(ss2))
152             return -1;
153         else
154             return 1;
155     }
156 }
157