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(struct in_addr *ip, const char *str)
204 uint32 a = interpret_addr(str);
209 /*******************************************************************
210 Map a text hostname or IP address (IPv4 or IPv6) into a
211 struct sockaddr_storage.
212 ******************************************************************/
214 bool interpret_string_addr(struct sockaddr_storage *pss,
218 char addr[INET6_ADDRSTRLEN];
219 struct addrinfo *res = NULL;
220 #if defined(HAVE_IPV6)
221 unsigned int scope_id = 0;
223 if (strchr_m(str, ':')) {
224 char *p = strchr_m(str, '%');
227 * Cope with link-local.
228 * This is IP:v6:addr%ifname.
231 if (p && (p > str) && ((scope_id = if_nametoindex(p+1)) != 0)) {
233 MIN(PTR_DIFF(p,str)+1,
242 if (!interpret_string_addr_internal(&res, str, flags|AI_ADDRCONFIG)) {
248 /* Copy the first sockaddr. */
249 memcpy(pss, res->ai_addr, res->ai_addrlen);
251 #if defined(HAVE_IPV6)
252 if (pss->ss_family == AF_INET6 && scope_id) {
253 struct sockaddr_in6 *ps6 = (struct sockaddr_in6 *)pss;
254 if (IN6_IS_ADDR_LINKLOCAL(&ps6->sin6_addr) &&
255 ps6->sin6_scope_id == 0) {
256 ps6->sin6_scope_id = scope_id;
265 /*******************************************************************
266 Check if an IPv7 is 127.0.0.1
267 ******************************************************************/
269 bool is_loopback_ip_v4(struct in_addr ip)
272 a.s_addr = htonl(INADDR_LOOPBACK);
273 return(ip.s_addr == a.s_addr);
276 /*******************************************************************
277 Check if a struct sockaddr_storage is the loopback address.
278 ******************************************************************/
280 bool is_loopback_addr(const struct sockaddr_storage *pss)
282 #if defined(HAVE_IPV6)
283 if (pss->ss_family == AF_INET6) {
284 struct in6_addr *pin6 =
285 &((struct sockaddr_in6 *)pss)->sin6_addr;
286 return IN6_IS_ADDR_LOOPBACK(pin6);
289 if (pss->ss_family == AF_INET) {
290 struct in_addr *pin = &((struct sockaddr_in *)pss)->sin_addr;
291 return is_loopback_ip_v4(*pin);
296 /*******************************************************************
297 Check if an IPv4 is 0.0.0.0.
298 ******************************************************************/
300 bool is_zero_ip_v4(struct in_addr ip)
303 putip((char *)&a,(char *)&ip);
307 /*******************************************************************
308 Check if a struct sockaddr_storage has an unspecified address.
309 ******************************************************************/
311 bool is_zero_addr(const struct sockaddr_storage *pss)
313 #if defined(HAVE_IPV6)
314 if (pss->ss_family == AF_INET6) {
315 struct in6_addr *pin6 =
316 &((struct sockaddr_in6 *)pss)->sin6_addr;
317 return IN6_IS_ADDR_UNSPECIFIED(pin6);
320 if (pss->ss_family == AF_INET) {
321 struct in_addr *pin = &((struct sockaddr_in *)pss)->sin_addr;
322 return is_zero_ip_v4(*pin);
327 /*******************************************************************
328 Set an IP to 0.0.0.0.
329 ******************************************************************/
331 void zero_ip_v4(struct in_addr *ip)
333 memset(ip, '\0', sizeof(struct in_addr));
336 /*******************************************************************
337 Set an address to INADDR_ANY.
338 ******************************************************************/
340 void zero_addr(struct sockaddr_storage *pss)
342 memset(pss, '\0', sizeof(*pss));
343 /* Ensure we're at least a valid sockaddr-storage. */
344 pss->ss_family = AF_INET;
347 /*******************************************************************
348 Are two IPs on the same subnet - IPv4 version ?
349 ********************************************************************/
351 bool same_net_v4(struct in_addr ip1,struct in_addr ip2,struct in_addr mask)
353 uint32 net1,net2,nmask;
355 nmask = ntohl(mask.s_addr);
356 net1 = ntohl(ip1.s_addr);
357 net2 = ntohl(ip2.s_addr);
359 return((net1 & nmask) == (net2 & nmask));
362 /*******************************************************************
363 Convert an IPv4 struct in_addr to a struct sockaddr_storage.
364 ********************************************************************/
366 void in_addr_to_sockaddr_storage(struct sockaddr_storage *ss,
369 struct sockaddr_in *sa = (struct sockaddr_in *)ss;
370 memset(ss, '\0', sizeof(*ss));
371 ss->ss_family = AF_INET;
375 #if defined(HAVE_IPV6)
376 /*******************************************************************
377 Convert an IPv6 struct in_addr to a struct sockaddr_storage.
378 ********************************************************************/
380 void in6_addr_to_sockaddr_storage(struct sockaddr_storage *ss,
383 struct sockaddr_in6 *sa = (struct sockaddr_in6 *)ss;
384 memset(ss, '\0', sizeof(*ss));
385 ss->ss_family = AF_INET6;
390 /*******************************************************************
391 Are two IPs on the same subnet?
392 ********************************************************************/
394 bool same_net(const struct sockaddr_storage *ip1,
395 const struct sockaddr_storage *ip2,
396 const struct sockaddr_storage *mask)
398 if (ip1->ss_family != ip2->ss_family) {
399 /* Never on the same net. */
403 #if defined(HAVE_IPV6)
404 if (ip1->ss_family == AF_INET6) {
405 struct sockaddr_in6 ip1_6 = *(struct sockaddr_in6 *)ip1;
406 struct sockaddr_in6 ip2_6 = *(struct sockaddr_in6 *)ip2;
407 struct sockaddr_in6 mask_6 = *(struct sockaddr_in6 *)mask;
408 char *p1 = (char *)&ip1_6.sin6_addr;
409 char *p2 = (char *)&ip2_6.sin6_addr;
410 char *m = (char *)&mask_6.sin6_addr;
413 for (i = 0; i < sizeof(struct in6_addr); i++) {
418 return (memcmp(&ip1_6.sin6_addr,
420 sizeof(struct in6_addr)) == 0);
423 if (ip1->ss_family == AF_INET) {
424 return same_net_v4(((const struct sockaddr_in *)ip1)->sin_addr,
425 ((const struct sockaddr_in *)ip2)->sin_addr,
426 ((const struct sockaddr_in *)mask)->sin_addr);
431 /*******************************************************************
432 Are two sockaddr_storage's the same family and address ? Ignore port etc.
433 ********************************************************************/
435 bool addr_equal(const struct sockaddr_storage *ip1,
436 const struct sockaddr_storage *ip2)
438 if (ip1->ss_family != ip2->ss_family) {
439 /* Never the same. */
443 #if defined(HAVE_IPV6)
444 if (ip1->ss_family == AF_INET6) {
445 return (memcmp(&((const struct sockaddr_in6 *)ip1)->sin6_addr,
446 &((const struct sockaddr_in6 *)ip2)->sin6_addr,
447 sizeof(struct in6_addr)) == 0);
450 if (ip1->ss_family == AF_INET) {
451 return (memcmp(&((const struct sockaddr_in *)ip1)->sin_addr,
452 &((const struct sockaddr_in *)ip2)->sin_addr,
453 sizeof(struct in_addr)) == 0);
458 /****************************************************************************
459 Is an IP address the INADDR_ANY or in6addr_any value ?
460 ****************************************************************************/
462 bool is_address_any(const struct sockaddr_storage *psa)
464 #if defined(HAVE_IPV6)
465 if (psa->ss_family == AF_INET6) {
466 struct sockaddr_in6 *si6 = (struct sockaddr_in6 *)psa;
467 if (memcmp(&in6addr_any,
469 sizeof(in6addr_any)) == 0) {
475 if (psa->ss_family == AF_INET) {
476 struct sockaddr_in *si = (struct sockaddr_in *)psa;
477 if (si->sin_addr.s_addr == INADDR_ANY) {
485 /****************************************************************************
486 Print out an IPv4 or IPv6 address from a struct sockaddr_storage.
487 ****************************************************************************/
489 static char *print_sockaddr_len(char *dest,
491 const struct sockaddr_storage *psa,
497 (void)sys_getnameinfo((const struct sockaddr *)psa,
505 /****************************************************************************
506 Print out an IPv4 or IPv6 address from a struct sockaddr_storage.
507 ****************************************************************************/
509 char *print_sockaddr(char *dest,
511 const struct sockaddr_storage *psa)
513 return print_sockaddr_len(dest, destlen, psa,
514 sizeof(struct sockaddr_storage));
517 /****************************************************************************
518 Print out a canonical IPv4 or IPv6 address from a struct sockaddr_storage.
519 ****************************************************************************/
521 char *print_canonical_sockaddr(TALLOC_CTX *ctx,
522 const struct sockaddr_storage *pss)
524 char addr[INET6_ADDRSTRLEN];
528 ret = getnameinfo((const struct sockaddr *)pss,
529 sizeof(struct sockaddr_storage),
536 if (pss->ss_family != AF_INET) {
537 #if defined(HAVE_IPV6)
539 const struct sockaddr_in6 *sa6 =
540 (const struct sockaddr_in6 *)pss;
541 uint16_t port = ntohs(sa6->sin6_port);
544 dest = talloc_asprintf(ctx,
549 dest = talloc_asprintf(ctx,
557 const struct sockaddr_in *sa =
558 (const struct sockaddr_in *)pss;
559 uint16_t port = ntohs(sa->sin_port);
562 dest = talloc_asprintf(ctx,
567 dest = talloc_asprintf(ctx,
575 /****************************************************************************
576 Set the global client_fd variable.
577 ****************************************************************************/
579 void client_setfd(int fd)
581 char addr[INET6_ADDRSTRLEN];
583 safe_strcpy(client_ip_string,
584 get_peer_addr(client_fd,addr,sizeof(addr)),
585 sizeof(client_ip_string)-1);
588 /****************************************************************************
589 Return the string of an IP address (IPv4 or IPv6).
590 ****************************************************************************/
592 static const char *get_socket_addr(int fd, char *addr_buf, size_t addr_len)
594 struct sockaddr_storage sa;
595 socklen_t length = sizeof(sa);
597 /* Ok, returning a hard coded IPv4 address
598 * is bogus, but it's just as bogus as a
599 * zero IPv6 address. No good choice here.
602 safe_strcpy(addr_buf, "0.0.0.0", addr_len-1);
608 if (getsockname(fd, (struct sockaddr *)&sa, &length) < 0) {
609 DEBUG(0,("getsockname failed. Error was %s\n",
614 return print_sockaddr_len(addr_buf, addr_len, &sa, length);
617 /****************************************************************************
618 Return the port number we've bound to on a socket.
619 ****************************************************************************/
621 static int get_socket_port(int fd)
623 struct sockaddr_storage sa;
624 socklen_t length = sizeof(sa);
630 if (getsockname(fd, (struct sockaddr *)&sa, &length) < 0) {
631 DEBUG(0,("getpeername failed. Error was %s\n",
636 #if defined(HAVE_IPV6)
637 if (sa.ss_family == AF_INET6) {
638 return ntohs(((struct sockaddr_in6 *)&sa)->sin6_port);
641 if (sa.ss_family == AF_INET) {
642 return ntohs(((struct sockaddr_in *)&sa)->sin_port);
647 const char *client_name(void)
649 return get_peer_name(client_fd,false);
652 const char *client_addr(char *addr, size_t addrlen)
654 return get_peer_addr(client_fd,addr,addrlen);
657 const char *client_socket_addr(char *addr, size_t addr_len)
659 return get_socket_addr(client_fd, addr, addr_len);
662 int client_socket_port(void)
664 return get_socket_port(client_fd);
667 /****************************************************************************
668 Accessor functions to make thread-safe code easier later...
669 ****************************************************************************/
671 static enum smb_read_errors smb_read_error = SMB_READ_OK;
673 enum smb_read_errors get_smb_read_error(void)
675 return smb_read_error;
678 void set_smb_read_error(enum smb_read_errors newerr)
680 smb_read_error = newerr;
683 void cond_set_smb_read_error(enum smb_read_errors newerr)
685 if (smb_read_error == SMB_READ_OK) {
686 smb_read_error = newerr;
690 /****************************************************************************
691 Determine if a file descriptor is in fact a socket.
692 ****************************************************************************/
694 bool is_a_socket(int fd)
699 return(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&v, &l) == 0);
702 enum SOCK_OPT_TYPES {OPT_BOOL,OPT_INT,OPT_ON};
704 typedef struct smb_socket_option {
712 static const smb_socket_option socket_options[] = {
713 {"SO_KEEPALIVE", SOL_SOCKET, SO_KEEPALIVE, 0, OPT_BOOL},
714 {"SO_REUSEADDR", SOL_SOCKET, SO_REUSEADDR, 0, OPT_BOOL},
715 {"SO_BROADCAST", SOL_SOCKET, SO_BROADCAST, 0, OPT_BOOL},
717 {"TCP_NODELAY", IPPROTO_TCP, TCP_NODELAY, 0, OPT_BOOL},
720 {"TCP_KEEPCNT", IPPROTO_TCP, TCP_KEEPCNT, 0, OPT_INT},
723 {"TCP_KEEPIDLE", IPPROTO_TCP, TCP_KEEPIDLE, 0, OPT_INT},
726 {"TCP_KEEPINTVL", IPPROTO_TCP, TCP_KEEPINTVL, 0, OPT_INT},
728 #ifdef IPTOS_LOWDELAY
729 {"IPTOS_LOWDELAY", IPPROTO_IP, IP_TOS, IPTOS_LOWDELAY, OPT_ON},
731 #ifdef IPTOS_THROUGHPUT
732 {"IPTOS_THROUGHPUT", IPPROTO_IP, IP_TOS, IPTOS_THROUGHPUT, OPT_ON},
735 {"SO_REUSEPORT", SOL_SOCKET, SO_REUSEPORT, 0, OPT_BOOL},
738 {"SO_SNDBUF", SOL_SOCKET, SO_SNDBUF, 0, OPT_INT},
741 {"SO_RCVBUF", SOL_SOCKET, SO_RCVBUF, 0, OPT_INT},
744 {"SO_SNDLOWAT", SOL_SOCKET, SO_SNDLOWAT, 0, OPT_INT},
747 {"SO_RCVLOWAT", SOL_SOCKET, SO_RCVLOWAT, 0, OPT_INT},
750 {"SO_SNDTIMEO", SOL_SOCKET, SO_SNDTIMEO, 0, OPT_INT},
753 {"SO_RCVTIMEO", SOL_SOCKET, SO_RCVTIMEO, 0, OPT_INT},
756 {"TCP_FASTACK", IPPROTO_TCP, TCP_FASTACK, 0, OPT_INT},
760 /****************************************************************************
761 Print socket options.
762 ****************************************************************************/
764 static void print_socket_options(int s)
768 const smb_socket_option *p = &socket_options[0];
770 /* wrapped in if statement to prevent streams
771 * leak in SCO Openserver 5.0 */
772 /* reported on samba-technical --jerry */
773 if ( DEBUGLEVEL >= 5 ) {
774 for (; p->name != NULL; p++) {
775 if (getsockopt(s, p->level, p->option,
776 (void *)&value, &vlen) == -1) {
777 DEBUG(5,("Could not test socket option %s.\n",
780 DEBUG(5,("socket option %s = %d\n",
787 /****************************************************************************
788 Set user socket options.
789 ****************************************************************************/
791 void set_socket_options(int fd, const char *options)
795 while (next_token(&options,tok," \t,", sizeof(tok))) {
799 bool got_value = false;
801 if ((p = strchr_m(tok,'='))) {
807 for (i=0;socket_options[i].name;i++)
808 if (strequal(socket_options[i].name,tok))
811 if (!socket_options[i].name) {
812 DEBUG(0,("Unknown socket option %s\n",tok));
816 switch (socket_options[i].opttype) {
819 ret = setsockopt(fd,socket_options[i].level,
820 socket_options[i].option,
821 (char *)&value,sizeof(int));
826 DEBUG(0,("syntax error - %s "
827 "does not take a value\n",tok));
830 int on = socket_options[i].value;
831 ret = setsockopt(fd,socket_options[i].level,
832 socket_options[i].option,
833 (char *)&on,sizeof(int));
839 DEBUG(0,("Failed to set socket option %s (Error %s)\n",
840 tok, strerror(errno) ));
844 print_socket_options(fd);
847 /****************************************************************************
849 ****************************************************************************/
851 ssize_t read_udp_v4_socket(int fd,
854 struct sockaddr_storage *psa)
857 socklen_t socklen = sizeof(*psa);
858 struct sockaddr_in *si = (struct sockaddr_in *)psa;
860 memset((char *)psa,'\0',socklen);
862 ret = (ssize_t)sys_recvfrom(fd,buf,len,0,
863 (struct sockaddr *)psa,&socklen);
865 /* Don't print a low debug error for a non-blocking socket. */
866 if (errno == EAGAIN) {
867 DEBUG(10,("read_udp_v4_socket: returned EAGAIN\n"));
869 DEBUG(2,("read_udp_v4_socket: failed. errno=%s\n",
875 if (psa->ss_family != AF_INET) {
876 DEBUG(2,("read_udp_v4_socket: invalid address family %d "
877 "(not IPv4)\n", (int)psa->ss_family));
881 DEBUG(10,("read_udp_v4_socket: ip %s port %d read: %lu\n",
882 inet_ntoa(si->sin_addr),
884 (unsigned long)ret));
889 /****************************************************************************
890 Read data from a socket with a timout in msec.
891 mincount = if timeout, minimum to read before returning
892 maxcount = number to be read.
893 time_out = timeout in milliseconds
894 ****************************************************************************/
896 ssize_t read_socket_with_timeout(int fd,
900 unsigned int time_out)
906 struct timeval timeout;
908 /* just checking .... */
912 set_smb_read_error(SMB_READ_OK);
920 while (nread < mincnt) {
921 readret = sys_read(fd, buf + nread, maxcnt - nread);
924 DEBUG(5,("read_socket_with_timeout: "
925 "blocking read. EOF from client.\n"));
926 set_smb_read_error(SMB_READ_EOF);
931 if (fd == client_fd) {
932 /* Try and give an error message
933 * saying what client failed. */
934 DEBUG(0,("read_socket_with_timeout: "
935 "client %s read error = %s.\n",
939 DEBUG(0,("read_socket_with_timeout: "
940 "read error = %s.\n",
943 set_smb_read_error(SMB_READ_ERROR);
948 return((ssize_t)nread);
951 /* Most difficult - timeout read */
952 /* If this is ever called on a disk file and
953 mincnt is greater then the filesize then
954 system performance will suffer severely as
955 select always returns true on disk files */
957 /* Set initial timeout */
958 timeout.tv_sec = (time_t)(time_out / 1000);
959 timeout.tv_usec = (long)(1000 * (time_out % 1000));
961 for (nread=0; nread < mincnt; ) {
965 selrtn = sys_select_intr(fd+1,&fds,NULL,NULL,&timeout);
969 /* something is wrong. Maybe the socket is dead? */
970 if (fd == client_fd) {
971 /* Try and give an error message saying
972 * what client failed. */
973 DEBUG(0,("read_socket_with_timeout: timeout "
974 "read for client %s. select error = %s.\n",
975 client_ip_string, strerror(errno) ));
977 DEBUG(0,("read_socket_with_timeout: timeout "
978 "read. select error = %s.\n",
981 set_smb_read_error(SMB_READ_ERROR);
985 /* Did we timeout ? */
987 DEBUG(10,("read_socket_with_timeout: timeout read. "
988 "select timed out.\n"));
989 set_smb_read_error(SMB_READ_TIMEOUT);
993 readret = sys_read(fd, buf+nread, maxcnt-nread);
996 /* we got EOF on the file descriptor */
997 DEBUG(5,("read_socket_with_timeout: timeout read. "
998 "EOF from client.\n"));
999 set_smb_read_error(SMB_READ_EOF);
1003 if (readret == -1) {
1004 /* the descriptor is probably dead */
1005 if (fd == client_fd) {
1006 /* Try and give an error message
1007 * saying what client failed. */
1008 DEBUG(0,("read_socket_with_timeout: timeout "
1009 "read to client %s. read error = %s.\n",
1010 client_ip_string, strerror(errno) ));
1012 DEBUG(0,("read_socket_with_timeout: timeout "
1013 "read. read error = %s.\n",
1016 set_smb_read_error(SMB_READ_ERROR);
1023 /* Return the number we got */
1024 return (ssize_t)nread;
1027 /****************************************************************************
1028 Read data from the client, reading exactly N bytes.
1029 ****************************************************************************/
1031 ssize_t read_data(int fd,char *buffer,size_t N)
1036 set_smb_read_error(SMB_READ_OK);
1039 ret = sys_read(fd,buffer + total,N - total);
1042 DEBUG(10,("read_data: read of %d returned 0. "
1044 (int)(N - total), strerror(errno) ));
1045 set_smb_read_error(SMB_READ_EOF);
1050 if (fd == client_fd) {
1051 /* Try and give an error message saying
1052 * what client failed. */
1053 DEBUG(0,("read_data: read failure for %d "
1054 "bytes to client %s. Error = %s\n",
1059 DEBUG(0,("read_data: read failure for %d. "
1064 set_smb_read_error(SMB_READ_ERROR);
1069 return (ssize_t)total;
1072 /****************************************************************************
1074 ****************************************************************************/
1076 ssize_t write_data(int fd, const char *buffer, size_t N)
1082 ret = sys_write(fd,buffer + total,N - total);
1085 if (fd == client_fd) {
1086 /* Try and give an error message saying
1087 * what client failed. */
1088 DEBUG(0,("write_data: write failure in "
1089 "writing to client %s. Error %s\n",
1090 client_ip_string, strerror(errno) ));
1092 DEBUG(0,("write_data: write failure. "
1093 "Error = %s\n", strerror(errno) ));
1104 return (ssize_t)total;
1107 /****************************************************************************
1108 Send a keepalive packet (rfc1002).
1109 ****************************************************************************/
1111 bool send_keepalive(int client)
1113 unsigned char buf[4];
1115 buf[0] = SMBkeepalive;
1116 buf[1] = buf[2] = buf[3] = 0;
1118 return(write_data(client,(char *)buf,4) == 4);
1121 /****************************************************************************
1122 Read 4 bytes of a smb packet and return the smb length of the packet.
1123 Store the result in the buffer.
1124 This version of the function will return a length of zero on receiving
1126 Timeout is in milliseconds.
1127 ****************************************************************************/
1129 ssize_t read_smb_length_return_keepalive(int fd,
1131 unsigned int timeout)
1139 ok = (read_socket_with_timeout(fd,inbuf,4,4,timeout)
1142 ok = (read_data(fd,inbuf,4) == 4);
1148 len = smb_len(inbuf);
1149 msg_type = CVAL(inbuf,0);
1151 if (msg_type == SMBkeepalive) {
1152 DEBUG(5,("Got keepalive packet\n"));
1156 DEBUG(10,("got smb length of %lu\n",(unsigned long)len));
1161 /****************************************************************************
1162 Read 4 bytes of a smb packet and return the smb length of the packet.
1163 Store the result in the buffer. This version of the function will
1164 never return a session keepalive (length of zero).
1165 Timeout is in milliseconds.
1166 ****************************************************************************/
1168 ssize_t read_smb_length(int fd, char *inbuf, unsigned int timeout)
1173 len = read_smb_length_return_keepalive(fd, inbuf, timeout);
1178 /* Ignore session keepalives. */
1179 if(CVAL(inbuf,0) != SMBkeepalive)
1183 DEBUG(10,("read_smb_length: got smb length of %lu\n",
1184 (unsigned long)len));
1189 /****************************************************************************
1190 Read an smb from a fd. Note that the buffer *MUST* be of size
1191 BUFFER_SIZE+SAFETY_MARGIN.
1192 The timeout is in milliseconds.
1193 This function will return on receipt of a session keepalive packet.
1194 maxlen is the max number of bytes to return, not including the 4 byte
1195 length. If zero it means BUFFER_SIZE+SAFETY_MARGIN limit.
1196 Doesn't check the MAC on signed packets.
1197 ****************************************************************************/
1199 ssize_t receive_smb_raw(int fd,
1201 unsigned int timeout,
1206 set_smb_read_error(SMB_READ_OK);
1208 len = read_smb_length_return_keepalive(fd,buffer,timeout);
1210 DEBUG(10,("receive_smb_raw: length < 0!\n"));
1213 * Correct fix. smb_read_error may have already been
1214 * set. Only set it here if not already set. Global
1215 * variables still suck :-). JRA.
1218 cond_set_smb_read_error(SMB_READ_ERROR);
1223 * A WRITEX with CAP_LARGE_WRITEX can be 64k worth of data plus 65 bytes
1224 * of header. Don't print the error if this fits.... JRA.
1227 if (len > (BUFFER_SIZE + LARGE_WRITEX_HDR_SIZE)) {
1228 DEBUG(0,("Invalid packet length! (%lu bytes).\n",
1229 (unsigned long)len));
1230 if (len > BUFFER_SIZE + (SAFETY_MARGIN/2)) {
1233 * Correct fix. smb_read_error may have already been
1234 * set. Only set it here if not already set. Global
1235 * variables still suck :-). JRA.
1238 cond_set_smb_read_error(SMB_READ_ERROR);
1245 len = MIN(len,maxlen);
1249 ret = read_socket_with_timeout(fd,
1255 ret = read_data(fd,buffer+4,len);
1259 cond_set_smb_read_error(SMB_READ_ERROR);
1263 /* not all of samba3 properly checks for packet-termination
1264 * of strings. This ensures that we don't run off into
1266 SSVAL(buffer+4,len, 0);
1272 /****************************************************************************
1273 Wrapper for receive_smb_raw().
1274 Checks the MAC on signed packets.
1275 ****************************************************************************/
1277 bool receive_smb(int fd, char *buffer, unsigned int timeout)
1279 if (receive_smb_raw(fd, buffer, timeout, 0) < 0) {
1283 /* Check the incoming SMB signature. */
1284 if (!srv_check_sign_mac(buffer, true)) {
1285 DEBUG(0, ("receive_smb: SMB Signature verification "
1286 "failed on incoming packet!\n"));
1287 if (get_smb_read_error() == 0) {
1288 smb_read_error = SMB_READ_BAD_SIG;
1296 /****************************************************************************
1297 Send an smb to a fd.
1298 ****************************************************************************/
1300 bool send_smb(int fd, char *buffer)
1306 /* Sign the outgoing packet if required. */
1307 srv_calculate_sign_mac(buffer);
1309 len = smb_len(buffer) + 4;
1311 while (nwritten < len) {
1312 ret = write_data(fd,buffer+nwritten,len - nwritten);
1314 DEBUG(0,("Error writing %d bytes to client. %d. (%s)\n",
1315 (int)len,(int)ret, strerror(errno) ));
1324 /****************************************************************************
1325 Open a socket of the specified type, port, and address for incoming data.
1326 ****************************************************************************/
1328 int open_socket_in(int type,
1331 const struct sockaddr_storage *psock,
1334 struct sockaddr_storage sock;
1336 socklen_t slen = sizeof(struct sockaddr_in);
1340 #if defined(HAVE_IPV6)
1341 if (sock.ss_family == AF_INET6) {
1342 ((struct sockaddr_in6 *)&sock)->sin6_port = htons(port);
1343 slen = sizeof(struct sockaddr_in6);
1346 if (sock.ss_family == AF_INET) {
1347 ((struct sockaddr_in *)&sock)->sin_port = htons(port);
1350 res = socket(sock.ss_family, type, 0 );
1353 dbgtext( "open_socket_in(): socket() call failed: " );
1354 dbgtext( "%s\n", strerror( errno ) );
1359 /* This block sets/clears the SO_REUSEADDR and possibly SO_REUSEPORT. */
1361 int val = rebind ? 1 : 0;
1362 if( setsockopt(res,SOL_SOCKET,SO_REUSEADDR,
1363 (char *)&val,sizeof(val)) == -1 ) {
1364 if( DEBUGLVL( dlevel ) ) {
1365 dbgtext( "open_socket_in(): setsockopt: " );
1366 dbgtext( "SO_REUSEADDR = %s ",
1367 val?"true":"false" );
1368 dbgtext( "on port %d failed ", port );
1369 dbgtext( "with error = %s\n", strerror(errno) );
1373 if( setsockopt(res,SOL_SOCKET,SO_REUSEPORT,
1374 (char *)&val,sizeof(val)) == -1 ) {
1375 if( DEBUGLVL( dlevel ) ) {
1376 dbgtext( "open_socket_in(): setsockopt: ");
1377 dbgtext( "SO_REUSEPORT = %s ",
1378 val?"true":"false");
1379 dbgtext( "on port %d failed ", port);
1380 dbgtext( "with error = %s\n", strerror(errno));
1383 #endif /* SO_REUSEPORT */
1386 /* now we've got a socket - we need to bind it */
1387 if (bind(res, (struct sockaddr *)&sock, slen) == -1 ) {
1388 if( DEBUGLVL(dlevel) && (port == SMB_PORT1 ||
1389 port == SMB_PORT2 || port == NMB_PORT) ) {
1390 char addr[INET6_ADDRSTRLEN];
1391 print_sockaddr(addr, sizeof(addr),
1393 dbgtext( "bind failed on port %d ", port);
1394 dbgtext( "socket_addr = %s.\n", addr);
1395 dbgtext( "Error = %s\n", strerror(errno));
1401 DEBUG( 10, ( "bind succeeded on port %d\n", port ) );
1405 /****************************************************************************
1406 Create an outgoing socket. timeout is in milliseconds.
1407 **************************************************************************/
1409 int open_socket_out(int type,
1410 const struct sockaddr_storage *pss,
1414 char addr[INET6_ADDRSTRLEN];
1415 struct sockaddr_storage sock_out = *pss;
1417 int connect_loop = 10;
1420 /* create a socket to write to */
1421 res = socket(pss->ss_family, type, 0);
1423 DEBUG(0,("socket error (%s)\n", strerror(errno)));
1427 if (type != SOCK_STREAM) {
1431 #if defined(HAVE_IPV6)
1432 if (pss->ss_family == AF_INET6) {
1433 struct sockaddr_in6 *psa6 = (struct sockaddr_in6 *)&sock_out;
1434 psa6->sin6_port = htons(port);
1435 if (psa6->sin6_scope_id == 0 &&
1436 IN6_IS_ADDR_LINKLOCAL(&psa6->sin6_addr)) {
1437 setup_linklocal_scope_id(&sock_out);
1441 if (pss->ss_family == AF_INET) {
1442 struct sockaddr_in *psa = (struct sockaddr_in *)&sock_out;
1443 psa->sin_port = htons(port);
1446 /* set it non-blocking */
1447 set_blocking(res,false);
1449 print_sockaddr(addr, sizeof(addr), &sock_out);
1450 DEBUG(3,("Connecting to %s at port %u\n",
1452 (unsigned int)port));
1454 /* and connect it to the destination */
1457 ret = connect(res,(struct sockaddr *)&sock_out,sizeof(sock_out));
1459 /* Some systems return EAGAIN when they mean EINPROGRESS */
1460 if (ret < 0 && (errno == EINPROGRESS || errno == EALREADY ||
1461 errno == EAGAIN) && (connect_loop < timeout) ) {
1462 smb_msleep(connect_loop);
1463 timeout -= connect_loop;
1464 connect_loop += increment;
1465 if (increment < 250) {
1466 /* After 8 rounds we end up at a max of 255 msec */
1472 if (ret < 0 && (errno == EINPROGRESS || errno == EALREADY ||
1474 DEBUG(1,("timeout connecting to %s:%u\n",
1476 (unsigned int)port));
1482 if (ret < 0 && errno == EISCONN) {
1489 DEBUG(2,("error connecting to %s:%d (%s)\n",
1497 /* set it blocking again */
1498 set_blocking(res,true);
1503 /****************************************************************************
1504 Create an outgoing TCP socket to any of the addrs. This is for
1505 simultaneous connects to port 445 and 139 of a host or even a variety
1506 of DC's all of which are equivalent for our purposes.
1507 **************************************************************************/
1509 bool open_any_socket_out(struct sockaddr_storage *addrs, int num_addrs,
1510 int timeout, int *fd_index, int *fd)
1512 int i, resulting_index, res;
1516 fd_set r_fds, wr_fds;
1520 int connect_loop = 10000; /* 10 milliseconds */
1522 timeout *= 1000; /* convert to microseconds */
1524 sockets = SMB_MALLOC_ARRAY(int, num_addrs);
1526 if (sockets == NULL)
1529 resulting_index = -1;
1531 for (i=0; i<num_addrs; i++)
1534 for (i=0; i<num_addrs; i++) {
1535 sockets[i] = socket(addrs[i].ss_family, SOCK_STREAM, 0);
1538 set_blocking(sockets[i], false);
1542 good_connect = false;
1544 for (i=0; i<num_addrs; i++) {
1546 if (sockets[i] == -1)
1549 if (connect(sockets[i], (struct sockaddr *)&(addrs[i]),
1550 sizeof(*addrs)) == 0) {
1551 /* Rather unlikely as we are non-blocking, but it
1552 * might actually happen. */
1553 resulting_index = i;
1557 if (errno == EINPROGRESS || errno == EALREADY ||
1561 errno == EAGAIN || errno == EINTR) {
1562 /* These are the error messages that something is
1564 good_connect = true;
1565 } else if (errno != 0) {
1566 /* There was a direct error */
1572 if (!good_connect) {
1573 /* All of the connect's resulted in real error conditions */
1577 /* Lets see if any of the connect attempts succeeded */
1583 for (i=0; i<num_addrs; i++) {
1584 if (sockets[i] == -1)
1586 FD_SET(sockets[i], &wr_fds);
1587 FD_SET(sockets[i], &r_fds);
1588 if (sockets[i]>maxfd)
1593 tv.tv_usec = connect_loop;
1595 res = sys_select_intr(maxfd+1, &r_fds, &wr_fds, NULL, &tv);
1603 for (i=0; i<num_addrs; i++) {
1605 if (sockets[i] == -1)
1608 /* Stevens, Network Programming says that if there's a
1609 * successful connect, the socket is only writable. Upon an
1610 * error, it's both readable and writable. */
1612 if (FD_ISSET(sockets[i], &r_fds) &&
1613 FD_ISSET(sockets[i], &wr_fds)) {
1614 /* readable and writable, so it's an error */
1620 if (!FD_ISSET(sockets[i], &r_fds) &&
1621 FD_ISSET(sockets[i], &wr_fds)) {
1622 /* Only writable, so it's connected */
1623 resulting_index = i;
1630 timeout -= connect_loop;
1633 connect_loop *= 1.5;
1634 if (connect_loop > timeout)
1635 connect_loop = timeout;
1639 for (i=0; i<num_addrs; i++) {
1640 if (i == resulting_index)
1642 if (sockets[i] >= 0)
1646 if (resulting_index >= 0) {
1647 *fd_index = resulting_index;
1648 *fd = sockets[*fd_index];
1649 set_blocking(*fd, true);
1654 return (resulting_index >= 0);
1656 /****************************************************************************
1657 Open a connected UDP socket to host on port
1658 **************************************************************************/
1660 int open_udp_socket(const char *host, int port)
1662 int type = SOCK_DGRAM;
1663 struct sockaddr_in sock_out;
1665 struct in_addr addr;
1667 (void)interpret_addr2(&addr, host);
1669 res = socket(PF_INET, type, 0);
1674 memset((char *)&sock_out,'\0',sizeof(sock_out));
1675 putip((char *)&sock_out.sin_addr,(char *)&addr);
1676 sock_out.sin_port = htons(port);
1677 sock_out.sin_family = PF_INET;
1679 if (connect(res,(struct sockaddr *)&sock_out,sizeof(sock_out))) {
1687 /*******************************************************************
1688 Return the IP addr of the remote end of a socket as a string.
1689 Optionally return the struct sockaddr_storage.
1690 ******************************************************************/
1692 static const char *get_peer_addr_internal(int fd,
1694 size_t addr_buf_len,
1695 struct sockaddr_storage *pss,
1698 struct sockaddr_storage ss;
1699 socklen_t length = sizeof(ss);
1701 safe_strcpy(addr_buf,"0.0.0.0",addr_buf_len-1);
1710 if (plength == NULL) {
1714 if (getpeername(fd, (struct sockaddr *)pss, plength) < 0) {
1715 DEBUG(0,("getpeername failed. Error was %s\n",
1720 print_sockaddr_len(addr_buf,
1727 /*******************************************************************
1728 Matchname - determine if host name matches IP address. Used to
1729 confirm a hostname lookup to prevent spoof attacks.
1730 ******************************************************************/
1732 static bool matchname(const char *remotehost,
1733 const struct sockaddr_storage *pss,
1736 struct addrinfo *res = NULL;
1737 struct addrinfo *ailist = NULL;
1738 char addr_buf[INET6_ADDRSTRLEN];
1739 bool ret = interpret_string_addr_internal(&ailist,
1741 AI_ADDRCONFIG|AI_CANONNAME);
1743 if (!ret || ailist == NULL) {
1744 DEBUG(3,("matchname: getaddrinfo failed for "
1747 gai_strerror(ret) ));
1752 * Make sure that getaddrinfo() returns the "correct" host name.
1755 if (ailist->ai_canonname == NULL ||
1756 (!strequal(remotehost, ailist->ai_canonname) &&
1757 !strequal(remotehost, "localhost"))) {
1758 DEBUG(0,("matchname: host name/name mismatch: %s != %s\n",
1760 ailist->ai_canonname ?
1761 ailist->ai_canonname : "(NULL)"));
1762 freeaddrinfo(ailist);
1766 /* Look up the host address in the address list we just got. */
1767 for (res = ailist; res; res = res->ai_next) {
1768 if (!res->ai_addr) {
1771 if (addr_equal((const struct sockaddr_storage *)res->ai_addr,
1773 freeaddrinfo(ailist);
1779 * The host name does not map to the original host address. Perhaps
1780 * someone has compromised a name server. More likely someone botched
1781 * it, but that could be dangerous, too.
1784 DEBUG(0,("matchname: host name/address mismatch: %s != %s\n",
1785 print_sockaddr_len(addr_buf,
1789 ailist->ai_canonname ? ailist->ai_canonname : "(NULL)"));
1792 freeaddrinfo(ailist);
1797 /*******************************************************************
1798 Return the DNS name of the remote end of a socket.
1799 ******************************************************************/
1801 static char addr_buf_cache[INET6_ADDRSTRLEN];
1803 const char *get_peer_name(int fd, bool force_lookup)
1805 static pstring name_buf;
1806 char addr_buf[INET6_ADDRSTRLEN];
1807 struct sockaddr_storage ss;
1808 socklen_t length = sizeof(ss);
1813 /* reverse lookups can be *very* expensive, and in many
1814 situations won't work because many networks don't link dhcp
1815 with dns. To avoid the delay we avoid the lookup if
1817 if (!lp_hostname_lookups() && (force_lookup == false)) {
1818 pstrcpy(name_buf, get_peer_addr(fd, addr_buf, sizeof(addr_buf)));
1822 p = get_peer_addr_internal(fd, addr_buf, sizeof(addr_buf), &ss, &length);
1824 /* it might be the same as the last one - save some DNS work */
1825 if (strcmp(p, addr_buf_cache) == 0) {
1829 pstrcpy(name_buf,"UNKNOWN");
1834 safe_strcpy(addr_buf_cache, p, sizeof(addr_buf_cache)-1);
1836 /* Look up the remote host name. */
1837 ret = getnameinfo((struct sockaddr *)&ss,
1846 DEBUG(1,("get_peer_name: getnameinfo failed "
1847 "for %s with error %s\n",
1849 gai_strerror(ret)));
1850 pstrcpy(name_buf, p);
1852 if (!matchname(name_buf, &ss, length)) {
1853 DEBUG(0,("Matchname failed on %s %s\n",name_buf,p));
1854 pstrcpy(name_buf,"UNKNOWN");
1858 /* can't pass the same source and dest strings in when you
1859 use --enable-developer or the clobber_region() call will
1862 pstrcpy(tmp_name, name_buf );
1863 alpha_strcpy(name_buf, tmp_name, "_-.", sizeof(name_buf));
1864 if (strstr(name_buf,"..")) {
1865 pstrcpy(name_buf, "UNKNOWN");
1871 /*******************************************************************
1872 Return the IP addr of the remote end of a socket as a string.
1873 ******************************************************************/
1875 const char *get_peer_addr(int fd, char *addr, size_t addr_len)
1877 return get_peer_addr_internal(fd, addr, addr_len, NULL, NULL);
1880 /*******************************************************************
1881 Create protected unix domain socket.
1883 Some unixes cannot set permissions on a ux-dom-sock, so we
1884 have to make sure that the directory contains the protection
1885 permissions instead.
1886 ******************************************************************/
1888 int create_pipe_sock(const char *socket_dir,
1889 const char *socket_name,
1892 #ifdef HAVE_UNIXSOCKET
1893 struct sockaddr_un sunaddr;
1899 old_umask = umask(0);
1901 /* Create the socket directory or reuse the existing one */
1903 if (lstat(socket_dir, &st) == -1) {
1904 if (errno == ENOENT) {
1905 /* Create directory */
1906 if (mkdir(socket_dir, dir_perms) == -1) {
1907 DEBUG(0, ("error creating socket directory "
1908 "%s: %s\n", socket_dir,
1913 DEBUG(0, ("lstat failed on socket directory %s: %s\n",
1914 socket_dir, strerror(errno)));
1918 /* Check ownership and permission on existing directory */
1919 if (!S_ISDIR(st.st_mode)) {
1920 DEBUG(0, ("socket directory %s isn't a directory\n",
1924 if ((st.st_uid != sec_initial_uid()) ||
1925 ((st.st_mode & 0777) != dir_perms)) {
1926 DEBUG(0, ("invalid permissions on socket directory "
1927 "%s\n", socket_dir));
1932 /* Create the socket file */
1934 sock = socket(AF_UNIX, SOCK_STREAM, 0);
1941 pstr_sprintf(path, "%s/%s", socket_dir, socket_name);
1944 memset(&sunaddr, 0, sizeof(sunaddr));
1945 sunaddr.sun_family = AF_UNIX;
1946 safe_strcpy(sunaddr.sun_path, path, sizeof(sunaddr.sun_path)-1);
1948 if (bind(sock, (struct sockaddr *)&sunaddr, sizeof(sunaddr)) == -1) {
1949 DEBUG(0, ("bind failed on pipe socket %s: %s\n", path,
1954 if (listen(sock, 5) == -1) {
1955 DEBUG(0, ("listen failed on pipe socket %s: %s\n", path,
1971 DEBUG(0, ("create_pipe_sock: No Unix sockets on this system\n"));
1973 #endif /* HAVE_UNIXSOCKET */
1976 /****************************************************************************
1977 Get my own canonical name, including domain.
1978 ****************************************************************************/
1980 static fstring dnshostname_cache;
1982 bool get_mydnsfullname(fstring my_dnsname)
1984 if (!*dnshostname_cache) {
1985 struct addrinfo *res = NULL;
1988 /* get my host name */
1989 if (gethostname(dnshostname_cache, sizeof(dnshostname_cache)) == -1) {
1990 *dnshostname_cache = '\0';
1991 DEBUG(0,("get_mydnsfullname: gethostname failed\n"));
1995 /* Ensure null termination. */
1996 dnshostname_cache[sizeof(dnshostname_cache)-1] = '\0';
1998 ret = interpret_string_addr_internal(&res,
2000 AI_ADDRCONFIG|AI_CANONNAME);
2002 if (!ret || res == NULL) {
2003 DEBUG(3,("get_mydnsfullname: getaddrinfo failed for "
2006 gai_strerror(ret) ));
2011 * Make sure that getaddrinfo() returns the "correct" host name.
2014 if (res->ai_canonname == NULL) {
2015 DEBUG(3,("get_mydnsfullname: failed to get "
2016 "canonical name for %s\n",
2017 dnshostname_cache));
2023 fstrcpy(dnshostname_cache, res->ai_canonname);
2026 fstrcpy(my_dnsname, dnshostname_cache);
2030 /************************************************************
2032 ************************************************************/
2034 bool is_myname_or_ipaddr(const char *s)
2036 fstring name, dnsname;
2043 /* Santize the string from '\\name' */
2046 servername = strrchr_m(name, '\\' );
2053 /* Optimize for the common case */
2054 if (strequal(servername, global_myname())) {
2058 /* Check for an alias */
2059 if (is_myname(servername)) {
2063 /* Check for loopback */
2064 if (strequal(servername, "127.0.0.1") ||
2065 strequal(servername, "::1")) {
2069 if (strequal(servername, "localhost")) {
2073 /* Maybe it's my dns name */
2074 if (get_mydnsfullname(dnsname)) {
2075 if (strequal(servername, dnsname)) {
2080 /* Handle possible CNAME records - convert to an IP addr. */
2081 if (!is_ipaddress(servername)) {
2082 /* Use DNS to resolve the name, but only the first address */
2083 struct sockaddr_storage ss;
2084 if (interpret_string_addr(&ss, servername,0)) {
2085 print_sockaddr(name,
2092 /* Maybe its an IP address? */
2093 if (is_ipaddress(servername)) {
2094 struct sockaddr_storage ss;
2095 struct iface_struct nics[MAX_INTERFACES];
2098 if (!interpret_string_addr(&ss, servername, AI_NUMERICHOST)) {
2102 if (is_zero_addr(&ss) || is_loopback_addr(&ss)) {
2106 n = get_interfaces(nics, MAX_INTERFACES);
2107 for (i=0; i<n; i++) {
2108 if (addr_equal(&nics[i].ip, &ss)) {