[Samba] Binding to a local IP address when mounting smb file system.

Ben Greear greearb at candelatech.com
Thu Jul 13 18:29:45 GMT 2006


Hello!

My company makes testing software that, among other things, can be used
to generate file-system traffic.  In order to better support Samba, I need
to make a few changes to the way samba mounts file systems:

1)  I need to be able to specify the local IP address for the socket connection.
   This will allow me to bind different mounts to different local interfaces.
   This information will be passed to the bind system call before initiating
   the 'connect'.

Example code:

    struct sockaddr_in my_ip_addr;
    memset(&my_ip_addr, 0, sizeof(my_ip_addr));

    my_ip_addr.sin_family = AF_INET;
    my_ip_addr.sin_addr.s_addr = htonl(ip_addr);
    my_ip_addr.sin_port = htons(ip_port);

    int r; //retval
    r = bind(s, (struct sockaddr*)(&my_ip_addr), sizeof(my_ip_addr));
    if (r < 0) {
       //system("netstat -an");
       cerr << "ERROR: tcp bind:  " << LFSTRERROR << endl;
       VLOG_ERR(VLOG << "ERROR: tcp bind:  " << LFSTRERROR << "  IP: "
                << toStringIP(ip_addr) << " ipPort: " << ip_port << endl);
       closesocket(s);
       return r;
    }
    else {
       VLOG_INF(VLOG << "Successfully bound to IP: " << toStringIP(ip_addr) << " port: "
                << ip_port << endl);
    }


2)  I need to be able to specify the local Network device name.  This will be
   used to also help bind to a specific local interface.  This will be passed
   to setsockopt before the connect() is called.

Example code:

    if (dev_to_bind_to) {
       // Bind to specific device.
#ifndef __WIN32__
       if (setsockopt(s, SOL_SOCKET, SO_BINDTODEVICE,
                      dev_to_bind_to, DEV_NAME_LEN + 1)) {
          VLOG_ERR(VLOG << "ERROR:  tcp-connect, setsockopt (BINDTODEVICE):  "
                   << LFSTRERROR << "  Not fatal in most cases..continuing...\n");
       }
#endif
    }//if


 From looking at the Samba code, it appears that I will need to modify the
open_socket_out method in lib/util_sock.c.  I will have to modify the method
to accept the extra configuration info (local IP, local device name) and
of course all of the callers of open_socket_out to pass in the info (or NULL if
the caller does not care to bind locally.)


Please let me know if this is a feature that would be accepted into samba
if I code it up.

Any suggestions are welcome as well.

Thanks,
Ben


-- 
Ben Greear <greearb at candelatech.com>
Candela Technologies Inc  http://www.candelatech.com



More information about the samba mailing list