diff --git a/gold/head/packet.go b/gold/head/packet.go index 2842cc6..5217603 100644 --- a/gold/head/packet.go +++ b/gold/head/packet.go @@ -7,7 +7,6 @@ import ( "unsafe" blake2b "github.com/minio/blake2b-simd" - "github.com/sirupsen/logrus" ) // Packet 是发送和接收的最小单位 @@ -41,7 +40,7 @@ type Packet struct { // NewPacket 生成一个新包 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{ Proto: proto, TTL: 16, diff --git a/gold/link/link.go b/gold/link/link.go index 1fd3659..4a45d98 100644 --- a/gold/link/link.go +++ b/gold/link/link.go @@ -137,17 +137,12 @@ func (l *Link) write(p *head.Packet, datasz uint32, offset uint16, istransfer, h return 0, errors.New("[link] ttl exceeded") } if err == nil { - peerlink := l.me.router.NextHop(p.Dst.String()) - if peerlink != nil { - peerep := peerlink.endpoint - 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") + peerep := l.endpoint + 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) } return } diff --git a/gold/link/listen.go b/gold/link/listen.go index 088ac26..d7f7923 100644 --- a/gold/link/listen.go +++ b/gold/link/listen.go @@ -80,11 +80,16 @@ func (m *Me) listen() (conn *net.UDPConn, err error) { } else if p.Accept(packet.Dst) { if p.allowtrans { // 转发 - n, err = p.Write(packet, true) - if err == nil { - logrus.Debugln("[link] trans", n, "bytes packet to", packet.Dst.String()+":"+strconv.Itoa(int(packet.DstPort))) + lnk := m.router.NextHop(packet.Dst.String()) + if lnk != nil { + 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 { - 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 { logrus.Warnln("[link] refused to trans packet to", packet.Dst.String()+":"+strconv.Itoa(int(packet.DstPort))) diff --git a/gold/link/me.go b/gold/link/me.go index da3e204..2a34393 100644 --- a/gold/link/me.go +++ b/gold/link/me.go @@ -118,7 +118,7 @@ func (m *Me) ListenFromNIC() { off = 0 } if err != nil { - logrus.Errorln("[lower] send read from nic err:", err) + logrus.Errorln("[me] send read from nic err:", err) break } if n == 0 { @@ -130,7 +130,7 @@ func (m *Me) ListenFromNIC() { n, rem = m.send(m.nic, rem) } 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 { off = copy(buf, rem) 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 if n > len(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 { rem = packet[n:] - logrus.Warnln("[lower] skip to send", n, "bytes ipv6 packet") + logrus.Warnln("[me] skip to send", n, "bytes ipv6 packet") } 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 } totl := waterutil.IPv4TotalLength(packet) @@ -174,15 +174,15 @@ func (m *Me) send(nc io.Reader, packet []byte) (n int, rem []byte) { packet = packet[:totl] n = int(totl) 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()))) - lnk, err := m.Connect(dst.String()) - if err != nil { - logrus.Warnln("[lower] connect to peer", dst.String(), "err:", err) + logrus.Debugln("[me] sending", len(packet), "bytes packet from :"+strconv.Itoa(int(m.SrcPort())), "to", dst.String()+":"+strconv.Itoa(int(m.DstPort()))) + lnk := m.router.NextHop(dst.String()) + if lnk == nil { + logrus.Warnln("[me] drop packet: nil nexthop") 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 { - logrus.Warnln("[lower] write to peer", dst.String(), "err:", err) + logrus.Warnln("[me] write to peer", dst.String(), "err:", err) } return } diff --git a/gold/link/peer.go b/gold/link/peer.go index 0057052..17c49b1 100644 --- a/gold/link/peer.go +++ b/gold/link/peer.go @@ -49,15 +49,15 @@ func (m *Me) AddPeer(peerip string, pubicKey *[32]byte, endPoint string, allowed _, cidr, err := net.ParseCIDR(ipnet) if err == nil { 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 { 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) go l.keepAlive() return