Removed calls to strerror() function from own libsamba implementation.
[midnight-commander.git] / vfs / samba / lib / util_sock.c
blobe8c7a205ad611d51abbcbe5eddc15d5083d51273
1 /*
2 Unix SMB/Netbios implementation.
3 Version 1.9.
4 Samba utility functions
5 Copyright (C) Andrew Tridgell 1992-1998
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #include "includes.h"
24 const char *unix_error_string (int error_num);
26 #ifdef WITH_SSL
27 #include <ssl.h>
28 #undef Realloc /* SSLeay defines this and samba has a function of this name */
29 extern SSL *ssl;
30 extern int sslFd;
31 #endif /* WITH_SSL */
33 extern int DEBUGLEVEL;
35 BOOL passive = False;
37 /* the client file descriptor */
38 int Client = -1;
40 /* the last IP received from */
41 struct in_addr lastip;
43 /* the last port received from */
44 int lastport=0;
47 int smb_read_error = 0;
50 /****************************************************************************
51 determine if a file descriptor is in fact a socket
52 ****************************************************************************/
53 BOOL is_a_socket(int fd)
55 int v;
56 unsigned int l;
57 l = sizeof(int);
58 return(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&v, &l) == 0);
62 enum SOCK_OPT_TYPES {OPT_BOOL,OPT_INT,OPT_ON};
64 static const struct
66 const char *name;
67 int level;
68 int option;
69 int value;
70 int opttype;
71 } socket_options[] = {
72 {"SO_KEEPALIVE", SOL_SOCKET, SO_KEEPALIVE, 0, OPT_BOOL},
73 {"SO_REUSEADDR", SOL_SOCKET, SO_REUSEADDR, 0, OPT_BOOL},
74 {"SO_BROADCAST", SOL_SOCKET, SO_BROADCAST, 0, OPT_BOOL},
75 #ifdef TCP_NODELAY
76 {"TCP_NODELAY", IPPROTO_TCP, TCP_NODELAY, 0, OPT_BOOL},
77 #endif
78 #ifdef IPTOS_LOWDELAY
79 {"IPTOS_LOWDELAY", IPPROTO_IP, IP_TOS, IPTOS_LOWDELAY, OPT_ON},
80 #endif
81 #ifdef IPTOS_THROUGHPUT
82 {"IPTOS_THROUGHPUT", IPPROTO_IP, IP_TOS, IPTOS_THROUGHPUT, OPT_ON},
83 #endif
84 #ifdef SO_SNDBUF
85 {"SO_SNDBUF", SOL_SOCKET, SO_SNDBUF, 0, OPT_INT},
86 #endif
87 #ifdef SO_RCVBUF
88 {"SO_RCVBUF", SOL_SOCKET, SO_RCVBUF, 0, OPT_INT},
89 #endif
90 #ifdef SO_SNDLOWAT
91 {"SO_SNDLOWAT", SOL_SOCKET, SO_SNDLOWAT, 0, OPT_INT},
92 #endif
93 #ifdef SO_RCVLOWAT
94 {"SO_RCVLOWAT", SOL_SOCKET, SO_RCVLOWAT, 0, OPT_INT},
95 #endif
96 #ifdef SO_SNDTIMEO
97 {"SO_SNDTIMEO", SOL_SOCKET, SO_SNDTIMEO, 0, OPT_INT},
98 #endif
99 #ifdef SO_RCVTIMEO
100 {"SO_RCVTIMEO", SOL_SOCKET, SO_RCVTIMEO, 0, OPT_INT},
101 #endif
102 {NULL,0,0,0,0}};
106 /****************************************************************************
107 set user socket options
108 ****************************************************************************/
109 void set_socket_options(int fd, char *options)
111 fstring tok;
113 while (next_token(&options,tok," \t,", sizeof(tok)))
115 int ret=0,i;
116 int value = 1;
117 char *p;
118 BOOL got_value = False;
120 if ((p = strchr(tok,'=')))
122 *p = 0;
123 value = atoi(p+1);
124 got_value = True;
127 for (i=0;socket_options[i].name;i++)
128 if (strequal(socket_options[i].name,tok))
129 break;
131 if (!socket_options[i].name)
133 DEBUG(0,("Unknown socket option %s\n",tok));
134 continue;
137 switch (socket_options[i].opttype)
139 case OPT_BOOL:
140 case OPT_INT:
141 ret = setsockopt(fd,socket_options[i].level,
142 socket_options[i].option,(char *)&value,sizeof(int));
143 break;
145 case OPT_ON:
146 if (got_value)
147 DEBUG(0,("syntax error - %s does not take a value\n",tok));
150 int on = socket_options[i].value;
151 ret = setsockopt(fd,socket_options[i].level,
152 socket_options[i].option,(char *)&on,sizeof(int));
154 break;
157 if (ret != 0)
158 DEBUG(0,("Failed to set socket option %s\n",tok));
164 /****************************************************************************
165 close the socket communication
166 ****************************************************************************/
167 void close_sockets(void )
169 #ifdef WITH_SSL
170 sslutil_disconnect(Client);
171 #endif /* WITH_SSL */
173 close(Client);
174 Client = -1;
179 /****************************************************************************
180 write to a socket
181 ****************************************************************************/
182 ssize_t write_socket(int fd,char *buf,size_t len)
184 ssize_t ret=0;
186 if (passive)
187 return(len);
188 DEBUG(6,("write_socket(%d,%d)\n",fd,(int)len));
189 ret = write_data(fd,buf,len);
191 DEBUG(6,("write_socket(%d,%d) wrote %d\n",fd,(int)len,(int)ret));
192 if(ret <= 0)
193 DEBUG(1,("write_socket: Error writing %d bytes to socket %d: ERRNO = %s\n",
194 (int)len, fd, unix_error_string (errno) ));
196 return(ret);
199 /****************************************************************************
200 read from a socket
201 ****************************************************************************/
202 ssize_t read_udp_socket(int fd,char *buf,size_t len)
204 ssize_t ret;
205 struct sockaddr_in sock;
206 unsigned int socklen;
208 socklen = sizeof(sock);
209 memset((char *)&sock,'\0',socklen);
210 memset((char *)&lastip,'\0',sizeof(lastip));
211 ret = (ssize_t)recvfrom(fd,buf,len,0,(struct sockaddr *)&sock,&socklen);
212 if (ret <= 0) {
213 DEBUG(2,("read socket failed. ERRNO=%s\n", unix_error_string (errno)));
214 return(0);
217 lastip = sock.sin_addr;
218 lastport = ntohs(sock.sin_port);
220 DEBUG(10,("read_udp_socket: lastip %s lastport %d read: %d\n",
221 inet_ntoa(lastip), lastport, (int)ret));
223 return(ret);
227 /****************************************************************************
228 read data from a device with a timout in msec.
229 mincount = if timeout, minimum to read before returning
230 maxcount = number to be read.
231 time_out = timeout in milliseconds
232 ****************************************************************************/
234 ssize_t read_with_timeout(int fd,char *buf,size_t mincnt,size_t maxcnt,unsigned int time_out)
236 fd_set fds;
237 int selrtn;
238 ssize_t readret;
239 size_t nread = 0;
240 struct timeval timeout;
242 /* just checking .... */
243 if (maxcnt <= 0) return(0);
245 smb_read_error = 0;
247 /* Blocking read */
248 if (time_out <= 0) {
249 if (mincnt == 0) mincnt = maxcnt;
251 while (nread < mincnt) {
252 #ifdef WITH_SSL
253 if(fd == sslFd){
254 readret = SSL_read(ssl, buf + nread, maxcnt - nread);
255 }else{
256 readret = read(fd, buf + nread, maxcnt - nread);
258 #else /* WITH_SSL */
259 readret = read(fd, buf + nread, maxcnt - nread);
260 #endif /* WITH_SSL */
262 if (readret == 0) {
263 DEBUG(5,("read_with_timeout: blocking read. EOF from client.\n"));
264 smb_read_error = READ_EOF;
265 return -1;
268 if (readret == -1) {
269 DEBUG(0,("read_with_timeout: read error = %s.\n", unix_error_string (errno) ));
270 smb_read_error = READ_ERROR;
271 return -1;
273 nread += readret;
275 return((ssize_t)nread);
278 /* Most difficult - timeout read */
279 /* If this is ever called on a disk file and
280 mincnt is greater then the filesize then
281 system performance will suffer severely as
282 select always returns true on disk files */
284 /* Set initial timeout */
285 timeout.tv_sec = (time_t)(time_out / 1000);
286 timeout.tv_usec = (long)(1000 * (time_out % 1000));
288 for (nread=0; nread < mincnt; )
290 FD_ZERO(&fds);
291 FD_SET(fd,&fds);
293 selrtn = sys_select(fd+1,&fds,&timeout);
295 /* Check if error */
296 if(selrtn == -1) {
297 /* something is wrong. Maybe the socket is dead? */
298 DEBUG(0,("read_with_timeout: timeout read. select error = %s.\n", unix_error_string (errno) ));
299 smb_read_error = READ_ERROR;
300 return -1;
303 /* Did we timeout ? */
304 if (selrtn == 0) {
305 DEBUG(10,("read_with_timeout: timeout read. select timed out.\n"));
306 smb_read_error = READ_TIMEOUT;
307 return -1;
310 #ifdef WITH_SSL
311 if(fd == sslFd){
312 readret = SSL_read(ssl, buf + nread, maxcnt - nread);
313 }else{
314 readret = read(fd, buf + nread, maxcnt - nread);
316 #else /* WITH_SSL */
317 readret = read(fd, buf+nread, maxcnt-nread);
318 #endif /* WITH_SSL */
320 if (readret == 0) {
321 /* we got EOF on the file descriptor */
322 DEBUG(5,("read_with_timeout: timeout read. EOF from client.\n"));
323 smb_read_error = READ_EOF;
324 return -1;
327 if (readret == -1) {
328 /* the descriptor is probably dead */
329 DEBUG(0,("read_with_timeout: timeout read. read error = %s.\n", unix_error_string (errno) ));
330 smb_read_error = READ_ERROR;
331 return -1;
334 nread += readret;
337 /* Return the number we got */
338 return((ssize_t)nread);
342 /****************************************************************************
343 send a keepalive packet (rfc1002)
344 ****************************************************************************/
345 BOOL send_keepalive(int client)
347 unsigned char buf[4];
349 buf[0] = 0x85;
350 buf[1] = buf[2] = buf[3] = 0;
352 return(write_data(client,(char *)buf,4) == 4);
357 /****************************************************************************
358 read data from the client, reading exactly N bytes.
359 ****************************************************************************/
360 ssize_t read_data(int fd,char *buffer,size_t N)
362 ssize_t ret;
363 size_t total=0;
365 smb_read_error = 0;
367 while (total < N)
369 #ifdef WITH_SSL
370 if(fd == sslFd){
371 ret = SSL_read(ssl, buffer + total, N - total);
372 }else{
373 ret = read(fd,buffer + total,N - total);
375 #else /* WITH_SSL */
376 ret = read(fd,buffer + total,N - total);
377 #endif /* WITH_SSL */
379 if (ret == 0)
381 DEBUG(10,("read_data: read of %d returned 0. Error = %s\n", (int)(N - total), unix_error_string (errno) ));
382 smb_read_error = READ_EOF;
383 return 0;
385 if (ret == -1)
387 DEBUG(0,("read_data: read failure for %d. Error = %s\n", (int)(N - total), unix_error_string (errno) ));
388 smb_read_error = READ_ERROR;
389 return -1;
391 total += ret;
393 return (ssize_t)total;
397 /****************************************************************************
398 write data to a fd
399 ****************************************************************************/
400 ssize_t write_data(int fd,char *buffer,size_t N)
402 size_t total=0;
403 ssize_t ret;
405 while (total < N)
407 #ifdef WITH_SSL
408 if(fd == sslFd){
409 ret = SSL_write(ssl,buffer + total,N - total);
410 }else{
411 ret = write(fd,buffer + total,N - total);
413 #else /* WITH_SSL */
414 ret = write(fd,buffer + total,N - total);
415 #endif /* WITH_SSL */
417 if (ret == -1) {
418 DEBUG(1,("write_data: write failure. Error = %s\n", unix_error_string (errno) ));
419 return -1;
421 if (ret == 0) return total;
423 total += ret;
425 return (ssize_t)total;
430 /****************************************************************************
431 read 4 bytes of a smb packet and return the smb length of the packet
432 store the result in the buffer
433 This version of the function will return a length of zero on receiving
434 a keepalive packet.
435 timeout is in milliseconds.
436 ****************************************************************************/
437 static ssize_t read_smb_length_return_keepalive(int fd,char *inbuf,unsigned int timeout)
439 ssize_t len=0;
440 int msg_type;
441 BOOL ok = False;
443 while (!ok)
445 if (timeout > 0)
446 ok = (read_with_timeout(fd,inbuf,4,4,timeout) == 4);
447 else
448 ok = (read_data(fd,inbuf,4) == 4);
450 if (!ok)
451 return(-1);
453 len = smb_len(inbuf);
454 msg_type = CVAL(inbuf,0);
456 if (msg_type == 0x85)
457 DEBUG(5,("Got keepalive packet\n"));
460 DEBUG(10,("got smb length of %d\n", (int)len));
462 return(len);
464 #if 0
465 /****************************************************************************
466 read 4 bytes of a smb packet and return the smb length of the packet
467 store the result in the buffer. This version of the function will
468 never return a session keepalive (length of zero).
469 timeout is in milliseconds.
470 ****************************************************************************/
471 ssize_t read_smb_length(int fd,char *inbuf,unsigned int timeout)
473 ssize_t len;
475 for(;;)
477 len = read_smb_length_return_keepalive(fd, inbuf, timeout);
479 if(len < 0)
480 return len;
482 /* Ignore session keepalives. */
483 if(CVAL(inbuf,0) != 0x85)
484 break;
487 DEBUG(10,("read_smb_length: got smb length of %d\n",len));
489 return len;
491 #endif /* 0 */
492 /****************************************************************************
493 read an smb from a fd. Note that the buffer *MUST* be of size
494 BUFFER_SIZE+SAFETY_MARGIN.
495 The timeout is in milliseconds.
496 This function will return on a
497 receipt of a session keepalive packet.
498 ****************************************************************************/
499 BOOL receive_smb(int fd,char *buffer, unsigned int timeout)
501 ssize_t len,ret;
503 smb_read_error = 0;
505 memset(buffer,'\0',smb_size + 100);
507 len = read_smb_length_return_keepalive(fd,buffer,timeout);
508 if (len < 0)
510 DEBUG(10,("receive_smb: length < 0!\n"));
511 return(False);
514 if (len > BUFFER_SIZE) {
515 DEBUG(0,("Invalid packet length! (%d bytes).\n", (int)len));
516 if (len > BUFFER_SIZE + (SAFETY_MARGIN/2))
518 exit(1);
522 if(len > 0) {
523 ret = read_data(fd,buffer+4,len);
524 if (ret != len) {
525 smb_read_error = READ_ERROR;
526 return False;
529 return(True);
532 /****************************************************************************
533 read an smb from a fd ignoring all keepalive packets. Note that the buffer
534 *MUST* be of size BUFFER_SIZE+SAFETY_MARGIN.
535 The timeout is in milliseconds
537 This is exactly the same as receive_smb except that it never returns
538 a session keepalive packet (just as receive_smb used to do).
539 receive_smb was changed to return keepalives as the oplock processing means this call
540 should never go into a blocking read.
541 ****************************************************************************/
543 BOOL client_receive_smb(int fd,char *buffer, unsigned int timeout)
545 BOOL ret;
547 for(;;)
549 ret = receive_smb(fd, buffer, timeout);
551 if (!ret)
553 DEBUG(10,("client_receive_smb failed\n"));
554 show_msg(buffer);
555 return ret;
558 /* Ignore session keepalive packets. */
559 if(CVAL(buffer,0) != 0x85)
560 break;
562 show_msg(buffer);
563 return ret;
566 /****************************************************************************
567 send an null session message to a fd
568 ****************************************************************************/
569 #if 0
570 BOOL send_null_session_msg(int fd)
572 ssize_t ret;
573 uint32 blank = 0;
574 size_t len = 4;
575 size_t nwritten=0;
576 char *buffer = (char *)&blank;
578 while (nwritten < len)
580 ret = write_socket(fd,buffer+nwritten,len - nwritten);
581 if (ret <= 0)
583 DEBUG(0,("send_null_session_msg: Error writing %d bytes to client. %d. Exiting\n",(int)len,(int)ret));
584 close_sockets();
585 exit(1);
587 nwritten += ret;
590 DEBUG(10,("send_null_session_msg: sent 4 null bytes to client.\n"));
591 return True;
594 /****************************************************************************
595 send an smb to a fd
596 ****************************************************************************/
597 BOOL send_smb(int fd,char *buffer)
599 size_t len;
600 size_t nwritten=0;
601 ssize_t ret;
602 len = smb_len(buffer) + 4;
604 while (nwritten < len)
606 ret = write_socket(fd,buffer+nwritten,len - nwritten);
607 if (ret <= 0)
609 DEBUG(0,("Error writing %d bytes to client. %d. Exiting\n",(int)len,(int)ret));
610 close_sockets();
611 exit(1);
613 nwritten += ret;
616 return True;
621 /****************************************************************************
622 send a single packet to a port on another machine
623 ****************************************************************************/
624 BOOL send_one_packet(char *buf,int len,struct in_addr ip,int port,int type)
626 BOOL ret;
627 int out_fd;
628 struct sockaddr_in sock_out;
630 if (passive)
631 return(True);
633 /* create a socket to write to */
634 out_fd = socket(AF_INET, type, 0);
635 if (out_fd == -1)
637 DEBUG(0,("socket failed"));
638 return False;
641 /* set the address and port */
642 memset((char *)&sock_out,'\0',sizeof(sock_out));
643 putip((char *)&sock_out.sin_addr,(char *)&ip);
644 sock_out.sin_port = htons( port );
645 sock_out.sin_family = AF_INET;
647 if (DEBUGLEVEL > 0)
648 DEBUG(3,("sending a packet of len %d to (%s) on port %d of type %s\n",
649 len,inet_ntoa(ip),port,type==SOCK_DGRAM?"DGRAM":"STREAM"));
651 /* send it */
652 ret = (sendto(out_fd,buf,len,0,(struct sockaddr *)&sock_out,sizeof(sock_out)) >= 0);
654 if (!ret)
655 DEBUG(0,("Packet send to %s(%d) failed ERRNO=%s\n",
656 inet_ntoa(ip),port,unix_error_string (errno)));
658 close(out_fd);
659 return(ret);
661 #endif /* 0 */
663 /****************************************************************************
664 open a socket of the specified type, port and address for incoming data
665 ****************************************************************************/
666 int open_socket_in(int type, int port, int dlevel,uint32 socket_addr, BOOL rebind)
668 struct hostent *hp;
669 struct sockaddr_in sock;
670 pstring host_name;
671 int res;
673 /* get my host name */
674 if (gethostname(host_name, MAXHOSTNAMELEN) == -1)
675 { DEBUG(0,("gethostname failed\n")); return -1; }
677 /* get host info */
678 if ((hp = Get_Hostbyname(host_name)) == 0)
680 DEBUG(0,( "Get_Hostbyname: Unknown host %s\n",host_name));
681 return -1;
684 memset((char *)&sock,'\0',sizeof(sock));
685 memcpy((char *)&sock.sin_addr,(char *)hp->h_addr, hp->h_length);
687 #ifdef HAVE_SOCK_SIN_LEN
688 sock.sin_len = sizeof(sock);
689 #endif
690 sock.sin_port = htons( port );
691 sock.sin_family = hp->h_addrtype;
692 sock.sin_addr.s_addr = socket_addr;
693 res = socket(hp->h_addrtype, type, 0);
694 if (res == -1)
695 { DEBUG(0,("socket failed\n")); return -1; }
698 int val=1;
699 if(rebind)
700 val=1;
701 else
702 val=0;
703 setsockopt(res,SOL_SOCKET,SO_REUSEADDR,(char *)&val,sizeof(val));
706 /* now we've got a socket - we need to bind it */
707 if (bind(res, (struct sockaddr * ) &sock,sizeof(sock)) < 0)
709 if (port) {
710 if (port == SMB_PORT || port == NMB_PORT)
711 DEBUG(dlevel,("bind failed on port %d socket_addr=%s (%s)\n",
712 port,inet_ntoa(sock.sin_addr),unix_error_string (errno)));
713 close(res);
715 if (dlevel > 0 && port < 1000)
716 port = 7999;
718 if (port >= 1000 && port < 9000)
719 return(open_socket_in(type,port+1,dlevel,socket_addr,rebind));
722 return(-1);
724 DEBUG(3,("bind succeeded on port %d\n",port));
726 return res;
730 /****************************************************************************
731 create an outgoing socket. timeout is in milliseconds.
732 **************************************************************************/
733 int open_socket_out(int type, struct in_addr *addr, int port ,int timeout)
735 struct sockaddr_in sock_out;
736 int res,ret;
737 int connect_loop = 250; /* 250 milliseconds */
738 int loops = (timeout) / connect_loop;
740 /* create a socket to write to */
741 res = socket(PF_INET, type, 0);
742 if (res == -1)
743 { DEBUG(0,("socket error\n")); return -1; }
745 if (type != SOCK_STREAM) return(res);
747 memset((char *)&sock_out,'\0',sizeof(sock_out));
748 putip((char *)&sock_out.sin_addr,(char *)addr);
750 sock_out.sin_port = htons( port );
751 sock_out.sin_family = PF_INET;
753 /* set it non-blocking */
754 set_blocking(res,False);
756 DEBUG(3,("Connecting to %s at port %d\n",inet_ntoa(*addr),port));
758 /* and connect it to the destination */
759 connect_again:
760 ret = connect(res,(struct sockaddr *)&sock_out,sizeof(sock_out));
762 /* Some systems return EAGAIN when they mean EINPROGRESS */
763 if (ret < 0 && (errno == EINPROGRESS || errno == EALREADY ||
764 errno == EAGAIN) && loops--) {
765 msleep(connect_loop);
766 goto connect_again;
769 if (ret < 0 && (errno == EINPROGRESS || errno == EALREADY ||
770 errno == EAGAIN)) {
771 DEBUG(1,("timeout connecting to %s:%d\n",inet_ntoa(*addr),port));
772 close(res);
773 return -1;
776 #ifdef EISCONN
777 if (ret < 0 && errno == EISCONN) {
778 errno = 0;
779 ret = 0;
781 #endif
783 if (ret < 0) {
784 DEBUG(1,("error connecting to %s:%d (%s)\n",
785 inet_ntoa(*addr),port,unix_error_string (errno)));
786 close(res);
787 return -1;
790 /* set it blocking again */
791 set_blocking(res,True);
793 return res;
797 /*******************************************************************
798 Reset the 'done' variables so after a client process is created
799 from a fork call these calls will be re-done. This should be
800 expanded if more variables need reseting.
801 ******************************************************************/
803 static BOOL global_client_name_done = False;
804 static BOOL global_client_addr_done = False;
806 /*******************************************************************
807 return the DNS name of the client
808 ******************************************************************/
809 char *client_name(int fd)
811 struct sockaddr sa;
812 struct sockaddr_in *sockin = (struct sockaddr_in *) (&sa);
813 unsigned int length = sizeof(sa);
814 static pstring name_buf;
815 struct hostent *hp;
816 static int last_fd=-1;
818 if (global_client_name_done && last_fd == fd)
819 return name_buf;
821 last_fd = fd;
822 global_client_name_done = False;
824 pstrcpy(name_buf,"UNKNOWN");
826 if (fd == -1) {
827 return name_buf;
830 if (getpeername(fd, &sa, &length) < 0) {
831 DEBUG(0,("getpeername failed. Error was %s\n", unix_error_string (errno) ));
832 return name_buf;
835 /* Look up the remote host name. */
836 if ((hp = gethostbyaddr((char *) &sockin->sin_addr,
837 sizeof(sockin->sin_addr),
838 AF_INET)) == 0) {
839 DEBUG(1,("Gethostbyaddr failed for %s\n",client_addr(fd)));
840 StrnCpy(name_buf,client_addr(fd),sizeof(name_buf) - 1);
841 } else {
842 StrnCpy(name_buf,(char *)hp->h_name,sizeof(name_buf) - 1);
843 if (!matchname(name_buf, sockin->sin_addr)) {
844 DEBUG(0,("Matchname failed on %s %s\n",name_buf,client_addr(fd)));
845 pstrcpy(name_buf,"UNKNOWN");
848 global_client_name_done = True;
849 return name_buf;
852 /*******************************************************************
853 return the IP addr of the client as a string
854 ******************************************************************/
855 char *client_addr(int fd)
857 struct sockaddr sa;
858 struct sockaddr_in *sockin = (struct sockaddr_in *) (&sa);
859 unsigned int length = sizeof(sa);
860 static fstring addr_buf;
861 static int last_fd = -1;
863 if (global_client_addr_done && fd == last_fd)
864 return addr_buf;
866 last_fd = fd;
867 global_client_addr_done = False;
869 fstrcpy(addr_buf,"0.0.0.0");
871 if (fd == -1) {
872 return addr_buf;
875 if (getpeername(fd, &sa, &length) < 0) {
876 DEBUG(0,("getpeername failed. Error was %s\n", unix_error_string (errno) ));
877 return addr_buf;
880 fstrcpy(addr_buf,(char *)inet_ntoa(sockin->sin_addr));
882 global_client_addr_done = True;
883 return addr_buf;