s3-torture: run_locktest2(): replace cli_lock() with cli_lock32()
[Samba.git] / lib / util / util_net.c
blob566bf56e8ffb2174395f192f667145effbd46046
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: getaddrinfo failed "
77 "for name %s [%s]\n",
78 str,
79 gai_strerror(ret) ));
80 return false;
82 return true;
85 /*******************************************************************
86 Map a text hostname or IP address (IPv4 or IPv6) into a
87 struct sockaddr_storage. Takes a flag which allows it to
88 prefer an IPv4 address (needed for DC's).
89 ******************************************************************/
91 static bool interpret_string_addr_pref(struct sockaddr_storage *pss,
92 const char *str,
93 int flags,
94 bool prefer_ipv4)
96 struct addrinfo *res = NULL;
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 strlcpy(addr, str,
111 MIN(PTR_DIFF(p,str)+1,
112 sizeof(addr)));
113 str = addr;
116 #endif
118 zero_sockaddr(pss);
120 if (!interpret_string_addr_internal(&res, str, flags|AI_ADDRCONFIG)) {
121 return false;
123 if (!res) {
124 return false;
127 if (prefer_ipv4) {
128 struct addrinfo *p;
130 for (p = res; p; p = p->ai_next) {
131 if (p->ai_family == AF_INET) {
132 memcpy(pss, p->ai_addr, p->ai_addrlen);
133 break;
136 if (p == NULL) {
137 /* Copy the first sockaddr. */
138 memcpy(pss, res->ai_addr, res->ai_addrlen);
140 } else {
141 /* Copy the first sockaddr. */
142 memcpy(pss, res->ai_addr, res->ai_addrlen);
145 #if defined(HAVE_IPV6)
146 if (pss->ss_family == AF_INET6 && scope_id) {
147 struct sockaddr_in6 *ps6 = (struct sockaddr_in6 *)pss;
148 if (IN6_IS_ADDR_LINKLOCAL(&ps6->sin6_addr) &&
149 ps6->sin6_scope_id == 0) {
150 ps6->sin6_scope_id = scope_id;
153 #endif
155 freeaddrinfo(res);
156 return true;
159 /*******************************************************************
160 Map a text hostname or IP address (IPv4 or IPv6) into a
161 struct sockaddr_storage. Address agnostic version.
162 ******************************************************************/
164 bool interpret_string_addr(struct sockaddr_storage *pss,
165 const char *str,
166 int flags)
168 return interpret_string_addr_pref(pss,
169 str,
170 flags,
171 false);
174 /*******************************************************************
175 Map a text hostname or IP address (IPv4 or IPv6) into a
176 struct sockaddr_storage. Version that prefers IPv4.
177 ******************************************************************/
179 bool interpret_string_addr_prefer_ipv4(struct sockaddr_storage *pss,
180 const char *str,
181 int flags)
183 return interpret_string_addr_pref(pss,
184 str,
185 flags,
186 true);
190 * Interpret an internet address or name into an IP address in 4 byte form.
191 * RETURNS IN NETWORK BYTE ORDER (big endian).
194 uint32_t interpret_addr(const char *str)
196 uint32_t ret;
198 /* If it's in the form of an IP address then
199 * get the lib to interpret it */
200 if (is_ipaddress_v4(str)) {
201 struct in_addr dest;
203 if (inet_pton(AF_INET, str, &dest) <= 0) {
204 /* Error - this shouldn't happen ! */
205 DEBUG(0,("interpret_addr: inet_pton failed "
206 "host %s\n",
207 str));
208 return 0;
210 ret = dest.s_addr; /* NETWORK BYTE ORDER ! */
211 } else {
212 /* Otherwise assume it's a network name of some sort and use
213 getadddrinfo. */
214 struct addrinfo *res = NULL;
215 struct addrinfo *res_list = NULL;
216 if (!interpret_string_addr_internal(&res_list,
217 str,
218 AI_ADDRCONFIG)) {
219 DEBUG(3,("interpret_addr: Unknown host. %s\n",str));
220 return 0;
223 /* Find the first IPv4 address. */
224 for (res = res_list; res; res = res->ai_next) {
225 if (res->ai_family != AF_INET) {
226 continue;
228 if (res->ai_addr == NULL) {
229 continue;
231 break;
233 if(res == NULL) {
234 DEBUG(3,("interpret_addr: host address is "
235 "invalid for host %s\n",str));
236 if (res_list) {
237 freeaddrinfo(res_list);
239 return 0;
241 memcpy((char *)&ret,
242 &((struct sockaddr_in *)res->ai_addr)->sin_addr.s_addr,
243 sizeof(ret));
244 if (res_list) {
245 freeaddrinfo(res_list);
249 /* This is so bogus - all callers need fixing... JRA. */
250 if (ret == (uint32_t)-1) {
251 return 0;
254 return ret;
258 A convenient addition to interpret_addr().
260 _PUBLIC_ struct in_addr interpret_addr2(const char *str)
262 struct in_addr ret;
263 uint32_t a = interpret_addr(str);
264 ret.s_addr = a;
265 return ret;
269 Check if an IP is the 0.0.0.0.
272 _PUBLIC_ bool is_zero_ip_v4(struct in_addr ip)
274 return ip.s_addr == 0;
278 Are two IPs on the same subnet?
281 _PUBLIC_ bool same_net_v4(struct in_addr ip1, struct in_addr ip2, struct in_addr mask)
283 uint32_t net1,net2,nmask;
285 nmask = ntohl(mask.s_addr);
286 net1 = ntohl(ip1.s_addr);
287 net2 = ntohl(ip2.s_addr);
289 return((net1 & nmask) == (net2 & nmask));
293 * Return true if a string could be an IPv4 address.
296 bool is_ipaddress_v4(const char *str)
298 int ret = -1;
299 struct in_addr dest;
301 ret = inet_pton(AF_INET, str, &dest);
302 if (ret > 0) {
303 return true;
305 return false;
309 * Return true if a string could be a IPv6 address.
312 bool is_ipaddress_v6(const char *str)
314 #if defined(HAVE_IPV6)
315 int ret = -1;
317 if (strchr_m(str, ':')) {
318 char addr[INET6_ADDRSTRLEN];
319 struct in6_addr dest6;
320 const char *sp = str;
321 char *p = strchr_m(str, '%');
324 * Cope with link-local.
325 * This is IP:v6:addr%ifname.
328 if (p && (p > str) && (if_nametoindex(p+1) != 0)) {
329 strlcpy(addr, str,
330 MIN(PTR_DIFF(p,str)+1,
331 sizeof(addr)));
332 sp = addr;
334 ret = inet_pton(AF_INET6, sp, &dest6);
335 if (ret > 0) {
336 return true;
339 #endif
340 return false;
344 * Return true if a string could be an IPv4 or IPv6 address.
347 bool is_ipaddress(const char *str)
349 return is_ipaddress_v4(str) || is_ipaddress_v6(str);
353 * Is a sockaddr a broadcast address ?
356 bool is_broadcast_addr(const struct sockaddr *pss)
358 #if defined(HAVE_IPV6)
359 if (pss->sa_family == AF_INET6) {
360 const struct in6_addr *sin6 =
361 &((const struct sockaddr_in6 *)pss)->sin6_addr;
362 return IN6_IS_ADDR_MULTICAST(sin6);
364 #endif
365 if (pss->sa_family == AF_INET) {
366 uint32_t addr =
367 ntohl(((const struct sockaddr_in *)pss)->sin_addr.s_addr);
368 return addr == INADDR_BROADCAST;
370 return false;
374 * Check if an IPv7 is 127.0.0.1
376 bool is_loopback_ip_v4(struct in_addr ip)
378 struct in_addr a;
379 a.s_addr = htonl(INADDR_LOOPBACK);
380 return(ip.s_addr == a.s_addr);
384 * Check if a struct sockaddr is the loopback address.
386 bool is_loopback_addr(const struct sockaddr *pss)
388 #if defined(HAVE_IPV6)
389 if (pss->sa_family == AF_INET6) {
390 const struct in6_addr *pin6 =
391 &((const struct sockaddr_in6 *)pss)->sin6_addr;
392 return IN6_IS_ADDR_LOOPBACK(pin6);
394 #endif
395 if (pss->sa_family == AF_INET) {
396 const struct in_addr *pin = &((const struct sockaddr_in *)pss)->sin_addr;
397 return is_loopback_ip_v4(*pin);
399 return false;
403 * Check if a struct sockaddr has an unspecified address.
405 bool is_zero_addr(const struct sockaddr_storage *pss)
407 #if defined(HAVE_IPV6)
408 if (pss->ss_family == AF_INET6) {
409 const struct in6_addr *pin6 =
410 &((const struct sockaddr_in6 *)pss)->sin6_addr;
411 return IN6_IS_ADDR_UNSPECIFIED(pin6);
413 #endif
414 if (pss->ss_family == AF_INET) {
415 const struct in_addr *pin = &((const struct sockaddr_in *)pss)->sin_addr;
416 return is_zero_ip_v4(*pin);
418 return false;
422 * Set an IP to 0.0.0.0.
424 void zero_ip_v4(struct in_addr *ip)
426 ZERO_STRUCTP(ip);
430 * Convert an IPv4 struct in_addr to a struct sockaddr_storage.
432 void in_addr_to_sockaddr_storage(struct sockaddr_storage *ss,
433 struct in_addr ip)
435 struct sockaddr_in *sa = (struct sockaddr_in *)ss;
436 ZERO_STRUCTP(ss);
437 sa->sin_family = AF_INET;
438 sa->sin_addr = ip;
441 #if defined(HAVE_IPV6)
443 * Convert an IPv6 struct in_addr to a struct sockaddr_storage.
445 void in6_addr_to_sockaddr_storage(struct sockaddr_storage *ss,
446 struct in6_addr ip)
448 struct sockaddr_in6 *sa = (struct sockaddr_in6 *)ss;
449 memset(ss, '\0', sizeof(*ss));
450 sa->sin6_family = AF_INET6;
451 sa->sin6_addr = ip;
453 #endif
456 * Are two IPs on the same subnet?
458 bool same_net(const struct sockaddr *ip1,
459 const struct sockaddr *ip2,
460 const struct sockaddr *mask)
462 if (ip1->sa_family != ip2->sa_family) {
463 /* Never on the same net. */
464 return false;
467 #if defined(HAVE_IPV6)
468 if (ip1->sa_family == AF_INET6) {
469 struct sockaddr_in6 ip1_6 = *(const struct sockaddr_in6 *)ip1;
470 struct sockaddr_in6 ip2_6 = *(const struct sockaddr_in6 *)ip2;
471 struct sockaddr_in6 mask_6 = *(const struct sockaddr_in6 *)mask;
472 char *p1 = (char *)&ip1_6.sin6_addr;
473 char *p2 = (char *)&ip2_6.sin6_addr;
474 char *m = (char *)&mask_6.sin6_addr;
475 int i;
477 for (i = 0; i < sizeof(struct in6_addr); i++) {
478 *p1++ &= *m;
479 *p2++ &= *m;
480 m++;
482 return (memcmp(&ip1_6.sin6_addr,
483 &ip2_6.sin6_addr,
484 sizeof(struct in6_addr)) == 0);
486 #endif
487 if (ip1->sa_family == AF_INET) {
488 return same_net_v4(((const struct sockaddr_in *)ip1)->sin_addr,
489 ((const struct sockaddr_in *)ip2)->sin_addr,
490 ((const struct sockaddr_in *)mask)->sin_addr);
492 return false;
496 * Are two sockaddr 's the same family and address ? Ignore port etc.
499 bool sockaddr_equal(const struct sockaddr *ip1,
500 const struct sockaddr *ip2)
502 if (ip1->sa_family != ip2->sa_family) {
503 /* Never the same. */
504 return false;
507 #if defined(HAVE_IPV6)
508 if (ip1->sa_family == AF_INET6) {
509 return (memcmp(&((const struct sockaddr_in6 *)ip1)->sin6_addr,
510 &((const struct sockaddr_in6 *)ip2)->sin6_addr,
511 sizeof(struct in6_addr)) == 0);
513 #endif
514 if (ip1->sa_family == AF_INET) {
515 return (memcmp(&((const struct sockaddr_in *)ip1)->sin_addr,
516 &((const struct sockaddr_in *)ip2)->sin_addr,
517 sizeof(struct in_addr)) == 0);
519 return false;
523 * Is an IP address the INADDR_ANY or in6addr_any value ?
525 bool is_address_any(const struct sockaddr *psa)
527 #if defined(HAVE_IPV6)
528 if (psa->sa_family == AF_INET6) {
529 const struct sockaddr_in6 *si6 = (const struct sockaddr_in6 *)psa;
530 if (memcmp(&in6addr_any,
531 &si6->sin6_addr,
532 sizeof(in6addr_any)) == 0) {
533 return true;
535 return false;
537 #endif
538 if (psa->sa_family == AF_INET) {
539 const struct sockaddr_in *si = (const struct sockaddr_in *)psa;
540 if (si->sin_addr.s_addr == INADDR_ANY) {
541 return true;
543 return false;
545 return false;
548 void set_sockaddr_port(struct sockaddr *psa, uint16_t port)
550 #if defined(HAVE_IPV6)
551 if (psa->sa_family == AF_INET6) {
552 ((struct sockaddr_in6 *)psa)->sin6_port = htons(port);
554 #endif
555 if (psa->sa_family == AF_INET) {
556 ((struct sockaddr_in *)psa)->sin_port = htons(port);
561 /****************************************************************************
562 Get a port number in host byte order from a sockaddr_storage.
563 ****************************************************************************/
565 uint16_t get_sockaddr_port(const struct sockaddr_storage *pss)
567 uint16_t port = 0;
569 if (pss->ss_family != AF_INET) {
570 #if defined(HAVE_IPV6)
571 /* IPv6 */
572 const struct sockaddr_in6 *sa6 =
573 (const struct sockaddr_in6 *)pss;
574 port = ntohs(sa6->sin6_port);
575 #endif
576 } else {
577 const struct sockaddr_in *sa =
578 (const struct sockaddr_in *)pss;
579 port = ntohs(sa->sin_port);
581 return port;
584 /****************************************************************************
585 Print out an IPv4 or IPv6 address from a struct sockaddr_storage.
586 ****************************************************************************/
588 char *print_sockaddr_len(char *dest,
589 size_t destlen,
590 const struct sockaddr *psa,
591 socklen_t psalen)
593 if (destlen > 0) {
594 dest[0] = '\0';
596 (void)sys_getnameinfo(psa,
597 psalen,
598 dest, destlen,
599 NULL, 0,
600 NI_NUMERICHOST);
601 return dest;
604 /****************************************************************************
605 Print out an IPv4 or IPv6 address from a struct sockaddr_storage.
606 ****************************************************************************/
608 char *print_sockaddr(char *dest,
609 size_t destlen,
610 const struct sockaddr_storage *psa)
612 return print_sockaddr_len(dest, destlen, (const struct sockaddr *)psa,
613 sizeof(struct sockaddr_storage));
616 /****************************************************************************
617 Print out a canonical IPv4 or IPv6 address from a struct sockaddr_storage.
618 ****************************************************************************/
620 char *print_canonical_sockaddr(TALLOC_CTX *ctx,
621 const struct sockaddr_storage *pss)
623 char addr[INET6_ADDRSTRLEN];
624 char *dest = NULL;
625 int ret;
627 /* Linux getnameinfo() man pages says port is unitialized if
628 service name is NULL. */
630 ret = sys_getnameinfo((const struct sockaddr *)pss,
631 sizeof(struct sockaddr_storage),
632 addr, sizeof(addr),
633 NULL, 0,
634 NI_NUMERICHOST);
635 if (ret != 0) {
636 return NULL;
639 if (pss->ss_family != AF_INET) {
640 #if defined(HAVE_IPV6)
641 dest = talloc_asprintf(ctx, "[%s]", addr);
642 #else
643 return NULL;
644 #endif
645 } else {
646 dest = talloc_asprintf(ctx, "%s", addr);
649 return dest;
652 /****************************************************************************
653 Return the port number we've bound to on a socket.
654 ****************************************************************************/
656 int get_socket_port(int fd)
658 struct sockaddr_storage sa;
659 socklen_t length = sizeof(sa);
661 if (fd == -1) {
662 return -1;
665 if (getsockname(fd, (struct sockaddr *)&sa, &length) < 0) {
666 int level = (errno == ENOTCONN) ? 2 : 0;
667 DEBUG(level, ("getsockname failed. Error was %s\n",
668 strerror(errno)));
669 return -1;
672 #if defined(HAVE_IPV6)
673 if (sa.ss_family == AF_INET6) {
674 return ntohs(((struct sockaddr_in6 *)&sa)->sin6_port);
676 #endif
677 if (sa.ss_family == AF_INET) {
678 return ntohs(((struct sockaddr_in *)&sa)->sin_port);
680 return -1;
683 /****************************************************************************
684 Return the string of an IP address (IPv4 or IPv6).
685 ****************************************************************************/
687 static const char *get_socket_addr(int fd, char *addr_buf, size_t addr_len)
689 struct sockaddr_storage sa;
690 socklen_t length = sizeof(sa);
692 /* Ok, returning a hard coded IPv4 address
693 * is bogus, but it's just as bogus as a
694 * zero IPv6 address. No good choice here.
697 strlcpy(addr_buf, "0.0.0.0", addr_len);
699 if (fd == -1) {
700 return addr_buf;
703 if (getsockname(fd, (struct sockaddr *)&sa, &length) < 0) {
704 DEBUG(0,("getsockname failed. Error was %s\n",
705 strerror(errno) ));
706 return addr_buf;
709 return print_sockaddr_len(addr_buf, addr_len, (struct sockaddr *)&sa, length);
712 const char *client_socket_addr(int fd, char *addr, size_t addr_len)
714 return get_socket_addr(fd, addr, addr_len);
718 enum SOCK_OPT_TYPES {OPT_BOOL,OPT_INT,OPT_ON};
720 typedef struct smb_socket_option {
721 const char *name;
722 int level;
723 int option;
724 int value;
725 int opttype;
726 } smb_socket_option;
728 static const smb_socket_option socket_options[] = {
729 {"SO_KEEPALIVE", SOL_SOCKET, SO_KEEPALIVE, 0, OPT_BOOL},
730 {"SO_REUSEADDR", SOL_SOCKET, SO_REUSEADDR, 0, OPT_BOOL},
731 {"SO_BROADCAST", SOL_SOCKET, SO_BROADCAST, 0, OPT_BOOL},
732 #ifdef TCP_NODELAY
733 {"TCP_NODELAY", IPPROTO_TCP, TCP_NODELAY, 0, OPT_BOOL},
734 #endif
735 #ifdef TCP_KEEPCNT
736 {"TCP_KEEPCNT", IPPROTO_TCP, TCP_KEEPCNT, 0, OPT_INT},
737 #endif
738 #ifdef TCP_KEEPIDLE
739 {"TCP_KEEPIDLE", IPPROTO_TCP, TCP_KEEPIDLE, 0, OPT_INT},
740 #endif
741 #ifdef TCP_KEEPINTVL
742 {"TCP_KEEPINTVL", IPPROTO_TCP, TCP_KEEPINTVL, 0, OPT_INT},
743 #endif
744 #ifdef IPTOS_LOWDELAY
745 {"IPTOS_LOWDELAY", IPPROTO_IP, IP_TOS, IPTOS_LOWDELAY, OPT_ON},
746 #endif
747 #ifdef IPTOS_THROUGHPUT
748 {"IPTOS_THROUGHPUT", IPPROTO_IP, IP_TOS, IPTOS_THROUGHPUT, OPT_ON},
749 #endif
750 #ifdef SO_REUSEPORT
751 {"SO_REUSEPORT", SOL_SOCKET, SO_REUSEPORT, 0, OPT_BOOL},
752 #endif
753 #ifdef SO_SNDBUF
754 {"SO_SNDBUF", SOL_SOCKET, SO_SNDBUF, 0, OPT_INT},
755 #endif
756 #ifdef SO_RCVBUF
757 {"SO_RCVBUF", SOL_SOCKET, SO_RCVBUF, 0, OPT_INT},
758 #endif
759 #ifdef SO_SNDLOWAT
760 {"SO_SNDLOWAT", SOL_SOCKET, SO_SNDLOWAT, 0, OPT_INT},
761 #endif
762 #ifdef SO_RCVLOWAT
763 {"SO_RCVLOWAT", SOL_SOCKET, SO_RCVLOWAT, 0, OPT_INT},
764 #endif
765 #ifdef SO_SNDTIMEO
766 {"SO_SNDTIMEO", SOL_SOCKET, SO_SNDTIMEO, 0, OPT_INT},
767 #endif
768 #ifdef SO_RCVTIMEO
769 {"SO_RCVTIMEO", SOL_SOCKET, SO_RCVTIMEO, 0, OPT_INT},
770 #endif
771 #ifdef TCP_FASTACK
772 {"TCP_FASTACK", IPPROTO_TCP, TCP_FASTACK, 0, OPT_INT},
773 #endif
774 #ifdef TCP_QUICKACK
775 {"TCP_QUICKACK", IPPROTO_TCP, TCP_QUICKACK, 0, OPT_BOOL},
776 #endif
777 #ifdef TCP_KEEPALIVE_THRESHOLD
778 {"TCP_KEEPALIVE_THRESHOLD", IPPROTO_TCP, TCP_KEEPALIVE_THRESHOLD, 0, OPT_INT},
779 #endif
780 #ifdef TCP_KEEPALIVE_ABORT_THRESHOLD
781 {"TCP_KEEPALIVE_ABORT_THRESHOLD", IPPROTO_TCP, TCP_KEEPALIVE_ABORT_THRESHOLD, 0, OPT_INT},
782 #endif
783 {NULL,0,0,0,0}};
785 /****************************************************************************
786 Print socket options.
787 ****************************************************************************/
789 static void print_socket_options(int s)
791 int value;
792 socklen_t vlen = 4;
793 const smb_socket_option *p = &socket_options[0];
795 /* wrapped in if statement to prevent streams
796 * leak in SCO Openserver 5.0 */
797 /* reported on samba-technical --jerry */
798 if ( DEBUGLEVEL >= 5 ) {
799 DEBUG(5,("Socket options:\n"));
800 for (; p->name != NULL; p++) {
801 if (getsockopt(s, p->level, p->option,
802 (void *)&value, &vlen) == -1) {
803 DEBUGADD(5,("\tCould not test socket option %s.\n",
804 p->name));
805 } else {
806 DEBUGADD(5,("\t%s = %d\n",
807 p->name,value));
813 /****************************************************************************
814 Set user socket options.
815 ****************************************************************************/
817 void set_socket_options(int fd, const char *options)
819 TALLOC_CTX *ctx = talloc_new(NULL);
820 char *tok;
822 while (next_token_talloc(ctx, &options, &tok," \t,")) {
823 int ret=0,i;
824 int value = 1;
825 char *p;
826 bool got_value = false;
828 if ((p = strchr_m(tok,'='))) {
829 *p = 0;
830 value = atoi(p+1);
831 got_value = true;
834 for (i=0;socket_options[i].name;i++)
835 if (strequal(socket_options[i].name,tok))
836 break;
838 if (!socket_options[i].name) {
839 DEBUG(0,("Unknown socket option %s\n",tok));
840 continue;
843 switch (socket_options[i].opttype) {
844 case OPT_BOOL:
845 case OPT_INT:
846 ret = setsockopt(fd,socket_options[i].level,
847 socket_options[i].option,
848 (char *)&value,sizeof(int));
849 break;
851 case OPT_ON:
852 if (got_value)
853 DEBUG(0,("syntax error - %s "
854 "does not take a value\n",tok));
857 int on = socket_options[i].value;
858 ret = setsockopt(fd,socket_options[i].level,
859 socket_options[i].option,
860 (char *)&on,sizeof(int));
862 break;
865 if (ret != 0) {
866 /* be aware that some systems like Solaris return
867 * EINVAL to a setsockopt() call when the client
868 * sent a RST previously - no need to worry */
869 DEBUG(2,("Failed to set socket option %s (Error %s)\n",
870 tok, strerror(errno) ));
874 TALLOC_FREE(ctx);
875 print_socket_options(fd);