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"
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;
53 size_t cnt_delimiter
= 0;
60 /* ignore a trailing '.' */
61 if (str
[len
- 1] == '.') {
66 if (len
>= INET6_ADDRSTRLEN
) {
73 cmp
= strncasecmp(&str
[len
], IPv6_LITERAL_NET
, llen
);
78 for (i
= 0; i
< len
; i
++) {
90 buf
[i
] = SCOPE_DELIMITER
;
124 if (cnt_delimiter
> 7) {
129 if (cnt_delimiter
< 2) {
133 for (; idx_chars
!= 0 && i
< len
; i
++) {
135 case SCOPE_DELIMITER
:
145 if (idx_chars
== 1) {
155 * Wrap getaddrinfo...
157 bool interpret_string_addr_internal(struct addrinfo
**ppres
,
158 const char *str
, int flags
)
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
);
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
)) {
181 p
= normalize_ipv6_literal(str
, addr
, &len
);
183 hints
.ai_family
= AF_INET6
;
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. */
207 memcpy(addr
, str
, len
);
216 ret
= getaddrinfo(str
, NULL
, &hints
, ppres
);
218 #if defined(HAVE_IPV6)
219 struct sockaddr_in6
*ps6
= NULL
;
227 if ((*ppres
) == NULL
) {
230 if ((*ppres
)->ai_addr
->sa_family
!= AF_INET6
) {
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
;
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
,
255 DEBUG(3, ("interpret_string_addr_internal: "
256 "getaddrinfo failed for name %s (flags %d) [%s]\n",
257 str
, flags
, gai_strerror(ret
)));
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
,
274 struct addrinfo
*res
= NULL
;
279 if (flags
& AI_NUMERICHOST
) {
282 int_flags
= flags
|AI_ADDRCONFIG
;
285 if (!interpret_string_addr_internal(&res
, str
, int_flags
)) {
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
);
302 /* Copy the first sockaddr. */
303 memcpy(pss
, res
->ai_addr
, res
->ai_addrlen
);
306 /* Copy the first sockaddr. */
307 memcpy(pss
, res
->ai_addr
, res
->ai_addrlen
);
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
,
323 return interpret_string_addr_pref(pss
,
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
,
338 return interpret_string_addr_pref(pss
,
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
)
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
)) {
358 if (inet_pton(AF_INET
, str
, &dest
) <= 0) {
359 /* Error - this shouldn't happen ! */
360 DEBUG(0,("interpret_addr: inet_pton failed "
365 ret
= dest
.s_addr
; /* NETWORK BYTE ORDER ! */
367 /* Otherwise assume it's a network name of some sort and use
369 struct addrinfo
*res
= NULL
;
370 struct addrinfo
*res_list
= NULL
;
371 if (!interpret_string_addr_internal(&res_list
,
374 DEBUG(3,("interpret_addr: Unknown host. %s\n",str
));
378 /* Find the first IPv4 address. */
379 for (res
= res_list
; res
; res
= res
->ai_next
) {
380 if (res
->ai_family
!= AF_INET
) {
383 if (res
->ai_addr
== NULL
) {
389 DEBUG(3,("interpret_addr: host address is "
390 "invalid for host %s\n",str
));
392 freeaddrinfo(res_list
);
397 &((struct sockaddr_in
*)res
->ai_addr
)->sin_addr
.s_addr
,
400 freeaddrinfo(res_list
);
404 /* This is so bogus - all callers need fixing... JRA. */
405 if (ret
== (uint32_t)-1) {
413 A convenient addition to interpret_addr().
415 _PUBLIC_
struct in_addr
interpret_addr2(const char *str
)
418 uint32_t a
= interpret_addr(str
);
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
)
456 ret
= inet_pton(AF_INET
, str
, &dest
);
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
);
470 if (len
>= sizeof(buf
)) {
474 p
= normalize_ipv6_literal(str
, buf
, &len
);
486 * Return true if a string could be a IPv6 address.
489 bool is_ipaddress_v6(const char *str
)
491 #if defined(HAVE_IPV6)
494 char buf
[INET6_ADDRSTRLEN
] = { 0, };
496 const char *addr
= str
;
497 const char *idxs
= NULL
;
498 unsigned int idx
= 0;
501 p
= strchr_m(str
, ':');
503 return is_ipv6_literal(str
);
506 p
= strchr_m(str
, SCOPE_DELIMITER
);
507 if (p
&& (p
> str
)) {
508 len
= PTR_DIFF(p
, str
);
514 if (len
>= sizeof(buf
)) {
518 strncpy(buf
, str
, len
);
523 * Cope with link-local.
524 * This is IP:v6:addr%ifidx.
529 ret
= sscanf(idxs
, "%5u%c", &idx
, &c
);
534 if (idx
> 0 && idx
< UINT16_MAX
) {
541 * Cope with link-local.
542 * This is IP:v6:addr%ifname.
545 idx
= if_nametoindex(idxs
);
557 ret
= inet_pton(AF_INET6
, addr
, &ip6
);
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
);
590 if (pss
->sa_family
== AF_INET
) {
592 ntohl(((const struct sockaddr_in
*)pss
)->sin_addr
.s_addr
);
593 return addr
== INADDR_BROADCAST
;
599 * Check if an IPv7 is 127.0.0.1
601 bool is_loopback_ip_v4(struct in_addr ip
)
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
);
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
);
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
);
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
);
647 * Set an IP to 0.0.0.0.
649 void zero_ip_v4(struct in_addr
*ip
)
654 bool is_linklocal_addr(const struct sockaddr_storage
*pss
)
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
);
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
);
678 * Convert an IPv4 struct in_addr to a struct sockaddr_storage.
680 void in_addr_to_sockaddr_storage(struct sockaddr_storage
*ss
,
683 struct sockaddr_in
*sa
= (struct sockaddr_in
*)ss
;
685 sa
->sin_family
= AF_INET
;
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
,
696 struct sockaddr_in6
*sa
= (struct sockaddr_in6
*)ss
;
697 memset(ss
, '\0', sizeof(*ss
));
698 sa
->sin6_family
= AF_INET6
;
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. */
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
;
725 for (i
= 0; i
< sizeof(struct in6_addr
); i
++) {
730 return (memcmp(&ip1_6
.sin6_addr
,
732 sizeof(struct in6_addr
)) == 0);
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
);
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. */
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);
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);
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
,
780 sizeof(in6addr_any
)) == 0) {
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
) {
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
);
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
)
817 if (pss
->ss_family
!= AF_INET
) {
818 #if defined(HAVE_IPV6)
820 const struct sockaddr_in6
*sa6
=
821 (const struct sockaddr_in6
*)pss
;
822 port
= ntohs(sa6
->sin6_port
);
825 const struct sockaddr_in
*sa
=
826 (const struct sockaddr_in
*)pss
;
827 port
= ntohs(sa
->sin_port
);
832 /****************************************************************************
833 Print out an IPv4 or IPv6 address from a struct sockaddr_storage.
834 ****************************************************************************/
836 char *print_sockaddr_len(char *dest
,
838 const struct sockaddr
*psa
,
844 (void)sys_getnameinfo(psa
,
852 /****************************************************************************
853 Print out an IPv4 or IPv6 address from a struct sockaddr_storage.
854 ****************************************************************************/
856 char *print_sockaddr(char *dest
,
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
];
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
),
887 if (pss
->ss_family
!= AF_INET
) {
888 #if defined(HAVE_IPV6)
889 dest
= talloc_asprintf(ctx
, "[%s]", addr
);
894 dest
= talloc_asprintf(ctx
, "%s", addr
);
900 enum SOCK_OPT_TYPES
{OPT_BOOL
,OPT_INT
,OPT_ON
};
902 typedef struct 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
},
915 {"TCP_NODELAY", IPPROTO_TCP
, TCP_NODELAY
, 0, OPT_BOOL
},
918 {"TCP_KEEPCNT", IPPROTO_TCP
, TCP_KEEPCNT
, 0, OPT_INT
},
921 {"TCP_KEEPIDLE", IPPROTO_TCP
, TCP_KEEPIDLE
, 0, OPT_INT
},
924 {"TCP_KEEPINTVL", IPPROTO_TCP
, TCP_KEEPINTVL
, 0, OPT_INT
},
926 #ifdef IPTOS_LOWDELAY
927 {"IPTOS_LOWDELAY", IPPROTO_IP
, IP_TOS
, IPTOS_LOWDELAY
, OPT_ON
},
929 #ifdef IPTOS_THROUGHPUT
930 {"IPTOS_THROUGHPUT", IPPROTO_IP
, IP_TOS
, IPTOS_THROUGHPUT
, OPT_ON
},
933 {"SO_REUSEPORT", SOL_SOCKET
, SO_REUSEPORT
, 0, OPT_BOOL
},
936 {"SO_SNDBUF", SOL_SOCKET
, SO_SNDBUF
, 0, OPT_INT
},
939 {"SO_RCVBUF", SOL_SOCKET
, SO_RCVBUF
, 0, OPT_INT
},
942 {"SO_SNDLOWAT", SOL_SOCKET
, SO_SNDLOWAT
, 0, OPT_INT
},
945 {"SO_RCVLOWAT", SOL_SOCKET
, SO_RCVLOWAT
, 0, OPT_INT
},
948 {"SO_SNDTIMEO", SOL_SOCKET
, SO_SNDTIMEO
, 0, OPT_INT
},
951 {"SO_RCVTIMEO", SOL_SOCKET
, SO_RCVTIMEO
, 0, OPT_INT
},
954 {"TCP_FASTACK", IPPROTO_TCP
, TCP_FASTACK
, 0, OPT_INT
},
957 {"TCP_QUICKACK", IPPROTO_TCP
, TCP_QUICKACK
, 0, OPT_BOOL
},
959 #ifdef TCP_NODELAYACK
960 {"TCP_NODELAYACK", IPPROTO_TCP
, TCP_NODELAYACK
, 0, OPT_BOOL
},
962 #ifdef TCP_KEEPALIVE_THRESHOLD
963 {"TCP_KEEPALIVE_THRESHOLD", IPPROTO_TCP
, TCP_KEEPALIVE_THRESHOLD
, 0, OPT_INT
},
965 #ifdef TCP_KEEPALIVE_ABORT_THRESHOLD
966 {"TCP_KEEPALIVE_ABORT_THRESHOLD", IPPROTO_TCP
, TCP_KEEPALIVE_ABORT_THRESHOLD
, 0, OPT_INT
},
968 #ifdef TCP_DEFER_ACCEPT
969 {"TCP_DEFER_ACCEPT", IPPROTO_TCP
, TCP_DEFER_ACCEPT
, 0, OPT_INT
},
971 #ifdef TCP_USER_TIMEOUT
972 {"TCP_USER_TIMEOUT", IPPROTO_TCP
, TCP_USER_TIMEOUT
, 0, OPT_INT
},
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];
986 if (DEBUGLEVEL
< 5) {
990 frame
= talloc_stackframe();
992 str
= talloc_strdup(frame
, "");
994 DBG_WARNING("talloc failed\n");
998 for (; p
->name
!= NULL
; p
++) {
1000 socklen_t vlen
= sizeof(val
);
1002 ret
= getsockopt(s
, p
->level
, p
->option
, (void *)&val
, &vlen
);
1004 DBG_INFO("Could not test socket option %s: %s.\n",
1005 p
->name
, strerror(errno
));
1009 str
= talloc_asprintf_append_buffer(
1012 str
[0] != '\0' ? ", " : "",
1016 DBG_WARNING("talloc_asprintf_append_buffer failed\n");
1021 DEBUG(5, ("socket options: %s\n", str
));
1026 /****************************************************************************
1027 Set user socket options.
1028 ****************************************************************************/
1030 void set_socket_options(int fd
, const char *options
)
1032 TALLOC_CTX
*ctx
= talloc_new(NULL
);
1035 while (next_token_talloc(ctx
, &options
, &tok
," \t,")) {
1039 bool got_value
= false;
1041 if ((p
= strchr_m(tok
,'='))) {
1047 for (i
=0;socket_options
[i
].name
;i
++)
1048 if (strequal(socket_options
[i
].name
,tok
))
1051 if (!socket_options
[i
].name
) {
1052 DEBUG(0,("Unknown socket option %s\n",tok
));
1056 switch (socket_options
[i
].opttype
) {
1059 ret
= setsockopt(fd
,socket_options
[i
].level
,
1060 socket_options
[i
].option
,
1061 (char *)&value
,sizeof(int));
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));
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
) ));
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
)
1102 switch (ss
->ss_family
) {
1104 sa
->sa_socklen
= sizeof(struct sockaddr_in
);
1108 sa
->sa_socklen
= sizeof(struct sockaddr_in6
);
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
);
1124 if (sa
->u
.sa
.sa_family
== AF_INET6
) {
1125 sa
->u
.in6
.sin6_port
= htons(port
);
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
);
1139 if (sa
->u
.sa
.sa_family
== AF_INET6
) {
1140 *port
= ntohs(sa
->u
.in6
.sin6_port
);