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 /****************************************************************************
25 Return true if a string could be an IPv4 address.
26 ****************************************************************************/
28 bool is_ipaddress_v4(const char *str)
33 ret = inet_pton(AF_INET, str, &dest);
40 /****************************************************************************
41 Return true if a string could be an IPv4 or IPv6 address.
42 ****************************************************************************/
44 bool is_ipaddress(const char *str)
48 #if defined(HAVE_IPV6)
49 if (strchr_m(str, ':')) {
50 char addr[INET6_ADDRSTRLEN];
51 struct in6_addr dest6;
53 char *p = strchr_m(str, '%');
56 * Cope with link-local.
57 * This is IP:v6:addr%ifname.
60 if (p && (p > str) && (if_nametoindex(p+1) != 0)) {
62 MIN(PTR_DIFF(p,str)+1,
66 ret = inet_pton(AF_INET6, addr, &dest6);
72 return is_ipaddress_v4(str);
75 /****************************************************************************
76 Is a sockaddr_storage a broadcast address ?
77 ****************************************************************************/
79 bool is_broadcast_addr(const struct sockaddr_storage *pss)
81 #if defined(HAVE_IPV6)
82 if (pss->ss_family == AF_INET6) {
83 const struct in6_addr *sin6 =
84 &((const struct sockaddr_in6 *)pss)->sin6_addr;
85 return IN6_IS_ADDR_MULTICAST(sin6);
88 if (pss->ss_family == AF_INET) {
90 ntohl(((const struct sockaddr_in *)pss)->sin_addr.s_addr);
91 return addr == INADDR_BROADCAST;
96 /*******************************************************************
98 ******************************************************************/
100 static bool interpret_string_addr_internal(struct addrinfo **ppres,
101 const char *str, int flags)
104 struct addrinfo hints;
106 memset(&hints, '\0', sizeof(hints));
107 /* By default make sure it supports TCP. */
108 hints.ai_socktype = SOCK_STREAM;
109 hints.ai_flags = flags;
111 ret = getaddrinfo(str, NULL,
115 DEBUG(3,("interpret_string_addr_interal: getaddrinfo failed "
116 "for name %s [%s]\n",
118 gai_strerror(ret) ));
124 /****************************************************************************
125 Interpret an internet address or name into an IP address in 4 byte form.
126 RETURNS IN NETWORK BYTE ORDER (big endian).
127 ****************************************************************************/
129 uint32 interpret_addr(const char *str)
133 /* If it's in the form of an IP address then
134 * get the lib to interpret it */
135 if (is_ipaddress_v4(str)) {
138 if (inet_pton(AF_INET, str, &dest) <= 0) {
139 /* Error - this shouldn't happen ! */
140 DEBUG(0,("interpret_addr: inet_pton failed "
145 ret = dest.s_addr; /* NETWORK BYTE ORDER ! */
147 /* Otherwise assume it's a network name of some sort and use
149 struct addrinfo *res = NULL;
150 struct addrinfo *res_list = NULL;
151 if (!interpret_string_addr_internal(&res_list,
154 DEBUG(3,("interpret_addr: Unknown host. %s\n",str));
158 /* Find the first IPv4 address. */
159 for (res = res_list; res; res = res->ai_next) {
160 if (res->ai_family != AF_INET) {
163 if (res->ai_addr == NULL) {
169 DEBUG(3,("interpret_addr: host address is "
170 "invalid for host %s\n",str));
172 freeaddrinfo(res_list);
177 &((struct sockaddr_in *)res->ai_addr)->sin_addr.s_addr);
179 freeaddrinfo(res_list);
183 /* This is so bogus - all callers need fixing... JRA. */
184 if (ret == (uint32)-1) {
191 /*******************************************************************
192 A convenient addition to interpret_addr().
193 ******************************************************************/
195 struct in_addr *interpret_addr2(struct in_addr *ip, const char *str)
197 uint32 a = interpret_addr(str);
202 /*******************************************************************
203 Map a text hostname or IP address (IPv4 or IPv6) into a
204 struct sockaddr_storage.
205 ******************************************************************/
207 bool interpret_string_addr(struct sockaddr_storage *pss,
211 char addr[INET6_ADDRSTRLEN];
212 struct addrinfo *res = NULL;
213 #if defined(HAVE_IPV6)
214 unsigned int scope_id = 0;
216 if (strchr_m(str, ':')) {
217 char *p = strchr_m(str, '%');
220 * Cope with link-local.
221 * This is IP:v6:addr%ifname.
224 if (p && (p > str) && ((scope_id = if_nametoindex(p+1)) != 0)) {
226 MIN(PTR_DIFF(p,str)+1,
235 if (!interpret_string_addr_internal(&res, str, flags|AI_ADDRCONFIG)) {
241 /* Copy the first sockaddr. */
242 memcpy(pss, res->ai_addr, res->ai_addrlen);
244 #if defined(HAVE_IPV6)
245 if (pss->ss_family == AF_INET6 && scope_id) {
246 struct sockaddr_in6 *ps6 = (struct sockaddr_in6 *)pss;
247 if (IN6_IS_ADDR_LINKLOCAL(&ps6->sin6_addr) &&
248 ps6->sin6_scope_id == 0) {
249 ps6->sin6_scope_id = scope_id;
258 /*******************************************************************
259 Check if an IPv7 is 127.0.0.1
260 ******************************************************************/
262 bool is_loopback_ip_v4(struct in_addr ip)
265 a.s_addr = htonl(INADDR_LOOPBACK);
266 return(ip.s_addr == a.s_addr);
269 /*******************************************************************
270 Check if a struct sockaddr_storage is the loopback address.
271 ******************************************************************/
273 bool is_loopback_addr(const struct sockaddr_storage *pss)
275 #if defined(HAVE_IPV6)
276 if (pss->ss_family == AF_INET6) {
277 struct in6_addr *pin6 =
278 &((struct sockaddr_in6 *)pss)->sin6_addr;
279 return IN6_IS_ADDR_LOOPBACK(pin6);
282 if (pss->ss_family == AF_INET) {
283 struct in_addr *pin = &((struct sockaddr_in *)pss)->sin_addr;
284 return is_loopback_ip_v4(*pin);
289 /*******************************************************************
290 Check if an IPv4 is 0.0.0.0.
291 ******************************************************************/
293 bool is_zero_ip_v4(struct in_addr ip)
296 putip((char *)&a,(char *)&ip);
300 /*******************************************************************
301 Check if a struct sockaddr_storage has an unspecified address.
302 ******************************************************************/
304 bool is_zero_addr(const struct sockaddr_storage *pss)
306 #if defined(HAVE_IPV6)
307 if (pss->ss_family == AF_INET6) {
308 struct in6_addr *pin6 =
309 &((struct sockaddr_in6 *)pss)->sin6_addr;
310 return IN6_IS_ADDR_UNSPECIFIED(pin6);
313 if (pss->ss_family == AF_INET) {
314 struct in_addr *pin = &((struct sockaddr_in *)pss)->sin_addr;
315 return is_zero_ip_v4(*pin);
320 /*******************************************************************
321 Set an IP to 0.0.0.0.
322 ******************************************************************/
324 void zero_ip_v4(struct in_addr *ip)
326 memset(ip, '\0', sizeof(struct in_addr));
329 /*******************************************************************
330 Set an address to INADDR_ANY.
331 ******************************************************************/
333 void zero_addr(struct sockaddr_storage *pss)
335 memset(pss, '\0', sizeof(*pss));
336 /* Ensure we're at least a valid sockaddr-storage. */
337 pss->ss_family = AF_INET;
340 /*******************************************************************
341 Are two IPs on the same subnet - IPv4 version ?
342 ********************************************************************/
344 bool same_net_v4(struct in_addr ip1,struct in_addr ip2,struct in_addr mask)
346 uint32 net1,net2,nmask;
348 nmask = ntohl(mask.s_addr);
349 net1 = ntohl(ip1.s_addr);
350 net2 = ntohl(ip2.s_addr);
352 return((net1 & nmask) == (net2 & nmask));
355 /*******************************************************************
356 Convert an IPv4 struct in_addr to a struct sockaddr_storage.
357 ********************************************************************/
359 void in_addr_to_sockaddr_storage(struct sockaddr_storage *ss,
362 struct sockaddr_in *sa = (struct sockaddr_in *)ss;
363 memset(ss, '\0', sizeof(*ss));
364 ss->ss_family = AF_INET;
368 #if defined(HAVE_IPV6)
369 /*******************************************************************
370 Convert an IPv6 struct in_addr to a struct sockaddr_storage.
371 ********************************************************************/
373 void in6_addr_to_sockaddr_storage(struct sockaddr_storage *ss,
376 struct sockaddr_in6 *sa = (struct sockaddr_in6 *)ss;
377 memset(ss, '\0', sizeof(*ss));
378 ss->ss_family = AF_INET6;
383 /*******************************************************************
384 Are two IPs on the same subnet?
385 ********************************************************************/
387 bool same_net(const struct sockaddr_storage *ip1,
388 const struct sockaddr_storage *ip2,
389 const struct sockaddr_storage *mask)
391 if (ip1->ss_family != ip2->ss_family) {
392 /* Never on the same net. */
396 #if defined(HAVE_IPV6)
397 if (ip1->ss_family == AF_INET6) {
398 struct sockaddr_in6 ip1_6 = *(struct sockaddr_in6 *)ip1;
399 struct sockaddr_in6 ip2_6 = *(struct sockaddr_in6 *)ip2;
400 struct sockaddr_in6 mask_6 = *(struct sockaddr_in6 *)mask;
401 char *p1 = (char *)&ip1_6.sin6_addr;
402 char *p2 = (char *)&ip2_6.sin6_addr;
403 char *m = (char *)&mask_6.sin6_addr;
406 for (i = 0; i < sizeof(struct in6_addr); i++) {
411 return (memcmp(&ip1_6.sin6_addr,
413 sizeof(struct in6_addr)) == 0);
416 if (ip1->ss_family == AF_INET) {
417 return same_net_v4(((const struct sockaddr_in *)ip1)->sin_addr,
418 ((const struct sockaddr_in *)ip2)->sin_addr,
419 ((const struct sockaddr_in *)mask)->sin_addr);
424 /*******************************************************************
425 Are two sockaddr_storage's the same family and address ? Ignore port etc.
426 ********************************************************************/
428 bool addr_equal(const struct sockaddr_storage *ip1,
429 const struct sockaddr_storage *ip2)
431 if (ip1->ss_family != ip2->ss_family) {
432 /* Never the same. */
436 #if defined(HAVE_IPV6)
437 if (ip1->ss_family == AF_INET6) {
438 return (memcmp(&((const struct sockaddr_in6 *)ip1)->sin6_addr,
439 &((const struct sockaddr_in6 *)ip2)->sin6_addr,
440 sizeof(struct in6_addr)) == 0);
443 if (ip1->ss_family == AF_INET) {
444 return (memcmp(&((const struct sockaddr_in *)ip1)->sin_addr,
445 &((const struct sockaddr_in *)ip2)->sin_addr,
446 sizeof(struct in_addr)) == 0);
451 /****************************************************************************
452 Is an IP address the INADDR_ANY or in6addr_any value ?
453 ****************************************************************************/
455 bool is_address_any(const struct sockaddr_storage *psa)
457 #if defined(HAVE_IPV6)
458 if (psa->ss_family == AF_INET6) {
459 struct sockaddr_in6 *si6 = (struct sockaddr_in6 *)psa;
460 if (memcmp(&in6addr_any,
462 sizeof(in6addr_any)) == 0) {
468 if (psa->ss_family == AF_INET) {
469 struct sockaddr_in *si = (struct sockaddr_in *)psa;
470 if (si->sin_addr.s_addr == INADDR_ANY) {
478 /****************************************************************************
479 Print out an IPv4 or IPv6 address from a struct sockaddr_storage.
480 ****************************************************************************/
482 static char *print_sockaddr_len(char *dest,
484 const struct sockaddr_storage *psa,
490 (void)sys_getnameinfo((const struct sockaddr *)psa,
498 /****************************************************************************
499 Print out an IPv4 or IPv6 address from a struct sockaddr_storage.
500 ****************************************************************************/
502 char *print_sockaddr(char *dest,
504 const struct sockaddr_storage *psa)
506 return print_sockaddr_len(dest, destlen, psa,
507 sizeof(struct sockaddr_storage));
510 /****************************************************************************
511 Print out a canonical IPv4 or IPv6 address from a struct sockaddr_storage.
512 ****************************************************************************/
514 char *print_canonical_sockaddr(TALLOC_CTX *ctx,
515 const struct sockaddr_storage *pss)
517 char addr[INET6_ADDRSTRLEN];
521 ret = getnameinfo((const struct sockaddr *)pss,
522 sizeof(struct sockaddr_storage),
529 if (pss->ss_family != AF_INET) {
530 #if defined(HAVE_IPV6)
532 const struct sockaddr_in6 *sa6 =
533 (const struct sockaddr_in6 *)pss;
534 uint16_t port = ntohs(sa6->sin6_port);
537 dest = talloc_asprintf(ctx,
542 dest = talloc_asprintf(ctx,
550 const struct sockaddr_in *sa =
551 (const struct sockaddr_in *)pss;
552 uint16_t port = ntohs(sa->sin_port);
555 dest = talloc_asprintf(ctx,
560 dest = talloc_asprintf(ctx,
568 /****************************************************************************
569 Return the string of an IP address (IPv4 or IPv6).
570 ****************************************************************************/
572 static const char *get_socket_addr(int fd, char *addr_buf, size_t addr_len)
574 struct sockaddr_storage sa;
575 socklen_t length = sizeof(sa);
577 /* Ok, returning a hard coded IPv4 address
578 * is bogus, but it's just as bogus as a
579 * zero IPv6 address. No good choice here.
582 safe_strcpy(addr_buf, "0.0.0.0", addr_len-1);
588 if (getsockname(fd, (struct sockaddr *)&sa, &length) < 0) {
589 DEBUG(0,("getsockname failed. Error was %s\n",
594 return print_sockaddr_len(addr_buf, addr_len, &sa, length);
598 /* Not currently used. JRA. */
599 /****************************************************************************
600 Return the port number we've bound to on a socket.
601 ****************************************************************************/
603 static int get_socket_port(int fd)
605 struct sockaddr_storage sa;
606 socklen_t length = sizeof(sa);
612 if (getsockname(fd, (struct sockaddr *)&sa, &length) < 0) {
613 DEBUG(0,("getpeername failed. Error was %s\n",
618 #if defined(HAVE_IPV6)
619 if (sa.ss_family == AF_INET6) {
620 return ntohs(((struct sockaddr_in6 *)&sa)->sin6_port);
623 if (sa.ss_family == AF_INET) {
624 return ntohs(((struct sockaddr_in *)&sa)->sin_port);
630 const char *client_name(int fd)
632 return get_peer_name(fd,false);
635 const char *client_addr(int fd, char *addr, size_t addrlen)
637 return get_peer_addr(fd,addr,addrlen);
640 const char *client_socket_addr(int fd, char *addr, size_t addr_len)
642 return get_socket_addr(fd, addr, addr_len);
646 /* Not currently used. JRA. */
647 int client_socket_port(int fd)
649 return get_socket_port(fd);
653 /****************************************************************************
654 Accessor functions to make thread-safe code easier later...
655 ****************************************************************************/
657 static enum smb_read_errors smb_read_error = SMB_READ_OK;
659 enum smb_read_errors get_smb_read_error(void)
661 return smb_read_error;
664 void set_smb_read_error(enum smb_read_errors newerr)
666 smb_read_error = newerr;
669 void cond_set_smb_read_error(enum smb_read_errors newerr)
671 if (smb_read_error == SMB_READ_OK) {
672 smb_read_error = newerr;
676 /****************************************************************************
677 Determine if a file descriptor is in fact a socket.
678 ****************************************************************************/
680 bool is_a_socket(int fd)
685 return(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&v, &l) == 0);
688 enum SOCK_OPT_TYPES {OPT_BOOL,OPT_INT,OPT_ON};
690 typedef struct smb_socket_option {
698 static const smb_socket_option socket_options[] = {
699 {"SO_KEEPALIVE", SOL_SOCKET, SO_KEEPALIVE, 0, OPT_BOOL},
700 {"SO_REUSEADDR", SOL_SOCKET, SO_REUSEADDR, 0, OPT_BOOL},
701 {"SO_BROADCAST", SOL_SOCKET, SO_BROADCAST, 0, OPT_BOOL},
703 {"TCP_NODELAY", IPPROTO_TCP, TCP_NODELAY, 0, OPT_BOOL},
706 {"TCP_KEEPCNT", IPPROTO_TCP, TCP_KEEPCNT, 0, OPT_INT},
709 {"TCP_KEEPIDLE", IPPROTO_TCP, TCP_KEEPIDLE, 0, OPT_INT},
712 {"TCP_KEEPINTVL", IPPROTO_TCP, TCP_KEEPINTVL, 0, OPT_INT},
714 #ifdef IPTOS_LOWDELAY
715 {"IPTOS_LOWDELAY", IPPROTO_IP, IP_TOS, IPTOS_LOWDELAY, OPT_ON},
717 #ifdef IPTOS_THROUGHPUT
718 {"IPTOS_THROUGHPUT", IPPROTO_IP, IP_TOS, IPTOS_THROUGHPUT, OPT_ON},
721 {"SO_REUSEPORT", SOL_SOCKET, SO_REUSEPORT, 0, OPT_BOOL},
724 {"SO_SNDBUF", SOL_SOCKET, SO_SNDBUF, 0, OPT_INT},
727 {"SO_RCVBUF", SOL_SOCKET, SO_RCVBUF, 0, OPT_INT},
730 {"SO_SNDLOWAT", SOL_SOCKET, SO_SNDLOWAT, 0, OPT_INT},
733 {"SO_RCVLOWAT", SOL_SOCKET, SO_RCVLOWAT, 0, OPT_INT},
736 {"SO_SNDTIMEO", SOL_SOCKET, SO_SNDTIMEO, 0, OPT_INT},
739 {"SO_RCVTIMEO", SOL_SOCKET, SO_RCVTIMEO, 0, OPT_INT},
742 {"TCP_FASTACK", IPPROTO_TCP, TCP_FASTACK, 0, OPT_INT},
746 /****************************************************************************
747 Print socket options.
748 ****************************************************************************/
750 static void print_socket_options(int s)
754 const smb_socket_option *p = &socket_options[0];
756 /* wrapped in if statement to prevent streams
757 * leak in SCO Openserver 5.0 */
758 /* reported on samba-technical --jerry */
759 if ( DEBUGLEVEL >= 5 ) {
760 for (; p->name != NULL; p++) {
761 if (getsockopt(s, p->level, p->option,
762 (void *)&value, &vlen) == -1) {
763 DEBUG(5,("Could not test socket option %s.\n",
766 DEBUG(5,("socket option %s = %d\n",
773 /****************************************************************************
774 Set user socket options.
775 ****************************************************************************/
777 void set_socket_options(int fd, const char *options)
781 while (next_token(&options,tok," \t,", sizeof(tok))) {
785 bool got_value = false;
787 if ((p = strchr_m(tok,'='))) {
793 for (i=0;socket_options[i].name;i++)
794 if (strequal(socket_options[i].name,tok))
797 if (!socket_options[i].name) {
798 DEBUG(0,("Unknown socket option %s\n",tok));
802 switch (socket_options[i].opttype) {
805 ret = setsockopt(fd,socket_options[i].level,
806 socket_options[i].option,
807 (char *)&value,sizeof(int));
812 DEBUG(0,("syntax error - %s "
813 "does not take a value\n",tok));
816 int on = socket_options[i].value;
817 ret = setsockopt(fd,socket_options[i].level,
818 socket_options[i].option,
819 (char *)&on,sizeof(int));
825 DEBUG(0,("Failed to set socket option %s (Error %s)\n",
826 tok, strerror(errno) ));
830 print_socket_options(fd);
833 /****************************************************************************
835 ****************************************************************************/
837 ssize_t read_udp_v4_socket(int fd,
840 struct sockaddr_storage *psa)
843 socklen_t socklen = sizeof(*psa);
844 struct sockaddr_in *si = (struct sockaddr_in *)psa;
846 memset((char *)psa,'\0',socklen);
848 ret = (ssize_t)sys_recvfrom(fd,buf,len,0,
849 (struct sockaddr *)psa,&socklen);
851 /* Don't print a low debug error for a non-blocking socket. */
852 if (errno == EAGAIN) {
853 DEBUG(10,("read_udp_v4_socket: returned EAGAIN\n"));
855 DEBUG(2,("read_udp_v4_socket: failed. errno=%s\n",
861 if (psa->ss_family != AF_INET) {
862 DEBUG(2,("read_udp_v4_socket: invalid address family %d "
863 "(not IPv4)\n", (int)psa->ss_family));
867 DEBUG(10,("read_udp_v4_socket: ip %s port %d read: %lu\n",
868 inet_ntoa(si->sin_addr),
870 (unsigned long)ret));
875 /****************************************************************************
876 Read data from a socket with a timout in msec.
877 mincount = if timeout, minimum to read before returning
878 maxcount = number to be read.
879 time_out = timeout in milliseconds
880 ****************************************************************************/
882 ssize_t read_socket_with_timeout(int fd,
886 unsigned int time_out)
892 struct timeval timeout;
893 char addr[INET6_ADDRSTRLEN];
895 /* just checking .... */
899 set_smb_read_error(SMB_READ_OK);
907 while (nread < mincnt) {
908 readret = sys_read(fd, buf + nread, maxcnt - nread);
911 DEBUG(5,("read_socket_with_timeout: "
912 "blocking read. EOF from client.\n"));
913 set_smb_read_error(SMB_READ_EOF);
918 if (fd == get_client_fd()) {
919 /* Try and give an error message
920 * saying what client failed. */
921 DEBUG(0,("read_socket_with_timeout: "
922 "client %s read error = %s.\n",
923 get_peer_addr(fd,addr,sizeof(addr)),
926 DEBUG(0,("read_socket_with_timeout: "
927 "read error = %s.\n",
930 set_smb_read_error(SMB_READ_ERROR);
935 return((ssize_t)nread);
938 /* Most difficult - timeout read */
939 /* If this is ever called on a disk file and
940 mincnt is greater then the filesize then
941 system performance will suffer severely as
942 select always returns true on disk files */
944 /* Set initial timeout */
945 timeout.tv_sec = (time_t)(time_out / 1000);
946 timeout.tv_usec = (long)(1000 * (time_out % 1000));
948 for (nread=0; nread < mincnt; ) {
952 selrtn = sys_select_intr(fd+1,&fds,NULL,NULL,&timeout);
956 /* something is wrong. Maybe the socket is dead? */
957 if (fd == get_client_fd()) {
958 /* Try and give an error message saying
959 * what client failed. */
960 DEBUG(0,("read_socket_with_timeout: timeout "
961 "read for client %s. select error = %s.\n",
962 get_peer_addr(fd,addr,sizeof(addr)),
965 DEBUG(0,("read_socket_with_timeout: timeout "
966 "read. select error = %s.\n",
969 set_smb_read_error(SMB_READ_ERROR);
973 /* Did we timeout ? */
975 DEBUG(10,("read_socket_with_timeout: timeout read. "
976 "select timed out.\n"));
977 set_smb_read_error(SMB_READ_TIMEOUT);
981 readret = sys_read(fd, buf+nread, maxcnt-nread);
984 /* we got EOF on the file descriptor */
985 DEBUG(5,("read_socket_with_timeout: timeout read. "
986 "EOF from client.\n"));
987 set_smb_read_error(SMB_READ_EOF);
992 /* the descriptor is probably dead */
993 if (fd == get_client_fd()) {
994 /* Try and give an error message
995 * saying what client failed. */
996 DEBUG(0,("read_socket_with_timeout: timeout "
997 "read to client %s. read error = %s.\n",
998 get_peer_addr(fd,addr,sizeof(addr)),
1001 DEBUG(0,("read_socket_with_timeout: timeout "
1002 "read. read error = %s.\n",
1005 set_smb_read_error(SMB_READ_ERROR);
1012 /* Return the number we got */
1013 return (ssize_t)nread;
1016 /****************************************************************************
1017 Read data from the client, reading exactly N bytes.
1018 ****************************************************************************/
1020 ssize_t read_data(int fd,char *buffer,size_t N)
1024 char addr[INET6_ADDRSTRLEN];
1026 set_smb_read_error(SMB_READ_OK);
1029 ret = sys_read(fd,buffer + total,N - total);
1032 DEBUG(10,("read_data: read of %d returned 0. "
1034 (int)(N - total), strerror(errno) ));
1035 set_smb_read_error(SMB_READ_EOF);
1040 if (fd == get_client_fd()) {
1041 /* Try and give an error message saying
1042 * what client failed. */
1043 DEBUG(0,("read_data: read failure for %d "
1044 "bytes to client %s. Error = %s\n",
1046 get_peer_addr(fd,addr,sizeof(addr)),
1049 DEBUG(0,("read_data: read failure for %d. "
1054 set_smb_read_error(SMB_READ_ERROR);
1059 return (ssize_t)total;
1062 /****************************************************************************
1064 ****************************************************************************/
1066 ssize_t write_data(int fd, const char *buffer, size_t N)
1070 char addr[INET6_ADDRSTRLEN];
1073 ret = sys_write(fd,buffer + total,N - total);
1076 if (fd == get_client_fd()) {
1077 /* Try and give an error message saying
1078 * what client failed. */
1079 DEBUG(0,("write_data: write failure in "
1080 "writing to client %s. Error %s\n",
1081 get_peer_addr(fd,addr,sizeof(addr)),
1084 DEBUG(0,("write_data: write failure. "
1085 "Error = %s\n", strerror(errno) ));
1096 return (ssize_t)total;
1099 /****************************************************************************
1100 Send a keepalive packet (rfc1002).
1101 ****************************************************************************/
1103 bool send_keepalive(int client)
1105 unsigned char buf[4];
1107 buf[0] = SMBkeepalive;
1108 buf[1] = buf[2] = buf[3] = 0;
1110 return(write_data(client,(char *)buf,4) == 4);
1113 /****************************************************************************
1114 Read 4 bytes of a smb packet and return the smb length of the packet.
1115 Store the result in the buffer.
1116 This version of the function will return a length of zero on receiving
1118 Timeout is in milliseconds.
1119 ****************************************************************************/
1121 ssize_t read_smb_length_return_keepalive(int fd,
1123 unsigned int timeout)
1131 ok = (read_socket_with_timeout(fd,inbuf,4,4,timeout)
1134 ok = (read_data(fd,inbuf,4) == 4);
1140 len = smb_len(inbuf);
1141 msg_type = CVAL(inbuf,0);
1143 if (msg_type == SMBkeepalive) {
1144 DEBUG(5,("Got keepalive packet\n"));
1148 DEBUG(10,("got smb length of %lu\n",(unsigned long)len));
1153 /****************************************************************************
1154 Read 4 bytes of a smb packet and return the smb length of the packet.
1155 Store the result in the buffer. This version of the function will
1156 never return a session keepalive (length of zero).
1157 Timeout is in milliseconds.
1158 ****************************************************************************/
1160 ssize_t read_smb_length(int fd, char *inbuf, unsigned int timeout)
1165 len = read_smb_length_return_keepalive(fd, inbuf, timeout);
1170 /* Ignore session keepalives. */
1171 if(CVAL(inbuf,0) != SMBkeepalive)
1175 DEBUG(10,("read_smb_length: got smb length of %lu\n",
1176 (unsigned long)len));
1181 /****************************************************************************
1182 Read an smb from a fd. Note that the buffer *MUST* be of size
1183 BUFFER_SIZE+SAFETY_MARGIN.
1184 The timeout is in milliseconds.
1185 This function will return on receipt of a session keepalive packet.
1186 maxlen is the max number of bytes to return, not including the 4 byte
1187 length. If zero it means BUFFER_SIZE+SAFETY_MARGIN limit.
1188 Doesn't check the MAC on signed packets.
1189 ****************************************************************************/
1191 ssize_t receive_smb_raw(int fd,
1193 unsigned int timeout,
1198 set_smb_read_error(SMB_READ_OK);
1200 len = read_smb_length_return_keepalive(fd,buffer,timeout);
1202 DEBUG(10,("receive_smb_raw: length < 0!\n"));
1205 * Correct fix. smb_read_error may have already been
1206 * set. Only set it here if not already set. Global
1207 * variables still suck :-). JRA.
1210 cond_set_smb_read_error(SMB_READ_ERROR);
1215 * A WRITEX with CAP_LARGE_WRITEX can be 64k worth of data plus 65 bytes
1216 * of header. Don't print the error if this fits.... JRA.
1219 if (len > (BUFFER_SIZE + LARGE_WRITEX_HDR_SIZE)) {
1220 DEBUG(0,("Invalid packet length! (%lu bytes).\n",
1221 (unsigned long)len));
1222 if (len > BUFFER_SIZE + (SAFETY_MARGIN/2)) {
1225 * Correct fix. smb_read_error may have already been
1226 * set. Only set it here if not already set. Global
1227 * variables still suck :-). JRA.
1230 cond_set_smb_read_error(SMB_READ_ERROR);
1237 len = MIN(len,maxlen);
1241 ret = read_socket_with_timeout(fd,
1247 ret = read_data(fd,buffer+4,len);
1251 cond_set_smb_read_error(SMB_READ_ERROR);
1255 /* not all of samba3 properly checks for packet-termination
1256 * of strings. This ensures that we don't run off into
1258 SSVAL(buffer+4,len, 0);
1264 /****************************************************************************
1265 Wrapper for receive_smb_raw().
1266 Checks the MAC on signed packets.
1267 ****************************************************************************/
1269 bool receive_smb(int fd, char *buffer, unsigned int timeout)
1271 if (receive_smb_raw(fd, buffer, timeout, 0) < 0) {
1275 /* Check the incoming SMB signature. */
1276 if (!srv_check_sign_mac(buffer, true)) {
1277 DEBUG(0, ("receive_smb: SMB Signature verification "
1278 "failed on incoming packet!\n"));
1279 if (get_smb_read_error() == SMB_READ_OK) {
1280 smb_read_error = SMB_READ_BAD_SIG;
1288 /****************************************************************************
1289 Send an smb to a fd.
1290 ****************************************************************************/
1292 bool send_smb(int fd, char *buffer)
1298 /* Sign the outgoing packet if required. */
1299 srv_calculate_sign_mac(buffer);
1301 len = smb_len(buffer) + 4;
1303 while (nwritten < len) {
1304 ret = write_data(fd,buffer+nwritten,len - nwritten);
1306 DEBUG(0,("Error writing %d bytes to client. %d. (%s)\n",
1307 (int)len,(int)ret, strerror(errno) ));
1316 /****************************************************************************
1317 Open a socket of the specified type, port, and address for incoming data.
1318 ****************************************************************************/
1320 int open_socket_in(int type,
1323 const struct sockaddr_storage *psock,
1326 struct sockaddr_storage sock;
1328 socklen_t slen = sizeof(struct sockaddr_in);
1332 #if defined(HAVE_IPV6)
1333 if (sock.ss_family == AF_INET6) {
1334 ((struct sockaddr_in6 *)&sock)->sin6_port = htons(port);
1335 slen = sizeof(struct sockaddr_in6);
1338 if (sock.ss_family == AF_INET) {
1339 ((struct sockaddr_in *)&sock)->sin_port = htons(port);
1342 res = socket(sock.ss_family, type, 0 );
1345 dbgtext( "open_socket_in(): socket() call failed: " );
1346 dbgtext( "%s\n", strerror( errno ) );
1351 /* This block sets/clears the SO_REUSEADDR and possibly SO_REUSEPORT. */
1353 int val = rebind ? 1 : 0;
1354 if( setsockopt(res,SOL_SOCKET,SO_REUSEADDR,
1355 (char *)&val,sizeof(val)) == -1 ) {
1356 if( DEBUGLVL( dlevel ) ) {
1357 dbgtext( "open_socket_in(): setsockopt: " );
1358 dbgtext( "SO_REUSEADDR = %s ",
1359 val?"true":"false" );
1360 dbgtext( "on port %d failed ", port );
1361 dbgtext( "with error = %s\n", strerror(errno) );
1365 if( setsockopt(res,SOL_SOCKET,SO_REUSEPORT,
1366 (char *)&val,sizeof(val)) == -1 ) {
1367 if( DEBUGLVL( dlevel ) ) {
1368 dbgtext( "open_socket_in(): setsockopt: ");
1369 dbgtext( "SO_REUSEPORT = %s ",
1370 val?"true":"false");
1371 dbgtext( "on port %d failed ", port);
1372 dbgtext( "with error = %s\n", strerror(errno));
1375 #endif /* SO_REUSEPORT */
1378 /* now we've got a socket - we need to bind it */
1379 if (bind(res, (struct sockaddr *)&sock, slen) == -1 ) {
1380 if( DEBUGLVL(dlevel) && (port == SMB_PORT1 ||
1381 port == SMB_PORT2 || port == NMB_PORT) ) {
1382 char addr[INET6_ADDRSTRLEN];
1383 print_sockaddr(addr, sizeof(addr),
1385 dbgtext( "bind failed on port %d ", port);
1386 dbgtext( "socket_addr = %s.\n", addr);
1387 dbgtext( "Error = %s\n", strerror(errno));
1393 DEBUG( 10, ( "bind succeeded on port %d\n", port ) );
1397 /****************************************************************************
1398 Create an outgoing socket. timeout is in milliseconds.
1399 **************************************************************************/
1401 int open_socket_out(int type,
1402 const struct sockaddr_storage *pss,
1406 char addr[INET6_ADDRSTRLEN];
1407 struct sockaddr_storage sock_out = *pss;
1409 int connect_loop = 10;
1412 /* create a socket to write to */
1413 res = socket(pss->ss_family, type, 0);
1415 DEBUG(0,("socket error (%s)\n", strerror(errno)));
1419 if (type != SOCK_STREAM) {
1423 #if defined(HAVE_IPV6)
1424 if (pss->ss_family == AF_INET6) {
1425 struct sockaddr_in6 *psa6 = (struct sockaddr_in6 *)&sock_out;
1426 psa6->sin6_port = htons(port);
1427 if (psa6->sin6_scope_id == 0 &&
1428 IN6_IS_ADDR_LINKLOCAL(&psa6->sin6_addr)) {
1429 setup_linklocal_scope_id(&sock_out);
1433 if (pss->ss_family == AF_INET) {
1434 struct sockaddr_in *psa = (struct sockaddr_in *)&sock_out;
1435 psa->sin_port = htons(port);
1438 /* set it non-blocking */
1439 set_blocking(res,false);
1441 print_sockaddr(addr, sizeof(addr), &sock_out);
1442 DEBUG(3,("Connecting to %s at port %u\n",
1444 (unsigned int)port));
1446 /* and connect it to the destination */
1449 ret = connect(res,(struct sockaddr *)&sock_out,sizeof(sock_out));
1451 /* Some systems return EAGAIN when they mean EINPROGRESS */
1452 if (ret < 0 && (errno == EINPROGRESS || errno == EALREADY ||
1453 errno == EAGAIN) && (connect_loop < timeout) ) {
1454 smb_msleep(connect_loop);
1455 timeout -= connect_loop;
1456 connect_loop += increment;
1457 if (increment < 250) {
1458 /* After 8 rounds we end up at a max of 255 msec */
1464 if (ret < 0 && (errno == EINPROGRESS || errno == EALREADY ||
1466 DEBUG(1,("timeout connecting to %s:%u\n",
1468 (unsigned int)port));
1474 if (ret < 0 && errno == EISCONN) {
1481 DEBUG(2,("error connecting to %s:%d (%s)\n",
1489 /* set it blocking again */
1490 set_blocking(res,true);
1495 /****************************************************************************
1496 Create an outgoing TCP socket to any of the addrs. This is for
1497 simultaneous connects to port 445 and 139 of a host or even a variety
1498 of DC's all of which are equivalent for our purposes.
1499 **************************************************************************/
1501 bool open_any_socket_out(struct sockaddr_storage *addrs, int num_addrs,
1502 int timeout, int *fd_index, int *fd)
1504 int i, resulting_index, res;
1508 fd_set r_fds, wr_fds;
1512 int connect_loop = 10000; /* 10 milliseconds */
1514 timeout *= 1000; /* convert to microseconds */
1516 sockets = SMB_MALLOC_ARRAY(int, num_addrs);
1518 if (sockets == NULL)
1521 resulting_index = -1;
1523 for (i=0; i<num_addrs; i++)
1526 for (i=0; i<num_addrs; i++) {
1527 sockets[i] = socket(addrs[i].ss_family, SOCK_STREAM, 0);
1530 set_blocking(sockets[i], false);
1534 good_connect = false;
1536 for (i=0; i<num_addrs; i++) {
1538 if (sockets[i] == -1)
1541 if (connect(sockets[i], (struct sockaddr *)&(addrs[i]),
1542 sizeof(*addrs)) == 0) {
1543 /* Rather unlikely as we are non-blocking, but it
1544 * might actually happen. */
1545 resulting_index = i;
1549 if (errno == EINPROGRESS || errno == EALREADY ||
1553 errno == EAGAIN || errno == EINTR) {
1554 /* These are the error messages that something is
1556 good_connect = true;
1557 } else if (errno != 0) {
1558 /* There was a direct error */
1564 if (!good_connect) {
1565 /* All of the connect's resulted in real error conditions */
1569 /* Lets see if any of the connect attempts succeeded */
1575 for (i=0; i<num_addrs; i++) {
1576 if (sockets[i] == -1)
1578 FD_SET(sockets[i], &wr_fds);
1579 FD_SET(sockets[i], &r_fds);
1580 if (sockets[i]>maxfd)
1585 tv.tv_usec = connect_loop;
1587 res = sys_select_intr(maxfd+1, &r_fds, &wr_fds, NULL, &tv);
1595 for (i=0; i<num_addrs; i++) {
1597 if (sockets[i] == -1)
1600 /* Stevens, Network Programming says that if there's a
1601 * successful connect, the socket is only writable. Upon an
1602 * error, it's both readable and writable. */
1604 if (FD_ISSET(sockets[i], &r_fds) &&
1605 FD_ISSET(sockets[i], &wr_fds)) {
1606 /* readable and writable, so it's an error */
1612 if (!FD_ISSET(sockets[i], &r_fds) &&
1613 FD_ISSET(sockets[i], &wr_fds)) {
1614 /* Only writable, so it's connected */
1615 resulting_index = i;
1622 timeout -= connect_loop;
1625 connect_loop *= 1.5;
1626 if (connect_loop > timeout)
1627 connect_loop = timeout;
1631 for (i=0; i<num_addrs; i++) {
1632 if (i == resulting_index)
1634 if (sockets[i] >= 0)
1638 if (resulting_index >= 0) {
1639 *fd_index = resulting_index;
1640 *fd = sockets[*fd_index];
1641 set_blocking(*fd, true);
1646 return (resulting_index >= 0);
1648 /****************************************************************************
1649 Open a connected UDP socket to host on port
1650 **************************************************************************/
1652 int open_udp_socket(const char *host, int port)
1654 int type = SOCK_DGRAM;
1655 struct sockaddr_in sock_out;
1657 struct in_addr addr;
1659 (void)interpret_addr2(&addr, host);
1661 res = socket(PF_INET, type, 0);
1666 memset((char *)&sock_out,'\0',sizeof(sock_out));
1667 putip((char *)&sock_out.sin_addr,(char *)&addr);
1668 sock_out.sin_port = htons(port);
1669 sock_out.sin_family = PF_INET;
1671 if (connect(res,(struct sockaddr *)&sock_out,sizeof(sock_out))) {
1679 /*******************************************************************
1680 Return the IP addr of the remote end of a socket as a string.
1681 Optionally return the struct sockaddr_storage.
1682 ******************************************************************/
1684 static const char *get_peer_addr_internal(int fd,
1686 size_t addr_buf_len,
1687 struct sockaddr_storage *pss,
1690 struct sockaddr_storage ss;
1691 socklen_t length = sizeof(ss);
1693 safe_strcpy(addr_buf,"0.0.0.0",addr_buf_len-1);
1702 if (plength == NULL) {
1706 if (getpeername(fd, (struct sockaddr *)pss, plength) < 0) {
1707 DEBUG(0,("getpeername failed. Error was %s\n",
1712 print_sockaddr_len(addr_buf,
1719 /*******************************************************************
1720 Matchname - determine if host name matches IP address. Used to
1721 confirm a hostname lookup to prevent spoof attacks.
1722 ******************************************************************/
1724 static bool matchname(const char *remotehost,
1725 const struct sockaddr_storage *pss,
1728 struct addrinfo *res = NULL;
1729 struct addrinfo *ailist = NULL;
1730 char addr_buf[INET6_ADDRSTRLEN];
1731 bool ret = interpret_string_addr_internal(&ailist,
1733 AI_ADDRCONFIG|AI_CANONNAME);
1735 if (!ret || ailist == NULL) {
1736 DEBUG(3,("matchname: getaddrinfo failed for "
1739 gai_strerror(ret) ));
1744 * Make sure that getaddrinfo() returns the "correct" host name.
1747 if (ailist->ai_canonname == NULL ||
1748 (!strequal(remotehost, ailist->ai_canonname) &&
1749 !strequal(remotehost, "localhost"))) {
1750 DEBUG(0,("matchname: host name/name mismatch: %s != %s\n",
1752 ailist->ai_canonname ?
1753 ailist->ai_canonname : "(NULL)"));
1754 freeaddrinfo(ailist);
1758 /* Look up the host address in the address list we just got. */
1759 for (res = ailist; res; res = res->ai_next) {
1760 if (!res->ai_addr) {
1763 if (addr_equal((const struct sockaddr_storage *)res->ai_addr,
1765 freeaddrinfo(ailist);
1771 * The host name does not map to the original host address. Perhaps
1772 * someone has compromised a name server. More likely someone botched
1773 * it, but that could be dangerous, too.
1776 DEBUG(0,("matchname: host name/address mismatch: %s != %s\n",
1777 print_sockaddr_len(addr_buf,
1781 ailist->ai_canonname ? ailist->ai_canonname : "(NULL)"));
1784 freeaddrinfo(ailist);
1789 /*******************************************************************
1790 Return the DNS name of the remote end of a socket.
1791 ******************************************************************/
1793 static char addr_buf_cache[INET6_ADDRSTRLEN];
1795 const char *get_peer_name(int fd, bool force_lookup)
1797 static pstring name_buf;
1798 char addr_buf[INET6_ADDRSTRLEN];
1799 struct sockaddr_storage ss;
1800 socklen_t length = sizeof(ss);
1805 /* reverse lookups can be *very* expensive, and in many
1806 situations won't work because many networks don't link dhcp
1807 with dns. To avoid the delay we avoid the lookup if
1809 if (!lp_hostname_lookups() && (force_lookup == false)) {
1810 pstrcpy(name_buf, get_peer_addr(fd, addr_buf, sizeof(addr_buf)));
1814 p = get_peer_addr_internal(fd, addr_buf, sizeof(addr_buf), &ss, &length);
1816 /* it might be the same as the last one - save some DNS work */
1817 if (strcmp(p, addr_buf_cache) == 0) {
1821 pstrcpy(name_buf,"UNKNOWN");
1826 safe_strcpy(addr_buf_cache, p, sizeof(addr_buf_cache)-1);
1828 /* Look up the remote host name. */
1829 ret = getnameinfo((struct sockaddr *)&ss,
1838 DEBUG(1,("get_peer_name: getnameinfo failed "
1839 "for %s with error %s\n",
1841 gai_strerror(ret)));
1842 pstrcpy(name_buf, p);
1844 if (!matchname(name_buf, &ss, length)) {
1845 DEBUG(0,("Matchname failed on %s %s\n",name_buf,p));
1846 pstrcpy(name_buf,"UNKNOWN");
1850 /* can't pass the same source and dest strings in when you
1851 use --enable-developer or the clobber_region() call will
1854 pstrcpy(tmp_name, name_buf );
1855 alpha_strcpy(name_buf, tmp_name, "_-.", sizeof(name_buf));
1856 if (strstr(name_buf,"..")) {
1857 pstrcpy(name_buf, "UNKNOWN");
1863 /*******************************************************************
1864 Return the IP addr of the remote end of a socket as a string.
1865 ******************************************************************/
1867 const char *get_peer_addr(int fd, char *addr, size_t addr_len)
1869 return get_peer_addr_internal(fd, addr, addr_len, NULL, NULL);
1872 /*******************************************************************
1873 Create protected unix domain socket.
1875 Some unixes cannot set permissions on a ux-dom-sock, so we
1876 have to make sure that the directory contains the protection
1877 permissions instead.
1878 ******************************************************************/
1880 int create_pipe_sock(const char *socket_dir,
1881 const char *socket_name,
1884 #ifdef HAVE_UNIXSOCKET
1885 struct sockaddr_un sunaddr;
1891 old_umask = umask(0);
1893 /* Create the socket directory or reuse the existing one */
1895 if (lstat(socket_dir, &st) == -1) {
1896 if (errno == ENOENT) {
1897 /* Create directory */
1898 if (mkdir(socket_dir, dir_perms) == -1) {
1899 DEBUG(0, ("error creating socket directory "
1900 "%s: %s\n", socket_dir,
1905 DEBUG(0, ("lstat failed on socket directory %s: %s\n",
1906 socket_dir, strerror(errno)));
1910 /* Check ownership and permission on existing directory */
1911 if (!S_ISDIR(st.st_mode)) {
1912 DEBUG(0, ("socket directory %s isn't a directory\n",
1916 if ((st.st_uid != sec_initial_uid()) ||
1917 ((st.st_mode & 0777) != dir_perms)) {
1918 DEBUG(0, ("invalid permissions on socket directory "
1919 "%s\n", socket_dir));
1924 /* Create the socket file */
1926 sock = socket(AF_UNIX, SOCK_STREAM, 0);
1933 pstr_sprintf(path, "%s/%s", socket_dir, socket_name);
1936 memset(&sunaddr, 0, sizeof(sunaddr));
1937 sunaddr.sun_family = AF_UNIX;
1938 safe_strcpy(sunaddr.sun_path, path, sizeof(sunaddr.sun_path)-1);
1940 if (bind(sock, (struct sockaddr *)&sunaddr, sizeof(sunaddr)) == -1) {
1941 DEBUG(0, ("bind failed on pipe socket %s: %s\n", path,
1946 if (listen(sock, 5) == -1) {
1947 DEBUG(0, ("listen failed on pipe socket %s: %s\n", path,
1963 DEBUG(0, ("create_pipe_sock: No Unix sockets on this system\n"));
1965 #endif /* HAVE_UNIXSOCKET */
1968 /****************************************************************************
1969 Get my own canonical name, including domain.
1970 ****************************************************************************/
1972 static fstring dnshostname_cache;
1974 bool get_mydnsfullname(fstring my_dnsname)
1976 if (!*dnshostname_cache) {
1977 struct addrinfo *res = NULL;
1980 /* get my host name */
1981 if (gethostname(dnshostname_cache, sizeof(dnshostname_cache)) == -1) {
1982 *dnshostname_cache = '\0';
1983 DEBUG(0,("get_mydnsfullname: gethostname failed\n"));
1987 /* Ensure null termination. */
1988 dnshostname_cache[sizeof(dnshostname_cache)-1] = '\0';
1990 ret = interpret_string_addr_internal(&res,
1992 AI_ADDRCONFIG|AI_CANONNAME);
1994 if (!ret || res == NULL) {
1995 DEBUG(3,("get_mydnsfullname: getaddrinfo failed for "
1998 gai_strerror(ret) ));
2003 * Make sure that getaddrinfo() returns the "correct" host name.
2006 if (res->ai_canonname == NULL) {
2007 DEBUG(3,("get_mydnsfullname: failed to get "
2008 "canonical name for %s\n",
2009 dnshostname_cache));
2015 fstrcpy(dnshostname_cache, res->ai_canonname);
2018 fstrcpy(my_dnsname, dnshostname_cache);
2022 /************************************************************
2024 ************************************************************/
2026 bool is_myname_or_ipaddr(const char *s)
2028 fstring name, dnsname;
2035 /* Santize the string from '\\name' */
2038 servername = strrchr_m(name, '\\' );
2045 /* Optimize for the common case */
2046 if (strequal(servername, global_myname())) {
2050 /* Check for an alias */
2051 if (is_myname(servername)) {
2055 /* Check for loopback */
2056 if (strequal(servername, "127.0.0.1") ||
2057 strequal(servername, "::1")) {
2061 if (strequal(servername, "localhost")) {
2065 /* Maybe it's my dns name */
2066 if (get_mydnsfullname(dnsname)) {
2067 if (strequal(servername, dnsname)) {
2072 /* Handle possible CNAME records - convert to an IP addr. */
2073 if (!is_ipaddress(servername)) {
2074 /* Use DNS to resolve the name, but only the first address */
2075 struct sockaddr_storage ss;
2076 if (interpret_string_addr(&ss, servername,0)) {
2077 print_sockaddr(name,
2084 /* Maybe its an IP address? */
2085 if (is_ipaddress(servername)) {
2086 struct sockaddr_storage ss;
2087 struct iface_struct nics[MAX_INTERFACES];
2090 if (!interpret_string_addr(&ss, servername, AI_NUMERICHOST)) {
2094 if (is_zero_addr(&ss) || is_loopback_addr(&ss)) {
2098 n = get_interfaces(nics, MAX_INTERFACES);
2099 for (i=0; i<n; i++) {
2100 if (addr_equal(&nics[i].ip, &ss)) {