2 Unix SMB/CIFS implementation.
3 Samba utility functions
4 Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2008
5 Copyright (C) Andrew Tridgell 1992-1998
6 Copyright (C) Jeremy Allison 1992-2007
7 Copyright (C) Simo Sorce 2001
8 Copyright (C) Jim McDonough (jmcd@us.ibm.com) 2003.
9 Copyright (C) James J Myers 2003
10 Copyright (C) Tim Potter 2000-2001
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 3 of the License, or
15 (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #include "system/network.h"
28 #include "system/locale.h"
29 #include "system/filesys.h"
32 /*******************************************************************
33 Set an address to INADDR_ANY.
34 ******************************************************************/
36 void zero_sockaddr(struct sockaddr_storage
*pss
)
38 memset(pss
, '\0', sizeof(*pss
));
39 /* Ensure we're at least a valid sockaddr-storage. */
40 pss
->ss_family
= AF_INET
;
46 bool interpret_string_addr_internal(struct addrinfo
**ppres
,
47 const char *str
, int flags
)
50 struct addrinfo hints
;
52 memset(&hints
, '\0', sizeof(hints
));
53 /* By default make sure it supports TCP. */
54 hints
.ai_socktype
= SOCK_STREAM
;
55 hints
.ai_flags
= flags
;
57 /* Linux man page on getaddinfo() says port will be
58 uninitialized when service string in NULL */
60 ret
= getaddrinfo(str
, NULL
,
65 DEBUG(3,("interpret_string_addr_internal: getaddrinfo failed "
74 /*******************************************************************
75 Map a text hostname or IP address (IPv4 or IPv6) into a
76 struct sockaddr_storage. Takes a flag which allows it to
77 prefer an IPv4 address (needed for DC's).
78 ******************************************************************/
80 static bool interpret_string_addr_pref(struct sockaddr_storage
*pss
,
85 struct addrinfo
*res
= NULL
;
86 #if defined(HAVE_IPV6)
87 char addr
[INET6_ADDRSTRLEN
];
88 unsigned int scope_id
= 0;
90 if (strchr_m(str
, ':')) {
91 char *p
= strchr_m(str
, '%');
94 * Cope with link-local.
95 * This is IP:v6:addr%ifname.
98 if (p
&& (p
> str
) && ((scope_id
= if_nametoindex(p
+1)) != 0)) {
100 MIN(PTR_DIFF(p
,str
)+1,
109 if (!interpret_string_addr_internal(&res
, str
, flags
|AI_ADDRCONFIG
)) {
119 for (p
= res
; p
; p
= p
->ai_next
) {
120 if (p
->ai_family
== AF_INET
) {
121 memcpy(pss
, p
->ai_addr
, p
->ai_addrlen
);
126 /* Copy the first sockaddr. */
127 memcpy(pss
, res
->ai_addr
, res
->ai_addrlen
);
130 /* Copy the first sockaddr. */
131 memcpy(pss
, res
->ai_addr
, res
->ai_addrlen
);
134 #if defined(HAVE_IPV6)
135 if (pss
->ss_family
== AF_INET6
&& scope_id
) {
136 struct sockaddr_in6
*ps6
= (struct sockaddr_in6
*)pss
;
137 if (IN6_IS_ADDR_LINKLOCAL(&ps6
->sin6_addr
) &&
138 ps6
->sin6_scope_id
== 0) {
139 ps6
->sin6_scope_id
= scope_id
;
148 /*******************************************************************
149 Map a text hostname or IP address (IPv4 or IPv6) into a
150 struct sockaddr_storage. Address agnostic version.
151 ******************************************************************/
153 bool interpret_string_addr(struct sockaddr_storage
*pss
,
157 return interpret_string_addr_pref(pss
,
163 /*******************************************************************
164 Map a text hostname or IP address (IPv4 or IPv6) into a
165 struct sockaddr_storage. Version that prefers IPv4.
166 ******************************************************************/
168 bool interpret_string_addr_prefer_ipv4(struct sockaddr_storage
*pss
,
172 return interpret_string_addr_pref(pss
,
179 * Interpret an internet address or name into an IP address in 4 byte form.
180 * RETURNS IN NETWORK BYTE ORDER (big endian).
183 uint32_t interpret_addr(const char *str
)
187 /* If it's in the form of an IP address then
188 * get the lib to interpret it */
189 if (is_ipaddress_v4(str
)) {
192 if (inet_pton(AF_INET
, str
, &dest
) <= 0) {
193 /* Error - this shouldn't happen ! */
194 DEBUG(0,("interpret_addr: inet_pton failed "
199 ret
= dest
.s_addr
; /* NETWORK BYTE ORDER ! */
201 /* Otherwise assume it's a network name of some sort and use
203 struct addrinfo
*res
= NULL
;
204 struct addrinfo
*res_list
= NULL
;
205 if (!interpret_string_addr_internal(&res_list
,
208 DEBUG(3,("interpret_addr: Unknown host. %s\n",str
));
212 /* Find the first IPv4 address. */
213 for (res
= res_list
; res
; res
= res
->ai_next
) {
214 if (res
->ai_family
!= AF_INET
) {
217 if (res
->ai_addr
== NULL
) {
223 DEBUG(3,("interpret_addr: host address is "
224 "invalid for host %s\n",str
));
226 freeaddrinfo(res_list
);
231 &((struct sockaddr_in
*)res
->ai_addr
)->sin_addr
.s_addr
,
234 freeaddrinfo(res_list
);
238 /* This is so bogus - all callers need fixing... JRA. */
239 if (ret
== (uint32_t)-1) {
247 A convenient addition to interpret_addr().
249 _PUBLIC_
struct in_addr
interpret_addr2(const char *str
)
252 uint32_t a
= interpret_addr(str
);
258 Check if an IP is the 0.0.0.0.
261 _PUBLIC_
bool is_zero_ip_v4(struct in_addr ip
)
263 return ip
.s_addr
== 0;
267 Are two IPs on the same subnet?
270 _PUBLIC_
bool same_net_v4(struct in_addr ip1
, struct in_addr ip2
, struct in_addr mask
)
272 uint32_t net1
,net2
,nmask
;
274 nmask
= ntohl(mask
.s_addr
);
275 net1
= ntohl(ip1
.s_addr
);
276 net2
= ntohl(ip2
.s_addr
);
278 return((net1
& nmask
) == (net2
& nmask
));
282 * Return true if a string could be an IPv4 address.
285 bool is_ipaddress_v4(const char *str
)
290 ret
= inet_pton(AF_INET
, str
, &dest
);
298 * Return true if a string could be an IPv4 or IPv6 address.
301 bool is_ipaddress(const char *str
)
303 #if defined(HAVE_IPV6)
306 if (strchr_m(str
, ':')) {
307 char addr
[INET6_ADDRSTRLEN
];
308 struct in6_addr dest6
;
309 const char *sp
= str
;
310 char *p
= strchr_m(str
, '%');
313 * Cope with link-local.
314 * This is IP:v6:addr%ifname.
317 if (p
&& (p
> str
) && (if_nametoindex(p
+1) != 0)) {
319 MIN(PTR_DIFF(p
,str
)+1,
323 ret
= inet_pton(AF_INET6
, sp
, &dest6
);
329 return is_ipaddress_v4(str
);
333 * Is a sockaddr a broadcast address ?
336 bool is_broadcast_addr(const struct sockaddr
*pss
)
338 #if defined(HAVE_IPV6)
339 if (pss
->sa_family
== AF_INET6
) {
340 const struct in6_addr
*sin6
=
341 &((const struct sockaddr_in6
*)pss
)->sin6_addr
;
342 return IN6_IS_ADDR_MULTICAST(sin6
);
345 if (pss
->sa_family
== AF_INET
) {
347 ntohl(((const struct sockaddr_in
*)pss
)->sin_addr
.s_addr
);
348 return addr
== INADDR_BROADCAST
;
354 * Check if an IPv7 is 127.0.0.1
356 bool is_loopback_ip_v4(struct in_addr ip
)
359 a
.s_addr
= htonl(INADDR_LOOPBACK
);
360 return(ip
.s_addr
== a
.s_addr
);
364 * Check if a struct sockaddr is the loopback address.
366 bool is_loopback_addr(const struct sockaddr
*pss
)
368 #if defined(HAVE_IPV6)
369 if (pss
->sa_family
== AF_INET6
) {
370 const struct in6_addr
*pin6
=
371 &((const struct sockaddr_in6
*)pss
)->sin6_addr
;
372 return IN6_IS_ADDR_LOOPBACK(pin6
);
375 if (pss
->sa_family
== AF_INET
) {
376 const struct in_addr
*pin
= &((const struct sockaddr_in
*)pss
)->sin_addr
;
377 return is_loopback_ip_v4(*pin
);
383 * Check if a struct sockaddr has an unspecified address.
385 bool is_zero_addr(const struct sockaddr
*pss
)
387 #if defined(HAVE_IPV6)
388 if (pss
->sa_family
== AF_INET6
) {
389 const struct in6_addr
*pin6
=
390 &((const struct sockaddr_in6
*)pss
)->sin6_addr
;
391 return IN6_IS_ADDR_UNSPECIFIED(pin6
);
394 if (pss
->sa_family
== AF_INET
) {
395 const struct in_addr
*pin
= &((const struct sockaddr_in
*)pss
)->sin_addr
;
396 return is_zero_ip_v4(*pin
);
402 * Set an IP to 0.0.0.0.
404 void zero_ip_v4(struct in_addr
*ip
)
406 memset(ip
, '\0', sizeof(struct in_addr
));
410 * Convert an IPv4 struct in_addr to a struct sockaddr_storage.
412 void in_addr_to_sockaddr_storage(struct sockaddr_storage
*ss
,
415 struct sockaddr_in
*sa
= (struct sockaddr_in
*)ss
;
416 memset(ss
, '\0', sizeof(*ss
));
417 sa
->sin_family
= AF_INET
;
421 #if defined(HAVE_IPV6)
423 * Convert an IPv6 struct in_addr to a struct sockaddr_storage.
425 void in6_addr_to_sockaddr_storage(struct sockaddr_storage
*ss
,
428 struct sockaddr_in6
*sa
= (struct sockaddr_in6
*)ss
;
429 memset(ss
, '\0', sizeof(*ss
));
430 sa
->sin6_family
= AF_INET6
;
436 * Are two IPs on the same subnet?
438 bool same_net(const struct sockaddr
*ip1
,
439 const struct sockaddr
*ip2
,
440 const struct sockaddr
*mask
)
442 if (ip1
->sa_family
!= ip2
->sa_family
) {
443 /* Never on the same net. */
447 #if defined(HAVE_IPV6)
448 if (ip1
->sa_family
== AF_INET6
) {
449 struct sockaddr_in6 ip1_6
= *(const struct sockaddr_in6
*)ip1
;
450 struct sockaddr_in6 ip2_6
= *(const struct sockaddr_in6
*)ip2
;
451 struct sockaddr_in6 mask_6
= *(const struct sockaddr_in6
*)mask
;
452 char *p1
= (char *)&ip1_6
.sin6_addr
;
453 char *p2
= (char *)&ip2_6
.sin6_addr
;
454 char *m
= (char *)&mask_6
.sin6_addr
;
457 for (i
= 0; i
< sizeof(struct in6_addr
); i
++) {
462 return (memcmp(&ip1_6
.sin6_addr
,
464 sizeof(struct in6_addr
)) == 0);
467 if (ip1
->sa_family
== AF_INET
) {
468 return same_net_v4(((const struct sockaddr_in
*)ip1
)->sin_addr
,
469 ((const struct sockaddr_in
*)ip2
)->sin_addr
,
470 ((const struct sockaddr_in
*)mask
)->sin_addr
);
476 * Are two sockaddr 's the same family and address ? Ignore port etc.
479 bool sockaddr_equal(const struct sockaddr
*ip1
,
480 const struct sockaddr
*ip2
)
482 if (ip1
->sa_family
!= ip2
->sa_family
) {
483 /* Never the same. */
487 #if defined(HAVE_IPV6)
488 if (ip1
->sa_family
== AF_INET6
) {
489 return (memcmp(&((const struct sockaddr_in6
*)ip1
)->sin6_addr
,
490 &((const struct sockaddr_in6
*)ip2
)->sin6_addr
,
491 sizeof(struct in6_addr
)) == 0);
494 if (ip1
->sa_family
== AF_INET
) {
495 return (memcmp(&((const struct sockaddr_in
*)ip1
)->sin_addr
,
496 &((const struct sockaddr_in
*)ip2
)->sin_addr
,
497 sizeof(struct in_addr
)) == 0);
503 * Is an IP address the INADDR_ANY or in6addr_any value ?
505 bool is_address_any(const struct sockaddr
*psa
)
507 #if defined(HAVE_IPV6)
508 if (psa
->sa_family
== AF_INET6
) {
509 const struct sockaddr_in6
*si6
= (const struct sockaddr_in6
*)psa
;
510 if (memcmp(&in6addr_any
,
512 sizeof(in6addr_any
)) == 0) {
518 if (psa
->sa_family
== AF_INET
) {
519 const struct sockaddr_in
*si
= (const struct sockaddr_in
*)psa
;
520 if (si
->sin_addr
.s_addr
== INADDR_ANY
) {
528 void set_sockaddr_port(struct sockaddr
*psa
, uint16_t port
)
530 #if defined(HAVE_IPV6)
531 if (psa
->sa_family
== AF_INET6
) {
532 ((struct sockaddr_in6
*)psa
)->sin6_port
= htons(port
);
535 if (psa
->sa_family
== AF_INET
) {
536 ((struct sockaddr_in
*)psa
)->sin_port
= htons(port
);