- let attr_set = self.routes.lock().unwrap().get_route_attrs(addr);
- let mut paths: Vec<(ASPath, u32, u32)> = Vec::new();
- for attrs in attr_set.iter() {
- let mut as4_path = None;
- let mut as_path = None;
- let mut pref = 100;
- let mut med = 0;
- for attr in attrs.iter() {
- match attr {
- PathAttribute::AS4_PATH(path) => as4_path = Some(path),
- PathAttribute::AS_PATH(path) => as_path = Some(path),
- PathAttribute::LOCAL_PREF(p) => pref = *p,
- PathAttribute::MULTI_EXIT_DISC(m) => med = *m,
- _ => {},
+ let mut path_vecs = self.routes.lock().unwrap().get_route_attrs(addr).clone();
+ if path_vecs.is_empty() { return 0; }
+
+ path_vecs.sort_unstable_by(|path_a, path_b| {
+ path_a.pref.cmp(&path_b.pref)
+ .then(path_b.path.len().cmp(&path_a.path.len()))
+ .then(path_b.med.cmp(&path_a.med))
+ });
+
+ let primary_route = path_vecs.pop().unwrap();
+ 'asn_candidates: for asn in primary_route.path.iter().rev() {
+ for secondary_route in path_vecs.iter() {
+ if !secondary_route.path.contains(asn) {
+ continue 'asn_candidates;