updating release notes & merging Derrel Lipman's libsmbclient patch from HEAD
[kai/samba.git] / source3 / lib / util_sock.c
index b59d7aa7ebb51b8f823d2f07d4874b74d06dba0f..19fb41f6ca305b11f584c9c05410d0cd11dab18d 100644 (file)
@@ -29,6 +29,29 @@ int lastport=0;
 
 int smb_read_error = 0;
 
+static char *get_socket_addr(int fd)
+{
+       struct sockaddr sa;
+       struct sockaddr_in *sockin = (struct sockaddr_in *) (&sa);
+       int     length = sizeof(sa);
+       static fstring addr_buf;
+
+       fstrcpy(addr_buf,"0.0.0.0");
+
+       if (fd == -1) {
+               return addr_buf;
+       }
+       
+       if (getsockname(fd, &sa, &length) < 0) {
+               DEBUG(0,("getpeername failed. Error was %s\n", strerror(errno) ));
+               return addr_buf;
+       }
+       
+       fstrcpy(addr_buf,(char *)inet_ntoa(sockin->sin_addr));
+       
+       return addr_buf;
+}
+
 /****************************************************************************
  Determine if a file descriptor is in fact a socket.
 ****************************************************************************/
@@ -552,6 +575,10 @@ BOOL receive_smb_raw(int fd,char *buffer, unsigned int timeout)
                                smb_read_error = READ_ERROR;
                        return False;
                }
+               
+               /* not all of samba3 properly checks for packet-termination of strings. This
+                  ensures that we don't run off into empty space. */
+               SSVAL(buffer+4,len, 0);
        }
 
        return True;
@@ -687,7 +714,7 @@ int open_socket_out(int type, struct in_addr *addr, int port ,int timeout)
        /* create a socket to write to */
        res = socket(PF_INET, type, 0);
        if (res == -1) {
-               DEBUG(0,("socket error\n"));
+                DEBUG(0,("socket error (%s)\n", strerror(errno)));
                return -1;
        }
 
@@ -713,7 +740,7 @@ int open_socket_out(int type, struct in_addr *addr, int port ,int timeout)
        /* Some systems return EAGAIN when they mean EINPROGRESS */
        if (ret < 0 && (errno == EINPROGRESS || errno == EALREADY ||
                        errno == EAGAIN) && (connect_loop < timeout) ) {
-               msleep(connect_loop);
+               smb_msleep(connect_loop);
                connect_loop += increment;
                if (increment < 250) {
                        /* After 8 rounds we end up at a max of 255 msec */
@@ -794,10 +821,15 @@ void client_setfd(int fd)
 
 char *client_name(void)
 {
-       return get_socket_name(client_fd,False);
+       return get_peer_name(client_fd,False);
 }
 
 char *client_addr(void)
+{
+       return get_peer_addr(client_fd);
+}
+
+char *client_socket_addr(void)
 {
        return get_socket_addr(client_fd);
 }
@@ -866,9 +898,10 @@ static BOOL matchname(char *remotehost,struct in_addr  addr)
  Return the DNS name of the remote end of a socket.
 ******************************************************************/
 
-char *get_socket_name(int fd, BOOL force_lookup)
+char *get_peer_name(int fd, BOOL force_lookup)
 {
        static pstring name_buf;
+       pstring tmp_name;
        static fstring addr_buf;
        struct hostent *hp;
        struct in_addr addr;
@@ -879,16 +912,18 @@ char *get_socket_name(int fd, BOOL force_lookup)
           with dns. To avoid the delay we avoid the lookup if
           possible */
        if (!lp_hostname_lookups() && (force_lookup == False)) {
-               return get_socket_addr(fd);
+               return get_peer_addr(fd);
        }
        
-       p = get_socket_addr(fd);
+       p = get_peer_addr(fd);
 
        /* it might be the same as the last one - save some DNS work */
-       if (strcmp(p, addr_buf) == 0) return name_buf;
+       if (strcmp(p, addr_buf) == 0) 
+               return name_buf;
 
        pstrcpy(name_buf,"UNKNOWN");
-       if (fd == -1) return name_buf;
+       if (fd == -1) 
+               return name_buf;
 
        fstrcpy(addr_buf, p);
 
@@ -906,7 +941,12 @@ char *get_socket_name(int fd, BOOL force_lookup)
                }
        }
 
-       alpha_strcpy(name_buf, name_buf, "_-.", sizeof(name_buf));
+       /* can't pass the same source and dest strings in when you 
+          use --enable-developer or the clobber_region() call will 
+          get you */
+       
+       pstrcpy( tmp_name, name_buf );
+       alpha_strcpy(name_buf, tmp_name, "_-.", sizeof(name_buf));
        if (strstr(name_buf,"..")) {
                pstrcpy(name_buf, "UNKNOWN");
        }
@@ -918,7 +958,7 @@ char *get_socket_name(int fd, BOOL force_lookup)
  Return the IP addr of the remote end of a socket as a string.
  ******************************************************************/
 
-char *get_socket_addr(int fd)
+char *get_peer_addr(int fd)
 {
        struct sockaddr sa;
        struct sockaddr_in *sockin = (struct sockaddr_in *) (&sa);