Ticket 1551: Update GPL version from 2 to 3
[midnight-commander.git] / src / vfs / smbfs / helpers / lib / util_sock.c
blobc4b7e7a852dd49783a4f0644c49f27f95fc972bf
1 /*
2 Unix SMB/Netbios implementation.
3 Version 1.9.
4 Samba utility functions
6 Copyright (C) Andrew Tridgell 1992-1998
8 Copyright (C) 2011
9 The Free Software Foundation, Inc.
11 This file is part of the Midnight Commander.
13 The Midnight Commander is free software: you can redistribute it
14 and/or modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation, either version 3 of the License,
16 or (at your option) any later version.
18 The Midnight Commander is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #include "includes.h"
29 const char *unix_error_string (int error_num);
31 #ifdef WITH_SSL
32 #include <ssl.h>
33 #undef Realloc /* SSLeay defines this and samba has a function of this name */
34 extern SSL *ssl;
35 extern int sslFd;
36 #endif /* WITH_SSL */
38 extern int DEBUGLEVEL;
40 BOOL passive = False;
42 /* the client file descriptor */
43 int Client = -1;
45 /* the last IP received from */
46 struct in_addr lastip;
48 /* the last port received from */
49 int lastport=0;
52 int smb_read_error = 0;
55 /****************************************************************************
56 determine if a file descriptor is in fact a socket
57 ****************************************************************************/
58 BOOL is_a_socket(int fd)
60 int v;
61 unsigned int l;
62 l = sizeof(int);
63 return(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&v, &l) == 0);
67 enum SOCK_OPT_TYPES {OPT_BOOL,OPT_INT,OPT_ON};
69 static const struct
71 const char *name;
72 int level;
73 int option;
74 int value;
75 int opttype;
76 } socket_options[] = {
77 {"SO_KEEPALIVE", SOL_SOCKET, SO_KEEPALIVE, 0, OPT_BOOL},
78 {"SO_REUSEADDR", SOL_SOCKET, SO_REUSEADDR, 0, OPT_BOOL},
79 {"SO_BROADCAST", SOL_SOCKET, SO_BROADCAST, 0, OPT_BOOL},
80 #ifdef TCP_NODELAY
81 {"TCP_NODELAY", IPPROTO_TCP, TCP_NODELAY, 0, OPT_BOOL},
82 #endif
83 #ifdef IPTOS_LOWDELAY
84 {"IPTOS_LOWDELAY", IPPROTO_IP, IP_TOS, IPTOS_LOWDELAY, OPT_ON},
85 #endif
86 #ifdef IPTOS_THROUGHPUT
87 {"IPTOS_THROUGHPUT", IPPROTO_IP, IP_TOS, IPTOS_THROUGHPUT, OPT_ON},
88 #endif
89 #ifdef SO_SNDBUF
90 {"SO_SNDBUF", SOL_SOCKET, SO_SNDBUF, 0, OPT_INT},
91 #endif
92 #ifdef SO_RCVBUF
93 {"SO_RCVBUF", SOL_SOCKET, SO_RCVBUF, 0, OPT_INT},
94 #endif
95 #ifdef SO_SNDLOWAT
96 {"SO_SNDLOWAT", SOL_SOCKET, SO_SNDLOWAT, 0, OPT_INT},
97 #endif
98 #ifdef SO_RCVLOWAT
99 {"SO_RCVLOWAT", SOL_SOCKET, SO_RCVLOWAT, 0, OPT_INT},
100 #endif
101 #ifdef SO_SNDTIMEO
102 {"SO_SNDTIMEO", SOL_SOCKET, SO_SNDTIMEO, 0, OPT_INT},
103 #endif
104 #ifdef SO_RCVTIMEO
105 {"SO_RCVTIMEO", SOL_SOCKET, SO_RCVTIMEO, 0, OPT_INT},
106 #endif
107 {NULL,0,0,0,0}};
111 /****************************************************************************
112 set user socket options
113 ****************************************************************************/
114 void set_socket_options(int fd, char *options)
116 fstring tok;
118 while (next_token(&options,tok," \t,", sizeof(tok)))
120 int ret=0,i;
121 int value = 1;
122 char *p;
123 BOOL got_value = False;
125 if ((p = strchr(tok,'=')))
127 *p = 0;
128 value = atoi(p+1);
129 got_value = True;
132 for (i=0;socket_options[i].name;i++)
133 if (strequal(socket_options[i].name,tok))
134 break;
136 if (!socket_options[i].name)
138 DEBUG(0,("Unknown socket option %s\n",tok));
139 continue;
142 switch (socket_options[i].opttype)
144 case OPT_BOOL:
145 case OPT_INT:
146 ret = setsockopt(fd,socket_options[i].level,
147 socket_options[i].option,(char *)&value,sizeof(int));
148 break;
150 case OPT_ON:
151 if (got_value)
152 DEBUG(0,("syntax error - %s does not take a value\n",tok));
155 int on = socket_options[i].value;
156 ret = setsockopt(fd,socket_options[i].level,
157 socket_options[i].option,(char *)&on,sizeof(int));
159 break;
162 if (ret != 0)
163 DEBUG(0,("Failed to set socket option %s\n",tok));
169 /****************************************************************************
170 close the socket communication
171 ****************************************************************************/
172 void close_sockets(void )
174 #ifdef WITH_SSL
175 sslutil_disconnect(Client);
176 #endif /* WITH_SSL */
178 close(Client);
179 Client = -1;
184 /****************************************************************************
185 write to a socket
186 ****************************************************************************/
187 ssize_t write_socket(int fd,char *buf,size_t len)
189 ssize_t ret=0;
191 if (passive)
192 return(len);
193 DEBUG(6,("write_socket(%d,%d)\n",fd,(int)len));
194 ret = write_data(fd,buf,len);
196 DEBUG(6,("write_socket(%d,%d) wrote %d\n",fd,(int)len,(int)ret));
197 if(ret <= 0)
198 DEBUG(1,("write_socket: Error writing %d bytes to socket %d: ERRNO = %s\n",
199 (int)len, fd, unix_error_string (errno) ));
201 return(ret);
204 /****************************************************************************
205 read from a socket
206 ****************************************************************************/
207 ssize_t read_udp_socket(int fd,char *buf,size_t len)
209 ssize_t ret;
210 struct sockaddr_in sock;
211 unsigned int socklen;
213 socklen = sizeof(sock);
214 memset((char *)&sock,'\0',socklen);
215 memset((char *)&lastip,'\0',sizeof(lastip));
216 ret = (ssize_t)recvfrom(fd,buf,len,0,(struct sockaddr *)&sock,&socklen);
217 if (ret <= 0) {
218 DEBUG(2,("read socket failed. ERRNO=%s\n", unix_error_string (errno)));
219 return(0);
222 lastip = sock.sin_addr;
223 lastport = ntohs(sock.sin_port);
225 DEBUG(10,("read_udp_socket: lastip %s lastport %d read: %d\n",
226 inet_ntoa(lastip), lastport, (int)ret));
228 return(ret);
232 /****************************************************************************
233 read data from a device with a timout in msec.
234 mincount = if timeout, minimum to read before returning
235 maxcount = number to be read.
236 time_out = timeout in milliseconds
237 ****************************************************************************/
239 ssize_t read_with_timeout(int fd,char *buf,size_t mincnt,size_t maxcnt,unsigned int time_out)
241 fd_set fds;
242 int selrtn;
243 ssize_t readret;
244 size_t nread = 0;
245 struct timeval timeout;
247 /* just checking .... */
248 if (maxcnt <= 0) return(0);
250 smb_read_error = 0;
252 /* Blocking read */
253 if (time_out <= 0) {
254 if (mincnt == 0) mincnt = maxcnt;
256 while (nread < mincnt) {
257 #ifdef WITH_SSL
258 if(fd == sslFd){
259 readret = SSL_read(ssl, buf + nread, maxcnt - nread);
260 }else{
261 readret = read(fd, buf + nread, maxcnt - nread);
263 #else /* WITH_SSL */
264 readret = read(fd, buf + nread, maxcnt - nread);
265 #endif /* WITH_SSL */
267 if (readret == 0) {
268 DEBUG(5,("read_with_timeout: blocking read. EOF from client.\n"));
269 smb_read_error = READ_EOF;
270 return -1;
273 if (readret == -1) {
274 DEBUG(0,("read_with_timeout: read error = %s.\n", unix_error_string (errno) ));
275 smb_read_error = READ_ERROR;
276 return -1;
278 nread += readret;
280 return((ssize_t)nread);
283 /* Most difficult - timeout read */
284 /* If this is ever called on a disk file and
285 mincnt is greater then the filesize then
286 system performance will suffer severely as
287 select always returns true on disk files */
289 /* Set initial timeout */
290 timeout.tv_sec = (time_t)(time_out / 1000);
291 timeout.tv_usec = (long)(1000 * (time_out % 1000));
293 for (nread=0; nread < mincnt; )
295 FD_ZERO(&fds);
296 FD_SET(fd,&fds);
298 selrtn = sys_select(fd+1,&fds,&timeout);
300 /* Check if error */
301 if(selrtn == -1) {
302 /* something is wrong. Maybe the socket is dead? */
303 DEBUG(0,("read_with_timeout: timeout read. select error = %s.\n", unix_error_string (errno) ));
304 smb_read_error = READ_ERROR;
305 return -1;
308 /* Did we timeout ? */
309 if (selrtn == 0) {
310 DEBUG(10,("read_with_timeout: timeout read. select timed out.\n"));
311 smb_read_error = READ_TIMEOUT;
312 return -1;
315 #ifdef WITH_SSL
316 if(fd == sslFd){
317 readret = SSL_read(ssl, buf + nread, maxcnt - nread);
318 }else{
319 readret = read(fd, buf + nread, maxcnt - nread);
321 #else /* WITH_SSL */
322 readret = read(fd, buf+nread, maxcnt-nread);
323 #endif /* WITH_SSL */
325 if (readret == 0) {
326 /* we got EOF on the file descriptor */
327 DEBUG(5,("read_with_timeout: timeout read. EOF from client.\n"));
328 smb_read_error = READ_EOF;
329 return -1;
332 if (readret == -1) {
333 /* the descriptor is probably dead */
334 DEBUG(0,("read_with_timeout: timeout read. read error = %s.\n", unix_error_string (errno) ));
335 smb_read_error = READ_ERROR;
336 return -1;
339 nread += readret;
342 /* Return the number we got */
343 return((ssize_t)nread);
347 /****************************************************************************
348 send a keepalive packet (rfc1002)
349 ****************************************************************************/
350 BOOL send_keepalive(int client)
352 unsigned char buf[4];
354 buf[0] = 0x85;
355 buf[1] = buf[2] = buf[3] = 0;
357 return(write_data(client,(char *)buf,4) == 4);
362 /****************************************************************************
363 read data from the client, reading exactly N bytes.
364 ****************************************************************************/
365 ssize_t read_data(int fd,char *buffer,size_t N)
367 ssize_t ret;
368 size_t total=0;
370 smb_read_error = 0;
372 while (total < N)
374 #ifdef WITH_SSL
375 if(fd == sslFd){
376 ret = SSL_read(ssl, buffer + total, N - total);
377 }else{
378 ret = read(fd,buffer + total,N - total);
380 #else /* WITH_SSL */
381 ret = read(fd,buffer + total,N - total);
382 #endif /* WITH_SSL */
384 if (ret == 0)
386 DEBUG(10,("read_data: read of %d returned 0. Error = %s\n", (int)(N - total), unix_error_string (errno) ));
387 smb_read_error = READ_EOF;
388 return 0;
390 if (ret == -1)
392 DEBUG(0,("read_data: read failure for %d. Error = %s\n", (int)(N - total), unix_error_string (errno) ));
393 smb_read_error = READ_ERROR;
394 return -1;
396 total += ret;
398 return (ssize_t)total;
402 /****************************************************************************
403 write data to a fd
404 ****************************************************************************/
405 ssize_t write_data(int fd,char *buffer,size_t N)
407 size_t total=0;
408 ssize_t ret;
410 while (total < N)
412 #ifdef WITH_SSL
413 if(fd == sslFd){
414 ret = SSL_write(ssl,buffer + total,N - total);
415 }else{
416 ret = write(fd,buffer + total,N - total);
418 #else /* WITH_SSL */
419 ret = write(fd,buffer + total,N - total);
420 #endif /* WITH_SSL */
422 if (ret == -1) {
423 DEBUG(1,("write_data: write failure. Error = %s\n", unix_error_string (errno) ));
424 return -1;
426 if (ret == 0) return total;
428 total += ret;
430 return (ssize_t)total;
435 /****************************************************************************
436 read 4 bytes of a smb packet and return the smb length of the packet
437 store the result in the buffer
438 This version of the function will return a length of zero on receiving
439 a keepalive packet.
440 timeout is in milliseconds.
441 ****************************************************************************/
442 static ssize_t read_smb_length_return_keepalive(int fd,char *inbuf,unsigned int timeout)
444 ssize_t len=0;
445 int msg_type;
446 BOOL ok = False;
448 while (!ok)
450 if (timeout > 0)
451 ok = (read_with_timeout(fd,inbuf,4,4,timeout) == 4);
452 else
453 ok = (read_data(fd,inbuf,4) == 4);
455 if (!ok)
456 return(-1);
458 len = smb_len(inbuf);
459 msg_type = CVAL(inbuf,0);
461 if (msg_type == 0x85)
462 DEBUG(5,("Got keepalive packet\n"));
465 DEBUG(10,("got smb length of %d\n", (int)len));
467 return(len);
469 #if 0
470 /****************************************************************************
471 read 4 bytes of a smb packet and return the smb length of the packet
472 store the result in the buffer. This version of the function will
473 never return a session keepalive (length of zero).
474 timeout is in milliseconds.
475 ****************************************************************************/
476 ssize_t read_smb_length(int fd,char *inbuf,unsigned int timeout)
478 ssize_t len;
480 for(;;)
482 len = read_smb_length_return_keepalive(fd, inbuf, timeout);
484 if(len < 0)
485 return len;
487 /* Ignore session keepalives. */
488 if(CVAL(inbuf,0) != 0x85)
489 break;
492 DEBUG(10,("read_smb_length: got smb length of %d\n",len));
494 return len;
496 #endif /* 0 */
497 /****************************************************************************
498 read an smb from a fd. Note that the buffer *MUST* be of size
499 BUFFER_SIZE+SAFETY_MARGIN.
500 The timeout is in milliseconds.
501 This function will return on a
502 receipt of a session keepalive packet.
503 ****************************************************************************/
504 BOOL receive_smb(int fd,char *buffer, unsigned int timeout)
506 ssize_t len,ret;
508 smb_read_error = 0;
510 memset(buffer,'\0',smb_size + 100);
512 len = read_smb_length_return_keepalive(fd,buffer,timeout);
513 if (len < 0)
515 DEBUG(10,("receive_smb: length < 0!\n"));
516 return(False);
519 if (len > BUFFER_SIZE) {
520 DEBUG(0,("Invalid packet length! (%d bytes).\n", (int)len));
521 if (len > BUFFER_SIZE + (SAFETY_MARGIN/2))
523 exit(1);
527 if(len > 0) {
528 ret = read_data(fd,buffer+4,len);
529 if (ret != len) {
530 smb_read_error = READ_ERROR;
531 return False;
534 return(True);
537 /****************************************************************************
538 read an smb from a fd ignoring all keepalive packets. Note that the buffer
539 *MUST* be of size BUFFER_SIZE+SAFETY_MARGIN.
540 The timeout is in milliseconds
542 This is exactly the same as receive_smb except that it never returns
543 a session keepalive packet (just as receive_smb used to do).
544 receive_smb was changed to return keepalives as the oplock processing means this call
545 should never go into a blocking read.
546 ****************************************************************************/
548 BOOL client_receive_smb(int fd,char *buffer, unsigned int timeout)
550 BOOL ret;
552 for(;;)
554 ret = receive_smb(fd, buffer, timeout);
556 if (!ret)
558 DEBUG(10,("client_receive_smb failed\n"));
559 show_msg(buffer);
560 return ret;
563 /* Ignore session keepalive packets. */
564 if(CVAL(buffer,0) != 0x85)
565 break;
567 show_msg(buffer);
568 return ret;
571 /****************************************************************************
572 send an null session message to a fd
573 ****************************************************************************/
574 #if 0
575 BOOL send_null_session_msg(int fd)
577 ssize_t ret;
578 uint32 blank = 0;
579 size_t len = 4;
580 size_t nwritten=0;
581 char *buffer = (char *)&blank;
583 while (nwritten < len)
585 ret = write_socket(fd,buffer+nwritten,len - nwritten);
586 if (ret <= 0)
588 DEBUG(0,("send_null_session_msg: Error writing %d bytes to client. %d. Exiting\n",(int)len,(int)ret));
589 close_sockets();
590 exit(1);
592 nwritten += ret;
595 DEBUG(10,("send_null_session_msg: sent 4 null bytes to client.\n"));
596 return True;
599 /****************************************************************************
600 send an smb to a fd
601 ****************************************************************************/
602 BOOL send_smb(int fd,char *buffer)
604 size_t len;
605 size_t nwritten=0;
606 ssize_t ret;
607 len = smb_len(buffer) + 4;
609 while (nwritten < len)
611 ret = write_socket(fd,buffer+nwritten,len - nwritten);
612 if (ret <= 0)
614 DEBUG(0,("Error writing %d bytes to client. %d. Exiting\n",(int)len,(int)ret));
615 close_sockets();
616 exit(1);
618 nwritten += ret;
621 return True;
626 /****************************************************************************
627 send a single packet to a port on another machine
628 ****************************************************************************/
629 BOOL send_one_packet(char *buf,int len,struct in_addr ip,int port,int type)
631 BOOL ret;
632 int out_fd;
633 struct sockaddr_in sock_out;
635 if (passive)
636 return(True);
638 /* create a socket to write to */
639 out_fd = socket(AF_INET, type, 0);
640 if (out_fd == -1)
642 DEBUG(0,("socket failed"));
643 return False;
646 /* set the address and port */
647 memset((char *)&sock_out,'\0',sizeof(sock_out));
648 putip((char *)&sock_out.sin_addr,(char *)&ip);
649 sock_out.sin_port = htons( port );
650 sock_out.sin_family = AF_INET;
652 if (DEBUGLEVEL > 0)
653 DEBUG(3,("sending a packet of len %d to (%s) on port %d of type %s\n",
654 len,inet_ntoa(ip),port,type==SOCK_DGRAM?"DGRAM":"STREAM"));
656 /* send it */
657 ret = (sendto(out_fd,buf,len,0,(struct sockaddr *)&sock_out,sizeof(sock_out)) >= 0);
659 if (!ret)
660 DEBUG(0,("Packet send to %s(%d) failed ERRNO=%s\n",
661 inet_ntoa(ip),port,unix_error_string (errno)));
663 close(out_fd);
664 return(ret);
666 #endif /* 0 */
668 /****************************************************************************
669 open a socket of the specified type, port and address for incoming data
670 ****************************************************************************/
671 int open_socket_in(int type, int port, int dlevel,uint32 socket_addr, BOOL rebind)
673 struct hostent *hp;
674 struct sockaddr_in sock;
675 pstring host_name;
676 int res;
678 /* get my host name */
679 if (gethostname(host_name, MAXHOSTNAMELEN) == -1)
680 { DEBUG(0,("gethostname failed\n")); return -1; }
682 /* get host info */
683 if ((hp = Get_Hostbyname(host_name)) == 0)
685 DEBUG(0,( "Get_Hostbyname: Unknown host %s\n",host_name));
686 return -1;
689 memset((char *)&sock,'\0',sizeof(sock));
690 memcpy((char *)&sock.sin_addr,(char *)hp->h_addr, hp->h_length);
692 #ifdef HAVE_SOCK_SIN_LEN
693 sock.sin_len = sizeof(sock);
694 #endif
695 sock.sin_port = htons( port );
696 sock.sin_family = hp->h_addrtype;
697 sock.sin_addr.s_addr = socket_addr;
698 res = socket(hp->h_addrtype, type, 0);
699 if (res == -1)
700 { DEBUG(0,("socket failed\n")); return -1; }
703 int val=1;
704 if(rebind)
705 val=1;
706 else
707 val=0;
708 setsockopt(res,SOL_SOCKET,SO_REUSEADDR,(char *)&val,sizeof(val));
711 /* now we've got a socket - we need to bind it */
712 if (bind(res, (struct sockaddr * ) &sock,sizeof(sock)) < 0)
714 if (port) {
715 if (port == SMB_PORT || port == NMB_PORT)
716 DEBUG(dlevel,("bind failed on port %d socket_addr=%s (%s)\n",
717 port,inet_ntoa(sock.sin_addr),unix_error_string (errno)));
718 close(res);
720 if (dlevel > 0 && port < 1000)
721 port = 7999;
723 if (port >= 1000 && port < 9000)
724 return(open_socket_in(type,port+1,dlevel,socket_addr,rebind));
727 return(-1);
729 DEBUG(3,("bind succeeded on port %d\n",port));
731 return res;
735 /****************************************************************************
736 create an outgoing socket. timeout is in milliseconds.
737 **************************************************************************/
738 int open_socket_out(int type, struct in_addr *addr, int port ,int timeout)
740 struct sockaddr_in sock_out;
741 int res,ret;
742 int connect_loop = 250; /* 250 milliseconds */
743 int loops = (timeout) / connect_loop;
745 /* create a socket to write to */
746 res = socket(PF_INET, type, 0);
747 if (res == -1)
748 { DEBUG(0,("socket error\n")); return -1; }
750 if (type != SOCK_STREAM) return(res);
752 memset((char *)&sock_out,'\0',sizeof(sock_out));
753 putip((char *)&sock_out.sin_addr,(char *)addr);
755 sock_out.sin_port = htons( port );
756 sock_out.sin_family = PF_INET;
758 /* set it non-blocking */
759 set_blocking(res,False);
761 DEBUG(3,("Connecting to %s at port %d\n",inet_ntoa(*addr),port));
763 /* and connect it to the destination */
764 connect_again:
765 ret = connect(res,(struct sockaddr *)&sock_out,sizeof(sock_out));
767 /* Some systems return EAGAIN when they mean EINPROGRESS */
768 if (ret < 0 && (errno == EINPROGRESS || errno == EALREADY ||
769 errno == EAGAIN) && loops--) {
770 msleep(connect_loop);
771 goto connect_again;
774 if (ret < 0 && (errno == EINPROGRESS || errno == EALREADY ||
775 errno == EAGAIN)) {
776 DEBUG(1,("timeout connecting to %s:%d\n",inet_ntoa(*addr),port));
777 close(res);
778 return -1;
781 #ifdef EISCONN
782 if (ret < 0 && errno == EISCONN) {
783 errno = 0;
784 ret = 0;
786 #endif
788 if (ret < 0) {
789 DEBUG(1,("error connecting to %s:%d (%s)\n",
790 inet_ntoa(*addr),port,unix_error_string (errno)));
791 close(res);
792 return -1;
795 /* set it blocking again */
796 set_blocking(res,True);
798 return res;
802 /*******************************************************************
803 Reset the 'done' variables so after a client process is created
804 from a fork call these calls will be re-done. This should be
805 expanded if more variables need reseting.
806 ******************************************************************/
808 static BOOL global_client_name_done = False;
809 static BOOL global_client_addr_done = False;
811 /*******************************************************************
812 return the DNS name of the client
813 ******************************************************************/
814 char *client_name(int fd)
816 struct sockaddr sa;
817 struct sockaddr_in *sockin = (struct sockaddr_in *) (&sa);
818 unsigned int length = sizeof(sa);
819 static pstring name_buf;
820 struct hostent *hp;
821 static int last_fd=-1;
823 if (global_client_name_done && last_fd == fd)
824 return name_buf;
826 last_fd = fd;
827 global_client_name_done = False;
829 pstrcpy(name_buf,"UNKNOWN");
831 if (fd == -1) {
832 return name_buf;
835 if (getpeername(fd, &sa, &length) < 0) {
836 DEBUG(0,("getpeername failed. Error was %s\n", unix_error_string (errno) ));
837 return name_buf;
840 /* Look up the remote host name. */
841 if ((hp = gethostbyaddr((char *) &sockin->sin_addr,
842 sizeof(sockin->sin_addr),
843 AF_INET)) == 0) {
844 DEBUG(1,("Gethostbyaddr failed for %s\n",client_addr(fd)));
845 StrnCpy(name_buf,client_addr(fd),sizeof(name_buf) - 1);
846 } else {
847 StrnCpy(name_buf,(char *)hp->h_name,sizeof(name_buf) - 1);
848 if (!matchname(name_buf, sockin->sin_addr)) {
849 DEBUG(0,("Matchname failed on %s %s\n",name_buf,client_addr(fd)));
850 pstrcpy(name_buf,"UNKNOWN");
853 global_client_name_done = True;
854 return name_buf;
857 /*******************************************************************
858 return the IP addr of the client as a string
859 ******************************************************************/
860 char *client_addr(int fd)
862 struct sockaddr sa;
863 struct sockaddr_in *sockin = (struct sockaddr_in *) (&sa);
864 unsigned int length = sizeof(sa);
865 static fstring addr_buf;
866 static int last_fd = -1;
868 if (global_client_addr_done && fd == last_fd)
869 return addr_buf;
871 last_fd = fd;
872 global_client_addr_done = False;
874 fstrcpy(addr_buf,"0.0.0.0");
876 if (fd == -1) {
877 return addr_buf;
880 if (getpeername(fd, &sa, &length) < 0) {
881 DEBUG(0,("getpeername failed. Error was %s\n", unix_error_string (errno) ));
882 return addr_buf;
885 fstrcpy(addr_buf,(char *)inet_ntoa(sockin->sin_addr));
887 global_client_addr_done = True;
888 return addr_buf;