mirror of
https://github.com/fumiama/WireGold.git
synced 2026-06-20 18:30:25 +08:00
fix router
This commit is contained in:
@@ -7,7 +7,6 @@ import (
|
|||||||
"unsafe"
|
"unsafe"
|
||||||
|
|
||||||
blake2b "github.com/minio/blake2b-simd"
|
blake2b "github.com/minio/blake2b-simd"
|
||||||
"github.com/sirupsen/logrus"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Packet 是发送和接收的最小单位
|
// Packet 是发送和接收的最小单位
|
||||||
@@ -41,7 +40,7 @@ type Packet struct {
|
|||||||
|
|
||||||
// NewPacket 生成一个新包
|
// NewPacket 生成一个新包
|
||||||
func NewPacket(proto uint8, srcPort uint16, dst net.IP, dstPort uint16, data []byte) *Packet {
|
func NewPacket(proto uint8, srcPort uint16, dst net.IP, dstPort uint16, data []byte) *Packet {
|
||||||
logrus.Debugln("[packet] new: [proto:", proto, ", srcport:", srcPort, ", dstport:", dstPort, ", dst:", dst, ", data:", data)
|
// logrus.Debugln("[packet] new: [proto:", proto, ", srcport:", srcPort, ", dstport:", dstPort, ", dst:", dst, ", data:", data)
|
||||||
return &Packet{
|
return &Packet{
|
||||||
Proto: proto,
|
Proto: proto,
|
||||||
TTL: 16,
|
TTL: 16,
|
||||||
|
|||||||
@@ -137,17 +137,12 @@ func (l *Link) write(p *head.Packet, datasz uint32, offset uint16, istransfer, h
|
|||||||
return 0, errors.New("[link] ttl exceeded")
|
return 0, errors.New("[link] ttl exceeded")
|
||||||
}
|
}
|
||||||
if err == nil {
|
if err == nil {
|
||||||
peerlink := l.me.router.NextHop(p.Dst.String())
|
peerep := l.endpoint
|
||||||
if peerlink != nil {
|
if peerep == nil {
|
||||||
peerep := peerlink.endpoint
|
return 0, errors.New("[link] nil endpoint of " + p.Dst.String())
|
||||||
if peerep == nil {
|
|
||||||
return 0, errors.New("[link] nil endpoint of " + p.Dst.String())
|
|
||||||
}
|
|
||||||
logrus.Debugln("[link] write", len(d), "bytes data from ep", l.me.myconn.LocalAddr(), "to", peerep, "offset:", fmt.Sprintf("%04x", offset))
|
|
||||||
n, err = l.me.myconn.WriteToUDP(d, peerep)
|
|
||||||
} else {
|
|
||||||
logrus.Warnln("[link] drop packet: nil peerlink")
|
|
||||||
}
|
}
|
||||||
|
logrus.Debugln("[link] write", len(d), "bytes data from ep", l.me.myconn.LocalAddr(), "to", peerep, "offset:", fmt.Sprintf("%04x", offset))
|
||||||
|
n, err = l.me.myconn.WriteToUDP(d, peerep)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -80,11 +80,16 @@ func (m *Me) listen() (conn *net.UDPConn, err error) {
|
|||||||
} else if p.Accept(packet.Dst) {
|
} else if p.Accept(packet.Dst) {
|
||||||
if p.allowtrans {
|
if p.allowtrans {
|
||||||
// 转发
|
// 转发
|
||||||
n, err = p.Write(packet, true)
|
lnk := m.router.NextHop(packet.Dst.String())
|
||||||
if err == nil {
|
if lnk != nil {
|
||||||
logrus.Debugln("[link] trans", n, "bytes packet to", packet.Dst.String()+":"+strconv.Itoa(int(packet.DstPort)))
|
n, err = lnk.Write(packet, true)
|
||||||
|
if err == nil {
|
||||||
|
logrus.Debugln("[link] trans", n, "bytes packet to", packet.Dst.String()+":"+strconv.Itoa(int(packet.DstPort)))
|
||||||
|
} else {
|
||||||
|
logrus.Errorln("[link] trans packet to", packet.Dst.String()+":"+strconv.Itoa(int(packet.DstPort)), "err:", err)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
logrus.Errorln("[link] trans packet to", packet.Dst.String()+":"+strconv.Itoa(int(packet.DstPort)), "err:", err)
|
logrus.Warnln("[link] transfer drop packet: nil nexthop")
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
logrus.Warnln("[link] refused to trans packet to", packet.Dst.String()+":"+strconv.Itoa(int(packet.DstPort)))
|
logrus.Warnln("[link] refused to trans packet to", packet.Dst.String()+":"+strconv.Itoa(int(packet.DstPort)))
|
||||||
|
|||||||
@@ -118,7 +118,7 @@ func (m *Me) ListenFromNIC() {
|
|||||||
off = 0
|
off = 0
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Errorln("[lower] send read from nic err:", err)
|
logrus.Errorln("[me] send read from nic err:", err)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
if n == 0 {
|
if n == 0 {
|
||||||
@@ -130,7 +130,7 @@ func (m *Me) ListenFromNIC() {
|
|||||||
n, rem = m.send(m.nic, rem)
|
n, rem = m.send(m.nic, rem)
|
||||||
}
|
}
|
||||||
if len(rem) > 0 {
|
if len(rem) > 0 {
|
||||||
logrus.Debugln("[lower] remain", len(rem), "bytes to send")
|
logrus.Debugln("[me] remain", len(rem), "bytes to send")
|
||||||
if off > 0 {
|
if off > 0 {
|
||||||
off = copy(buf, rem)
|
off = copy(buf, rem)
|
||||||
isrev = true
|
isrev = true
|
||||||
@@ -149,14 +149,14 @@ func (m *Me) send(nc io.Reader, packet []byte) (n int, rem []byte) {
|
|||||||
n = int(binary.BigEndian.Uint16(packet[4:6])) + 40
|
n = int(binary.BigEndian.Uint16(packet[4:6])) + 40
|
||||||
if n > len(packet) {
|
if n > len(packet) {
|
||||||
rem = packet
|
rem = packet
|
||||||
logrus.Warnln("[lower] skip to send", len(packet), "bytes ipv6 packet head")
|
logrus.Warnln("[me] skip to send", len(packet), "bytes ipv6 packet head")
|
||||||
} else {
|
} else {
|
||||||
rem = packet[n:]
|
rem = packet[n:]
|
||||||
logrus.Warnln("[lower] skip to send", n, "bytes ipv6 packet")
|
logrus.Warnln("[me] skip to send", n, "bytes ipv6 packet")
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
logrus.Warnln("[lower] skip to send", len(packet), "bytes non-ipv4/v6 packet")
|
logrus.Warnln("[me] skip to send", len(packet), "bytes non-ipv4/v6 packet")
|
||||||
return len(packet), nil
|
return len(packet), nil
|
||||||
}
|
}
|
||||||
totl := waterutil.IPv4TotalLength(packet)
|
totl := waterutil.IPv4TotalLength(packet)
|
||||||
@@ -174,15 +174,15 @@ func (m *Me) send(nc io.Reader, packet []byte) (n int, rem []byte) {
|
|||||||
packet = packet[:totl]
|
packet = packet[:totl]
|
||||||
n = int(totl)
|
n = int(totl)
|
||||||
dst := waterutil.IPv4Destination(packet)
|
dst := waterutil.IPv4Destination(packet)
|
||||||
logrus.Debugln("[lower] sending", len(packet), "bytes packet from :"+strconv.Itoa(int(m.SrcPort())), "to", dst.String()+":"+strconv.Itoa(int(m.DstPort())))
|
logrus.Debugln("[me] sending", len(packet), "bytes packet from :"+strconv.Itoa(int(m.SrcPort())), "to", dst.String()+":"+strconv.Itoa(int(m.DstPort())))
|
||||||
lnk, err := m.Connect(dst.String())
|
lnk := m.router.NextHop(dst.String())
|
||||||
if err != nil {
|
if lnk == nil {
|
||||||
logrus.Warnln("[lower] connect to peer", dst.String(), "err:", err)
|
logrus.Warnln("[me] drop packet: nil nexthop")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
_, err = lnk.Write(head.NewPacket(head.ProtoData, m.SrcPort(), dst, m.DstPort(), packet), false)
|
_, err := lnk.Write(head.NewPacket(head.ProtoData, m.SrcPort(), dst, m.DstPort(), packet), false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Warnln("[lower] write to peer", dst.String(), "err:", err)
|
logrus.Warnln("[me] write to peer", dst.String(), "err:", err)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -49,15 +49,15 @@ func (m *Me) AddPeer(peerip string, pubicKey *[32]byte, endPoint string, allowed
|
|||||||
_, cidr, err := net.ParseCIDR(ipnet)
|
_, cidr, err := net.ParseCIDR(ipnet)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
l.allowedips = append(l.allowedips, cidr)
|
l.allowedips = append(l.allowedips, cidr)
|
||||||
|
l.me.router.SetItem(cidr, l)
|
||||||
|
l.me.connmapmu.Lock()
|
||||||
|
l.me.connections[peerip] = l
|
||||||
|
l.me.connmapmu.Unlock()
|
||||||
} else {
|
} else {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
l.me.router.SetItem(&net.IPNet{IP: l.peerip, Mask: net.IPMask(net.IPv4bcast)}, l)
|
|
||||||
l.me.connmapmu.Lock()
|
|
||||||
l.me.connections[peerip] = l
|
|
||||||
l.me.connmapmu.Unlock()
|
|
||||||
logrus.Infoln("[peer] add peer:", peerip, "allow:", allowedIPs)
|
logrus.Infoln("[peer] add peer:", peerip, "allow:", allowedIPs)
|
||||||
go l.keepAlive()
|
go l.keepAlive()
|
||||||
return
|
return
|
||||||
|
|||||||
Reference in New Issue
Block a user