1
0
mirror of https://github.com/fumiama/WireGold.git synced 2026-06-09 02:02:41 +08:00

更新nexthop (#1)

* Add files via upload

* Delete router.go

* update nexthop

* Update router.go

Co-authored-by: 源文雨 <41315874+fumiama@users.noreply.github.com>
This commit is contained in:
butwedo
2021-12-30 16:20:58 +08:00
committed by GitHub
parent 66d5778df3
commit 7122614c1f

View File

@@ -40,11 +40,28 @@ func (r *Router) SetDefault(l *Link) {
}
// NextHop 得到前往 ip 的下一跳的 link
func (r *Router) NextHop(cidr string) *Link {
logrus.Infoln("[router] search for cidr", cidr)
func (r *Router) NextHop(ip string) *Link {
ipb := net.ParseIP(ip)
logrus.Infoln("[router] search for ip", ipb)
if ipb == nil {
return nil
}
// TODO: 遍历 r.table得到正确的下一跳
// 注意使用 r.mu 读写锁避免竞争
return r.table[cidr]
r.mu.RLock()
defer r.mu.RUnlock()
for c, l := range r.table {
_, cdr, err := net.ParseCIDR(c)
if err == nil {
if cdr.Contains(ipb) {
return l
}
}
}
return nil
}
// SetItem 添加一条表项