[1798] in Moira

home help back first fref pref prev next nref lref last post

network.gen changes

daemon@ATHENA.MIT.EDU (Garry Zacheiss)
Tue May 22 03:07:03 2001

Message-Id: <200105220706.DAA13064@riff-raff.mit.edu>
To: moiradev@MIT.EDU
Date: Tue, 22 May 2001 03:06:56 -0400
From: Garry Zacheiss <zacheiss@MIT.EDU>

	These are the changes kretch asked for to network.gen to include
the netmask (in CIDR notation) for each network defined in moira.

Garry

Index: network.pc
===================================================================
RCS file: /afs/athena.mit.edu/astaff/project/moiradev/repository/moira/gen/network.pc,v
retrieving revision 1.5
diff -u -r1.5 network.pc
--- network.pc	2000/11/30 23:27:45	1.5
+++ network.pc	2001/05/22 07:05:22
@@ -27,15 +27,18 @@
 char *whoami = "network.gen";
 char *db = "moira/moira";
 
+char *cidr_from_inaddr(struct in_addr in);
+
 int main(int argc, char **argv)
 {
   FILE *out = stdout;
-  char *outf = NULL, outft[MAXPATHLEN];
+  char *outf = NULL, *cidr = NULL, address[BUFSIZ], outft[MAXPATHLEN];
   struct timeval now;
-  struct in_addr addr;
+  struct in_addr addr, maskaddr;
   EXEC SQL BEGIN DECLARE SECTION;
   int id, saddr;
   char name[SUBNET_NAME_SIZE], description[SUBNET_DESCRIPTION_SIZE];
+  char mask[SUBNET_MASK_SIZE];
   EXEC SQL END DECLARE SECTION;
 
   EXEC SQL CONNECT :db;
@@ -70,12 +73,12 @@
 	  ctime(&now.tv_sec));
 
   EXEC SQL DECLARE x CURSOR FOR SELECT
-    name, snet_id, saddr, description
+    name, snet_id, saddr, mask, description
     FROM subnet ORDER BY saddr;
   EXEC SQL OPEN x;
   while (1)
     {
-      EXEC SQL FETCH x INTO :name, :id, :saddr, :description;
+      EXEC SQL FETCH x INTO :name, :id, :saddr, :mask, :description;
       if (sqlca.sqlcode)
 	break;
       if (id == 0)
@@ -83,8 +86,16 @@
       if (!*strtrim(name))
 	continue;
       addr.s_addr = htonl(saddr);
-      fprintf(out, "NETWORK : %-16.16s : %-12.12s : %s\n", name,
-	      inet_ntoa(addr), strtrim(description));
+
+      maskaddr.s_addr = htonl(atoi(mask));
+      cidr = cidr_from_inaddr(maskaddr);
+
+      strcpy(address, inet_ntoa(addr));
+      if (cidr)
+	strcat(address, cidr);
+
+      fprintf(out, "NETWORK : %-16.16s : %-15.15s : %s\n", name,
+	      address, strtrim(description));
     }
 
   EXEC SQL CLOSE x;
@@ -104,4 +115,64 @@
 sqlerr:
   db_error(sqlca.sqlcode);
   exit(MR_DBMS_ERR);
+}
+
+char *cidr_from_inaddr(struct in_addr in) {
+  char *ptr1, *ptr2, *address, *out;
+  int a, b, c, d, addr, i, j = 0, k = 0;
+  int bits[32];
+
+  address = inet_ntoa(in);
+  
+  ptr1 = ptr2 = address;
+  ptr2 = strchr(ptr1, '.');
+  if (!ptr2) 
+    return(NULL);
+  a = atoi(ptr1);
+
+  ptr1 = ptr2 + 1;
+  ptr2 = strchr(ptr1, '.');
+  if (!ptr2)
+    return(NULL);
+  b = atoi(ptr1);
+
+  ptr1 = ptr2 + 1;
+  ptr2 = strchr(ptr1, '.');
+  if (!ptr2)
+    return(NULL);
+  c = atoi(ptr1);
+
+  ptr1 = ptr2 + 1;
+  d = atoi(ptr1);
+
+  if (a < 0 || a > 255 ||
+      b < 0 || b > 255 ||
+      c < 0 || c > 255 ||
+      d < 0 || d > 255)
+    return(NULL);
+
+  addr = d + (c*256) + (b*256*256) + (a*256*256*256);
+
+  for (i = 0; i < 32; i++) {
+    bits[i] = (addr & 1);
+    addr = (addr >> 1);
+  }
+  
+  while (bits[j] == 0) {
+    j++;
+    if (j > 31) break;
+  }
+  while (bits[j] == 1) {
+    j++;
+    k++;
+    if (j > 31) break;
+  }
+
+  if (j != 32)
+    return(NULL);
+
+  out = malloc(20);
+  sprintf(out, "/%i", k);
+
+  return(out);
 }

home help back first fref pref prev next nref lref last post