Filewatcher File Search
FTP Search
  
Directory 
  
Content Search 
   
pkg://ncftp-fw-2.3.0-3.src.rpm:195236/ncftp-fw-bind.diff  info  downloads

--- ncftp-2.3.0/FTP.c.bind	Sun Oct 29 15:21:25 1995
+++ ncftp-2.3.0/FTP.c	Fri Aug 22 15:43:09 1997
@@ -660,6 +660,48 @@
 
 
 
+#define FIRST_PORT 33000
+#define LAST_PORT  33099
+
+static
+int BindData(int dataSocket, struct sockaddr_in *saddr)
+{
+	static unsigned short int port = FIRST_PORT;
+	unsigned short int start;
+
+	saddr->sin_family = AF_INET;
+	*saddr = gOurClientCtrlAddr;
+
+	start = port;
+	do {
+		saddr->sin_port = htons(port);
+		DebugMsg("Trying to bind local end of data connection to port %hu\n", port);
+#ifdef HAVE_LIBSOCKS
+		if (Rbind(dataSocket, (struct sockaddr *) saddr,
+			(int) sizeof (* saddr),
+			gServerCtrlAddr.sin_addr.s_addr) < 0) 
+#else
+		if (bind(dataSocket, (struct sockaddr *) saddr,
+			(int) sizeof (* saddr)) < 0) 
+#endif
+		{
+			if (errno != EADDRINUSE) {
+				break;
+			}
+		} else {
+			DebugMsg("Bound local end of data connection to port %hu\n", port);
+			return 0;
+		}
+
+		++port;
+		if (port > LAST_PORT) {
+			port = FIRST_PORT;
+		}
+	} while (port != start);
+
+	return -1;
+} /* BindData */
+
 static
 int Passive(struct sockaddr_in *saddr, int *weird)
 {
@@ -742,32 +784,13 @@
 		return (-1);
 	}
 
+	if (BindData(dataSocket, &gOurClientDataAddr) < 0) {
+		Error(kDoPerror, "Could not bind the data socket.\n");
+		goto bad;
+	}
+
 	if ((gHasPASV == 0) || (mode == kSendPortMode)) {
 tryPort:
-		gOurClientDataAddr.sin_family = AF_INET;
-		gOurClientDataAddr = gOurClientCtrlAddr;
-		/* bind will assign us an unused port, typically between 1024-5000. */ 
-		gOurClientDataAddr.sin_port = 0;
-		
-#ifdef HAVE_LIBSOCKS
-		if (Rbind(dataSocket, (struct sockaddr *) &gOurClientDataAddr,
-			(int) sizeof (gOurClientDataAddr),
-			gServerCtrlAddr.sin_addr.s_addr) < 0) 
-#else
-		if (bind(dataSocket, (struct sockaddr *) &gOurClientDataAddr,
-			(int) sizeof (gOurClientDataAddr)) < 0) 
-#endif
-		{
-			Error(kDoPerror, "Could not bind the data socket.\n");
-			goto bad;
-		}
-	
-		/* Need to do this so we can figure out which port the system
-		 * gave to us.
-		 */
-		if (GetSocketAddress(dataSocket, &gOurClientDataAddr) < 0)
-			goto bad;
-	
 		if (listen(dataSocket, 1) < 0) {
 			Error(kDoPerror, "listen failed.\n");
 			goto bad;
@@ -779,10 +802,7 @@
 		gDataPortMode = kSendPortMode;
 	} else {
 		/* Passive mode.  Let the other side decide where to send. */
-		
-		gOurClientDataAddr.sin_family = AF_INET;
-		gOurClientDataAddr = gOurClientCtrlAddr;
-
+	
 		if ((!gHasPASV) || (Passive(&gOurClientDataAddr, &weirdPort) < 0)) {
 			Error(kDontPerror, "Passive mode refused.\n");
 			gHasPASV = 0;
Results 1 - 1
Help - FTP Sites List - Software Dir.
Searching half a billion files worldwide
© 1997-2009 MARUHN Internet Solutions