samba-gpupdate: Test that sysvol paths download in case-insensitive way
[Samba.git] / lib / util / util_net.c
blobfe474dc16be7663ed5b07432b3d2bd03fa10fb38
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"
32 #undef strcasecmp
33 #undef strncasecmp
35 /*******************************************************************
36 Set an address to INADDR_ANY.
37 ******************************************************************/
39 void zero_sockaddr(struct sockaddr_storage *pss)
41 /* Ensure we're at least a valid sockaddr-storage. */
42 *pss = (struct sockaddr_storage) { .ss_family = AF_INET };
45 static char *normalize_ipv6_literal(const char *str, char *buf, size_t *_len)
47 #define IPv6_LITERAL_NET ".ipv6-literal.net"
48 const size_t llen = sizeof(IPv6_LITERAL_NET) - 1;
49 size_t len = *_len;
50 int cmp;
51 size_t i;
52 size_t idx_chars = 0;
53 size_t cnt_delimiter = 0;
54 size_t cnt_chars = 0;
56 if (len <= llen) {
57 return NULL;
60 /* ignore a trailing '.' */
61 if (str[len - 1] == '.') {
62 len -= 1;
65 len -= llen;
66 if (len >= INET6_ADDRSTRLEN) {
67 return NULL;
69 if (len < 2) {
70 return NULL;
73 cmp = strncasecmp(&str[len], IPv6_LITERAL_NET, llen);
74 if (cmp != 0) {
75 return NULL;
78 for (i = 0; i < len; i++) {
79 if (idx_chars != 0) {
80 break;
83 switch (str[i]) {
84 case '-':
85 buf[i] = ':';
86 cnt_chars = 0;
87 cnt_delimiter += 1;
88 break;
89 case 's':
90 buf[i] = SCOPE_DELIMITER;
91 idx_chars += 1;
92 break;
93 case '0':
94 case '1':
95 case '2':
96 case '3':
97 case '4':
98 case '5':
99 case '6':
100 case '7':
101 case '8':
102 case '9':
103 case 'a':
104 case 'A':
105 case 'b':
106 case 'B':
107 case 'c':
108 case 'C':
109 case 'd':
110 case 'D':
111 case 'e':
112 case 'E':
113 case 'f':
114 case 'F':
115 buf[i] = str[i];
116 cnt_chars += 1;
117 break;
118 default:
119 return NULL;
121 if (cnt_chars > 4) {
122 return NULL;
124 if (cnt_delimiter > 7) {
125 return NULL;
129 if (cnt_delimiter < 2) {
130 return NULL;
133 for (; idx_chars != 0 && i < len; i++) {
134 switch (str[i]) {
135 case SCOPE_DELIMITER:
136 case ':':
137 return NULL;
138 default:
139 buf[i] = str[i];
140 idx_chars += 1;
141 break;
145 if (idx_chars == 1) {
146 return NULL;
149 buf[i] = '\0';
150 *_len = len;
151 return buf;
155 * Wrap getaddrinfo...
157 bool interpret_string_addr_internal(struct addrinfo **ppres,
158 const char *str, int flags)
160 int ret;
161 struct addrinfo hints;
162 #if defined(HAVE_IPV6)
163 char addr[INET6_ADDRSTRLEN*2] = { 0, };
164 unsigned int scope_id = 0;
165 size_t len = strlen(str);
166 #endif
168 ZERO_STRUCT(hints);
170 /* By default make sure it supports TCP. */
171 hints.ai_socktype = SOCK_STREAM;
173 /* always try as a numeric host first. This prevents unnecessary name
174 * lookups, and also ensures we accept IPv6 addresses */
175 hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST;
177 #if defined(HAVE_IPV6)
178 if (len < sizeof(addr)) {
179 char *p = NULL;
181 p = normalize_ipv6_literal(str, addr, &len);
182 if (p != NULL) {
183 hints.ai_family = AF_INET6;
184 str = p;
188 if (strchr_m(str, ':')) {
189 char *p = strchr_m(str, SCOPE_DELIMITER);
192 * Cope with link-local.
193 * This is IP:v6:addr%ifname.
196 if (p && (p > str) && ((scope_id = if_nametoindex(p+1)) != 0)) {
197 /* Length of string we want to copy.
198 This is IP:v6:addr (removing the %ifname).
200 len = PTR_DIFF(p,str);
202 if (len+1 > sizeof(addr)) {
203 /* string+nul too long for array. */
204 return false;
206 if (str != addr) {
207 memcpy(addr, str, len);
209 addr[len] = '\0';
211 str = addr;
214 #endif
216 ret = getaddrinfo(str, NULL, &hints, ppres);
217 if (ret == 0) {
218 #if defined(HAVE_IPV6)
219 struct sockaddr_in6 *ps6 = NULL;
221 if (scope_id == 0) {
222 return true;
224 if (ppres == NULL) {
225 return true;
227 if ((*ppres) == NULL) {
228 return true;
230 if ((*ppres)->ai_addr->sa_family != AF_INET6) {
231 return true;
234 ps6 = (struct sockaddr_in6 *)(*ppres)->ai_addr;
236 if (IN6_IS_ADDR_LINKLOCAL(&ps6->sin6_addr) &&
237 ps6->sin6_scope_id == 0) {
238 ps6->sin6_scope_id = scope_id;
240 #endif
242 return true;
245 hints.ai_flags = flags;
247 /* Linux man page on getaddrinfo() says port will be
248 uninitialized when service string is NULL */
250 ret = getaddrinfo(str, NULL,
251 &hints,
252 ppres);
254 if (ret) {
255 DEBUG(3, ("interpret_string_addr_internal: "
256 "getaddrinfo failed for name %s (flags %d) [%s]\n",
257 str, flags, gai_strerror(ret)));
258 return false;
260 return true;
263 /*******************************************************************
264 Map a text hostname or IP address (IPv4 or IPv6) into a
265 struct sockaddr_storage. Takes a flag which allows it to
266 prefer an IPv4 address (needed for DC's).
267 ******************************************************************/
269 static bool interpret_string_addr_pref(struct sockaddr_storage *pss,
270 const char *str,
271 int flags,
272 bool prefer_ipv4)
274 struct addrinfo *res = NULL;
275 int int_flags;
277 zero_sockaddr(pss);
279 if (flags & AI_NUMERICHOST) {
280 int_flags = flags;
281 } else {
282 int_flags = flags|AI_ADDRCONFIG;
285 if (!interpret_string_addr_internal(&res, str, int_flags)) {
286 return false;
288 if (!res) {
289 return false;
292 if (prefer_ipv4) {
293 struct addrinfo *p;
295 for (p = res; p; p = p->ai_next) {
296 if (p->ai_family == AF_INET) {
297 memcpy(pss, p->ai_addr, p->ai_addrlen);
298 break;
301 if (p == NULL) {
302 /* Copy the first sockaddr. */
303 memcpy(pss, res->ai_addr, res->ai_addrlen);
305 } else {
306 /* Copy the first sockaddr. */
307 memcpy(pss, res->ai_addr, res->ai_addrlen);
310 freeaddrinfo(res);
311 return true;
314 /*******************************************************************
315 Map a text hostname or IP address (IPv4 or IPv6) into a
316 struct sockaddr_storage. Address agnostic version.
317 ******************************************************************/
319 bool interpret_string_addr(struct sockaddr_storage *pss,
320 const char *str,
321 int flags)
323 return interpret_string_addr_pref(pss,
324 str,
325 flags,
326 false);
329 /*******************************************************************
330 Map a text hostname or IP address (IPv4 or IPv6) into a
331 struct sockaddr_storage. Version that prefers IPv4.
332 ******************************************************************/
334 bool interpret_string_addr_prefer_ipv4(struct sockaddr_storage *pss,
335 const char *str,
336 int flags)
338 return interpret_string_addr_pref(pss,
339 str,
340 flags,
341 true);
345 * Interpret an internet address or name into an IP address in 4 byte form.
346 * RETURNS IN NETWORK BYTE ORDER (big endian).
349 uint32_t interpret_addr(const char *str)
351 uint32_t ret;
353 /* If it's in the form of an IP address then
354 * get the lib to interpret it */
355 if (is_ipaddress_v4(str)) {
356 struct in_addr dest;
358 if (inet_pton(AF_INET, str, &dest) <= 0) {
359 /* Error - this shouldn't happen ! */
360 DEBUG(0,("interpret_addr: inet_pton failed "
361 "host %s\n",
362 str));
363 return 0;
365 ret = dest.s_addr; /* NETWORK BYTE ORDER ! */
366 } else {
367 /* Otherwise assume it's a network name of some sort and use
368 getadddrinfo. */
369 struct addrinfo *res = NULL;
370 struct addrinfo *res_list = NULL;
371 if (!interpret_string_addr_internal(&res_list,
372 str,
373 AI_ADDRCONFIG)) {
374 DEBUG(3,("interpret_addr: Unknown host. %s\n",str));
375 return 0;
378 /* Find the first IPv4 address. */
379 for (res = res_list; res; res = res->ai_next) {
380 if (res->ai_family != AF_INET) {
381 continue;
383 if (res->ai_addr == NULL) {
384 continue;
386 break;
388 if(res == NULL) {
389 DEBUG(3,("interpret_addr: host address is "
390 "invalid for host %s\n",str));
391 if (res_list) {
392 freeaddrinfo(res_list);
394 return 0;
396 memcpy((char *)&ret,
397 &((struct sockaddr_in *)res->ai_addr)->sin_addr.s_addr,
398 sizeof(ret));
399 if (res_list) {
400 freeaddrinfo(res_list);
404 /* This is so bogus - all callers need fixing... JRA. */
405 if (ret == (uint32_t)-1) {
406 return 0;
409 return ret;
413 A convenient addition to interpret_addr().
415 _PUBLIC_ struct in_addr interpret_addr2(const char *str)
417 struct in_addr ret;
418 uint32_t a = interpret_addr(str);
419 ret.s_addr = a;
420 return ret;
424 Check if an IP is the 0.0.0.0.
427 _PUBLIC_ bool is_zero_ip_v4(struct in_addr ip)
429 return ip.s_addr == 0;
433 Are two IPs on the same subnet?
436 _PUBLIC_ bool same_net_v4(struct in_addr ip1, struct in_addr ip2, struct in_addr mask)
438 uint32_t net1,net2,nmask;
440 nmask = ntohl(mask.s_addr);
441 net1 = ntohl(ip1.s_addr);
442 net2 = ntohl(ip2.s_addr);
444 return((net1 & nmask) == (net2 & nmask));
448 * Return true if a string could be an IPv4 address.
451 bool is_ipaddress_v4(const char *str)
453 int ret = -1;
454 struct in_addr dest;
456 ret = inet_pton(AF_INET, str, &dest);
457 if (ret > 0) {
458 return true;
460 return false;
463 bool is_ipv6_literal(const char *str)
465 #if defined(HAVE_IPV6)
466 char buf[INET6_ADDRSTRLEN*2] = { 0, };
467 size_t len = strlen(str);
468 char *p = NULL;
470 if (len >= sizeof(buf)) {
471 return false;
474 p = normalize_ipv6_literal(str, buf, &len);
475 if (p == NULL) {
476 return false;
479 return true;
480 #else
481 return false;
482 #endif
486 * Return true if a string could be a IPv6 address.
489 bool is_ipaddress_v6(const char *str)
491 #if defined(HAVE_IPV6)
492 int ret = -1;
493 char *p = NULL;
494 char buf[INET6_ADDRSTRLEN] = { 0, };
495 size_t len;
496 const char *addr = str;
497 const char *idxs = NULL;
498 unsigned int idx = 0;
499 struct in6_addr ip6;
501 p = strchr_m(str, ':');
502 if (p == NULL) {
503 return is_ipv6_literal(str);
506 p = strchr_m(str, SCOPE_DELIMITER);
507 if (p && (p > str)) {
508 len = PTR_DIFF(p, str);
509 idxs = p + 1;
510 } else {
511 len = strlen(str);
514 if (len >= sizeof(buf)) {
515 return false;
517 if (idxs != NULL) {
518 strncpy(buf, str, len);
519 addr = buf;
523 * Cope with link-local.
524 * This is IP:v6:addr%ifidx.
526 if (idxs != NULL) {
527 char c;
529 ret = sscanf(idxs, "%5u%c", &idx, &c);
530 if (ret != 1) {
531 idx = 0;
534 if (idx > 0 && idx < UINT16_MAX) {
535 /* a valid index */
536 idxs = NULL;
541 * Cope with link-local.
542 * This is IP:v6:addr%ifname.
544 if (idxs != NULL) {
545 idx = if_nametoindex(idxs);
547 if (idx > 0) {
548 /* a valid index */
549 idxs = NULL;
553 if (idxs != NULL) {
554 return false;
557 ret = inet_pton(AF_INET6, addr, &ip6);
558 if (ret <= 0) {
559 return false;
562 return true;
563 #else
564 return false;
565 #endif
569 * Return true if a string could be an IPv4 or IPv6 address.
572 bool is_ipaddress(const char *str)
574 return is_ipaddress_v4(str) || is_ipaddress_v6(str);
578 * Is a sockaddr a broadcast address ?
581 bool is_broadcast_addr(const struct sockaddr *pss)
583 #if defined(HAVE_IPV6)
584 if (pss->sa_family == AF_INET6) {
585 const struct in6_addr *sin6 =
586 &((const struct sockaddr_in6 *)pss)->sin6_addr;
587 return IN6_IS_ADDR_MULTICAST(sin6);
589 #endif
590 if (pss->sa_family == AF_INET) {
591 uint32_t addr =
592 ntohl(((const struct sockaddr_in *)pss)->sin_addr.s_addr);
593 return addr == INADDR_BROADCAST;
595 return false;
599 * Check if an IPv7 is 127.0.0.1
601 bool is_loopback_ip_v4(struct in_addr ip)
603 struct in_addr a;
604 a.s_addr = htonl(INADDR_LOOPBACK);
605 return(ip.s_addr == a.s_addr);
609 * Check if a struct sockaddr is the loopback address.
611 bool is_loopback_addr(const struct sockaddr *pss)
613 #if defined(HAVE_IPV6)
614 if (pss->sa_family == AF_INET6) {
615 const struct in6_addr *pin6 =
616 &((const struct sockaddr_in6 *)pss)->sin6_addr;
617 return IN6_IS_ADDR_LOOPBACK(pin6);
619 #endif
620 if (pss->sa_family == AF_INET) {
621 const struct in_addr *pin = &((const struct sockaddr_in *)pss)->sin_addr;
622 return is_loopback_ip_v4(*pin);
624 return false;
628 * Check if a struct sockaddr has an unspecified address.
630 bool is_zero_addr(const struct sockaddr_storage *pss)
632 #if defined(HAVE_IPV6)
633 if (pss->ss_family == AF_INET6) {
634 const struct in6_addr *pin6 =
635 &((const struct sockaddr_in6 *)pss)->sin6_addr;
636 return IN6_IS_ADDR_UNSPECIFIED(pin6);
638 #endif
639 if (pss->ss_family == AF_INET) {
640 const struct in_addr *pin = &((const struct sockaddr_in *)pss)->sin_addr;
641 return is_zero_ip_v4(*pin);
643 return false;
647 * Set an IP to 0.0.0.0.
649 void zero_ip_v4(struct in_addr *ip)
651 ZERO_STRUCTP(ip);
654 bool is_linklocal_addr(const struct sockaddr_storage *pss)
656 #ifdef HAVE_IPV6
657 if (pss->ss_family == AF_INET6) {
658 const struct in6_addr *pin6 =
659 &((const struct sockaddr_in6 *)pss)->sin6_addr;
660 return IN6_IS_ADDR_LINKLOCAL(pin6);
662 #endif
663 if (pss->ss_family == AF_INET) {
664 const struct in_addr *pin =
665 &((const struct sockaddr_in *)pss)->sin_addr;
666 struct in_addr ll_addr;
667 struct in_addr mask_addr;
669 /* 169.254.0.0/16, is link local, see RFC 3927 */
670 ll_addr.s_addr = 0xa9fe0000;
671 mask_addr.s_addr = 0xffff0000;
672 return same_net_v4(*pin, ll_addr, mask_addr);
674 return false;
678 * Convert an IPv4 struct in_addr to a struct sockaddr_storage.
680 void in_addr_to_sockaddr_storage(struct sockaddr_storage *ss,
681 struct in_addr ip)
683 struct sockaddr_in *sa = (struct sockaddr_in *)ss;
684 ZERO_STRUCTP(ss);
685 sa->sin_family = AF_INET;
686 sa->sin_addr = ip;
689 #if defined(HAVE_IPV6)
691 * Convert an IPv6 struct in_addr to a struct sockaddr_storage.
693 void in6_addr_to_sockaddr_storage(struct sockaddr_storage *ss,
694 struct in6_addr ip)
696 struct sockaddr_in6 *sa = (struct sockaddr_in6 *)ss;
697 memset(ss, '\0', sizeof(*ss));
698 sa->sin6_family = AF_INET6;
699 sa->sin6_addr = ip;
701 #endif
704 * Are two IPs on the same subnet?
706 bool same_net(const struct sockaddr *ip1,
707 const struct sockaddr *ip2,
708 const struct sockaddr *mask)
710 if (ip1->sa_family != ip2->sa_family) {
711 /* Never on the same net. */
712 return false;
715 #if defined(HAVE_IPV6)
716 if (ip1->sa_family == AF_INET6) {
717 struct sockaddr_in6 ip1_6 = *(const struct sockaddr_in6 *)ip1;
718 struct sockaddr_in6 ip2_6 = *(const struct sockaddr_in6 *)ip2;
719 struct sockaddr_in6 mask_6 = *(const struct sockaddr_in6 *)mask;
720 char *p1 = (char *)&ip1_6.sin6_addr;
721 char *p2 = (char *)&ip2_6.sin6_addr;
722 char *m = (char *)&mask_6.sin6_addr;
723 size_t i;
725 for (i = 0; i < sizeof(struct in6_addr); i++) {
726 *p1++ &= *m;
727 *p2++ &= *m;
728 m++;
730 return (memcmp(&ip1_6.sin6_addr,
731 &ip2_6.sin6_addr,
732 sizeof(struct in6_addr)) == 0);
734 #endif
735 if (ip1->sa_family == AF_INET) {
736 return same_net_v4(((const struct sockaddr_in *)ip1)->sin_addr,
737 ((const struct sockaddr_in *)ip2)->sin_addr,
738 ((const struct sockaddr_in *)mask)->sin_addr);
740 return false;
744 * Are two sockaddr 's the same family and address ? Ignore port etc.
747 bool sockaddr_equal(const struct sockaddr *ip1,
748 const struct sockaddr *ip2)
750 if (ip1->sa_family != ip2->sa_family) {
751 /* Never the same. */
752 return false;
755 #if defined(HAVE_IPV6)
756 if (ip1->sa_family == AF_INET6) {
757 return (memcmp(&((const struct sockaddr_in6 *)ip1)->sin6_addr,
758 &((const struct sockaddr_in6 *)ip2)->sin6_addr,
759 sizeof(struct in6_addr)) == 0);
761 #endif
762 if (ip1->sa_family == AF_INET) {
763 return (memcmp(&((const struct sockaddr_in *)ip1)->sin_addr,
764 &((const struct sockaddr_in *)ip2)->sin_addr,
765 sizeof(struct in_addr)) == 0);
767 return false;
771 * Is an IP address the INADDR_ANY or in6addr_any value ?
773 bool is_address_any(const struct sockaddr *psa)
775 #if defined(HAVE_IPV6)
776 if (psa->sa_family == AF_INET6) {
777 const struct sockaddr_in6 *si6 = (const struct sockaddr_in6 *)psa;
778 if (memcmp(&in6addr_any,
779 &si6->sin6_addr,
780 sizeof(in6addr_any)) == 0) {
781 return true;
783 return false;
785 #endif
786 if (psa->sa_family == AF_INET) {
787 const struct sockaddr_in *si = (const struct sockaddr_in *)psa;
788 if (si->sin_addr.s_addr == INADDR_ANY) {
789 return true;
791 return false;
793 return false;
796 void set_sockaddr_port(struct sockaddr *psa, uint16_t port)
798 #if defined(HAVE_IPV6)
799 if (psa->sa_family == AF_INET6) {
800 ((struct sockaddr_in6 *)psa)->sin6_port = htons(port);
802 #endif
803 if (psa->sa_family == AF_INET) {
804 ((struct sockaddr_in *)psa)->sin_port = htons(port);
809 /****************************************************************************
810 Get a port number in host byte order from a sockaddr_storage.
811 ****************************************************************************/
813 uint16_t get_sockaddr_port(const struct sockaddr_storage *pss)
815 uint16_t port = 0;
817 if (pss->ss_family != AF_INET) {
818 #if defined(HAVE_IPV6)
819 /* IPv6 */
820 const struct sockaddr_in6 *sa6 =
821 (const struct sockaddr_in6 *)pss;
822 port = ntohs(sa6->sin6_port);
823 #endif
824 } else {
825 const struct sockaddr_in *sa =
826 (const struct sockaddr_in *)pss;
827 port = ntohs(sa->sin_port);
829 return port;
832 /****************************************************************************
833 Print out an IPv4 or IPv6 address from a struct sockaddr_storage.
834 ****************************************************************************/
836 char *print_sockaddr_len(char *dest,
837 size_t destlen,
838 const struct sockaddr *psa,
839 socklen_t psalen)
841 if (destlen > 0) {
842 dest[0] = '\0';
844 (void)sys_getnameinfo(psa,
845 psalen,
846 dest, destlen,
847 NULL, 0,
848 NI_NUMERICHOST);
849 return dest;
852 /****************************************************************************
853 Print out an IPv4 or IPv6 address from a struct sockaddr_storage.
854 ****************************************************************************/
856 char *print_sockaddr(char *dest,
857 size_t destlen,
858 const struct sockaddr_storage *psa)
860 return print_sockaddr_len(dest, destlen, (const struct sockaddr *)psa,
861 sizeof(struct sockaddr_storage));
864 /****************************************************************************
865 Print out a canonical IPv4 or IPv6 address from a struct sockaddr_storage.
866 ****************************************************************************/
868 char *print_canonical_sockaddr(TALLOC_CTX *ctx,
869 const struct sockaddr_storage *pss)
871 char addr[INET6_ADDRSTRLEN];
872 char *dest = NULL;
873 int ret;
875 /* Linux getnameinfo() man pages says port is uninitialized if
876 service name is NULL. */
878 ret = sys_getnameinfo((const struct sockaddr *)pss,
879 sizeof(struct sockaddr_storage),
880 addr, sizeof(addr),
881 NULL, 0,
882 NI_NUMERICHOST);
883 if (ret != 0) {
884 return NULL;
887 if (pss->ss_family != AF_INET) {
888 #if defined(HAVE_IPV6)
889 dest = talloc_asprintf(ctx, "[%s]", addr);
890 #else
891 return NULL;
892 #endif
893 } else {
894 dest = talloc_asprintf(ctx, "%s", addr);
897 return dest;
900 enum SOCK_OPT_TYPES {OPT_BOOL,OPT_INT,OPT_ON};
902 typedef struct smb_socket_option {
903 const char *name;
904 int level;
905 int option;
906 int value;
907 int opttype;
908 } smb_socket_option;
910 static const smb_socket_option socket_options[] = {
911 {"SO_KEEPALIVE", SOL_SOCKET, SO_KEEPALIVE, 0, OPT_BOOL},
912 {"SO_REUSEADDR", SOL_SOCKET, SO_REUSEADDR, 0, OPT_BOOL},
913 {"SO_BROADCAST", SOL_SOCKET, SO_BROADCAST, 0, OPT_BOOL},
914 #ifdef TCP_NODELAY
915 {"TCP_NODELAY", IPPROTO_TCP, TCP_NODELAY, 0, OPT_BOOL},
916 #endif
917 #ifdef TCP_KEEPCNT
918 {"TCP_KEEPCNT", IPPROTO_TCP, TCP_KEEPCNT, 0, OPT_INT},
919 #endif
920 #ifdef TCP_KEEPIDLE
921 {"TCP_KEEPIDLE", IPPROTO_TCP, TCP_KEEPIDLE, 0, OPT_INT},
922 #endif
923 #ifdef TCP_KEEPINTVL
924 {"TCP_KEEPINTVL", IPPROTO_TCP, TCP_KEEPINTVL, 0, OPT_INT},
925 #endif
926 #ifdef IPTOS_LOWDELAY
927 {"IPTOS_LOWDELAY", IPPROTO_IP, IP_TOS, IPTOS_LOWDELAY, OPT_ON},
928 #endif
929 #ifdef IPTOS_THROUGHPUT
930 {"IPTOS_THROUGHPUT", IPPROTO_IP, IP_TOS, IPTOS_THROUGHPUT, OPT_ON},
931 #endif
932 #ifdef SO_REUSEPORT
933 {"SO_REUSEPORT", SOL_SOCKET, SO_REUSEPORT, 0, OPT_BOOL},
934 #endif
935 #ifdef SO_SNDBUF
936 {"SO_SNDBUF", SOL_SOCKET, SO_SNDBUF, 0, OPT_INT},
937 #endif
938 #ifdef SO_RCVBUF
939 {"SO_RCVBUF", SOL_SOCKET, SO_RCVBUF, 0, OPT_INT},
940 #endif
941 #ifdef SO_SNDLOWAT
942 {"SO_SNDLOWAT", SOL_SOCKET, SO_SNDLOWAT, 0, OPT_INT},
943 #endif
944 #ifdef SO_RCVLOWAT
945 {"SO_RCVLOWAT", SOL_SOCKET, SO_RCVLOWAT, 0, OPT_INT},
946 #endif
947 #ifdef SO_SNDTIMEO
948 {"SO_SNDTIMEO", SOL_SOCKET, SO_SNDTIMEO, 0, OPT_INT},
949 #endif
950 #ifdef SO_RCVTIMEO
951 {"SO_RCVTIMEO", SOL_SOCKET, SO_RCVTIMEO, 0, OPT_INT},
952 #endif
953 #ifdef TCP_FASTACK
954 {"TCP_FASTACK", IPPROTO_TCP, TCP_FASTACK, 0, OPT_INT},
955 #endif
956 #ifdef TCP_QUICKACK
957 {"TCP_QUICKACK", IPPROTO_TCP, TCP_QUICKACK, 0, OPT_BOOL},
958 #endif
959 #ifdef TCP_NODELAYACK
960 {"TCP_NODELAYACK", IPPROTO_TCP, TCP_NODELAYACK, 0, OPT_BOOL},
961 #endif
962 #ifdef TCP_KEEPALIVE_THRESHOLD
963 {"TCP_KEEPALIVE_THRESHOLD", IPPROTO_TCP, TCP_KEEPALIVE_THRESHOLD, 0, OPT_INT},
964 #endif
965 #ifdef TCP_KEEPALIVE_ABORT_THRESHOLD
966 {"TCP_KEEPALIVE_ABORT_THRESHOLD", IPPROTO_TCP, TCP_KEEPALIVE_ABORT_THRESHOLD, 0, OPT_INT},
967 #endif
968 #ifdef TCP_DEFER_ACCEPT
969 {"TCP_DEFER_ACCEPT", IPPROTO_TCP, TCP_DEFER_ACCEPT, 0, OPT_INT},
970 #endif
971 #ifdef TCP_USER_TIMEOUT
972 {"TCP_USER_TIMEOUT", IPPROTO_TCP, TCP_USER_TIMEOUT, 0, OPT_INT},
973 #endif
974 {NULL,0,0,0,0}};
976 /****************************************************************************
977 Print socket options.
978 ****************************************************************************/
980 static void print_socket_options(int s)
982 TALLOC_CTX *frame = NULL;
983 const smb_socket_option *p = &socket_options[0];
984 char *str = NULL;
986 if (DEBUGLEVEL < 5) {
987 return;
990 frame = talloc_stackframe();
992 str = talloc_strdup(frame, "");
993 if (str == NULL) {
994 DBG_WARNING("talloc failed\n");
995 goto done;
998 for (; p->name != NULL; p++) {
999 int ret, val;
1000 socklen_t vlen = sizeof(val);
1002 ret = getsockopt(s, p->level, p->option, (void *)&val, &vlen);
1003 if (ret == -1) {
1004 DBG_INFO("Could not test socket option %s: %s.\n",
1005 p->name, strerror(errno));
1006 continue;
1009 str = talloc_asprintf_append_buffer(
1010 str,
1011 "%s%s=%d",
1012 str[0] != '\0' ? ", " : "",
1013 p->name,
1014 val);
1015 if (str == NULL) {
1016 DBG_WARNING("talloc_asprintf_append_buffer failed\n");
1017 goto done;
1021 DEBUG(5, ("socket options: %s\n", str));
1022 done:
1023 TALLOC_FREE(frame);
1026 /****************************************************************************
1027 Set user socket options.
1028 ****************************************************************************/
1030 void set_socket_options(int fd, const char *options)
1032 TALLOC_CTX *ctx = talloc_new(NULL);
1033 char *tok;
1035 while (next_token_talloc(ctx, &options, &tok," \t,")) {
1036 int ret=0,i;
1037 int value = 1;
1038 char *p;
1039 bool got_value = false;
1041 if ((p = strchr_m(tok,'='))) {
1042 *p = 0;
1043 value = atoi(p+1);
1044 got_value = true;
1047 for (i=0;socket_options[i].name;i++)
1048 if (strequal(socket_options[i].name,tok))
1049 break;
1051 if (!socket_options[i].name) {
1052 DEBUG(0,("Unknown socket option %s\n",tok));
1053 continue;
1056 switch (socket_options[i].opttype) {
1057 case OPT_BOOL:
1058 case OPT_INT:
1059 ret = setsockopt(fd,socket_options[i].level,
1060 socket_options[i].option,
1061 (char *)&value,sizeof(int));
1062 break;
1064 case OPT_ON:
1065 if (got_value)
1066 DEBUG(0,("syntax error - %s "
1067 "does not take a value\n",tok));
1070 int on = socket_options[i].value;
1071 ret = setsockopt(fd,socket_options[i].level,
1072 socket_options[i].option,
1073 (char *)&on,sizeof(int));
1075 break;
1078 if (ret != 0) {
1079 /* be aware that some systems like Solaris return
1080 * EINVAL to a setsockopt() call when the client
1081 * sent a RST previously - no need to worry */
1082 DEBUG(2,("Failed to set socket option %s (Error %s)\n",
1083 tok, strerror(errno) ));
1087 TALLOC_FREE(ctx);
1088 print_socket_options(fd);
1092 * Utility function that copes only with AF_INET and AF_INET6
1093 * as that's all we're going to get out of DNS / NetBIOS / WINS
1094 * name resolution functions.
1097 bool sockaddr_storage_to_samba_sockaddr(
1098 struct samba_sockaddr *sa, const struct sockaddr_storage *ss)
1100 sa->u.ss = *ss;
1102 switch (ss->ss_family) {
1103 case AF_INET:
1104 sa->sa_socklen = sizeof(struct sockaddr_in);
1105 break;
1106 #ifdef HAVE_IPV6
1107 case AF_INET6:
1108 sa->sa_socklen = sizeof(struct sockaddr_in6);
1109 break;
1110 #endif
1111 default:
1112 return false;
1114 return true;
1117 bool samba_sockaddr_set_port(struct samba_sockaddr *sa, uint16_t port)
1119 if (sa->u.sa.sa_family == AF_INET) {
1120 sa->u.in.sin_port = htons(port);
1121 return true;
1123 #ifdef HAVE_IPV6
1124 if (sa->u.sa.sa_family == AF_INET6) {
1125 sa->u.in6.sin6_port = htons(port);
1126 return true;
1128 #endif
1129 return false;
1132 bool samba_sockaddr_get_port(const struct samba_sockaddr *sa, uint16_t *port)
1134 if (sa->u.sa.sa_family == AF_INET) {
1135 *port = ntohs(sa->u.in.sin_port);
1136 return true;
1138 #ifdef HAVE_IPV6
1139 if (sa->u.sa.sa_family == AF_INET6) {
1140 *port = ntohs(sa->u.in6.sin6_port);
1141 return true;
1143 #endif
1144 return false;