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"
30 #include "lib/util/util_net.h"
33 /*******************************************************************
34 Set an address to INADDR_ANY.
35 ******************************************************************/
37 void zero_sockaddr(struct sockaddr_storage
*pss
)
40 /* Ensure we're at least a valid sockaddr-storage. */
41 pss
->ss_family
= AF_INET
;
47 bool interpret_string_addr_internal(struct addrinfo
**ppres
,
48 const char *str
, int flags
)
51 struct addrinfo hints
;
55 /* By default make sure it supports TCP. */
56 hints
.ai_socktype
= SOCK_STREAM
;
57 hints
.ai_flags
= flags
;
59 /* Linux man page on getaddrinfo() says port will be
60 uninitialized when service string is NULL */
62 ret
= getaddrinfo(str
, NULL
,
67 DEBUG(3,("interpret_string_addr_internal: getaddrinfo failed "
76 /*******************************************************************
77 Map a text hostname or IP address (IPv4 or IPv6) into a
78 struct sockaddr_storage. Takes a flag which allows it to
79 prefer an IPv4 address (needed for DC's).
80 ******************************************************************/
82 static bool interpret_string_addr_pref(struct sockaddr_storage
*pss
,
87 struct addrinfo
*res
= NULL
;
88 #if defined(HAVE_IPV6)
89 char addr
[INET6_ADDRSTRLEN
];
90 unsigned int scope_id
= 0;
92 if (strchr_m(str
, ':')) {
93 char *p
= strchr_m(str
, '%');
96 * Cope with link-local.
97 * This is IP:v6:addr%ifname.
100 if (p
&& (p
> str
) && ((scope_id
= if_nametoindex(p
+1)) != 0)) {
102 MIN(PTR_DIFF(p
,str
)+1,
111 if (!interpret_string_addr_internal(&res
, str
, flags
|AI_ADDRCONFIG
)) {
121 for (p
= res
; p
; p
= p
->ai_next
) {
122 if (p
->ai_family
== AF_INET
) {
123 memcpy(pss
, p
->ai_addr
, p
->ai_addrlen
);
128 /* Copy the first sockaddr. */
129 memcpy(pss
, res
->ai_addr
, res
->ai_addrlen
);
132 /* Copy the first sockaddr. */
133 memcpy(pss
, res
->ai_addr
, res
->ai_addrlen
);
136 #if defined(HAVE_IPV6)
137 if (pss
->ss_family
== AF_INET6
&& scope_id
) {
138 struct sockaddr_in6
*ps6
= (struct sockaddr_in6
*)pss
;
139 if (IN6_IS_ADDR_LINKLOCAL(&ps6
->sin6_addr
) &&
140 ps6
->sin6_scope_id
== 0) {
141 ps6
->sin6_scope_id
= scope_id
;
150 /*******************************************************************
151 Map a text hostname or IP address (IPv4 or IPv6) into a
152 struct sockaddr_storage. Address agnostic version.
153 ******************************************************************/
155 bool interpret_string_addr(struct sockaddr_storage
*pss
,
159 return interpret_string_addr_pref(pss
,
165 /*******************************************************************
166 Map a text hostname or IP address (IPv4 or IPv6) into a
167 struct sockaddr_storage. Version that prefers IPv4.
168 ******************************************************************/
170 bool interpret_string_addr_prefer_ipv4(struct sockaddr_storage
*pss
,
174 return interpret_string_addr_pref(pss
,
181 * Interpret an internet address or name into an IP address in 4 byte form.
182 * RETURNS IN NETWORK BYTE ORDER (big endian).
185 uint32_t interpret_addr(const char *str
)
189 /* If it's in the form of an IP address then
190 * get the lib to interpret it */
191 if (is_ipaddress_v4(str
)) {
194 if (inet_pton(AF_INET
, str
, &dest
) <= 0) {
195 /* Error - this shouldn't happen ! */
196 DEBUG(0,("interpret_addr: inet_pton failed "
201 ret
= dest
.s_addr
; /* NETWORK BYTE ORDER ! */
203 /* Otherwise assume it's a network name of some sort and use
205 struct addrinfo
*res
= NULL
;
206 struct addrinfo
*res_list
= NULL
;
207 if (!interpret_string_addr_internal(&res_list
,
210 DEBUG(3,("interpret_addr: Unknown host. %s\n",str
));
214 /* Find the first IPv4 address. */
215 for (res
= res_list
; res
; res
= res
->ai_next
) {
216 if (res
->ai_family
!= AF_INET
) {
219 if (res
->ai_addr
== NULL
) {
225 DEBUG(3,("interpret_addr: host address is "
226 "invalid for host %s\n",str
));
228 freeaddrinfo(res_list
);
233 &((struct sockaddr_in
*)res
->ai_addr
)->sin_addr
.s_addr
,
236 freeaddrinfo(res_list
);
240 /* This is so bogus - all callers need fixing... JRA. */
241 if (ret
== (uint32_t)-1) {
249 A convenient addition to interpret_addr().
251 _PUBLIC_
struct in_addr
interpret_addr2(const char *str
)
254 uint32_t a
= interpret_addr(str
);
260 Check if an IP is the 0.0.0.0.
263 _PUBLIC_
bool is_zero_ip_v4(struct in_addr ip
)
265 return ip
.s_addr
== 0;
269 Are two IPs on the same subnet?
272 _PUBLIC_
bool same_net_v4(struct in_addr ip1
, struct in_addr ip2
, struct in_addr mask
)
274 uint32_t net1
,net2
,nmask
;
276 nmask
= ntohl(mask
.s_addr
);
277 net1
= ntohl(ip1
.s_addr
);
278 net2
= ntohl(ip2
.s_addr
);
280 return((net1
& nmask
) == (net2
& nmask
));
284 * Return true if a string could be an IPv4 address.
287 bool is_ipaddress_v4(const char *str
)
292 ret
= inet_pton(AF_INET
, str
, &dest
);
300 * Return true if a string could be an IPv4 or IPv6 address.
303 bool is_ipaddress(const char *str
)
305 #if defined(HAVE_IPV6)
308 if (strchr_m(str
, ':')) {
309 char addr
[INET6_ADDRSTRLEN
];
310 struct in6_addr dest6
;
311 const char *sp
= str
;
312 char *p
= strchr_m(str
, '%');
315 * Cope with link-local.
316 * This is IP:v6:addr%ifname.
319 if (p
&& (p
> str
) && (if_nametoindex(p
+1) != 0)) {
321 MIN(PTR_DIFF(p
,str
)+1,
325 ret
= inet_pton(AF_INET6
, sp
, &dest6
);
331 return is_ipaddress_v4(str
);
335 * Is a sockaddr a broadcast address ?
338 bool is_broadcast_addr(const struct sockaddr
*pss
)
340 #if defined(HAVE_IPV6)
341 if (pss
->sa_family
== AF_INET6
) {
342 const struct in6_addr
*sin6
=
343 &((const struct sockaddr_in6
*)pss
)->sin6_addr
;
344 return IN6_IS_ADDR_MULTICAST(sin6
);
347 if (pss
->sa_family
== AF_INET
) {
349 ntohl(((const struct sockaddr_in
*)pss
)->sin_addr
.s_addr
);
350 return addr
== INADDR_BROADCAST
;
356 * Check if an IPv7 is 127.0.0.1
358 bool is_loopback_ip_v4(struct in_addr ip
)
361 a
.s_addr
= htonl(INADDR_LOOPBACK
);
362 return(ip
.s_addr
== a
.s_addr
);
366 * Check if a struct sockaddr is the loopback address.
368 bool is_loopback_addr(const struct sockaddr
*pss
)
370 #if defined(HAVE_IPV6)
371 if (pss
->sa_family
== AF_INET6
) {
372 const struct in6_addr
*pin6
=
373 &((const struct sockaddr_in6
*)pss
)->sin6_addr
;
374 return IN6_IS_ADDR_LOOPBACK(pin6
);
377 if (pss
->sa_family
== AF_INET
) {
378 const struct in_addr
*pin
= &((const struct sockaddr_in
*)pss
)->sin_addr
;
379 return is_loopback_ip_v4(*pin
);
385 * Check if a struct sockaddr has an unspecified address.
387 bool is_zero_addr(const struct sockaddr_storage
*pss
)
389 #if defined(HAVE_IPV6)
390 if (pss
->ss_family
== AF_INET6
) {
391 const struct in6_addr
*pin6
=
392 &((const struct sockaddr_in6
*)pss
)->sin6_addr
;
393 return IN6_IS_ADDR_UNSPECIFIED(pin6
);
396 if (pss
->ss_family
== AF_INET
) {
397 const struct in_addr
*pin
= &((const struct sockaddr_in
*)pss
)->sin_addr
;
398 return is_zero_ip_v4(*pin
);
404 * Set an IP to 0.0.0.0.
406 void zero_ip_v4(struct in_addr
*ip
)
408 memset(ip
, '\0', sizeof(struct in_addr
));
412 * Convert an IPv4 struct in_addr to a struct sockaddr_storage.
414 void in_addr_to_sockaddr_storage(struct sockaddr_storage
*ss
,
417 struct sockaddr_in
*sa
= (struct sockaddr_in
*)ss
;
418 memset(ss
, '\0', sizeof(*ss
));
419 sa
->sin_family
= AF_INET
;
423 #if defined(HAVE_IPV6)
425 * Convert an IPv6 struct in_addr to a struct sockaddr_storage.
427 void in6_addr_to_sockaddr_storage(struct sockaddr_storage
*ss
,
430 struct sockaddr_in6
*sa
= (struct sockaddr_in6
*)ss
;
431 memset(ss
, '\0', sizeof(*ss
));
432 sa
->sin6_family
= AF_INET6
;
438 * Are two IPs on the same subnet?
440 bool same_net(const struct sockaddr
*ip1
,
441 const struct sockaddr
*ip2
,
442 const struct sockaddr
*mask
)
444 if (ip1
->sa_family
!= ip2
->sa_family
) {
445 /* Never on the same net. */
449 #if defined(HAVE_IPV6)
450 if (ip1
->sa_family
== AF_INET6
) {
451 struct sockaddr_in6 ip1_6
= *(const struct sockaddr_in6
*)ip1
;
452 struct sockaddr_in6 ip2_6
= *(const struct sockaddr_in6
*)ip2
;
453 struct sockaddr_in6 mask_6
= *(const struct sockaddr_in6
*)mask
;
454 char *p1
= (char *)&ip1_6
.sin6_addr
;
455 char *p2
= (char *)&ip2_6
.sin6_addr
;
456 char *m
= (char *)&mask_6
.sin6_addr
;
459 for (i
= 0; i
< sizeof(struct in6_addr
); i
++) {
464 return (memcmp(&ip1_6
.sin6_addr
,
466 sizeof(struct in6_addr
)) == 0);
469 if (ip1
->sa_family
== AF_INET
) {
470 return same_net_v4(((const struct sockaddr_in
*)ip1
)->sin_addr
,
471 ((const struct sockaddr_in
*)ip2
)->sin_addr
,
472 ((const struct sockaddr_in
*)mask
)->sin_addr
);
478 * Are two sockaddr 's the same family and address ? Ignore port etc.
481 bool sockaddr_equal(const struct sockaddr
*ip1
,
482 const struct sockaddr
*ip2
)
484 if (ip1
->sa_family
!= ip2
->sa_family
) {
485 /* Never the same. */
489 #if defined(HAVE_IPV6)
490 if (ip1
->sa_family
== AF_INET6
) {
491 return (memcmp(&((const struct sockaddr_in6
*)ip1
)->sin6_addr
,
492 &((const struct sockaddr_in6
*)ip2
)->sin6_addr
,
493 sizeof(struct in6_addr
)) == 0);
496 if (ip1
->sa_family
== AF_INET
) {
497 return (memcmp(&((const struct sockaddr_in
*)ip1
)->sin_addr
,
498 &((const struct sockaddr_in
*)ip2
)->sin_addr
,
499 sizeof(struct in_addr
)) == 0);
505 * Is an IP address the INADDR_ANY or in6addr_any value ?
507 bool is_address_any(const struct sockaddr
*psa
)
509 #if defined(HAVE_IPV6)
510 if (psa
->sa_family
== AF_INET6
) {
511 const struct sockaddr_in6
*si6
= (const struct sockaddr_in6
*)psa
;
512 if (memcmp(&in6addr_any
,
514 sizeof(in6addr_any
)) == 0) {
520 if (psa
->sa_family
== AF_INET
) {
521 const struct sockaddr_in
*si
= (const struct sockaddr_in
*)psa
;
522 if (si
->sin_addr
.s_addr
== INADDR_ANY
) {
530 void set_sockaddr_port(struct sockaddr
*psa
, uint16_t port
)
532 #if defined(HAVE_IPV6)
533 if (psa
->sa_family
== AF_INET6
) {
534 ((struct sockaddr_in6
*)psa
)->sin6_port
= htons(port
);
537 if (psa
->sa_family
== AF_INET
) {
538 ((struct sockaddr_in
*)psa
)->sin_port
= htons(port
);
543 /****************************************************************************
544 Get a port number in host byte order from a sockaddr_storage.
545 ****************************************************************************/
547 uint16_t get_sockaddr_port(const struct sockaddr_storage
*pss
)
551 if (pss
->ss_family
!= AF_INET
) {
552 #if defined(HAVE_IPV6)
554 const struct sockaddr_in6
*sa6
=
555 (const struct sockaddr_in6
*)pss
;
556 port
= ntohs(sa6
->sin6_port
);
559 const struct sockaddr_in
*sa
=
560 (const struct sockaddr_in
*)pss
;
561 port
= ntohs(sa
->sin_port
);
566 /****************************************************************************
567 Print out an IPv4 or IPv6 address from a struct sockaddr_storage.
568 ****************************************************************************/
570 char *print_sockaddr_len(char *dest
,
572 const struct sockaddr
*psa
,
578 (void)sys_getnameinfo(psa
,
586 /****************************************************************************
587 Print out an IPv4 or IPv6 address from a struct sockaddr_storage.
588 ****************************************************************************/
590 char *print_sockaddr(char *dest
,
592 const struct sockaddr_storage
*psa
)
594 return print_sockaddr_len(dest
, destlen
, (struct sockaddr
*)psa
,
595 sizeof(struct sockaddr_storage
));
598 /****************************************************************************
599 Print out a canonical IPv4 or IPv6 address from a struct sockaddr_storage.
600 ****************************************************************************/
602 char *print_canonical_sockaddr(TALLOC_CTX
*ctx
,
603 const struct sockaddr_storage
*pss
)
605 char addr
[INET6_ADDRSTRLEN
];
609 /* Linux getnameinfo() man pages says port is unitialized if
610 service name is NULL. */
612 ret
= sys_getnameinfo((const struct sockaddr
*)pss
,
613 sizeof(struct sockaddr_storage
),
621 if (pss
->ss_family
!= AF_INET
) {
622 #if defined(HAVE_IPV6)
623 dest
= talloc_asprintf(ctx
, "[%s]", addr
);
628 dest
= talloc_asprintf(ctx
, "%s", addr
);
634 /****************************************************************************
635 Return the port number we've bound to on a socket.
636 ****************************************************************************/
638 int get_socket_port(int fd
)
640 struct sockaddr_storage sa
;
641 socklen_t length
= sizeof(sa
);
647 if (getsockname(fd
, (struct sockaddr
*)&sa
, &length
) < 0) {
648 int level
= (errno
== ENOTCONN
) ? 2 : 0;
649 DEBUG(level
, ("getsockname failed. Error was %s\n",
654 #if defined(HAVE_IPV6)
655 if (sa
.ss_family
== AF_INET6
) {
656 return ntohs(((struct sockaddr_in6
*)&sa
)->sin6_port
);
659 if (sa
.ss_family
== AF_INET
) {
660 return ntohs(((struct sockaddr_in
*)&sa
)->sin_port
);
665 /****************************************************************************
666 Return the string of an IP address (IPv4 or IPv6).
667 ****************************************************************************/
669 static const char *get_socket_addr(int fd
, char *addr_buf
, size_t addr_len
)
671 struct sockaddr_storage sa
;
672 socklen_t length
= sizeof(sa
);
674 /* Ok, returning a hard coded IPv4 address
675 * is bogus, but it's just as bogus as a
676 * zero IPv6 address. No good choice here.
679 strlcpy(addr_buf
, "0.0.0.0", addr_len
);
685 if (getsockname(fd
, (struct sockaddr
*)&sa
, &length
) < 0) {
686 DEBUG(0,("getsockname failed. Error was %s\n",
691 return print_sockaddr_len(addr_buf
, addr_len
, (struct sockaddr
*)&sa
, length
);
694 const char *client_socket_addr(int fd
, char *addr
, size_t addr_len
)
696 return get_socket_addr(fd
, addr
, addr_len
);