mirror of
https://github.com/fumiama/WireGold.git
synced 2026-06-21 19:13:20 +08:00
完善路由表
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
package head
|
package head
|
||||||
|
|
||||||
// Notify 是 map[peerip]endpoint
|
// Notify 是 map[peerip]endpoint
|
||||||
type Notify map[string]string
|
type Notify = map[string]string
|
||||||
|
|
||||||
// Query 是 peerips 组成的数组
|
// Query 是 peerips 组成的数组
|
||||||
type Query []string
|
type Query = []string
|
||||||
|
|||||||
@@ -54,10 +54,10 @@ func listen() (conn *net.UDPConn, err error) {
|
|||||||
}
|
}
|
||||||
case head.ProtoNotify:
|
case head.ProtoNotify:
|
||||||
logrus.Infoln("[link] recv notify")
|
logrus.Infoln("[link] recv notify")
|
||||||
onNotify(&packet)
|
p.onNotify(&packet)
|
||||||
case head.ProtoQuery:
|
case head.ProtoQuery:
|
||||||
logrus.Infoln("[link] recv query")
|
logrus.Infoln("[link] recv query")
|
||||||
onQuery(&packet)
|
p.onQuery(&packet)
|
||||||
case head.ProtoData:
|
case head.ProtoData:
|
||||||
logrus.Infoln("[link] deliver to", p.peerip)
|
logrus.Infoln("[link] deliver to", p.peerip)
|
||||||
p.pipe <- &packet
|
p.pipe <- &packet
|
||||||
|
|||||||
@@ -24,11 +24,22 @@ func (l *Link) keepAlive() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 收到询问包的处理函数
|
// 收到询问包的处理函数
|
||||||
func onQuery(packet *head.Packet) {
|
func (l *Link) onQuery(packet *head.Packet) {
|
||||||
// TODO: 完成data解包与notify分发
|
// TODO: 完成data解包与notify分发
|
||||||
|
// 1. Data 解包
|
||||||
|
// ---- 使用 head.Query 解释 packet.Data
|
||||||
|
// ---- 根据 Query 确定需要封装的 Notify
|
||||||
|
// 2. notify分发
|
||||||
|
// ---- 封装 Notify 到 新的 packet.Data
|
||||||
|
// ---- 调用 l.Send 发送到对方
|
||||||
}
|
}
|
||||||
|
|
||||||
// 收到通告包的处理函数
|
// 收到通告包的处理函数
|
||||||
func onNotify(packet *head.Packet) {
|
func (l *Link) onNotify(packet *head.Packet) {
|
||||||
// TODO: 完成data解包与endpoint注册
|
// TODO: 完成data解包与endpoint注册
|
||||||
|
// 1. Data 解包
|
||||||
|
// ---- 使用 head.Notify 解释 packet.Data
|
||||||
|
// 2. endpoint注册
|
||||||
|
// ---- 遍历 Notify,注册对方的 endpoint 到
|
||||||
|
// ---- connections,注意使用读写锁connmapmu
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -42,8 +42,9 @@ func AddPeer(peerip string, pubicKey *[32]byte, endPoint string, allowedIPs []st
|
|||||||
l.allowedips = make([]*net.IPNet, len(allowedIPs))
|
l.allowedips = make([]*net.IPNet, len(allowedIPs))
|
||||||
for _, ipnet := range allowedIPs {
|
for _, ipnet := range allowedIPs {
|
||||||
_, 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)
|
||||||
|
routetable[cidr.String()] = append(routetable[cidr.String()], l)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,14 @@
|
|||||||
package link
|
package link
|
||||||
|
|
||||||
import "net"
|
import (
|
||||||
|
"net"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
routetable = make(map[string][]*Link)
|
||||||
|
routetablemu sync.RWMutex
|
||||||
|
)
|
||||||
|
|
||||||
// Accept 判断是否应当接受 ip 发来的包
|
// Accept 判断是否应当接受 ip 发来的包
|
||||||
func (l *Link) Accept(ip net.IP) bool {
|
func (l *Link) Accept(ip net.IP) bool {
|
||||||
@@ -19,5 +27,7 @@ func (l *Link) IsToMe(ip net.IP) bool {
|
|||||||
|
|
||||||
// NextHop 得到前往 ip 的下一跳的 link
|
// NextHop 得到前往 ip 的下一跳的 link
|
||||||
func (l *Link) NextHop(ip net.IP) *Link {
|
func (l *Link) NextHop(ip net.IP) *Link {
|
||||||
|
// TODO: 遍历 routetable,得到正确的下一跳
|
||||||
|
// 注意使用 routetablemu 读写锁避免竞争
|
||||||
return l
|
return l
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user