2 Unix SMB/Netbios implementation.
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., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #undef Realloc /* SSLeay defines this and samba has a function of this name */
31 extern int DEBUGLEVEL;
33 /* the last IP received from */
34 struct in_addr lastip;
36 /* the last port received from */
39 int smb_read_error = 0;
41 /****************************************************************************
42 Determine if a file descriptor is in fact a socket.
43 ****************************************************************************/
45 BOOL is_a_socket(int fd)
49 return(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&v, &l) == 0);
52 enum SOCK_OPT_TYPES {OPT_BOOL,OPT_INT,OPT_ON};
54 typedef struct smb_socket_option
63 smb_socket_option socket_options[] = {
64 {"SO_KEEPALIVE", SOL_SOCKET, SO_KEEPALIVE, 0, OPT_BOOL},
65 {"SO_REUSEADDR", SOL_SOCKET, SO_REUSEADDR, 0, OPT_BOOL},
66 {"SO_BROADCAST", SOL_SOCKET, SO_BROADCAST, 0, OPT_BOOL},
68 {"TCP_NODELAY", IPPROTO_TCP, TCP_NODELAY, 0, OPT_BOOL},
71 {"IPTOS_LOWDELAY", IPPROTO_IP, IP_TOS, IPTOS_LOWDELAY, OPT_ON},
73 #ifdef IPTOS_THROUGHPUT
74 {"IPTOS_THROUGHPUT", IPPROTO_IP, IP_TOS, IPTOS_THROUGHPUT, OPT_ON},
77 {"SO_REUSEPORT", SOL_SOCKET, SO_REUSEPORT, 0, OPT_BOOL},
80 {"SO_SNDBUF", SOL_SOCKET, SO_SNDBUF, 0, OPT_INT},
83 {"SO_RCVBUF", SOL_SOCKET, SO_RCVBUF, 0, OPT_INT},
86 {"SO_SNDLOWAT", SOL_SOCKET, SO_SNDLOWAT, 0, OPT_INT},
89 {"SO_RCVLOWAT", SOL_SOCKET, SO_RCVLOWAT, 0, OPT_INT},
92 {"SO_SNDTIMEO", SOL_SOCKET, SO_SNDTIMEO, 0, OPT_INT},
95 {"SO_RCVTIMEO", SOL_SOCKET, SO_RCVTIMEO, 0, OPT_INT},
99 /****************************************************************************
100 Print socket options.
101 ****************************************************************************/
102 static void print_socket_options(int s)
105 smb_socket_option *p = &socket_options[0];
107 for (; p->name != NULL; p++) {
108 if (getsockopt(s, p->level, p->option, (void *)&value, &vlen) == -1) {
109 DEBUG(5,("Could not test socket option %s.\n", p->name));
111 DEBUG(5,("socket option %s = %d\n",p->name,value));
116 /****************************************************************************
117 Set user socket options.
118 ****************************************************************************/
120 void set_socket_options(int fd, char *options)
124 while (next_token(&options,tok," \t,", sizeof(tok))) {
128 BOOL got_value = False;
130 if ((p = strchr(tok,'='))) {
136 for (i=0;socket_options[i].name;i++)
137 if (strequal(socket_options[i].name,tok))
140 if (!socket_options[i].name) {
141 DEBUG(0,("Unknown socket option %s\n",tok));
145 switch (socket_options[i].opttype) {
148 ret = setsockopt(fd,socket_options[i].level,
149 socket_options[i].option,(char *)&value,sizeof(int));
154 DEBUG(0,("syntax error - %s does not take a value\n",tok));
157 int on = socket_options[i].value;
158 ret = setsockopt(fd,socket_options[i].level,
159 socket_options[i].option,(char *)&on,sizeof(int));
165 DEBUG(0,("Failed to set socket option %s (Error %s)\n",tok, strerror(errno) ));
168 print_socket_options(fd);
171 /****************************************************************************
173 ****************************************************************************/
175 ssize_t read_udp_socket(int fd,char *buf,size_t len)
178 struct sockaddr_in sock;
181 socklen = sizeof(sock);
182 memset((char *)&sock,'\0',socklen);
183 memset((char *)&lastip,'\0',sizeof(lastip));
184 ret = (ssize_t)recvfrom(fd,buf,len,0,(struct sockaddr *)&sock,&socklen);
186 DEBUG(2,("read socket failed. ERRNO=%s\n",strerror(errno)));
190 lastip = sock.sin_addr;
191 lastport = ntohs(sock.sin_port);
193 DEBUG(10,("read_udp_socket: lastip %s lastport %d read: %d\n",
194 inet_ntoa(lastip), lastport, ret));
199 /****************************************************************************
200 Read data from a socket with a timout in msec.
201 mincount = if timeout, minimum to read before returning
202 maxcount = number to be read.
203 time_out = timeout in milliseconds
204 ****************************************************************************/
206 static ssize_t read_socket_with_timeout(int fd,char *buf,size_t mincnt,size_t maxcnt,unsigned int time_out)
212 struct timeval timeout;
214 /* just checking .... */
222 if (mincnt == 0) mincnt = maxcnt;
224 while (nread < mincnt) {
227 readret = SSL_read(ssl, buf + nread, maxcnt - nread);
229 readret = read(fd, buf + nread, maxcnt - nread);
232 readret = read(fd, buf + nread, maxcnt - nread);
233 #endif /* WITH_SSL */
236 DEBUG(5,("read_socket_with_timeout: blocking read. EOF from client.\n"));
237 smb_read_error = READ_EOF;
242 DEBUG(0,("read_socket_with_timeout: read error = %s.\n", strerror(errno) ));
243 smb_read_error = READ_ERROR;
248 return((ssize_t)nread);
251 /* Most difficult - timeout read */
252 /* If this is ever called on a disk file and
253 mincnt is greater then the filesize then
254 system performance will suffer severely as
255 select always returns true on disk files */
257 /* Set initial timeout */
258 timeout.tv_sec = (time_t)(time_out / 1000);
259 timeout.tv_usec = (long)(1000 * (time_out % 1000));
261 for (nread=0; nread < mincnt; ) {
265 selrtn = sys_select_intr(fd+1,&fds,&timeout);
269 /* something is wrong. Maybe the socket is dead? */
270 DEBUG(0,("read_socket_with_timeout: timeout read. select error = %s.\n", strerror(errno) ));
271 smb_read_error = READ_ERROR;
275 /* Did we timeout ? */
277 DEBUG(10,("read_socket_with_timeout: timeout read. select timed out.\n"));
278 smb_read_error = READ_TIMEOUT;
284 readret = SSL_read(ssl, buf + nread, maxcnt - nread);
286 readret = read(fd, buf + nread, maxcnt - nread);
289 readret = read(fd, buf+nread, maxcnt-nread);
290 #endif /* WITH_SSL */
293 /* we got EOF on the file descriptor */
294 DEBUG(5,("read_socket_with_timeout: timeout read. EOF from client.\n"));
295 smb_read_error = READ_EOF;
300 /* the descriptor is probably dead */
301 DEBUG(0,("read_socket_with_timeout: timeout read. read error = %s.\n", strerror(errno) ));
302 smb_read_error = READ_ERROR;
309 /* Return the number we got */
310 return((ssize_t)nread);
313 /****************************************************************************
314 Read data from a fd with a timout in msec.
315 mincount = if timeout, minimum to read before returning
316 maxcount = number to be read.
317 time_out = timeout in milliseconds
318 ****************************************************************************/
320 ssize_t read_with_timeout(int fd,char *buf,size_t mincnt,size_t maxcnt,unsigned int time_out)
326 struct timeval timeout;
328 /* just checking .... */
334 if (mincnt == 0) mincnt = maxcnt;
336 while (nread < mincnt) {
339 readret = SSL_read(ssl, buf + nread, maxcnt - nread);
341 readret = read(fd, buf + nread, maxcnt - nread);
344 readret = read(fd, buf + nread, maxcnt - nread);
345 #endif /* WITH_SSL */
352 return((ssize_t)nread);
355 /* Most difficult - timeout read */
356 /* If this is ever called on a disk file and
357 mincnt is greater then the filesize then
358 system performance will suffer severely as
359 select always returns true on disk files */
361 /* Set initial timeout */
362 timeout.tv_sec = (time_t)(time_out / 1000);
363 timeout.tv_usec = (long)(1000 * (time_out % 1000));
365 for (nread=0; nread < mincnt; ) {
369 selrtn = sys_select_intr(fd+1,&fds,&timeout);
376 readret = SSL_read(ssl, buf + nread, maxcnt - nread);
378 readret = read(fd, buf + nread, maxcnt - nread);
381 readret = read(fd, buf+nread, maxcnt-nread);
382 #endif /* WITH_SSL */
390 /* Return the number we got */
391 return((ssize_t)nread);
394 /****************************************************************************
395 send a keepalive packet (rfc1002)
396 ****************************************************************************/
398 BOOL send_keepalive(int client)
400 unsigned char buf[4];
403 buf[1] = buf[2] = buf[3] = 0;
405 return(write_socket_data(client,(char *)buf,4) == 4);
408 /****************************************************************************
409 read data from the client, reading exactly N bytes.
410 ****************************************************************************/
412 ssize_t read_data(int fd,char *buffer,size_t N)
423 ret = SSL_read(ssl, buffer + total, N - total);
425 ret = read(fd,buffer + total,N - total);
428 ret = read(fd,buffer + total,N - total);
429 #endif /* WITH_SSL */
433 DEBUG(10,("read_data: read of %d returned 0. Error = %s\n", (int)(N - total), strerror(errno) ));
434 smb_read_error = READ_EOF;
439 DEBUG(0,("read_data: read failure for %d. Error = %s\n", (int)(N - total), strerror(errno) ));
440 smb_read_error = READ_ERROR;
445 return (ssize_t)total;
448 /****************************************************************************
449 Read data from a socket, reading exactly N bytes.
450 ****************************************************************************/
452 static ssize_t read_socket_data(int fd,char *buffer,size_t N)
463 ret = SSL_read(ssl, buffer + total, N - total);
465 ret = read(fd,buffer + total,N - total);
468 ret = read(fd,buffer + total,N - total);
469 #endif /* WITH_SSL */
473 DEBUG(10,("read_socket_data: recv of %d returned 0. Error = %s\n", (int)(N - total), strerror(errno) ));
474 smb_read_error = READ_EOF;
479 DEBUG(0,("read_socket_data: recv failure for %d. Error = %s\n", (int)(N - total), strerror(errno) ));
480 smb_read_error = READ_ERROR;
485 return (ssize_t)total;
488 /****************************************************************************
490 ****************************************************************************/
492 ssize_t write_data(int fd,char *buffer,size_t N)
501 ret = SSL_write(ssl,buffer + total,N - total);
503 ret = write(fd,buffer + total,N - total);
506 ret = write(fd,buffer + total,N - total);
507 #endif /* WITH_SSL */
510 DEBUG(0,("write_data: write failure. Error = %s\n", strerror(errno) ));
513 if (ret == 0) return total;
517 return (ssize_t)total;
520 /****************************************************************************
521 Write data to a socket - use send rather than write.
522 ****************************************************************************/
524 ssize_t write_socket_data(int fd,char *buffer,size_t N)
533 ret = SSL_write(ssl,buffer + total,N - total);
535 ret = send(fd,buffer + total,N - total, 0);
538 ret = send(fd,buffer + total,N - total,0);
539 #endif /* WITH_SSL */
542 DEBUG(0,("write_socket_data: write failure. Error = %s\n", strerror(errno) ));
545 if (ret == 0) return total;
549 return (ssize_t)total;
552 /****************************************************************************
554 ****************************************************************************/
556 ssize_t write_socket(int fd,char *buf,size_t len)
560 DEBUG(6,("write_socket(%d,%d)\n",fd,(int)len));
561 ret = write_socket_data(fd,buf,len);
563 DEBUG(6,("write_socket(%d,%d) wrote %d\n",fd,(int)len,(int)ret));
565 DEBUG(0,("write_socket: Error writing %d bytes to socket %d: ERRNO = %s\n",
566 (int)len, fd, strerror(errno) ));
571 /****************************************************************************
572 read 4 bytes of a smb packet and return the smb length of the packet
573 store the result in the buffer
574 This version of the function will return a length of zero on receiving
576 timeout is in milliseconds.
577 ****************************************************************************/
579 static ssize_t read_smb_length_return_keepalive(int fd,char *inbuf,unsigned int timeout)
588 ok = (read_socket_with_timeout(fd,inbuf,4,4,timeout) == 4);
590 ok = (read_socket_data(fd,inbuf,4) == 4);
595 len = smb_len(inbuf);
596 msg_type = CVAL(inbuf,0);
598 if (msg_type == 0x85)
599 DEBUG(5,("Got keepalive packet\n"));
602 DEBUG(10,("got smb length of %d\n",len));
607 /****************************************************************************
608 read 4 bytes of a smb packet and return the smb length of the packet
609 store the result in the buffer. This version of the function will
610 never return a session keepalive (length of zero).
611 timeout is in milliseconds.
612 ****************************************************************************/
614 ssize_t read_smb_length(int fd,char *inbuf,unsigned int timeout)
620 len = read_smb_length_return_keepalive(fd, inbuf, timeout);
625 /* Ignore session keepalives. */
626 if(CVAL(inbuf,0) != 0x85)
630 DEBUG(10,("read_smb_length: got smb length of %d\n",len));
635 /****************************************************************************
636 read an smb from a fd. Note that the buffer *MUST* be of size
637 BUFFER_SIZE+SAFETY_MARGIN.
638 The timeout is in milliseconds.
639 This function will return on a
640 receipt of a session keepalive packet.
641 ****************************************************************************/
643 BOOL receive_smb(int fd,char *buffer, unsigned int timeout)
649 memset(buffer,'\0',smb_size + 100);
651 len = read_smb_length_return_keepalive(fd,buffer,timeout);
653 DEBUG(10,("receive_smb: length < 0!\n"));
658 * A WRITEX with CAP_LARGE_WRITEX can be 64k worth of data plus 65 bytes
659 * of header. Don't print the error if this fits.... JRA.
662 if (len > (BUFFER_SIZE + LARGE_WRITEX_HDR_SIZE)) {
663 DEBUG(0,("Invalid packet length! (%d bytes).\n",len));
664 if (len > BUFFER_SIZE + (SAFETY_MARGIN/2))
669 ret = read_socket_data(fd,buffer+4,len);
671 smb_read_error = READ_ERROR;
679 /****************************************************************************
680 read an smb from a fd ignoring all keepalive packets. Note that the buffer
681 *MUST* be of size BUFFER_SIZE+SAFETY_MARGIN.
682 The timeout is in milliseconds
684 This is exactly the same as receive_smb except that it never returns
685 a session keepalive packet (just as receive_smb used to do).
686 receive_smb was changed to return keepalives as the oplock processing means this call
687 should never go into a blocking read.
688 ****************************************************************************/
690 BOOL client_receive_smb(int fd,char *buffer, unsigned int timeout)
696 ret = receive_smb(fd, buffer, timeout);
700 DEBUG(10,("client_receive_smb failed\n"));
705 /* Ignore session keepalive packets. */
706 if(CVAL(buffer,0) != 0x85)
713 /****************************************************************************
714 send an null session message to a fd
715 ****************************************************************************/
717 BOOL send_null_session_msg(int fd)
723 char *buffer = (char *)␣
725 while (nwritten < len)
727 ret = write_socket(fd,buffer+nwritten,len - nwritten);
730 DEBUG(0,("send_null_session_msg: Error writing %d bytes to client. %d. Exiting\n",(int)len,(int)ret));
736 DEBUG(10,("send_null_session_msg: sent 4 null bytes to client.\n"));
740 /****************************************************************************
742 ****************************************************************************/
744 BOOL send_smb(int fd,char *buffer)
749 len = smb_len(buffer) + 4;
751 while (nwritten < len)
753 ret = write_socket(fd,buffer+nwritten,len - nwritten);
756 DEBUG(0,("Error writing %d bytes to client. %d. Exiting\n",(int)len,(int)ret));
765 /****************************************************************************
766 send a single packet to a port on another machine
767 ****************************************************************************/
769 BOOL send_one_packet(char *buf,int len,struct in_addr ip,int port,int type)
773 struct sockaddr_in sock_out;
775 /* create a socket to write to */
776 out_fd = socket(AF_INET, type, 0);
779 DEBUG(0,("socket failed"));
783 /* set the address and port */
784 memset((char *)&sock_out,'\0',sizeof(sock_out));
785 putip((char *)&sock_out.sin_addr,(char *)&ip);
786 sock_out.sin_port = htons( port );
787 sock_out.sin_family = AF_INET;
790 DEBUG(3,("sending a packet of len %d to (%s) on port %d of type %s\n",
791 len,inet_ntoa(ip),port,type==SOCK_DGRAM?"DGRAM":"STREAM"));
794 ret = (sendto(out_fd,buf,len,0,(struct sockaddr *)&sock_out,sizeof(sock_out)) >= 0);
797 DEBUG(0,("Packet send to %s(%d) failed ERRNO=%s\n",
798 inet_ntoa(ip),port,strerror(errno)));
804 /****************************************************************************
805 open a socket of the specified type, port and address for incoming data
806 ****************************************************************************/
808 int open_socket_in(int type, int port, int dlevel,uint32 socket_addr, BOOL rebind)
811 struct sockaddr_in sock;
815 /* get my host name */
816 if (gethostname(host_name, MAXHOSTNAMELEN) == -1)
817 { DEBUG(0,("gethostname failed\n")); return -1; }
820 if ((hp = Get_Hostbyname(host_name)) == 0)
822 DEBUG(0,( "Get_Hostbyname: Unknown host %s\n",host_name));
826 memset((char *)&sock,'\0',sizeof(sock));
827 memcpy((char *)&sock.sin_addr,(char *)hp->h_addr, hp->h_length);
829 #ifdef HAVE_SOCK_SIN_LEN
830 sock.sin_len = sizeof(sock);
832 sock.sin_port = htons( port );
833 sock.sin_family = hp->h_addrtype;
834 sock.sin_addr.s_addr = socket_addr;
835 res = socket(hp->h_addrtype, type, 0);
837 { DEBUG(0,("socket failed\n")); return -1; }
845 if(setsockopt(res,SOL_SOCKET,SO_REUSEADDR,(char *)&val,sizeof(val)) == -1)
846 DEBUG(dlevel,("setsockopt: SO_REUSEADDR=%d on port %d failed with error = %s\n",
847 val, port, strerror(errno) ));
849 if(setsockopt(res,SOL_SOCKET,SO_REUSEPORT,(char *)&val,sizeof(val)) == -1)
850 DEBUG(dlevel,("setsockopt: SO_REUSEPORT=%d on port %d failed with error = %s\n",
851 val, port, strerror(errno) ));
852 #endif /* SO_REUSEPORT */
855 /* now we've got a socket - we need to bind it */
856 if (bind(res, (struct sockaddr * ) &sock,sizeof(sock)) < 0)
859 if (port == SMB_PORT || port == NMB_PORT)
860 DEBUG(dlevel,("bind failed on port %d socket_addr=%s (%s)\n",
861 port,inet_ntoa(sock.sin_addr),strerror(errno)));
864 if (dlevel > 0 && port < 1000)
867 if (port >= 1000 && port < 9000)
868 return(open_socket_in(type,port+1,dlevel,socket_addr,rebind));
873 DEBUG(3,("bind succeeded on port %d\n",port));
878 /****************************************************************************
879 create an outgoing socket. timeout is in milliseconds.
880 **************************************************************************/
882 int open_socket_out(int type, struct in_addr *addr, int port ,int timeout)
884 struct sockaddr_in sock_out;
886 int connect_loop = 250; /* 250 milliseconds */
887 int loops = (timeout) / connect_loop;
889 /* create a socket to write to */
890 res = socket(PF_INET, type, 0);
892 { DEBUG(0,("socket error\n")); return -1; }
894 if (type != SOCK_STREAM) return(res);
896 memset((char *)&sock_out,'\0',sizeof(sock_out));
897 putip((char *)&sock_out.sin_addr,(char *)addr);
899 sock_out.sin_port = htons( port );
900 sock_out.sin_family = PF_INET;
902 /* set it non-blocking */
903 set_blocking(res,False);
905 DEBUG(3,("Connecting to %s at port %d\n",inet_ntoa(*addr),port));
907 /* and connect it to the destination */
909 ret = connect(res,(struct sockaddr *)&sock_out,sizeof(sock_out));
911 /* Some systems return EAGAIN when they mean EINPROGRESS */
912 if (ret < 0 && (errno == EINPROGRESS || errno == EALREADY ||
913 errno == EAGAIN) && loops--) {
914 msleep(connect_loop);
918 if (ret < 0 && (errno == EINPROGRESS || errno == EALREADY ||
920 DEBUG(1,("timeout connecting to %s:%d\n",inet_ntoa(*addr),port));
926 if (ret < 0 && errno == EISCONN) {
933 DEBUG(1,("error connecting to %s:%d (%s)\n",
934 inet_ntoa(*addr),port,strerror(errno)));
939 /* set it blocking again */
940 set_blocking(res,True);
945 /* the following 3 client_*() functions are nasty ways of allowing
946 some generic functions to get info that really should be hidden in
947 particular modules */
948 static int client_fd = -1;
950 void client_setfd(int fd)
955 char *client_name(void)
957 return get_socket_name(client_fd);
960 char *client_addr(void)
962 return get_socket_addr(client_fd);
965 /*******************************************************************
966 matchname - determine if host name matches IP address. Used to
967 confirm a hostname lookup to prevent spoof attacks
968 ******************************************************************/
969 static BOOL matchname(char *remotehost,struct in_addr addr)
974 if ((hp = Get_Hostbyname(remotehost)) == 0) {
975 DEBUG(0,("Get_Hostbyname(%s): lookup failure.\n", remotehost));
980 * Make sure that gethostbyname() returns the "correct" host name.
981 * Unfortunately, gethostbyname("localhost") sometimes yields
982 * "localhost.domain". Since the latter host name comes from the
983 * local DNS, we just have to trust it (all bets are off if the local
984 * DNS is perverted). We always check the address list, though.
987 if (strcasecmp(remotehost, hp->h_name)
988 && strcasecmp(remotehost, "localhost")) {
989 DEBUG(0,("host name/name mismatch: %s != %s\n",
990 remotehost, hp->h_name));
994 /* Look up the host address in the address list we just got. */
995 for (i = 0; hp->h_addr_list[i]; i++) {
996 if (memcmp(hp->h_addr_list[i], (caddr_t) & addr, sizeof(addr)) == 0)
1001 * The host name does not map to the original host address. Perhaps
1002 * someone has compromised a name server. More likely someone botched
1003 * it, but that could be dangerous, too.
1006 DEBUG(0,("host name/address mismatch: %s != %s\n",
1007 inet_ntoa(addr), hp->h_name));
1012 /*******************************************************************
1013 return the DNS name of the remote end of a socket
1014 ******************************************************************/
1015 char *get_socket_name(int fd)
1017 static pstring name_buf;
1018 static fstring addr_buf;
1020 struct in_addr addr;
1023 p = get_socket_addr(fd);
1025 /* it might be the same as the last one - save some DNS work */
1026 if (strcmp(p, addr_buf) == 0) return name_buf;
1028 pstrcpy(name_buf,"UNKNOWN");
1029 if (fd == -1) return name_buf;
1031 fstrcpy(addr_buf, p);
1033 addr = *interpret_addr2(p);
1035 /* Look up the remote host name. */
1036 if ((hp = gethostbyaddr((char *)&addr.s_addr, sizeof(addr.s_addr), AF_INET)) == 0) {
1037 DEBUG(1,("Gethostbyaddr failed for %s\n",p));
1038 pstrcpy(name_buf, p);
1040 pstrcpy(name_buf,(char *)hp->h_name);
1041 if (!matchname(name_buf, addr)) {
1042 DEBUG(0,("Matchname failed on %s %s\n",name_buf,p));
1043 pstrcpy(name_buf,"UNKNOWN");
1049 /*******************************************************************
1050 return the IP addr of the remote end of a socket as a string
1051 ******************************************************************/
1052 char *get_socket_addr(int fd)
1055 struct sockaddr_in *sockin = (struct sockaddr_in *) (&sa);
1056 int length = sizeof(sa);
1057 static fstring addr_buf;
1059 fstrcpy(addr_buf,"0.0.0.0");
1065 if (getpeername(fd, &sa, &length) < 0) {
1066 DEBUG(0,("getpeername failed. Error was %s\n", strerror(errno) ));
1070 fstrcpy(addr_buf,(char *)inet_ntoa(sockin->sin_addr));
1075 /*******************************************************************
1076 opens and connects to a unix pipe socket
1077 ******************************************************************/
1078 int open_pipe_sock(char *path)
1081 struct sockaddr_un sa;
1083 sock = socket(AF_UNIX, SOCK_STREAM, 0);
1087 DEBUG(0, ("unix socket open failed\n"));
1092 sa.sun_family = AF_UNIX;
1093 safe_strcpy(sa.sun_path, path, sizeof(sa.sun_path)-1);
1095 DEBUG(10, ("socket open succeeded. file name: %s\n", sa.sun_path));
1097 if (connect(sock, (struct sockaddr*) &sa, sizeof(sa)) < 0)
1099 DEBUG(0,("socket connect to %s failed\n", sa.sun_path));
1107 int create_pipe_socket(char *dir, int dir_perms,
1108 char *path, int path_perms)
1111 struct sockaddr_un sa;
1113 DEBUG(0,("create_pipe_socket: %s %d %s %d\n",
1114 dir, dir_perms, path, path_perms));
1116 DEBUG(0,("*** RACE CONDITION. PLEASE SOMEONE EXAMINE create_pipe_Socket AND FIX IT ***\n"));
1118 mkdir(dir, dir_perms);
1120 if (chmod(dir, dir_perms) < 0)
1122 DEBUG(0, ("chmod on %s failed\n", dir));
1128 DEBUG(0, ("remove on %s failed\n", path));
1131 /* start listening on unix socket */
1132 s = socket(AF_UNIX, SOCK_STREAM, 0);
1136 DEBUG(0, ("socket open failed\n"));
1141 sa.sun_family = AF_UNIX;
1142 safe_strcpy(sa.sun_path, path, sizeof(sa.sun_path)-1);
1144 if (bind(s, (struct sockaddr*) &sa, sizeof(sa)) < 0)
1146 DEBUG(0, ("socket bind to %s failed\n", sa.sun_path));
1154 DEBUG(0,("bind failed\n"));
1159 if (path_perms != 0)
1161 chmod(path, path_perms);
1164 if (listen(s, 5) == -1)
1166 DEBUG(0,("listen failed\n"));
1170 DEBUG(5,("unix socket opened: %s\n", path));