@@ -18,7 +18,11 @@ type Route struct {
18
18
}
19
19
20
20
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 () {
22
26
// If the source and destination are the same, the route is trivial.
23
27
return fmt .Sprintf ("[%s][%s][%s]" , r .SourceHostname , r .SourceInterfaceName , r .DestinationIP )
24
28
} else {
@@ -32,22 +36,31 @@ func GetRoute(ip net.IP) (*Route, error) {
32
36
return nil , err
33
37
}
34
38
35
- iface , gateway , source , err := r . Route ( ip )
39
+ hostname , err := os . Hostname ( )
36
40
if err != nil {
37
- return nil , err
41
+ hostname = ""
38
42
}
39
43
40
- hostname , err := os . Hostname ( )
44
+ iface , gateway , source , err := r . Route ( ip )
41
45
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
43
64
}
44
65
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
53
66
}
0 commit comments