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
)
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
;
54 /* By default make sure it supports TCP. */
55 hints
.ai_socktype
= SOCK_STREAM
;
56 hints
.ai_flags
= flags
;
58 /* Linux man page on getaddrinfo() says port will be
59 uninitialized when service string in NULL */
61 ret
= getaddrinfo(str
, NULL
,
66 DEBUG(3,("interpret_string_addr_internal: getaddrinfo failed "
75 /*******************************************************************
76 Map a text hostname or IP address (IPv4 or IPv6) into a
77 struct sockaddr_storage. Takes a flag which allows it to
78 prefer an IPv4 address (needed for DC's).
79 ******************************************************************/
81 static bool interpret_string_addr_pref(struct sockaddr_storage
*pss
,
86 struct addrinfo
*res
= NULL
;
87 #if defined(HAVE_IPV6)
88 char addr
[INET6_ADDRSTRLEN
];
89 unsigned int scope_id
= 0;
91 if (strchr_m(str
, ':')) {
92 char *p
= strchr_m(str
, '%');
95 * Cope with link-local.
96 * This is IP:v6:addr%ifname.
99 if (p
&& (p
> str
) && ((scope_id
= if_nametoindex(p
+1)) != 0)) {
101 MIN(PTR_DIFF(p
,str
)+1,
110 if (!interpret_string_addr_internal(&res
, str
, flags
|AI_ADDRCONFIG
)) {
120 for (p
= res
; p
; p
= p
->ai_next
) {
121 if (p
->ai_family
== AF_INET
) {
122 memcpy(pss
, p
->ai_addr
, p
->ai_addrlen
);
127 /* Copy the first sockaddr. */
128 memcpy(pss
, res
->ai_addr
, res
->ai_addrlen
);
131 /* Copy the first sockaddr. */
132 memcpy(pss
, res
->ai_addr
, res
->ai_addrlen
);
135 #if defined(HAVE_IPV6)
136 if (pss
->ss_family
== AF_INET6
&& scope_id
) {
137 struct sockaddr_in6
*ps6
= (struct sockaddr_in6
*)pss
;
138 if (IN6_IS_ADDR_LINKLOCAL(&ps6
->sin6_addr
) &&
139 ps6
->sin6_scope_id
== 0) {
140 ps6
->sin6_scope_id
= scope_id
;
149 /*******************************************************************
150 Map a text hostname or IP address (IPv4 or IPv6) into a
151 struct sockaddr_storage. Address agnostic version.
152 ******************************************************************/
154 bool interpret_string_addr(struct sockaddr_storage
*pss
,
158 return interpret_string_addr_pref(pss
,
164 /*******************************************************************
165 Map a text hostname or IP address (IPv4 or IPv6) into a
166 struct sockaddr_storage. Version that prefers IPv4.
167 ******************************************************************/
169 bool interpret_string_addr_prefer_ipv4(struct sockaddr_storage
*pss
,
173 return interpret_string_addr_pref(pss
,
180 * Interpret an internet address or name into an IP address in 4 byte form.
181 * RETURNS IN NETWORK BYTE ORDER (big endian).
184 uint32_t interpret_addr(const char *str
)
188 /* If it's in the form of an IP address then
189 * get the lib to interpret it */
190 if (is_ipaddress_v4(str
)) {
193 if (inet_pton(AF_INET
, str
, &dest
) <= 0) {
194 /* Error - this shouldn't happen ! */
195 DEBUG(0,("interpret_addr: inet_pton failed "
200 ret
= dest
.s_addr
; /* NETWORK BYTE ORDER ! */
202 /* Otherwise assume it's a network name of some sort and use
204 struct addrinfo
*res
= NULL
;
205 struct addrinfo
*res_list
= NULL
;
206 if (!interpret_string_addr_internal(&res_list
,
209 DEBUG(3,("interpret_addr: Unknown host. %s\n",str
));
213 /* Find the first IPv4 address. */
214 for (res
= res_list
; res
; res
= res
->ai_next
) {
215 if (res
->ai_family
!= AF_INET
) {
218 if (res
->ai_addr
== NULL
) {
224 DEBUG(3,("interpret_addr: host address is "
225 "invalid for host %s\n",str
));
227 freeaddrinfo(res_list
);
232 &((struct sockaddr_in
*)res
->ai_addr
)->sin_addr
.s_addr
,
235 freeaddrinfo(res_list
);
239 /* This is so bogus - all callers need fixing... JRA. */
240 if (ret
== (uint32_t)-1) {
248 A convenient addition to interpret_addr().
250 _PUBLIC_
struct in_addr
interpret_addr2(const char *str
)
253 uint32_t a
= interpret_addr(str
);
259 Check if an IP is the 0.0.0.0.
262 _PUBLIC_
bool is_zero_ip_v4(struct in_addr ip
)
264 return ip
.s_addr
== 0;
268 Are two IPs on the same subnet?
271 _PUBLIC_
bool same_net_v4(struct in_addr ip1
, struct in_addr ip2
, struct in_addr mask
)
273 uint32_t net1
,net2
,nmask
;
275 nmask
= ntohl(mask
.s_addr
);
276 net1
= ntohl(ip1
.s_addr
);
277 net2
= ntohl(ip2
.s_addr
);
279 return((net1
& nmask
) == (net2
& nmask
));
283 * Return true if a string could be an IPv4 address.
286 bool is_ipaddress_v4(const char *str
)
291 ret
= inet_pton(AF_INET
, str
, &dest
);
299 * Return true if a string could be an IPv4 or IPv6 address.
302 bool is_ipaddress(const char *str
)
304 #if defined(HAVE_IPV6)
307 if (strchr_m(str
, ':')) {
308 char addr
[INET6_ADDRSTRLEN
];
309 struct in6_addr dest6
;
310 const char *sp
= str
;
311 char *p
= strchr_m(str
, '%');
314 * Cope with link-local.
315 * This is IP:v6:addr%ifname.
318 if (p
&& (p
> str
) && (if_nametoindex(p
+1) != 0)) {
320 MIN(PTR_DIFF(p
,str
)+1,
324 ret
= inet_pton(AF_INET6
, sp
, &dest6
);
330 return is_ipaddress_v4(str
);
334 * Is a sockaddr a broadcast address ?
337 bool is_broadcast_addr(const struct sockaddr
*pss
)
339 #if defined(HAVE_IPV6)
340 if (pss
->sa_family
== AF_INET6
) {
341 const struct in6_addr
*sin6
=
342 &((const struct sockaddr_in6
*)pss
)->sin6_addr
;
343 return IN6_IS_ADDR_MULTICAST(sin6
);
346 if (pss
->sa_family
== AF_INET
) {
348 ntohl(((const struct sockaddr_in
*)pss
)->sin_addr
.s_addr
);
349 return addr
== INADDR_BROADCAST
;
355 * Check if an IPv7 is 127.0.0.1
357 bool is_loopback_ip_v4(struct in_addr ip
)
360 a
.s_addr
= htonl(INADDR_LOOPBACK
);
361 return(ip
.s_addr
== a
.s_addr
);
365 * Check if a struct sockaddr is the loopback address.
367 bool is_loopback_addr(const struct sockaddr
*pss
)
369 #if defined(HAVE_IPV6)
370 if (pss
->sa_family
== AF_INET6
) {
371 const struct in6_addr
*pin6
=
372 &((const struct sockaddr_in6
*)pss
)->sin6_addr
;
373 return IN6_IS_ADDR_LOOPBACK(pin6
);
376 if (pss
->sa_family
== AF_INET
) {
377 const struct in_addr
*pin
= &((const struct sockaddr_in
*)pss
)->sin_addr
;
378 return is_loopback_ip_v4(*pin
);
384 * Check if a struct sockaddr has an unspecified address.
386 bool is_zero_addr(const struct sockaddr
*pss
)
388 #if defined(HAVE_IPV6)
389 if (pss
->sa_family
== AF_INET6
) {
390 const struct in6_addr
*pin6
=
391 &((const struct sockaddr_in6
*)pss
)->sin6_addr
;
392 return IN6_IS_ADDR_UNSPECIFIED(pin6
);
395 if (pss
->sa_family
== AF_INET
) {
396 const struct in_addr
*pin
= &((const struct sockaddr_in
*)pss
)->sin_addr
;
397 return is_zero_ip_v4(*pin
);
403 * Set an IP to 0.0.0.0.
405 void zero_ip_v4(struct in_addr
*ip
)
407 memset(ip
, '\0', sizeof(struct in_addr
));
411 * Convert an IPv4 struct in_addr to a struct sockaddr_storage.
413 void in_addr_to_sockaddr_storage(struct sockaddr_storage
*ss
,
416 struct sockaddr_in
*sa
= (struct sockaddr_in
*)ss
;
417 memset(ss
, '\0', sizeof(*ss
));
418 sa
->sin_family
= AF_INET
;
422 #if defined(HAVE_IPV6)
424 * Convert an IPv6 struct in_addr to a struct sockaddr_storage.
426 void in6_addr_to_sockaddr_storage(struct sockaddr_storage
*ss
,
429 struct sockaddr_in6
*sa
= (struct sockaddr_in6
*)ss
;
430 memset(ss
, '\0', sizeof(*ss
));
431 sa
->sin6_family
= AF_INET6
;
437 * Are two IPs on the same subnet?
439 bool same_net(const struct sockaddr
*ip1
,
440 const struct sockaddr
*ip2
,
441 const struct sockaddr
*mask
)
443 if (ip1
->sa_family
!= ip2
->sa_family
) {
444 /* Never on the same net. */
448 #if defined(HAVE_IPV6)
449 if (ip1
->sa_family
== AF_INET6
) {
450 struct sockaddr_in6 ip1_6
= *(const struct sockaddr_in6
*)ip1
;
451 struct sockaddr_in6 ip2_6
= *(const struct sockaddr_in6
*)ip2
;
452 struct sockaddr_in6 mask_6
= *(const struct sockaddr_in6
*)mask
;
453 char *p1
= (char *)&ip1_6
.sin6_addr
;
454 char *p2
= (char *)&ip2_6
.sin6_addr
;
455 char *m
= (char *)&mask_6
.sin6_addr
;
458 for (i
= 0; i
< sizeof(struct in6_addr
); i
++) {
463 return (memcmp(&ip1_6
.sin6_addr
,
465 sizeof(struct in6_addr
)) == 0);
468 if (ip1
->sa_family
== AF_INET
) {
469 return same_net_v4(((const struct sockaddr_in
*)ip1
)->sin_addr
,
470 ((const struct sockaddr_in
*)ip2
)->sin_addr
,
471 ((const struct sockaddr_in
*)mask
)->sin_addr
);
477 * Are two sockaddr 's the same family and address ? Ignore port etc.
480 bool sockaddr_equal(const struct sockaddr
*ip1
,
481 const struct sockaddr
*ip2
)
483 if (ip1
->sa_family
!= ip2
->sa_family
) {
484 /* Never the same. */
488 #if defined(HAVE_IPV6)
489 if (ip1
->sa_family
== AF_INET6
) {
490 return (memcmp(&((const struct sockaddr_in6
*)ip1
)->sin6_addr
,
491 &((const struct sockaddr_in6
*)ip2
)->sin6_addr
,
492 sizeof(struct in6_addr
)) == 0);
495 if (ip1
->sa_family
== AF_INET
) {
496 return (memcmp(&((const struct sockaddr_in
*)ip1
)->sin_addr
,
497 &((const struct sockaddr_in
*)ip2
)->sin_addr
,
498 sizeof(struct in_addr
)) == 0);
504 * Is an IP address the INADDR_ANY or in6addr_any value ?
506 bool is_address_any(const struct sockaddr
*psa
)
508 #if defined(HAVE_IPV6)
509 if (psa
->sa_family
== AF_INET6
) {
510 const struct sockaddr_in6
*si6
= (const struct sockaddr_in6
*)psa
;
511 if (memcmp(&in6addr_any
,
513 sizeof(in6addr_any
)) == 0) {
519 if (psa
->sa_family
== AF_INET
) {
520 const struct sockaddr_in
*si
= (const struct sockaddr_in
*)psa
;
521 if (si
->sin_addr
.s_addr
== INADDR_ANY
) {
529 void set_sockaddr_port(struct sockaddr
*psa
, uint16_t port
)
531 #if defined(HAVE_IPV6)
532 if (psa
->sa_family
== AF_INET6
) {
533 ((struct sockaddr_in6
*)psa
)->sin6_port
= htons(port
);
536 if (psa
->sa_family
== AF_INET
) {
537 ((struct sockaddr_in
*)psa
)->sin_port
= htons(port
);