summaryrefslogtreecommitdiff
path: root/usr/src/lib/librstp/common/vector.c
blob: a268b9231985a867aa99ff6f27b17aff0f98a2fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/************************************************************************ 
 * RSTP library - Rapid Spanning Tree (802.1t, 802.1w) 
 * Copyright (C) 2001-2003 Optical Access 
 * Author: Alex Rozin 
 * 
 * This file is part of RSTP library. 
 * 
 * RSTP library is free software; you can redistribute it and/or modify it 
 * under the terms of the GNU Lesser General Public License as published by the 
 * Free Software Foundation; version 2.1 
 * 
 * RSTP library is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser 
 * General Public License for more details. 
 * 
 * You should have received a copy of the GNU Lesser General Public License 
 * along with RSTP library; see the file COPYING.  If not, write to the Free 
 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 
 * 02111-1307, USA. 
 **********************************************************************/

/* STP priority vectors API : 17.4.2 */
 
#include "base.h"
#include "stp_bpdu.h"
#include "vector.h"
#include "stp_vectors.h"

int
STP_VECT_compare_bridge_id (BRIDGE_ID* b1, BRIDGE_ID* b2)
{
  if (b1->prio < b2->prio)
    return -1;

  if (b1->prio > b2->prio)
    return 1;
  return memcmp (b1->addr, b2->addr, 6);
}

void
STP_VECT_copy (OUT PRIO_VECTOR_T* t, IN PRIO_VECTOR_T* f)
{
  (void) memcpy (t, f, sizeof (PRIO_VECTOR_T));
}

void
STP_VECT_create (OUT PRIO_VECTOR_T* t,
                 IN BRIDGE_ID* root_br,
                 IN unsigned long root_path_cost,
                 IN BRIDGE_ID* design_bridge,
                 IN PORT_ID design_port,
                 IN PORT_ID bridge_port)
{
  (void) memcpy (&t->root_bridge, root_br, sizeof (BRIDGE_ID));
  t->root_path_cost = root_path_cost;
  (void) memcpy (&t->design_bridge, design_bridge, sizeof (BRIDGE_ID));
  t->design_port = design_port;
  t->bridge_port = bridge_port;
}

int
STP_VECT_compare_vector (PRIO_VECTOR_T* v1, PRIO_VECTOR_T* v2)
{
  int bridcmp;

  bridcmp = STP_VECT_compare_bridge_id (&v1->root_bridge, &v2->root_bridge);
  if (bridcmp < 0) return bridcmp;

  if (! bridcmp) {
    bridcmp = v1->root_path_cost - v2->root_path_cost;
    if (bridcmp < 0) return bridcmp;
    if (! bridcmp) {
      bridcmp = STP_VECT_compare_bridge_id (&v1->design_bridge, &v2->design_bridge);
      if (bridcmp < 0) return bridcmp;
      if (! bridcmp) {
        bridcmp = v1->design_port - v2->design_port;
        if (bridcmp < 0) return bridcmp;
        if (! bridcmp)
          return v1->bridge_port - v2->bridge_port;
      }
    }
  }

  return bridcmp;
}

static unsigned short
stp_vect_get_short (IN unsigned char* f)
{
  /* LINTED: alignment */
  return ntohs (*(unsigned short *)f);
}

static void
stp_vect_set_short (IN unsigned short f, OUT unsigned char* t)
{
  /* LINTED: alignment */
  *(unsigned short *)t = htons (f);
}

static void
stp_vect_get_bridge_id (IN unsigned char* c_br, OUT BRIDGE_ID* bridge_id)
{
  bridge_id->prio = stp_vect_get_short (c_br);
  (void) memcpy (bridge_id->addr, c_br + 2, 6);
}

static void
stp_vect_set_bridge_id (IN BRIDGE_ID* bridge_id, OUT unsigned char* c_br)
{
  stp_vect_set_short (bridge_id->prio, c_br);
  (void) memcpy (c_br + 2, bridge_id->addr, 6);
}

void
STP_VECT_get_vector (IN BPDU_BODY_T* b, OUT PRIO_VECTOR_T* v)
{
  stp_vect_get_bridge_id (b->root_id, &v->root_bridge);

  /* LINTED: alignment */
  v->root_path_cost = ntohl (*((long*) b->root_path_cost));

  stp_vect_get_bridge_id (b->bridge_id, &v->design_bridge);

  v->design_port = stp_vect_get_short (b->port_id);
}

void
STP_VECT_set_vector (IN PRIO_VECTOR_T* v, OUT BPDU_BODY_T* b)
{
  unsigned long root_path_cost;

  stp_vect_set_bridge_id (&v->root_bridge, b->root_id);

  root_path_cost = htonl (v->root_path_cost);
  (void) memcpy (b->root_path_cost, &root_path_cost, 4);

  stp_vect_set_bridge_id (&v->design_bridge, b->bridge_id);

  stp_vect_set_short (v->design_port, b->port_id);
}

#ifdef STP_DBG

/*ARGSUSED*/
void
STP_VECT_br_id_print (IN char *title, IN BRIDGE_ID* br_id, IN Bool cr)
{
  stp_trace ("%s=%04lX-%02x%02x%02x%02x%02x%02x",
            title,
          (unsigned long) br_id->prio,
          (unsigned char) br_id->addr[0],
          (unsigned char) br_id->addr[1],
          (unsigned char) br_id->addr[2],
          (unsigned char) br_id->addr[3],
          (unsigned char) br_id->addr[4],
          (unsigned char) br_id->addr[5]);
#ifndef __SUN__
  stp_trace (cr ? "\n" : " ");
#endif
}

void
STP_VECT_print (IN char *title, IN PRIO_VECTOR_T *v)
{
  stp_trace ("%s:", title);
  STP_VECT_br_id_print ("rootBr", &v->root_bridge, False);
    
/****
  stp_trace (" rpc=%ld ", (long) v->root_path_cost);
****/

  STP_VECT_br_id_print ("designBr", &v->design_bridge, False);

/****/
  stp_trace (" dp=%lx bp=%lx ",
          (unsigned long) v->design_port,
          (unsigned long) v->bridge_port);
/***********/
  stp_trace ("\n");
}
#endif