Skip to content

Commit

Permalink
修复了VRRP V2 把版本以及 消息格式错误时导致退出消息接收问题
Browse files Browse the repository at this point in the history
  • Loading branch information
Trisia committed Feb 1, 2024
1 parent 0b85c0c commit 8024cd3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
11 changes: 9 additions & 2 deletions virtual_router.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,15 @@ func (r *VirtualRouter) fetchVRRPDaemon() {
}
packet, err := r.vrrpConn.ReadMessage()
if err != nil {
logg.Printf("ERROR receive vrrp message: %v, fetch message will be stop", err)
return
// 由于网络原因,接收 VRRP Advertisement 消息失败,停止接收 VRRP Advertisement 消息
if _, ok := err.(NetErr); ok {
logg.Printf("ERROR receive vrrp message: %v, fetch message will be stop", err)
return
} else {
//logg.Printf("ERROR receive err format vrrp message: %v", err)
// 由于消息格式错误,忽略该消息
continue
}
}
//logg.Printf("VRID [%d] received VRRP packet: \n%s\n\n", r.vrID, packet.String())
if r.vrID != packet.GetVirtualRouterID() {
Expand Down
13 changes: 9 additions & 4 deletions vrrp_conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ import (
"net"
)

// NetErr 网络异常
type NetErr struct {
error
}

// NewIPv4VRRPMsgConn 创建的IPv4 VRRP虚拟连接
// ift: 工作网口
// src: IP数据包中源地址,应该为工作网口的IP地址
Expand Down Expand Up @@ -57,7 +62,7 @@ type IPv4VRRPMsgCon struct {
func (conn *IPv4VRRPMsgCon) WriteMessage(packet *VRRPPacket) error {
//cm := &ipv4.ControlMessage{TTL: 255, Src: conn.local, IfIndex: conn.itf.Index}
if _, err := conn.pc.WriteTo(packet.ToBytes(), nil, conn.remote); err != nil {
return fmt.Errorf("IPv4VRRPMsgCon.WriteMessage: %v", err)
return NetErr{fmt.Errorf("IPv4VRRPMsgCon.WriteMessage: %v", err)}
}
return nil
}
Expand All @@ -67,7 +72,7 @@ func (conn *IPv4VRRPMsgCon) ReadMessage() (*VRRPPacket, error) {
// 此处读取到的数据为 IP数据包
var n, cm, _, err = conn.pc.ReadFrom(conn.buffer)
if err != nil {
return nil, fmt.Errorf("IPv4VRRPMsgCon.ReadMessage: %v", err)
return nil, NetErr{fmt.Errorf("IPv4VRRPMsgCon.ReadMessage: %v", err)}
}
// 检查 TTL 应该为 255 (see RFC5798 5.1.1.3. TTL)
if cm.TTL != 255 {
Expand Down Expand Up @@ -152,7 +157,7 @@ type IPv6VRRPMsgCon struct {
func (con *IPv6VRRPMsgCon) WriteMessage(packet *VRRPPacket) error {
//cm := &ipv6.ControlMessage{TTL: 255, IfIndex: con.itf.Index}
if _, err := con.pc.WriteTo(packet.ToBytes(), nil, con.remote); err != nil {
return fmt.Errorf("IPv6VRRPMsgCon.WriteMessage: %v", err)
return NetErr{fmt.Errorf("IPv6VRRPMsgCon.WriteMessage: %v", err)}
}
return nil
}
Expand All @@ -161,7 +166,7 @@ func (con *IPv6VRRPMsgCon) WriteMessage(packet *VRRPPacket) error {
func (con *IPv6VRRPMsgCon) ReadMessage() (*VRRPPacket, error) {
n, cm, _, err := con.pc.ReadFrom(con.buffer)
if err != nil {
return nil, fmt.Errorf("IPv6VRRPMsgCon.ReadMessage: %v", err)
return nil, NetErr{fmt.Errorf("IPv6VRRPMsgCon.ReadMessage: %v", err)}
}
// 检查 TTL 应该为 255 (see RFC5798
if cm.HopLimit != 255 {
Expand Down

0 comments on commit 8024cd3

Please sign in to comment.