Ben Greear
2006-Jul-13 19:15 UTC
[Samba] Binding to a local IP address when mounting smb file system.
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@candelatech.com> Candela Technologies Inc http://www.candelatech.com
Ben Greear
2006-Jul-14 23:06 UTC
[Samba] Binding to a local IP address when mounting smb file system.
I have completed the first draft of the patch to allow binding to local addresses. Many of the binaries do not fully support this option since I did not know a clean way to get the config info to them, didn't think it was really required for my needs. As far as I can tell, the libraries are well supported, so the applications can have support added as needed. smbmount and a few others *do* support the new options, and I have tested that smbmount works as I had hoped. The attached patch enables this behaviour: SMB client machine: eth0 IP: 172.2.2.230 netmask 255.255.255.0 eth1 IP: 172.2.2.231 netmask 255.255.255.0 eth2 IP: 173.2.2.232 netmask 255.255.255.0 ... local mount dirs: /mnt/smb1 /mnt/smb2 /mnt/smb3 .... SMB server is exporting share 'samba' I want to stress the SMB server as if many SMB clients are connecting. I can make arbitrarily many of the (virtual) interfaces on the client, but when mounting the SMB server, I want each mount point to use a specific interface and local IP. With my patch applied, I can bind the smbmount process to a particular local IP and device: smbmount //172.2.2.2/samba /mnt/smb2 -o local_dev=eth1,local_ip=173.2.2.231,username=lanforge,password=lanforge smbmount //172.2.2.2/samba /mnt/smb3 -o local_dev=eth2,local_ip=173.2.2.232,username=lanforge,password=lanforge ... As far as I can tell, the server treats each of the mounts as separate entities, accomplishing my goal. I would like to see this patch included in the official samba code, and would like your feedback on any changes needed to make that happen. I already know that I need to get rid of some of the debugging 'printf' statements, and will do that when the rest of the issues have been addressed. Thanks, Ben -- Ben Greear <greearb@candelatech.com> Candela Technologies Inc http://www.candelatech.com