Describe implication of upstream ICU-22610
[Samba.git] / lib / util / util_net.c
blob48c9552558b9d6df847244d11d8e3bf53212cced
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 "system/select.h"
31 #include "lib/util/select.h"
32 #include "lib/util/util_net.h"
34 #undef strcasecmp
35 #undef strncasecmp
37 /*******************************************************************
38 Set an address to INADDR_ANY.
39 ******************************************************************/
41 void zero_sockaddr(struct sockaddr_storage *pss)
43 /* Ensure we're at least a valid sockaddr-storage. */
44 *pss = (struct sockaddr_storage) { .ss_family = AF_INET };
47 static char *normalize_ipv6_literal(const char *str, char *buf, size_t *_len)
49 #define IPv6_LITERAL_NET ".ipv6-literal.net"
50 const size_t llen = sizeof(IPv6_LITERAL_NET) - 1;
51 size_t len = *_len;
52 int cmp;
53 size_t i;
54 size_t idx_chars = 0;
55 size_t cnt_delimiter = 0;
56 size_t cnt_chars = 0;
58 if (len <= llen) {
59 return NULL;
62 /* ignore a trailing '.' */
63 if (str[len - 1] == '.') {
64 len -= 1;
67 len -= llen;
68 if (len >= INET6_ADDRSTRLEN) {
69 return NULL;
71 if (len < 2) {
72 return NULL;
75 cmp = strncasecmp(&str[len], IPv6_LITERAL_NET, llen);
76 if (cmp != 0) {
77 return NULL;
80 for (i = 0; i < len; i++) {
81 if (idx_chars != 0) {
82 break;
85 switch (str[i]) {
86 case '-':
87 buf[i] = ':';
88 cnt_chars = 0;
89 cnt_delimiter += 1;
90 break;
91 case 's':
92 buf[i] = SCOPE_DELIMITER;
93 idx_chars += 1;
94 break;
95 case '0':
96 case '1':
97 case '2':
98 case '3':
99 case '4':
100 case '5':
101 case '6':
102 case '7':
103 case '8':
104 case '9':
105 case 'a':
106 case 'A':
107 case 'b':
108 case 'B':
109 case 'c':
110 case 'C':
111 case 'd':
112 case 'D':
113 case 'e':
114 case 'E':
115 case 'f':
116 case 'F':
117 buf[i] = str[i];
118 cnt_chars += 1;
119 break;
120 default:
121 return NULL;
123 if (cnt_chars > 4) {
124 return NULL;
126 if (cnt_delimiter > 7) {
127 return NULL;
131 if (cnt_delimiter < 2) {
132 return NULL;
135 for (; idx_chars != 0 && i < len; i++) {
136 switch (str[i]) {
137 case SCOPE_DELIMITER:
138 case ':':
139 return NULL;
140 default:
141 buf[i] = str[i];
142 idx_chars += 1;
143 break;
147 if (idx_chars == 1) {
148 return NULL;
151 buf[i] = '\0';
152 *_len = len;
153 return buf;
157 * Wrap getaddrinfo...
159 bool interpret_string_addr_internal(struct addrinfo **ppres,
160 const char *str, int flags)
162 int ret;
163 struct addrinfo hints;
164 #if defined(HAVE_IPV6)
165 char addr[INET6_ADDRSTRLEN*2] = { 0, };
166 unsigned int scope_id = 0;
167 size_t len = strlen(str);
168 #endif
170 ZERO_STRUCT(hints);
172 /* By default make sure it supports TCP. */
173 hints.ai_socktype = SOCK_STREAM;
175 /* always try as a numeric host first. This prevents unnecessary name
176 * lookups, and also ensures we accept IPv6 addresses */
177 hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST;
179 #if defined(HAVE_IPV6)
180 if (len < sizeof(addr)) {
181 char *p = NULL;
183 p = normalize_ipv6_literal(str, addr, &len);
184 if (p != NULL) {
185 hints.ai_family = AF_INET6;
186 str = p;
190 if (strchr_m(str, ':')) {
191 char *p = strchr_m(str, SCOPE_DELIMITER);
194 * Cope with link-local.
195 * This is IP:v6:addr%ifname.
198 if (p && (p > str) && ((scope_id = if_nametoindex(p+1)) != 0)) {
199 /* Length of string we want to copy.
200 This is IP:v6:addr (removing the %ifname).
202 len = PTR_DIFF(p,str);
204 if (len+1 > sizeof(addr)) {
205 /* string+nul too long for array. */
206 return false;
208 if (str != addr) {
209 memcpy(addr, str, len);
211 addr[len] = '\0';
213 str = addr;
216 #endif
218 ret = getaddrinfo(str, NULL, &hints, ppres);
219 if (ret == 0) {
220 #if defined(HAVE_IPV6)
221 struct sockaddr_in6 *ps6 = NULL;
223 if (scope_id == 0) {
224 return true;
226 if (ppres == NULL) {
227 return true;
229 if ((*ppres) == NULL) {
230 return true;
232 if ((*ppres)->ai_addr->sa_family != AF_INET6) {
233 return true;
236 ps6 = (struct sockaddr_in6 *)(*ppres)->ai_addr;
238 if (IN6_IS_ADDR_LINKLOCAL(&ps6->sin6_addr) &&
239 ps6->sin6_scope_id == 0) {
240 ps6->sin6_scope_id = scope_id;
242 #endif
244 return true;
247 hints.ai_flags = flags;
249 /* Linux man page on getaddrinfo() says port will be
250 uninitialized when service string is NULL */
252 ret = getaddrinfo(str, NULL,
253 &hints,
254 ppres);
256 if (ret) {
257 DEBUG(3, ("interpret_string_addr_internal: "
258 "getaddrinfo failed for name %s (flags %d) [%s]\n",
259 str, flags, gai_strerror(ret)));
260 return false;
262 return true;
265 /*******************************************************************
266 Map a text hostname or IP address (IPv4 or IPv6) into a
267 struct sockaddr_storage. Takes a flag which allows it to
268 prefer an IPv4 address (needed for DC's).
269 ******************************************************************/
271 static bool interpret_string_addr_pref(struct sockaddr_storage *pss,
272 const char *str,
273 int flags,
274 bool prefer_ipv4)
276 struct addrinfo *res = NULL;
277 int int_flags;
279 zero_sockaddr(pss);
281 if (flags & AI_NUMERICHOST) {
282 int_flags = flags;
283 } else {
284 int_flags = flags|AI_ADDRCONFIG;
287 if (!interpret_string_addr_internal(&res, str, int_flags)) {
288 return false;
290 if (!res) {
291 return false;
294 if (prefer_ipv4) {
295 struct addrinfo *p;
297 for (p = res; p; p = p->ai_next) {
298 if (p->ai_family == AF_INET) {
299 memcpy(pss, p->ai_addr, p->ai_addrlen);
300 break;
303 if (p == NULL) {
304 /* Copy the first sockaddr. */
305 memcpy(pss, res->ai_addr, res->ai_addrlen);
307 } else {
308 /* Copy the first sockaddr. */
309 memcpy(pss, res->ai_addr, res->ai_addrlen);
312 freeaddrinfo(res);
313 return true;
316 /*******************************************************************
317 Map a text hostname or IP address (IPv4 or IPv6) into a
318 struct sockaddr_storage. Address agnostic version.
319 ******************************************************************/
321 bool interpret_string_addr(struct sockaddr_storage *pss,
322 const char *str,
323 int flags)
325 return interpret_string_addr_pref(pss,
326 str,
327 flags,
328 false);
331 /*******************************************************************
332 Map a text hostname or IP address (IPv4 or IPv6) into a
333 struct sockaddr_storage. Version that prefers IPv4.
334 ******************************************************************/
336 bool interpret_string_addr_prefer_ipv4(struct sockaddr_storage *pss,
337 const char *str,
338 int flags)
340 return interpret_string_addr_pref(pss,
341 str,
342 flags,
343 true);
347 * Interpret an internet address or name into an IP address in 4 byte form.
348 * RETURNS IN NETWORK BYTE ORDER (big endian).
351 uint32_t interpret_addr(const char *str)
353 uint32_t ret;
355 /* If it's in the form of an IP address then
356 * get the lib to interpret it */
357 if (is_ipaddress_v4(str)) {
358 struct in_addr dest;
360 if (inet_pton(AF_INET, str, &dest) <= 0) {
361 /* Error - this shouldn't happen ! */
362 DEBUG(0,("interpret_addr: inet_pton failed "
363 "host %s\n",
364 str));
365 return 0;
367 ret = dest.s_addr; /* NETWORK BYTE ORDER ! */
368 } else {
369 /* Otherwise assume it's a network name of some sort and use
370 getaddrinfo. */
371 struct addrinfo *res = NULL;
372 struct addrinfo *res_list = NULL;
373 if (!interpret_string_addr_internal(&res_list,
374 str,
375 AI_ADDRCONFIG)) {
376 DEBUG(3,("interpret_addr: Unknown host. %s\n",str));
377 return 0;
380 /* Find the first IPv4 address. */
381 for (res = res_list; res; res = res->ai_next) {
382 if (res->ai_family != AF_INET) {
383 continue;
385 if (res->ai_addr == NULL) {
386 continue;
388 break;
390 if(res == NULL) {
391 DEBUG(3,("interpret_addr: host address is "
392 "invalid for host %s\n",str));
393 if (res_list) {
394 freeaddrinfo(res_list);
396 return 0;
398 memcpy((char *)&ret,
399 &((struct sockaddr_in *)res->ai_addr)->sin_addr.s_addr,
400 sizeof(ret));
401 if (res_list) {
402 freeaddrinfo(res_list);
406 /* This is so bogus - all callers need fixing... JRA. */
407 if (ret == (uint32_t)-1) {
408 return 0;
411 return ret;
415 A convenient addition to interpret_addr().
417 _PUBLIC_ struct in_addr interpret_addr2(const char *str)
419 struct in_addr ret;
420 uint32_t a = interpret_addr(str);
421 ret.s_addr = a;
422 return ret;
426 Check if an IP is the 0.0.0.0.
429 _PUBLIC_ bool is_zero_ip_v4(struct in_addr ip)
431 return ip.s_addr == 0;
435 Are two IPs on the same subnet?
438 _PUBLIC_ bool same_net_v4(struct in_addr ip1, struct in_addr ip2, struct in_addr mask)
440 uint32_t net1,net2,nmask;
442 nmask = ntohl(mask.s_addr);
443 net1 = ntohl(ip1.s_addr);
444 net2 = ntohl(ip2.s_addr);
446 return((net1 & nmask) == (net2 & nmask));
450 * Return true if a string could be an IPv4 address.
453 bool is_ipaddress_v4(const char *str)
455 int ret = -1;
456 struct in_addr dest;
458 ret = inet_pton(AF_INET, str, &dest);
459 if (ret > 0) {
460 return true;
462 return false;
465 bool is_ipv6_literal(const char *str)
467 #if defined(HAVE_IPV6)
468 char buf[INET6_ADDRSTRLEN*2] = { 0, };
469 size_t len = strlen(str);
470 char *p = NULL;
472 if (len >= sizeof(buf)) {
473 return false;
476 p = normalize_ipv6_literal(str, buf, &len);
477 if (p == NULL) {
478 return false;
481 return true;
482 #else
483 return false;
484 #endif
488 * Return true if a string could be a IPv6 address.
491 bool is_ipaddress_v6(const char *str)
493 #if defined(HAVE_IPV6)
494 int ret = -1;
495 char *p = NULL;
496 char buf[INET6_ADDRSTRLEN] = { 0, };
497 size_t len;
498 const char *addr = str;
499 const char *idxs = NULL;
500 unsigned int idx = 0;
501 struct in6_addr ip6;
503 p = strchr_m(str, ':');
504 if (p == NULL) {
505 return is_ipv6_literal(str);
508 p = strchr_m(str, SCOPE_DELIMITER);
509 if (p && (p > str)) {
510 len = PTR_DIFF(p, str);
511 idxs = p + 1;
512 } else {
513 len = strlen(str);
516 if (len >= sizeof(buf)) {
517 return false;
519 if (idxs != NULL) {
520 strncpy(buf, str, len);
521 addr = buf;
525 * Cope with link-local.
526 * This is IP:v6:addr%ifidx.
528 if (idxs != NULL) {
529 char c;
531 ret = sscanf(idxs, "%5u%c", &idx, &c);
532 if (ret != 1) {
533 idx = 0;
536 if (idx > 0 && idx < UINT16_MAX) {
537 /* a valid index */
538 idxs = NULL;
543 * Cope with link-local.
544 * This is IP:v6:addr%ifname.
546 if (idxs != NULL) {
547 idx = if_nametoindex(idxs);
549 if (idx > 0) {
550 /* a valid index */
551 idxs = NULL;
555 if (idxs != NULL) {
556 return false;
559 ret = inet_pton(AF_INET6, addr, &ip6);
560 if (ret <= 0) {
561 return false;
564 return true;
565 #else
566 return false;
567 #endif
571 * Return true if a string could be an IPv4 or IPv6 address.
574 bool is_ipaddress(const char *str)
576 return is_ipaddress_v4(str) || is_ipaddress_v6(str);
580 * Is a sockaddr a broadcast address ?
583 bool is_broadcast_addr(const struct sockaddr *pss)
585 #if defined(HAVE_IPV6)
586 if (pss->sa_family == AF_INET6) {
587 const struct in6_addr *sin6 =
588 &((const struct sockaddr_in6 *)pss)->sin6_addr;
589 return IN6_IS_ADDR_MULTICAST(sin6);
591 #endif
592 if (pss->sa_family == AF_INET) {
593 uint32_t addr =
594 ntohl(((const struct sockaddr_in *)pss)->sin_addr.s_addr);
595 return addr == INADDR_BROADCAST;
597 return false;
601 * Check if an IPv7 is 127.0.0.1
603 bool is_loopback_ip_v4(struct in_addr ip)
605 struct in_addr a;
606 a.s_addr = htonl(INADDR_LOOPBACK);
607 return(ip.s_addr == a.s_addr);
611 * Check if a struct sockaddr is the loopback address.
613 bool is_loopback_addr(const struct sockaddr *pss)
615 #if defined(HAVE_IPV6)
616 if (pss->sa_family == AF_INET6) {
617 const struct in6_addr *pin6 =
618 &((const struct sockaddr_in6 *)pss)->sin6_addr;
619 return IN6_IS_ADDR_LOOPBACK(pin6);
621 #endif
622 if (pss->sa_family == AF_INET) {
623 const struct in_addr *pin = &((const struct sockaddr_in *)pss)->sin_addr;
624 return is_loopback_ip_v4(*pin);
626 return false;
630 * Check if a struct sockaddr has an unspecified address.
632 bool is_zero_addr(const struct sockaddr_storage *pss)
634 #if defined(HAVE_IPV6)
635 if (pss->ss_family == AF_INET6) {
636 const struct in6_addr *pin6 =
637 &((const struct sockaddr_in6 *)pss)->sin6_addr;
638 return IN6_IS_ADDR_UNSPECIFIED(pin6);
640 #endif
641 if (pss->ss_family == AF_INET) {
642 const struct in_addr *pin = &((const struct sockaddr_in *)pss)->sin_addr;
643 return is_zero_ip_v4(*pin);
645 if (pss->ss_family == AF_UNSPEC) {
646 return true;
648 return false;
652 * Set an IP to 0.0.0.0.
654 void zero_ip_v4(struct in_addr *ip)
656 ZERO_STRUCTP(ip);
659 bool is_linklocal_addr(const struct sockaddr_storage *pss)
661 #ifdef HAVE_IPV6
662 if (pss->ss_family == AF_INET6) {
663 const struct in6_addr *pin6 =
664 &((const struct sockaddr_in6 *)pss)->sin6_addr;
665 return IN6_IS_ADDR_LINKLOCAL(pin6);
667 #endif
668 if (pss->ss_family == AF_INET) {
669 const struct in_addr *pin =
670 &((const struct sockaddr_in *)pss)->sin_addr;
671 struct in_addr ll_addr;
672 struct in_addr mask_addr;
674 /* 169.254.0.0/16, is link local, see RFC 3927 */
675 ll_addr.s_addr = 0xa9fe0000;
676 mask_addr.s_addr = 0xffff0000;
677 return same_net_v4(*pin, ll_addr, mask_addr);
679 return false;
683 * Convert an IPv4 struct in_addr to a struct sockaddr_storage.
685 void in_addr_to_sockaddr_storage(struct sockaddr_storage *ss,
686 struct in_addr ip)
688 struct sockaddr_in *sa = (struct sockaddr_in *)ss;
689 ZERO_STRUCTP(ss);
690 sa->sin_family = AF_INET;
691 sa->sin_addr = ip;
694 #if defined(HAVE_IPV6)
696 * Convert an IPv6 struct in_addr to a struct sockaddr_storage.
698 void in6_addr_to_sockaddr_storage(struct sockaddr_storage *ss,
699 struct in6_addr ip)
701 struct sockaddr_in6 *sa = (struct sockaddr_in6 *)ss;
702 memset(ss, '\0', sizeof(*ss));
703 sa->sin6_family = AF_INET6;
704 sa->sin6_addr = ip;
706 #endif
709 * Are two IPs on the same subnet?
711 bool same_net(const struct sockaddr *ip1,
712 const struct sockaddr *ip2,
713 const struct sockaddr *mask)
715 if (ip1->sa_family != ip2->sa_family) {
716 /* Never on the same net. */
717 return false;
720 #if defined(HAVE_IPV6)
721 if (ip1->sa_family == AF_INET6) {
722 struct sockaddr_in6 ip1_6 = *(const struct sockaddr_in6 *)ip1;
723 struct sockaddr_in6 ip2_6 = *(const struct sockaddr_in6 *)ip2;
724 struct sockaddr_in6 mask_6 = *(const struct sockaddr_in6 *)mask;
725 char *p1 = (char *)&ip1_6.sin6_addr;
726 char *p2 = (char *)&ip2_6.sin6_addr;
727 char *m = (char *)&mask_6.sin6_addr;
728 size_t i;
730 for (i = 0; i < sizeof(struct in6_addr); i++) {
731 *p1++ &= *m;
732 *p2++ &= *m;
733 m++;
735 return (memcmp(&ip1_6.sin6_addr,
736 &ip2_6.sin6_addr,
737 sizeof(struct in6_addr)) == 0);
739 #endif
740 if (ip1->sa_family == AF_INET) {
741 return same_net_v4(((const struct sockaddr_in *)ip1)->sin_addr,
742 ((const struct sockaddr_in *)ip2)->sin_addr,
743 ((const struct sockaddr_in *)mask)->sin_addr);
745 return false;
749 * Are two sockaddr 's the same family and address ? Ignore port etc.
752 bool sockaddr_equal(const struct sockaddr *ip1,
753 const struct sockaddr *ip2)
755 if (ip1->sa_family != ip2->sa_family) {
756 /* Never the same. */
757 return false;
760 #if defined(HAVE_IPV6)
761 if (ip1->sa_family == AF_INET6) {
762 return (memcmp(&((const struct sockaddr_in6 *)ip1)->sin6_addr,
763 &((const struct sockaddr_in6 *)ip2)->sin6_addr,
764 sizeof(struct in6_addr)) == 0);
766 #endif
767 if (ip1->sa_family == AF_INET) {
768 return (memcmp(&((const struct sockaddr_in *)ip1)->sin_addr,
769 &((const struct sockaddr_in *)ip2)->sin_addr,
770 sizeof(struct in_addr)) == 0);
772 return false;
776 * Is an IP address the INADDR_ANY or in6addr_any value ?
778 bool is_address_any(const struct sockaddr *psa)
780 #if defined(HAVE_IPV6)
781 if (psa->sa_family == AF_INET6) {
782 const struct sockaddr_in6 *si6 = (const struct sockaddr_in6 *)psa;
783 if (memcmp(&in6addr_any,
784 &si6->sin6_addr,
785 sizeof(in6addr_any)) == 0) {
786 return true;
788 return false;
790 #endif
791 if (psa->sa_family == AF_INET) {
792 const struct sockaddr_in *si = (const struct sockaddr_in *)psa;
793 if (si->sin_addr.s_addr == INADDR_ANY) {
794 return true;
796 return false;
798 return false;
801 void set_sockaddr_port(struct sockaddr *psa, uint16_t port)
803 #if defined(HAVE_IPV6)
804 if (psa->sa_family == AF_INET6) {
805 ((struct sockaddr_in6 *)psa)->sin6_port = htons(port);
807 #endif
808 if (psa->sa_family == AF_INET) {
809 ((struct sockaddr_in *)psa)->sin_port = htons(port);
814 /****************************************************************************
815 Get a port number in host byte order from a sockaddr_storage.
816 ****************************************************************************/
818 uint16_t get_sockaddr_port(const struct sockaddr_storage *pss)
820 uint16_t port = 0;
822 if (pss->ss_family != AF_INET) {
823 #if defined(HAVE_IPV6)
824 /* IPv6 */
825 const struct sockaddr_in6 *sa6 =
826 (const struct sockaddr_in6 *)pss;
827 port = ntohs(sa6->sin6_port);
828 #endif
829 } else {
830 const struct sockaddr_in *sa =
831 (const struct sockaddr_in *)pss;
832 port = ntohs(sa->sin_port);
834 return port;
837 /****************************************************************************
838 Print out an IPv4 or IPv6 address from a struct sockaddr_storage.
839 ****************************************************************************/
841 char *print_sockaddr_len(char *dest,
842 size_t destlen,
843 const struct sockaddr *psa,
844 socklen_t psalen)
846 if (destlen > 0) {
847 dest[0] = '\0';
849 (void)sys_getnameinfo(psa,
850 psalen,
851 dest, destlen,
852 NULL, 0,
853 NI_NUMERICHOST);
854 return dest;
857 /****************************************************************************
858 Print out an IPv4 or IPv6 address from a struct sockaddr_storage.
859 ****************************************************************************/
861 char *print_sockaddr(char *dest,
862 size_t destlen,
863 const struct sockaddr_storage *psa)
865 return print_sockaddr_len(dest, destlen, (const struct sockaddr *)psa,
866 sizeof(struct sockaddr_storage));
869 /****************************************************************************
870 Print out a canonical IPv4 or IPv6 address from a struct sockaddr_storage.
871 ****************************************************************************/
873 char *print_canonical_sockaddr(TALLOC_CTX *ctx,
874 const struct sockaddr_storage *pss)
876 char addr[INET6_ADDRSTRLEN];
877 char *dest = NULL;
878 int ret;
880 /* Linux getnameinfo() man pages says port is uninitialized if
881 service name is NULL. */
883 ret = sys_getnameinfo((const struct sockaddr *)pss,
884 sizeof(struct sockaddr_storage),
885 addr, sizeof(addr),
886 NULL, 0,
887 NI_NUMERICHOST);
888 if (ret != 0) {
889 return NULL;
892 if (pss->ss_family != AF_INET) {
893 #if defined(HAVE_IPV6)
894 dest = talloc_asprintf(ctx, "[%s]", addr);
895 #else
896 return NULL;
897 #endif
898 } else {
899 dest = talloc_asprintf(ctx, "%s", addr);
902 return dest;
905 enum SOCK_OPT_TYPES {OPT_BOOL,OPT_INT,OPT_ON};
907 typedef struct smb_socket_option {
908 const char *name;
909 int level;
910 int option;
911 int value;
912 int opttype;
913 } smb_socket_option;
915 static const smb_socket_option socket_options[] = {
916 {"SO_KEEPALIVE", SOL_SOCKET, SO_KEEPALIVE, 0, OPT_BOOL},
917 {"SO_REUSEADDR", SOL_SOCKET, SO_REUSEADDR, 0, OPT_BOOL},
918 {"SO_BROADCAST", SOL_SOCKET, SO_BROADCAST, 0, OPT_BOOL},
919 #ifdef TCP_NODELAY
920 {"TCP_NODELAY", IPPROTO_TCP, TCP_NODELAY, 0, OPT_BOOL},
921 #endif
922 #ifdef TCP_KEEPCNT
923 {"TCP_KEEPCNT", IPPROTO_TCP, TCP_KEEPCNT, 0, OPT_INT},
924 #endif
925 #ifdef TCP_KEEPIDLE
926 {"TCP_KEEPIDLE", IPPROTO_TCP, TCP_KEEPIDLE, 0, OPT_INT},
927 #endif
928 #ifdef TCP_KEEPINTVL
929 {"TCP_KEEPINTVL", IPPROTO_TCP, TCP_KEEPINTVL, 0, OPT_INT},
930 #endif
931 #ifdef IPTOS_LOWDELAY
932 {"IPTOS_LOWDELAY", IPPROTO_IP, IP_TOS, IPTOS_LOWDELAY, OPT_ON},
933 #endif
934 #ifdef IPTOS_THROUGHPUT
935 {"IPTOS_THROUGHPUT", IPPROTO_IP, IP_TOS, IPTOS_THROUGHPUT, OPT_ON},
936 #endif
937 #ifdef SO_REUSEPORT
938 {"SO_REUSEPORT", SOL_SOCKET, SO_REUSEPORT, 0, OPT_BOOL},
939 #endif
940 #ifdef SO_SNDBUF
941 {"SO_SNDBUF", SOL_SOCKET, SO_SNDBUF, 0, OPT_INT},
942 #endif
943 #ifdef SO_RCVBUF
944 {"SO_RCVBUF", SOL_SOCKET, SO_RCVBUF, 0, OPT_INT},
945 #endif
946 #ifdef SO_SNDLOWAT
947 {"SO_SNDLOWAT", SOL_SOCKET, SO_SNDLOWAT, 0, OPT_INT},
948 #endif
949 #ifdef SO_RCVLOWAT
950 {"SO_RCVLOWAT", SOL_SOCKET, SO_RCVLOWAT, 0, OPT_INT},
951 #endif
952 #ifdef SO_SNDTIMEO
953 {"SO_SNDTIMEO", SOL_SOCKET, SO_SNDTIMEO, 0, OPT_INT},
954 #endif
955 #ifdef SO_RCVTIMEO
956 {"SO_RCVTIMEO", SOL_SOCKET, SO_RCVTIMEO, 0, OPT_INT},
957 #endif
958 #ifdef TCP_FASTACK
959 {"TCP_FASTACK", IPPROTO_TCP, TCP_FASTACK, 0, OPT_INT},
960 #endif
961 #ifdef TCP_QUICKACK
962 {"TCP_QUICKACK", IPPROTO_TCP, TCP_QUICKACK, 0, OPT_BOOL},
963 #endif
964 #ifdef TCP_NODELAYACK
965 {"TCP_NODELAYACK", IPPROTO_TCP, TCP_NODELAYACK, 0, OPT_BOOL},
966 #endif
967 #ifdef TCP_KEEPALIVE_THRESHOLD
968 {"TCP_KEEPALIVE_THRESHOLD", IPPROTO_TCP, TCP_KEEPALIVE_THRESHOLD, 0, OPT_INT},
969 #endif
970 #ifdef TCP_KEEPALIVE_ABORT_THRESHOLD
971 {"TCP_KEEPALIVE_ABORT_THRESHOLD", IPPROTO_TCP, TCP_KEEPALIVE_ABORT_THRESHOLD, 0, OPT_INT},
972 #endif
973 #ifdef TCP_DEFER_ACCEPT
974 {"TCP_DEFER_ACCEPT", IPPROTO_TCP, TCP_DEFER_ACCEPT, 0, OPT_INT},
975 #endif
976 #ifdef TCP_USER_TIMEOUT
977 {"TCP_USER_TIMEOUT", IPPROTO_TCP, TCP_USER_TIMEOUT, 0, OPT_INT},
978 #endif
979 {NULL,0,0,0,0}};
981 /****************************************************************************
982 Print socket options.
983 ****************************************************************************/
985 static void print_socket_options(TALLOC_CTX *ctx, int s)
987 const smb_socket_option *p = &socket_options[0];
988 char *str = NULL;
990 if (DEBUGLEVEL < 5) {
991 return;
994 str = talloc_strdup(ctx, "");
995 if (str == NULL) {
996 DBG_WARNING("talloc failed\n");
997 goto done;
1000 for (; p->name != NULL; p++) {
1001 int ret, val;
1002 socklen_t vlen = sizeof(val);
1004 ret = getsockopt(s, p->level, p->option, (void *)&val, &vlen);
1005 if (ret == -1) {
1006 DBG_INFO("Could not test socket option %s: %s.\n",
1007 p->name, strerror(errno));
1008 continue;
1011 talloc_asprintf_addbuf(
1012 &str,
1013 "%s%s=%d",
1014 str[0] != '\0' ? ", " : "",
1015 p->name,
1016 val);
1019 DEBUG(5, ("socket options: %s\n", str));
1020 done:
1021 TALLOC_FREE(str);
1024 /****************************************************************************
1025 Set user socket options.
1026 ****************************************************************************/
1028 void set_socket_options(int fd, const char *options)
1030 TALLOC_CTX *ctx = talloc_new(NULL);
1031 char *tok;
1033 while (next_token_talloc(ctx, &options, &tok," \t,")) {
1034 int ret=0,i;
1035 int value = 1;
1036 char *p;
1037 bool got_value = false;
1039 if ((p = strchr_m(tok,'='))) {
1040 *p = 0;
1041 value = atoi(p+1);
1042 got_value = true;
1045 for (i=0;socket_options[i].name;i++)
1046 if (strequal(socket_options[i].name,tok))
1047 break;
1049 if (!socket_options[i].name) {
1050 DEBUG(0,("Unknown socket option %s\n",tok));
1051 continue;
1054 switch (socket_options[i].opttype) {
1055 case OPT_BOOL:
1056 case OPT_INT:
1057 ret = setsockopt(fd,socket_options[i].level,
1058 socket_options[i].option,
1059 (char *)&value,sizeof(int));
1060 break;
1062 case OPT_ON:
1063 if (got_value)
1064 DEBUG(0,("syntax error - %s "
1065 "does not take a value\n",tok));
1068 int on = socket_options[i].value;
1069 ret = setsockopt(fd,socket_options[i].level,
1070 socket_options[i].option,
1071 (char *)&on,sizeof(int));
1073 break;
1076 if (ret != 0) {
1077 /* be aware that some systems like Solaris return
1078 * EINVAL to a setsockopt() call when the client
1079 * sent a RST previously - no need to worry */
1080 DEBUG(2,("Failed to set socket option %s (Error %s)\n",
1081 tok, strerror(errno) ));
1085 print_socket_options(ctx, fd);
1086 TALLOC_FREE(ctx);
1090 * Utility function that copes only with AF_INET and AF_INET6
1091 * as that's all we're going to get out of DNS / NetBIOS / WINS
1092 * name resolution functions.
1095 bool sockaddr_storage_to_samba_sockaddr(
1096 struct samba_sockaddr *sa, const struct sockaddr_storage *ss)
1098 sa->u.ss = *ss;
1100 switch (ss->ss_family) {
1101 case AF_INET:
1102 sa->sa_socklen = sizeof(struct sockaddr_in);
1103 break;
1104 #ifdef HAVE_IPV6
1105 case AF_INET6:
1106 sa->sa_socklen = sizeof(struct sockaddr_in6);
1107 break;
1108 #endif
1109 default:
1110 return false;
1112 return true;
1115 bool samba_sockaddr_set_port(struct samba_sockaddr *sa, uint16_t port)
1117 if (sa->u.sa.sa_family == AF_INET) {
1118 sa->u.in.sin_port = htons(port);
1119 return true;
1121 #ifdef HAVE_IPV6
1122 if (sa->u.sa.sa_family == AF_INET6) {
1123 sa->u.in6.sin6_port = htons(port);
1124 return true;
1126 #endif
1127 return false;
1130 bool samba_sockaddr_get_port(const struct samba_sockaddr *sa, uint16_t *port)
1132 if (sa->u.sa.sa_family == AF_INET) {
1133 *port = ntohs(sa->u.in.sin_port);
1134 return true;
1136 #ifdef HAVE_IPV6
1137 if (sa->u.sa.sa_family == AF_INET6) {
1138 *port = ntohs(sa->u.in6.sin6_port);
1139 return true;
1141 #endif
1142 return false;
1145 int samba_socket_poll_error(int fd)
1147 struct pollfd pfd = {
1148 .fd = fd,
1149 #ifdef POLLRDHUP
1150 .events = POLLRDHUP, /* POLLERR and POLLHUP are not needed */
1151 #endif
1153 int ret;
1155 errno = 0;
1156 ret = sys_poll_intr(&pfd, 1, 0);
1157 if (ret == 0) {
1158 return 0;
1160 if (ret != 1) {
1161 return POLLNVAL;
1164 if (pfd.revents & POLLERR) {
1165 return POLLERR;
1167 if (pfd.revents & POLLHUP) {
1168 return POLLHUP;
1170 #ifdef POLLRDHUP
1171 if (pfd.revents & POLLRDHUP) {
1172 return POLLRDHUP;
1174 #endif
1176 /* should never be reached! */
1177 return POLLNVAL;
1180 int samba_socket_sock_error(int fd)
1182 int ret, error = 0;
1183 socklen_t len = sizeof(error);
1186 * if no data is available check if the socket is in error state. For
1187 * dgram sockets it's the way to return ICMP error messages of
1188 * connected sockets to the caller.
1190 ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len);
1191 if (ret == -1) {
1192 return ret;
1194 if (error != 0) {
1195 errno = error;
1196 return -1;
1198 return 0;
1201 int samba_socket_poll_or_sock_error(int fd)
1203 int ret;
1204 int poll_error = 0;
1206 poll_error = samba_socket_poll_error(fd);
1207 if (poll_error == 0) {
1208 return 0;
1211 #ifdef POLLRDHUP
1212 if (poll_error == POLLRDHUP) {
1213 errno = ECONNRESET;
1214 return -1;
1216 #endif
1218 if (poll_error == POLLHUP) {
1219 errno = EPIPE;
1220 return -1;
1224 * POLLERR and POLLNVAL fallback to
1225 * getsockopt(fd, SOL_SOCKET, SO_ERROR)
1226 * and force EPIPE as fallback.
1229 errno = 0;
1230 ret = samba_socket_sock_error(fd);
1231 if (ret == 0) {
1232 errno = EPIPE;
1235 if (errno == 0) {
1236 errno = EPIPE;
1239 return -1;