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
)
39 /* Ensure we're at least a valid sockaddr-storage. */
40 *pss
= (struct sockaddr_storage
) { .ss_family
= AF_INET
};
43 static char *normalize_ipv6_literal(const char *str
, char *buf
, size_t *_len
)
45 #define IPv6_LITERAL_NET ".ipv6-literal.net"
46 const size_t llen
= sizeof(IPv6_LITERAL_NET
) - 1;
51 size_t cnt_delimiter
= 0;
58 /* ignore a trailing '.' */
59 if (str
[len
- 1] == '.') {
64 if (len
>= INET6_ADDRSTRLEN
) {
71 cmp
= strncasecmp(&str
[len
], IPv6_LITERAL_NET
, llen
);
76 for (i
= 0; i
< len
; i
++) {
88 buf
[i
] = SCOPE_DELIMITER
;
122 if (cnt_delimiter
> 7) {
127 if (cnt_delimiter
< 2) {
131 for (; idx_chars
!= 0 && i
< len
; i
++) {
133 case SCOPE_DELIMITER
:
143 if (idx_chars
== 1) {
153 * Wrap getaddrinfo...
155 bool interpret_string_addr_internal(struct addrinfo
**ppres
,
156 const char *str
, int flags
)
159 struct addrinfo hints
;
160 #if defined(HAVE_IPV6)
161 char addr
[INET6_ADDRSTRLEN
*2] = { 0, };
162 unsigned int scope_id
= 0;
163 size_t len
= strlen(str
);
168 /* By default make sure it supports TCP. */
169 hints
.ai_socktype
= SOCK_STREAM
;
171 /* always try as a numeric host first. This prevents unnecessary name
172 * lookups, and also ensures we accept IPv6 addresses */
173 hints
.ai_flags
= AI_PASSIVE
| AI_NUMERICHOST
;
175 #if defined(HAVE_IPV6)
176 if (len
< sizeof(addr
)) {
179 p
= normalize_ipv6_literal(str
, addr
, &len
);
181 hints
.ai_family
= AF_INET6
;
186 if (strchr_m(str
, ':')) {
187 char *p
= strchr_m(str
, SCOPE_DELIMITER
);
190 * Cope with link-local.
191 * This is IP:v6:addr%ifname.
194 if (p
&& (p
> str
) && ((scope_id
= if_nametoindex(p
+1)) != 0)) {
195 /* Length of string we want to copy.
196 This is IP:v6:addr (removing the %ifname).
198 len
= PTR_DIFF(p
,str
);
200 if (len
+1 > sizeof(addr
)) {
201 /* string+nul too long for array. */
205 memcpy(addr
, str
, len
);
214 ret
= getaddrinfo(str
, NULL
, &hints
, ppres
);
216 #if defined(HAVE_IPV6)
217 struct sockaddr_in6
*ps6
= NULL
;
225 if ((*ppres
) == NULL
) {
228 if ((*ppres
)->ai_addr
->sa_family
!= AF_INET6
) {
232 ps6
= (struct sockaddr_in6
*)(*ppres
)->ai_addr
;
234 if (IN6_IS_ADDR_LINKLOCAL(&ps6
->sin6_addr
) &&
235 ps6
->sin6_scope_id
== 0) {
236 ps6
->sin6_scope_id
= scope_id
;
243 hints
.ai_flags
= flags
;
245 /* Linux man page on getaddrinfo() says port will be
246 uninitialized when service string is NULL */
248 ret
= getaddrinfo(str
, NULL
,
253 DEBUG(3, ("interpret_string_addr_internal: "
254 "getaddrinfo failed for name %s (flags %d) [%s]\n",
255 str
, flags
, gai_strerror(ret
)));
261 /*******************************************************************
262 Map a text hostname or IP address (IPv4 or IPv6) into a
263 struct sockaddr_storage. Takes a flag which allows it to
264 prefer an IPv4 address (needed for DC's).
265 ******************************************************************/
267 static bool interpret_string_addr_pref(struct sockaddr_storage
*pss
,
272 struct addrinfo
*res
= NULL
;
277 if (flags
& AI_NUMERICHOST
) {
280 int_flags
= flags
|AI_ADDRCONFIG
;
283 if (!interpret_string_addr_internal(&res
, str
, int_flags
)) {
293 for (p
= res
; p
; p
= p
->ai_next
) {
294 if (p
->ai_family
== AF_INET
) {
295 memcpy(pss
, p
->ai_addr
, p
->ai_addrlen
);
300 /* Copy the first sockaddr. */
301 memcpy(pss
, res
->ai_addr
, res
->ai_addrlen
);
304 /* Copy the first sockaddr. */
305 memcpy(pss
, res
->ai_addr
, res
->ai_addrlen
);
312 /*******************************************************************
313 Map a text hostname or IP address (IPv4 or IPv6) into a
314 struct sockaddr_storage. Address agnostic version.
315 ******************************************************************/
317 bool interpret_string_addr(struct sockaddr_storage
*pss
,
321 return interpret_string_addr_pref(pss
,
327 /*******************************************************************
328 Map a text hostname or IP address (IPv4 or IPv6) into a
329 struct sockaddr_storage. Version that prefers IPv4.
330 ******************************************************************/
332 bool interpret_string_addr_prefer_ipv4(struct sockaddr_storage
*pss
,
336 return interpret_string_addr_pref(pss
,
343 * Interpret an internet address or name into an IP address in 4 byte form.
344 * RETURNS IN NETWORK BYTE ORDER (big endian).
347 uint32_t interpret_addr(const char *str
)
351 /* If it's in the form of an IP address then
352 * get the lib to interpret it */
353 if (is_ipaddress_v4(str
)) {
356 if (inet_pton(AF_INET
, str
, &dest
) <= 0) {
357 /* Error - this shouldn't happen ! */
358 DEBUG(0,("interpret_addr: inet_pton failed "
363 ret
= dest
.s_addr
; /* NETWORK BYTE ORDER ! */
365 /* Otherwise assume it's a network name of some sort and use
367 struct addrinfo
*res
= NULL
;
368 struct addrinfo
*res_list
= NULL
;
369 if (!interpret_string_addr_internal(&res_list
,
372 DEBUG(3,("interpret_addr: Unknown host. %s\n",str
));
376 /* Find the first IPv4 address. */
377 for (res
= res_list
; res
; res
= res
->ai_next
) {
378 if (res
->ai_family
!= AF_INET
) {
381 if (res
->ai_addr
== NULL
) {
387 DEBUG(3,("interpret_addr: host address is "
388 "invalid for host %s\n",str
));
390 freeaddrinfo(res_list
);
395 &((struct sockaddr_in
*)res
->ai_addr
)->sin_addr
.s_addr
,
398 freeaddrinfo(res_list
);
402 /* This is so bogus - all callers need fixing... JRA. */
403 if (ret
== (uint32_t)-1) {
411 A convenient addition to interpret_addr().
413 _PUBLIC_
struct in_addr
interpret_addr2(const char *str
)
416 uint32_t a
= interpret_addr(str
);
422 Check if an IP is the 0.0.0.0.
425 _PUBLIC_
bool is_zero_ip_v4(struct in_addr ip
)
427 return ip
.s_addr
== 0;
431 Are two IPs on the same subnet?
434 _PUBLIC_
bool same_net_v4(struct in_addr ip1
, struct in_addr ip2
, struct in_addr mask
)
436 uint32_t net1
,net2
,nmask
;
438 nmask
= ntohl(mask
.s_addr
);
439 net1
= ntohl(ip1
.s_addr
);
440 net2
= ntohl(ip2
.s_addr
);
442 return((net1
& nmask
) == (net2
& nmask
));
446 * Return true if a string could be an IPv4 address.
449 bool is_ipaddress_v4(const char *str
)
454 ret
= inet_pton(AF_INET
, str
, &dest
);
461 bool is_ipv6_literal(const char *str
)
463 #if defined(HAVE_IPV6)
464 char buf
[INET6_ADDRSTRLEN
*2] = { 0, };
465 size_t len
= strlen(str
);
468 if (len
>= sizeof(buf
)) {
472 p
= normalize_ipv6_literal(str
, buf
, &len
);
484 * Return true if a string could be a IPv6 address.
487 bool is_ipaddress_v6(const char *str
)
489 #if defined(HAVE_IPV6)
492 char buf
[INET6_ADDRSTRLEN
] = { 0, };
494 const char *addr
= str
;
495 const char *idxs
= NULL
;
496 unsigned int idx
= 0;
499 p
= strchr_m(str
, ':');
501 return is_ipv6_literal(str
);
504 p
= strchr_m(str
, SCOPE_DELIMITER
);
505 if (p
&& (p
> str
)) {
506 len
= PTR_DIFF(p
, str
);
512 if (len
>= sizeof(buf
)) {
516 strncpy(buf
, str
, len
);
521 * Cope with link-local.
522 * This is IP:v6:addr%ifidx.
527 ret
= sscanf(idxs
, "%5u%c", &idx
, &c
);
532 if (idx
> 0 && idx
< UINT16_MAX
) {
539 * Cope with link-local.
540 * This is IP:v6:addr%ifname.
543 idx
= if_nametoindex(idxs
);
555 ret
= inet_pton(AF_INET6
, addr
, &ip6
);
566 * Return true if a string could be an IPv4 or IPv6 address.
569 bool is_ipaddress(const char *str
)
571 return is_ipaddress_v4(str
) || is_ipaddress_v6(str
);
575 * Is a sockaddr a broadcast address ?
578 bool is_broadcast_addr(const struct sockaddr
*pss
)
580 #if defined(HAVE_IPV6)
581 if (pss
->sa_family
== AF_INET6
) {
582 const struct in6_addr
*sin6
=
583 &((const struct sockaddr_in6
*)pss
)->sin6_addr
;
584 return IN6_IS_ADDR_MULTICAST(sin6
);
587 if (pss
->sa_family
== AF_INET
) {
589 ntohl(((const struct sockaddr_in
*)pss
)->sin_addr
.s_addr
);
590 return addr
== INADDR_BROADCAST
;
596 * Check if an IPv7 is 127.0.0.1
598 bool is_loopback_ip_v4(struct in_addr ip
)
601 a
.s_addr
= htonl(INADDR_LOOPBACK
);
602 return(ip
.s_addr
== a
.s_addr
);
606 * Check if a struct sockaddr is the loopback address.
608 bool is_loopback_addr(const struct sockaddr
*pss
)
610 #if defined(HAVE_IPV6)
611 if (pss
->sa_family
== AF_INET6
) {
612 const struct in6_addr
*pin6
=
613 &((const struct sockaddr_in6
*)pss
)->sin6_addr
;
614 return IN6_IS_ADDR_LOOPBACK(pin6
);
617 if (pss
->sa_family
== AF_INET
) {
618 const struct in_addr
*pin
= &((const struct sockaddr_in
*)pss
)->sin_addr
;
619 return is_loopback_ip_v4(*pin
);
625 * Check if a struct sockaddr has an unspecified address.
627 bool is_zero_addr(const struct sockaddr_storage
*pss
)
629 #if defined(HAVE_IPV6)
630 if (pss
->ss_family
== AF_INET6
) {
631 const struct in6_addr
*pin6
=
632 &((const struct sockaddr_in6
*)pss
)->sin6_addr
;
633 return IN6_IS_ADDR_UNSPECIFIED(pin6
);
636 if (pss
->ss_family
== AF_INET
) {
637 const struct in_addr
*pin
= &((const struct sockaddr_in
*)pss
)->sin_addr
;
638 return is_zero_ip_v4(*pin
);
644 * Set an IP to 0.0.0.0.
646 void zero_ip_v4(struct in_addr
*ip
)
651 bool is_linklocal_addr(const struct sockaddr_storage
*pss
)
654 if (pss
->ss_family
== AF_INET6
) {
655 const struct in6_addr
*pin6
=
656 &((const struct sockaddr_in6
*)pss
)->sin6_addr
;
657 return IN6_IS_ADDR_LINKLOCAL(pin6
);
660 if (pss
->ss_family
== AF_INET
) {
661 const struct in_addr
*pin
=
662 &((const struct sockaddr_in
*)pss
)->sin_addr
;
663 struct in_addr ll_addr
;
664 struct in_addr mask_addr
;
666 /* 169.254.0.0/16, is link local, see RFC 3927 */
667 ll_addr
.s_addr
= 0xa9fe0000;
668 mask_addr
.s_addr
= 0xffff0000;
669 return same_net_v4(*pin
, ll_addr
, mask_addr
);
675 * Convert an IPv4 struct in_addr to a struct sockaddr_storage.
677 void in_addr_to_sockaddr_storage(struct sockaddr_storage
*ss
,
680 struct sockaddr_in
*sa
= (struct sockaddr_in
*)ss
;
682 sa
->sin_family
= AF_INET
;
686 #if defined(HAVE_IPV6)
688 * Convert an IPv6 struct in_addr to a struct sockaddr_storage.
690 void in6_addr_to_sockaddr_storage(struct sockaddr_storage
*ss
,
693 struct sockaddr_in6
*sa
= (struct sockaddr_in6
*)ss
;
694 memset(ss
, '\0', sizeof(*ss
));
695 sa
->sin6_family
= AF_INET6
;
701 * Are two IPs on the same subnet?
703 bool same_net(const struct sockaddr
*ip1
,
704 const struct sockaddr
*ip2
,
705 const struct sockaddr
*mask
)
707 if (ip1
->sa_family
!= ip2
->sa_family
) {
708 /* Never on the same net. */
712 #if defined(HAVE_IPV6)
713 if (ip1
->sa_family
== AF_INET6
) {
714 struct sockaddr_in6 ip1_6
= *(const struct sockaddr_in6
*)ip1
;
715 struct sockaddr_in6 ip2_6
= *(const struct sockaddr_in6
*)ip2
;
716 struct sockaddr_in6 mask_6
= *(const struct sockaddr_in6
*)mask
;
717 char *p1
= (char *)&ip1_6
.sin6_addr
;
718 char *p2
= (char *)&ip2_6
.sin6_addr
;
719 char *m
= (char *)&mask_6
.sin6_addr
;
722 for (i
= 0; i
< sizeof(struct in6_addr
); i
++) {
727 return (memcmp(&ip1_6
.sin6_addr
,
729 sizeof(struct in6_addr
)) == 0);
732 if (ip1
->sa_family
== AF_INET
) {
733 return same_net_v4(((const struct sockaddr_in
*)ip1
)->sin_addr
,
734 ((const struct sockaddr_in
*)ip2
)->sin_addr
,
735 ((const struct sockaddr_in
*)mask
)->sin_addr
);
741 * Are two sockaddr 's the same family and address ? Ignore port etc.
744 bool sockaddr_equal(const struct sockaddr
*ip1
,
745 const struct sockaddr
*ip2
)
747 if (ip1
->sa_family
!= ip2
->sa_family
) {
748 /* Never the same. */
752 #if defined(HAVE_IPV6)
753 if (ip1
->sa_family
== AF_INET6
) {
754 return (memcmp(&((const struct sockaddr_in6
*)ip1
)->sin6_addr
,
755 &((const struct sockaddr_in6
*)ip2
)->sin6_addr
,
756 sizeof(struct in6_addr
)) == 0);
759 if (ip1
->sa_family
== AF_INET
) {
760 return (memcmp(&((const struct sockaddr_in
*)ip1
)->sin_addr
,
761 &((const struct sockaddr_in
*)ip2
)->sin_addr
,
762 sizeof(struct in_addr
)) == 0);
768 * Is an IP address the INADDR_ANY or in6addr_any value ?
770 bool is_address_any(const struct sockaddr
*psa
)
772 #if defined(HAVE_IPV6)
773 if (psa
->sa_family
== AF_INET6
) {
774 const struct sockaddr_in6
*si6
= (const struct sockaddr_in6
*)psa
;
775 if (memcmp(&in6addr_any
,
777 sizeof(in6addr_any
)) == 0) {
783 if (psa
->sa_family
== AF_INET
) {
784 const struct sockaddr_in
*si
= (const struct sockaddr_in
*)psa
;
785 if (si
->sin_addr
.s_addr
== INADDR_ANY
) {
793 void set_sockaddr_port(struct sockaddr
*psa
, uint16_t port
)
795 #if defined(HAVE_IPV6)
796 if (psa
->sa_family
== AF_INET6
) {
797 ((struct sockaddr_in6
*)psa
)->sin6_port
= htons(port
);
800 if (psa
->sa_family
== AF_INET
) {
801 ((struct sockaddr_in
*)psa
)->sin_port
= htons(port
);
806 /****************************************************************************
807 Get a port number in host byte order from a sockaddr_storage.
808 ****************************************************************************/
810 uint16_t get_sockaddr_port(const struct sockaddr_storage
*pss
)
814 if (pss
->ss_family
!= AF_INET
) {
815 #if defined(HAVE_IPV6)
817 const struct sockaddr_in6
*sa6
=
818 (const struct sockaddr_in6
*)pss
;
819 port
= ntohs(sa6
->sin6_port
);
822 const struct sockaddr_in
*sa
=
823 (const struct sockaddr_in
*)pss
;
824 port
= ntohs(sa
->sin_port
);
829 /****************************************************************************
830 Print out an IPv4 or IPv6 address from a struct sockaddr_storage.
831 ****************************************************************************/
833 char *print_sockaddr_len(char *dest
,
835 const struct sockaddr
*psa
,
841 (void)sys_getnameinfo(psa
,
849 /****************************************************************************
850 Print out an IPv4 or IPv6 address from a struct sockaddr_storage.
851 ****************************************************************************/
853 char *print_sockaddr(char *dest
,
855 const struct sockaddr_storage
*psa
)
857 return print_sockaddr_len(dest
, destlen
, (const struct sockaddr
*)psa
,
858 sizeof(struct sockaddr_storage
));
861 /****************************************************************************
862 Print out a canonical IPv4 or IPv6 address from a struct sockaddr_storage.
863 ****************************************************************************/
865 char *print_canonical_sockaddr(TALLOC_CTX
*ctx
,
866 const struct sockaddr_storage
*pss
)
868 char addr
[INET6_ADDRSTRLEN
];
872 /* Linux getnameinfo() man pages says port is unitialized if
873 service name is NULL. */
875 ret
= sys_getnameinfo((const struct sockaddr
*)pss
,
876 sizeof(struct sockaddr_storage
),
884 if (pss
->ss_family
!= AF_INET
) {
885 #if defined(HAVE_IPV6)
886 dest
= talloc_asprintf(ctx
, "[%s]", addr
);
891 dest
= talloc_asprintf(ctx
, "%s", addr
);
897 /****************************************************************************
898 Return the port number we've bound to on a socket.
899 ****************************************************************************/
901 int get_socket_port(int fd
)
903 struct sockaddr_storage sa
;
904 socklen_t length
= sizeof(sa
);
910 if (getsockname(fd
, (struct sockaddr
*)&sa
, &length
) < 0) {
911 int level
= (errno
== ENOTCONN
) ? 2 : 0;
912 DEBUG(level
, ("getsockname failed. Error was %s\n",
917 #if defined(HAVE_IPV6)
918 if (sa
.ss_family
== AF_INET6
) {
919 struct sockaddr_in6
*sa_in6
= (struct sockaddr_in6
*)&sa
;
920 return ntohs(sa_in6
->sin6_port
);
923 if (sa
.ss_family
== AF_INET
) {
924 struct sockaddr_in
*sa_in
= (struct sockaddr_in
*)&sa
;
925 return ntohs(sa_in
->sin_port
);
930 /****************************************************************************
931 Return the string of an IP address (IPv4 or IPv6).
932 ****************************************************************************/
934 static const char *get_socket_addr(int fd
, char *addr_buf
, size_t addr_len
)
936 struct sockaddr_storage sa
;
937 socklen_t length
= sizeof(sa
);
939 /* Ok, returning a hard coded IPv4 address
940 * is bogus, but it's just as bogus as a
941 * zero IPv6 address. No good choice here.
944 if (strlcpy(addr_buf
, "0.0.0.0", addr_len
) >= addr_len
) {
953 if (getsockname(fd
, (struct sockaddr
*)&sa
, &length
) < 0) {
954 DEBUG(0,("getsockname failed. Error was %s\n",
959 return print_sockaddr_len(addr_buf
, addr_len
, (struct sockaddr
*)&sa
, length
);
962 const char *client_socket_addr(int fd
, char *addr
, size_t addr_len
)
964 return get_socket_addr(fd
, addr
, addr_len
);
968 enum SOCK_OPT_TYPES
{OPT_BOOL
,OPT_INT
,OPT_ON
};
970 typedef struct smb_socket_option
{
978 static const smb_socket_option socket_options
[] = {
979 {"SO_KEEPALIVE", SOL_SOCKET
, SO_KEEPALIVE
, 0, OPT_BOOL
},
980 {"SO_REUSEADDR", SOL_SOCKET
, SO_REUSEADDR
, 0, OPT_BOOL
},
981 {"SO_BROADCAST", SOL_SOCKET
, SO_BROADCAST
, 0, OPT_BOOL
},
983 {"TCP_NODELAY", IPPROTO_TCP
, TCP_NODELAY
, 0, OPT_BOOL
},
986 {"TCP_KEEPCNT", IPPROTO_TCP
, TCP_KEEPCNT
, 0, OPT_INT
},
989 {"TCP_KEEPIDLE", IPPROTO_TCP
, TCP_KEEPIDLE
, 0, OPT_INT
},
992 {"TCP_KEEPINTVL", IPPROTO_TCP
, TCP_KEEPINTVL
, 0, OPT_INT
},
994 #ifdef IPTOS_LOWDELAY
995 {"IPTOS_LOWDELAY", IPPROTO_IP
, IP_TOS
, IPTOS_LOWDELAY
, OPT_ON
},
997 #ifdef IPTOS_THROUGHPUT
998 {"IPTOS_THROUGHPUT", IPPROTO_IP
, IP_TOS
, IPTOS_THROUGHPUT
, OPT_ON
},
1001 {"SO_REUSEPORT", SOL_SOCKET
, SO_REUSEPORT
, 0, OPT_BOOL
},
1004 {"SO_SNDBUF", SOL_SOCKET
, SO_SNDBUF
, 0, OPT_INT
},
1007 {"SO_RCVBUF", SOL_SOCKET
, SO_RCVBUF
, 0, OPT_INT
},
1010 {"SO_SNDLOWAT", SOL_SOCKET
, SO_SNDLOWAT
, 0, OPT_INT
},
1013 {"SO_RCVLOWAT", SOL_SOCKET
, SO_RCVLOWAT
, 0, OPT_INT
},
1016 {"SO_SNDTIMEO", SOL_SOCKET
, SO_SNDTIMEO
, 0, OPT_INT
},
1019 {"SO_RCVTIMEO", SOL_SOCKET
, SO_RCVTIMEO
, 0, OPT_INT
},
1022 {"TCP_FASTACK", IPPROTO_TCP
, TCP_FASTACK
, 0, OPT_INT
},
1025 {"TCP_QUICKACK", IPPROTO_TCP
, TCP_QUICKACK
, 0, OPT_BOOL
},
1027 #ifdef TCP_NODELAYACK
1028 {"TCP_NODELAYACK", IPPROTO_TCP
, TCP_NODELAYACK
, 0, OPT_BOOL
},
1030 #ifdef TCP_KEEPALIVE_THRESHOLD
1031 {"TCP_KEEPALIVE_THRESHOLD", IPPROTO_TCP
, TCP_KEEPALIVE_THRESHOLD
, 0, OPT_INT
},
1033 #ifdef TCP_KEEPALIVE_ABORT_THRESHOLD
1034 {"TCP_KEEPALIVE_ABORT_THRESHOLD", IPPROTO_TCP
, TCP_KEEPALIVE_ABORT_THRESHOLD
, 0, OPT_INT
},
1036 #ifdef TCP_DEFER_ACCEPT
1037 {"TCP_DEFER_ACCEPT", IPPROTO_TCP
, TCP_DEFER_ACCEPT
, 0, OPT_INT
},
1041 /****************************************************************************
1042 Print socket options.
1043 ****************************************************************************/
1045 static void print_socket_options(int s
)
1049 const smb_socket_option
*p
= &socket_options
[0];
1051 /* wrapped in if statement to prevent streams
1052 * leak in SCO Openserver 5.0 */
1053 /* reported on samba-technical --jerry */
1054 if ( DEBUGLEVEL
>= 5 ) {
1055 DEBUG(5,("Socket options:\n"));
1056 for (; p
->name
!= NULL
; p
++) {
1057 if (getsockopt(s
, p
->level
, p
->option
,
1058 (void *)&value
, &vlen
) == -1) {
1059 DEBUGADD(5,("\tCould not test socket option %s.\n",
1062 DEBUGADD(5,("\t%s = %d\n",
1069 /****************************************************************************
1070 Set user socket options.
1071 ****************************************************************************/
1073 void set_socket_options(int fd
, const char *options
)
1075 TALLOC_CTX
*ctx
= talloc_new(NULL
);
1078 while (next_token_talloc(ctx
, &options
, &tok
," \t,")) {
1082 bool got_value
= false;
1084 if ((p
= strchr_m(tok
,'='))) {
1090 for (i
=0;socket_options
[i
].name
;i
++)
1091 if (strequal(socket_options
[i
].name
,tok
))
1094 if (!socket_options
[i
].name
) {
1095 DEBUG(0,("Unknown socket option %s\n",tok
));
1099 switch (socket_options
[i
].opttype
) {
1102 ret
= setsockopt(fd
,socket_options
[i
].level
,
1103 socket_options
[i
].option
,
1104 (char *)&value
,sizeof(int));
1109 DEBUG(0,("syntax error - %s "
1110 "does not take a value\n",tok
));
1113 int on
= socket_options
[i
].value
;
1114 ret
= setsockopt(fd
,socket_options
[i
].level
,
1115 socket_options
[i
].option
,
1116 (char *)&on
,sizeof(int));
1122 /* be aware that some systems like Solaris return
1123 * EINVAL to a setsockopt() call when the client
1124 * sent a RST previously - no need to worry */
1125 DEBUG(2,("Failed to set socket option %s (Error %s)\n",
1126 tok
, strerror(errno
) ));
1131 print_socket_options(fd
);