network analyzer - first attempt
[svn/Prometheus-QoS/.git] / networks.c
diff --git a/networks.c b/networks.c
new file mode 100644 (file)
index 0000000..2c7df01
--- /dev/null
@@ -0,0 +1,107 @@
+/* Modified by: xChaos, 20131028 */
+
+#include "cll1-0.6.2.h"
+#include "ipstruct.h"
+
+#define STRLEN 512
+
+extern struct IP *ips, *networks;
+
+struct IP* find_network_for_ip(char *ipaddr_orig)
+{
+ struct IP *network;
+ char *netaddr, *lastnum, *ipaddr;
+ int ipnum, netnum;
+
+ duplicate(ipaddr_orig, ipaddr);
+ lastnum = strrchr(ipaddr, '.');
+ if(lastnum)
+ {
+  ipnum = atoi(lastnum + 1);
+  *lastnum = 0;
+ }
+
+ for_each(network, networks)
+ {
+  duplicate(network->addr, netaddr);
+  lastnum = strrchr(netaddr, '.');
+  if(lastnum)
+  {
+   netnum = atoi(lastnum + 1);
+   *lastnum = 0;
+//   printf("%s/%d + %d\n",network->addr,network->mask,(1<<(32-network->mask)));
+   if(     eq(netaddr, ipaddr) 
+       and netnum + (1<<(32-network->mask)) > ipnum
+       and netnum <= ipnum)
+   {
+    return network;
+   }       
+  }
+ }
+ return NULL;
+}
+
+void analyse_topology(char *traceroute)
+{
+ char *buf, *netaddr, *ptr, *lastnum, *str;
+ int col, gateway, netnum, tracert;
+ struct IP *network=NULL, *ip;
+
+ for_each(ip, networks)
+ {
+  printf("%s/%d %s min=%d max=%d sum=%d\n",ip->addr, ip->mask, ip->name, ip->min, ip->max, ip->desired); 
+ }
+
+ /*-----------------------------------------------------------------*/
+ puts("Analysing network topology ...");
+ /*-----------------------------------------------------------------*/
+ for_each(ip, networks)
+ { 
+  printf("%s/%d %s\n",ip->addr, ip->mask, ip->name);
+  duplicate(ip->addr, buf);
+  lastnum = strrchr(buf, '.');
+  if(lastnum)
+  {
+   gateway = atoi(lastnum + 1) + 1;  /* this is just common rule... */
+   *lastnum = 0;
+   string(str,STRLEN); 
+   sprintf(str, traceroute, buf, gateway);
+   shell(str);
+   input(str,STRLEN)
+   {
+    if(    not strstr(str, "traceroute")
+       and not strstr(str, "* * *"))
+    {
+     printf("%s",str);
+     duplicate(str, buf);
+     valid_columns(ptr, buf, ' ', col)
+     if(*ptr=='*')
+     {
+      col--;
+     }
+     else if(col==2)
+     {
+      printf("via [%s]\n", ptr);
+      network = find_network_for_ip(ptr);
+      if(network)
+      {
+       network->min += ip->min;
+       network->desired += ip->max;
+       if(ip->max > network->max)
+       {
+        network->max = ip->max;
+       }
+      }      
+     }
+    }
+   }
+  }
+ }
+ sort(network, networks, desc_order_by, min);
+ sort(network, networks, desc_order_by, max);
+ for_each(ip, networks)
+ {
+  printf("%s/%d %s min=%d max=%d sum=%d\n",ip->addr, ip->mask, ip->name, ip->min, ip->max, ip->desired); 
+ }
+ exit(-1);
+}
This page took 0.134125 seconds and 4 git commands to generate.