If and when I implement RSVP-TE I need the naming scheme to make
[mpls-ldp-portable.git] / common / mpls_compare.c
blob002d60072a69072ea343342e138db556323bd72a
1 #include "mpls_struct.h"
2 #include "mpls_assert.h"
4 int mpls_inet_addr_compare(struct mpls_inet_addr *addr1,
5 struct mpls_inet_addr *addr2) {
6 if (addr1->type != addr2->type) {
7 return 1;
9 switch(addr1->type) {
10 case MPLS_FAMILY_IPV4:
11 if (addr1->u.ipv4 != addr2->u.ipv4) {
12 return addr1->u.ipv4 > addr2->u.ipv4 ? 1 : -1;
14 break;
15 case MPLS_FAMILY_IPV6:
16 return memcmp(addr1->u.ipv6, addr2->u.ipv6, 16);
17 default:
18 MPLS_ASSERT(0);
20 return 0;
23 int mpls_nexthop_compare(struct mpls_nexthop *nh1, struct mpls_nexthop *nh2) {
24 int retval = 0;
25 int match = 0;
27 if (nh1->type != nh2->type) {
28 return 1;
30 if (nh1->type & MPLS_NH_IP) {
31 match++;
32 if ((retval = mpls_inet_addr_compare(&nh1->ip, &nh2->ip))) {
33 return retval;
36 if (nh1->type & MPLS_NH_IF) {
37 match++;
38 if ((retval = mpls_if_handle_compare(nh1->if_handle, nh2->if_handle))) {
39 return retval;
42 if (nh1->type & MPLS_NH_OUTSEGMENT) {
43 match++;
44 if ((retval = mpls_outsegment_handle_compare(nh1->outsegment_handle,
45 nh2->outsegment_handle))) {
46 return retval;
50 if (!match) {
51 return 1;
53 return 0;
56 int mpls_label_struct_compare(struct mpls_label_struct* l1,
57 struct mpls_label_struct* l2) {
58 if (l1->type != l2->type) {
59 return 1;
61 switch(l1->type) {
62 case MPLS_LABEL_TYPE_GENERIC:
63 if (l1->u.gen != l2->u.gen) {
64 return (l1->u.gen > l2->u.gen) ? 1 : -1;
66 break;
67 case MPLS_LABEL_TYPE_ATM:
68 if (l1->u.atm.vpi != l2->u.atm.vpi) {
69 return (l1->u.atm.vpi > l2->u.atm.vpi) ? 1 : -1;
71 if (l1->u.atm.vci != l2->u.atm.vci) {
72 return (l1->u.atm.vci > l2->u.atm.vci) ? 1 : -1;
74 break;
75 case MPLS_LABEL_TYPE_FR:
76 if (l1->u.fr.len != l2->u.fr.len) {
77 return (l1->u.fr.dlci > l2->u.fr.dlci) ? 1 : -1;
79 if (l1->u.fr.dlci != l2->u.fr.dlci) {
80 return (l1->u.fr.len > l2->u.fr.len) ? 1 : -1;
82 break;
83 default:
84 MPLS_ASSERT(0);
86 return 0;
89 int mpls_dest_compare(struct mpls_dest* d1, struct mpls_dest* d2) {
90 int retval;
91 if ((retval = mpls_inet_addr_compare(&d1->addr, &d2->addr))) {
92 return retval;
94 if (d1->port != d2->port) {
95 return (d1->port > d2->port) ? 1 : -1;
97 if ((retval = mpls_if_handle_compare(d1->if_handle, d2->if_handle))) {
98 return retval;
100 return 0;
103 int mpls_range_compare(struct mpls_range* r1, struct mpls_range* r2) {
104 int retval;
105 if ((retval = mpls_label_struct_compare(&r1->min, &r2->min))) {
106 return retval;
108 if ((retval = mpls_label_struct_compare(&r1->max, &r2->max))) {
109 return retval;
111 return 0;
114 int mpls_fec_compare(struct mpls_fec* f1, struct mpls_fec* f2) {
115 int retval;
116 if ((retval = mpls_nexthop_compare(&f1->nh, &f2->nh))) {
117 return retval;
120 if (f1->type != f2->type) {
121 return 1;
123 switch(f1->type) {
124 case MPLS_FEC_PREFIX:
125 if ((retval = mpls_inet_addr_compare(&f1->u.prefix.network,
126 &f2->u.prefix.network))) {
127 return retval;
129 if (f1->u.prefix.length > f2->u.prefix.length) {
130 return (f1->u.prefix.length != f2->u.prefix.length) ? 1 : -1;
132 break;
133 case MPLS_FEC_HOST:
134 return mpls_inet_addr_compare(&f1->u.host, &f2->u.host);
135 case MPLS_FEC_L2CC:
136 if (f1->u.l2cc.connection_id != f2->u.l2cc.connection_id) {
137 return (f1->u.l2cc.connection_id>f2->u.l2cc.connection_id) ? 1 : -1;
139 if (f1->u.l2cc.group_id != f2->u.l2cc.group_id) {
140 return (f1->u.l2cc.group_id > f2->u.l2cc.group_id) ? 1 : -1;
142 if (f1->u.l2cc.type != f2->u.l2cc.type) {
143 return 1;
145 break;
146 default:
147 MPLS_ASSERT(0);
149 return 0;