summaryrefslogtreecommitdiff
path: root/source3/lib/util_sock.c
diff options
context:
space:
mode:
Diffstat (limited to 'source3/lib/util_sock.c')
-rw-r--r--source3/lib/util_sock.c529
1 files changed, 305 insertions, 224 deletions
diff --git a/source3/lib/util_sock.c b/source3/lib/util_sock.c
index d9b0f97259..77ba4a7f4c 100644
--- a/source3/lib/util_sock.c
+++ b/source3/lib/util_sock.c
@@ -35,22 +35,18 @@ BOOL passive = False;
/* the client file descriptor */
int Client = -1;
-/* the port, where client connected */
-int ClientPort = 0;
-
/* the last IP received from */
struct in_addr lastip;
/* the last port received from */
int lastport=0;
-
int smb_read_error = 0;
-
/****************************************************************************
-determine if a file descriptor is in fact a socket
+ Determine if a file descriptor is in fact a socket.
****************************************************************************/
+
BOOL is_a_socket(int fd)
{
int v,l;
@@ -58,7 +54,6 @@ BOOL is_a_socket(int fd)
return(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&v, &l) == 0);
}
-
enum SOCK_OPT_TYPES {OPT_BOOL,OPT_INT,OPT_ON};
struct
@@ -101,69 +96,63 @@ struct
#endif
{NULL,0,0,0,0}};
-
-
/****************************************************************************
-set user socket options
+ Set user socket options.
****************************************************************************/
+
void set_socket_options(int fd, char *options)
{
- fstring tok;
-
- while (next_token(&options,tok," \t,", sizeof(tok)))
- {
- int ret=0,i;
- int value = 1;
- char *p;
- BOOL got_value = False;
-
- if ((p = strchr(tok,'=')))
- {
- *p = 0;
- value = atoi(p+1);
- got_value = True;
- }
+ fstring tok;
+
+ while (next_token(&options,tok," \t,", sizeof(tok))) {
+ int ret=0,i;
+ int value = 1;
+ char *p;
+ BOOL got_value = False;
+
+ if ((p = strchr(tok,'='))) {
+ *p = 0;
+ value = atoi(p+1);
+ got_value = True;
+ }
- for (i=0;socket_options[i].name;i++)
- if (strequal(socket_options[i].name,tok))
- break;
+ for (i=0;socket_options[i].name;i++)
+ if (strequal(socket_options[i].name,tok))
+ break;
- if (!socket_options[i].name)
- {
- DEBUG(0,("Unknown socket option %s\n",tok));
- continue;
- }
+ if (!socket_options[i].name) {
+ DEBUG(0,("Unknown socket option %s\n",tok));
+ continue;
+ }
- switch (socket_options[i].opttype)
- {
- case OPT_BOOL:
- case OPT_INT:
- ret = setsockopt(fd,socket_options[i].level,
- socket_options[i].option,(char *)&value,sizeof(int));
- break;
-
- case OPT_ON:
- if (got_value)
- DEBUG(0,("syntax error - %s does not take a value\n",tok));
-
- {
- int on = socket_options[i].value;
- ret = setsockopt(fd,socket_options[i].level,
- socket_options[i].option,(char *)&on,sizeof(int));
- }
- break;
- }
+ switch (socket_options[i].opttype) {
+ case OPT_BOOL:
+ case OPT_INT:
+ ret = setsockopt(fd,socket_options[i].level,
+ socket_options[i].option,(char *)&value,sizeof(int));
+ break;
+
+ case OPT_ON:
+ if (got_value)
+ DEBUG(0,("syntax error - %s does not take a value\n",tok));
+
+ {
+ int on = socket_options[i].value;
+ ret = setsockopt(fd,socket_options[i].level,
+ socket_options[i].option,(char *)&on,sizeof(int));
+ }
+ break;
+ }
- if (ret != 0)
- DEBUG(0,("Failed to set socket option %s\n",tok));
- }
+ if (ret != 0)
+ DEBUG(0,("Failed to set socket option %s (Error %s)\n",tok, strerror(errno) ));
+ }
}
-
-
/****************************************************************************
- close the socket communication
+ Close the socket communication.
****************************************************************************/
+
void close_sockets(void )
{
#ifdef WITH_SSL
@@ -174,31 +163,10 @@ void close_sockets(void )
Client = -1;
}
-
-
/****************************************************************************
-write to a socket
+ Read from a socket.
****************************************************************************/
-ssize_t write_socket(int fd,char *buf,size_t len)
-{
- ssize_t ret=0;
-
- if (passive)
- return(len);
- DEBUG(6,("write_socket(%d,%d)\n",fd,len));
- ret = write_data(fd,buf,len);
-
- DEBUG(6,("write_socket(%d,%d) wrote %d\n",fd,len,ret));
- if(ret <= 0)
- DEBUG(0,("write_socket: Error writing %d bytes to socket %d: ERRNO = %s\n",
- len, fd, strerror(errno) ));
- return(ret);
-}
-
-/****************************************************************************
-read from a socket
-****************************************************************************/
ssize_t read_udp_socket(int fd,char *buf,size_t len)
{
ssize_t ret;
@@ -206,8 +174,8 @@ ssize_t read_udp_socket(int fd,char *buf,size_t len)
int socklen;
socklen = sizeof(sock);
- bzero((char *)&sock,socklen);
- bzero((char *)&lastip,sizeof(lastip));
+ memset((char *)&sock,'\0',socklen);
+ memset((char *)&lastip,'\0',sizeof(lastip));
ret = (ssize_t)recvfrom(fd,buf,len,0,(struct sockaddr *)&sock,&socklen);
if (ret <= 0) {
DEBUG(2,("read socket failed. ERRNO=%s\n",strerror(errno)));
@@ -224,13 +192,13 @@ ssize_t read_udp_socket(int fd,char *buf,size_t len)
}
/****************************************************************************
-read data from a device with a timout in msec.
-mincount = if timeout, minimum to read before returning
-maxcount = number to be read.
-time_out = timeout in milliseconds
+ Read data from a socket with a timout in msec.
+ mincount = if timeout, minimum to read before returning
+ maxcount = number to be read.
+ time_out = timeout in milliseconds
****************************************************************************/
-ssize_t read_with_timeout(int fd,char *buf,size_t mincnt,size_t maxcnt,unsigned int time_out)
+static ssize_t read_socket_with_timeout(int fd,char *buf,size_t mincnt,size_t maxcnt,unsigned int time_out)
{
fd_set fds;
int selrtn;
@@ -239,7 +207,8 @@ ssize_t read_with_timeout(int fd,char *buf,size_t mincnt,size_t maxcnt,unsigned
struct timeval timeout;
/* just checking .... */
- if (maxcnt <= 0) return(0);
+ if (maxcnt <= 0)
+ return(0);
smb_read_error = 0;
@@ -259,11 +228,13 @@ ssize_t read_with_timeout(int fd,char *buf,size_t mincnt,size_t maxcnt,unsigned
#endif /* WITH_SSL */
if (readret == 0) {
+ DEBUG(5,("read_socket_with_timeout: blocking read. EOF from client.\n"));
smb_read_error = READ_EOF;
return -1;
}
if (readret == -1) {
+ DEBUG(0,("read_socket_with_timeout: read error = %s.\n", strerror(errno) ));
smb_read_error = READ_ERROR;
return -1;
}
@@ -282,22 +253,23 @@ ssize_t read_with_timeout(int fd,char *buf,size_t mincnt,size_t maxcnt,unsigned
timeout.tv_sec = (time_t)(time_out / 1000);
timeout.tv_usec = (long)(1000 * (time_out % 1000));
- for (nread=0; nread < mincnt; )
- {
+ for (nread=0; nread < mincnt; ) {
FD_ZERO(&fds);
FD_SET(fd,&fds);
- selrtn = sys_select(fd+1,&fds,NULL, &timeout);
+ selrtn = sys_select(fd+1,&fds,&timeout);
/* Check if error */
if(selrtn == -1) {
/* something is wrong. Maybe the socket is dead? */
+ DEBUG(0,("read_socket_with_timeout: timeout read. select error = %s.\n", strerror(errno) ));
smb_read_error = READ_ERROR;
return -1;
}
-
+
/* Did we timeout ? */
if (selrtn == 0) {
+ DEBUG(10,("read_socket_with_timeout: timeout read. select timed out.\n"));
smb_read_error = READ_TIMEOUT;
return -1;
}
@@ -314,12 +286,14 @@ ssize_t read_with_timeout(int fd,char *buf,size_t mincnt,size_t maxcnt,unsigned
if (readret == 0) {
/* we got EOF on the file descriptor */
+ DEBUG(5,("read_socket_with_timeout: timeout read. EOF from client.\n"));
smb_read_error = READ_EOF;
return -1;
}
if (readret == -1) {
/* the descriptor is probably dead */
+ DEBUG(0,("read_socket_with_timeout: timeout read. read error = %s.\n", strerror(errno) ));
smb_read_error = READ_ERROR;
return -1;
}
@@ -331,10 +305,91 @@ ssize_t read_with_timeout(int fd,char *buf,size_t mincnt,size_t maxcnt,unsigned
return((ssize_t)nread);
}
+/****************************************************************************
+ Read data from a fd with a timout in msec.
+ mincount = if timeout, minimum to read before returning
+ maxcount = number to be read.
+ time_out = timeout in milliseconds
+****************************************************************************/
+
+ssize_t read_with_timeout(int fd,char *buf,size_t mincnt,size_t maxcnt,unsigned int time_out)
+{
+ fd_set fds;
+ int selrtn;
+ ssize_t readret;
+ size_t nread = 0;
+ struct timeval timeout;
+
+ /* just checking .... */
+ if (maxcnt <= 0)
+ return(0);
+
+ /* Blocking read */
+ if (time_out <= 0) {
+ if (mincnt == 0) mincnt = maxcnt;
+
+ while (nread < mincnt) {
+#ifdef WITH_SSL
+ if(fd == sslFd){
+ readret = SSL_read(ssl, buf + nread, maxcnt - nread);
+ }else{
+ readret = read(fd, buf + nread, maxcnt - nread);
+ }
+#else /* WITH_SSL */
+ readret = read(fd, buf + nread, maxcnt - nread);
+#endif /* WITH_SSL */
+
+ if (readret <= 0)
+ return readret;
+
+ nread += readret;
+ }
+ return((ssize_t)nread);
+ }
+
+ /* Most difficult - timeout read */
+ /* If this is ever called on a disk file and
+ mincnt is greater then the filesize then
+ system performance will suffer severely as
+ select always returns true on disk files */
+
+ /* Set initial timeout */
+ timeout.tv_sec = (time_t)(time_out / 1000);
+ timeout.tv_usec = (long)(1000 * (time_out % 1000));
+
+ for (nread=0; nread < mincnt; ) {
+ FD_ZERO(&fds);
+ FD_SET(fd,&fds);
+
+ selrtn = sys_select(fd+1,&fds,&timeout);
+
+ if(selrtn <= 0)
+ return selrtn;
+
+#ifdef WITH_SSL
+ if(fd == sslFd){
+ readret = SSL_read(ssl, buf + nread, maxcnt - nread);
+ }else{
+ readret = read(fd, buf + nread, maxcnt - nread);
+ }
+#else /* WITH_SSL */
+ readret = read(fd, buf+nread, maxcnt-nread);
+#endif /* WITH_SSL */
+
+ if (readret <= 0)
+ return readret;
+
+ nread += readret;
+ }
+
+ /* Return the number we got */
+ return((ssize_t)nread);
+}
/****************************************************************************
send a keepalive packet (rfc1002)
****************************************************************************/
+
BOOL send_keepalive(int client)
{
unsigned char buf[4];
@@ -342,14 +397,13 @@ BOOL send_keepalive(int client)
buf[0] = 0x85;
buf[1] = buf[2] = buf[3] = 0;
- return(write_data(client,(char *)buf,4) == 4);
+ return(write_socket_data(client,(char *)buf,4) == 4);
}
-
-
/****************************************************************************
read data from the client, reading exactly N bytes.
****************************************************************************/
+
ssize_t read_data(int fd,char *buffer,size_t N)
{
ssize_t ret;
@@ -371,11 +425,13 @@ ssize_t read_data(int fd,char *buffer,size_t N)
if (ret == 0)
{
+ DEBUG(10,("read_data: read of %d returned 0. Error = %s\n", (int)(N - total), strerror(errno) ));
smb_read_error = READ_EOF;
return 0;
}
if (ret == -1)
{
+ DEBUG(0,("read_data: read failure for %d. Error = %s\n", (int)(N - total), strerror(errno) ));
smb_read_error = READ_ERROR;
return -1;
}
@@ -384,10 +440,50 @@ ssize_t read_data(int fd,char *buffer,size_t N)
return (ssize_t)total;
}
+/****************************************************************************
+ Read data from a socket, reading exactly N bytes.
+****************************************************************************/
+
+static ssize_t read_socket_data(int fd,char *buffer,size_t N)
+{
+ ssize_t ret;
+ size_t total=0;
+
+ smb_read_error = 0;
+
+ while (total < N)
+ {
+#ifdef WITH_SSL
+ if(fd == sslFd){
+ ret = SSL_read(ssl, buffer + total, N - total);
+ }else{
+ ret = read(fd,buffer + total,N - total);
+ }
+#else /* WITH_SSL */
+ ret = read(fd,buffer + total,N - total);
+#endif /* WITH_SSL */
+
+ if (ret == 0)
+ {
+ DEBUG(10,("read_socket_data: recv of %d returned 0. Error = %s\n", (int)(N - total), strerror(errno) ));
+ smb_read_error = READ_EOF;
+ return 0;
+ }
+ if (ret == -1)
+ {
+ DEBUG(0,("read_socket_data: recv failure for %d. Error = %s\n", (int)(N - total), strerror(errno) ));
+ smb_read_error = READ_ERROR;
+ return -1;
+ }
+ total += ret;
+ }
+ return (ssize_t)total;
+}
/****************************************************************************
- write data to a fd
+ Write data to a fd.
****************************************************************************/
+
ssize_t write_data(int fd,char *buffer,size_t N)
{
size_t total=0;
@@ -405,7 +501,42 @@ ssize_t write_data(int fd,char *buffer,size_t N)
ret = write(fd,buffer + total,N - total);
#endif /* WITH_SSL */
- if (ret == -1) return -1;
+ if (ret == -1) {
+ DEBUG(0,("write_data: write failure. Error = %s\n", strerror(errno) ));
+ return -1;
+ }
+ if (ret == 0) return total;
+
+ total += ret;
+ }
+ return (ssize_t)total;
+}
+
+/****************************************************************************
+ Write data to a socket - use send rather than write.
+****************************************************************************/
+
+ssize_t write_socket_data(int fd,char *buffer,size_t N)
+{
+ size_t total=0;
+ ssize_t ret;
+
+ while (total < N)
+ {
+#ifdef WITH_SSL
+ if(fd == sslFd){
+ ret = SSL_write(ssl,buffer + total,N - total);
+ }else{
+ ret = send(fd,buffer + total,N - total, 0);
+ }
+#else /* WITH_SSL */
+ ret = send(fd,buffer + total,N - total,0);
+#endif /* WITH_SSL */
+
+ if (ret == -1) {
+ DEBUG(0,("write_socket_data: write failure. Error = %s\n", strerror(errno) ));
+ return -1;
+ }
if (ret == 0) return total;
total += ret;
@@ -413,7 +544,26 @@ ssize_t write_data(int fd,char *buffer,size_t N)
return (ssize_t)total;
}
+/****************************************************************************
+write to a socket
+****************************************************************************/
+ssize_t write_socket(int fd,char *buf,size_t len)
+{
+ ssize_t ret=0;
+
+ if (passive)
+ return(len);
+ DEBUG(6,("write_socket(%d,%d)\n",fd,(int)len));
+ ret = write_socket_data(fd,buf,len);
+
+ DEBUG(6,("write_socket(%d,%d) wrote %d\n",fd,(int)len,(int)ret));
+ if(ret <= 0)
+ DEBUG(0,("write_socket: Error writing %d bytes to socket %d: ERRNO = %s\n",
+ (int)len, fd, strerror(errno) ));
+
+ return(ret);
+}
/****************************************************************************
read 4 bytes of a smb packet and return the smb length of the packet
@@ -422,6 +572,7 @@ This version of the function will return a length of zero on receiving
a keepalive packet.
timeout is in milliseconds.
****************************************************************************/
+
static ssize_t read_smb_length_return_keepalive(int fd,char *inbuf,unsigned int timeout)
{
ssize_t len=0;
@@ -431,9 +582,9 @@ static ssize_t read_smb_length_return_keepalive(int fd,char *inbuf,unsigned int
while (!ok)
{
if (timeout > 0)
- ok = (read_with_timeout(fd,inbuf,4,4,timeout) == 4);
+ ok = (read_socket_with_timeout(fd,inbuf,4,4,timeout) == 4);
else
- ok = (read_data(fd,inbuf,4) == 4);
+ ok = (read_socket_data(fd,inbuf,4) == 4);
if (!ok)
return(-1);
@@ -456,6 +607,7 @@ store the result in the buffer. This version of the function will
never return a session keepalive (length of zero).
timeout is in milliseconds.
****************************************************************************/
+
ssize_t read_smb_length(int fd,char *inbuf,unsigned int timeout)
{
ssize_t len;
@@ -472,6 +624,8 @@ ssize_t read_smb_length(int fd,char *inbuf,unsigned int timeout)
break;
}
+ DEBUG(10,("read_smb_length: got smb length of %d\n",len));
+
return len;
}
@@ -482,13 +636,14 @@ ssize_t read_smb_length(int fd,char *inbuf,unsigned int timeout)
This function will return on a
receipt of a session keepalive packet.
****************************************************************************/
+
BOOL receive_smb(int fd,char *buffer, unsigned int timeout)
{
ssize_t len,ret;
smb_read_error = 0;
- bzero(buffer,smb_size + 100);
+ memset(buffer,'\0',smb_size + 100);
len = read_smb_length_return_keepalive(fd,buffer,timeout);
if (len < 0)
@@ -506,7 +661,7 @@ BOOL receive_smb(int fd,char *buffer, unsigned int timeout)
}
if(len > 0) {
- ret = read_data(fd,buffer+4,len);
+ ret = read_socket_data(fd,buffer+4,len);
if (ret != len) {
smb_read_error = READ_ERROR;
return False;
@@ -529,7 +684,6 @@ BOOL receive_smb(int fd,char *buffer, unsigned int timeout)
BOOL client_receive_smb(int fd,char *buffer, unsigned int timeout)
{
BOOL ret;
- uint8 msg_type;
for(;;)
{
@@ -543,25 +697,45 @@ BOOL client_receive_smb(int fd,char *buffer, unsigned int timeout)
}
/* Ignore session keepalive packets. */
- msg_type = CVAL(buffer,0);
- if (msg_type != 0x85)
+ if(CVAL(buffer,0) != 0x85)
break;
}
- if (msg_type == 0)
- {
- show_msg(buffer);
- }
- else
- {
- dump_data(10, buffer, smb_len(buffer) + 4);
- }
show_msg(buffer);
return ret;
}
/****************************************************************************
+ send an null session message to a fd
+****************************************************************************/
+
+BOOL send_null_session_msg(int fd)
+{
+ ssize_t ret;
+ uint32 blank = 0;
+ size_t len = 4;
+ size_t nwritten=0;
+ char *buffer = (char *)&blank;
+
+ while (nwritten < len)
+ {
+ ret = write_socket(fd,buffer+nwritten,len - nwritten);
+ if (ret <= 0)
+ {
+ DEBUG(0,("send_null_session_msg: Error writing %d bytes to client. %d. Exiting\n",(int)len,(int)ret));
+ close_sockets();
+ exit(1);
+ }
+ nwritten += ret;
+ }
+
+ DEBUG(10,("send_null_session_msg: sent 4 null bytes to client.\n"));
+ return True;
+}
+
+/****************************************************************************
send an smb to a fd
****************************************************************************/
+
BOOL send_smb(int fd,char *buffer)
{
size_t len;
@@ -574,7 +748,7 @@ BOOL send_smb(int fd,char *buffer)
ret = write_socket(fd,buffer+nwritten,len - nwritten);
if (ret <= 0)
{
- DEBUG(0,("Error writing %d bytes to client. %d. Exiting\n",len,ret));
+ DEBUG(0,("Error writing %d bytes to client. %d. Exiting\n",(int)len,(int)ret));
close_sockets();
exit(1);
}
@@ -584,11 +758,10 @@ BOOL send_smb(int fd,char *buffer)
return True;
}
-
-
/****************************************************************************
send a single packet to a port on another machine
****************************************************************************/
+
BOOL send_one_packet(char *buf,int len,struct in_addr ip,int port,int type)
{
BOOL ret;
@@ -607,7 +780,7 @@ BOOL send_one_packet(char *buf,int len,struct in_addr ip,int port,int type)
}
/* set the address and port */
- bzero((char *)&sock_out,sizeof(sock_out));
+ memset((char *)&sock_out,'\0',sizeof(sock_out));
putip((char *)&sock_out.sin_addr,(char *)&ip);
sock_out.sin_port = htons( port );
sock_out.sin_family = AF_INET;
@@ -627,11 +800,11 @@ BOOL send_one_packet(char *buf,int len,struct in_addr ip,int port,int type)
return(ret);
}
-
/****************************************************************************
open a socket of the specified type, port and address for incoming data
****************************************************************************/
-int open_socket_in(int type, int port, int dlevel,uint32 socket_addr)
+
+int open_socket_in(int type, int port, int dlevel,uint32 socket_addr, BOOL rebind)
{
struct hostent *hp;
struct sockaddr_in sock;
@@ -649,7 +822,7 @@ int open_socket_in(int type, int port, int dlevel,uint32 socket_addr)
return -1;
}
- bzero((char *)&sock,sizeof(sock));
+ memset((char *)&sock,'\0',sizeof(sock));
memcpy((char *)&sock.sin_addr,(char *)hp->h_addr, hp->h_length);
#ifdef HAVE_SOCK_SIN_LEN
@@ -663,8 +836,12 @@ int open_socket_in(int type, int port, int dlevel,uint32 socket_addr)
{ DEBUG(0,("socket failed\n")); return -1; }
{
- int one=1;
- setsockopt(res,SOL_SOCKET,SO_REUSEADDR,(char *)&one,sizeof(one));
+ int val=1;
+ if(rebind)
+ val=1;
+ else
+ val=0;
+ setsockopt(res,SOL_SOCKET,SO_REUSEADDR,(char *)&val,sizeof(val));
}
/* now we've got a socket - we need to bind it */
@@ -680,7 +857,7 @@ int open_socket_in(int type, int port, int dlevel,uint32 socket_addr)
port = 7999;
if (port >= 1000 && port < 9000)
- return(open_socket_in(type,port+1,dlevel,socket_addr));
+ return(open_socket_in(type,port+1,dlevel,socket_addr,rebind));
}
return(-1);
@@ -690,16 +867,16 @@ int open_socket_in(int type, int port, int dlevel,uint32 socket_addr)
return res;
}
-
/****************************************************************************
- create an outgoing socket
+ create an outgoing socket. timeout is in milliseconds.
**************************************************************************/
+
int open_socket_out(int type, struct in_addr *addr, int port ,int timeout)
{
struct sockaddr_in sock_out;
int res,ret;
int connect_loop = 250; /* 250 milliseconds */
- int loops = (timeout * 1000) / connect_loop;
+ int loops = (timeout) / connect_loop;
/* create a socket to write to */
res = socket(PF_INET, type, 0);
@@ -708,7 +885,7 @@ int open_socket_out(int type, struct in_addr *addr, int port ,int timeout)
if (type != SOCK_STREAM) return(res);
- bzero((char *)&sock_out,sizeof(sock_out));
+ memset((char *)&sock_out,'\0',sizeof(sock_out));
putip((char *)&sock_out.sin_addr,(char *)addr);
sock_out.sin_port = htons( port );
@@ -786,6 +963,7 @@ void reset_globals_after_fork(void)
/*******************************************************************
return the DNS name of the client
******************************************************************/
+
char *client_name(int fd)
{
struct sockaddr sa;
@@ -832,6 +1010,7 @@ char *client_name(int fd)
/*******************************************************************
return the IP addr of the client as a string
******************************************************************/
+
char *client_addr(int fd)
{
struct sockaddr sa;
@@ -862,101 +1041,3 @@ char *client_addr(int fd)
global_client_addr_done = True;
return addr_buf;
}
-
-/*******************************************************************
- opens and connects to a unix pipe socket
- ******************************************************************/
-int open_pipe_sock(char *path)
-{
- int sock;
- struct sockaddr_un sa;
-
- sock = socket(AF_UNIX, SOCK_STREAM, 0);
-
- if (sock < 0)
- {
- DEBUG(0, ("unix socket open failed\n"));
- return sock;
- }
-
- ZERO_STRUCT(sa);
- sa.sun_family = AF_UNIX;
- safe_strcpy(sa.sun_path, path, sizeof(sa.sun_path)-1);
-
- DEBUG(10, ("socket open succeeded. file name: %s\n", sa.sun_path));
-
- if (connect(sock, (struct sockaddr*) &sa, sizeof(sa)) < 0)
- {
- DEBUG(0,("socket connect to %s failed\n", sa.sun_path));
- close(sock);
- return -1;
- }
-
- return sock;
-}
-
-int create_pipe_socket(char *dir, int dir_perms,
- char *path, int path_perms)
-{
- int s;
- struct sockaddr_un sa;
-
- DEBUG(10,("create_pipe_socket: %s %d %s %d\n",
- dir, dir_perms, path, path_perms));
-
- mkdir(dir, dir_perms);
-
- if (chmod(dir, dir_perms) < 0)
- {
- DEBUG(0, ("chmod on %s failed\n", dir));
- return -1;
- }
-
- if (!remove(path))
- {
- DEBUG(0, ("remove on %s failed\n", path));
- }
-
- /* start listening on unix socket */
- s = socket(AF_UNIX, SOCK_STREAM, 0);
-
- if (s < 0)
- {
- DEBUG(0, ("socket open failed\n"));
- return -1;
- }
-
- ZERO_STRUCT(sa);
- sa.sun_family = AF_UNIX;
- safe_strcpy(sa.sun_path, path, sizeof(sa.sun_path)-1);
-
- if (bind(s, (struct sockaddr*) &sa, sizeof(sa)) < 0)
- {
- DEBUG(0, ("socket bind to %s failed\n", sa.sun_path));
- close(s);
- remove(path);
- return -1;
- }
-
- if (s == -1)
- {
- DEBUG(0,("bind failed\n"));
- remove(path);
- return -1;
- }
-
- if (path_perms != 0)
- {
- chmod(path, path_perms);
- }
-
- if (listen(s, 5) == -1)
- {
- DEBUG(0,("listen failed\n"));
- return -1;
- }
-
- DEBUG(5,("unix socket opened: %s\n", path));
-
- return s;
-}