Paul Stewart | dd7df79 | 2011-07-15 11:09:50 -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_RTNL_MESSAGE_ |
| 6 | #define SHILL_RTNL_MESSAGE_ |
| 7 | |
| 8 | #include <base/basictypes.h> |
| 9 | #include <base/hash_tables.h> |
| 10 | #include <base/stl_util-inl.h> |
| 11 | |
| 12 | #include "shill/byte_string.h" |
| 13 | #include "shill/ip_address.h" |
| 14 | |
| 15 | struct rtattr; |
| 16 | |
| 17 | namespace shill { |
| 18 | |
| 19 | struct RTNLHeader; |
| 20 | |
| 21 | class RTNLMessage { |
| 22 | public: |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 23 | enum Type { |
| 24 | kTypeUnknown, |
| 25 | kTypeLink, |
| 26 | kTypeAddress, |
| 27 | kTypeRoute |
Paul Stewart | dd7df79 | 2011-07-15 11:09:50 -0700 | [diff] [blame] | 28 | }; |
| 29 | |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 30 | enum Mode { |
| 31 | kModeUnknown, |
| 32 | kModeGet, |
| 33 | kModeAdd, |
| 34 | kModeDelete |
Paul Stewart | dd7df79 | 2011-07-15 11:09:50 -0700 | [diff] [blame] | 35 | }; |
| 36 | |
| 37 | struct LinkStatus { |
| 38 | LinkStatus() |
| 39 | : type(0), |
| 40 | flags(0), |
| 41 | change(0) {} |
| 42 | LinkStatus(unsigned int in_type, |
| 43 | unsigned int in_flags, |
| 44 | unsigned int in_change) |
| 45 | : type(in_type), |
| 46 | flags(in_flags), |
| 47 | change(in_change) {} |
| 48 | unsigned int type; |
| 49 | unsigned int flags; |
| 50 | unsigned int change; |
| 51 | }; |
| 52 | |
| 53 | struct AddressStatus { |
| 54 | AddressStatus() |
| 55 | : prefix_len(0), |
| 56 | flags(0), |
| 57 | scope(0) {} |
| 58 | AddressStatus(unsigned char prefix_len_in, |
| 59 | unsigned char flags_in, |
| 60 | unsigned char scope_in) |
| 61 | : prefix_len(prefix_len_in), |
| 62 | flags(flags_in), |
| 63 | scope(scope_in) {} |
| 64 | unsigned char prefix_len; |
| 65 | unsigned char flags; |
| 66 | unsigned char scope; |
| 67 | }; |
| 68 | |
| 69 | struct RouteStatus { |
| 70 | RouteStatus() |
| 71 | : dst_prefix(0), |
| 72 | src_prefix(0), |
| 73 | table(0), |
| 74 | protocol(0), |
| 75 | scope(0), |
| 76 | type(0), |
| 77 | flags(0) {} |
| 78 | RouteStatus(unsigned char dst_prefix_in, |
| 79 | unsigned char src_prefix_in, |
| 80 | unsigned char table_in, |
| 81 | unsigned char protocol_in, |
| 82 | unsigned char scope_in, |
| 83 | unsigned char type_in, |
| 84 | unsigned char flags_in) |
| 85 | : dst_prefix(dst_prefix_in), |
| 86 | src_prefix(src_prefix_in), |
| 87 | table(table_in), |
| 88 | protocol(protocol_in), |
| 89 | scope(scope_in), |
| 90 | type(type_in), |
| 91 | flags(flags_in) {} |
| 92 | unsigned char dst_prefix; |
| 93 | unsigned char src_prefix; |
| 94 | unsigned char table; |
| 95 | unsigned char protocol; |
| 96 | unsigned char scope; |
| 97 | unsigned char type; |
| 98 | unsigned char flags; |
| 99 | }; |
| 100 | |
| 101 | // Empty constructor |
| 102 | RTNLMessage(); |
| 103 | // Build an RTNL message from arguments |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 104 | RTNLMessage(Type type, |
| 105 | Mode mode, |
Paul Stewart | dd7df79 | 2011-07-15 11:09:50 -0700 | [diff] [blame] | 106 | unsigned int flags, |
| 107 | uint32 seq, |
| 108 | uint32 pid, |
| 109 | int interface_index, |
| 110 | IPAddress::Family family); |
| 111 | |
| 112 | // Parse an RTNL message. Returns true on success. |
| 113 | bool Decode(const ByteString &data); |
| 114 | // Encode an RTNL message. Returns empty ByteString on failure. |
| 115 | ByteString Encode(); |
| 116 | |
| 117 | // Getters and setters |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 118 | Type type() const { return type_; } |
| 119 | Mode mode() const { return mode_; } |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 120 | uint16 flags() const { return flags_; } |
| 121 | uint32 seq() const { return seq_; } |
Paul Stewart | dd7df79 | 2011-07-15 11:09:50 -0700 | [diff] [blame] | 122 | void set_seq(uint32 seq) { seq_ = seq; } |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 123 | uint32 pid() const { return pid_; } |
| 124 | uint32 interface_index() const { return interface_index_; } |
| 125 | IPAddress::Family family() const { return family_; } |
Paul Stewart | dd7df79 | 2011-07-15 11:09:50 -0700 | [diff] [blame] | 126 | |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 127 | const LinkStatus &link_status() const { return link_status_; } |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 128 | void set_link_status(const LinkStatus &link_status) { |
| 129 | link_status_ = link_status; |
| 130 | } |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 131 | const AddressStatus &address_status() const { return address_status_; } |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 132 | void set_address_status(const AddressStatus &address_status) { |
Paul Stewart | dd7df79 | 2011-07-15 11:09:50 -0700 | [diff] [blame] | 133 | address_status_ = address_status; |
| 134 | } |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 135 | const RouteStatus &route_status() const { return route_status_; } |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 136 | void set_route_status(const RouteStatus &route_status) { |
Paul Stewart | dd7df79 | 2011-07-15 11:09:50 -0700 | [diff] [blame] | 137 | route_status_ = route_status; |
| 138 | } |
| 139 | // GLint hates "unsigned short", and I don't blame it, but that's the |
| 140 | // type that's used in the system headers. Use uint16 instead and hope |
| 141 | // that the conversion never ends up truncating on some strange platform. |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 142 | bool HasAttribute(uint16 attr) const { |
Paul Stewart | dd7df79 | 2011-07-15 11:09:50 -0700 | [diff] [blame] | 143 | return ContainsKey(attributes_, attr); |
| 144 | } |
Chris Masone | 2aa9707 | 2011-08-09 17:35:08 -0700 | [diff] [blame] | 145 | const ByteString GetAttribute(uint16 attr) const { |
| 146 | return HasAttribute(attr) ? |
| 147 | attributes_.find(attr)->second : ByteString(0); |
Paul Stewart | dd7df79 | 2011-07-15 11:09:50 -0700 | [diff] [blame] | 148 | } |
| 149 | void SetAttribute(uint16 attr, const ByteString &val) { |
| 150 | attributes_[attr] = val; |
| 151 | } |
| 152 | |
| 153 | private: |
| 154 | bool DecodeInternal(const ByteString &msg); |
| 155 | bool DecodeLink(const RTNLHeader *hdr, |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 156 | Mode mode, |
Paul Stewart | dd7df79 | 2011-07-15 11:09:50 -0700 | [diff] [blame] | 157 | rtattr **attr_data, |
| 158 | int *attr_length); |
| 159 | bool DecodeAddress(const RTNLHeader *hdr, |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 160 | Mode mode, |
Paul Stewart | dd7df79 | 2011-07-15 11:09:50 -0700 | [diff] [blame] | 161 | rtattr **attr_data, |
| 162 | int *attr_length); |
| 163 | bool DecodeRoute(const RTNLHeader *hdr, |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 164 | Mode mode, |
Paul Stewart | dd7df79 | 2011-07-15 11:09:50 -0700 | [diff] [blame] | 165 | rtattr **attr_data, |
| 166 | int *attr_length); |
| 167 | void EncodeLink(RTNLHeader *hdr); |
| 168 | void EncodeAddress(RTNLHeader *hdr); |
| 169 | void EncodeRoute(RTNLHeader *hdr); |
| 170 | |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 171 | Type type_; |
| 172 | Mode mode_; |
Paul Stewart | dd7df79 | 2011-07-15 11:09:50 -0700 | [diff] [blame] | 173 | uint16 flags_; |
| 174 | uint32 seq_; |
| 175 | uint32 pid_; |
| 176 | unsigned int interface_index_; |
| 177 | IPAddress::Family family_; |
| 178 | LinkStatus link_status_; |
| 179 | AddressStatus address_status_; |
| 180 | RouteStatus route_status_; |
Hristo Stefanov | ed2c28c | 2011-11-29 15:37:30 -0800 | [diff] [blame] | 181 | base::hash_map<uint16, ByteString> attributes_; // NOLINT |
| 182 | // NOLINT above: hash_map from base, no need to #include <hash_map> |
Paul Stewart | dd7df79 | 2011-07-15 11:09:50 -0700 | [diff] [blame] | 183 | |
| 184 | DISALLOW_COPY_AND_ASSIGN(RTNLMessage); |
| 185 | }; |
| 186 | |
| 187 | } // namespace shill |
| 188 | |
| 189 | #endif // SHILL_RTNL_MESSAGE_ |