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
)
40 /* Ensure we're at least a valid sockaddr-storage. */
41 pss
->ss_family
= AF_INET
;
44 static char *normalize_ipv6_literal(const char *str
, char *buf
, size_t *_len
)
46 #define IPv6_LITERAL_NET ".ipv6-literal.net"
47 static const size_t llen
= sizeof(IPv6_LITERAL_NET
) - 1;
52 size_t cnt_delimiter
= 0;
59 /* ignore a trailing '.' */
60 if (str
[len
- 1] == '.') {
65 if (len
>= INET6_ADDRSTRLEN
) {
72 cmp
= strncasecmp(&str
[len
], IPv6_LITERAL_NET
, llen
);
77 for (i
= 0; i
< len
; i
++) {
123 if (cnt_delimiter
> 7) {
128 if (cnt_delimiter
< 2) {
132 for (; idx_chars
!= 0 && i
< len
; i
++) {
144 if (idx_chars
== 1) {
154 * Wrap getaddrinfo...
156 bool interpret_string_addr_internal(struct addrinfo
**ppres
,
157 const char *str
, int flags
)
160 struct addrinfo hints
;
161 #if defined(HAVE_IPV6)
162 char addr
[INET6_ADDRSTRLEN
*2] = { 0, };
163 unsigned int scope_id
= 0;
164 size_t len
= strlen(str
);
169 /* By default make sure it supports TCP. */
170 hints
.ai_socktype
= SOCK_STREAM
;
172 /* always try as a numeric host first. This prevents unnecessary name
173 * lookups, and also ensures we accept IPv6 addresses */
174 hints
.ai_flags
= AI_PASSIVE
| AI_NUMERICHOST
;
176 #if defined(HAVE_IPV6)
177 if (len
< sizeof(addr
)) {
180 p
= normalize_ipv6_literal(str
, addr
, &len
);
182 hints
.ai_family
= AF_INET6
;
187 if (strchr_m(str
, ':')) {
188 char *p
= strchr_m(str
, '%');
191 * Cope with link-local.
192 * This is IP:v6:addr%ifname.
195 if (p
&& (p
> str
) && ((scope_id
= if_nametoindex(p
+1)) != 0)) {
196 /* Length of string we want to copy.
197 This is IP:v6:addr (removing the %ifname).
199 len
= PTR_DIFF(p
,str
);
201 if (len
+1 > sizeof(addr
)) {
202 /* string+nul too long for array. */
206 memcpy(addr
, str
, len
);
215 ret
= getaddrinfo(str
, NULL
, &hints
, ppres
);
217 #if defined(HAVE_IPV6)
218 struct sockaddr_in6
*ps6
= NULL
;
226 if ((*ppres
) == NULL
) {
229 if ((*ppres
)->ai_addr
->sa_family
!= AF_INET6
) {
233 ps6
= (struct sockaddr_in6
*)(*ppres
)->ai_addr
;
235 if (IN6_IS_ADDR_LINKLOCAL(&ps6
->sin6_addr
) &&
236 ps6
->sin6_scope_id
== 0) {
237 ps6
->sin6_scope_id
= scope_id
;
244 hints
.ai_flags
= flags
;
246 /* Linux man page on getaddrinfo() says port will be
247 uninitialized when service string is NULL */
249 ret
= getaddrinfo(str
, NULL
,
254 DEBUG(3, ("interpret_string_addr_internal: "
255 "getaddrinfo failed for name %s (flags %d) [%s]\n",
256 str
, flags
, gai_strerror(ret
)));
262 /*******************************************************************
263 Map a text hostname or IP address (IPv4 or IPv6) into a
264 struct sockaddr_storage. Takes a flag which allows it to
265 prefer an IPv4 address (needed for DC's).
266 ******************************************************************/
268 static bool interpret_string_addr_pref(struct sockaddr_storage
*pss
,
273 struct addrinfo
*res
= NULL
;
278 if (flags
& AI_NUMERICHOST
) {
281 int_flags
= flags
|AI_ADDRCONFIG
;
284 if (!interpret_string_addr_internal(&res
, str
, int_flags
)) {
294 for (p
= res
; p
; p
= p
->ai_next
) {
295 if (p
->ai_family
== AF_INET
) {
296 memcpy(pss
, p
->ai_addr
, p
->ai_addrlen
);
301 /* Copy the first sockaddr. */
302 memcpy(pss
, res
->ai_addr
, res
->ai_addrlen
);
305 /* Copy the first sockaddr. */
306 memcpy(pss
, res
->ai_addr
, res
->ai_addrlen
);
313 /*******************************************************************
314 Map a text hostname or IP address (IPv4 or IPv6) into a
315 struct sockaddr_storage. Address agnostic version.
316 ******************************************************************/
318 bool interpret_string_addr(struct sockaddr_storage
*pss
,
322 return interpret_string_addr_pref(pss
,
328 /*******************************************************************
329 Map a text hostname or IP address (IPv4 or IPv6) into a
330 struct sockaddr_storage. Version that prefers IPv4.
331 ******************************************************************/
333 bool interpret_string_addr_prefer_ipv4(struct sockaddr_storage
*pss
,
337 return interpret_string_addr_pref(pss
,
344 * Interpret an internet address or name into an IP address in 4 byte form.
345 * RETURNS IN NETWORK BYTE ORDER (big endian).
348 uint32_t interpret_addr(const char *str
)
352 /* If it's in the form of an IP address then
353 * get the lib to interpret it */
354 if (is_ipaddress_v4(str
)) {
357 if (inet_pton(AF_INET
, str
, &dest
) <= 0) {
358 /* Error - this shouldn't happen ! */
359 DEBUG(0,("interpret_addr: inet_pton failed "
364 ret
= dest
.s_addr
; /* NETWORK BYTE ORDER ! */
366 /* Otherwise assume it's a network name of some sort and use
368 struct addrinfo
*res
= NULL
;
369 struct addrinfo
*res_list
= NULL
;
370 if (!interpret_string_addr_internal(&res_list
,
373 DEBUG(3,("interpret_addr: Unknown host. %s\n",str
));
377 /* Find the first IPv4 address. */
378 for (res
= res_list
; res
; res
= res
->ai_next
) {
379 if (res
->ai_family
!= AF_INET
) {
382 if (res
->ai_addr
== NULL
) {
388 DEBUG(3,("interpret_addr: host address is "
389 "invalid for host %s\n",str
));
391 freeaddrinfo(res_list
);
396 &((struct sockaddr_in
*)res
->ai_addr
)->sin_addr
.s_addr
,
399 freeaddrinfo(res_list
);
403 /* This is so bogus - all callers need fixing... JRA. */
404 if (ret
== (uint32_t)-1) {
412 A convenient addition to interpret_addr().
414 _PUBLIC_
struct in_addr
interpret_addr2(const char *str
)
417 uint32_t a
= interpret_addr(str
);
423 Check if an IP is the 0.0.0.0.
426 _PUBLIC_
bool is_zero_ip_v4(struct in_addr ip
)
428 return ip
.s_addr
== 0;
432 Are two IPs on the same subnet?
435 _PUBLIC_
bool same_net_v4(struct in_addr ip1
, struct in_addr ip2
, struct in_addr mask
)
437 uint32_t net1
,net2
,nmask
;
439 nmask
= ntohl(mask
.s_addr
);
440 net1
= ntohl(ip1
.s_addr
);
441 net2
= ntohl(ip2
.s_addr
);
443 return((net1
& nmask
) == (net2
& nmask
));
447 * Return true if a string could be an IPv4 address.
450 bool is_ipaddress_v4(const char *str
)
455 ret
= inet_pton(AF_INET
, str
, &dest
);
462 bool is_ipv6_literal(const char *str
)
464 #if defined(HAVE_IPV6)
465 char buf
[INET6_ADDRSTRLEN
*2] = { 0, };
466 size_t len
= strlen(str
);
469 if (len
>= sizeof(buf
)) {
473 p
= normalize_ipv6_literal(str
, buf
, &len
);
485 * Return true if a string could be a IPv6 address.
488 bool is_ipaddress_v6(const char *str
)
490 #if defined(HAVE_IPV6)
494 p
= strchr_m(str
, ':');
496 return is_ipv6_literal(str
);
498 char buf
[INET6_ADDRSTRLEN
] = { 0, };
500 const char *addr
= str
;
501 const char *idxs
= NULL
;
502 unsigned int idx
= 0;
505 p
= strchr_m(str
, '%');
506 if (p
&& (p
> str
)) {
507 len
= PTR_DIFF(p
, str
);
513 if (len
>= sizeof(buf
)) {
517 strncpy(buf
, str
, len
);
522 * Cope with link-local.
523 * This is IP:v6:addr%ifidx.
528 ret
= sscanf(idxs
, "%5u%c", &idx
, &c
);
533 if (idx
> 0 && idx
< UINT16_MAX
) {
540 * Cope with link-local.
541 * This is IP:v6:addr%ifname.
544 idx
= if_nametoindex(idxs
);
556 ret
= inet_pton(AF_INET6
, addr
, &ip6
);
568 * Return true if a string could be an IPv4 or IPv6 address.
571 bool is_ipaddress(const char *str
)
573 return is_ipaddress_v4(str
) || is_ipaddress_v6(str
);
577 * Is a sockaddr a broadcast address ?
580 bool is_broadcast_addr(const struct sockaddr
*pss
)
582 #if defined(HAVE_IPV6)
583 if (pss
->sa_family
== AF_INET6
) {
584 const struct in6_addr
*sin6
=
585 &((const struct sockaddr_in6
*)pss
)->sin6_addr
;
586 return IN6_IS_ADDR_MULTICAST(sin6
);
589 if (pss
->sa_family
== AF_INET
) {
591 ntohl(((const struct sockaddr_in
*)pss
)->sin_addr
.s_addr
);
592 return addr
== INADDR_BROADCAST
;
598 * Check if an IPv7 is 127.0.0.1
600 bool is_loopback_ip_v4(struct in_addr ip
)
603 a
.s_addr
= htonl(INADDR_LOOPBACK
);
604 return(ip
.s_addr
== a
.s_addr
);
608 * Check if a struct sockaddr is the loopback address.
610 bool is_loopback_addr(const struct sockaddr
*pss
)
612 #if defined(HAVE_IPV6)
613 if (pss
->sa_family
== AF_INET6
) {
614 const struct in6_addr
*pin6
=
615 &((const struct sockaddr_in6
*)pss
)->sin6_addr
;
616 return IN6_IS_ADDR_LOOPBACK(pin6
);
619 if (pss
->sa_family
== AF_INET
) {
620 const struct in_addr
*pin
= &((const struct sockaddr_in
*)pss
)->sin_addr
;
621 return is_loopback_ip_v4(*pin
);
627 * Check if a struct sockaddr has an unspecified address.
629 bool is_zero_addr(const struct sockaddr_storage
*pss
)
631 #if defined(HAVE_IPV6)
632 if (pss
->ss_family
== AF_INET6
) {
633 const struct in6_addr
*pin6
=
634 &((const struct sockaddr_in6
*)pss
)->sin6_addr
;
635 return IN6_IS_ADDR_UNSPECIFIED(pin6
);
638 if (pss
->ss_family
== AF_INET
) {
639 const struct in_addr
*pin
= &((const struct sockaddr_in
*)pss
)->sin_addr
;
640 return is_zero_ip_v4(*pin
);
646 * Set an IP to 0.0.0.0.
648 void zero_ip_v4(struct in_addr
*ip
)
653 bool is_linklocal_addr(const struct sockaddr_storage
*pss
)
656 if (pss
->ss_family
== AF_INET6
) {
657 const struct in6_addr
*pin6
=
658 &((const struct sockaddr_in6
*)pss
)->sin6_addr
;
659 return IN6_IS_ADDR_LINKLOCAL(pin6
);
662 if (pss
->ss_family
== AF_INET
) {
663 const struct in_addr
*pin
=
664 &((const struct sockaddr_in
*)pss
)->sin_addr
;
665 struct in_addr ll_addr
;
666 struct in_addr mask_addr
;
668 /* 169.254.0.0/16, is link local, see RFC 3927 */
669 ll_addr
.s_addr
= 0xa9fe0000;
670 mask_addr
.s_addr
= 0xffff0000;
671 return same_net_v4(*pin
, ll_addr
, mask_addr
);
677 * Convert an IPv4 struct in_addr to a struct sockaddr_storage.
679 void in_addr_to_sockaddr_storage(struct sockaddr_storage
*ss
,
682 struct sockaddr_in
*sa
= (struct sockaddr_in
*)ss
;
684 sa
->sin_family
= AF_INET
;
688 #if defined(HAVE_IPV6)
690 * Convert an IPv6 struct in_addr to a struct sockaddr_storage.
692 void in6_addr_to_sockaddr_storage(struct sockaddr_storage
*ss
,
695 struct sockaddr_in6
*sa
= (struct sockaddr_in6
*)ss
;
696 memset(ss
, '\0', sizeof(*ss
));
697 sa
->sin6_family
= AF_INET6
;
703 * Are two IPs on the same subnet?
705 bool same_net(const struct sockaddr
*ip1
,
706 const struct sockaddr
*ip2
,
707 const struct sockaddr
*mask
)
709 if (ip1
->sa_family
!= ip2
->sa_family
) {
710 /* Never on the same net. */
714 #if defined(HAVE_IPV6)
715 if (ip1
->sa_family
== AF_INET6
) {
716 struct sockaddr_in6 ip1_6
= *(const struct sockaddr_in6
*)ip1
;
717 struct sockaddr_in6 ip2_6
= *(const struct sockaddr_in6
*)ip2
;
718 struct sockaddr_in6 mask_6
= *(const struct sockaddr_in6
*)mask
;
719 char *p1
= (char *)&ip1_6
.sin6_addr
;
720 char *p2
= (char *)&ip2_6
.sin6_addr
;
721 char *m
= (char *)&mask_6
.sin6_addr
;
724 for (i
= 0; i
< sizeof(struct in6_addr
); i
++) {
729 return (memcmp(&ip1_6
.sin6_addr
,
731 sizeof(struct in6_addr
)) == 0);
734 if (ip1
->sa_family
== AF_INET
) {
735 return same_net_v4(((const struct sockaddr_in
*)ip1
)->sin_addr
,
736 ((const struct sockaddr_in
*)ip2
)->sin_addr
,
737 ((const struct sockaddr_in
*)mask
)->sin_addr
);
743 * Are two sockaddr 's the same family and address ? Ignore port etc.
746 bool sockaddr_equal(const struct sockaddr
*ip1
,
747 const struct sockaddr
*ip2
)
749 if (ip1
->sa_family
!= ip2
->sa_family
) {
750 /* Never the same. */
754 #if defined(HAVE_IPV6)
755 if (ip1
->sa_family
== AF_INET6
) {
756 return (memcmp(&((const struct sockaddr_in6
*)ip1
)->sin6_addr
,
757 &((const struct sockaddr_in6
*)ip2
)->sin6_addr
,
758 sizeof(struct in6_addr
)) == 0);
761 if (ip1
->sa_family
== AF_INET
) {
762 return (memcmp(&((const struct sockaddr_in
*)ip1
)->sin_addr
,
763 &((const struct sockaddr_in
*)ip2
)->sin_addr
,
764 sizeof(struct in_addr
)) == 0);
770 * Is an IP address the INADDR_ANY or in6addr_any value ?
772 bool is_address_any(const struct sockaddr
*psa
)
774 #if defined(HAVE_IPV6)
775 if (psa
->sa_family
== AF_INET6
) {
776 const struct sockaddr_in6
*si6
= (const struct sockaddr_in6
*)psa
;
777 if (memcmp(&in6addr_any
,
779 sizeof(in6addr_any
)) == 0) {
785 if (psa
->sa_family
== AF_INET
) {
786 const struct sockaddr_in
*si
= (const struct sockaddr_in
*)psa
;
787 if (si
->sin_addr
.s_addr
== INADDR_ANY
) {
795 void set_sockaddr_port(struct sockaddr
*psa
, uint16_t port
)
797 #if defined(HAVE_IPV6)
798 if (psa
->sa_family
== AF_INET6
) {
799 ((struct sockaddr_in6
*)psa
)->sin6_port
= htons(port
);
802 if (psa
->sa_family
== AF_INET
) {
803 ((struct sockaddr_in
*)psa
)->sin_port
= htons(port
);
808 /****************************************************************************
809 Get a port number in host byte order from a sockaddr_storage.
810 ****************************************************************************/
812 uint16_t get_sockaddr_port(const struct sockaddr_storage
*pss
)
816 if (pss
->ss_family
!= AF_INET
) {
817 #if defined(HAVE_IPV6)
819 const struct sockaddr_in6
*sa6
=
820 (const struct sockaddr_in6
*)pss
;
821 port
= ntohs(sa6
->sin6_port
);
824 const struct sockaddr_in
*sa
=
825 (const struct sockaddr_in
*)pss
;
826 port
= ntohs(sa
->sin_port
);
831 /****************************************************************************
832 Print out an IPv4 or IPv6 address from a struct sockaddr_storage.
833 ****************************************************************************/
835 char *print_sockaddr_len(char *dest
,
837 const struct sockaddr
*psa
,
843 (void)sys_getnameinfo(psa
,
851 /****************************************************************************
852 Print out an IPv4 or IPv6 address from a struct sockaddr_storage.
853 ****************************************************************************/
855 char *print_sockaddr(char *dest
,
857 const struct sockaddr_storage
*psa
)
859 return print_sockaddr_len(dest
, destlen
, (const struct sockaddr
*)psa
,
860 sizeof(struct sockaddr_storage
));
863 /****************************************************************************
864 Print out a canonical IPv4 or IPv6 address from a struct sockaddr_storage.
865 ****************************************************************************/
867 char *print_canonical_sockaddr(TALLOC_CTX
*ctx
,
868 const struct sockaddr_storage
*pss
)
870 char addr
[INET6_ADDRSTRLEN
];
874 /* Linux getnameinfo() man pages says port is unitialized if
875 service name is NULL. */
877 ret
= sys_getnameinfo((const struct sockaddr
*)pss
,
878 sizeof(struct sockaddr_storage
),
886 if (pss
->ss_family
!= AF_INET
) {
887 #if defined(HAVE_IPV6)
888 dest
= talloc_asprintf(ctx
, "[%s]", addr
);
893 dest
= talloc_asprintf(ctx
, "%s", addr
);
899 /****************************************************************************
900 Return the port number we've bound to on a socket.
901 ****************************************************************************/
903 int get_socket_port(int fd
)
905 struct sockaddr_storage sa
;
906 socklen_t length
= sizeof(sa
);
912 if (getsockname(fd
, (struct sockaddr
*)&sa
, &length
) < 0) {
913 int level
= (errno
== ENOTCONN
) ? 2 : 0;
914 DEBUG(level
, ("getsockname failed. Error was %s\n",
919 #if defined(HAVE_IPV6)
920 if (sa
.ss_family
== AF_INET6
) {
921 struct sockaddr_in6
*sa_in6
= (struct sockaddr_in6
*)&sa
;
922 return ntohs(sa_in6
->sin6_port
);
925 if (sa
.ss_family
== AF_INET
) {
926 struct sockaddr_in
*sa_in
= (struct sockaddr_in
*)&sa
;
927 return ntohs(sa_in
->sin_port
);
932 /****************************************************************************
933 Return the string of an IP address (IPv4 or IPv6).
934 ****************************************************************************/
936 static const char *get_socket_addr(int fd
, char *addr_buf
, size_t addr_len
)
938 struct sockaddr_storage sa
;
939 socklen_t length
= sizeof(sa
);
941 /* Ok, returning a hard coded IPv4 address
942 * is bogus, but it's just as bogus as a
943 * zero IPv6 address. No good choice here.
946 if (strlcpy(addr_buf
, "0.0.0.0", addr_len
) >= addr_len
) {
955 if (getsockname(fd
, (struct sockaddr
*)&sa
, &length
) < 0) {
956 DEBUG(0,("getsockname failed. Error was %s\n",
961 return print_sockaddr_len(addr_buf
, addr_len
, (struct sockaddr
*)&sa
, length
);
964 const char *client_socket_addr(int fd
, char *addr
, size_t addr_len
)
966 return get_socket_addr(fd
, addr
, addr_len
);
970 enum SOCK_OPT_TYPES
{OPT_BOOL
,OPT_INT
,OPT_ON
};
972 typedef struct smb_socket_option
{
980 static const smb_socket_option socket_options
[] = {
981 {"SO_KEEPALIVE", SOL_SOCKET
, SO_KEEPALIVE
, 0, OPT_BOOL
},
982 {"SO_REUSEADDR", SOL_SOCKET
, SO_REUSEADDR
, 0, OPT_BOOL
},
983 {"SO_BROADCAST", SOL_SOCKET
, SO_BROADCAST
, 0, OPT_BOOL
},
985 {"TCP_NODELAY", IPPROTO_TCP
, TCP_NODELAY
, 0, OPT_BOOL
},
988 {"TCP_KEEPCNT", IPPROTO_TCP
, TCP_KEEPCNT
, 0, OPT_INT
},
991 {"TCP_KEEPIDLE", IPPROTO_TCP
, TCP_KEEPIDLE
, 0, OPT_INT
},
994 {"TCP_KEEPINTVL", IPPROTO_TCP
, TCP_KEEPINTVL
, 0, OPT_INT
},
996 #ifdef IPTOS_LOWDELAY
997 {"IPTOS_LOWDELAY", IPPROTO_IP
, IP_TOS
, IPTOS_LOWDELAY
, OPT_ON
},
999 #ifdef IPTOS_THROUGHPUT
1000 {"IPTOS_THROUGHPUT", IPPROTO_IP
, IP_TOS
, IPTOS_THROUGHPUT
, OPT_ON
},
1003 {"SO_REUSEPORT", SOL_SOCKET
, SO_REUSEPORT
, 0, OPT_BOOL
},
1006 {"SO_SNDBUF", SOL_SOCKET
, SO_SNDBUF
, 0, OPT_INT
},
1009 {"SO_RCVBUF", SOL_SOCKET
, SO_RCVBUF
, 0, OPT_INT
},
1012 {"SO_SNDLOWAT", SOL_SOCKET
, SO_SNDLOWAT
, 0, OPT_INT
},
1015 {"SO_RCVLOWAT", SOL_SOCKET
, SO_RCVLOWAT
, 0, OPT_INT
},
1018 {"SO_SNDTIMEO", SOL_SOCKET
, SO_SNDTIMEO
, 0, OPT_INT
},
1021 {"SO_RCVTIMEO", SOL_SOCKET
, SO_RCVTIMEO
, 0, OPT_INT
},
1024 {"TCP_FASTACK", IPPROTO_TCP
, TCP_FASTACK
, 0, OPT_INT
},
1027 {"TCP_QUICKACK", IPPROTO_TCP
, TCP_QUICKACK
, 0, OPT_BOOL
},
1029 #ifdef TCP_NODELAYACK
1030 {"TCP_NODELAYACK", IPPROTO_TCP
, TCP_NODELAYACK
, 0, OPT_BOOL
},
1032 #ifdef TCP_KEEPALIVE_THRESHOLD
1033 {"TCP_KEEPALIVE_THRESHOLD", IPPROTO_TCP
, TCP_KEEPALIVE_THRESHOLD
, 0, OPT_INT
},
1035 #ifdef TCP_KEEPALIVE_ABORT_THRESHOLD
1036 {"TCP_KEEPALIVE_ABORT_THRESHOLD", IPPROTO_TCP
, TCP_KEEPALIVE_ABORT_THRESHOLD
, 0, OPT_INT
},
1038 #ifdef TCP_DEFER_ACCEPT
1039 {"TCP_DEFER_ACCEPT", IPPROTO_TCP
, TCP_DEFER_ACCEPT
, 0, OPT_INT
},
1043 /****************************************************************************
1044 Print socket options.
1045 ****************************************************************************/
1047 static void print_socket_options(int s
)
1051 const smb_socket_option
*p
= &socket_options
[0];
1053 /* wrapped in if statement to prevent streams
1054 * leak in SCO Openserver 5.0 */
1055 /* reported on samba-technical --jerry */
1056 if ( DEBUGLEVEL
>= 5 ) {
1057 DEBUG(5,("Socket options:\n"));
1058 for (; p
->name
!= NULL
; p
++) {
1059 if (getsockopt(s
, p
->level
, p
->option
,
1060 (void *)&value
, &vlen
) == -1) {
1061 DEBUGADD(5,("\tCould not test socket option %s.\n",
1064 DEBUGADD(5,("\t%s = %d\n",
1071 /****************************************************************************
1072 Set user socket options.
1073 ****************************************************************************/
1075 void set_socket_options(int fd
, const char *options
)
1077 TALLOC_CTX
*ctx
= talloc_new(NULL
);
1080 while (next_token_talloc(ctx
, &options
, &tok
," \t,")) {
1084 bool got_value
= false;
1086 if ((p
= strchr_m(tok
,'='))) {
1092 for (i
=0;socket_options
[i
].name
;i
++)
1093 if (strequal(socket_options
[i
].name
,tok
))
1096 if (!socket_options
[i
].name
) {
1097 DEBUG(0,("Unknown socket option %s\n",tok
));
1101 switch (socket_options
[i
].opttype
) {
1104 ret
= setsockopt(fd
,socket_options
[i
].level
,
1105 socket_options
[i
].option
,
1106 (char *)&value
,sizeof(int));
1111 DEBUG(0,("syntax error - %s "
1112 "does not take a value\n",tok
));
1115 int on
= socket_options
[i
].value
;
1116 ret
= setsockopt(fd
,socket_options
[i
].level
,
1117 socket_options
[i
].option
,
1118 (char *)&on
,sizeof(int));
1124 /* be aware that some systems like Solaris return
1125 * EINVAL to a setsockopt() call when the client
1126 * sent a RST previously - no need to worry */
1127 DEBUG(2,("Failed to set socket option %s (Error %s)\n",
1128 tok
, strerror(errno
) ));
1133 print_socket_options(fd
);