Vanilla List Mailing List Archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Virtual-hosting newstartd



Hi all,

I've done a bit of work on making netrek bind to a single interface, so
you can run multiple servers on the same port on different ips of a single
machine. (You can see this in action right now with the hockey and newbie
servers that jeffno is running on a single machine of mine...)

The first patch allows you to append "ip:" to the start of a port in
.ports, and have newstartd only listen on that ip (the old format to
listen on all ips still works).

The second patch is to fix UDP_PORTSWAP, so that the returned UDP packets
come from the virtual host IP instead of the main IP, as portswap didn't
work at all after I made the first change. (This changes normal UDP to do
the same thing, but this doesn't seem to be a problem, and it seems to
make more sense to have UDP come from the IP that you connect to in any
case.)

This is my first time looking at the networking code, so I have no clue if
there's anywhere else that I need to make changes. Please let me know if
there is...

(Also: should I be bugging someone for CVS write access to submit this
stuff?)

- Karthik
--- /home/netrek/Vanilla-2.9pl5/newstartd/newstartd.c	Fri Feb 18 18:47:36 2000
+++ newstartd.c	Sat Feb 19 16:30:07 2000
@@ -98,6 +98,7 @@
   int accepts;				/* count of accept() calls	*/
   int denials;				/* count of deny() calls	*/
   int forks;				/* count of fork() calls	*/
+  unsigned long addr;		/* address to bind() to		*/
 } prog[MAXPROG];
 
 extern char peerhostname[];		/* defined in newaccess.c	*/
@@ -400,7 +401,7 @@
       }
       
       addr.sin_family = AF_INET;
-      addr.sin_addr.s_addr = INADDR_ANY;
+      addr.sin_addr.s_addr = prog[i].addr;
       addr.sin_port = htons (prog[i].port);
       
       if (bind (sock, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
@@ -645,22 +646,35 @@
 int read_portfile (char *portfile)
 {
   FILE *fi;
-  char buf[BUFSIZ];
-  int i = 0, p, n;
+  char buf[BUFSIZ], addrbuf[BUFSIZ], *port;
+  int i = 0, n;
   struct stat sbuf;
 
   fi = fopen (portfile, "r");
   if (fi) {
     while (fgets (buf, BUFSIZ, fi)) {
-      if ((n = sscanf (buf, "%d %s \"%[^\"]\" %s %s %s %s", &p,
+	if (buf[0] == '#')
+		continue;
+      if ((n = sscanf (buf, "%s %s \"%[^\"]\" %s %s %s %s",
+               addrbuf,
 		       prog[i].prog,
 		       prog[i].progname,
 		       prog[i].arg[0],
 		       prog[i].arg[1],
 		       prog[i].arg[2],
 		       prog[i].arg[3])) >= 3) {
+	if (!(port = strchr(addrbuf, ':')))
+	{
+		prog[i].addr = INADDR_ANY;
+		prog[i].port = atoi(addrbuf);
+	}
+	else
+	{
+		*port++ = '\0';
+		prog[i].addr = inet_addr(addrbuf);
+		prog[i].port = atoi(port);
+	};
 	prog[i].nargs = n-3;
-	prog[i].port = (unsigned short) p;
 	prog[i].sock = -1;
 	prog[i].internal = (!strcmp (prog[i].prog, "special"));
 	prog[i].accepts = 0;
--- /home/netrek/Vanilla-2.9pl5/ntserv/socket.c	Fri Jul 23 18:09:13 1999
+++ socket.c	Mon Feb 21 02:30:42 2000
@@ -2261,7 +2261,7 @@
 
 static int connUdpConn(void)
 {
-    struct sockaddr_in addr;
+    struct sockaddr_in addr, myaddr;
     int len;
 
     if (udpSock > 0) {
@@ -2279,8 +2279,18 @@
     }
 
 #ifdef UDP_FIX 			/* 15/6/93 SK UDP connection time out fix */
+	len = sizeof(myaddr);
+	if (getsockname(sock, (struct sockaddr *)&myaddr, &len) < 0)
+	{
+		perror("ntserv: cannot get address of socket");
+		close(udpSock);
+		udpSock = -1;
+		return -1;
+	}
+
     addr.sin_family = AF_INET;
-    addr.sin_addr.s_addr = INADDR_ANY;
+	addr.sin_addr.s_addr = myaddr.sin_addr.s_addr;
+	UDPDIAG(("UDP request to %s\n", inet_ntoa(myaddr.sin_addr)));
     addr.sin_port = 0;
 
     if (bind(udpSock, &addr, sizeof(addr)) < 0) {