📄 vector.c
字号:
/************************************************************************
* 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
* port priority vector = {RootBrigeID:RootPathCost;DesignatedBridgeID:
DesignatedPortID:BridgePortID}
*/
#include "base.h"
#include "stp_bpdu.h"
#include "vector.h"
#include "generic.h"
#include "big_endian.h"
extern int cl_serv_console_fd;
extern int mn_fd_printf(int fd,const char *format, ...);
/**********************************************************************
* 函数名称: STP_VECT_compare_bridge_id
* 功能描述: 比较两个网桥的bridge_id
* 输入参数:
* 输出参数:
* 返回值 : -1 --b1优于b2
0 --相等
1 --b1劣于b2
* 备注:
* 作者:
* 日期:
**********************************************************************/
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);
}
/**********************************************************************
* function: STP_VECT_copy
* purpose:将一个网桥的PRIO_VECTOR_T向量拷贝到另一个
* input: f
* output: t
* return :
* remark:
* author:
* date:
**********************************************************************/
void STP_VECT_copy (OUT PRIO_VECTOR_T * t, IN PRIO_VECTOR_T * f)
{
memcpy (t, f, sizeof (PRIO_VECTOR_T));
}
/**********************************************************************
* 函数名称: STP_VECT_create
* 功能描述: 填充一个网桥的PRIO_VECTOR_T向量
* 输入参数: root_br,root_path_cost,design_bridge,design_port,bridge_port
* 输出参数: t
* 返回值 :
* 备注:
* 作者:
* 日期:
**********************************************************************/
void STP_VECT_create (OUT PRIO_VECTOR_T * to_vector, IN BRIDGE_ID * root_br,
IN unsigned long root_path_cost, IN BRIDGE_ID * design_bridge,
IN unsigned short design_port, IN unsigned short bridge_port)
{
memcpy (&to_vector->root_bridge, root_br, sizeof (BRIDGE_ID));
to_vector->root_path_cost = root_path_cost;
memcpy (&to_vector->design_bridge, design_bridge, sizeof (BRIDGE_ID));
to_vector->design_port = design_port;
to_vector->bridge_port = bridge_port;
}
/**********************************************************************
* 函数名称: STP_VECT_compare_vector
* 功能描述:比较两个网桥的RSTP向量
* 输入参数: v1,v2
* 输出参数:
* 返回值 :
* 备注:
* 作者:
* 日期:
**********************************************************************/
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;
}
else /*进行第二个元素的比较 */
{
bridcmp = v1->root_path_cost - v2->root_path_cost;
if (bridcmp != 0)
{
return bridcmp;
}
else /*进行第三个元素的比较 */
{
bridcmp =
STP_VECT_compare_bridge_id (&v1->design_bridge,
&v2->design_bridge);
if (bridcmp != 0)
{
return bridcmp;
}
else /*进行第四个元素的比较 */
{
bridcmp = v1->design_port - v2->design_port;
if (bridcmp < 0)
{
return bridcmp;
}
else /*进行第五个元素的比较 */
{
return v1->bridge_port - v2->bridge_port;
}
}
}
}
}
/*added by wl for debug 2005-7-22*/
#if defined(__KERNEL__) || (defined (__GLIBC__) && __GLIBC__ >= 2)
#define htonl(x) ___htonl(x)
#define ntohl(x) ___ntohl(x)
#else
#define htonl(x) ((unsigned long)___htonl(x))
#define ntohl(x) ((unsigned long)___ntohl(x))
#endif
#undef htons(x)
#define htons(x) ___htons(x)
#undef ntohs(x)
#define ntohs(x) ___ntohs(x)
#define ___htonl(x) __cpu_to_be32(x)
#define ___htons(x) __cpu_to_be16(x)
#define ___ntohl(x) __be32_to_cpu(x)
#define ___ntohs(x) __be16_to_cpu(x)
/**********************************************************************
* 函数名称: stp_vect_get_short
* 功能描述: 将f由网络顺序转为主机顺序
* 输入参数: f
* 输出参数:
* 返回值 :
* 备注:
* 作者:
* 日期:
**********************************************************************/
static unsigned short stp_vect_get_short (IN unsigned char *f)
{
return ntohs (*(unsigned short *) f);
}
/**********************************************************************
* 函数名称: stp_vect_set_short
* 功能描述: 将from由主机顺序转为网络顺序to
* 输入参数: from
* 输出参数: to
* 返回值 :
* 备注:
* 作者:
* 日期:
**********************************************************************/
static void stp_vect_set_short (IN unsigned short from, OUT unsigned char *to)
{
*(unsigned short *) to = htons (from);
}
/**********************************************************************
* function: stp_vect_get_bridge_id
* purpose:由输入的c_br构建bridge_id
* input: c_br
* output: bridge_id
* return :
* remark:
* author:
* date:
**********************************************************************/
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);
memcpy (bridge_id->addr, c_br + 2, 6);
}
/**********************************************************************
* 函数名称: stp_vect_set_bridge_id
* 功能描述:将bridge_id写到c_br中
* 输入参数: bridge_id
* 输出参数: c_br
* 返回值 : N/A
* 备注:
* 作者:
* 日期:
**********************************************************************/
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);
memcpy (c_br + 2, bridge_id->addr, 6);
}
/**********************************************************************
* function: STP_VECT_get_vector
* purpose:从BPDU包中得到向量
* input: b
* output: v
* return :
* remark:
* author:
* date:
**********************************************************************/
void STP_VECT_get_vector (IN BPDU_BODY_T * b, OUT PRIO_VECTOR_T * v)
{
/*
* 从BPDU包中得到向量的前四个元素。第五个元素为
* 接收端口自身
*/
stp_vect_get_bridge_id (b->root_id, &v->root_bridge);
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);
}
/**********************************************************************
* function: STP_VECT_set_Vector
* purpose:根据向量内容写BPDU包
* input: v
* output: b
* return :
* remark:
* author:
* date:
**********************************************************************/
void STP_VECT_set_vector (IN PRIO_VECTOR_T * vector, OUT BPDU_BODY_T * bpdu_body)
{
unsigned long root_path_cost;
stp_vect_set_bridge_id (&vector->root_bridge, bpdu_body->root_id);
root_path_cost = htonl (vector->root_path_cost);
memcpy (bpdu_body->root_path_cost, &root_path_cost, 4);
stp_vect_set_bridge_id (&vector->design_bridge, bpdu_body->bridge_id);
stp_vect_set_short (vector->design_port, bpdu_body->port_id);
}
void STP_VECT_set_vector_new (IN PRIO_VECTOR_T * vector,unsigned long cost, OUT BPDU_BODY_T * bpdu_body)
{
unsigned long root_path_cost;
stp_vect_set_bridge_id (&vector->root_bridge, bpdu_body->root_id);
/*root_path_cost = htonl (vector->root_path_cost);*/
root_path_cost = htonl(cost);
memcpy (bpdu_body->root_path_cost, &root_path_cost, 4);
stp_vect_set_bridge_id (&vector->design_bridge, bpdu_body->bridge_id);
stp_vect_set_short (vector->design_port, bpdu_body->port_id);
}
#ifdef STP_DBG
void STP_VECT_br_id_print (IN char *title, IN BRIDGE_ID * br_id, IN Bool cr)
{
Print ("%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]);
Print (cr ? "\n" : " ");
}
void STP_VECT_print (IN char *title, IN PRIO_VECTOR_T * v)
{
Print ("%s:", title);
STP_VECT_br_id_print ("rootBr", &v->root_bridge, False);
/****
Print (" rpc=%ld ", (long) v->root_path_cost);
****/
STP_VECT_br_id_print ("designBr", &v->design_bridge, False);
/****/
Print (" dp=%lx bp=%lx ", (unsigned long) v->design_port,
(unsigned long) v->bridge_port);
/***********/
Print ("\n");
}
int stp_trace(const char* fmt,...)
{
mn_fd_printf(cl_serv_console_fd,const char *format, ...);
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -