winbind: Don't explicitly send "server_id" for ONLINESTATUS
[Samba.git] / lib / util / util_net.c
blobf0a30168342f2d17a80d3eb59c4fb4a19700e630
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 /* Ensure we're at least a valid sockaddr-storage. */
40 *pss = (struct sockaddr_storage) { .ss_family = AF_INET };
43 static char *normalize_ipv6_literal(const char *str, char *buf, size_t *_len)
45 #define IPv6_LITERAL_NET ".ipv6-literal.net"
46 const size_t llen = sizeof(IPv6_LITERAL_NET) - 1;
47 size_t len = *_len;
48 int cmp;
49 size_t i;
50 size_t idx_chars = 0;
51 size_t cnt_delimiter = 0;
52 size_t cnt_chars = 0;
54 if (len <= llen) {
55 return NULL;
58 /* ignore a trailing '.' */
59 if (str[len - 1] == '.') {
60 len -= 1;
63 len -= llen;
64 if (len >= INET6_ADDRSTRLEN) {
65 return NULL;
67 if (len < 2) {
68 return NULL;
71 cmp = strncasecmp(&str[len], IPv6_LITERAL_NET, llen);
72 if (cmp != 0) {
73 return NULL;
76 for (i = 0; i < len; i++) {
77 if (idx_chars != 0) {
78 break;
81 switch (str[i]) {
82 case '-':
83 buf[i] = ':';
84 cnt_chars = 0;
85 cnt_delimiter += 1;
86 break;
87 case 's':
88 buf[i] = SCOPE_DELIMITER;
89 idx_chars += 1;
90 break;
91 case '0':
92 case '1':
93 case '2':
94 case '3':
95 case '4':
96 case '5':
97 case '6':
98 case '7':
99 case '8':
100 case '9':
101 case 'a':
102 case 'A':
103 case 'b':
104 case 'B':
105 case 'c':
106 case 'C':
107 case 'd':
108 case 'D':
109 case 'e':
110 case 'E':
111 case 'f':
112 case 'F':
113 buf[i] = str[i];
114 cnt_chars += 1;
115 break;
116 default:
117 return NULL;
119 if (cnt_chars > 4) {
120 return NULL;
122 if (cnt_delimiter > 7) {
123 return NULL;
127 if (cnt_delimiter < 2) {
128 return NULL;
131 for (; idx_chars != 0 && i < len; i++) {
132 switch (str[i]) {
133 case SCOPE_DELIMITER:
134 case ':':
135 return NULL;
136 default:
137 buf[i] = str[i];
138 idx_chars += 1;
139 break;
143 if (idx_chars == 1) {
144 return NULL;
147 buf[i] = '\0';
148 *_len = len;
149 return buf;
153 * Wrap getaddrinfo...
155 bool interpret_string_addr_internal(struct addrinfo **ppres,
156 const char *str, int flags)
158 int ret;
159 struct addrinfo hints;
160 #if defined(HAVE_IPV6)
161 char addr[INET6_ADDRSTRLEN*2] = { 0, };
162 unsigned int scope_id = 0;
163 size_t len = strlen(str);
164 #endif
166 ZERO_STRUCT(hints);
168 /* By default make sure it supports TCP. */
169 hints.ai_socktype = SOCK_STREAM;
171 /* always try as a numeric host first. This prevents unnecessary name
172 * lookups, and also ensures we accept IPv6 addresses */
173 hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST;
175 #if defined(HAVE_IPV6)
176 if (len < sizeof(addr)) {
177 char *p = NULL;
179 p = normalize_ipv6_literal(str, addr, &len);
180 if (p != NULL) {
181 hints.ai_family = AF_INET6;
182 str = p;
186 if (strchr_m(str, ':')) {
187 char *p = strchr_m(str, SCOPE_DELIMITER);
190 * Cope with link-local.
191 * This is IP:v6:addr%ifname.
194 if (p && (p > str) && ((scope_id = if_nametoindex(p+1)) != 0)) {
195 /* Length of string we want to copy.
196 This is IP:v6:addr (removing the %ifname).
198 len = PTR_DIFF(p,str);
200 if (len+1 > sizeof(addr)) {
201 /* string+nul too long for array. */
202 return false;
204 if (str != addr) {
205 memcpy(addr, str, len);
207 addr[len] = '\0';
209 str = addr;
212 #endif
214 ret = getaddrinfo(str, NULL, &hints, ppres);
215 if (ret == 0) {
216 #if defined(HAVE_IPV6)
217 struct sockaddr_in6 *ps6 = NULL;
219 if (scope_id == 0) {
220 return true;
222 if (ppres == NULL) {
223 return true;
225 if ((*ppres) == NULL) {
226 return true;
228 if ((*ppres)->ai_addr->sa_family != AF_INET6) {
229 return true;
232 ps6 = (struct sockaddr_in6 *)(*ppres)->ai_addr;
234 if (IN6_IS_ADDR_LINKLOCAL(&ps6->sin6_addr) &&
235 ps6->sin6_scope_id == 0) {
236 ps6->sin6_scope_id = scope_id;
238 #endif
240 return true;
243 hints.ai_flags = flags;
245 /* Linux man page on getaddrinfo() says port will be
246 uninitialized when service string is NULL */
248 ret = getaddrinfo(str, NULL,
249 &hints,
250 ppres);
252 if (ret) {
253 DEBUG(3, ("interpret_string_addr_internal: "
254 "getaddrinfo failed for name %s (flags %d) [%s]\n",
255 str, flags, gai_strerror(ret)));
256 return false;
258 return true;
261 /*******************************************************************
262 Map a text hostname or IP address (IPv4 or IPv6) into a
263 struct sockaddr_storage. Takes a flag which allows it to
264 prefer an IPv4 address (needed for DC's).
265 ******************************************************************/
267 static bool interpret_string_addr_pref(struct sockaddr_storage *pss,
268 const char *str,
269 int flags,
270 bool prefer_ipv4)
272 struct addrinfo *res = NULL;
273 int int_flags;
275 zero_sockaddr(pss);
277 if (flags & AI_NUMERICHOST) {
278 int_flags = flags;
279 } else {
280 int_flags = flags|AI_ADDRCONFIG;
283 if (!interpret_string_addr_internal(&res, str, int_flags)) {
284 return false;
286 if (!res) {
287 return false;
290 if (prefer_ipv4) {
291 struct addrinfo *p;
293 for (p = res; p; p = p->ai_next) {
294 if (p->ai_family == AF_INET) {
295 memcpy(pss, p->ai_addr, p->ai_addrlen);
296 break;
299 if (p == NULL) {
300 /* Copy the first sockaddr. */
301 memcpy(pss, res->ai_addr, res->ai_addrlen);
303 } else {
304 /* Copy the first sockaddr. */
305 memcpy(pss, res->ai_addr, res->ai_addrlen);
308 freeaddrinfo(res);
309 return true;
312 /*******************************************************************
313 Map a text hostname or IP address (IPv4 or IPv6) into a
314 struct sockaddr_storage. Address agnostic version.
315 ******************************************************************/
317 bool interpret_string_addr(struct sockaddr_storage *pss,
318 const char *str,
319 int flags)
321 return interpret_string_addr_pref(pss,
322 str,
323 flags,
324 false);
327 /*******************************************************************
328 Map a text hostname or IP address (IPv4 or IPv6) into a
329 struct sockaddr_storage. Version that prefers IPv4.
330 ******************************************************************/
332 bool interpret_string_addr_prefer_ipv4(struct sockaddr_storage *pss,
333 const char *str,
334 int flags)
336 return interpret_string_addr_pref(pss,
337 str,
338 flags,
339 true);
343 * Interpret an internet address or name into an IP address in 4 byte form.
344 * RETURNS IN NETWORK BYTE ORDER (big endian).
347 uint32_t interpret_addr(const char *str)
349 uint32_t ret;
351 /* If it's in the form of an IP address then
352 * get the lib to interpret it */
353 if (is_ipaddress_v4(str)) {
354 struct in_addr dest;
356 if (inet_pton(AF_INET, str, &dest) <= 0) {
357 /* Error - this shouldn't happen ! */
358 DEBUG(0,("interpret_addr: inet_pton failed "
359 "host %s\n",
360 str));
361 return 0;
363 ret = dest.s_addr; /* NETWORK BYTE ORDER ! */
364 } else {
365 /* Otherwise assume it's a network name of some sort and use
366 getadddrinfo. */
367 struct addrinfo *res = NULL;
368 struct addrinfo *res_list = NULL;
369 if (!interpret_string_addr_internal(&res_list,
370 str,
371 AI_ADDRCONFIG)) {
372 DEBUG(3,("interpret_addr: Unknown host. %s\n",str));
373 return 0;
376 /* Find the first IPv4 address. */
377 for (res = res_list; res; res = res->ai_next) {
378 if (res->ai_family != AF_INET) {
379 continue;
381 if (res->ai_addr == NULL) {
382 continue;
384 break;
386 if(res == NULL) {
387 DEBUG(3,("interpret_addr: host address is "
388 "invalid for host %s\n",str));
389 if (res_list) {
390 freeaddrinfo(res_list);
392 return 0;
394 memcpy((char *)&ret,
395 &((struct sockaddr_in *)res->ai_addr)->sin_addr.s_addr,
396 sizeof(ret));
397 if (res_list) {
398 freeaddrinfo(res_list);
402 /* This is so bogus - all callers need fixing... JRA. */
403 if (ret == (uint32_t)-1) {
404 return 0;
407 return ret;
411 A convenient addition to interpret_addr().
413 _PUBLIC_ struct in_addr interpret_addr2(const char *str)
415 struct in_addr ret;
416 uint32_t a = interpret_addr(str);
417 ret.s_addr = a;
418 return ret;
422 Check if an IP is the 0.0.0.0.
425 _PUBLIC_ bool is_zero_ip_v4(struct in_addr ip)
427 return ip.s_addr == 0;
431 Are two IPs on the same subnet?
434 _PUBLIC_ bool same_net_v4(struct in_addr ip1, struct in_addr ip2, struct in_addr mask)
436 uint32_t net1,net2,nmask;
438 nmask = ntohl(mask.s_addr);
439 net1 = ntohl(ip1.s_addr);
440 net2 = ntohl(ip2.s_addr);
442 return((net1 & nmask) == (net2 & nmask));
446 * Return true if a string could be an IPv4 address.
449 bool is_ipaddress_v4(const char *str)
451 int ret = -1;
452 struct in_addr dest;
454 ret = inet_pton(AF_INET, str, &dest);
455 if (ret > 0) {
456 return true;
458 return false;
461 bool is_ipv6_literal(const char *str)
463 #if defined(HAVE_IPV6)
464 char buf[INET6_ADDRSTRLEN*2] = { 0, };
465 size_t len = strlen(str);
466 char *p = NULL;
468 if (len >= sizeof(buf)) {
469 return false;
472 p = normalize_ipv6_literal(str, buf, &len);
473 if (p == NULL) {
474 return false;
477 return true;
478 #else
479 return false;
480 #endif
484 * Return true if a string could be a IPv6 address.
487 bool is_ipaddress_v6(const char *str)
489 #if defined(HAVE_IPV6)
490 int ret = -1;
491 char *p = NULL;
492 char buf[INET6_ADDRSTRLEN] = { 0, };
493 size_t len;
494 const char *addr = str;
495 const char *idxs = NULL;
496 unsigned int idx = 0;
497 struct in6_addr ip6;
499 p = strchr_m(str, ':');
500 if (p == NULL) {
501 return is_ipv6_literal(str);
504 p = strchr_m(str, SCOPE_DELIMITER);
505 if (p && (p > str)) {
506 len = PTR_DIFF(p, str);
507 idxs = p + 1;
508 } else {
509 len = strlen(str);
512 if (len >= sizeof(buf)) {
513 return false;
515 if (idxs != NULL) {
516 strncpy(buf, str, len);
517 addr = buf;
521 * Cope with link-local.
522 * This is IP:v6:addr%ifidx.
524 if (idxs != NULL) {
525 char c;
527 ret = sscanf(idxs, "%5u%c", &idx, &c);
528 if (ret != 1) {
529 idx = 0;
532 if (idx > 0 && idx < UINT16_MAX) {
533 /* a valid index */
534 idxs = NULL;
539 * Cope with link-local.
540 * This is IP:v6:addr%ifname.
542 if (idxs != NULL) {
543 idx = if_nametoindex(idxs);
545 if (idx > 0) {
546 /* a valid index */
547 idxs = NULL;
551 if (idxs != NULL) {
552 return false;
555 ret = inet_pton(AF_INET6, addr, &ip6);
556 if (ret <= 0) {
557 return false;
560 return true;
561 #endif
562 return false;
566 * Return true if a string could be an IPv4 or IPv6 address.
569 bool is_ipaddress(const char *str)
571 return is_ipaddress_v4(str) || is_ipaddress_v6(str);
575 * Is a sockaddr a broadcast address ?
578 bool is_broadcast_addr(const struct sockaddr *pss)
580 #if defined(HAVE_IPV6)
581 if (pss->sa_family == AF_INET6) {
582 const struct in6_addr *sin6 =
583 &((const struct sockaddr_in6 *)pss)->sin6_addr;
584 return IN6_IS_ADDR_MULTICAST(sin6);
586 #endif
587 if (pss->sa_family == AF_INET) {
588 uint32_t addr =
589 ntohl(((const struct sockaddr_in *)pss)->sin_addr.s_addr);
590 return addr == INADDR_BROADCAST;
592 return false;
596 * Check if an IPv7 is 127.0.0.1
598 bool is_loopback_ip_v4(struct in_addr ip)
600 struct in_addr a;
601 a.s_addr = htonl(INADDR_LOOPBACK);
602 return(ip.s_addr == a.s_addr);
606 * Check if a struct sockaddr is the loopback address.
608 bool is_loopback_addr(const struct sockaddr *pss)
610 #if defined(HAVE_IPV6)
611 if (pss->sa_family == AF_INET6) {
612 const struct in6_addr *pin6 =
613 &((const struct sockaddr_in6 *)pss)->sin6_addr;
614 return IN6_IS_ADDR_LOOPBACK(pin6);
616 #endif
617 if (pss->sa_family == AF_INET) {
618 const struct in_addr *pin = &((const struct sockaddr_in *)pss)->sin_addr;
619 return is_loopback_ip_v4(*pin);
621 return false;
625 * Check if a struct sockaddr has an unspecified address.
627 bool is_zero_addr(const struct sockaddr_storage *pss)
629 #if defined(HAVE_IPV6)
630 if (pss->ss_family == AF_INET6) {
631 const struct in6_addr *pin6 =
632 &((const struct sockaddr_in6 *)pss)->sin6_addr;
633 return IN6_IS_ADDR_UNSPECIFIED(pin6);
635 #endif
636 if (pss->ss_family == AF_INET) {
637 const struct in_addr *pin = &((const struct sockaddr_in *)pss)->sin_addr;
638 return is_zero_ip_v4(*pin);
640 return false;
644 * Set an IP to 0.0.0.0.
646 void zero_ip_v4(struct in_addr *ip)
648 ZERO_STRUCTP(ip);
651 bool is_linklocal_addr(const struct sockaddr_storage *pss)
653 #ifdef HAVE_IPV6
654 if (pss->ss_family == AF_INET6) {
655 const struct in6_addr *pin6 =
656 &((const struct sockaddr_in6 *)pss)->sin6_addr;
657 return IN6_IS_ADDR_LINKLOCAL(pin6);
659 #endif
660 if (pss->ss_family == AF_INET) {
661 const struct in_addr *pin =
662 &((const struct sockaddr_in *)pss)->sin_addr;
663 struct in_addr ll_addr;
664 struct in_addr mask_addr;
666 /* 169.254.0.0/16, is link local, see RFC 3927 */
667 ll_addr.s_addr = 0xa9fe0000;
668 mask_addr.s_addr = 0xffff0000;
669 return same_net_v4(*pin, ll_addr, mask_addr);
671 return false;
675 * Convert an IPv4 struct in_addr to a struct sockaddr_storage.
677 void in_addr_to_sockaddr_storage(struct sockaddr_storage *ss,
678 struct in_addr ip)
680 struct sockaddr_in *sa = (struct sockaddr_in *)ss;
681 ZERO_STRUCTP(ss);
682 sa->sin_family = AF_INET;
683 sa->sin_addr = ip;
686 #if defined(HAVE_IPV6)
688 * Convert an IPv6 struct in_addr to a struct sockaddr_storage.
690 void in6_addr_to_sockaddr_storage(struct sockaddr_storage *ss,
691 struct in6_addr ip)
693 struct sockaddr_in6 *sa = (struct sockaddr_in6 *)ss;
694 memset(ss, '\0', sizeof(*ss));
695 sa->sin6_family = AF_INET6;
696 sa->sin6_addr = ip;
698 #endif
701 * Are two IPs on the same subnet?
703 bool same_net(const struct sockaddr *ip1,
704 const struct sockaddr *ip2,
705 const struct sockaddr *mask)
707 if (ip1->sa_family != ip2->sa_family) {
708 /* Never on the same net. */
709 return false;
712 #if defined(HAVE_IPV6)
713 if (ip1->sa_family == AF_INET6) {
714 struct sockaddr_in6 ip1_6 = *(const struct sockaddr_in6 *)ip1;
715 struct sockaddr_in6 ip2_6 = *(const struct sockaddr_in6 *)ip2;
716 struct sockaddr_in6 mask_6 = *(const struct sockaddr_in6 *)mask;
717 char *p1 = (char *)&ip1_6.sin6_addr;
718 char *p2 = (char *)&ip2_6.sin6_addr;
719 char *m = (char *)&mask_6.sin6_addr;
720 int i;
722 for (i = 0; i < sizeof(struct in6_addr); i++) {
723 *p1++ &= *m;
724 *p2++ &= *m;
725 m++;
727 return (memcmp(&ip1_6.sin6_addr,
728 &ip2_6.sin6_addr,
729 sizeof(struct in6_addr)) == 0);
731 #endif
732 if (ip1->sa_family == AF_INET) {
733 return same_net_v4(((const struct sockaddr_in *)ip1)->sin_addr,
734 ((const struct sockaddr_in *)ip2)->sin_addr,
735 ((const struct sockaddr_in *)mask)->sin_addr);
737 return false;
741 * Are two sockaddr 's the same family and address ? Ignore port etc.
744 bool sockaddr_equal(const struct sockaddr *ip1,
745 const struct sockaddr *ip2)
747 if (ip1->sa_family != ip2->sa_family) {
748 /* Never the same. */
749 return false;
752 #if defined(HAVE_IPV6)
753 if (ip1->sa_family == AF_INET6) {
754 return (memcmp(&((const struct sockaddr_in6 *)ip1)->sin6_addr,
755 &((const struct sockaddr_in6 *)ip2)->sin6_addr,
756 sizeof(struct in6_addr)) == 0);
758 #endif
759 if (ip1->sa_family == AF_INET) {
760 return (memcmp(&((const struct sockaddr_in *)ip1)->sin_addr,
761 &((const struct sockaddr_in *)ip2)->sin_addr,
762 sizeof(struct in_addr)) == 0);
764 return false;
768 * Is an IP address the INADDR_ANY or in6addr_any value ?
770 bool is_address_any(const struct sockaddr *psa)
772 #if defined(HAVE_IPV6)
773 if (psa->sa_family == AF_INET6) {
774 const struct sockaddr_in6 *si6 = (const struct sockaddr_in6 *)psa;
775 if (memcmp(&in6addr_any,
776 &si6->sin6_addr,
777 sizeof(in6addr_any)) == 0) {
778 return true;
780 return false;
782 #endif
783 if (psa->sa_family == AF_INET) {
784 const struct sockaddr_in *si = (const struct sockaddr_in *)psa;
785 if (si->sin_addr.s_addr == INADDR_ANY) {
786 return true;
788 return false;
790 return false;
793 void set_sockaddr_port(struct sockaddr *psa, uint16_t port)
795 #if defined(HAVE_IPV6)
796 if (psa->sa_family == AF_INET6) {
797 ((struct sockaddr_in6 *)psa)->sin6_port = htons(port);
799 #endif
800 if (psa->sa_family == AF_INET) {
801 ((struct sockaddr_in *)psa)->sin_port = htons(port);
806 /****************************************************************************
807 Get a port number in host byte order from a sockaddr_storage.
808 ****************************************************************************/
810 uint16_t get_sockaddr_port(const struct sockaddr_storage *pss)
812 uint16_t port = 0;
814 if (pss->ss_family != AF_INET) {
815 #if defined(HAVE_IPV6)
816 /* IPv6 */
817 const struct sockaddr_in6 *sa6 =
818 (const struct sockaddr_in6 *)pss;
819 port = ntohs(sa6->sin6_port);
820 #endif
821 } else {
822 const struct sockaddr_in *sa =
823 (const struct sockaddr_in *)pss;
824 port = ntohs(sa->sin_port);
826 return port;
829 /****************************************************************************
830 Print out an IPv4 or IPv6 address from a struct sockaddr_storage.
831 ****************************************************************************/
833 char *print_sockaddr_len(char *dest,
834 size_t destlen,
835 const struct sockaddr *psa,
836 socklen_t psalen)
838 if (destlen > 0) {
839 dest[0] = '\0';
841 (void)sys_getnameinfo(psa,
842 psalen,
843 dest, destlen,
844 NULL, 0,
845 NI_NUMERICHOST);
846 return dest;
849 /****************************************************************************
850 Print out an IPv4 or IPv6 address from a struct sockaddr_storage.
851 ****************************************************************************/
853 char *print_sockaddr(char *dest,
854 size_t destlen,
855 const struct sockaddr_storage *psa)
857 return print_sockaddr_len(dest, destlen, (const struct sockaddr *)psa,
858 sizeof(struct sockaddr_storage));
861 /****************************************************************************
862 Print out a canonical IPv4 or IPv6 address from a struct sockaddr_storage.
863 ****************************************************************************/
865 char *print_canonical_sockaddr(TALLOC_CTX *ctx,
866 const struct sockaddr_storage *pss)
868 char addr[INET6_ADDRSTRLEN];
869 char *dest = NULL;
870 int ret;
872 /* Linux getnameinfo() man pages says port is unitialized if
873 service name is NULL. */
875 ret = sys_getnameinfo((const struct sockaddr *)pss,
876 sizeof(struct sockaddr_storage),
877 addr, sizeof(addr),
878 NULL, 0,
879 NI_NUMERICHOST);
880 if (ret != 0) {
881 return NULL;
884 if (pss->ss_family != AF_INET) {
885 #if defined(HAVE_IPV6)
886 dest = talloc_asprintf(ctx, "[%s]", addr);
887 #else
888 return NULL;
889 #endif
890 } else {
891 dest = talloc_asprintf(ctx, "%s", addr);
894 return dest;
897 /****************************************************************************
898 Return the port number we've bound to on a socket.
899 ****************************************************************************/
901 int get_socket_port(int fd)
903 struct sockaddr_storage sa;
904 socklen_t length = sizeof(sa);
906 if (fd == -1) {
907 return -1;
910 if (getsockname(fd, (struct sockaddr *)&sa, &length) < 0) {
911 int level = (errno == ENOTCONN) ? 2 : 0;
912 DEBUG(level, ("getsockname failed. Error was %s\n",
913 strerror(errno)));
914 return -1;
917 #if defined(HAVE_IPV6)
918 if (sa.ss_family == AF_INET6) {
919 struct sockaddr_in6 *sa_in6 = (struct sockaddr_in6 *)&sa;
920 return ntohs(sa_in6->sin6_port);
922 #endif
923 if (sa.ss_family == AF_INET) {
924 struct sockaddr_in *sa_in = (struct sockaddr_in *)&sa;
925 return ntohs(sa_in->sin_port);
927 return -1;
930 /****************************************************************************
931 Return the string of an IP address (IPv4 or IPv6).
932 ****************************************************************************/
934 static const char *get_socket_addr(int fd, char *addr_buf, size_t addr_len)
936 struct sockaddr_storage sa;
937 socklen_t length = sizeof(sa);
939 /* Ok, returning a hard coded IPv4 address
940 * is bogus, but it's just as bogus as a
941 * zero IPv6 address. No good choice here.
944 if (strlcpy(addr_buf, "0.0.0.0", addr_len) >= addr_len) {
945 /* Truncate ! */
946 return NULL;
949 if (fd == -1) {
950 return addr_buf;
953 if (getsockname(fd, (struct sockaddr *)&sa, &length) < 0) {
954 DEBUG(0,("getsockname failed. Error was %s\n",
955 strerror(errno) ));
956 return addr_buf;
959 return print_sockaddr_len(addr_buf, addr_len, (struct sockaddr *)&sa, length);
962 const char *client_socket_addr(int fd, char *addr, size_t addr_len)
964 return get_socket_addr(fd, addr, addr_len);
968 enum SOCK_OPT_TYPES {OPT_BOOL,OPT_INT,OPT_ON};
970 typedef struct smb_socket_option {
971 const char *name;
972 int level;
973 int option;
974 int value;
975 int opttype;
976 } smb_socket_option;
978 static const smb_socket_option socket_options[] = {
979 {"SO_KEEPALIVE", SOL_SOCKET, SO_KEEPALIVE, 0, OPT_BOOL},
980 {"SO_REUSEADDR", SOL_SOCKET, SO_REUSEADDR, 0, OPT_BOOL},
981 {"SO_BROADCAST", SOL_SOCKET, SO_BROADCAST, 0, OPT_BOOL},
982 #ifdef TCP_NODELAY
983 {"TCP_NODELAY", IPPROTO_TCP, TCP_NODELAY, 0, OPT_BOOL},
984 #endif
985 #ifdef TCP_KEEPCNT
986 {"TCP_KEEPCNT", IPPROTO_TCP, TCP_KEEPCNT, 0, OPT_INT},
987 #endif
988 #ifdef TCP_KEEPIDLE
989 {"TCP_KEEPIDLE", IPPROTO_TCP, TCP_KEEPIDLE, 0, OPT_INT},
990 #endif
991 #ifdef TCP_KEEPINTVL
992 {"TCP_KEEPINTVL", IPPROTO_TCP, TCP_KEEPINTVL, 0, OPT_INT},
993 #endif
994 #ifdef IPTOS_LOWDELAY
995 {"IPTOS_LOWDELAY", IPPROTO_IP, IP_TOS, IPTOS_LOWDELAY, OPT_ON},
996 #endif
997 #ifdef IPTOS_THROUGHPUT
998 {"IPTOS_THROUGHPUT", IPPROTO_IP, IP_TOS, IPTOS_THROUGHPUT, OPT_ON},
999 #endif
1000 #ifdef SO_REUSEPORT
1001 {"SO_REUSEPORT", SOL_SOCKET, SO_REUSEPORT, 0, OPT_BOOL},
1002 #endif
1003 #ifdef SO_SNDBUF
1004 {"SO_SNDBUF", SOL_SOCKET, SO_SNDBUF, 0, OPT_INT},
1005 #endif
1006 #ifdef SO_RCVBUF
1007 {"SO_RCVBUF", SOL_SOCKET, SO_RCVBUF, 0, OPT_INT},
1008 #endif
1009 #ifdef SO_SNDLOWAT
1010 {"SO_SNDLOWAT", SOL_SOCKET, SO_SNDLOWAT, 0, OPT_INT},
1011 #endif
1012 #ifdef SO_RCVLOWAT
1013 {"SO_RCVLOWAT", SOL_SOCKET, SO_RCVLOWAT, 0, OPT_INT},
1014 #endif
1015 #ifdef SO_SNDTIMEO
1016 {"SO_SNDTIMEO", SOL_SOCKET, SO_SNDTIMEO, 0, OPT_INT},
1017 #endif
1018 #ifdef SO_RCVTIMEO
1019 {"SO_RCVTIMEO", SOL_SOCKET, SO_RCVTIMEO, 0, OPT_INT},
1020 #endif
1021 #ifdef TCP_FASTACK
1022 {"TCP_FASTACK", IPPROTO_TCP, TCP_FASTACK, 0, OPT_INT},
1023 #endif
1024 #ifdef TCP_QUICKACK
1025 {"TCP_QUICKACK", IPPROTO_TCP, TCP_QUICKACK, 0, OPT_BOOL},
1026 #endif
1027 #ifdef TCP_NODELAYACK
1028 {"TCP_NODELAYACK", IPPROTO_TCP, TCP_NODELAYACK, 0, OPT_BOOL},
1029 #endif
1030 #ifdef TCP_KEEPALIVE_THRESHOLD
1031 {"TCP_KEEPALIVE_THRESHOLD", IPPROTO_TCP, TCP_KEEPALIVE_THRESHOLD, 0, OPT_INT},
1032 #endif
1033 #ifdef TCP_KEEPALIVE_ABORT_THRESHOLD
1034 {"TCP_KEEPALIVE_ABORT_THRESHOLD", IPPROTO_TCP, TCP_KEEPALIVE_ABORT_THRESHOLD, 0, OPT_INT},
1035 #endif
1036 #ifdef TCP_DEFER_ACCEPT
1037 {"TCP_DEFER_ACCEPT", IPPROTO_TCP, TCP_DEFER_ACCEPT, 0, OPT_INT},
1038 #endif
1039 {NULL,0,0,0,0}};
1041 /****************************************************************************
1042 Print socket options.
1043 ****************************************************************************/
1045 static void print_socket_options(int s)
1047 int value;
1048 socklen_t vlen = 4;
1049 const smb_socket_option *p = &socket_options[0];
1051 /* wrapped in if statement to prevent streams
1052 * leak in SCO Openserver 5.0 */
1053 /* reported on samba-technical --jerry */
1054 if ( DEBUGLEVEL >= 5 ) {
1055 DEBUG(5,("Socket options:\n"));
1056 for (; p->name != NULL; p++) {
1057 if (getsockopt(s, p->level, p->option,
1058 (void *)&value, &vlen) == -1) {
1059 DEBUGADD(5,("\tCould not test socket option %s.\n",
1060 p->name));
1061 } else {
1062 DEBUGADD(5,("\t%s = %d\n",
1063 p->name,value));
1069 /****************************************************************************
1070 Set user socket options.
1071 ****************************************************************************/
1073 void set_socket_options(int fd, const char *options)
1075 TALLOC_CTX *ctx = talloc_new(NULL);
1076 char *tok;
1078 while (next_token_talloc(ctx, &options, &tok," \t,")) {
1079 int ret=0,i;
1080 int value = 1;
1081 char *p;
1082 bool got_value = false;
1084 if ((p = strchr_m(tok,'='))) {
1085 *p = 0;
1086 value = atoi(p+1);
1087 got_value = true;
1090 for (i=0;socket_options[i].name;i++)
1091 if (strequal(socket_options[i].name,tok))
1092 break;
1094 if (!socket_options[i].name) {
1095 DEBUG(0,("Unknown socket option %s\n",tok));
1096 continue;
1099 switch (socket_options[i].opttype) {
1100 case OPT_BOOL:
1101 case OPT_INT:
1102 ret = setsockopt(fd,socket_options[i].level,
1103 socket_options[i].option,
1104 (char *)&value,sizeof(int));
1105 break;
1107 case OPT_ON:
1108 if (got_value)
1109 DEBUG(0,("syntax error - %s "
1110 "does not take a value\n",tok));
1113 int on = socket_options[i].value;
1114 ret = setsockopt(fd,socket_options[i].level,
1115 socket_options[i].option,
1116 (char *)&on,sizeof(int));
1118 break;
1121 if (ret != 0) {
1122 /* be aware that some systems like Solaris return
1123 * EINVAL to a setsockopt() call when the client
1124 * sent a RST previously - no need to worry */
1125 DEBUG(2,("Failed to set socket option %s (Error %s)\n",
1126 tok, strerror(errno) ));
1130 TALLOC_FREE(ctx);
1131 print_socket_options(fd);