really dumbe string pointer error
[svn/Prometheus-QoS/.git] / networks.c
CommitLineData
cdfd88eb 1/* Modified by: xChaos, 20131029 */
af37be1d 2
3#include "cll1-0.6.2.h"
4#include "ipstruct.h"
5
6#define STRLEN 512
7
9694a8ec 8extern struct IP *ip, *ips, *networks;
af37be1d 9
9694a8ec 10struct IP *locate_network(char *look_for)
af37be1d 11{
12 struct IP *network;
13 char *netaddr, *lastnum, *ipaddr;
9694a8ec 14 int ipnum, netnum, total;
af37be1d 15
2b460b72 16 duplicate(look_for, ipaddr);
af37be1d 17 lastnum = strrchr(ipaddr, '.');
18 if(lastnum)
19 {
20 ipnum = atoi(lastnum + 1);
21 *lastnum = 0;
22 }
23
24 for_each(network, networks)
25 {
26 duplicate(network->addr, netaddr);
27 lastnum = strrchr(netaddr, '.');
9694a8ec 28 if(lastnum or total)
af37be1d 29 {
30 netnum = atoi(lastnum + 1);
31 *lastnum = 0;
9694a8ec 32 if( eq(netaddr, ipaddr)
33 and netnum + (1<<(32-network->mask)) > ipnum
34 and netnum <= ipnum)
af37be1d 35 {
9694a8ec 36 return network;
37 }
38 }
39 }
40 return NULL;
41}
2b460b72 42
2b460b72 43
9694a8ec 44void update_network_direct(struct IP *network, struct IP *ip)
45{
46 if(ip and network)
47 {
48 network->group += 1;
49 network->min += ip->min;
50 network->direct += ip->max>>10; /* sum of Mbps, not kbps */
51
52 if(ip->max > network->max)
53 {
54 network->max = ip->max;
55 }
56
57 if(network->max > network->min)
58 {
59 network->desired = network->max>>10;
60 }
61 else
62 {
63 network->desired = network->min>>10;
af37be1d 64 }
65 }
af37be1d 66}
67
9694a8ec 68
69void update_network(char *look_for, struct IP* ip)
70{
71 update_network_direct(locate_network(look_for),ip);
72}
73
74
af37be1d 75void analyse_topology(char *traceroute)
76{
9694a8ec 77 char *buf, *netaddr, *ptr, *lastnum, *str, *redundant;
78 int col, gateway, netnum, tracert, distance;
79 struct IP *network = NULL, *uplink = NULL;
80
81 string(redundant, STRLEN);
82 string(str, STRLEN);
af37be1d 83
af37be1d 84 /*-----------------------------------------------------------------*/
85 puts("Analysing network topology ...");
86 /*-----------------------------------------------------------------*/
9694a8ec 87 for_each(ip, networks) if(ip->group) /* just non-empty networks */
af37be1d 88 {
9694a8ec 89 printf("%s/%d %s ", ip->addr, ip->mask, ip->name);
af37be1d 90 duplicate(ip->addr, buf);
91 lastnum = strrchr(buf, '.');
92 if(lastnum)
93 {
94 gateway = atoi(lastnum + 1) + 1; /* this is just common rule... */
95 *lastnum = 0;
af37be1d 96 sprintf(str, traceroute, buf, gateway);
97 shell(str);
9694a8ec 98 *redundant = 0;
99 distance = 1;
100 uplink = NULL;
af37be1d 101 input(str,STRLEN)
102 {
9694a8ec 103 if(not strstr(str, "traceroute")) /*skip header */
af37be1d 104 {
9694a8ec 105#ifdef DEBUG
af37be1d 106 printf("%s",str);
9694a8ec 107#endif
af37be1d 108 duplicate(str, buf);
109 valid_columns(ptr, buf, ' ', col)
9694a8ec 110 if(*ptr == '*')
af37be1d 111 {
9694a8ec 112 col -= 1;
af37be1d 113 }
9694a8ec 114 else if(col==2 and not strstr(redundant, ptr))
af37be1d 115 {
9694a8ec 116#ifdef DEBUG
117 printf("[%s] ", ptr);
118#endif
119 network = locate_network(ptr);
120 if(network)
121 {
122 printf("[%s/%d] ", network->addr, network->mask);
123 network->mark = distance;
124 distance += 1;
125 if(uplink)
126 {
127 network->uplink = uplink;
128#ifdef DEBUG
129 printf("[%d: uplink of %s/%d is %s/%d] ",
130 network->mark, network->addr, network->mask, network->uplink->addr, network->uplink->mask);
131#endif
132 }
133 uplink = network;
134 }
135
136 if(strlen(redundant) < STRLEN - 17)
137 {
138 strcat(redundant, ptr);
139 strcat(redundant, " ");
140 }
af37be1d 141 }
142 }
9694a8ec 143 }//end input loop
144
145// ip->mark = distance;
146// if(uplink)
147// {
148// ip->uplink = uplink;
149// }
150 if(distance == 1)
151 {
152 printf("fail! \n");
153 }
154 else
155 {
156 printf("done. \n");
af37be1d 157 }
158 }
9694a8ec 159 }//end for_each()
160
161 TheIP("0.0.0.0", TRUE);
162 ip->name = "TOTAL";
163 ip->mask = 0;
164
165 sort(network, networks, desc_order_by, mark); /* most distant networks first */
166 for_each(network, networks)
167 {
168 if(not network->uplink)
169 {
170 network->uplink = ip; /* global checksum */
171 }
172 printf("(%d) uplink of %s/%d is %s/%d \n",
173 network->mark, network->addr, network->mask, network->uplink->addr, network->uplink->mask);
174 update_network_direct(network->uplink, network);
af37be1d 175 }
9694a8ec 176
177 sort(network, networks, desc_order_by, min); /* secondary ordering */
178 sort(network, networks, desc_order_by, desired); /* primary ordering */
2b460b72 179
180 /*-----------------------------------------------------------------*/
9694a8ec 181 puts("\nRequested network parameters are:");
2b460b72 182 /*-----------------------------------------------------------------*/
2e5fdf25 183 for_each(ip, networks) if(ip->desired)
af37be1d 184 {
2b460b72 185 printf("%s/%d %s REQUESTED=%dM (classes=%d, sum_min=%dk, max_1=%dk, sum_max=%LuM, agreg.=1:%d)\n",
2e5fdf25 186 ip->addr, ip->mask, ip->name, ip->desired, ip->group, ip->min, ip->max, ip->direct,
9694a8ec 187 (int)(ip->direct/(float)(ip->desired)+.5));
af37be1d 188 }
189 exit(-1);
190}
This page took 0.277821 seconds and 4 git commands to generate.