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_m(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;
179 socklen_t socklen = sizeof(sock);
181 memset((char *)&sock,'\0',socklen);
182 memset((char *)&lastip,'\0',sizeof(lastip));
183 ret = (ssize_t)recvfrom(fd,buf,len,0,(struct sockaddr *)&sock,&socklen);
185 DEBUG(2,("read socket failed. ERRNO=%s\n",strerror(errno)));
189 lastip = sock.sin_addr;
190 lastport = ntohs(sock.sin_port);
192 DEBUG(10,("read_udp_socket: lastip %s lastport %d read: %d\n",
193 inet_ntoa(lastip), lastport, ret));
198 /****************************************************************************
199 Read data from a socket with a timout in msec.
200 mincount = if timeout, minimum to read before returning
201 maxcount = number to be read.
202 time_out = timeout in milliseconds
203 ****************************************************************************/
205 static ssize_t read_socket_with_timeout(int fd,char *buf,size_t mincnt,size_t maxcnt,unsigned int time_out)
211 struct timeval timeout;
213 /* just checking .... */
221 if (mincnt == 0) mincnt = maxcnt;
223 while (nread < mincnt) {
226 readret = SSL_read(ssl, buf + nread, maxcnt - nread);
228 readret = read(fd, buf + nread, maxcnt - nread);
231 readret = read(fd, buf + nread, maxcnt - nread);
232 #endif /* WITH_SSL */
235 DEBUG(5,("read_socket_with_timeout: blocking read. EOF from client.\n"));
236 smb_read_error = READ_EOF;
241 DEBUG(0,("read_socket_with_timeout: read error = %s.\n", strerror(errno) ));
242 smb_read_error = READ_ERROR;
247 return((ssize_t)nread);
250 /* Most difficult - timeout read */
251 /* If this is ever called on a disk file and
252 mincnt is greater then the filesize then
253 system performance will suffer severely as
254 select always returns true on disk files */
256 /* Set initial timeout */
257 timeout.tv_sec = (time_t)(time_out / 1000);
258 timeout.tv_usec = (long)(1000 * (time_out % 1000));
260 for (nread=0; nread < mincnt; ) {
264 selrtn = sys_select_intr(fd+1,&fds,&timeout);
268 /* something is wrong. Maybe the socket is dead? */
269 DEBUG(0,("read_socket_with_timeout: timeout read. select error = %s.\n", strerror(errno) ));
270 smb_read_error = READ_ERROR;
274 /* Did we timeout ? */
276 DEBUG(10,("read_socket_with_timeout: timeout read. select timed out.\n"));
277 smb_read_error = READ_TIMEOUT;
283 readret = SSL_read(ssl, buf + nread, maxcnt - nread);
285 readret = read(fd, buf + nread, maxcnt - nread);
288 readret = read(fd, buf+nread, maxcnt-nread);
289 #endif /* WITH_SSL */
292 /* we got EOF on the file descriptor */
293 DEBUG(5,("read_socket_with_timeout: timeout read. EOF from client.\n"));
294 smb_read_error = READ_EOF;
299 /* the descriptor is probably dead */
300 DEBUG(0,("read_socket_with_timeout: timeout read. read error = %s.\n", strerror(errno) ));
301 smb_read_error = READ_ERROR;
308 /* Return the number we got */
309 return((ssize_t)nread);
312 /****************************************************************************
313 Read data from a fd with a timout in msec.
314 mincount = if timeout, minimum to read before returning
315 maxcount = number to be read.
316 time_out = timeout in milliseconds
317 ****************************************************************************/
319 ssize_t read_with_timeout(int fd,char *buf,size_t mincnt,size_t maxcnt,unsigned int time_out)
325 struct timeval timeout;
327 /* just checking .... */
333 if (mincnt == 0) mincnt = maxcnt;
335 while (nread < mincnt) {
338 readret = SSL_read(ssl, buf + nread, maxcnt - nread);
340 readret = read(fd, buf + nread, maxcnt - nread);
343 readret = read(fd, buf + nread, maxcnt - nread);
344 #endif /* WITH_SSL */
351 return((ssize_t)nread);
354 /* Most difficult - timeout read */
355 /* If this is ever called on a disk file and
356 mincnt is greater then the filesize then
357 system performance will suffer severely as
358 select always returns true on disk files */
360 /* Set initial timeout */
361 timeout.tv_sec = (time_t)(time_out / 1000);
362 timeout.tv_usec = (long)(1000 * (time_out % 1000));
364 for (nread=0; nread < mincnt; ) {
368 selrtn = sys_select_intr(fd+1,&fds,&timeout);
375 readret = SSL_read(ssl, buf + nread, maxcnt - nread);
377 readret = read(fd, buf + nread, maxcnt - nread);
380 readret = read(fd, buf+nread, maxcnt-nread);
381 #endif /* WITH_SSL */
389 /* Return the number we got */
390 return((ssize_t)nread);
393 /****************************************************************************
394 send a keepalive packet (rfc1002)
395 ****************************************************************************/
397 BOOL send_keepalive(int client)
399 unsigned char buf[4];
402 buf[1] = buf[2] = buf[3] = 0;
404 return(write_socket_data(client,(char *)buf,4) == 4);
407 /****************************************************************************
408 read data from the client, reading exactly N bytes.
409 ****************************************************************************/
411 ssize_t read_data(int fd,char *buffer,size_t N)
422 ret = SSL_read(ssl, buffer + total, N - total);
424 ret = read(fd,buffer + total,N - total);
427 ret = read(fd,buffer + total,N - total);
428 #endif /* WITH_SSL */
432 DEBUG(10,("read_data: read of %d returned 0. Error = %s\n", (int)(N - total), strerror(errno) ));
433 smb_read_error = READ_EOF;
438 DEBUG(0,("read_data: read failure for %d. Error = %s\n", (int)(N - total), strerror(errno) ));
439 smb_read_error = READ_ERROR;
444 return (ssize_t)total;
447 /****************************************************************************
448 Read data from a socket, reading exactly N bytes.
449 ****************************************************************************/
451 static ssize_t read_socket_data(int fd,char *buffer,size_t N)
462 ret = SSL_read(ssl, buffer + total, N - total);
464 ret = read(fd,buffer + total,N - total);
467 ret = read(fd,buffer + total,N - total);
468 #endif /* WITH_SSL */
472 DEBUG(10,("read_socket_data: recv of %d returned 0. Error = %s\n", (int)(N - total), strerror(errno) ));
473 smb_read_error = READ_EOF;
478 DEBUG(0,("read_socket_data: recv failure for %d. Error = %s\n", (int)(N - total), strerror(errno) ));
479 smb_read_error = READ_ERROR;
484 return (ssize_t)total;
487 /****************************************************************************
489 ****************************************************************************/
491 ssize_t write_data(int fd,char *buffer,size_t N)
500 ret = SSL_write(ssl,buffer + total,N - total);
502 ret = write(fd,buffer + total,N - total);
505 ret = write(fd,buffer + total,N - total);
506 #endif /* WITH_SSL */
509 DEBUG(0,("write_data: write failure. Error = %s\n", strerror(errno) ));
512 if (ret == 0) return total;
516 return (ssize_t)total;
519 /****************************************************************************
520 Write data to a socket - use send rather than write.
521 ****************************************************************************/
523 ssize_t write_socket_data(int fd,char *buffer,size_t N)
532 ret = SSL_write(ssl,buffer + total,N - total);
534 ret = send(fd,buffer + total,N - total, 0);
537 ret = send(fd,buffer + total,N - total,0);
538 #endif /* WITH_SSL */
541 DEBUG(0,("write_socket_data: write failure. Error = %s\n", strerror(errno) ));
544 if (ret == 0) return total;
548 return (ssize_t)total;
551 /****************************************************************************
553 ****************************************************************************/
555 ssize_t write_socket(int fd,char *buf,size_t len)
559 DEBUG(6,("write_socket(%d,%d)\n",fd,(int)len));
560 ret = write_socket_data(fd,buf,len);
562 DEBUG(6,("write_socket(%d,%d) wrote %d\n",fd,(int)len,(int)ret));
564 DEBUG(0,("write_socket: Error writing %d bytes to socket %d: ERRNO = %s\n",
565 (int)len, fd, strerror(errno) ));
570 /****************************************************************************
571 read 4 bytes of a smb packet and return the smb length of the packet
572 store the result in the buffer
573 This version of the function will return a length of zero on receiving
575 timeout is in milliseconds.
576 ****************************************************************************/
578 static ssize_t read_smb_length_return_keepalive(int fd,char *inbuf,unsigned int timeout)
587 ok = (read_socket_with_timeout(fd,inbuf,4,4,timeout) == 4);
589 ok = (read_socket_data(fd,inbuf,4) == 4);
594 len = smb_len(inbuf);
595 msg_type = CVAL(inbuf,0);
597 if (msg_type == 0x85)
598 DEBUG(5,("Got keepalive packet\n"));
601 DEBUG(10,("got smb length of %d\n",len));
606 /****************************************************************************
607 read 4 bytes of a smb packet and return the smb length of the packet
608 store the result in the buffer. This version of the function will
609 never return a session keepalive (length of zero).
610 timeout is in milliseconds.
611 ****************************************************************************/
613 ssize_t read_smb_length(int fd,char *inbuf,unsigned int timeout)
619 len = read_smb_length_return_keepalive(fd, inbuf, timeout);
624 /* Ignore session keepalives. */
625 if(CVAL(inbuf,0) != 0x85)
629 DEBUG(10,("read_smb_length: got smb length of %d\n",len));
634 /****************************************************************************
635 read an smb from a fd. Note that the buffer *MUST* be of size
636 BUFFER_SIZE+SAFETY_MARGIN.
637 The timeout is in milliseconds.
638 This function will return on a
639 receipt of a session keepalive packet.
640 ****************************************************************************/
642 BOOL receive_smb(int fd,char *buffer, unsigned int timeout)
648 memset(buffer,'\0',smb_size + 100);
650 len = read_smb_length_return_keepalive(fd,buffer,timeout);
652 DEBUG(10,("receive_smb: length < 0!\n"));
657 * A WRITEX with CAP_LARGE_WRITEX can be 64k worth of data plus 65 bytes
658 * of header. Don't print the error if this fits.... JRA.
661 if (len > (BUFFER_SIZE + LARGE_WRITEX_HDR_SIZE)) {
662 DEBUG(0,("Invalid packet length! (%d bytes).\n",len));
663 if (len > BUFFER_SIZE + (SAFETY_MARGIN/2)) {
664 smb_read_error = READ_ERROR;
670 ret = read_socket_data(fd,buffer+4,len);
672 smb_read_error = READ_ERROR;
680 /****************************************************************************
681 read an smb from a fd ignoring all keepalive packets. Note that the buffer
682 *MUST* be of size BUFFER_SIZE+SAFETY_MARGIN.
683 The timeout is in milliseconds
685 This is exactly the same as receive_smb except that it never returns
686 a session keepalive packet (just as receive_smb used to do).
687 receive_smb was changed to return keepalives as the oplock processing means this call
688 should never go into a blocking read.
689 ****************************************************************************/
691 BOOL client_receive_smb(int fd,char *buffer, unsigned int timeout)
697 ret = receive_smb(fd, buffer, timeout);
701 DEBUG(10,("client_receive_smb failed\n"));
706 /* Ignore session keepalive packets. */
707 if(CVAL(buffer,0) != 0x85)
714 /****************************************************************************
716 ****************************************************************************/
718 BOOL send_smb(int fd,char *buffer)
723 len = smb_len(buffer) + 4;
725 while (nwritten < len) {
726 ret = write_socket(fd,buffer+nwritten,len - nwritten);
728 DEBUG(0,("Error writing %d bytes to client. %d. (%s)\n",
729 (int)len,(int)ret, strerror(errno) ));
738 /****************************************************************************
739 send a single packet to a port on another machine
740 ****************************************************************************/
742 BOOL send_one_packet(char *buf,int len,struct in_addr ip,int port,int type)
746 struct sockaddr_in sock_out;
748 /* create a socket to write to */
749 out_fd = socket(AF_INET, type, 0);
752 DEBUG(0,("socket failed"));
756 /* set the address and port */
757 memset((char *)&sock_out,'\0',sizeof(sock_out));
758 putip((char *)&sock_out.sin_addr,(char *)&ip);
759 sock_out.sin_port = htons( port );
760 sock_out.sin_family = AF_INET;
763 DEBUG(3,("sending a packet of len %d to (%s) on port %d of type %s\n",
764 len,inet_ntoa(ip),port,type==SOCK_DGRAM?"DGRAM":"STREAM"));
767 ret = (sendto(out_fd,buf,len,0,(struct sockaddr *)&sock_out,sizeof(sock_out)) >= 0);
770 DEBUG(0,("Packet send to %s(%d) failed ERRNO=%s\n",
771 inet_ntoa(ip),port,strerror(errno)));
777 /****************************************************************************
778 open a socket of the specified type, port and address for incoming data
779 ****************************************************************************/
781 int open_socket_in(int type, int port, int dlevel,uint32 socket_addr, BOOL rebind)
784 struct sockaddr_in sock;
788 /* get my host name */
789 if (gethostname(host_name, MAXHOSTNAMELEN) == -1)
790 { DEBUG(0,("gethostname failed\n")); return -1; }
793 if ((hp = sys_gethostbyname(host_name)) == 0)
795 DEBUG(0,( "sys_gethostbyname: Unknown host %s\n",host_name));
799 memset((char *)&sock,'\0',sizeof(sock));
800 memcpy((char *)&sock.sin_addr,(char *)hp->h_addr, hp->h_length);
802 #ifdef HAVE_SOCK_SIN_LEN
803 sock.sin_len = sizeof(sock);
805 sock.sin_port = htons( port );
806 sock.sin_family = hp->h_addrtype;
807 sock.sin_addr.s_addr = socket_addr;
808 res = socket(hp->h_addrtype, type, 0);
810 { DEBUG(0,("socket failed\n")); return -1; }
818 if(setsockopt(res,SOL_SOCKET,SO_REUSEADDR,(char *)&val,sizeof(val)) == -1)
819 DEBUG(dlevel,("setsockopt: SO_REUSEADDR=%d on port %d failed with error = %s\n",
820 val, port, strerror(errno) ));
822 if(setsockopt(res,SOL_SOCKET,SO_REUSEPORT,(char *)&val,sizeof(val)) == -1)
823 DEBUG(dlevel,("setsockopt: SO_REUSEPORT=%d on port %d failed with error = %s\n",
824 val, port, strerror(errno) ));
825 #endif /* SO_REUSEPORT */
828 /* now we've got a socket - we need to bind it */
829 if (bind(res, (struct sockaddr * ) &sock,sizeof(sock)) < 0)
832 if (port == SMB_PORT || port == NMB_PORT)
833 DEBUG(dlevel,("bind failed on port %d socket_addr=%s (%s)\n",
834 port,inet_ntoa(sock.sin_addr),strerror(errno)));
837 if (dlevel > 0 && port < 1000)
840 if (port >= 1000 && port < 9000)
841 return(open_socket_in(type,port+1,dlevel,socket_addr,rebind));
846 DEBUG(3,("bind succeeded on port %d\n",port));
851 /****************************************************************************
852 create an outgoing socket. timeout is in milliseconds.
853 **************************************************************************/
855 int open_socket_out(int type, struct in_addr *addr, int port ,int timeout)
857 struct sockaddr_in sock_out;
859 int connect_loop = 250; /* 250 milliseconds */
860 int loops = (timeout) / connect_loop;
862 /* create a socket to write to */
863 res = socket(PF_INET, type, 0);
865 { DEBUG(0,("socket error\n")); return -1; }
867 if (type != SOCK_STREAM) return(res);
869 memset((char *)&sock_out,'\0',sizeof(sock_out));
870 putip((char *)&sock_out.sin_addr,(char *)addr);
872 sock_out.sin_port = htons( port );
873 sock_out.sin_family = PF_INET;
875 /* set it non-blocking */
876 set_blocking(res,False);
878 DEBUG(3,("Connecting to %s at port %d\n",inet_ntoa(*addr),port));
880 /* and connect it to the destination */
882 ret = connect(res,(struct sockaddr *)&sock_out,sizeof(sock_out));
884 /* Some systems return EAGAIN when they mean EINPROGRESS */
885 if (ret < 0 && (errno == EINPROGRESS || errno == EALREADY ||
886 errno == EAGAIN) && loops--) {
887 msleep(connect_loop);
891 if (ret < 0 && (errno == EINPROGRESS || errno == EALREADY ||
893 DEBUG(1,("timeout connecting to %s:%d\n",inet_ntoa(*addr),port));
899 if (ret < 0 && errno == EISCONN) {
906 DEBUG(1,("error connecting to %s:%d (%s)\n",
907 inet_ntoa(*addr),port,strerror(errno)));
912 /* set it blocking again */
913 set_blocking(res,True);
918 /* the following 3 client_*() functions are nasty ways of allowing
919 some generic functions to get info that really should be hidden in
920 particular modules */
921 static int client_fd = -1;
923 void client_setfd(int fd)
928 char *client_name(void)
930 return get_socket_name(client_fd);
933 char *client_addr(void)
935 return get_socket_addr(client_fd);
938 /*******************************************************************
939 matchname - determine if host name matches IP address. Used to
940 confirm a hostname lookup to prevent spoof attacks
941 ******************************************************************/
942 static BOOL matchname(char *remotehost,struct in_addr addr)
947 if ((hp = sys_gethostbyname(remotehost)) == 0) {
948 DEBUG(0,("sys_gethostbyname(%s): lookup failure.\n", remotehost));
953 * Make sure that gethostbyname() returns the "correct" host name.
954 * Unfortunately, gethostbyname("localhost") sometimes yields
955 * "localhost.domain". Since the latter host name comes from the
956 * local DNS, we just have to trust it (all bets are off if the local
957 * DNS is perverted). We always check the address list, though.
960 if (strcasecmp(remotehost, hp->h_name)
961 && strcasecmp(remotehost, "localhost")) {
962 DEBUG(0,("host name/name mismatch: %s != %s\n",
963 remotehost, hp->h_name));
967 /* Look up the host address in the address list we just got. */
968 for (i = 0; hp->h_addr_list[i]; i++) {
969 if (memcmp(hp->h_addr_list[i], (caddr_t) & addr, sizeof(addr)) == 0)
974 * The host name does not map to the original host address. Perhaps
975 * someone has compromised a name server. More likely someone botched
976 * it, but that could be dangerous, too.
979 DEBUG(0,("host name/address mismatch: %s != %s\n",
980 inet_ntoa(addr), hp->h_name));
985 /*******************************************************************
986 return the DNS name of the remote end of a socket
987 ******************************************************************/
988 char *get_socket_name(int fd)
990 static pstring name_buf;
991 static fstring addr_buf;
996 p = get_socket_addr(fd);
998 /* it might be the same as the last one - save some DNS work */
999 if (strcmp(p, addr_buf) == 0) return name_buf;
1001 pstrcpy(name_buf,"UNKNOWN");
1002 if (fd == -1) return name_buf;
1004 fstrcpy(addr_buf, p);
1006 addr = *interpret_addr2(p);
1008 /* Look up the remote host name. */
1009 if ((hp = gethostbyaddr((char *)&addr.s_addr, sizeof(addr.s_addr), AF_INET)) == 0) {
1010 DEBUG(1,("Gethostbyaddr failed for %s\n",p));
1011 pstrcpy(name_buf, p);
1013 pstrcpy(name_buf,(char *)hp->h_name);
1014 if (!matchname(name_buf, addr)) {
1015 DEBUG(0,("Matchname failed on %s %s\n",name_buf,p));
1016 pstrcpy(name_buf,"UNKNOWN");
1020 alpha_strcpy(name_buf, name_buf, "_-.", sizeof(name_buf));
1021 if (strstr(name_buf,"..")) {
1022 pstrcpy(name_buf, "UNKNOWN");
1028 /*******************************************************************
1029 return the IP addr of the remote end of a socket as a string
1030 ******************************************************************/
1031 char *get_socket_addr(int fd)
1034 struct sockaddr_in *sockin = (struct sockaddr_in *) (&sa);
1035 int length = sizeof(sa);
1036 static fstring addr_buf;
1038 fstrcpy(addr_buf,"0.0.0.0");
1044 if (getpeername(fd, &sa, &length) < 0) {
1045 DEBUG(0,("getpeername failed. Error was %s\n", strerror(errno) ));
1049 fstrcpy(addr_buf,(char *)inet_ntoa(sockin->sin_addr));
1054 /*******************************************************************
1055 opens and connects to a unix pipe socket
1056 ******************************************************************/
1057 int open_pipe_sock(char *path)
1060 struct sockaddr_un sa;
1062 sock = socket(AF_UNIX, SOCK_STREAM, 0);
1066 DEBUG(0, ("unix socket open failed\n"));
1071 sa.sun_family = AF_UNIX;
1072 safe_strcpy(sa.sun_path, path, sizeof(sa.sun_path)-1);
1074 DEBUG(10, ("socket open succeeded. file name: %s\n", sa.sun_path));
1076 if (connect(sock, (struct sockaddr*) &sa, sizeof(sa)) < 0)
1078 DEBUG(0,("socket connect to %s failed\n", sa.sun_path));
1086 int create_pipe_socket(char *dir, int dir_perms,
1087 char *path, int path_perms)
1090 struct sockaddr_un sa;
1092 DEBUG(0,("create_pipe_socket: %s %d %s %d\n",
1093 dir, dir_perms, path, path_perms));
1095 DEBUG(0,("*** RACE CONDITION. PLEASE SOMEONE EXAMINE create_pipe_Socket AND FIX IT ***\n"));
1097 mkdir(dir, dir_perms);
1099 if (chmod(dir, dir_perms) < 0)
1101 DEBUG(0, ("chmod on %s failed\n", dir));
1107 DEBUG(0, ("remove on %s failed\n", path));
1110 /* start listening on unix socket */
1111 s = socket(AF_UNIX, SOCK_STREAM, 0);
1115 DEBUG(0, ("socket open failed\n"));
1120 sa.sun_family = AF_UNIX;
1121 safe_strcpy(sa.sun_path, path, sizeof(sa.sun_path)-1);
1123 if (bind(s, (struct sockaddr*) &sa, sizeof(sa)) < 0)
1125 DEBUG(0, ("socket bind to %s failed\n", sa.sun_path));
1133 DEBUG(0,("bind failed\n"));
1138 if (path_perms != 0)
1140 chmod(path, path_perms);
1143 if (listen(s, 5) == -1)
1145 DEBUG(0,("listen failed\n"));
1149 DEBUG(5,("unix socket opened: %s\n", path));
1154 /*******************************************************************
1155 this is like socketpair but uses tcp. It is used by the Samba
1156 regression test code
1157 The function guarantees that nobody else can attach to the socket,
1158 or if they do that this function fails and the socket gets closed
1159 returns 0 on success, -1 on failure
1160 the resulting file descriptors are symmetrical
1161 ******************************************************************/
1162 static int socketpair_tcp(int fd[2])
1165 struct sockaddr_in sock;
1166 struct sockaddr_in sock2;
1167 socklen_t socklen = sizeof(sock);
1168 int connect_done = 0;
1170 fd[0] = fd[1] = listener = -1;
1172 memset(&sock, 0, sizeof(sock));
1174 if ((listener = socket(PF_INET, SOCK_STREAM, 0)) == -1) goto failed;
1176 memset(&sock2, 0, sizeof(sock2));
1177 #ifdef HAVE_SOCK_SIN_LEN
1178 sock2.sin_len = sizeof(sock2);
1180 sock2.sin_family = PF_INET;
1182 bind(listener, (struct sockaddr *)&sock2, sizeof(sock2));
1184 if (listen(listener, 1) != 0) goto failed;
1186 if (getsockname(listener, (struct sockaddr *)&sock, &socklen) != 0) goto failed;
1188 if ((fd[1] = socket(PF_INET, SOCK_STREAM, 0)) == -1) goto failed;
1190 set_blocking(fd[1], 0);
1192 sock.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
1194 if (connect(fd[1],(struct sockaddr *)&sock,sizeof(sock)) == -1) {
1195 if (errno != EINPROGRESS) goto failed;
1200 if ((fd[0] = accept(listener, (struct sockaddr *)&sock, &socklen)) == -1) goto failed;
1203 if (connect_done == 0) {
1204 if (connect(fd[1],(struct sockaddr *)&sock,sizeof(sock)) != 0
1205 && errno != EISCONN) goto failed;
1208 set_blocking(fd[1], 1);
1214 if (fd[0] != -1) close(fd[0]);
1215 if (fd[1] != -1) close(fd[1]);
1216 if (listener != -1) close(listener);
1221 /*******************************************************************
1222 run a program on a local tcp socket, this is used to launch smbd
1223 when regression testing
1224 the return value is a socket which is attached to a subprocess
1225 running "prog". stdin and stdout are attached. stderr is left
1226 attached to the original stderr
1227 ******************************************************************/
1228 int sock_exec(const char *prog)
1231 if (socketpair_tcp(fd) != 0) {
1232 DEBUG(0,("socketpair_tcp failed (%s)\n", strerror(errno)));