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