Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 1 | // 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 Stewart | 0d2ada3 | 2011-08-09 17:01:57 -0700 | [diff] [blame] | 12 | #include <base/lazy_instance.h> |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 13 | #include <base/memory/ref_counted.h> |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 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 | |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 20 | namespace shill { |
| 21 | |
| 22 | class RoutingTableEntry; |
| 23 | class 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). |
| 29 | class RoutingTable { |
| 30 | public: |
Paul Stewart | 0d2ada3 | 2011-08-09 17:01:57 -0700 | [diff] [blame] | 31 | virtual ~RoutingTable(); |
| 32 | |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 33 | static RoutingTable *GetInstance(); |
| 34 | |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 35 | virtual void Start(); |
| 36 | virtual void Stop(); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 37 | |
| 38 | // Add an entry to the routing table |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 39 | virtual bool AddRoute(int interface_index, const RoutingTableEntry &entry); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 40 | |
| 41 | // Get the default route associated with an interface of a given addr family |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 42 | virtual bool GetDefaultRoute(int interface_index, |
| 43 | IPAddress::Family family, |
| 44 | RoutingTableEntry *entry); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 45 | |
| 46 | // Set the default route for an interface, given an ipconfig entry |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 47 | virtual bool SetDefaultRoute(int interface_index, |
| 48 | const IPConfigRefPtr &ipconfig, |
| 49 | uint32 metric); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 50 | |
| 51 | // Remove all routes associated with interface |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 52 | virtual void FlushRoutes(int interface_index); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 53 | |
| 54 | // Reset local state for this interface |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 55 | virtual void ResetTable(int interface_index); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 56 | |
| 57 | // Set the metric (priority) on existing default routes for an interface |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 58 | virtual void SetDefaultMetric(int interface_index, uint32 metric); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 59 | |
Paul Stewart | 0d2ada3 | 2011-08-09 17:01:57 -0700 | [diff] [blame] | 60 | protected: |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 61 | RoutingTable(); |
Paul Stewart | 0d2ada3 | 2011-08-09 17:01:57 -0700 | [diff] [blame] | 62 | |
| 63 | private: |
| 64 | friend struct base::DefaultLazyInstanceTraits<RoutingTable>; |
| 65 | friend class RoutingTableTest; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 66 | |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 67 | void RouteMsgHandler(const RTNLMessage &msg); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 68 | bool ApplyRoute(uint32 interface_index, |
| 69 | const RoutingTableEntry &entry, |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 70 | RTNLMessage::Mode mode, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 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 Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 78 | scoped_ptr<Callback1<const RTNLMessage &>::Type> route_callback_; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 79 | scoped_ptr<RTNLListener> route_listener_; |
| 80 | |
| 81 | DISALLOW_COPY_AND_ASSIGN(RoutingTable); |
| 82 | }; |
| 83 | |
| 84 | } // namespace shill |
| 85 | |
| 86 | #endif // SHILL_ROUTING_TABLE_ |