s3:libsmb: Plumb cli_smb2_dskattr() inside cli_dskattr().
[Samba.git] / lib / util / util_net.c
blob83afda4c78e95a060c93bf3233f982c00bf720c4
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"
31 #undef strcasecmp
33 /*******************************************************************
34 Set an address to INADDR_ANY.
35 ******************************************************************/
37 void zero_sockaddr(struct sockaddr_storage *pss)
39 ZERO_STRUCTP(pss);
40 /* Ensure we're at least a valid sockaddr-storage. */
41 pss->ss_family = AF_INET;
44 /**
45 * Wrap getaddrinfo...
47 bool interpret_string_addr_internal(struct addrinfo **ppres,
48 const char *str, int flags)
50 int ret;
51 struct addrinfo hints;
53 ZERO_STRUCT(hints);
55 /* By default make sure it supports TCP. */
56 hints.ai_socktype = SOCK_STREAM;
58 /* always try as a numeric host first. This prevents unnecessary name
59 * lookups, and also ensures we accept IPv6 addresses */
60 hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST;
61 ret = getaddrinfo(str, NULL, &hints, ppres);
62 if (ret == 0) {
63 return true;
66 hints.ai_flags = flags;
68 /* Linux man page on getaddrinfo() says port will be
69 uninitialized when service string is NULL */
71 ret = getaddrinfo(str, NULL,
72 &hints,
73 ppres);
75 if (ret) {
76 DEBUG(3, ("interpret_string_addr_internal: "
77 "getaddrinfo failed for name %s (flags %d) [%s]\n",
78 str, flags, gai_strerror(ret)));
79 return false;
81 return true;
84 /*******************************************************************
85 Map a text hostname or IP address (IPv4 or IPv6) into a
86 struct sockaddr_storage. Takes a flag which allows it to
87 prefer an IPv4 address (needed for DC's).
88 ******************************************************************/
90 static bool interpret_string_addr_pref(struct sockaddr_storage *pss,
91 const char *str,
92 int flags,
93 bool prefer_ipv4)
95 struct addrinfo *res = NULL;
96 int int_flags;
97 #if defined(HAVE_IPV6)
98 char addr[INET6_ADDRSTRLEN];
99 unsigned int scope_id = 0;
101 if (strchr_m(str, ':')) {
102 char *p = strchr_m(str, '%');
105 * Cope with link-local.
106 * This is IP:v6:addr%ifname.
109 if (p && (p > str) && ((scope_id = if_nametoindex(p+1)) != 0)) {
110 /* Length of string we want to copy.
111 This is IP:v6:addr (removing the %ifname).
113 size_t len = PTR_DIFF(p,str);
115 if (len+1 > sizeof(addr)) {
116 /* string+nul too long for array. */
117 return false;
119 memcpy(addr, str, len);
120 addr[len] = '\0';
122 str = addr;
125 #endif
127 zero_sockaddr(pss);
129 if (flags & AI_NUMERICHOST) {
130 int_flags = flags;
131 } else {
132 int_flags = flags|AI_ADDRCONFIG;
135 if (!interpret_string_addr_internal(&res, str, int_flags)) {
136 return false;
138 if (!res) {
139 return false;
142 if (prefer_ipv4) {
143 struct addrinfo *p;
145 for (p = res; p; p = p->ai_next) {
146 if (p->ai_family == AF_INET) {
147 memcpy(pss, p->ai_addr, p->ai_addrlen);
148 break;
151 if (p == NULL) {
152 /* Copy the first sockaddr. */
153 memcpy(pss, res->ai_addr, res->ai_addrlen);
155 } else {
156 /* Copy the first sockaddr. */
157 memcpy(pss, res->ai_addr, res->ai_addrlen);
160 #if defined(HAVE_IPV6)
161 if (pss->ss_family == AF_INET6 && scope_id) {
162 struct sockaddr_in6 *ps6 = (struct sockaddr_in6 *)pss;
163 if (IN6_IS_ADDR_LINKLOCAL(&ps6->sin6_addr) &&
164 ps6->sin6_scope_id == 0) {
165 ps6->sin6_scope_id = scope_id;
168 #endif
170 freeaddrinfo(res);
171 return true;
174 /*******************************************************************
175 Map a text hostname or IP address (IPv4 or IPv6) into a
176 struct sockaddr_storage. Address agnostic version.
177 ******************************************************************/
179 bool interpret_string_addr(struct sockaddr_storage *pss,
180 const char *str,
181 int flags)
183 return interpret_string_addr_pref(pss,
184 str,
185 flags,
186 false);
189 /*******************************************************************
190 Map a text hostname or IP address (IPv4 or IPv6) into a
191 struct sockaddr_storage. Version that prefers IPv4.
192 ******************************************************************/
194 bool interpret_string_addr_prefer_ipv4(struct sockaddr_storage *pss,
195 const char *str,
196 int flags)
198 return interpret_string_addr_pref(pss,
199 str,
200 flags,
201 true);
205 * Interpret an internet address or name into an IP address in 4 byte form.
206 * RETURNS IN NETWORK BYTE ORDER (big endian).
209 uint32_t interpret_addr(const char *str)
211 uint32_t ret;
213 /* If it's in the form of an IP address then
214 * get the lib to interpret it */
215 if (is_ipaddress_v4(str)) {
216 struct in_addr dest;
218 if (inet_pton(AF_INET, str, &dest) <= 0) {
219 /* Error - this shouldn't happen ! */
220 DEBUG(0,("interpret_addr: inet_pton failed "
221 "host %s\n",
222 str));
223 return 0;
225 ret = dest.s_addr; /* NETWORK BYTE ORDER ! */
226 } else {
227 /* Otherwise assume it's a network name of some sort and use
228 getadddrinfo. */
229 struct addrinfo *res = NULL;
230 struct addrinfo *res_list = NULL;
231 if (!interpret_string_addr_internal(&res_list,
232 str,
233 AI_ADDRCONFIG)) {
234 DEBUG(3,("interpret_addr: Unknown host. %s\n",str));
235 return 0;
238 /* Find the first IPv4 address. */
239 for (res = res_list; res; res = res->ai_next) {
240 if (res->ai_family != AF_INET) {
241 continue;
243 if (res->ai_addr == NULL) {
244 continue;
246 break;
248 if(res == NULL) {
249 DEBUG(3,("interpret_addr: host address is "
250 "invalid for host %s\n",str));
251 if (res_list) {
252 freeaddrinfo(res_list);
254 return 0;
256 memcpy((char *)&ret,
257 &((struct sockaddr_in *)res->ai_addr)->sin_addr.s_addr,
258 sizeof(ret));
259 if (res_list) {
260 freeaddrinfo(res_list);
264 /* This is so bogus - all callers need fixing... JRA. */
265 if (ret == (uint32_t)-1) {
266 return 0;
269 return ret;
273 A convenient addition to interpret_addr().
275 _PUBLIC_ struct in_addr interpret_addr2(const char *str)
277 struct in_addr ret;
278 uint32_t a = interpret_addr(str);
279 ret.s_addr = a;
280 return ret;
284 Check if an IP is the 0.0.0.0.
287 _PUBLIC_ bool is_zero_ip_v4(struct in_addr ip)
289 return ip.s_addr == 0;
293 Are two IPs on the same subnet?
296 _PUBLIC_ bool same_net_v4(struct in_addr ip1, struct in_addr ip2, struct in_addr mask)
298 uint32_t net1,net2,nmask;
300 nmask = ntohl(mask.s_addr);
301 net1 = ntohl(ip1.s_addr);
302 net2 = ntohl(ip2.s_addr);
304 return((net1 & nmask) == (net2 & nmask));
308 * Return true if a string could be an IPv4 address.
311 bool is_ipaddress_v4(const char *str)
313 int ret = -1;
314 struct in_addr dest;
316 ret = inet_pton(AF_INET, str, &dest);
317 if (ret > 0) {
318 return true;
320 return false;
324 * Return true if a string could be a IPv6 address.
327 bool is_ipaddress_v6(const char *str)
329 #if defined(HAVE_IPV6)
330 int ret = -1;
332 if (strchr_m(str, ':')) {
333 char addr[INET6_ADDRSTRLEN];
334 struct in6_addr dest6;
335 const char *sp = str;
336 char *p = strchr_m(str, '%');
339 * Cope with link-local.
340 * This is IP:v6:addr%ifname.
343 if (p && (p > str) && (if_nametoindex(p+1) != 0)) {
344 size_t len = MIN(PTR_DIFF(p,str)+1, sizeof(addr));
345 if (strlcpy(addr, str, len) >= len) {
346 /* Truncate. */
347 return false;
349 sp = addr;
351 ret = inet_pton(AF_INET6, sp, &dest6);
352 if (ret > 0) {
353 return true;
356 #endif
357 return false;
361 * Return true if a string could be an IPv4 or IPv6 address.
364 bool is_ipaddress(const char *str)
366 return is_ipaddress_v4(str) || is_ipaddress_v6(str);
370 * Is a sockaddr a broadcast address ?
373 bool is_broadcast_addr(const struct sockaddr *pss)
375 #if defined(HAVE_IPV6)
376 if (pss->sa_family == AF_INET6) {
377 const struct in6_addr *sin6 =
378 &((const struct sockaddr_in6 *)pss)->sin6_addr;
379 return IN6_IS_ADDR_MULTICAST(sin6);
381 #endif
382 if (pss->sa_family == AF_INET) {
383 uint32_t addr =
384 ntohl(((const struct sockaddr_in *)pss)->sin_addr.s_addr);
385 return addr == INADDR_BROADCAST;
387 return false;
391 * Check if an IPv7 is 127.0.0.1
393 bool is_loopback_ip_v4(struct in_addr ip)
395 struct in_addr a;
396 a.s_addr = htonl(INADDR_LOOPBACK);
397 return(ip.s_addr == a.s_addr);
401 * Check if a struct sockaddr is the loopback address.
403 bool is_loopback_addr(const struct sockaddr *pss)
405 #if defined(HAVE_IPV6)
406 if (pss->sa_family == AF_INET6) {
407 const struct in6_addr *pin6 =
408 &((const struct sockaddr_in6 *)pss)->sin6_addr;
409 return IN6_IS_ADDR_LOOPBACK(pin6);
411 #endif
412 if (pss->sa_family == AF_INET) {
413 const struct in_addr *pin = &((const struct sockaddr_in *)pss)->sin_addr;
414 return is_loopback_ip_v4(*pin);
416 return false;
420 * Check if a struct sockaddr has an unspecified address.
422 bool is_zero_addr(const struct sockaddr_storage *pss)
424 #if defined(HAVE_IPV6)
425 if (pss->ss_family == AF_INET6) {
426 const struct in6_addr *pin6 =
427 &((const struct sockaddr_in6 *)pss)->sin6_addr;
428 return IN6_IS_ADDR_UNSPECIFIED(pin6);
430 #endif
431 if (pss->ss_family == AF_INET) {
432 const struct in_addr *pin = &((const struct sockaddr_in *)pss)->sin_addr;
433 return is_zero_ip_v4(*pin);
435 return false;
439 * Set an IP to 0.0.0.0.
441 void zero_ip_v4(struct in_addr *ip)
443 ZERO_STRUCTP(ip);
446 bool is_linklocal_addr(const struct sockaddr_storage *pss)
448 #ifdef HAVE_IPV6
449 if (pss->ss_family == AF_INET6) {
450 const struct in6_addr *pin6 =
451 &((const struct sockaddr_in6 *)pss)->sin6_addr;
452 return IN6_IS_ADDR_LINKLOCAL(pin6);
454 #endif
455 if (pss->ss_family == AF_INET) {
456 const struct in_addr *pin =
457 &((const struct sockaddr_in *)pss)->sin_addr;
458 struct in_addr ll_addr;
459 struct in_addr mask_addr;
461 /* 169.254.0.0/16, is link local, see RFC 3927 */
462 ll_addr.s_addr = 0xa9fe0000;
463 mask_addr.s_addr = 0xffff0000;
464 return same_net_v4(*pin, ll_addr, mask_addr);
466 return false;
470 * Convert an IPv4 struct in_addr to a struct sockaddr_storage.
472 void in_addr_to_sockaddr_storage(struct sockaddr_storage *ss,
473 struct in_addr ip)
475 struct sockaddr_in *sa = (struct sockaddr_in *)ss;
476 ZERO_STRUCTP(ss);
477 sa->sin_family = AF_INET;
478 sa->sin_addr = ip;
481 #if defined(HAVE_IPV6)
483 * Convert an IPv6 struct in_addr to a struct sockaddr_storage.
485 void in6_addr_to_sockaddr_storage(struct sockaddr_storage *ss,
486 struct in6_addr ip)
488 struct sockaddr_in6 *sa = (struct sockaddr_in6 *)ss;
489 memset(ss, '\0', sizeof(*ss));
490 sa->sin6_family = AF_INET6;
491 sa->sin6_addr = ip;
493 #endif
496 * Are two IPs on the same subnet?
498 bool same_net(const struct sockaddr *ip1,
499 const struct sockaddr *ip2,
500 const struct sockaddr *mask)
502 if (ip1->sa_family != ip2->sa_family) {
503 /* Never on the same net. */
504 return false;
507 #if defined(HAVE_IPV6)
508 if (ip1->sa_family == AF_INET6) {
509 struct sockaddr_in6 ip1_6 = *(const struct sockaddr_in6 *)ip1;
510 struct sockaddr_in6 ip2_6 = *(const struct sockaddr_in6 *)ip2;
511 struct sockaddr_in6 mask_6 = *(const struct sockaddr_in6 *)mask;
512 char *p1 = (char *)&ip1_6.sin6_addr;
513 char *p2 = (char *)&ip2_6.sin6_addr;
514 char *m = (char *)&mask_6.sin6_addr;
515 int i;
517 for (i = 0; i < sizeof(struct in6_addr); i++) {
518 *p1++ &= *m;
519 *p2++ &= *m;
520 m++;
522 return (memcmp(&ip1_6.sin6_addr,
523 &ip2_6.sin6_addr,
524 sizeof(struct in6_addr)) == 0);
526 #endif
527 if (ip1->sa_family == AF_INET) {
528 return same_net_v4(((const struct sockaddr_in *)ip1)->sin_addr,
529 ((const struct sockaddr_in *)ip2)->sin_addr,
530 ((const struct sockaddr_in *)mask)->sin_addr);
532 return false;
536 * Are two sockaddr 's the same family and address ? Ignore port etc.
539 bool sockaddr_equal(const struct sockaddr *ip1,
540 const struct sockaddr *ip2)
542 if (ip1->sa_family != ip2->sa_family) {
543 /* Never the same. */
544 return false;
547 #if defined(HAVE_IPV6)
548 if (ip1->sa_family == AF_INET6) {
549 return (memcmp(&((const struct sockaddr_in6 *)ip1)->sin6_addr,
550 &((const struct sockaddr_in6 *)ip2)->sin6_addr,
551 sizeof(struct in6_addr)) == 0);
553 #endif
554 if (ip1->sa_family == AF_INET) {
555 return (memcmp(&((const struct sockaddr_in *)ip1)->sin_addr,
556 &((const struct sockaddr_in *)ip2)->sin_addr,
557 sizeof(struct in_addr)) == 0);
559 return false;
563 * Is an IP address the INADDR_ANY or in6addr_any value ?
565 bool is_address_any(const struct sockaddr *psa)
567 #if defined(HAVE_IPV6)
568 if (psa->sa_family == AF_INET6) {
569 const struct sockaddr_in6 *si6 = (const struct sockaddr_in6 *)psa;
570 if (memcmp(&in6addr_any,
571 &si6->sin6_addr,
572 sizeof(in6addr_any)) == 0) {
573 return true;
575 return false;
577 #endif
578 if (psa->sa_family == AF_INET) {
579 const struct sockaddr_in *si = (const struct sockaddr_in *)psa;
580 if (si->sin_addr.s_addr == INADDR_ANY) {
581 return true;
583 return false;
585 return false;
588 void set_sockaddr_port(struct sockaddr *psa, uint16_t port)
590 #if defined(HAVE_IPV6)
591 if (psa->sa_family == AF_INET6) {
592 ((struct sockaddr_in6 *)psa)->sin6_port = htons(port);
594 #endif
595 if (psa->sa_family == AF_INET) {
596 ((struct sockaddr_in *)psa)->sin_port = htons(port);
601 /****************************************************************************
602 Get a port number in host byte order from a sockaddr_storage.
603 ****************************************************************************/
605 uint16_t get_sockaddr_port(const struct sockaddr_storage *pss)
607 uint16_t port = 0;
609 if (pss->ss_family != AF_INET) {
610 #if defined(HAVE_IPV6)
611 /* IPv6 */
612 const struct sockaddr_in6 *sa6 =
613 (const struct sockaddr_in6 *)pss;
614 port = ntohs(sa6->sin6_port);
615 #endif
616 } else {
617 const struct sockaddr_in *sa =
618 (const struct sockaddr_in *)pss;
619 port = ntohs(sa->sin_port);
621 return port;
624 /****************************************************************************
625 Print out an IPv4 or IPv6 address from a struct sockaddr_storage.
626 ****************************************************************************/
628 char *print_sockaddr_len(char *dest,
629 size_t destlen,
630 const struct sockaddr *psa,
631 socklen_t psalen)
633 if (destlen > 0) {
634 dest[0] = '\0';
636 (void)sys_getnameinfo(psa,
637 psalen,
638 dest, destlen,
639 NULL, 0,
640 NI_NUMERICHOST);
641 return dest;
644 /****************************************************************************
645 Print out an IPv4 or IPv6 address from a struct sockaddr_storage.
646 ****************************************************************************/
648 char *print_sockaddr(char *dest,
649 size_t destlen,
650 const struct sockaddr_storage *psa)
652 return print_sockaddr_len(dest, destlen, (const struct sockaddr *)psa,
653 sizeof(struct sockaddr_storage));
656 /****************************************************************************
657 Print out a canonical IPv4 or IPv6 address from a struct sockaddr_storage.
658 ****************************************************************************/
660 char *print_canonical_sockaddr(TALLOC_CTX *ctx,
661 const struct sockaddr_storage *pss)
663 char addr[INET6_ADDRSTRLEN];
664 char *dest = NULL;
665 int ret;
667 /* Linux getnameinfo() man pages says port is unitialized if
668 service name is NULL. */
670 ret = sys_getnameinfo((const struct sockaddr *)pss,
671 sizeof(struct sockaddr_storage),
672 addr, sizeof(addr),
673 NULL, 0,
674 NI_NUMERICHOST);
675 if (ret != 0) {
676 return NULL;
679 if (pss->ss_family != AF_INET) {
680 #if defined(HAVE_IPV6)
681 dest = talloc_asprintf(ctx, "[%s]", addr);
682 #else
683 return NULL;
684 #endif
685 } else {
686 dest = talloc_asprintf(ctx, "%s", addr);
689 return dest;
692 /****************************************************************************
693 Return the port number we've bound to on a socket.
694 ****************************************************************************/
696 int get_socket_port(int fd)
698 struct sockaddr_storage sa;
699 socklen_t length = sizeof(sa);
701 if (fd == -1) {
702 return -1;
705 if (getsockname(fd, (struct sockaddr *)&sa, &length) < 0) {
706 int level = (errno == ENOTCONN) ? 2 : 0;
707 DEBUG(level, ("getsockname failed. Error was %s\n",
708 strerror(errno)));
709 return -1;
712 #if defined(HAVE_IPV6)
713 if (sa.ss_family == AF_INET6) {
714 return ntohs(((struct sockaddr_in6 *)&sa)->sin6_port);
716 #endif
717 if (sa.ss_family == AF_INET) {
718 return ntohs(((struct sockaddr_in *)&sa)->sin_port);
720 return -1;
723 /****************************************************************************
724 Return the string of an IP address (IPv4 or IPv6).
725 ****************************************************************************/
727 static const char *get_socket_addr(int fd, char *addr_buf, size_t addr_len)
729 struct sockaddr_storage sa;
730 socklen_t length = sizeof(sa);
732 /* Ok, returning a hard coded IPv4 address
733 * is bogus, but it's just as bogus as a
734 * zero IPv6 address. No good choice here.
737 if (strlcpy(addr_buf, "0.0.0.0", addr_len) >= addr_len) {
738 /* Truncate ! */
739 return NULL;
742 if (fd == -1) {
743 return addr_buf;
746 if (getsockname(fd, (struct sockaddr *)&sa, &length) < 0) {
747 DEBUG(0,("getsockname failed. Error was %s\n",
748 strerror(errno) ));
749 return addr_buf;
752 return print_sockaddr_len(addr_buf, addr_len, (struct sockaddr *)&sa, length);
755 const char *client_socket_addr(int fd, char *addr, size_t addr_len)
757 return get_socket_addr(fd, addr, addr_len);
761 enum SOCK_OPT_TYPES {OPT_BOOL,OPT_INT,OPT_ON};
763 typedef struct smb_socket_option {
764 const char *name;
765 int level;
766 int option;
767 int value;
768 int opttype;
769 } smb_socket_option;
771 static const smb_socket_option socket_options[] = {
772 {"SO_KEEPALIVE", SOL_SOCKET, SO_KEEPALIVE, 0, OPT_BOOL},
773 {"SO_REUSEADDR", SOL_SOCKET, SO_REUSEADDR, 0, OPT_BOOL},
774 {"SO_BROADCAST", SOL_SOCKET, SO_BROADCAST, 0, OPT_BOOL},
775 #ifdef TCP_NODELAY
776 {"TCP_NODELAY", IPPROTO_TCP, TCP_NODELAY, 0, OPT_BOOL},
777 #endif
778 #ifdef TCP_KEEPCNT
779 {"TCP_KEEPCNT", IPPROTO_TCP, TCP_KEEPCNT, 0, OPT_INT},
780 #endif
781 #ifdef TCP_KEEPIDLE
782 {"TCP_KEEPIDLE", IPPROTO_TCP, TCP_KEEPIDLE, 0, OPT_INT},
783 #endif
784 #ifdef TCP_KEEPINTVL
785 {"TCP_KEEPINTVL", IPPROTO_TCP, TCP_KEEPINTVL, 0, OPT_INT},
786 #endif
787 #ifdef IPTOS_LOWDELAY
788 {"IPTOS_LOWDELAY", IPPROTO_IP, IP_TOS, IPTOS_LOWDELAY, OPT_ON},
789 #endif
790 #ifdef IPTOS_THROUGHPUT
791 {"IPTOS_THROUGHPUT", IPPROTO_IP, IP_TOS, IPTOS_THROUGHPUT, OPT_ON},
792 #endif
793 #ifdef SO_REUSEPORT
794 {"SO_REUSEPORT", SOL_SOCKET, SO_REUSEPORT, 0, OPT_BOOL},
795 #endif
796 #ifdef SO_SNDBUF
797 {"SO_SNDBUF", SOL_SOCKET, SO_SNDBUF, 0, OPT_INT},
798 #endif
799 #ifdef SO_RCVBUF
800 {"SO_RCVBUF", SOL_SOCKET, SO_RCVBUF, 0, OPT_INT},
801 #endif
802 #ifdef SO_SNDLOWAT
803 {"SO_SNDLOWAT", SOL_SOCKET, SO_SNDLOWAT, 0, OPT_INT},
804 #endif
805 #ifdef SO_RCVLOWAT
806 {"SO_RCVLOWAT", SOL_SOCKET, SO_RCVLOWAT, 0, OPT_INT},
807 #endif
808 #ifdef SO_SNDTIMEO
809 {"SO_SNDTIMEO", SOL_SOCKET, SO_SNDTIMEO, 0, OPT_INT},
810 #endif
811 #ifdef SO_RCVTIMEO
812 {"SO_RCVTIMEO", SOL_SOCKET, SO_RCVTIMEO, 0, OPT_INT},
813 #endif
814 #ifdef TCP_FASTACK
815 {"TCP_FASTACK", IPPROTO_TCP, TCP_FASTACK, 0, OPT_INT},
816 #endif
817 #ifdef TCP_QUICKACK
818 {"TCP_QUICKACK", IPPROTO_TCP, TCP_QUICKACK, 0, OPT_BOOL},
819 #endif
820 #ifdef TCP_NODELAYACK
821 {"TCP_NODELAYACK", IPPROTO_TCP, TCP_NODELAYACK, 0, OPT_BOOL},
822 #endif
823 #ifdef TCP_KEEPALIVE_THRESHOLD
824 {"TCP_KEEPALIVE_THRESHOLD", IPPROTO_TCP, TCP_KEEPALIVE_THRESHOLD, 0, OPT_INT},
825 #endif
826 #ifdef TCP_KEEPALIVE_ABORT_THRESHOLD
827 {"TCP_KEEPALIVE_ABORT_THRESHOLD", IPPROTO_TCP, TCP_KEEPALIVE_ABORT_THRESHOLD, 0, OPT_INT},
828 #endif
829 #ifdef TCP_DEFER_ACCEPT
830 {"TCP_DEFER_ACCEPT", IPPROTO_TCP, TCP_DEFER_ACCEPT, 0, OPT_INT},
831 #endif
832 {NULL,0,0,0,0}};
834 /****************************************************************************
835 Print socket options.
836 ****************************************************************************/
838 static void print_socket_options(int s)
840 int value;
841 socklen_t vlen = 4;
842 const smb_socket_option *p = &socket_options[0];
844 /* wrapped in if statement to prevent streams
845 * leak in SCO Openserver 5.0 */
846 /* reported on samba-technical --jerry */
847 if ( DEBUGLEVEL >= 5 ) {
848 DEBUG(5,("Socket options:\n"));
849 for (; p->name != NULL; p++) {
850 if (getsockopt(s, p->level, p->option,
851 (void *)&value, &vlen) == -1) {
852 DEBUGADD(5,("\tCould not test socket option %s.\n",
853 p->name));
854 } else {
855 DEBUGADD(5,("\t%s = %d\n",
856 p->name,value));
862 /****************************************************************************
863 Set user socket options.
864 ****************************************************************************/
866 void set_socket_options(int fd, const char *options)
868 TALLOC_CTX *ctx = talloc_new(NULL);
869 char *tok;
871 while (next_token_talloc(ctx, &options, &tok," \t,")) {
872 int ret=0,i;
873 int value = 1;
874 char *p;
875 bool got_value = false;
877 if ((p = strchr_m(tok,'='))) {
878 *p = 0;
879 value = atoi(p+1);
880 got_value = true;
883 for (i=0;socket_options[i].name;i++)
884 if (strequal(socket_options[i].name,tok))
885 break;
887 if (!socket_options[i].name) {
888 DEBUG(0,("Unknown socket option %s\n",tok));
889 continue;
892 switch (socket_options[i].opttype) {
893 case OPT_BOOL:
894 case OPT_INT:
895 ret = setsockopt(fd,socket_options[i].level,
896 socket_options[i].option,
897 (char *)&value,sizeof(int));
898 break;
900 case OPT_ON:
901 if (got_value)
902 DEBUG(0,("syntax error - %s "
903 "does not take a value\n",tok));
906 int on = socket_options[i].value;
907 ret = setsockopt(fd,socket_options[i].level,
908 socket_options[i].option,
909 (char *)&on,sizeof(int));
911 break;
914 if (ret != 0) {
915 /* be aware that some systems like Solaris return
916 * EINVAL to a setsockopt() call when the client
917 * sent a RST previously - no need to worry */
918 DEBUG(2,("Failed to set socket option %s (Error %s)\n",
919 tok, strerror(errno) ));
923 TALLOC_FREE(ctx);
924 print_socket_options(fd);