Complete.Org: Mailing Lists: Archives: freeciv-dev: May 2004:
[Freeciv-Dev] (PR#8653) Move min_free_port to utility
Home

[Freeciv-Dev] (PR#8653) Move min_free_port to utility

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: undisclosed-recipients: ;
Subject: [Freeciv-Dev] (PR#8653) Move min_free_port to utility
From: "Raimar Falke" <i-freeciv-lists@xxxxxxxxxxxxx>
Date: Sun, 2 May 2004 06:39:47 -0700
Reply-to: rt@xxxxxxxxxxx

<URL: http://rt.freeciv.org/Ticket/Display.html?id=8653 >


This patch does:
 - move the function to utility
 - rename it
 - clean it up a bit
 - correct it ( I have no idea who coded this

 int port, n, s;
...
   memcpy(&tmp. sin_addr, &n, sizeof(long));

but it should be obvious that this is unsafe for
sizeof(long)!=sizeof(int). Additionally it isn't endiansafe.
)

        Raimar

-- 
 email: rf13@xxxxxxxxxxxxxxxxx
 1 + 1 = 3, for large values of 1

Index: client/connectdlg_common.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/connectdlg_common.c,v
retrieving revision 1.10
diff -u -u -r1.10 connectdlg_common.c
--- client/connectdlg_common.c  25 Apr 2004 19:03:40 -0000      1.10
+++ client/connectdlg_common.c  2 May 2004 13:34:52 -0000
@@ -21,26 +21,18 @@
 #include <signal.h>
 #include <time.h>
 
-#ifdef HAVE_SYS_SOCKET_H
-  #include <sys/socket.h>
-#endif
-  
 #ifdef HAVE_SYS_TYPES_H
-  #include <sys/types.h>
+#include <sys/types.h>         /* fchmod */
 #endif
 
 #ifdef HAVE_SYS_STAT_H
-  #include <sys/stat.h>
+#include <sys/stat.h>          /* fchmod */
 #endif
 
 #ifdef HAVE_SYS_WAIT_H
 #include <sys/wait.h>
 #endif
 
-#ifdef HAVE_WINSOCK
-#include <winsock.h>
-#endif
-
 #include "fcintl.h"
 #include "mem.h"
 #include "netintf.h"
@@ -153,7 +145,7 @@
   append_output_window(_("Starting server..."));
 
   /* find a free port */ 
-  server_port = min_free_port();
+  server_port = find_next_free_port(5555);
 
   server_pid = fork();
   
@@ -263,31 +255,6 @@
 #else /* Can't do much without fork(). */
   return FALSE;
 #endif
-}
-
-/************************************************************************** 
-Finds the lowest port which can be used for the 
-server (starting at the 5555C)
-**************************************************************************/ 
-int min_free_port(void)
-{
-  int port, n, s;
-  struct sockaddr_in tmp;
-  
-  s = socket(AF_INET, SOCK_STREAM, 0);
-  n = INADDR_ANY;
-  port = 5554; /* make looping convenient */ 
-  do {
-    port++;
-    memset(&tmp, 0, sizeof(struct sockaddr_in));
-    tmp.sin_family = AF_INET;
-    tmp.sin_port = htons(port);
-    memcpy(&tmp. sin_addr, &n, sizeof(long));
-  } while(bind(s, (struct sockaddr*) &tmp, sizeof(struct sockaddr_in)));
-
-  my_closesocket(s);
-  
-  return port;
 }
 
 /**************************************************************** 
Index: utility/netintf.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/utility/netintf.c,v
retrieving revision 1.19
diff -u -u -r1.19 netintf.c
--- utility/netintf.c   25 Jan 2004 13:55:14 -0000      1.19
+++ utility/netintf.c   2 May 2004 13:34:59 -0000
@@ -225,3 +225,30 @@
 
   return fz_from_stream(fp);
 }
+
+/************************************************************************** 
+  Finds the next (lowest) free port.
+**************************************************************************/ 
+int find_next_free_port(int starting_port)
+{
+  int port, s = socket(AF_INET, SOCK_STREAM, 0);
+
+  for (port = starting_port;; port++) {
+    union my_sockaddr tmp;
+    struct sockaddr_in *sock = &tmp.sockaddr_in;
+
+    memset(&tmp, 0, sizeof(tmp));
+
+    sock->sin_family = AF_INET;
+    sock->sin_port = htons(port);
+    sock->sin_addr.s_addr = htonl(INADDR_ANY);
+
+    if (bind(s, &tmp.sockaddr, sizeof(tmp.sockaddr)) == 0) {
+      break;
+    }
+  }
+
+  my_closesocket(s);
+  
+  return port;
+}
Index: utility/netintf.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/utility/netintf.h,v
retrieving revision 1.9
diff -u -u -r1.9 netintf.h
--- utility/netintf.h   25 Jan 2004 13:55:14 -0000      1.9
+++ utility/netintf.h   2 May 2004 13:34:59 -0000
@@ -61,5 +61,6 @@
 bool net_lookup_service(const char *name, int port, 
                         union my_sockaddr *addr);
 fz_FILE *my_querysocket(int sock, void *buf, size_t size);
+int find_next_free_port(int starting_port);
 
 #endif  /* FC__NETINTF_H */

[Prev in Thread] Current Thread [Next in Thread]
  • [Freeciv-Dev] (PR#8653) Move min_free_port to utility, Raimar Falke <=