| 1 | /* Modified by: xChaos, 20131029 */ |
| 2 | |
| 3 | #include "cll1-0.6.2.h" |
| 4 | #include "ipstruct.h" |
| 5 | |
| 6 | #define STRLEN 512 |
| 7 | |
| 8 | extern struct IP *ip, *ips, *networks; |
| 9 | |
| 10 | struct IP *locate_network(char *look_for) |
| 11 | { |
| 12 | struct IP *network; |
| 13 | char *netaddr, *lastnum, *ipaddr; |
| 14 | int ipnum, netnum, total; |
| 15 | |
| 16 | duplicate(look_for, ipaddr); |
| 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, '.'); |
| 28 | if(lastnum or total) |
| 29 | { |
| 30 | netnum = atoi(lastnum + 1); |
| 31 | *lastnum = 0; |
| 32 | if( eq(netaddr, ipaddr) |
| 33 | and netnum + (1<<(32-network->mask)) > ipnum |
| 34 | and netnum <= ipnum) |
| 35 | { |
| 36 | return network; |
| 37 | } |
| 38 | } |
| 39 | } |
| 40 | return NULL; |
| 41 | } |
| 42 | |
| 43 | |
| 44 | void 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; |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | |
| 69 | void update_network(char *look_for, struct IP* ip) |
| 70 | { |
| 71 | update_network_direct(locate_network(look_for),ip); |
| 72 | } |
| 73 | |
| 74 | |
| 75 | void analyse_topology(char *traceroute) |
| 76 | { |
| 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); |
| 83 | |
| 84 | /*-----------------------------------------------------------------*/ |
| 85 | puts("Analysing network topology ..."); |
| 86 | /*-----------------------------------------------------------------*/ |
| 87 | for_each(ip, networks) if(ip->group) /* just non-empty networks */ |
| 88 | { |
| 89 | printf("%s/%d %s ", ip->addr, ip->mask, ip->name); |
| 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; |
| 96 | sprintf(str, traceroute, buf, gateway); |
| 97 | shell(str); |
| 98 | *redundant = 0; |
| 99 | distance = 1; |
| 100 | uplink = NULL; |
| 101 | input(str,STRLEN) |
| 102 | { |
| 103 | if(not strstr(str, "traceroute")) /*skip header */ |
| 104 | { |
| 105 | #ifdef DEBUG |
| 106 | printf("%s",str); |
| 107 | #endif |
| 108 | duplicate(str, buf); |
| 109 | valid_columns(ptr, buf, ' ', col) |
| 110 | if(*ptr == '*') |
| 111 | { |
| 112 | col -= 1; |
| 113 | } |
| 114 | else if(col==2 and not strstr(redundant, ptr)) |
| 115 | { |
| 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 | } |
| 141 | } |
| 142 | } |
| 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"); |
| 157 | } |
| 158 | } |
| 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); |
| 175 | } |
| 176 | |
| 177 | sort(network, networks, desc_order_by, min); /* secondary ordering */ |
| 178 | sort(network, networks, desc_order_by, desired); /* primary ordering */ |
| 179 | |
| 180 | /*-----------------------------------------------------------------*/ |
| 181 | puts("\nRequested network parameters are:"); |
| 182 | /*-----------------------------------------------------------------*/ |
| 183 | for_each(ip, networks) if(ip->desired) |
| 184 | { |
| 185 | printf("%s/%d %s REQUESTED=%dM (classes=%d, sum_min=%dk, max_1=%dk, sum_max=%LuM, agreg.=1:%d)\n", |
| 186 | ip->addr, ip->mask, ip->name, ip->desired, ip->group, ip->min, ip->max, ip->direct, |
| 187 | (int)(ip->direct/(float)(ip->desired)+.5)); |
| 188 | } |
| 189 | exit(-1); |
| 190 | } |