Skip to content

Commit a681ee5

Browse files
committed
tolerate routing failures
1 parent 7be4fc4 commit a681ee5

File tree

2 files changed

+26
-14
lines changed

2 files changed

+26
-14
lines changed

destinations.go

-1
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,6 @@ func (dest *Destination) Check() bool {
168168
route, err := GetRoute(ip)
169169
if err != nil {
170170
LogDestinationError(dest, fmt.Sprintf("Failed to route to %s", ip.String()), err)
171-
return false
172171
}
173172

174173
if dest.Protocol == "icmp" {

router.go

+26-13
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ type Route struct {
1818
}
1919

2020
func (r *Route) String() string {
21-
if r.SourceIP.String() == r.DestinationIP.String() || r.SourceIP.IsLoopback() {
21+
if r.SourceIP == nil && r.GatewayIP == nil {
22+
// If we failed to route through a gateway, log without source IP or outgoing iface.
23+
// (Assume IPv4!)
24+
return fmt.Sprintf("[%s][127.0.0.1 › %s]", r.SourceHostname, r.DestinationIP)
25+
} else if r.SourceIP.String() == r.DestinationIP.String() || r.SourceIP.IsLoopback() {
2226
// If the source and destination are the same, the route is trivial.
2327
return fmt.Sprintf("[%s][%s][%s]", r.SourceHostname, r.SourceInterfaceName, r.DestinationIP)
2428
} else {
@@ -32,22 +36,31 @@ func GetRoute(ip net.IP) (*Route, error) {
3236
return nil, err
3337
}
3438

35-
iface, gateway, source, err := r.Route(ip)
39+
hostname, err := os.Hostname()
3640
if err != nil {
37-
return nil, err
41+
hostname = ""
3842
}
3943

40-
hostname, err := os.Hostname()
44+
iface, gateway, source, err := r.Route(ip)
4145
if err != nil {
42-
hostname = ""
46+
// This is possibly a workaround until something like https://github.com/google/gopacket/pull/697 is released
47+
return &Route{
48+
SourceHostname: hostname,
49+
SourceInterfaceName: "",
50+
SourceHardwareAddress: nil,
51+
SourceIP: nil,
52+
GatewayIP: nil,
53+
DestinationIP: ip},
54+
err
55+
} else {
56+
return &Route{
57+
SourceHostname: hostname,
58+
SourceInterfaceName: iface.Name,
59+
SourceHardwareAddress: iface.HardwareAddr,
60+
SourceIP: source,
61+
GatewayIP: gateway,
62+
DestinationIP: ip},
63+
nil
4364
}
4465

45-
return &Route{
46-
SourceHostname: hostname,
47-
SourceInterfaceName: iface.Name,
48-
SourceHardwareAddress: iface.HardwareAddr,
49-
SourceIP: source,
50-
GatewayIP: gateway,
51-
DestinationIP: ip},
52-
nil
5366
}

0 commit comments

Comments
 (0)