@@ -54,7 +54,7 @@ func (router *osRouter) EnsureRouteIsAdded(route *Route) error {
54
54
}
55
55
56
56
func (router * osRouter ) Inspect (route * Route ) (exists bool , conflict string , overlaps []string , err error ) {
57
- cmd := exec .Command ("netstat " , "-nr" , "-f" , "inet " )
57
+ cmd := exec .Command ("ip " , "r " )
58
58
cmd .Env = append (cmd .Env , "LC_ALL=C" )
59
59
stdInAndOut , err := cmd .CombinedOutput ()
60
60
if err != nil {
@@ -70,37 +70,39 @@ func (router *osRouter) Inspect(route *Route) (exists bool, conflict string, ove
70
70
71
71
func (router * osRouter ) parseTable (table []byte ) routingTable {
72
72
t := routingTable {}
73
- skip := true
74
73
for _ , line := range strings .Split (string (table ), "\n " ) {
75
- //after first line of header we can start consuming
76
- if strings .HasPrefix (line , "Destination" ) {
77
- skip = false
78
- continue
79
- }
80
74
81
75
fields := strings .Fields (line )
82
- //don't care about the 0.0.0.0 routes
83
- if skip || len (fields ) == 0 || len (fields ) > 0 && (fields [0 ] == "default" || fields [0 ] == "0.0.0.0" ) {
76
+
77
+ //don't care about the routes that 0.0.0.0
78
+ if len (fields ) == 0 ||
79
+ len (fields ) > 0 && (fields [0 ] == "default" || fields [0 ] == "0.0.0.0" ) {
84
80
continue
85
81
}
82
+
86
83
if len (fields ) > 2 {
87
- dstCIDRIP := net .ParseIP (fields [0 ])
88
- dstCIDRMask := fields [2 ]
89
- dstMaskIP := net .ParseIP (dstCIDRMask )
90
- gatewayIP := net .ParseIP (fields [1 ])
91
84
92
- if dstCIDRIP == nil || gatewayIP == nil || dstMaskIP == nil {
93
- glog .V (8 ).Infof ("skipping line: can't parse: %s" , line )
94
- } else {
85
+ //assuming "10.96.0.0/12 via 192.168.39.47 dev virbr1"
86
+ dstCIDRString := fields [0 ]
87
+ gatewayIPString := fields [2 ]
88
+ gatewayIP := net .ParseIP (gatewayIPString )
95
89
96
- dstCIDR := & net.IPNet {
97
- IP : dstCIDRIP ,
98
- Mask : net .IPv4Mask (dstMaskIP [12 ], dstMaskIP [13 ], dstMaskIP [14 ], dstMaskIP [15 ]),
99
- }
90
+ //if not via format, then gateway is assumed to be 0.0.0.0
91
+ // "1.2.3.0/24 dev eno1 proto kernel scope link src 1.2.3.54 metric 100"
92
+ if fields [1 ] != "via" {
93
+ gatewayIP = net .ParseIP ("0.0.0.0" )
94
+ }
95
+
96
+ _ , ipNet , err := net .ParseCIDR (dstCIDRString )
97
+ if err != nil {
98
+ glog .V (4 ).Infof ("skipping line: can't parse CIDR from routing table: %s" , dstCIDRString )
99
+ } else if gatewayIP == nil {
100
+ glog .V (4 ).Infof ("skipping line: can't parse IP from routing table: %s" , gatewayIPString )
101
+ } else {
100
102
101
103
tableLine := routingTableLine {
102
104
route : & Route {
103
- DestCIDR : dstCIDR ,
105
+ DestCIDR : ipNet ,
104
106
Gateway : gatewayIP ,
105
107
},
106
108
line : line ,
0 commit comments