]> git.ipfire.org Git - people/ms/rstp.git/blob - rstplib/vector.c
Initial commit
[people/ms/rstp.git] / rstplib / vector.c
1 /************************************************************************
2 * RSTP library - Rapid Spanning Tree (802.1t, 802.1w)
3 * Copyright (C) 2001-2003 Optical Access
4 * Author: Alex Rozin
5 *
6 * This file is part of RSTP library.
7 *
8 * RSTP library is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License as published by the
10 * Free Software Foundation; version 2.1
11 *
12 * RSTP library is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with RSTP library; see the file COPYING. If not, write to the Free
19 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 * 02111-1307, USA.
21 **********************************************************************/
22
23 /* STP priority vectors API : 17.4.2 */
24
25 #include "base.h"
26 #include "stp_bpdu.h"
27 #include "vector.h"
28
29 int
30 STP_VECT_compare_bridge_id (BRIDGE_ID* b1, BRIDGE_ID* b2)
31 {
32 if (b1->prio < b2->prio)
33 return -1;
34
35 if (b1->prio > b2->prio)
36 return 1;
37 return memcmp (b1->addr, b2->addr, 6);
38 }
39
40 void
41 STP_VECT_copy (OUT PRIO_VECTOR_T* t, IN PRIO_VECTOR_T* f)
42 {
43 memcpy (t, f, sizeof (PRIO_VECTOR_T));
44 }
45
46 void
47 STP_VECT_create (OUT PRIO_VECTOR_T* t,
48 IN BRIDGE_ID* root_br,
49 IN unsigned long root_path_cost,
50 IN BRIDGE_ID* design_bridge,
51 IN PORT_ID design_port,
52 IN PORT_ID bridge_port)
53 {
54 memcpy (&t->root_bridge, root_br, sizeof (BRIDGE_ID));
55 t->root_path_cost = root_path_cost;
56 memcpy (&t->design_bridge, design_bridge, sizeof (BRIDGE_ID));
57 t->design_port = design_port;
58 t->bridge_port = bridge_port;
59 }
60
61 int
62 STP_VECT_compare_vector (PRIO_VECTOR_T* v1, PRIO_VECTOR_T* v2)
63 {
64 int bridcmp;
65
66 bridcmp = STP_VECT_compare_bridge_id (&v1->root_bridge, &v2->root_bridge);
67 if (bridcmp < 0) return bridcmp;
68
69 if (! bridcmp) {
70 bridcmp = v1->root_path_cost - v2->root_path_cost;
71 if (bridcmp < 0) return bridcmp;
72 if (! bridcmp) {
73 bridcmp = STP_VECT_compare_bridge_id (&v1->design_bridge, &v2->design_bridge);
74 if (bridcmp < 0) return bridcmp;
75 if (! bridcmp) {
76 bridcmp = v1->design_port - v2->design_port;
77 if (bridcmp < 0) return bridcmp;
78 if (! bridcmp)
79 return v1->bridge_port - v2->bridge_port;
80 }
81 }
82 }
83
84 return bridcmp;
85 }
86
87 static unsigned short
88 stp_vect_get_short (IN unsigned char* f)
89 {
90 return ntohs (*(unsigned short *)f);
91 }
92
93 static void
94 stp_vect_set_short (IN unsigned short f, OUT unsigned char* t)
95 {
96 *(unsigned short *)t = htons (f);
97 }
98
99 static void
100 stp_vect_get_bridge_id (IN unsigned char* c_br, OUT BRIDGE_ID* bridge_id)
101 {
102 bridge_id->prio = stp_vect_get_short (c_br);
103 memcpy (bridge_id->addr, c_br + 2, 6);
104 }
105
106 static void
107 stp_vect_set_bridge_id (IN BRIDGE_ID* bridge_id, OUT unsigned char* c_br)
108 {
109 stp_vect_set_short (bridge_id->prio, c_br);
110 memcpy (c_br + 2, bridge_id->addr, 6);
111 }
112
113 void
114 STP_VECT_get_vector (IN BPDU_BODY_T* b, OUT PRIO_VECTOR_T* v)
115 {
116 stp_vect_get_bridge_id (b->root_id, &v->root_bridge);
117
118 v->root_path_cost = ntohl (*((long*) b->root_path_cost));
119
120 stp_vect_get_bridge_id (b->bridge_id, &v->design_bridge);
121
122 v->design_port = stp_vect_get_short (b->port_id);
123 }
124
125 void
126 STP_VECT_set_vector (IN PRIO_VECTOR_T* v, OUT BPDU_BODY_T* b)
127 {
128 unsigned long root_path_cost;
129
130 stp_vect_set_bridge_id (&v->root_bridge, b->root_id);
131
132 root_path_cost = htonl (v->root_path_cost);
133 memcpy (b->root_path_cost, &root_path_cost, 4);
134
135 stp_vect_set_bridge_id (&v->design_bridge, b->bridge_id);
136
137 stp_vect_set_short (v->design_port, b->port_id);
138 }
139
140 #ifdef STP_DBG
141
142 void
143 STP_VECT_br_id_print (IN char *title, IN BRIDGE_ID* br_id, IN Bool cr)
144 {
145 Print ("%s=%04lX-%02x%02x%02x%02x%02x%02x",
146 title,
147 (unsigned long) br_id->prio,
148 (unsigned char) br_id->addr[0],
149 (unsigned char) br_id->addr[1],
150 (unsigned char) br_id->addr[2],
151 (unsigned char) br_id->addr[3],
152 (unsigned char) br_id->addr[4],
153 (unsigned char) br_id->addr[5]);
154 Print (cr ? "\n" : " ");
155 }
156
157 void
158 STP_VECT_print (IN char *title, IN PRIO_VECTOR_T *v)
159 {
160 Print ("%s:", title);
161 STP_VECT_br_id_print ("rootBr", &v->root_bridge, False);
162
163 /****
164 Print (" rpc=%ld ", (long) v->root_path_cost);
165 ****/
166
167 STP_VECT_br_id_print ("designBr", &v->design_bridge, False);
168
169 /****/
170 Print (" dp=%lx bp=%lx ",
171 (unsigned long) v->design_port,
172 (unsigned long) v->bridge_port);
173 /***********/
174 Print ("\n");
175 }
176 #endif
177