blob: ec141d8ece0182a6db881cdc156f57c399b50c80 [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>
Paul Stewart0d2ada32011-08-09 17:01:57 -070012#include <base/lazy_instance.h>
Paul Stewart75e89d22011-08-01 10:00:02 -070013#include <base/memory/ref_counted.h>
Paul Stewart75e89d22011-08-01 10:00:02 -070014#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
Paul Stewart75e89d22011-08-01 10:00:02 -070020namespace shill {
21
22class RoutingTableEntry;
23class RTNLListener;
24
25// This singleton maintains an in-process copy of the routing table on
26// a per-interface basis. It offers the ability for other modules to
27// make modifications to the routing table, centered around setting the
28// default route for an interface or modifying its metric (priority).
29class RoutingTable {
30 public:
Paul Stewart0d2ada32011-08-09 17:01:57 -070031 virtual ~RoutingTable();
32
Paul Stewart75e89d22011-08-01 10:00:02 -070033 static RoutingTable *GetInstance();
34
Paul Stewartdd60e452011-08-08 11:38:36 -070035 virtual void Start();
36 virtual void Stop();
Paul Stewart75e89d22011-08-01 10:00:02 -070037
38 // Add an entry to the routing table
Paul Stewartdd60e452011-08-08 11:38:36 -070039 virtual bool AddRoute(int interface_index, const RoutingTableEntry &entry);
Paul Stewart75e89d22011-08-01 10:00:02 -070040
41 // Get the default route associated with an interface of a given addr family
Paul Stewartdd60e452011-08-08 11:38:36 -070042 virtual bool GetDefaultRoute(int interface_index,
43 IPAddress::Family family,
44 RoutingTableEntry *entry);
Paul Stewart75e89d22011-08-01 10:00:02 -070045
46 // Set the default route for an interface, given an ipconfig entry
Paul Stewartdd60e452011-08-08 11:38:36 -070047 virtual bool SetDefaultRoute(int interface_index,
48 const IPConfigRefPtr &ipconfig,
49 uint32 metric);
Paul Stewart75e89d22011-08-01 10:00:02 -070050
51 // Remove all routes associated with interface
Paul Stewartdd60e452011-08-08 11:38:36 -070052 virtual void FlushRoutes(int interface_index);
Paul Stewart75e89d22011-08-01 10:00:02 -070053
54 // Reset local state for this interface
Paul Stewartdd60e452011-08-08 11:38:36 -070055 virtual void ResetTable(int interface_index);
Paul Stewart75e89d22011-08-01 10:00:02 -070056
57 // Set the metric (priority) on existing default routes for an interface
Paul Stewartdd60e452011-08-08 11:38:36 -070058 virtual void SetDefaultMetric(int interface_index, uint32 metric);
Paul Stewart75e89d22011-08-01 10:00:02 -070059
Paul Stewart0d2ada32011-08-09 17:01:57 -070060 protected:
Paul Stewart75e89d22011-08-01 10:00:02 -070061 RoutingTable();
Paul Stewart0d2ada32011-08-09 17:01:57 -070062
63 private:
64 friend struct base::DefaultLazyInstanceTraits<RoutingTable>;
65 friend class RoutingTableTest;
Paul Stewart75e89d22011-08-01 10:00:02 -070066
Chris Masone2aa97072011-08-09 17:35:08 -070067 void RouteMsgHandler(const RTNLMessage &msg);
Paul Stewart75e89d22011-08-01 10:00:02 -070068 bool ApplyRoute(uint32 interface_index,
69 const RoutingTableEntry &entry,
70 RTNLMessage::MessageMode mode,
71 unsigned int flags);
72 bool FlushCache();
73
74 static const char kRouteFlushPath4[];
75 static const char kRouteFlushPath6[];
76
77 base::hash_map<int, std::vector<RoutingTableEntry> > tables_;
Chris Masone2aa97072011-08-09 17:35:08 -070078 scoped_ptr<Callback1<const RTNLMessage &>::Type> route_callback_;
Paul Stewart75e89d22011-08-01 10:00:02 -070079 scoped_ptr<RTNLListener> route_listener_;
80
81 DISALLOW_COPY_AND_ASSIGN(RoutingTable);
82};
83
84} // namespace shill
85
86#endif // SHILL_ROUTING_TABLE_