include/smb.h: Re-added zero pointer protection to ZERO_STRUCTP.
[samba.git] / source / lib / util_sock.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    Samba utility functions
5    Copyright (C) Andrew Tridgell 1992-1998
6    
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.
11    
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.
16    
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., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23
24 #ifdef WITH_SSL
25 #include <ssl.h>
26 #undef Realloc  /* SSLeay defines this and samba has a function of this name */
27 extern SSL  *ssl;
28 extern int  sslFd;
29 #endif  /* WITH_SSL */
30
31 extern int DEBUGLEVEL;
32
33 BOOL passive = False;
34
35 /* the client file descriptor */
36 int Client = -1;
37
38 /* the last IP received from */
39 struct in_addr lastip;
40
41 /* the last port received from */
42 int lastport=0;
43
44
45 int smb_read_error = 0;
46
47
48 /****************************************************************************
49 determine if a file descriptor is in fact a socket
50 ****************************************************************************/
51 BOOL is_a_socket(int fd)
52 {
53   int v,l;
54   l = sizeof(int);
55   return(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&v, &l) == 0);
56 }
57
58
59 enum SOCK_OPT_TYPES {OPT_BOOL,OPT_INT,OPT_ON};
60
61 struct
62 {
63   char *name;
64   int level;
65   int option;
66   int value;
67   int opttype;
68 } socket_options[] = {
69   {"SO_KEEPALIVE",      SOL_SOCKET,    SO_KEEPALIVE,    0,                 OPT_BOOL},
70   {"SO_REUSEADDR",      SOL_SOCKET,    SO_REUSEADDR,    0,                 OPT_BOOL},
71   {"SO_BROADCAST",      SOL_SOCKET,    SO_BROADCAST,    0,                 OPT_BOOL},
72 #ifdef TCP_NODELAY
73   {"TCP_NODELAY",       IPPROTO_TCP,   TCP_NODELAY,     0,                 OPT_BOOL},
74 #endif
75 #ifdef IPTOS_LOWDELAY
76   {"IPTOS_LOWDELAY",    IPPROTO_IP,    IP_TOS,          IPTOS_LOWDELAY,    OPT_ON},
77 #endif
78 #ifdef IPTOS_THROUGHPUT
79   {"IPTOS_THROUGHPUT",  IPPROTO_IP,    IP_TOS,          IPTOS_THROUGHPUT,  OPT_ON},
80 #endif
81 #ifdef SO_SNDBUF
82   {"SO_SNDBUF",         SOL_SOCKET,    SO_SNDBUF,       0,                 OPT_INT},
83 #endif
84 #ifdef SO_RCVBUF
85   {"SO_RCVBUF",         SOL_SOCKET,    SO_RCVBUF,       0,                 OPT_INT},
86 #endif
87 #ifdef SO_SNDLOWAT
88   {"SO_SNDLOWAT",       SOL_SOCKET,    SO_SNDLOWAT,     0,                 OPT_INT},
89 #endif
90 #ifdef SO_RCVLOWAT
91   {"SO_RCVLOWAT",       SOL_SOCKET,    SO_RCVLOWAT,     0,                 OPT_INT},
92 #endif
93 #ifdef SO_SNDTIMEO
94   {"SO_SNDTIMEO",       SOL_SOCKET,    SO_SNDTIMEO,     0,                 OPT_INT},
95 #endif
96 #ifdef SO_RCVTIMEO
97   {"SO_RCVTIMEO",       SOL_SOCKET,    SO_RCVTIMEO,     0,                 OPT_INT},
98 #endif
99   {NULL,0,0,0,0}};
100
101         
102
103 /****************************************************************************
104 set user socket options
105 ****************************************************************************/
106 void set_socket_options(int fd, char *options)
107 {
108   fstring tok;
109
110   while (next_token(&options,tok," \t,", sizeof(tok)))
111     {
112       int ret=0,i;
113       int value = 1;
114       char *p;
115       BOOL got_value = False;
116
117       if ((p = strchr(tok,'=')))
118         {
119           *p = 0;
120           value = atoi(p+1);
121           got_value = True;
122         }
123
124       for (i=0;socket_options[i].name;i++)
125         if (strequal(socket_options[i].name,tok))
126           break;
127
128       if (!socket_options[i].name)
129         {
130           DEBUG(0,("Unknown socket option %s\n",tok));
131           continue;
132         }
133
134       switch (socket_options[i].opttype)
135         {
136         case OPT_BOOL:
137         case OPT_INT:
138           ret = setsockopt(fd,socket_options[i].level,
139                            socket_options[i].option,(char *)&value,sizeof(int));
140           break;
141
142         case OPT_ON:
143           if (got_value)
144             DEBUG(0,("syntax error - %s does not take a value\n",tok));
145
146           {
147             int on = socket_options[i].value;
148             ret = setsockopt(fd,socket_options[i].level,
149                              socket_options[i].option,(char *)&on,sizeof(int));
150           }
151           break;          
152         }
153       
154       if (ret != 0)
155         DEBUG(0,("Failed to set socket option %s\n",tok));
156     }
157 }
158
159
160
161 /****************************************************************************
162   close the socket communication
163 ****************************************************************************/
164 void close_sockets(void )
165 {
166 #ifdef WITH_SSL
167   sslutil_disconnect(Client);
168 #endif /* WITH_SSL */
169
170   close(Client);
171   Client = 0;
172 }
173
174
175
176 /****************************************************************************
177 write to a socket
178 ****************************************************************************/
179 ssize_t write_socket(int fd,char *buf,size_t len)
180 {
181   ssize_t ret=0;
182
183   if (passive)
184     return(len);
185   DEBUG(6,("write_socket(%d,%d)\n",fd,len));
186   ret = write_data(fd,buf,len);
187       
188   DEBUG(6,("write_socket(%d,%d) wrote %d\n",fd,len,ret));
189   if(ret <= 0)
190     DEBUG(0,("write_socket: Error writing %d bytes to socket %d: ERRNO = %s\n", 
191        len, fd, strerror(errno) ));
192
193   return(ret);
194 }
195
196 /****************************************************************************
197 read from a socket
198 ****************************************************************************/
199 ssize_t read_udp_socket(int fd,char *buf,size_t len)
200 {
201   ssize_t ret;
202   struct sockaddr_in sock;
203   int socklen;
204   
205   socklen = sizeof(sock);
206   bzero((char *)&sock,socklen);
207   bzero((char *)&lastip,sizeof(lastip));
208   ret = (ssize_t)recvfrom(fd,buf,len,0,(struct sockaddr *)&sock,&socklen);
209   if (ret <= 0) {
210     DEBUG(2,("read socket failed. ERRNO=%s\n",strerror(errno)));
211     return(0);
212   }
213
214   lastip = sock.sin_addr;
215   lastport = ntohs(sock.sin_port);
216
217   DEBUG(10,("read_udp_socket: lastip %s lastport %d read: %d\n",
218              inet_ntoa(lastip), lastport, ret));
219
220   return(ret);
221 }
222
223 /****************************************************************************
224 read data from a device with a timout in msec.
225 mincount = if timeout, minimum to read before returning
226 maxcount = number to be read.
227 time_out = timeout in milliseconds
228 ****************************************************************************/
229
230 ssize_t read_with_timeout(int fd,char *buf,size_t mincnt,size_t maxcnt,unsigned int time_out)
231 {
232   fd_set fds;
233   int selrtn;
234   ssize_t readret;
235   size_t nread = 0;
236   struct timeval timeout;
237
238   /* just checking .... */
239   if (maxcnt <= 0) return(0);
240
241   smb_read_error = 0;
242
243   /* Blocking read */
244   if (time_out <= 0) {
245     if (mincnt == 0) mincnt = maxcnt;
246
247     while (nread < mincnt) {
248 #ifdef WITH_SSL
249       if(fd == sslFd){
250         readret = SSL_read(ssl, buf + nread, maxcnt - nread);
251       }else{
252         readret = read(fd, buf + nread, maxcnt - nread);
253       }
254 #else /* WITH_SSL */
255       readret = read(fd, buf + nread, maxcnt - nread);
256 #endif /* WITH_SSL */
257
258       if (readret == 0) {
259         smb_read_error = READ_EOF;
260         return -1;
261       }
262
263       if (readret == -1) {
264         smb_read_error = READ_ERROR;
265         return -1;
266       }
267       nread += readret;
268     }
269     return((ssize_t)nread);
270   }
271   
272   /* Most difficult - timeout read */
273   /* If this is ever called on a disk file and 
274      mincnt is greater then the filesize then
275      system performance will suffer severely as 
276      select always returns true on disk files */
277
278   /* Set initial timeout */
279   timeout.tv_sec = (time_t)(time_out / 1000);
280   timeout.tv_usec = (long)(1000 * (time_out % 1000));
281
282   for (nread=0; nread < mincnt; ) 
283   {      
284     FD_ZERO(&fds);
285     FD_SET(fd,&fds);
286       
287     selrtn = sys_select(fd+1,&fds,&timeout);
288
289     /* Check if error */
290     if(selrtn == -1) {
291       /* something is wrong. Maybe the socket is dead? */
292       smb_read_error = READ_ERROR;
293       return -1;
294     }
295       
296     /* Did we timeout ? */
297     if (selrtn == 0) {
298       smb_read_error = READ_TIMEOUT;
299       return -1;
300     }
301       
302 #ifdef WITH_SSL
303     if(fd == sslFd){
304       readret = SSL_read(ssl, buf + nread, maxcnt - nread);
305     }else{
306       readret = read(fd, buf + nread, maxcnt - nread);
307     }
308 #else /* WITH_SSL */
309     readret = read(fd, buf+nread, maxcnt-nread);
310 #endif /* WITH_SSL */
311
312     if (readret == 0) {
313       /* we got EOF on the file descriptor */
314       smb_read_error = READ_EOF;
315       return -1;
316     }
317
318     if (readret == -1) {
319       /* the descriptor is probably dead */
320       smb_read_error = READ_ERROR;
321       return -1;
322     }
323       
324     nread += readret;
325   }
326
327   /* Return the number we got */
328   return((ssize_t)nread);
329 }
330
331
332 /****************************************************************************
333 send a keepalive packet (rfc1002)
334 ****************************************************************************/
335 BOOL send_keepalive(int client)
336 {
337   unsigned char buf[4];
338
339   buf[0] = 0x85;
340   buf[1] = buf[2] = buf[3] = 0;
341
342   return(write_data(client,(char *)buf,4) == 4);
343 }
344
345
346
347 /****************************************************************************
348   read data from the client, reading exactly N bytes. 
349 ****************************************************************************/
350 ssize_t read_data(int fd,char *buffer,size_t N)
351 {
352   ssize_t  ret;
353   size_t total=0;  
354  
355   smb_read_error = 0;
356
357   while (total < N)
358   {
359 #ifdef WITH_SSL
360     if(fd == sslFd){
361       ret = SSL_read(ssl, buffer + total, N - total);
362     }else{
363       ret = read(fd,buffer + total,N - total);
364     }
365 #else /* WITH_SSL */
366     ret = read(fd,buffer + total,N - total);
367 #endif /* WITH_SSL */
368
369     if (ret == 0)
370     {
371       smb_read_error = READ_EOF;
372       return 0;
373     }
374     if (ret == -1)
375     {
376       smb_read_error = READ_ERROR;
377       return -1;
378     }
379     total += ret;
380   }
381   return (ssize_t)total;
382 }
383
384
385 /****************************************************************************
386   write data to a fd 
387 ****************************************************************************/
388 ssize_t write_data(int fd,char *buffer,size_t N)
389 {
390   size_t total=0;
391   ssize_t ret;
392
393   while (total < N)
394   {
395 #ifdef WITH_SSL
396     if(fd == sslFd){
397       ret = SSL_write(ssl,buffer + total,N - total);
398     }else{
399       ret = write(fd,buffer + total,N - total);
400     }
401 #else /* WITH_SSL */
402     ret = write(fd,buffer + total,N - total);
403 #endif /* WITH_SSL */
404
405     if (ret == -1) return -1;
406     if (ret == 0) return total;
407
408     total += ret;
409   }
410   return (ssize_t)total;
411 }
412
413
414
415 /****************************************************************************
416 read 4 bytes of a smb packet and return the smb length of the packet
417 store the result in the buffer
418 This version of the function will return a length of zero on receiving
419 a keepalive packet.
420 timeout is in milliseconds.
421 ****************************************************************************/
422 static ssize_t read_smb_length_return_keepalive(int fd,char *inbuf,unsigned int timeout)
423 {
424   ssize_t len=0;
425   int msg_type;
426   BOOL ok = False;
427
428   while (!ok)
429   {
430     if (timeout > 0)
431       ok = (read_with_timeout(fd,inbuf,4,4,timeout) == 4);
432     else 
433       ok = (read_data(fd,inbuf,4) == 4);
434
435     if (!ok)
436       return(-1);
437
438     len = smb_len(inbuf);
439     msg_type = CVAL(inbuf,0);
440
441     if (msg_type == 0x85) 
442       DEBUG(5,("Got keepalive packet\n"));
443   }
444
445   DEBUG(10,("got smb length of %d\n",len));
446
447   return(len);
448 }
449
450 /****************************************************************************
451 read 4 bytes of a smb packet and return the smb length of the packet
452 store the result in the buffer. This version of the function will
453 never return a session keepalive (length of zero).
454 timeout is in milliseconds.
455 ****************************************************************************/
456 ssize_t read_smb_length(int fd,char *inbuf,unsigned int timeout)
457 {
458   ssize_t len;
459
460   for(;;)
461   {
462     len = read_smb_length_return_keepalive(fd, inbuf, timeout);
463
464     if(len < 0)
465       return len;
466
467     /* Ignore session keepalives. */
468     if(CVAL(inbuf,0) != 0x85)
469       break;
470   }
471
472   return len;
473 }
474
475 /****************************************************************************
476   read an smb from a fd. Note that the buffer *MUST* be of size
477   BUFFER_SIZE+SAFETY_MARGIN.
478   The timeout is in milliseconds. 
479   This function will return on a
480   receipt of a session keepalive packet.
481 ****************************************************************************/
482 BOOL receive_smb(int fd,char *buffer, unsigned int timeout)
483 {
484   ssize_t len,ret;
485
486   smb_read_error = 0;
487
488   bzero(buffer,smb_size + 100);
489
490   len = read_smb_length_return_keepalive(fd,buffer,timeout);
491   if (len < 0)
492   {
493     DEBUG(10,("receive_smb: length < 0!\n"));
494     return(False);
495   }
496
497   if (len > BUFFER_SIZE) {
498     DEBUG(0,("Invalid packet length! (%d bytes).\n",len));
499     if (len > BUFFER_SIZE + (SAFETY_MARGIN/2))
500     {
501         exit(1);
502     }
503   }
504
505   if(len > 0) {
506     ret = read_data(fd,buffer+4,len);
507     if (ret != len) {
508       smb_read_error = READ_ERROR;
509       return False;
510     }
511   }
512   return(True);
513 }
514
515 /****************************************************************************
516   read an smb from a fd ignoring all keepalive packets. Note that the buffer 
517   *MUST* be of size BUFFER_SIZE+SAFETY_MARGIN.
518   The timeout is in milliseconds
519
520   This is exactly the same as receive_smb except that it never returns
521   a session keepalive packet (just as receive_smb used to do).
522   receive_smb was changed to return keepalives as the oplock processing means this call
523   should never go into a blocking read.
524 ****************************************************************************/
525
526 BOOL client_receive_smb(int fd,char *buffer, unsigned int timeout)
527 {
528   BOOL ret;
529
530   for(;;)
531   {
532     ret = receive_smb(fd, buffer, timeout);
533
534     if (!ret)
535     {
536       DEBUG(10,("client_receive_smb failed\n"));
537       show_msg(buffer);
538       return ret;
539     }
540
541     /* Ignore session keepalive packets. */
542     if(CVAL(buffer,0) != 0x85)
543       break;
544   }
545   show_msg(buffer);
546   return ret;
547 }
548
549 /****************************************************************************
550   send an smb to a fd 
551 ****************************************************************************/
552 BOOL send_smb(int fd,char *buffer)
553 {
554   size_t len;
555   size_t nwritten=0;
556   ssize_t ret;
557   len = smb_len(buffer) + 4;
558
559   while (nwritten < len)
560   {
561     ret = write_socket(fd,buffer+nwritten,len - nwritten);
562     if (ret <= 0)
563     {
564       DEBUG(0,("Error writing %d bytes to client. %d. Exiting\n",len,ret));
565       close_sockets();
566       exit(1);
567     }
568     nwritten += ret;
569   }
570
571   return True;
572 }
573
574
575
576 /****************************************************************************
577 send a single packet to a port on another machine
578 ****************************************************************************/
579 BOOL send_one_packet(char *buf,int len,struct in_addr ip,int port,int type)
580 {
581   BOOL ret;
582   int out_fd;
583   struct sockaddr_in sock_out;
584
585   if (passive)
586     return(True);
587
588   /* create a socket to write to */
589   out_fd = socket(AF_INET, type, 0);
590   if (out_fd == -1) 
591     {
592       DEBUG(0,("socket failed"));
593       return False;
594     }
595
596   /* set the address and port */
597   bzero((char *)&sock_out,sizeof(sock_out));
598   putip((char *)&sock_out.sin_addr,(char *)&ip);
599   sock_out.sin_port = htons( port );
600   sock_out.sin_family = AF_INET;
601   
602   if (DEBUGLEVEL > 0)
603     DEBUG(3,("sending a packet of len %d to (%s) on port %d of type %s\n",
604              len,inet_ntoa(ip),port,type==SOCK_DGRAM?"DGRAM":"STREAM"));
605         
606   /* send it */
607   ret = (sendto(out_fd,buf,len,0,(struct sockaddr *)&sock_out,sizeof(sock_out)) >= 0);
608
609   if (!ret)
610     DEBUG(0,("Packet send to %s(%d) failed ERRNO=%s\n",
611              inet_ntoa(ip),port,strerror(errno)));
612
613   close(out_fd);
614   return(ret);
615 }
616
617
618 /****************************************************************************
619 open a socket of the specified type, port and address for incoming data
620 ****************************************************************************/
621 int open_socket_in(int type, int port, int dlevel,uint32 socket_addr)
622 {
623   struct hostent *hp;
624   struct sockaddr_in sock;
625   pstring host_name;
626   int res;
627
628   /* get my host name */
629   if (gethostname(host_name, MAXHOSTNAMELEN) == -1) 
630     { DEBUG(0,("gethostname failed\n")); return -1; } 
631
632   /* get host info */
633   if ((hp = Get_Hostbyname(host_name)) == 0) 
634     {
635       DEBUG(0,( "Get_Hostbyname: Unknown host %s\n",host_name));
636       return -1;
637     }
638   
639   bzero((char *)&sock,sizeof(sock));
640   memcpy((char *)&sock.sin_addr,(char *)hp->h_addr, hp->h_length);
641
642 #ifdef HAVE_SOCK_SIN_LEN
643   sock.sin_len = sizeof(sock);
644 #endif
645   sock.sin_port = htons( port );
646   sock.sin_family = hp->h_addrtype;
647   sock.sin_addr.s_addr = socket_addr;
648   res = socket(hp->h_addrtype, type, 0);
649   if (res == -1) 
650     { DEBUG(0,("socket failed\n")); return -1; }
651
652   {
653     int one=1;
654     setsockopt(res,SOL_SOCKET,SO_REUSEADDR,(char *)&one,sizeof(one));
655   }
656
657   /* now we've got a socket - we need to bind it */
658   if (bind(res, (struct sockaddr * ) &sock,sizeof(sock)) < 0) 
659     { 
660       if (port) {
661         if (port == SMB_PORT || port == NMB_PORT)
662           DEBUG(dlevel,("bind failed on port %d socket_addr=%s (%s)\n",
663                         port,inet_ntoa(sock.sin_addr),strerror(errno))); 
664         close(res); 
665
666         if (dlevel > 0 && port < 1000)
667           port = 7999;
668
669         if (port >= 1000 && port < 9000)
670           return(open_socket_in(type,port+1,dlevel,socket_addr));
671       }
672
673       return(-1); 
674     }
675   DEBUG(3,("bind succeeded on port %d\n",port));
676
677   return res;
678 }
679
680
681 /****************************************************************************
682   create an outgoing socket
683   **************************************************************************/
684 int open_socket_out(int type, struct in_addr *addr, int port ,int timeout)
685 {
686   struct sockaddr_in sock_out;
687   int res,ret;
688   int connect_loop = 250; /* 250 milliseconds */
689   int loops = (timeout * 1000) / connect_loop;
690
691   /* create a socket to write to */
692   res = socket(PF_INET, type, 0);
693   if (res == -1) 
694     { DEBUG(0,("socket error\n")); return -1; }
695
696   if (type != SOCK_STREAM) return(res);
697   
698   bzero((char *)&sock_out,sizeof(sock_out));
699   putip((char *)&sock_out.sin_addr,(char *)addr);
700   
701   sock_out.sin_port = htons( port );
702   sock_out.sin_family = PF_INET;
703
704   /* set it non-blocking */
705   set_blocking(res,False);
706
707   DEBUG(3,("Connecting to %s at port %d\n",inet_ntoa(*addr),port));
708   
709   /* and connect it to the destination */
710 connect_again:
711   ret = connect(res,(struct sockaddr *)&sock_out,sizeof(sock_out));
712
713   /* Some systems return EAGAIN when they mean EINPROGRESS */
714   if (ret < 0 && (errno == EINPROGRESS || errno == EALREADY ||
715         errno == EAGAIN) && loops--) {
716     msleep(connect_loop);
717     goto connect_again;
718   }
719
720   if (ret < 0 && (errno == EINPROGRESS || errno == EALREADY ||
721          errno == EAGAIN)) {
722       DEBUG(1,("timeout connecting to %s:%d\n",inet_ntoa(*addr),port));
723       close(res);
724       return -1;
725   }
726
727 #ifdef EISCONN
728   if (ret < 0 && errno == EISCONN) {
729     errno = 0;
730     ret = 0;
731   }
732 #endif
733
734   if (ret < 0) {
735     DEBUG(1,("error connecting to %s:%d (%s)\n",
736              inet_ntoa(*addr),port,strerror(errno)));
737     close(res);
738     return -1;
739   }
740
741   /* set it blocking again */
742   set_blocking(res,True);
743
744   return res;
745 }
746
747
748 /*******************************************************************
749  Reset the 'done' variables so after a client process is created
750  from a fork call these calls will be re-done. This should be
751  expanded if more variables need reseting.
752  ******************************************************************/
753
754 static BOOL global_client_name_done = False;
755 static BOOL global_client_addr_done = False;
756
757 void reset_globals_after_fork(void)
758 {
759   global_client_name_done = False;
760   global_client_addr_done = False;
761
762   /*
763    * Re-seed the random crypto generator, so all smbd's
764    * started from the same parent won't generate the same
765    * sequence.
766    */
767   {
768     unsigned char dummy;
769     generate_random_buffer( &dummy, 1, True);
770   } 
771 }
772  
773 /*******************************************************************
774  return the DNS name of the client 
775  ******************************************************************/
776 char *client_name(int fd)
777 {
778         struct sockaddr sa;
779         struct sockaddr_in *sockin = (struct sockaddr_in *) (&sa);
780         int     length = sizeof(sa);
781         static pstring name_buf;
782         struct hostent *hp;
783         static int last_fd=-1;
784         
785         if (global_client_name_done && last_fd == fd) 
786                 return name_buf;
787         
788         last_fd = fd;
789         global_client_name_done = False;
790         
791         pstrcpy(name_buf,"UNKNOWN");
792         
793         if (fd == -1) {
794                 return name_buf;
795         }
796         
797         if (getpeername(fd, &sa, &length) < 0) {
798                 DEBUG(0,("getpeername failed. Error was %s\n", strerror(errno) ));
799                 return name_buf;
800         }
801         
802         /* Look up the remote host name. */
803         if ((hp = gethostbyaddr((char *) &sockin->sin_addr,
804                                 sizeof(sockin->sin_addr),
805                                 AF_INET)) == 0) {
806                 DEBUG(1,("Gethostbyaddr failed for %s\n",client_addr(fd)));
807                 StrnCpy(name_buf,client_addr(fd),sizeof(name_buf) - 1);
808         } else {
809                 StrnCpy(name_buf,(char *)hp->h_name,sizeof(name_buf) - 1);
810                 if (!matchname(name_buf, sockin->sin_addr)) {
811                         DEBUG(0,("Matchname failed on %s %s\n",name_buf,client_addr(fd)));
812                         pstrcpy(name_buf,"UNKNOWN");
813                 }
814         }
815         global_client_name_done = True;
816         return name_buf;
817 }
818
819 /*******************************************************************
820  return the IP addr of the client as a string 
821  ******************************************************************/
822 char *client_addr(int fd)
823 {
824         struct sockaddr sa;
825         struct sockaddr_in *sockin = (struct sockaddr_in *) (&sa);
826         int     length = sizeof(sa);
827         static fstring addr_buf;
828         static int last_fd = -1;
829
830         if (global_client_addr_done && fd == last_fd) 
831                 return addr_buf;
832
833         last_fd = fd;
834         global_client_addr_done = False;
835
836         fstrcpy(addr_buf,"0.0.0.0");
837
838         if (fd == -1) {
839                 return addr_buf;
840         }
841         
842         if (getpeername(fd, &sa, &length) < 0) {
843                 DEBUG(0,("getpeername failed. Error was %s\n", strerror(errno) ));
844                 return addr_buf;
845         }
846         
847         fstrcpy(addr_buf,(char *)inet_ntoa(sockin->sin_addr));
848         
849         global_client_addr_done = True;
850         return addr_buf;
851 }