2 Unix SMB/CIFS implementation.
3 Samba utility functions
4 Copyright (C) Andrew Tridgell 1992-1998
5 Copyright (C) Tim Potter 2000-2001
6 Copyright (C) Jeremy Allison 1992-2007
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 /* the following 3 client_*() functions are nasty ways of allowing
25 some generic functions to get info that really should be hidden in
27 static int client_fd = -1;
28 /* What to print out on a client disconnect error. */
29 static char client_ip_string[INET6_ADDRSTRLEN];
31 /****************************************************************************
32 Return true if a string could be an IPv4 address.
33 ****************************************************************************/
35 bool is_ipaddress_v4(const char *str)
40 ret = inet_pton(AF_INET, str, &dest);
47 /****************************************************************************
48 Return true if a string could be an IPv4 or IPv6 address.
49 ****************************************************************************/
51 bool is_ipaddress(const char *str)
55 #if defined(HAVE_IPV6)
56 if (strchr_m(str, ':')) {
57 char addr[INET6_ADDRSTRLEN];
58 struct in6_addr dest6;
60 char *p = strchr_m(str, '%');
63 * Cope with link-local.
64 * This is IP:v6:addr%ifname.
67 if (p && (p > str) && (if_nametoindex(p+1) != 0)) {
69 MIN(PTR_DIFF(p,str)+1,
73 ret = inet_pton(AF_INET6, addr, &dest6);
79 return is_ipaddress_v4(str);
82 /****************************************************************************
83 Is a sockaddr_storage a broadcast address ?
84 ****************************************************************************/
86 bool is_broadcast_addr(const struct sockaddr_storage *pss)
88 #if defined(HAVE_IPV6)
89 if (pss->ss_family == AF_INET6) {
90 const struct in6_addr *sin6 =
91 &((const struct sockaddr_in6 *)pss)->sin6_addr;
92 return IN6_IS_ADDR_MULTICAST(sin6);
95 if (pss->ss_family == AF_INET) {
97 ntohl(((const struct sockaddr_in *)pss)->sin_addr.s_addr);
98 return addr == INADDR_BROADCAST;
103 /*******************************************************************
105 ******************************************************************/
107 static bool interpret_string_addr_internal(struct addrinfo **ppres,
108 const char *str, int flags)
111 struct addrinfo hints;
113 memset(&hints, '\0', sizeof(hints));
114 /* By default make sure it supports TCP. */
115 hints.ai_socktype = SOCK_STREAM;
116 hints.ai_flags = flags;
118 ret = getaddrinfo(str, NULL,
122 DEBUG(3,("interpret_string_addr_interal: getaddrinfo failed "
123 "for name %s [%s]\n",
125 gai_strerror(ret) ));
131 /****************************************************************************
132 Interpret an internet address or name into an IP address in 4 byte form.
133 RETURNS IN NETWORK BYTE ORDER (big endian).
134 ****************************************************************************/
136 uint32 interpret_addr(const char *str)
140 /* If it's in the form of an IP address then
141 * get the lib to interpret it */
142 if (is_ipaddress_v4(str)) {
145 if (inet_pton(AF_INET, str, &dest) <= 0) {
146 /* Error - this shouldn't happen ! */
147 DEBUG(0,("interpret_addr: inet_pton failed "
152 ret = dest.s_addr; /* NETWORK BYTE ORDER ! */
154 /* Otherwise assume it's a network name of some sort and use
156 struct addrinfo *res = NULL;
157 struct addrinfo *res_list = NULL;
158 if (!interpret_string_addr_internal(&res_list,
161 DEBUG(3,("interpret_addr: Unknown host. %s\n",str));
165 /* Find the first IPv4 address. */
166 for (res = res_list; res; res = res->ai_next) {
167 if (res->ai_family != AF_INET) {
170 if (res->ai_addr == NULL) {
176 DEBUG(3,("interpret_addr: host address is "
177 "invalid for host %s\n",str));
179 freeaddrinfo(res_list);
184 &((struct sockaddr_in *)res->ai_addr)->sin_addr.s_addr);
186 freeaddrinfo(res_list);
190 /* This is so bogus - all callers need fixing... JRA. */
191 if (ret == (uint32)-1) {
198 /*******************************************************************
199 A convenient addition to interpret_addr().
200 ******************************************************************/
202 struct in_addr *interpret_addr2(const char *str)
204 static struct in_addr ret;
205 uint32 a = interpret_addr(str);
210 /*******************************************************************
211 Map a text hostname or IP address (IPv4 or IPv6) into a
212 struct sockaddr_storage.
213 ******************************************************************/
215 bool interpret_string_addr(struct sockaddr_storage *pss,
219 char addr[INET6_ADDRSTRLEN];
220 struct addrinfo *res = NULL;
221 #if defined(HAVE_IPV6)
222 unsigned int scope_id = 0;
224 if (strchr_m(str, ':')) {
225 char *p = strchr_m(str, '%');
228 * Cope with link-local.
229 * This is IP:v6:addr%ifname.
232 if (p && (p > str) && ((scope_id = if_nametoindex(p+1)) != 0)) {
234 MIN(PTR_DIFF(p,str)+1,
243 if (!interpret_string_addr_internal(&res, str, flags|AI_ADDRCONFIG)) {
249 /* Copy the first sockaddr. */
250 memcpy(pss, res->ai_addr, res->ai_addrlen);
252 #if defined(HAVE_IPV6)
253 if (pss->ss_family == AF_INET6 && scope_id) {
254 struct sockaddr_in6 *ps6 = (struct sockaddr_in6 *)pss;
255 if (IN6_IS_ADDR_LINKLOCAL(&ps6->sin6_addr) &&
256 ps6->sin6_scope_id == 0) {
257 ps6->sin6_scope_id = scope_id;
266 /*******************************************************************
267 Check if an IPv7 is 127.0.0.1
268 ******************************************************************/
270 bool is_loopback_ip_v4(struct in_addr ip)
273 a.s_addr = htonl(INADDR_LOOPBACK);
274 return(ip.s_addr == a.s_addr);
277 /*******************************************************************
278 Check if a struct sockaddr_storage is the loopback address.
279 ******************************************************************/
281 bool is_loopback_addr(const struct sockaddr_storage *pss)
283 #if defined(HAVE_IPV6)
284 if (pss->ss_family == AF_INET6) {
285 struct in6_addr *pin6 =
286 &((struct sockaddr_in6 *)pss)->sin6_addr;
287 return IN6_IS_ADDR_LOOPBACK(pin6);
290 if (pss->ss_family == AF_INET) {
291 struct in_addr *pin = &((struct sockaddr_in *)pss)->sin_addr;
292 return is_loopback_ip_v4(*pin);
297 /*******************************************************************
298 Check if an IPv4 is 0.0.0.0.
299 ******************************************************************/
301 bool is_zero_ip_v4(struct in_addr ip)
304 putip((char *)&a,(char *)&ip);
308 /*******************************************************************
309 Check if a struct sockaddr_storage has an unspecified address.
310 ******************************************************************/
312 bool is_zero_addr(const struct sockaddr_storage *pss)
314 #if defined(HAVE_IPV6)
315 if (pss->ss_family == AF_INET6) {
316 struct in6_addr *pin6 =
317 &((struct sockaddr_in6 *)pss)->sin6_addr;
318 return IN6_IS_ADDR_UNSPECIFIED(pin6);
321 if (pss->ss_family == AF_INET) {
322 struct in_addr *pin = &((struct sockaddr_in *)pss)->sin_addr;
323 return is_zero_ip_v4(*pin);
328 /*******************************************************************
329 Set an IP to 0.0.0.0.
330 ******************************************************************/
332 void zero_ip_v4(struct in_addr *ip)
335 static struct in_addr ipzero;
338 ipzero = *interpret_addr2("0.0.0.0");
345 /*******************************************************************
346 Set an address to INADDR_ANY.
347 ******************************************************************/
349 void zero_addr(struct sockaddr_storage *pss)
351 memset(pss, '\0', sizeof(*pss));
352 /* Ensure we're at least a valid sockaddr-storage. */
353 pss->ss_family = AF_INET;
356 /*******************************************************************
357 Are two IPs on the same subnet - IPv4 version ?
358 ********************************************************************/
360 bool same_net_v4(struct in_addr ip1,struct in_addr ip2,struct in_addr mask)
362 uint32 net1,net2,nmask;
364 nmask = ntohl(mask.s_addr);
365 net1 = ntohl(ip1.s_addr);
366 net2 = ntohl(ip2.s_addr);
368 return((net1 & nmask) == (net2 & nmask));
371 /*******************************************************************
372 Convert an IPv4 struct in_addr to a struct sockaddr_storage.
373 ********************************************************************/
375 void in_addr_to_sockaddr_storage(struct sockaddr_storage *ss,
378 struct sockaddr_in *sa = (struct sockaddr_in *)ss;
379 memset(ss, '\0', sizeof(*ss));
380 ss->ss_family = AF_INET;
384 #if defined(HAVE_IPV6)
385 /*******************************************************************
386 Convert an IPv6 struct in_addr to a struct sockaddr_storage.
387 ********************************************************************/
389 void in6_addr_to_sockaddr_storage(struct sockaddr_storage *ss,
392 struct sockaddr_in6 *sa = (struct sockaddr_in6 *)ss;
393 memset(ss, '\0', sizeof(*ss));
394 ss->ss_family = AF_INET6;
399 /*******************************************************************
400 Are two IPs on the same subnet?
401 ********************************************************************/
403 bool same_net(const struct sockaddr_storage *ip1,
404 const struct sockaddr_storage *ip2,
405 const struct sockaddr_storage *mask)
407 if (ip1->ss_family != ip2->ss_family) {
408 /* Never on the same net. */
412 #if defined(HAVE_IPV6)
413 if (ip1->ss_family == AF_INET6) {
414 struct sockaddr_in6 ip1_6 = *(struct sockaddr_in6 *)ip1;
415 struct sockaddr_in6 ip2_6 = *(struct sockaddr_in6 *)ip2;
416 struct sockaddr_in6 mask_6 = *(struct sockaddr_in6 *)mask;
417 char *p1 = (char *)&ip1_6.sin6_addr;
418 char *p2 = (char *)&ip2_6.sin6_addr;
419 char *m = (char *)&mask_6.sin6_addr;
422 for (i = 0; i < sizeof(struct in6_addr); i++) {
427 return (memcmp(&ip1_6.sin6_addr,
429 sizeof(struct in6_addr)) == 0);
432 if (ip1->ss_family == AF_INET) {
433 return same_net_v4(((const struct sockaddr_in *)ip1)->sin_addr,
434 ((const struct sockaddr_in *)ip2)->sin_addr,
435 ((const struct sockaddr_in *)mask)->sin_addr);
440 /*******************************************************************
441 Are two sockaddr_storage's the same family and address ? Ignore port etc.
442 ********************************************************************/
444 bool addr_equal(const struct sockaddr_storage *ip1,
445 const struct sockaddr_storage *ip2)
447 if (ip1->ss_family != ip2->ss_family) {
448 /* Never the same. */
452 #if defined(HAVE_IPV6)
453 if (ip1->ss_family == AF_INET6) {
454 return (memcmp(&((const struct sockaddr_in6 *)ip1)->sin6_addr,
455 &((const struct sockaddr_in6 *)ip2)->sin6_addr,
456 sizeof(struct in6_addr)) == 0);
459 if (ip1->ss_family == AF_INET) {
460 return (memcmp(&((const struct sockaddr_in *)ip1)->sin_addr,
461 &((const struct sockaddr_in *)ip2)->sin_addr,
462 sizeof(struct in_addr)) == 0);
467 /****************************************************************************
468 Is an IP address the INADDR_ANY or in6addr_any value ?
469 ****************************************************************************/
471 bool is_address_any(const struct sockaddr_storage *psa)
473 #if defined(HAVE_IPV6)
474 if (psa->ss_family == AF_INET6) {
475 struct sockaddr_in6 *si6 = (struct sockaddr_in6 *)psa;
476 if (memcmp(&in6addr_any,
478 sizeof(in6addr_any)) == 0) {
484 if (psa->ss_family == AF_INET) {
485 struct sockaddr_in *si = (struct sockaddr_in *)psa;
486 if (si->sin_addr.s_addr == INADDR_ANY) {
494 /****************************************************************************
495 Print out an IPv4 or IPv6 address from a struct sockaddr_storage.
496 ****************************************************************************/
498 static char *print_sockaddr_len(char *dest,
500 const struct sockaddr_storage *psa,
506 (void)sys_getnameinfo((const struct sockaddr *)psa,
514 /****************************************************************************
515 Print out an IPv4 or IPv6 address from a struct sockaddr_storage.
516 ****************************************************************************/
518 char *print_sockaddr(char *dest,
520 const struct sockaddr_storage *psa)
522 return print_sockaddr_len(dest, destlen, psa,
523 sizeof(struct sockaddr_storage));
526 /****************************************************************************
527 Print out a canonical IPv4 or IPv6 address from a struct sockaddr_storage.
528 ****************************************************************************/
530 char *print_canonical_sockaddr(TALLOC_CTX *ctx,
531 const struct sockaddr_storage *pss)
533 char addr[INET6_ADDRSTRLEN];
537 ret = getnameinfo((const struct sockaddr *)pss,
538 sizeof(struct sockaddr_storage),
545 if (pss->ss_family != AF_INET) {
546 #if defined(HAVE_IPV6)
548 const struct sockaddr_in6 *sa6 =
549 (const struct sockaddr_in6 *)pss;
550 uint16_t port = ntohs(sa6->sin6_port);
553 dest = talloc_asprintf(ctx,
558 dest = talloc_asprintf(ctx,
566 const struct sockaddr_in *sa =
567 (const struct sockaddr_in *)pss;
568 uint16_t port = ntohs(sa->sin_port);
571 dest = talloc_asprintf(ctx,
576 dest = talloc_asprintf(ctx,
584 /****************************************************************************
585 Set the global client_fd variable.
586 ****************************************************************************/
588 void client_setfd(int fd)
591 safe_strcpy(client_ip_string,
592 get_peer_addr(client_fd),
593 sizeof(client_ip_string)-1);
596 /****************************************************************************
597 Return a static string of an IP address (IPv4 or IPv6).
598 ****************************************************************************/
600 static const char *get_socket_addr(int fd)
602 struct sockaddr_storage sa;
603 socklen_t length = sizeof(sa);
604 static char addr_buf[INET6_ADDRSTRLEN];
606 /* Ok, returning a hard coded IPv4 address
607 * is bogus, but it's just as bogus as a
608 * zero IPv6 address. No good choice here.
611 safe_strcpy(addr_buf, "0.0.0.0", sizeof(addr_buf)-1);
617 if (getsockname(fd, (struct sockaddr *)&sa, &length) < 0) {
618 DEBUG(0,("getsockname failed. Error was %s\n",
623 return print_sockaddr_len(addr_buf, sizeof(addr_buf), &sa, length);
626 /****************************************************************************
627 Return the port number we've bound to on a socket.
628 ****************************************************************************/
630 static int get_socket_port(int fd)
632 struct sockaddr_storage sa;
633 socklen_t length = sizeof(sa);
639 if (getsockname(fd, (struct sockaddr *)&sa, &length) < 0) {
640 DEBUG(0,("getpeername failed. Error was %s\n",
645 #if defined(HAVE_IPV6)
646 if (sa.ss_family == AF_INET6) {
647 return ntohs(((struct sockaddr_in6 *)&sa)->sin6_port);
650 if (sa.ss_family == AF_INET) {
651 return ntohs(((struct sockaddr_in *)&sa)->sin_port);
656 const char *client_name(void)
658 return get_peer_name(client_fd,false);
661 const char *client_addr(void)
663 return get_peer_addr(client_fd);
666 const char *client_socket_addr(void)
668 return get_socket_addr(client_fd);
671 int client_socket_port(void)
673 return get_socket_port(client_fd);
676 /****************************************************************************
677 Accessor functions to make thread-safe code easier later...
678 ****************************************************************************/
680 static enum smb_read_errors smb_read_error = SMB_READ_OK;
682 enum smb_read_errors get_smb_read_error(void)
684 return smb_read_error;
687 void set_smb_read_error(enum smb_read_errors newerr)
689 smb_read_error = newerr;
692 void cond_set_smb_read_error(enum smb_read_errors newerr)
694 if (smb_read_error == SMB_READ_OK) {
695 smb_read_error = newerr;
699 /****************************************************************************
700 Determine if a file descriptor is in fact a socket.
701 ****************************************************************************/
703 bool is_a_socket(int fd)
708 return(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&v, &l) == 0);
711 enum SOCK_OPT_TYPES {OPT_BOOL,OPT_INT,OPT_ON};
713 typedef struct smb_socket_option {
721 static const smb_socket_option socket_options[] = {
722 {"SO_KEEPALIVE", SOL_SOCKET, SO_KEEPALIVE, 0, OPT_BOOL},
723 {"SO_REUSEADDR", SOL_SOCKET, SO_REUSEADDR, 0, OPT_BOOL},
724 {"SO_BROADCAST", SOL_SOCKET, SO_BROADCAST, 0, OPT_BOOL},
726 {"TCP_NODELAY", IPPROTO_TCP, TCP_NODELAY, 0, OPT_BOOL},
729 {"TCP_KEEPCNT", IPPROTO_TCP, TCP_KEEPCNT, 0, OPT_INT},
732 {"TCP_KEEPIDLE", IPPROTO_TCP, TCP_KEEPIDLE, 0, OPT_INT},
735 {"TCP_KEEPINTVL", IPPROTO_TCP, TCP_KEEPINTVL, 0, OPT_INT},
737 #ifdef IPTOS_LOWDELAY
738 {"IPTOS_LOWDELAY", IPPROTO_IP, IP_TOS, IPTOS_LOWDELAY, OPT_ON},
740 #ifdef IPTOS_THROUGHPUT
741 {"IPTOS_THROUGHPUT", IPPROTO_IP, IP_TOS, IPTOS_THROUGHPUT, OPT_ON},
744 {"SO_REUSEPORT", SOL_SOCKET, SO_REUSEPORT, 0, OPT_BOOL},
747 {"SO_SNDBUF", SOL_SOCKET, SO_SNDBUF, 0, OPT_INT},
750 {"SO_RCVBUF", SOL_SOCKET, SO_RCVBUF, 0, OPT_INT},
753 {"SO_SNDLOWAT", SOL_SOCKET, SO_SNDLOWAT, 0, OPT_INT},
756 {"SO_RCVLOWAT", SOL_SOCKET, SO_RCVLOWAT, 0, OPT_INT},
759 {"SO_SNDTIMEO", SOL_SOCKET, SO_SNDTIMEO, 0, OPT_INT},
762 {"SO_RCVTIMEO", SOL_SOCKET, SO_RCVTIMEO, 0, OPT_INT},
765 {"TCP_FASTACK", IPPROTO_TCP, TCP_FASTACK, 0, OPT_INT},
769 /****************************************************************************
770 Print socket options.
771 ****************************************************************************/
773 static void print_socket_options(int s)
777 const smb_socket_option *p = &socket_options[0];
779 /* wrapped in if statement to prevent streams
780 * leak in SCO Openserver 5.0 */
781 /* reported on samba-technical --jerry */
782 if ( DEBUGLEVEL >= 5 ) {
783 for (; p->name != NULL; p++) {
784 if (getsockopt(s, p->level, p->option,
785 (void *)&value, &vlen) == -1) {
786 DEBUG(5,("Could not test socket option %s.\n",
789 DEBUG(5,("socket option %s = %d\n",
796 /****************************************************************************
797 Set user socket options.
798 ****************************************************************************/
800 void set_socket_options(int fd, const char *options)
804 while (next_token(&options,tok," \t,", sizeof(tok))) {
808 bool got_value = false;
810 if ((p = strchr_m(tok,'='))) {
816 for (i=0;socket_options[i].name;i++)
817 if (strequal(socket_options[i].name,tok))
820 if (!socket_options[i].name) {
821 DEBUG(0,("Unknown socket option %s\n",tok));
825 switch (socket_options[i].opttype) {
828 ret = setsockopt(fd,socket_options[i].level,
829 socket_options[i].option,
830 (char *)&value,sizeof(int));
835 DEBUG(0,("syntax error - %s "
836 "does not take a value\n",tok));
839 int on = socket_options[i].value;
840 ret = setsockopt(fd,socket_options[i].level,
841 socket_options[i].option,
842 (char *)&on,sizeof(int));
848 DEBUG(0,("Failed to set socket option %s (Error %s)\n",
849 tok, strerror(errno) ));
853 print_socket_options(fd);
856 /****************************************************************************
858 ****************************************************************************/
860 ssize_t read_udp_v4_socket(int fd,
863 struct sockaddr_storage *psa)
866 socklen_t socklen = sizeof(*psa);
867 struct sockaddr_in *si = (struct sockaddr_in *)psa;
869 memset((char *)psa,'\0',socklen);
871 ret = (ssize_t)sys_recvfrom(fd,buf,len,0,
872 (struct sockaddr *)psa,&socklen);
874 /* Don't print a low debug error for a non-blocking socket. */
875 if (errno == EAGAIN) {
876 DEBUG(10,("read_udp_v4_socket: returned EAGAIN\n"));
878 DEBUG(2,("read_udp_v4_socket: failed. errno=%s\n",
884 if (psa->ss_family != AF_INET) {
885 DEBUG(2,("read_udp_v4_socket: invalid address family %d "
886 "(not IPv4)\n", (int)psa->ss_family));
890 DEBUG(10,("read_udp_v4_socket: ip %s port %d read: %lu\n",
891 inet_ntoa(si->sin_addr),
893 (unsigned long)ret));
898 /****************************************************************************
899 Read data from a socket with a timout in msec.
900 mincount = if timeout, minimum to read before returning
901 maxcount = number to be read.
902 time_out = timeout in milliseconds
903 ****************************************************************************/
905 ssize_t read_socket_with_timeout(int fd,
909 unsigned int time_out)
915 struct timeval timeout;
917 /* just checking .... */
921 set_smb_read_error(SMB_READ_OK);
929 while (nread < mincnt) {
930 readret = sys_read(fd, buf + nread, maxcnt - nread);
933 DEBUG(5,("read_socket_with_timeout: "
934 "blocking read. EOF from client.\n"));
935 set_smb_read_error(SMB_READ_EOF);
940 if (fd == client_fd) {
941 /* Try and give an error message
942 * saying what client failed. */
943 DEBUG(0,("read_socket_with_timeout: "
944 "client %s read error = %s.\n",
948 DEBUG(0,("read_socket_with_timeout: "
949 "read error = %s.\n",
952 set_smb_read_error(SMB_READ_ERROR);
957 return((ssize_t)nread);
960 /* Most difficult - timeout read */
961 /* If this is ever called on a disk file and
962 mincnt is greater then the filesize then
963 system performance will suffer severely as
964 select always returns true on disk files */
966 /* Set initial timeout */
967 timeout.tv_sec = (time_t)(time_out / 1000);
968 timeout.tv_usec = (long)(1000 * (time_out % 1000));
970 for (nread=0; nread < mincnt; ) {
974 selrtn = sys_select_intr(fd+1,&fds,NULL,NULL,&timeout);
978 /* something is wrong. Maybe the socket is dead? */
979 if (fd == client_fd) {
980 /* Try and give an error message saying
981 * what client failed. */
982 DEBUG(0,("read_socket_with_timeout: timeout "
983 "read for client %s. select error = %s.\n",
984 client_ip_string, strerror(errno) ));
986 DEBUG(0,("read_socket_with_timeout: timeout "
987 "read. select error = %s.\n",
990 set_smb_read_error(SMB_READ_ERROR);
994 /* Did we timeout ? */
996 DEBUG(10,("read_socket_with_timeout: timeout read. "
997 "select timed out.\n"));
998 set_smb_read_error(SMB_READ_TIMEOUT);
1002 readret = sys_read(fd, buf+nread, maxcnt-nread);
1005 /* we got EOF on the file descriptor */
1006 DEBUG(5,("read_socket_with_timeout: timeout read. "
1007 "EOF from client.\n"));
1008 set_smb_read_error(SMB_READ_EOF);
1012 if (readret == -1) {
1013 /* the descriptor is probably dead */
1014 if (fd == client_fd) {
1015 /* Try and give an error message
1016 * saying what client failed. */
1017 DEBUG(0,("read_socket_with_timeout: timeout "
1018 "read to client %s. read error = %s.\n",
1019 client_ip_string, strerror(errno) ));
1021 DEBUG(0,("read_socket_with_timeout: timeout "
1022 "read. read error = %s.\n",
1025 set_smb_read_error(SMB_READ_ERROR);
1032 /* Return the number we got */
1033 return (ssize_t)nread;
1036 /****************************************************************************
1037 Read data from the client, reading exactly N bytes.
1038 ****************************************************************************/
1040 ssize_t read_data(int fd,char *buffer,size_t N)
1045 set_smb_read_error(SMB_READ_OK);
1048 ret = sys_read(fd,buffer + total,N - total);
1051 DEBUG(10,("read_data: read of %d returned 0. "
1053 (int)(N - total), strerror(errno) ));
1054 set_smb_read_error(SMB_READ_EOF);
1059 if (fd == client_fd) {
1060 /* Try and give an error message saying
1061 * what client failed. */
1062 DEBUG(0,("read_data: read failure for %d "
1063 "bytes to client %s. Error = %s\n",
1068 DEBUG(0,("read_data: read failure for %d. "
1073 set_smb_read_error(SMB_READ_ERROR);
1078 return (ssize_t)total;
1081 /****************************************************************************
1083 ****************************************************************************/
1085 ssize_t write_data(int fd, const char *buffer, size_t N)
1091 ret = sys_write(fd,buffer + total,N - total);
1094 if (fd == client_fd) {
1095 /* Try and give an error message saying
1096 * what client failed. */
1097 DEBUG(0,("write_data: write failure in "
1098 "writing to client %s. Error %s\n",
1099 client_ip_string, strerror(errno) ));
1101 DEBUG(0,("write_data: write failure. "
1102 "Error = %s\n", strerror(errno) ));
1113 return (ssize_t)total;
1116 /****************************************************************************
1117 Send a keepalive packet (rfc1002).
1118 ****************************************************************************/
1120 bool send_keepalive(int client)
1122 unsigned char buf[4];
1124 buf[0] = SMBkeepalive;
1125 buf[1] = buf[2] = buf[3] = 0;
1127 return(write_data(client,(char *)buf,4) == 4);
1130 /****************************************************************************
1131 Read 4 bytes of a smb packet and return the smb length of the packet.
1132 Store the result in the buffer.
1133 This version of the function will return a length of zero on receiving
1135 Timeout is in milliseconds.
1136 ****************************************************************************/
1138 ssize_t read_smb_length_return_keepalive(int fd,
1140 unsigned int timeout)
1148 ok = (read_socket_with_timeout(fd,inbuf,4,4,timeout)
1151 ok = (read_data(fd,inbuf,4) == 4);
1157 len = smb_len(inbuf);
1158 msg_type = CVAL(inbuf,0);
1160 if (msg_type == SMBkeepalive) {
1161 DEBUG(5,("Got keepalive packet\n"));
1165 DEBUG(10,("got smb length of %lu\n",(unsigned long)len));
1170 /****************************************************************************
1171 Read 4 bytes of a smb packet and return the smb length of the packet.
1172 Store the result in the buffer. This version of the function will
1173 never return a session keepalive (length of zero).
1174 Timeout is in milliseconds.
1175 ****************************************************************************/
1177 ssize_t read_smb_length(int fd, char *inbuf, unsigned int timeout)
1182 len = read_smb_length_return_keepalive(fd, inbuf, timeout);
1187 /* Ignore session keepalives. */
1188 if(CVAL(inbuf,0) != SMBkeepalive)
1192 DEBUG(10,("read_smb_length: got smb length of %lu\n",
1193 (unsigned long)len));
1198 /****************************************************************************
1199 Read an smb from a fd. Note that the buffer *MUST* be of size
1200 BUFFER_SIZE+SAFETY_MARGIN.
1201 The timeout is in milliseconds.
1202 This function will return on receipt of a session keepalive packet.
1203 maxlen is the max number of bytes to return, not including the 4 byte
1204 length. If zero it means BUFFER_SIZE+SAFETY_MARGIN limit.
1205 Doesn't check the MAC on signed packets.
1206 ****************************************************************************/
1208 ssize_t receive_smb_raw(int fd,
1210 unsigned int timeout,
1215 set_smb_read_error(SMB_READ_OK);
1217 len = read_smb_length_return_keepalive(fd,buffer,timeout);
1219 DEBUG(10,("receive_smb_raw: length < 0!\n"));
1222 * Correct fix. smb_read_error may have already been
1223 * set. Only set it here if not already set. Global
1224 * variables still suck :-). JRA.
1227 cond_set_smb_read_error(SMB_READ_ERROR);
1232 * A WRITEX with CAP_LARGE_WRITEX can be 64k worth of data plus 65 bytes
1233 * of header. Don't print the error if this fits.... JRA.
1236 if (len > (BUFFER_SIZE + LARGE_WRITEX_HDR_SIZE)) {
1237 DEBUG(0,("Invalid packet length! (%lu bytes).\n",
1238 (unsigned long)len));
1239 if (len > BUFFER_SIZE + (SAFETY_MARGIN/2)) {
1242 * Correct fix. smb_read_error may have already been
1243 * set. Only set it here if not already set. Global
1244 * variables still suck :-). JRA.
1247 cond_set_smb_read_error(SMB_READ_ERROR);
1254 len = MIN(len,maxlen);
1258 ret = read_socket_with_timeout(fd,
1264 ret = read_data(fd,buffer+4,len);
1268 cond_set_smb_read_error(SMB_READ_ERROR);
1272 /* not all of samba3 properly checks for packet-termination
1273 * of strings. This ensures that we don't run off into
1275 SSVAL(buffer+4,len, 0);
1281 /****************************************************************************
1282 Wrapper for receive_smb_raw().
1283 Checks the MAC on signed packets.
1284 ****************************************************************************/
1286 bool receive_smb(int fd, char *buffer, unsigned int timeout)
1288 if (receive_smb_raw(fd, buffer, timeout, 0) < 0) {
1292 /* Check the incoming SMB signature. */
1293 if (!srv_check_sign_mac(buffer, true)) {
1294 DEBUG(0, ("receive_smb: SMB Signature verification "
1295 "failed on incoming packet!\n"));
1296 if (get_smb_read_error() == 0) {
1297 smb_read_error = SMB_READ_BAD_SIG;
1305 /****************************************************************************
1306 Send an smb to a fd.
1307 ****************************************************************************/
1309 bool send_smb(int fd, char *buffer)
1315 /* Sign the outgoing packet if required. */
1316 srv_calculate_sign_mac(buffer);
1318 len = smb_len(buffer) + 4;
1320 while (nwritten < len) {
1321 ret = write_data(fd,buffer+nwritten,len - nwritten);
1323 DEBUG(0,("Error writing %d bytes to client. %d. (%s)\n",
1324 (int)len,(int)ret, strerror(errno) ));
1333 /****************************************************************************
1334 Open a socket of the specified type, port, and address for incoming data.
1335 ****************************************************************************/
1337 int open_socket_in(int type,
1340 const struct sockaddr_storage *psock,
1343 struct sockaddr_storage sock;
1345 socklen_t slen = sizeof(struct sockaddr_in);
1349 #if defined(HAVE_IPV6)
1350 if (sock.ss_family == AF_INET6) {
1351 ((struct sockaddr_in6 *)&sock)->sin6_port = htons(port);
1352 slen = sizeof(struct sockaddr_in6);
1355 if (sock.ss_family == AF_INET) {
1356 ((struct sockaddr_in *)&sock)->sin_port = htons(port);
1359 res = socket(sock.ss_family, type, 0 );
1362 dbgtext( "open_socket_in(): socket() call failed: " );
1363 dbgtext( "%s\n", strerror( errno ) );
1368 /* This block sets/clears the SO_REUSEADDR and possibly SO_REUSEPORT. */
1370 int val = rebind ? 1 : 0;
1371 if( setsockopt(res,SOL_SOCKET,SO_REUSEADDR,
1372 (char *)&val,sizeof(val)) == -1 ) {
1373 if( DEBUGLVL( dlevel ) ) {
1374 dbgtext( "open_socket_in(): setsockopt: " );
1375 dbgtext( "SO_REUSEADDR = %s ",
1376 val?"true":"false" );
1377 dbgtext( "on port %d failed ", port );
1378 dbgtext( "with error = %s\n", strerror(errno) );
1382 if( setsockopt(res,SOL_SOCKET,SO_REUSEPORT,
1383 (char *)&val,sizeof(val)) == -1 ) {
1384 if( DEBUGLVL( dlevel ) ) {
1385 dbgtext( "open_socket_in(): setsockopt: ");
1386 dbgtext( "SO_REUSEPORT = %s ",
1387 val?"true":"false");
1388 dbgtext( "on port %d failed ", port);
1389 dbgtext( "with error = %s\n", strerror(errno));
1392 #endif /* SO_REUSEPORT */
1395 /* now we've got a socket - we need to bind it */
1396 if (bind(res, (struct sockaddr *)&sock, slen) == -1 ) {
1397 if( DEBUGLVL(dlevel) && (port == SMB_PORT1 ||
1398 port == SMB_PORT2 || port == NMB_PORT) ) {
1399 char addr[INET6_ADDRSTRLEN];
1400 print_sockaddr(addr, sizeof(addr),
1402 dbgtext( "bind failed on port %d ", port);
1403 dbgtext( "socket_addr = %s.\n", addr);
1404 dbgtext( "Error = %s\n", strerror(errno));
1410 DEBUG( 10, ( "bind succeeded on port %d\n", port ) );
1414 /****************************************************************************
1415 Create an outgoing socket. timeout is in milliseconds.
1416 **************************************************************************/
1418 int open_socket_out(int type,
1419 const struct sockaddr_storage *pss,
1423 char addr[INET6_ADDRSTRLEN];
1424 struct sockaddr_storage sock_out = *pss;
1426 int connect_loop = 10;
1429 /* create a socket to write to */
1430 res = socket(pss->ss_family, type, 0);
1432 DEBUG(0,("socket error (%s)\n", strerror(errno)));
1436 if (type != SOCK_STREAM) {
1440 #if defined(HAVE_IPV6)
1441 if (pss->ss_family == AF_INET6) {
1442 struct sockaddr_in6 *psa6 = (struct sockaddr_in6 *)&sock_out;
1443 psa6->sin6_port = htons(port);
1444 if (psa6->sin6_scope_id == 0 &&
1445 IN6_IS_ADDR_LINKLOCAL(&psa6->sin6_addr)) {
1446 setup_linklocal_scope_id(&sock_out);
1450 if (pss->ss_family == AF_INET) {
1451 struct sockaddr_in *psa = (struct sockaddr_in *)&sock_out;
1452 psa->sin_port = htons(port);
1455 /* set it non-blocking */
1456 set_blocking(res,false);
1458 print_sockaddr(addr, sizeof(addr), &sock_out);
1459 DEBUG(3,("Connecting to %s at port %u\n",
1461 (unsigned int)port));
1463 /* and connect it to the destination */
1466 ret = connect(res,(struct sockaddr *)&sock_out,sizeof(sock_out));
1468 /* Some systems return EAGAIN when they mean EINPROGRESS */
1469 if (ret < 0 && (errno == EINPROGRESS || errno == EALREADY ||
1470 errno == EAGAIN) && (connect_loop < timeout) ) {
1471 smb_msleep(connect_loop);
1472 timeout -= connect_loop;
1473 connect_loop += increment;
1474 if (increment < 250) {
1475 /* After 8 rounds we end up at a max of 255 msec */
1481 if (ret < 0 && (errno == EINPROGRESS || errno == EALREADY ||
1483 DEBUG(1,("timeout connecting to %s:%u\n",
1485 (unsigned int)port));
1491 if (ret < 0 && errno == EISCONN) {
1498 DEBUG(2,("error connecting to %s:%d (%s)\n",
1506 /* set it blocking again */
1507 set_blocking(res,true);
1512 /****************************************************************************
1513 Create an outgoing TCP socket to any of the addrs. This is for
1514 simultaneous connects to port 445 and 139 of a host or even a variety
1515 of DC's all of which are equivalent for our purposes.
1516 **************************************************************************/
1518 bool open_any_socket_out(struct sockaddr_storage *addrs, int num_addrs,
1519 int timeout, int *fd_index, int *fd)
1521 int i, resulting_index, res;
1525 fd_set r_fds, wr_fds;
1529 int connect_loop = 10000; /* 10 milliseconds */
1531 timeout *= 1000; /* convert to microseconds */
1533 sockets = SMB_MALLOC_ARRAY(int, num_addrs);
1535 if (sockets == NULL)
1538 resulting_index = -1;
1540 for (i=0; i<num_addrs; i++)
1543 for (i=0; i<num_addrs; i++) {
1544 sockets[i] = socket(addrs[i].ss_family, SOCK_STREAM, 0);
1547 set_blocking(sockets[i], false);
1551 good_connect = false;
1553 for (i=0; i<num_addrs; i++) {
1555 if (sockets[i] == -1)
1558 if (connect(sockets[i], (struct sockaddr *)&(addrs[i]),
1559 sizeof(*addrs)) == 0) {
1560 /* Rather unlikely as we are non-blocking, but it
1561 * might actually happen. */
1562 resulting_index = i;
1566 if (errno == EINPROGRESS || errno == EALREADY ||
1570 errno == EAGAIN || errno == EINTR) {
1571 /* These are the error messages that something is
1573 good_connect = true;
1574 } else if (errno != 0) {
1575 /* There was a direct error */
1581 if (!good_connect) {
1582 /* All of the connect's resulted in real error conditions */
1586 /* Lets see if any of the connect attempts succeeded */
1592 for (i=0; i<num_addrs; i++) {
1593 if (sockets[i] == -1)
1595 FD_SET(sockets[i], &wr_fds);
1596 FD_SET(sockets[i], &r_fds);
1597 if (sockets[i]>maxfd)
1602 tv.tv_usec = connect_loop;
1604 res = sys_select_intr(maxfd+1, &r_fds, &wr_fds, NULL, &tv);
1612 for (i=0; i<num_addrs; i++) {
1614 if (sockets[i] == -1)
1617 /* Stevens, Network Programming says that if there's a
1618 * successful connect, the socket is only writable. Upon an
1619 * error, it's both readable and writable. */
1621 if (FD_ISSET(sockets[i], &r_fds) &&
1622 FD_ISSET(sockets[i], &wr_fds)) {
1623 /* readable and writable, so it's an error */
1629 if (!FD_ISSET(sockets[i], &r_fds) &&
1630 FD_ISSET(sockets[i], &wr_fds)) {
1631 /* Only writable, so it's connected */
1632 resulting_index = i;
1639 timeout -= connect_loop;
1642 connect_loop *= 1.5;
1643 if (connect_loop > timeout)
1644 connect_loop = timeout;
1648 for (i=0; i<num_addrs; i++) {
1649 if (i == resulting_index)
1651 if (sockets[i] >= 0)
1655 if (resulting_index >= 0) {
1656 *fd_index = resulting_index;
1657 *fd = sockets[*fd_index];
1658 set_blocking(*fd, true);
1663 return (resulting_index >= 0);
1665 /****************************************************************************
1666 Open a connected UDP socket to host on port
1667 **************************************************************************/
1669 int open_udp_socket(const char *host, int port)
1671 int type = SOCK_DGRAM;
1672 struct sockaddr_in sock_out;
1674 struct in_addr *addr;
1676 addr = interpret_addr2(host);
1678 res = socket(PF_INET, type, 0);
1683 memset((char *)&sock_out,'\0',sizeof(sock_out));
1684 putip((char *)&sock_out.sin_addr,(char *)addr);
1685 sock_out.sin_port = htons(port);
1686 sock_out.sin_family = PF_INET;
1688 if (connect(res,(struct sockaddr *)&sock_out,sizeof(sock_out))) {
1696 /*******************************************************************
1697 Return the IP addr of the remote end of a socket as a string.
1698 Optionally return the struct sockaddr_storage.
1699 ******************************************************************/
1701 static const char *get_peer_addr_internal(int fd,
1702 struct sockaddr_storage *pss,
1705 struct sockaddr_storage ss;
1706 socklen_t length = sizeof(ss);
1707 static char addr_buf[INET6_ADDRSTRLEN];
1709 safe_strcpy(addr_buf,"0.0.0.0",sizeof(addr_buf)-1);
1718 if (plength == NULL) {
1722 if (getpeername(fd, (struct sockaddr *)pss, plength) < 0) {
1723 DEBUG(0,("getpeername failed. Error was %s\n",
1728 print_sockaddr_len(addr_buf,
1736 /*******************************************************************
1737 Matchname - determine if host name matches IP address. Used to
1738 confirm a hostname lookup to prevent spoof attacks.
1739 ******************************************************************/
1741 static bool matchname(const char *remotehost,
1742 const struct sockaddr_storage *pss,
1745 struct addrinfo *res = NULL;
1746 struct addrinfo *ailist = NULL;
1747 char addr_buf[INET6_ADDRSTRLEN];
1748 bool ret = interpret_string_addr_internal(&ailist,
1750 AI_ADDRCONFIG|AI_CANONNAME);
1752 if (!ret || ailist == NULL) {
1753 DEBUG(3,("matchname: getaddrinfo failed for "
1756 gai_strerror(ret) ));
1761 * Make sure that getaddrinfo() returns the "correct" host name.
1764 if (ailist->ai_canonname == NULL ||
1765 (!strequal(remotehost, ailist->ai_canonname) &&
1766 !strequal(remotehost, "localhost"))) {
1767 DEBUG(0,("matchname: host name/name mismatch: %s != %s\n",
1769 ailist->ai_canonname ?
1770 ailist->ai_canonname : "(NULL)"));
1771 freeaddrinfo(ailist);
1775 /* Look up the host address in the address list we just got. */
1776 for (res = ailist; res; res = res->ai_next) {
1777 if (!res->ai_addr) {
1780 if (addr_equal((const struct sockaddr_storage *)res->ai_addr,
1782 freeaddrinfo(ailist);
1788 * The host name does not map to the original host address. Perhaps
1789 * someone has compromised a name server. More likely someone botched
1790 * it, but that could be dangerous, too.
1793 DEBUG(0,("matchname: host name/address mismatch: %s != %s\n",
1794 print_sockaddr_len(addr_buf,
1798 ailist->ai_canonname ? ailist->ai_canonname : "(NULL)"));
1801 freeaddrinfo(ailist);
1806 /*******************************************************************
1807 Return the DNS name of the remote end of a socket.
1808 ******************************************************************/
1810 const char *get_peer_name(int fd, bool force_lookup)
1812 static fstring addr_buf;
1813 static pstring name_buf;
1814 struct sockaddr_storage ss;
1815 socklen_t length = sizeof(ss);
1820 /* reverse lookups can be *very* expensive, and in many
1821 situations won't work because many networks don't link dhcp
1822 with dns. To avoid the delay we avoid the lookup if
1824 if (!lp_hostname_lookups() && (force_lookup == false)) {
1825 return get_peer_addr(fd);
1828 p = get_peer_addr_internal(fd, &ss, &length);
1830 /* it might be the same as the last one - save some DNS work */
1831 if (strcmp(p, addr_buf) == 0) {
1835 pstrcpy(name_buf,"UNKNOWN");
1840 fstrcpy(addr_buf, p);
1842 /* Look up the remote host name. */
1843 ret = getnameinfo((struct sockaddr *)&ss,
1852 DEBUG(1,("get_peer_name: getnameinfo failed "
1853 "for %s with error %s\n",
1855 gai_strerror(ret)));
1856 pstrcpy(name_buf, p);
1858 if (!matchname(name_buf, &ss, length)) {
1859 DEBUG(0,("Matchname failed on %s %s\n",name_buf,p));
1860 pstrcpy(name_buf,"UNKNOWN");
1864 /* can't pass the same source and dest strings in when you
1865 use --enable-developer or the clobber_region() call will
1868 pstrcpy(tmp_name, name_buf );
1869 alpha_strcpy(name_buf, tmp_name, "_-.", sizeof(name_buf));
1870 if (strstr(name_buf,"..")) {
1871 pstrcpy(name_buf, "UNKNOWN");
1877 /*******************************************************************
1878 Return the IP addr of the remote end of a socket as a string.
1879 ******************************************************************/
1881 const char *get_peer_addr(int fd)
1883 return get_peer_addr_internal(fd, NULL, NULL);
1886 /*******************************************************************
1887 Create protected unix domain socket.
1889 Some unixes cannot set permissions on a ux-dom-sock, so we
1890 have to make sure that the directory contains the protection
1891 permissions instead.
1892 ******************************************************************/
1894 int create_pipe_sock(const char *socket_dir,
1895 const char *socket_name,
1898 #ifdef HAVE_UNIXSOCKET
1899 struct sockaddr_un sunaddr;
1905 old_umask = umask(0);
1907 /* Create the socket directory or reuse the existing one */
1909 if (lstat(socket_dir, &st) == -1) {
1910 if (errno == ENOENT) {
1911 /* Create directory */
1912 if (mkdir(socket_dir, dir_perms) == -1) {
1913 DEBUG(0, ("error creating socket directory "
1914 "%s: %s\n", socket_dir,
1919 DEBUG(0, ("lstat failed on socket directory %s: %s\n",
1920 socket_dir, strerror(errno)));
1924 /* Check ownership and permission on existing directory */
1925 if (!S_ISDIR(st.st_mode)) {
1926 DEBUG(0, ("socket directory %s isn't a directory\n",
1930 if ((st.st_uid != sec_initial_uid()) ||
1931 ((st.st_mode & 0777) != dir_perms)) {
1932 DEBUG(0, ("invalid permissions on socket directory "
1933 "%s\n", socket_dir));
1938 /* Create the socket file */
1940 sock = socket(AF_UNIX, SOCK_STREAM, 0);
1947 pstr_sprintf(path, "%s/%s", socket_dir, socket_name);
1950 memset(&sunaddr, 0, sizeof(sunaddr));
1951 sunaddr.sun_family = AF_UNIX;
1952 safe_strcpy(sunaddr.sun_path, path, sizeof(sunaddr.sun_path)-1);
1954 if (bind(sock, (struct sockaddr *)&sunaddr, sizeof(sunaddr)) == -1) {
1955 DEBUG(0, ("bind failed on pipe socket %s: %s\n", path,
1960 if (listen(sock, 5) == -1) {
1961 DEBUG(0, ("listen failed on pipe socket %s: %s\n", path,
1977 DEBUG(0, ("create_pipe_sock: No Unix sockets on this system\n"));
1979 #endif /* HAVE_UNIXSOCKET */
1982 /****************************************************************************
1983 Get my own canonical name, including domain.
1984 ****************************************************************************/
1986 bool get_mydnsfullname(fstring my_dnsname)
1988 static fstring dnshostname;
1990 if (!*dnshostname) {
1991 struct addrinfo *res = NULL;
1994 /* get my host name */
1995 if (gethostname(dnshostname, sizeof(dnshostname)) == -1) {
1996 *dnshostname = '\0';
1997 DEBUG(0,("get_mydnsfullname: gethostname failed\n"));
2001 /* Ensure null termination. */
2002 dnshostname[sizeof(dnshostname)-1] = '\0';
2004 ret = interpret_string_addr_internal(&res,
2006 AI_ADDRCONFIG|AI_CANONNAME);
2008 if (!ret || res == NULL) {
2009 DEBUG(3,("get_mydnsfullname: getaddrinfo failed for "
2012 gai_strerror(ret) ));
2017 * Make sure that getaddrinfo() returns the "correct" host name.
2020 if (res->ai_canonname == NULL) {
2021 DEBUG(3,("get_mydnsfullname: failed to get "
2022 "canonical name for %s\n",
2029 fstrcpy(dnshostname, res->ai_canonname);
2032 fstrcpy(my_dnsname, dnshostname);
2036 /************************************************************
2038 ************************************************************/
2040 bool is_myname_or_ipaddr(const char *s)
2042 fstring name, dnsname;
2049 /* Santize the string from '\\name' */
2052 servername = strrchr_m(name, '\\' );
2059 /* Optimize for the common case */
2060 if (strequal(servername, global_myname())) {
2064 /* Check for an alias */
2065 if (is_myname(servername)) {
2069 /* Check for loopback */
2070 if (strequal(servername, "127.0.0.1") ||
2071 strequal(servername, "::1")) {
2075 if (strequal(servername, "localhost")) {
2079 /* Maybe it's my dns name */
2080 if (get_mydnsfullname(dnsname)) {
2081 if (strequal(servername, dnsname)) {
2086 /* Handle possible CNAME records - convert to an IP addr. */
2087 if (!is_ipaddress(servername)) {
2088 /* Use DNS to resolve the name, but only the first address */
2089 struct sockaddr_storage ss;
2090 if (interpret_string_addr(&ss, servername,0)) {
2091 print_sockaddr(name,
2098 /* Maybe its an IP address? */
2099 if (is_ipaddress(servername)) {
2100 struct sockaddr_storage ss;
2101 struct iface_struct nics[MAX_INTERFACES];
2104 if (!interpret_string_addr(&ss, servername, AI_NUMERICHOST)) {
2108 if (is_zero_addr(&ss) || is_loopback_addr(&ss)) {
2112 n = get_interfaces(nics, MAX_INTERFACES);
2113 for (i=0; i<n; i++) {
2114 if (addr_equal(&nics[i].ip, &ss)) {