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;
35 /* the last IP received from */
36 struct in_addr lastip;
38 /* the last port received from */
41 int smb_read_error = 0;
43 /****************************************************************************
44 Determine if a file descriptor is in fact a socket.
45 ****************************************************************************/
47 BOOL is_a_socket(int fd)
51 return(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&v, &l) == 0);
54 enum SOCK_OPT_TYPES {OPT_BOOL,OPT_INT,OPT_ON};
63 } 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 Set user socket options.
101 ****************************************************************************/
103 void set_socket_options(int fd, char *options)
107 while (next_token(&options,tok," \t,", sizeof(tok))) {
111 BOOL got_value = False;
113 if ((p = strchr(tok,'='))) {
119 for (i=0;socket_options[i].name;i++)
120 if (strequal(socket_options[i].name,tok))
123 if (!socket_options[i].name) {
124 DEBUG(0,("Unknown socket option %s\n",tok));
128 switch (socket_options[i].opttype) {
131 ret = setsockopt(fd,socket_options[i].level,
132 socket_options[i].option,(char *)&value,sizeof(int));
137 DEBUG(0,("syntax error - %s does not take a value\n",tok));
140 int on = socket_options[i].value;
141 ret = setsockopt(fd,socket_options[i].level,
142 socket_options[i].option,(char *)&on,sizeof(int));
148 DEBUG(0,("Failed to set socket option %s (Error %s)\n",tok, strerror(errno) ));
152 /****************************************************************************
154 ****************************************************************************/
156 ssize_t read_udp_socket(int fd,char *buf,size_t len)
159 struct sockaddr_in sock;
162 socklen = sizeof(sock);
163 memset((char *)&sock,'\0',socklen);
164 memset((char *)&lastip,'\0',sizeof(lastip));
165 ret = (ssize_t)recvfrom(fd,buf,len,0,(struct sockaddr *)&sock,&socklen);
167 DEBUG(2,("read socket failed. ERRNO=%s\n",strerror(errno)));
171 lastip = sock.sin_addr;
172 lastport = ntohs(sock.sin_port);
174 DEBUG(10,("read_udp_socket: lastip %s lastport %d read: %d\n",
175 inet_ntoa(lastip), lastport, ret));
180 /****************************************************************************
181 Read data from a socket with a timout in msec.
182 mincount = if timeout, minimum to read before returning
183 maxcount = number to be read.
184 time_out = timeout in milliseconds
185 ****************************************************************************/
187 static ssize_t read_socket_with_timeout(int fd,char *buf,size_t mincnt,size_t maxcnt,unsigned int time_out)
193 struct timeval timeout;
195 /* just checking .... */
203 if (mincnt == 0) mincnt = maxcnt;
205 while (nread < mincnt) {
208 readret = SSL_read(ssl, buf + nread, maxcnt - nread);
210 readret = read(fd, buf + nread, maxcnt - nread);
213 readret = read(fd, buf + nread, maxcnt - nread);
214 #endif /* WITH_SSL */
217 DEBUG(5,("read_socket_with_timeout: blocking read. EOF from client.\n"));
218 smb_read_error = READ_EOF;
223 DEBUG(0,("read_socket_with_timeout: read error = %s.\n", strerror(errno) ));
224 smb_read_error = READ_ERROR;
229 return((ssize_t)nread);
232 /* Most difficult - timeout read */
233 /* If this is ever called on a disk file and
234 mincnt is greater then the filesize then
235 system performance will suffer severely as
236 select always returns true on disk files */
238 /* Set initial timeout */
239 timeout.tv_sec = (time_t)(time_out / 1000);
240 timeout.tv_usec = (long)(1000 * (time_out % 1000));
242 for (nread=0; nread < mincnt; ) {
246 selrtn = sys_select(fd+1,&fds,&timeout);
250 /* something is wrong. Maybe the socket is dead? */
251 DEBUG(0,("read_socket_with_timeout: timeout read. select error = %s.\n", strerror(errno) ));
252 smb_read_error = READ_ERROR;
256 /* Did we timeout ? */
258 DEBUG(10,("read_socket_with_timeout: timeout read. select timed out.\n"));
259 smb_read_error = READ_TIMEOUT;
265 readret = SSL_read(ssl, buf + nread, maxcnt - nread);
267 readret = read(fd, buf + nread, maxcnt - nread);
270 readret = read(fd, buf+nread, maxcnt-nread);
271 #endif /* WITH_SSL */
274 /* we got EOF on the file descriptor */
275 DEBUG(5,("read_socket_with_timeout: timeout read. EOF from client.\n"));
276 smb_read_error = READ_EOF;
281 /* the descriptor is probably dead */
282 DEBUG(0,("read_socket_with_timeout: timeout read. read error = %s.\n", strerror(errno) ));
283 smb_read_error = READ_ERROR;
290 /* Return the number we got */
291 return((ssize_t)nread);
294 /****************************************************************************
295 Read data from a fd with a timout in msec.
296 mincount = if timeout, minimum to read before returning
297 maxcount = number to be read.
298 time_out = timeout in milliseconds
299 ****************************************************************************/
301 ssize_t read_with_timeout(int fd,char *buf,size_t mincnt,size_t maxcnt,unsigned int time_out)
307 struct timeval timeout;
309 /* just checking .... */
315 if (mincnt == 0) mincnt = maxcnt;
317 while (nread < mincnt) {
320 readret = SSL_read(ssl, buf + nread, maxcnt - nread);
322 readret = read(fd, buf + nread, maxcnt - nread);
325 readret = read(fd, buf + nread, maxcnt - nread);
326 #endif /* WITH_SSL */
333 return((ssize_t)nread);
336 /* Most difficult - timeout read */
337 /* If this is ever called on a disk file and
338 mincnt is greater then the filesize then
339 system performance will suffer severely as
340 select always returns true on disk files */
342 /* Set initial timeout */
343 timeout.tv_sec = (time_t)(time_out / 1000);
344 timeout.tv_usec = (long)(1000 * (time_out % 1000));
346 for (nread=0; nread < mincnt; ) {
350 selrtn = sys_select(fd+1,&fds,&timeout);
357 readret = SSL_read(ssl, buf + nread, maxcnt - nread);
359 readret = read(fd, buf + nread, maxcnt - nread);
362 readret = read(fd, buf+nread, maxcnt-nread);
363 #endif /* WITH_SSL */
371 /* Return the number we got */
372 return((ssize_t)nread);
375 /****************************************************************************
376 send a keepalive packet (rfc1002)
377 ****************************************************************************/
379 BOOL send_keepalive(int client)
381 unsigned char buf[4];
384 buf[1] = buf[2] = buf[3] = 0;
386 return(write_socket_data(client,(char *)buf,4) == 4);
389 /****************************************************************************
390 read data from the client, reading exactly N bytes.
391 ****************************************************************************/
393 ssize_t read_data(int fd,char *buffer,size_t N)
404 ret = SSL_read(ssl, buffer + total, N - total);
406 ret = read(fd,buffer + total,N - total);
409 ret = read(fd,buffer + total,N - total);
410 #endif /* WITH_SSL */
414 DEBUG(10,("read_data: read of %d returned 0. Error = %s\n", (int)(N - total), strerror(errno) ));
415 smb_read_error = READ_EOF;
420 DEBUG(0,("read_data: read failure for %d. Error = %s\n", (int)(N - total), strerror(errno) ));
421 smb_read_error = READ_ERROR;
426 return (ssize_t)total;
429 /****************************************************************************
430 Read data from a socket, reading exactly N bytes.
431 ****************************************************************************/
433 static ssize_t read_socket_data(int fd,char *buffer,size_t N)
444 ret = SSL_read(ssl, buffer + total, N - total);
446 ret = read(fd,buffer + total,N - total);
449 ret = read(fd,buffer + total,N - total);
450 #endif /* WITH_SSL */
454 DEBUG(10,("read_socket_data: recv of %d returned 0. Error = %s\n", (int)(N - total), strerror(errno) ));
455 smb_read_error = READ_EOF;
460 DEBUG(0,("read_socket_data: recv failure for %d. Error = %s\n", (int)(N - total), strerror(errno) ));
461 smb_read_error = READ_ERROR;
466 return (ssize_t)total;
469 /****************************************************************************
471 ****************************************************************************/
473 ssize_t write_data(int fd,char *buffer,size_t N)
482 ret = SSL_write(ssl,buffer + total,N - total);
484 ret = write(fd,buffer + total,N - total);
487 ret = write(fd,buffer + total,N - total);
488 #endif /* WITH_SSL */
491 DEBUG(0,("write_data: write failure. Error = %s\n", strerror(errno) ));
494 if (ret == 0) return total;
498 return (ssize_t)total;
501 /****************************************************************************
502 Write data to a socket - use send rather than write.
503 ****************************************************************************/
505 ssize_t write_socket_data(int fd,char *buffer,size_t N)
514 ret = SSL_write(ssl,buffer + total,N - total);
516 ret = send(fd,buffer + total,N - total, 0);
519 ret = send(fd,buffer + total,N - total,0);
520 #endif /* WITH_SSL */
523 DEBUG(0,("write_socket_data: write failure. Error = %s\n", strerror(errno) ));
526 if (ret == 0) return total;
530 return (ssize_t)total;
533 /****************************************************************************
535 ****************************************************************************/
537 ssize_t write_socket(int fd,char *buf,size_t len)
543 DEBUG(6,("write_socket(%d,%d)\n",fd,(int)len));
544 ret = write_socket_data(fd,buf,len);
546 DEBUG(6,("write_socket(%d,%d) wrote %d\n",fd,(int)len,(int)ret));
548 DEBUG(0,("write_socket: Error writing %d bytes to socket %d: ERRNO = %s\n",
549 (int)len, fd, strerror(errno) ));
554 /****************************************************************************
555 read 4 bytes of a smb packet and return the smb length of the packet
556 store the result in the buffer
557 This version of the function will return a length of zero on receiving
559 timeout is in milliseconds.
560 ****************************************************************************/
562 static ssize_t read_smb_length_return_keepalive(int fd,char *inbuf,unsigned int timeout)
571 ok = (read_socket_with_timeout(fd,inbuf,4,4,timeout) == 4);
573 ok = (read_socket_data(fd,inbuf,4) == 4);
578 len = smb_len(inbuf);
579 msg_type = CVAL(inbuf,0);
581 if (msg_type == 0x85)
582 DEBUG(5,("Got keepalive packet\n"));
585 DEBUG(10,("got smb length of %d\n",len));
590 /****************************************************************************
591 read 4 bytes of a smb packet and return the smb length of the packet
592 store the result in the buffer. This version of the function will
593 never return a session keepalive (length of zero).
594 timeout is in milliseconds.
595 ****************************************************************************/
597 ssize_t read_smb_length(int fd,char *inbuf,unsigned int timeout)
603 len = read_smb_length_return_keepalive(fd, inbuf, timeout);
608 /* Ignore session keepalives. */
609 if(CVAL(inbuf,0) != 0x85)
613 DEBUG(10,("read_smb_length: got smb length of %d\n",len));
618 /****************************************************************************
619 read an smb from a fd. Note that the buffer *MUST* be of size
620 BUFFER_SIZE+SAFETY_MARGIN.
621 The timeout is in milliseconds.
622 This function will return on a
623 receipt of a session keepalive packet.
624 ****************************************************************************/
626 BOOL receive_smb(int fd,char *buffer, unsigned int timeout)
632 memset(buffer,'\0',smb_size + 100);
634 len = read_smb_length_return_keepalive(fd,buffer,timeout);
637 DEBUG(10,("receive_smb: length < 0!\n"));
641 if (len > BUFFER_SIZE) {
642 DEBUG(0,("Invalid packet length! (%d bytes).\n",len));
643 if (len > BUFFER_SIZE + (SAFETY_MARGIN/2))
650 ret = read_socket_data(fd,buffer+4,len);
652 smb_read_error = READ_ERROR;
659 /****************************************************************************
660 read an smb from a fd ignoring all keepalive packets. Note that the buffer
661 *MUST* be of size BUFFER_SIZE+SAFETY_MARGIN.
662 The timeout is in milliseconds
664 This is exactly the same as receive_smb except that it never returns
665 a session keepalive packet (just as receive_smb used to do).
666 receive_smb was changed to return keepalives as the oplock processing means this call
667 should never go into a blocking read.
668 ****************************************************************************/
670 BOOL client_receive_smb(int fd,char *buffer, unsigned int timeout)
676 ret = receive_smb(fd, buffer, timeout);
680 DEBUG(10,("client_receive_smb failed\n"));
685 /* Ignore session keepalive packets. */
686 if(CVAL(buffer,0) != 0x85)
693 /****************************************************************************
694 send an null session message to a fd
695 ****************************************************************************/
697 BOOL send_null_session_msg(int fd)
703 char *buffer = (char *)␣
705 while (nwritten < len)
707 ret = write_socket(fd,buffer+nwritten,len - nwritten);
710 DEBUG(0,("send_null_session_msg: Error writing %d bytes to client. %d. Exiting\n",(int)len,(int)ret));
716 DEBUG(10,("send_null_session_msg: sent 4 null bytes to client.\n"));
720 /****************************************************************************
722 ****************************************************************************/
724 BOOL send_smb(int fd,char *buffer)
729 len = smb_len(buffer) + 4;
731 while (nwritten < len)
733 ret = write_socket(fd,buffer+nwritten,len - nwritten);
736 DEBUG(0,("Error writing %d bytes to client. %d. Exiting\n",(int)len,(int)ret));
745 /****************************************************************************
746 send a single packet to a port on another machine
747 ****************************************************************************/
749 BOOL send_one_packet(char *buf,int len,struct in_addr ip,int port,int type)
753 struct sockaddr_in sock_out;
758 /* create a socket to write to */
759 out_fd = socket(AF_INET, type, 0);
762 DEBUG(0,("socket failed"));
766 /* set the address and port */
767 memset((char *)&sock_out,'\0',sizeof(sock_out));
768 putip((char *)&sock_out.sin_addr,(char *)&ip);
769 sock_out.sin_port = htons( port );
770 sock_out.sin_family = AF_INET;
773 DEBUG(3,("sending a packet of len %d to (%s) on port %d of type %s\n",
774 len,inet_ntoa(ip),port,type==SOCK_DGRAM?"DGRAM":"STREAM"));
777 ret = (sendto(out_fd,buf,len,0,(struct sockaddr *)&sock_out,sizeof(sock_out)) >= 0);
780 DEBUG(0,("Packet send to %s(%d) failed ERRNO=%s\n",
781 inet_ntoa(ip),port,strerror(errno)));
787 /****************************************************************************
788 open a socket of the specified type, port and address for incoming data
789 ****************************************************************************/
791 int open_socket_in(int type, int port, int dlevel,uint32 socket_addr, BOOL rebind)
794 struct sockaddr_in sock;
798 /* get my host name */
799 if (gethostname(host_name, MAXHOSTNAMELEN) == -1)
800 { DEBUG(0,("gethostname failed\n")); return -1; }
803 if ((hp = Get_Hostbyname(host_name)) == 0)
805 DEBUG(0,( "Get_Hostbyname: Unknown host %s\n",host_name));
809 memset((char *)&sock,'\0',sizeof(sock));
810 memcpy((char *)&sock.sin_addr,(char *)hp->h_addr, hp->h_length);
812 #ifdef HAVE_SOCK_SIN_LEN
813 sock.sin_len = sizeof(sock);
815 sock.sin_port = htons( port );
816 sock.sin_family = hp->h_addrtype;
817 sock.sin_addr.s_addr = socket_addr;
818 res = socket(hp->h_addrtype, type, 0);
820 { DEBUG(0,("socket failed\n")); return -1; }
828 if(setsockopt(res,SOL_SOCKET,SO_REUSEADDR,(char *)&val,sizeof(val)) == -1)
829 DEBUG(dlevel,("setsockopt: SO_REUSEADDR=%d on port %d failed with error = %s\n",
830 val, port, strerror(errno) ));
832 if(setsockopt(res,SOL_SOCKET,SO_REUSEPORT,(char *)&val,sizeof(val)) == -1)
833 DEBUG(dlevel,("setsockopt: SO_REUSEPORT=%d on port %d failed with error = %s\n",
834 val, port, strerror(errno) ));
835 #endif /* SO_REUSEPORT */
838 /* now we've got a socket - we need to bind it */
839 if (bind(res, (struct sockaddr * ) &sock,sizeof(sock)) < 0)
842 if (port == SMB_PORT || port == NMB_PORT)
843 DEBUG(dlevel,("bind failed on port %d socket_addr=%s (%s)\n",
844 port,inet_ntoa(sock.sin_addr),strerror(errno)));
847 if (dlevel > 0 && port < 1000)
850 if (port >= 1000 && port < 9000)
851 return(open_socket_in(type,port+1,dlevel,socket_addr,rebind));
856 DEBUG(3,("bind succeeded on port %d\n",port));
861 /****************************************************************************
862 create an outgoing socket. timeout is in milliseconds.
863 **************************************************************************/
865 int open_socket_out(int type, struct in_addr *addr, int port ,int timeout)
867 struct sockaddr_in sock_out;
869 int connect_loop = 250; /* 250 milliseconds */
870 int loops = (timeout) / connect_loop;
872 /* create a socket to write to */
873 res = socket(PF_INET, type, 0);
875 { DEBUG(0,("socket error\n")); return -1; }
877 if (type != SOCK_STREAM) return(res);
879 memset((char *)&sock_out,'\0',sizeof(sock_out));
880 putip((char *)&sock_out.sin_addr,(char *)addr);
882 sock_out.sin_port = htons( port );
883 sock_out.sin_family = PF_INET;
885 /* set it non-blocking */
886 set_blocking(res,False);
888 DEBUG(3,("Connecting to %s at port %d\n",inet_ntoa(*addr),port));
890 /* and connect it to the destination */
892 ret = connect(res,(struct sockaddr *)&sock_out,sizeof(sock_out));
894 /* Some systems return EAGAIN when they mean EINPROGRESS */
895 if (ret < 0 && (errno == EINPROGRESS || errno == EALREADY ||
896 errno == EAGAIN) && loops--) {
897 msleep(connect_loop);
901 if (ret < 0 && (errno == EINPROGRESS || errno == EALREADY ||
903 DEBUG(1,("timeout connecting to %s:%d\n",inet_ntoa(*addr),port));
909 if (ret < 0 && errno == EISCONN) {
916 DEBUG(1,("error connecting to %s:%d (%s)\n",
917 inet_ntoa(*addr),port,strerror(errno)));
922 /* set it blocking again */
923 set_blocking(res,True);
929 /*******************************************************************
930 Reset the 'done' variables so after a client process is created
931 from a fork call these calls will be re-done. This should be
932 expanded if more variables need reseting.
933 ******************************************************************/
936 void reset_globals_after_fork(void)
939 * Re-seed the random crypto generator, so all smbd's
940 * started from the same parent won't generate the same
945 generate_random_buffer( &dummy, 1, True);
949 /* the following 3 client_*() functions are nasty ways of allowing
950 some generic functions to get info that really should be hidden in
951 particular modules */
952 static int client_fd = -1;
954 void client_setfd(int fd)
959 char *client_name(void)
961 return get_socket_name(client_fd);
964 char *client_addr(void)
966 return get_socket_addr(client_fd);
969 /*******************************************************************
970 matchname - determine if host name matches IP address. Used to
971 confirm a hostname lookup to prevent spoof attacks
972 ******************************************************************/
973 static BOOL matchname(char *remotehost,struct in_addr addr)
978 if ((hp = Get_Hostbyname(remotehost)) == 0) {
979 DEBUG(0,("Get_Hostbyname(%s): lookup failure.\n", remotehost));
984 * Make sure that gethostbyname() returns the "correct" host name.
985 * Unfortunately, gethostbyname("localhost") sometimes yields
986 * "localhost.domain". Since the latter host name comes from the
987 * local DNS, we just have to trust it (all bets are off if the local
988 * DNS is perverted). We always check the address list, though.
991 if (strcasecmp(remotehost, hp->h_name)
992 && strcasecmp(remotehost, "localhost")) {
993 DEBUG(0,("host name/name mismatch: %s != %s\n",
994 remotehost, hp->h_name));
998 /* Look up the host address in the address list we just got. */
999 for (i = 0; hp->h_addr_list[i]; i++) {
1000 if (memcmp(hp->h_addr_list[i], (caddr_t) & addr, sizeof(addr)) == 0)
1005 * The host name does not map to the original host address. Perhaps
1006 * someone has compromised a name server. More likely someone botched
1007 * it, but that could be dangerous, too.
1010 DEBUG(0,("host name/address mismatch: %s != %s\n",
1011 inet_ntoa(addr), hp->h_name));
1016 /*******************************************************************
1017 return the DNS name of the remote end of a socket
1018 ******************************************************************/
1019 char *get_socket_name(int fd)
1021 static pstring name_buf;
1022 static fstring addr_buf;
1024 struct in_addr addr;
1027 p = get_socket_addr(fd);
1029 /* it might be the same as the last one - save some DNS work */
1030 if (strcmp(p, addr_buf) == 0) return name_buf;
1032 pstrcpy(name_buf,"UNKNOWN");
1033 if (fd == -1) return name_buf;
1035 fstrcpy(addr_buf, p);
1037 if (inet_aton(p, &addr) == 0) return name_buf;
1039 /* Look up the remote host name. */
1040 if ((hp = gethostbyaddr((char *)&addr.s_addr, sizeof(addr.s_addr), AF_INET)) == 0) {
1041 DEBUG(1,("Gethostbyaddr failed for %s\n",p));
1042 pstrcpy(name_buf, p);
1044 pstrcpy(name_buf,(char *)hp->h_name);
1045 if (!matchname(name_buf, addr)) {
1046 DEBUG(0,("Matchname failed on %s %s\n",name_buf,p));
1047 pstrcpy(name_buf,"UNKNOWN");
1053 /*******************************************************************
1054 return the IP addr of the remote end of a socket as a string
1055 ******************************************************************/
1056 char *get_socket_addr(int fd)
1059 struct sockaddr_in *sockin = (struct sockaddr_in *) (&sa);
1060 int length = sizeof(sa);
1061 static fstring addr_buf;
1063 fstrcpy(addr_buf,"0.0.0.0");
1069 if (getpeername(fd, &sa, &length) < 0) {
1070 DEBUG(0,("getpeername failed. Error was %s\n", strerror(errno) ));
1074 fstrcpy(addr_buf,(char *)inet_ntoa(sockin->sin_addr));
1079 /*******************************************************************
1080 opens and connects to a unix pipe socket
1081 ******************************************************************/
1082 int open_pipe_sock(char *path)
1085 struct sockaddr_un sa;
1087 sock = socket(AF_UNIX, SOCK_STREAM, 0);
1091 DEBUG(0, ("unix socket open failed\n"));
1096 sa.sun_family = AF_UNIX;
1097 safe_strcpy(sa.sun_path, path, sizeof(sa.sun_path)-1);
1099 DEBUG(10, ("socket open succeeded. file name: %s\n", sa.sun_path));
1101 if (connect(sock, (struct sockaddr*) &sa, sizeof(sa)) < 0)
1103 DEBUG(0,("socket connect to %s failed\n", sa.sun_path));
1111 int create_pipe_socket(char *dir, int dir_perms,
1112 char *path, int path_perms)
1115 struct sockaddr_un sa;
1117 DEBUG(0,("create_pipe_socket: %s %d %s %d\n",
1118 dir, dir_perms, path, path_perms));
1120 DEBUG(0,("*** RACE CONDITION. PLEASE SOMEONE EXAMINE create_pipe_Socket AND FIX IT ***\n"));
1122 mkdir(dir, dir_perms);
1124 if (chmod(dir, dir_perms) < 0)
1126 DEBUG(0, ("chmod on %s failed\n", dir));
1132 DEBUG(0, ("remove on %s failed\n", path));
1135 /* start listening on unix socket */
1136 s = socket(AF_UNIX, SOCK_STREAM, 0);
1140 DEBUG(0, ("socket open failed\n"));
1145 sa.sun_family = AF_UNIX;
1146 safe_strcpy(sa.sun_path, path, sizeof(sa.sun_path)-1);
1148 if (bind(s, (struct sockaddr*) &sa, sizeof(sa)) < 0)
1150 DEBUG(0, ("socket bind to %s failed\n", sa.sun_path));
1158 DEBUG(0,("bind failed\n"));
1163 if (path_perms != 0)
1165 chmod(path, path_perms);
1168 if (listen(s, 5) == -1)
1170 DEBUG(0,("listen failed\n"));
1174 DEBUG(5,("unix socket opened: %s\n", path));