From: Vasu Dasari <vdasari@gmail.com>
[mpls-ldp-portable.git] / common / mpls_compare.c
blobd2aa1c386a5b288b8b9e79caeceb04c6a618d025
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;
117 if (f1->type != f2->type) {
118 return 1;
121 switch(f1->type) {
122 case MPLS_FEC_PREFIX:
123 if ((retval = mpls_inet_addr_compare(&f1->u.prefix.network,
124 &f2->u.prefix.network))) {
125 return retval;
127 if (f1->u.prefix.length > f2->u.prefix.length) {
128 return (f1->u.prefix.length != f2->u.prefix.length) ? 1 : -1;
130 break;
131 case MPLS_FEC_HOST:
132 return mpls_inet_addr_compare(&f1->u.host, &f2->u.host);
133 case MPLS_FEC_L2CC:
134 if (f1->u.l2cc.connection_id != f2->u.l2cc.connection_id) {
135 return (f1->u.l2cc.connection_id>f2->u.l2cc.connection_id) ? 1 : -1;
137 if (f1->u.l2cc.group_id != f2->u.l2cc.group_id) {
138 return (f1->u.l2cc.group_id > f2->u.l2cc.group_id) ? 1 : -1;
140 if (f1->u.l2cc.type != f2->u.l2cc.type) {
141 return 1;
143 break;
144 default:
145 MPLS_ASSERT(0);
147 return 0;