blob: f75425015830637f5061478d09ef9ea457401c97 [file] [log] [blame]
Paul Stewart75e89d22011-08-01 10:00:02 -07001// Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef SHILL_ROUTING_TABLE_
6#define SHILL_ROUTING_TABLE_
7
8#include <string>
9
10#include <base/callback_old.h>
11#include <base/hash_tables.h>
12#include <base/memory/ref_counted.h>
13#include <base/memory/singleton.h>
14#include <base/memory/scoped_ptr.h>
15
16#include "shill/ip_address.h"
17#include "shill/refptr_types.h"
18#include "shill/rtnl_message.h"
19
20struct nlmsghdr;
21
22namespace shill {
23
24class RoutingTableEntry;
25class RTNLListener;
26
27// This singleton maintains an in-process copy of the routing table on
28// a per-interface basis. It offers the ability for other modules to
29// make modifications to the routing table, centered around setting the
30// default route for an interface or modifying its metric (priority).
31class RoutingTable {
32 public:
33 // Since this is a singleton, use RoutingTable::GetInstance()->Foo()
34 static RoutingTable *GetInstance();
35
36 void Start();
37 void Stop();
38
39 // Add an entry to the routing table
40 bool AddRoute(int interface_index, const RoutingTableEntry &entry);
41
42 // Get the default route associated with an interface of a given addr family
43 bool GetDefaultRoute(int interface_index,
44 IPAddress::Family family,
45 RoutingTableEntry *entry);
46
47 // Set the default route for an interface, given an ipconfig entry
48 bool SetDefaultRoute(int interface_index,
49 const IPConfigRefPtr &ipconfig,
50 uint32 metric);
51
52 // Remove all routes associated with interface
53 void FlushRoutes(int interface_index);
54
55 // Reset local state for this interface
56 void ResetTable(int interface_index);
57
58 // Set the metric (priority) on existing default routes for an interface
59 void SetDefaultMetric(int interface_index, uint32 metric);
60
61 private:
62 friend struct DefaultSingletonTraits<RoutingTable>;
63 friend class RoutingTableTest;
64
65 // Constructor and destructor are private since this is a singleton
66 RoutingTable();
67 ~RoutingTable();
68
69 void RouteMsgHandler(struct nlmsghdr *hdr);
70 bool ApplyRoute(uint32 interface_index,
71 const RoutingTableEntry &entry,
72 RTNLMessage::MessageMode mode,
73 unsigned int flags);
74 bool FlushCache();
75
76 static const char kRouteFlushPath4[];
77 static const char kRouteFlushPath6[];
78
79 base::hash_map<int, std::vector<RoutingTableEntry> > tables_;
80 scoped_ptr<Callback1<struct nlmsghdr *>::Type> route_callback_;
81 scoped_ptr<RTNLListener> route_listener_;
82
83 DISALLOW_COPY_AND_ASSIGN(RoutingTable);
84};
85
86} // namespace shill
87
88#endif // SHILL_ROUTING_TABLE_