blob: cf31f1c6b4ecd21b2a0447b83c24c3624fc2a475 [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>
Hristo Stefanoved2c28c2011-11-29 15:37:30 -08009#include <vector>
Paul Stewart75e89d22011-08-01 10:00:02 -070010
11#include <base/callback_old.h>
12#include <base/hash_tables.h>
Paul Stewart0d2ada32011-08-09 17:01:57 -070013#include <base/lazy_instance.h>
Paul Stewart75e89d22011-08-01 10:00:02 -070014#include <base/memory/ref_counted.h>
Paul Stewart75e89d22011-08-01 10:00:02 -070015#include <base/memory/scoped_ptr.h>
16
17#include "shill/ip_address.h"
18#include "shill/refptr_types.h"
19#include "shill/rtnl_message.h"
20
Paul Stewart75e89d22011-08-01 10:00:02 -070021namespace shill {
22
23class RoutingTableEntry;
24class RTNLListener;
25
26// This singleton maintains an in-process copy of the routing table on
27// a per-interface basis. It offers the ability for other modules to
28// make modifications to the routing table, centered around setting the
29// default route for an interface or modifying its metric (priority).
30class RoutingTable {
31 public:
Paul Stewart0d2ada32011-08-09 17:01:57 -070032 virtual ~RoutingTable();
33
Paul Stewart75e89d22011-08-01 10:00:02 -070034 static RoutingTable *GetInstance();
35
Paul Stewartdd60e452011-08-08 11:38:36 -070036 virtual void Start();
37 virtual void Stop();
Paul Stewart75e89d22011-08-01 10:00:02 -070038
Paul Stewartc8f4bef2011-12-13 09:45:51 -080039 // Add an entry to the routing table.
Paul Stewartdd60e452011-08-08 11:38:36 -070040 virtual bool AddRoute(int interface_index, const RoutingTableEntry &entry);
Paul Stewart75e89d22011-08-01 10:00:02 -070041
Paul Stewartc8f4bef2011-12-13 09:45:51 -080042 // Get the default route associated with an interface of a given addr family.
Paul Stewartdd60e452011-08-08 11:38:36 -070043 virtual bool GetDefaultRoute(int interface_index,
44 IPAddress::Family family,
45 RoutingTableEntry *entry);
Paul Stewart75e89d22011-08-01 10:00:02 -070046
47 // Set the default route for an interface, given an ipconfig entry
Paul Stewartdd60e452011-08-08 11:38:36 -070048 virtual bool SetDefaultRoute(int interface_index,
49 const IPConfigRefPtr &ipconfig,
50 uint32 metric);
Paul Stewart75e89d22011-08-01 10:00:02 -070051
Paul Stewartc8f4bef2011-12-13 09:45:51 -080052 // Remove all routes associated with interface.
Paul Stewartdd60e452011-08-08 11:38:36 -070053 virtual void FlushRoutes(int interface_index);
Paul Stewart75e89d22011-08-01 10:00:02 -070054
Paul Stewartc8f4bef2011-12-13 09:45:51 -080055 // Flush the routing cache for all interfaces.
56 virtual bool FlushCache();
57
58 // Reset local state for this interface.
Paul Stewartdd60e452011-08-08 11:38:36 -070059 virtual void ResetTable(int interface_index);
Paul Stewart75e89d22011-08-01 10:00:02 -070060
Paul Stewartc8f4bef2011-12-13 09:45:51 -080061 // Set the metric (priority) on existing default routes for an interface.
Paul Stewartdd60e452011-08-08 11:38:36 -070062 virtual void SetDefaultMetric(int interface_index, uint32 metric);
Paul Stewart75e89d22011-08-01 10:00:02 -070063
Paul Stewart0d2ada32011-08-09 17:01:57 -070064 protected:
Paul Stewart75e89d22011-08-01 10:00:02 -070065 RoutingTable();
Paul Stewart0d2ada32011-08-09 17:01:57 -070066
67 private:
68 friend struct base::DefaultLazyInstanceTraits<RoutingTable>;
69 friend class RoutingTableTest;
Paul Stewart75e89d22011-08-01 10:00:02 -070070
Chris Masone2aa97072011-08-09 17:35:08 -070071 void RouteMsgHandler(const RTNLMessage &msg);
Paul Stewart75e89d22011-08-01 10:00:02 -070072 bool ApplyRoute(uint32 interface_index,
73 const RoutingTableEntry &entry,
Paul Stewart9a908082011-08-31 12:18:48 -070074 RTNLMessage::Mode mode,
Paul Stewart75e89d22011-08-01 10:00:02 -070075 unsigned int flags);
Paul Stewartc1dec4d2011-12-08 15:25:28 -080076 void ReplaceMetric(uint32 interface_index,
77 const RoutingTableEntry &entry,
78 uint32 metric);
Paul Stewart75e89d22011-08-01 10:00:02 -070079
80 static const char kRouteFlushPath4[];
81 static const char kRouteFlushPath6[];
82
Hristo Stefanoved2c28c2011-11-29 15:37:30 -080083 base::hash_map<int, std::vector<RoutingTableEntry> > tables_; // NOLINT
Paul Stewartc8f4bef2011-12-13 09:45:51 -080084 // NOLINT above: hash_map from base, no need to #include <hash_map>.
Hristo Stefanoved2c28c2011-11-29 15:37:30 -080085
Chris Masone2aa97072011-08-09 17:35:08 -070086 scoped_ptr<Callback1<const RTNLMessage &>::Type> route_callback_;
Paul Stewart75e89d22011-08-01 10:00:02 -070087 scoped_ptr<RTNLListener> route_listener_;
88
89 DISALLOW_COPY_AND_ASSIGN(RoutingTable);
90};
91
92} // namespace shill
93
94#endif // SHILL_ROUTING_TABLE_