WHATSNEW: Start release notes for Samba 3.6.7.
[Samba.git] / lib / util / util_net.c
blobe80447128f41bf42e6af2f6e117afbaaaecc89aa
1 /*
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/>.
26 #include "includes.h"
27 #include "system/network.h"
28 #include "system/locale.h"
29 #include "system/filesys.h"
30 #include "lib/util/util_net.h"
31 #undef strcasecmp
33 /*******************************************************************
34 Set an address to INADDR_ANY.
35 ******************************************************************/
37 void zero_sockaddr(struct sockaddr_storage *pss)
39 ZERO_STRUCTP(pss);
40 /* Ensure we're at least a valid sockaddr-storage. */
41 pss->ss_family = AF_INET;
44 /**
45 * Wrap getaddrinfo...
47 bool interpret_string_addr_internal(struct addrinfo **ppres,
48 const char *str, int flags)
50 int ret;
51 struct addrinfo hints;
53 ZERO_STRUCT(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,
63 &hints,
64 ppres);
66 if (ret) {
67 DEBUG(3,("interpret_string_addr_internal: getaddrinfo failed "
68 "for name %s [%s]\n",
69 str,
70 gai_strerror(ret) ));
71 return false;
73 return true;
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,
83 const char *str,
84 int flags,
85 bool prefer_ipv4)
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)) {
101 strlcpy(addr, str,
102 MIN(PTR_DIFF(p,str)+1,
103 sizeof(addr)));
104 str = addr;
107 #endif
109 zero_sockaddr(pss);
111 if (!interpret_string_addr_internal(&res, str, flags|AI_ADDRCONFIG)) {
112 return false;
114 if (!res) {
115 return false;
118 if (prefer_ipv4) {
119 struct addrinfo *p;
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);
124 break;
127 if (p == NULL) {
128 /* Copy the first sockaddr. */
129 memcpy(pss, res->ai_addr, res->ai_addrlen);
131 } else {
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;
144 #endif
146 freeaddrinfo(res);
147 return true;
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,
156 const char *str,
157 int flags)
159 return interpret_string_addr_pref(pss,
160 str,
161 flags,
162 false);
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,
171 const char *str,
172 int flags)
174 return interpret_string_addr_pref(pss,
175 str,
176 flags,
177 true);
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)
187 uint32_t ret;
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)) {
192 struct in_addr dest;
194 if (inet_pton(AF_INET, str, &dest) <= 0) {
195 /* Error - this shouldn't happen ! */
196 DEBUG(0,("interpret_addr: inet_pton failed "
197 "host %s\n",
198 str));
199 return 0;
201 ret = dest.s_addr; /* NETWORK BYTE ORDER ! */
202 } else {
203 /* Otherwise assume it's a network name of some sort and use
204 getadddrinfo. */
205 struct addrinfo *res = NULL;
206 struct addrinfo *res_list = NULL;
207 if (!interpret_string_addr_internal(&res_list,
208 str,
209 AI_ADDRCONFIG)) {
210 DEBUG(3,("interpret_addr: Unknown host. %s\n",str));
211 return 0;
214 /* Find the first IPv4 address. */
215 for (res = res_list; res; res = res->ai_next) {
216 if (res->ai_family != AF_INET) {
217 continue;
219 if (res->ai_addr == NULL) {
220 continue;
222 break;
224 if(res == NULL) {
225 DEBUG(3,("interpret_addr: host address is "
226 "invalid for host %s\n",str));
227 if (res_list) {
228 freeaddrinfo(res_list);
230 return 0;
232 memcpy((char *)&ret,
233 &((struct sockaddr_in *)res->ai_addr)->sin_addr.s_addr,
234 sizeof(ret));
235 if (res_list) {
236 freeaddrinfo(res_list);
240 /* This is so bogus - all callers need fixing... JRA. */
241 if (ret == (uint32_t)-1) {
242 return 0;
245 return ret;
249 A convenient addition to interpret_addr().
251 _PUBLIC_ struct in_addr interpret_addr2(const char *str)
253 struct in_addr ret;
254 uint32_t a = interpret_addr(str);
255 ret.s_addr = a;
256 return ret;
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)
289 int ret = -1;
290 struct in_addr dest;
292 ret = inet_pton(AF_INET, str, &dest);
293 if (ret > 0) {
294 return true;
296 return false;
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)
306 int ret = -1;
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)) {
320 strlcpy(addr, str,
321 MIN(PTR_DIFF(p,str)+1,
322 sizeof(addr)));
323 sp = addr;
325 ret = inet_pton(AF_INET6, sp, &dest6);
326 if (ret > 0) {
327 return true;
330 #endif
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);
346 #endif
347 if (pss->sa_family == AF_INET) {
348 uint32_t addr =
349 ntohl(((const struct sockaddr_in *)pss)->sin_addr.s_addr);
350 return addr == INADDR_BROADCAST;
352 return false;
356 * Check if an IPv7 is 127.0.0.1
358 bool is_loopback_ip_v4(struct in_addr ip)
360 struct in_addr a;
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);
376 #endif
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);
381 return false;
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);
395 #endif
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);
400 return false;
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,
415 struct in_addr ip)
417 struct sockaddr_in *sa = (struct sockaddr_in *)ss;
418 memset(ss, '\0', sizeof(*ss));
419 sa->sin_family = AF_INET;
420 sa->sin_addr = ip;
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,
428 struct in6_addr ip)
430 struct sockaddr_in6 *sa = (struct sockaddr_in6 *)ss;
431 memset(ss, '\0', sizeof(*ss));
432 sa->sin6_family = AF_INET6;
433 sa->sin6_addr = ip;
435 #endif
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. */
446 return false;
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;
457 int i;
459 for (i = 0; i < sizeof(struct in6_addr); i++) {
460 *p1++ &= *m;
461 *p2++ &= *m;
462 m++;
464 return (memcmp(&ip1_6.sin6_addr,
465 &ip2_6.sin6_addr,
466 sizeof(struct in6_addr)) == 0);
468 #endif
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);
474 return false;
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. */
486 return false;
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);
495 #endif
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);
501 return false;
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,
513 &si6->sin6_addr,
514 sizeof(in6addr_any)) == 0) {
515 return true;
517 return false;
519 #endif
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) {
523 return true;
525 return false;
527 return false;
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);
536 #endif
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)
549 uint16_t port = 0;
551 if (pss->ss_family != AF_INET) {
552 #if defined(HAVE_IPV6)
553 /* IPv6 */
554 const struct sockaddr_in6 *sa6 =
555 (const struct sockaddr_in6 *)pss;
556 port = ntohs(sa6->sin6_port);
557 #endif
558 } else {
559 const struct sockaddr_in *sa =
560 (const struct sockaddr_in *)pss;
561 port = ntohs(sa->sin_port);
563 return port;
566 /****************************************************************************
567 Print out an IPv4 or IPv6 address from a struct sockaddr_storage.
568 ****************************************************************************/
570 char *print_sockaddr_len(char *dest,
571 size_t destlen,
572 const struct sockaddr *psa,
573 socklen_t psalen)
575 if (destlen > 0) {
576 dest[0] = '\0';
578 (void)sys_getnameinfo(psa,
579 psalen,
580 dest, destlen,
581 NULL, 0,
582 NI_NUMERICHOST);
583 return dest;
586 /****************************************************************************
587 Print out an IPv4 or IPv6 address from a struct sockaddr_storage.
588 ****************************************************************************/
590 char *print_sockaddr(char *dest,
591 size_t destlen,
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];
606 char *dest = NULL;
607 int ret;
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),
614 addr, sizeof(addr),
615 NULL, 0,
616 NI_NUMERICHOST);
617 if (ret != 0) {
618 return NULL;
621 if (pss->ss_family != AF_INET) {
622 #if defined(HAVE_IPV6)
623 dest = talloc_asprintf(ctx, "[%s]", addr);
624 #else
625 return NULL;
626 #endif
627 } else {
628 dest = talloc_asprintf(ctx, "%s", addr);
631 return dest;
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);
643 if (fd == -1) {
644 return -1;
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",
650 strerror(errno)));
651 return -1;
654 #if defined(HAVE_IPV6)
655 if (sa.ss_family == AF_INET6) {
656 return ntohs(((struct sockaddr_in6 *)&sa)->sin6_port);
658 #endif
659 if (sa.ss_family == AF_INET) {
660 return ntohs(((struct sockaddr_in *)&sa)->sin_port);
662 return -1;
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);
681 if (fd == -1) {
682 return addr_buf;
685 if (getsockname(fd, (struct sockaddr *)&sa, &length) < 0) {
686 DEBUG(0,("getsockname failed. Error was %s\n",
687 strerror(errno) ));
688 return addr_buf;
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);