1
0
mirror of https://github.com/fumiama/WireGold.git synced 2026-06-20 18:30:25 +08:00

完善路由表

This commit is contained in:
fumiama
2021-10-25 21:35:51 +08:00
parent 51b557f06f
commit 7cb3ef6488
5 changed files with 30 additions and 8 deletions

View File

@@ -1,7 +1,7 @@
package head
// Notify 是 map[peerip]endpoint
type Notify map[string]string
type Notify = map[string]string
// Query 是 peerips 组成的数组
type Query []string
type Query = []string

View File

@@ -54,10 +54,10 @@ func listen() (conn *net.UDPConn, err error) {
}
case head.ProtoNotify:
logrus.Infoln("[link] recv notify")
onNotify(&packet)
p.onNotify(&packet)
case head.ProtoQuery:
logrus.Infoln("[link] recv query")
onQuery(&packet)
p.onQuery(&packet)
case head.ProtoData:
logrus.Infoln("[link] deliver to", p.peerip)
p.pipe <- &packet

View File

@@ -24,11 +24,22 @@ func (l *Link) keepAlive() {
}
// 收到询问包的处理函数
func onQuery(packet *head.Packet) {
func (l *Link) onQuery(packet *head.Packet) {
// 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注册
// 1. Data 解包
// ---- 使用 head.Notify 解释 packet.Data
// 2. endpoint注册
// ---- 遍历 Notify注册对方的 endpoint 到
// ---- connections注意使用读写锁connmapmu
}

View File

@@ -42,8 +42,9 @@ func AddPeer(peerip string, pubicKey *[32]byte, endPoint string, allowedIPs []st
l.allowedips = make([]*net.IPNet, len(allowedIPs))
for _, ipnet := range allowedIPs {
_, cidr, err := net.ParseCIDR(ipnet)
if err != nil {
if err == nil {
l.allowedips = append(l.allowedips, cidr)
routetable[cidr.String()] = append(routetable[cidr.String()], l)
}
}
}

View File

@@ -1,6 +1,14 @@
package link
import "net"
import (
"net"
"sync"
)
var (
routetable = make(map[string][]*Link)
routetablemu sync.RWMutex
)
// Accept 判断是否应当接受 ip 发来的包
func (l *Link) Accept(ip net.IP) bool {
@@ -19,5 +27,7 @@ func (l *Link) IsToMe(ip net.IP) bool {
// NextHop 得到前往 ip 的下一跳的 link
func (l *Link) NextHop(ip net.IP) *Link {
// TODO: 遍历 routetable得到正确的下一跳
// 注意使用 routetablemu 读写锁避免竞争
return l
}