Revert commit "0551284dc08eb93ef7b2b2227a45e5ec21d482fb" - simplify
[Samba/fernandojvsilva.git] / lib / util / util_net.c
blob0ce495e57cbabe1af82053c0dce28fdd4349587b
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 #undef strcasecmp
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;
43 /**
44 * Wrap getaddrinfo...
46 bool interpret_string_addr_internal(struct addrinfo **ppres,
47 const char *str, int flags)
49 int ret;
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,
61 &hints,
62 ppres);
64 if (ret) {
65 DEBUG(3,("interpret_string_addr_internal: getaddrinfo failed "
66 "for name %s [%s]\n",
67 str,
68 gai_strerror(ret) ));
69 return false;
71 return true;
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,
81 const char *str,
82 int flags,
83 bool prefer_ipv4)
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)) {
99 strlcpy(addr, str,
100 MIN(PTR_DIFF(p,str)+1,
101 sizeof(addr)));
102 str = addr;
105 #endif
107 zero_sockaddr(pss);
109 if (!interpret_string_addr_internal(&res, str, flags|AI_ADDRCONFIG)) {
110 return false;
112 if (!res) {
113 return false;
116 if (prefer_ipv4) {
117 struct addrinfo *p;
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);
122 break;
125 if (p == NULL) {
126 /* Copy the first sockaddr. */
127 memcpy(pss, res->ai_addr, res->ai_addrlen);
129 } else {
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;
142 #endif
144 freeaddrinfo(res);
145 return true;
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,
154 const char *str,
155 int flags)
157 return interpret_string_addr_pref(pss,
158 str,
159 flags,
160 false);
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,
169 const char *str,
170 int flags)
172 return interpret_string_addr_pref(pss,
173 str,
174 flags,
175 true);
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)
185 uint32_t ret;
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)) {
190 struct in_addr dest;
192 if (inet_pton(AF_INET, str, &dest) <= 0) {
193 /* Error - this shouldn't happen ! */
194 DEBUG(0,("interpret_addr: inet_pton failed "
195 "host %s\n",
196 str));
197 return 0;
199 ret = dest.s_addr; /* NETWORK BYTE ORDER ! */
200 } else {
201 /* Otherwise assume it's a network name of some sort and use
202 getadddrinfo. */
203 struct addrinfo *res = NULL;
204 struct addrinfo *res_list = NULL;
205 if (!interpret_string_addr_internal(&res_list,
206 str,
207 AI_ADDRCONFIG)) {
208 DEBUG(3,("interpret_addr: Unknown host. %s\n",str));
209 return 0;
212 /* Find the first IPv4 address. */
213 for (res = res_list; res; res = res->ai_next) {
214 if (res->ai_family != AF_INET) {
215 continue;
217 if (res->ai_addr == NULL) {
218 continue;
220 break;
222 if(res == NULL) {
223 DEBUG(3,("interpret_addr: host address is "
224 "invalid for host %s\n",str));
225 if (res_list) {
226 freeaddrinfo(res_list);
228 return 0;
230 memcpy((char *)&ret,
231 &((struct sockaddr_in *)res->ai_addr)->sin_addr.s_addr,
232 sizeof(ret));
233 if (res_list) {
234 freeaddrinfo(res_list);
238 /* This is so bogus - all callers need fixing... JRA. */
239 if (ret == (uint32_t)-1) {
240 return 0;
243 return ret;
247 A convenient addition to interpret_addr().
249 _PUBLIC_ struct in_addr interpret_addr2(const char *str)
251 struct in_addr ret;
252 uint32_t a = interpret_addr(str);
253 ret.s_addr = a;
254 return ret;
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)
287 int ret = -1;
288 struct in_addr dest;
290 ret = inet_pton(AF_INET, str, &dest);
291 if (ret > 0) {
292 return true;
294 return false;
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)
304 int ret = -1;
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)) {
318 strlcpy(addr, str,
319 MIN(PTR_DIFF(p,str)+1,
320 sizeof(addr)));
321 sp = addr;
323 ret = inet_pton(AF_INET6, sp, &dest6);
324 if (ret > 0) {
325 return true;
328 #endif
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);
344 #endif
345 if (pss->sa_family == AF_INET) {
346 uint32_t addr =
347 ntohl(((const struct sockaddr_in *)pss)->sin_addr.s_addr);
348 return addr == INADDR_BROADCAST;
350 return false;
354 * Check if an IPv7 is 127.0.0.1
356 bool is_loopback_ip_v4(struct in_addr ip)
358 struct in_addr a;
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);
374 #endif
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);
379 return false;
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);
393 #endif
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);
398 return false;
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,
413 struct in_addr ip)
415 struct sockaddr_in *sa = (struct sockaddr_in *)ss;
416 memset(ss, '\0', sizeof(*ss));
417 sa->sin_family = AF_INET;
418 sa->sin_addr = ip;
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,
426 struct in6_addr ip)
428 struct sockaddr_in6 *sa = (struct sockaddr_in6 *)ss;
429 memset(ss, '\0', sizeof(*ss));
430 sa->sin6_family = AF_INET6;
431 sa->sin6_addr = ip;
433 #endif
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. */
444 return false;
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;
455 int i;
457 for (i = 0; i < sizeof(struct in6_addr); i++) {
458 *p1++ &= *m;
459 *p2++ &= *m;
460 m++;
462 return (memcmp(&ip1_6.sin6_addr,
463 &ip2_6.sin6_addr,
464 sizeof(struct in6_addr)) == 0);
466 #endif
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);
472 return false;
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. */
484 return false;
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);
493 #endif
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);
499 return false;
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,
511 &si6->sin6_addr,
512 sizeof(in6addr_any)) == 0) {
513 return true;
515 return false;
517 #endif
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) {
521 return true;
523 return false;
525 return false;
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);
534 #endif
535 if (psa->sa_family == AF_INET) {
536 ((struct sockaddr_in *)psa)->sin_port = htons(port);