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 "system/select.h"
31 #include "lib/util/select.h"
32 #include "lib/util/util_net.h"
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;
55 size_t cnt_delimiter
= 0;
62 /* ignore a trailing '.' */
63 if (str
[len
- 1] == '.') {
68 if (len
>= INET6_ADDRSTRLEN
) {
75 cmp
= strncasecmp(&str
[len
], IPv6_LITERAL_NET
, llen
);
80 for (i
= 0; i
< len
; i
++) {
92 buf
[i
] = SCOPE_DELIMITER
;
126 if (cnt_delimiter
> 7) {
131 if (cnt_delimiter
< 2) {
135 for (; idx_chars
!= 0 && i
< len
; i
++) {
137 case SCOPE_DELIMITER
:
147 if (idx_chars
== 1) {
157 * Wrap getaddrinfo...
159 bool interpret_string_addr_internal(struct addrinfo
**ppres
,
160 const char *str
, int flags
)
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
);
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
)) {
183 p
= normalize_ipv6_literal(str
, addr
, &len
);
185 hints
.ai_family
= AF_INET6
;
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. */
209 memcpy(addr
, str
, len
);
218 ret
= getaddrinfo(str
, NULL
, &hints
, ppres
);
220 #if defined(HAVE_IPV6)
221 struct sockaddr_in6
*ps6
= NULL
;
229 if ((*ppres
) == NULL
) {
232 if ((*ppres
)->ai_addr
->sa_family
!= AF_INET6
) {
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
;
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
,
257 DEBUG(3, ("interpret_string_addr_internal: "
258 "getaddrinfo failed for name %s (flags %d) [%s]\n",
259 str
, flags
, gai_strerror(ret
)));
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
,
276 struct addrinfo
*res
= NULL
;
281 if (flags
& AI_NUMERICHOST
) {
284 int_flags
= flags
|AI_ADDRCONFIG
;
287 if (!interpret_string_addr_internal(&res
, str
, int_flags
)) {
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
);
304 /* Copy the first sockaddr. */
305 memcpy(pss
, res
->ai_addr
, res
->ai_addrlen
);
308 /* Copy the first sockaddr. */
309 memcpy(pss
, res
->ai_addr
, res
->ai_addrlen
);
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
,
325 return interpret_string_addr_pref(pss
,
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
,
340 return interpret_string_addr_pref(pss
,
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
)
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
)) {
360 if (inet_pton(AF_INET
, str
, &dest
) <= 0) {
361 /* Error - this shouldn't happen ! */
362 DEBUG(0,("interpret_addr: inet_pton failed "
367 ret
= dest
.s_addr
; /* NETWORK BYTE ORDER ! */
369 /* Otherwise assume it's a network name of some sort and use
371 struct addrinfo
*res
= NULL
;
372 struct addrinfo
*res_list
= NULL
;
373 if (!interpret_string_addr_internal(&res_list
,
376 DEBUG(3,("interpret_addr: Unknown host. %s\n",str
));
380 /* Find the first IPv4 address. */
381 for (res
= res_list
; res
; res
= res
->ai_next
) {
382 if (res
->ai_family
!= AF_INET
) {
385 if (res
->ai_addr
== NULL
) {
391 DEBUG(3,("interpret_addr: host address is "
392 "invalid for host %s\n",str
));
394 freeaddrinfo(res_list
);
399 &((struct sockaddr_in
*)res
->ai_addr
)->sin_addr
.s_addr
,
402 freeaddrinfo(res_list
);
406 /* This is so bogus - all callers need fixing... JRA. */
407 if (ret
== (uint32_t)-1) {
415 A convenient addition to interpret_addr().
417 _PUBLIC_
struct in_addr
interpret_addr2(const char *str
)
420 uint32_t a
= interpret_addr(str
);
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
)
458 ret
= inet_pton(AF_INET
, str
, &dest
);
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
);
472 if (len
>= sizeof(buf
)) {
476 p
= normalize_ipv6_literal(str
, buf
, &len
);
488 * Return true if a string could be a IPv6 address.
491 bool is_ipaddress_v6(const char *str
)
493 #if defined(HAVE_IPV6)
496 char buf
[INET6_ADDRSTRLEN
] = { 0, };
498 const char *addr
= str
;
499 const char *idxs
= NULL
;
500 unsigned int idx
= 0;
503 p
= strchr_m(str
, ':');
505 return is_ipv6_literal(str
);
508 p
= strchr_m(str
, SCOPE_DELIMITER
);
509 if (p
&& (p
> str
)) {
510 len
= PTR_DIFF(p
, str
);
516 if (len
>= sizeof(buf
)) {
520 strncpy(buf
, str
, len
);
525 * Cope with link-local.
526 * This is IP:v6:addr%ifidx.
531 ret
= sscanf(idxs
, "%5u%c", &idx
, &c
);
536 if (idx
> 0 && idx
< UINT16_MAX
) {
543 * Cope with link-local.
544 * This is IP:v6:addr%ifname.
547 idx
= if_nametoindex(idxs
);
559 ret
= inet_pton(AF_INET6
, addr
, &ip6
);
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
);
592 if (pss
->sa_family
== AF_INET
) {
594 ntohl(((const struct sockaddr_in
*)pss
)->sin_addr
.s_addr
);
595 return addr
== INADDR_BROADCAST
;
601 * Check if an IPv7 is 127.0.0.1
603 bool is_loopback_ip_v4(struct in_addr ip
)
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
);
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
);
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
);
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
) {
652 * Set an IP to 0.0.0.0.
654 void zero_ip_v4(struct in_addr
*ip
)
659 bool is_linklocal_addr(const struct sockaddr_storage
*pss
)
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
);
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
);
683 * Convert an IPv4 struct in_addr to a struct sockaddr_storage.
685 void in_addr_to_sockaddr_storage(struct sockaddr_storage
*ss
,
688 struct sockaddr_in
*sa
= (struct sockaddr_in
*)ss
;
690 sa
->sin_family
= AF_INET
;
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
,
701 struct sockaddr_in6
*sa
= (struct sockaddr_in6
*)ss
;
702 memset(ss
, '\0', sizeof(*ss
));
703 sa
->sin6_family
= AF_INET6
;
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. */
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
;
730 for (i
= 0; i
< sizeof(struct in6_addr
); i
++) {
735 return (memcmp(&ip1_6
.sin6_addr
,
737 sizeof(struct in6_addr
)) == 0);
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
);
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. */
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);
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);
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
,
785 sizeof(in6addr_any
)) == 0) {
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
) {
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
);
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
)
822 if (pss
->ss_family
!= AF_INET
) {
823 #if defined(HAVE_IPV6)
825 const struct sockaddr_in6
*sa6
=
826 (const struct sockaddr_in6
*)pss
;
827 port
= ntohs(sa6
->sin6_port
);
830 const struct sockaddr_in
*sa
=
831 (const struct sockaddr_in
*)pss
;
832 port
= ntohs(sa
->sin_port
);
837 /****************************************************************************
838 Print out an IPv4 or IPv6 address from a struct sockaddr_storage.
839 ****************************************************************************/
841 char *print_sockaddr_len(char *dest
,
843 const struct sockaddr
*psa
,
849 (void)sys_getnameinfo(psa
,
857 /****************************************************************************
858 Print out an IPv4 or IPv6 address from a struct sockaddr_storage.
859 ****************************************************************************/
861 char *print_sockaddr(char *dest
,
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
];
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
),
892 if (pss
->ss_family
!= AF_INET
) {
893 #if defined(HAVE_IPV6)
894 dest
= talloc_asprintf(ctx
, "[%s]", addr
);
899 dest
= talloc_asprintf(ctx
, "%s", addr
);
905 enum SOCK_OPT_TYPES
{OPT_BOOL
,OPT_INT
,OPT_ON
};
907 typedef struct 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
},
920 {"TCP_NODELAY", IPPROTO_TCP
, TCP_NODELAY
, 0, OPT_BOOL
},
923 {"TCP_KEEPCNT", IPPROTO_TCP
, TCP_KEEPCNT
, 0, OPT_INT
},
926 {"TCP_KEEPIDLE", IPPROTO_TCP
, TCP_KEEPIDLE
, 0, OPT_INT
},
929 {"TCP_KEEPINTVL", IPPROTO_TCP
, TCP_KEEPINTVL
, 0, OPT_INT
},
931 #ifdef IPTOS_LOWDELAY
932 {"IPTOS_LOWDELAY", IPPROTO_IP
, IP_TOS
, IPTOS_LOWDELAY
, OPT_ON
},
934 #ifdef IPTOS_THROUGHPUT
935 {"IPTOS_THROUGHPUT", IPPROTO_IP
, IP_TOS
, IPTOS_THROUGHPUT
, OPT_ON
},
938 {"SO_REUSEPORT", SOL_SOCKET
, SO_REUSEPORT
, 0, OPT_BOOL
},
941 {"SO_SNDBUF", SOL_SOCKET
, SO_SNDBUF
, 0, OPT_INT
},
944 {"SO_RCVBUF", SOL_SOCKET
, SO_RCVBUF
, 0, OPT_INT
},
947 {"SO_SNDLOWAT", SOL_SOCKET
, SO_SNDLOWAT
, 0, OPT_INT
},
950 {"SO_RCVLOWAT", SOL_SOCKET
, SO_RCVLOWAT
, 0, OPT_INT
},
953 {"SO_SNDTIMEO", SOL_SOCKET
, SO_SNDTIMEO
, 0, OPT_INT
},
956 {"SO_RCVTIMEO", SOL_SOCKET
, SO_RCVTIMEO
, 0, OPT_INT
},
959 {"TCP_FASTACK", IPPROTO_TCP
, TCP_FASTACK
, 0, OPT_INT
},
962 {"TCP_QUICKACK", IPPROTO_TCP
, TCP_QUICKACK
, 0, OPT_BOOL
},
964 #ifdef TCP_NODELAYACK
965 {"TCP_NODELAYACK", IPPROTO_TCP
, TCP_NODELAYACK
, 0, OPT_BOOL
},
967 #ifdef TCP_KEEPALIVE_THRESHOLD
968 {"TCP_KEEPALIVE_THRESHOLD", IPPROTO_TCP
, TCP_KEEPALIVE_THRESHOLD
, 0, OPT_INT
},
970 #ifdef TCP_KEEPALIVE_ABORT_THRESHOLD
971 {"TCP_KEEPALIVE_ABORT_THRESHOLD", IPPROTO_TCP
, TCP_KEEPALIVE_ABORT_THRESHOLD
, 0, OPT_INT
},
973 #ifdef TCP_DEFER_ACCEPT
974 {"TCP_DEFER_ACCEPT", IPPROTO_TCP
, TCP_DEFER_ACCEPT
, 0, OPT_INT
},
976 #ifdef TCP_USER_TIMEOUT
977 {"TCP_USER_TIMEOUT", IPPROTO_TCP
, TCP_USER_TIMEOUT
, 0, OPT_INT
},
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];
990 if (DEBUGLEVEL
< 5) {
994 str
= talloc_strdup(ctx
, "");
996 DBG_WARNING("talloc failed\n");
1000 for (; p
->name
!= NULL
; p
++) {
1002 socklen_t vlen
= sizeof(val
);
1004 ret
= getsockopt(s
, p
->level
, p
->option
, (void *)&val
, &vlen
);
1006 DBG_INFO("Could not test socket option %s: %s.\n",
1007 p
->name
, strerror(errno
));
1011 talloc_asprintf_addbuf(
1014 str
[0] != '\0' ? ", " : "",
1019 DEBUG(5, ("socket options: %s\n", 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
);
1033 while (next_token_talloc(ctx
, &options
, &tok
," \t,")) {
1037 bool got_value
= false;
1039 if ((p
= strchr_m(tok
,'='))) {
1045 for (i
=0;socket_options
[i
].name
;i
++)
1046 if (strequal(socket_options
[i
].name
,tok
))
1049 if (!socket_options
[i
].name
) {
1050 DEBUG(0,("Unknown socket option %s\n",tok
));
1054 switch (socket_options
[i
].opttype
) {
1057 ret
= setsockopt(fd
,socket_options
[i
].level
,
1058 socket_options
[i
].option
,
1059 (char *)&value
,sizeof(int));
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));
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
);
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
)
1100 switch (ss
->ss_family
) {
1102 sa
->sa_socklen
= sizeof(struct sockaddr_in
);
1106 sa
->sa_socklen
= sizeof(struct sockaddr_in6
);
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
);
1122 if (sa
->u
.sa
.sa_family
== AF_INET6
) {
1123 sa
->u
.in6
.sin6_port
= htons(port
);
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
);
1137 if (sa
->u
.sa
.sa_family
== AF_INET6
) {
1138 *port
= ntohs(sa
->u
.in6
.sin6_port
);
1145 int samba_socket_poll_error(int fd
)
1147 struct pollfd pfd
= {
1150 .events
= POLLRDHUP
, /* POLLERR and POLLHUP are not needed */
1156 ret
= sys_poll_intr(&pfd
, 1, 0);
1164 if (pfd
.revents
& POLLERR
) {
1167 if (pfd
.revents
& POLLHUP
) {
1171 if (pfd
.revents
& POLLRDHUP
) {
1176 /* should never be reached! */
1180 int samba_socket_sock_error(int fd
)
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
);
1201 int samba_socket_poll_or_sock_error(int fd
)
1206 poll_error
= samba_socket_poll_error(fd
);
1207 if (poll_error
== 0) {
1212 if (poll_error
== POLLRDHUP
) {
1218 if (poll_error
== POLLHUP
) {
1224 * POLLERR and POLLNVAL fallback to
1225 * getsockopt(fd, SOL_SOCKET, SO_ERROR)
1226 * and force EPIPE as fallback.
1230 ret
= samba_socket_sock_error(fd
);