google / google/gopacket

handle multiple default route cases

Open
#908 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Go
Stars
6.8k
Forks
1.2k
PR merge metrics
No merged PRs in 30d

Description

My machine has 2 default routes:

default via 192.10.10.1 dev eth0 proto dhcp metric 100 ===> ip of eth0 is 192.168.10.2
default via 192.168.56.1 dev eth1 proto dhcp metric 101 =====> ip of eth1 is 192.168.56.2
...

And my wrapper func is:
`
func GetLinkIPByRoute(dstIP string) string {

r,_ := routing.New()
_, _, preferSrcIP,_ := r.Route(net.ParseIP(dstIP))
return preferSrcIP.String(), nil
}
`

If I call GetLinkIPByRoute(1.2.3.4), it will give me the ip "192.168.56.2".

I read the source code, I think the bug is from(routing/routing.go):

`

137 if rt.Src == nil && rt.Dst == nil {
138 defaultGateway = rt
139 continue
140 }

`

Maybe here we should add a compare like:
`
if rt.Src == nil && rt.Dst == nil {
if defaultGateway == nil || defaultGateway.Priroty < rt.Priority {
defaultGateway = rt
}
continue
}
`

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.