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

CVS update: Vanilla/ntserv



Date:	Tuesday February 29, 2000 @ 20:49
Author:	unbelver

Update of /home/netrek/cvsroot/Vanilla/ntserv
In directory swashbuckler.fortress.real-time.com:/var/tmp/cvs-serv4903/ntserv

Modified Files:
	solicit.c 
Log Message:

Two things to commit.

First: Re-write the metaservers file parser to be a little smarter and
actually work. fscanf() sucks.  Some error checking still has to be
done.

Second: Solicit now binds to the interface listed in the
"this.host.name" field for outgoing solicit packets.  It will fall
back to the default interface if "this.host.name" is invalid, or
doesn't belong to the machine its running on.

--Carlos V.




****************************************

Index: Vanilla/ntserv/solicit.c
diff -u Vanilla/ntserv/solicit.c:1.13 Vanilla/ntserv/solicit.c:1.14
--- Vanilla/ntserv/solicit.c:1.13	Mon Feb 21 21:45:09 2000
+++ Vanilla/ntserv/solicit.c	Tue Feb 29 20:49:40 2000
@@ -47,12 +47,31 @@
   }
   
   /* bind the local socket */
-  m->address.sin_addr.s_addr = INADDR_ANY;
+  /* bind to interface attatched to this.host.name */
+
+  /* numeric first */
+  if( (m->address.sin_addr.s_addr = inet_addr(m->ours)) == -1 ) {
+    struct hostent *hp;
+
+    /* then name */
+    if ((hp = gethostbyname(m->ours)) == NULL) {
+      /* bad hostname or unable to get ip address, use default interface */
+      m->address.sin_addr.s_addr = INADDR_ANY;
+      fprintf(stderr, "Bad this.host.name field in .metaservers. Using default interface.\n");
+    } else {
+      m->address.sin_addr.s_addr = *(long *) hp->h_addr;
+    }
+  }
+
   m->address.sin_family      = AF_INET;
   m->address.sin_port        = 0;
   if (bind(m->sock,(struct sockaddr *)&m->address, sizeof(m->address)) < 0) {
-    perror("solicit: udp_attach: bind");
-    return 0;
+    perror("solicit: udp_attach: bind, trying default interface next");
+    m->address.sin_addr.s_addr = INADDR_ANY;
+    if (bind(m->sock,(struct sockaddr *)&m->address, sizeof(m->address)) < 0) {
+      perror("solicit: udp_attatch: bind, 2nd try failed");
+      return 0;
+    }
   }
   
   /* build the destination address */
@@ -99,6 +118,8 @@
   char *name, *login;             /* name and login guaranteed not blank */
   char unknown[] = "unknown";     /* unknown player/login string */
   char *here = packet;
+  char line[256];                /* where to hold the .metaservers line */
+  char *token;                   /* current line token */
   time_t now = time(NULL);
   int gamefull = 0;              /* is the game full? */
   int isrsa = 0;                 /* is this server RSA? */
@@ -121,13 +142,68 @@
     for (i=0; i<MAXMETASERVERS; i++) {
       struct metaserver *m = &metaservers[i];
       
-      /* if end of file reached, stop */
+      /* scan the line */
+
+      fgets(line, 255, file);
+
+      /* if end of file reached, stop assumes there is a \n before a eof */
+      
       if (feof(file)) break;
+
+      token = strtok(line, " ");
+      if(token != NULL)
+	strncpy(m->host, token, 32); /* meta host name */
+      else
+	continue;         /* bad line, try next one */
+
+      token = strtok(NULL, " ");
+      if(token != NULL)
+	m->port = atoi(token);     /* meta port */
+      else
+	continue;         /* bad line, try next one */
+
+      token = strtok(NULL, " ");
+      if(token != NULL)
+	m->minimum = atoi(token); /* min solicit time */
+      else
+	continue;         /* bad line, try next one */
+
+      token = strtok(NULL, " ");
+      if(token != NULL)
+	m->maximum = atoi(token); /* max solicit time */
+      else
+	continue;         /* bad line, try next one */
+
+      token = strtok(NULL, " ");
+      if(token != NULL)
+	strncpy(m->ours, token, 32); /* our host name */
+      else
+	continue;         /* bad line, try next one */
       
-      /* scan the line */
-      fscanf(file, "%s %d %d %d %s %s %d %d %[^\n]\n", m->host, &m->port,
-	     &m->minimum, &m->maximum, m->ours, m->type, &m->pport,
-	     &m->oport, m->comment);
+      token = strtok(NULL, " ");
+      if(token != NULL)
+	strncpy(m->type, token, 2); /* server type */
+      else
+	continue;         /* bad line, try next one */
+
+      token = strtok(NULL, " ");
+      if(token != NULL)
+	m->pport = atoi(token);     /* player port */
+      else
+	continue;         /* bad line, try next one */
+      
+      token = strtok(NULL, " ");
+      if(token != NULL)
+	m->oport = atoi(token);     /* observer port */
+      else
+	  continue;         /* bad line, try next one */
+
+      token = strtok(NULL, " ");
+      if(token != NULL)
+	strncpy(m->comment, token, 32); /* comment line */
+      else
+	continue;         /* bad line, try next one */
+
 
       /* force minimum and maximum delays (see note on #define) */
       if (m->minimum < META_MINIMUM_DELAY)