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: |
| 23 | enum MessageType { |
| 24 | kMessageTypeUnknown, |
| 25 | kMessageTypeLink, |
| 26 | kMessageTypeAddress, |
| 27 | kMessageTypeRoute |
| 28 | }; |
| 29 | |
| 30 | enum MessageMode { |
| 31 | kMessageModeUnknown, |
| 32 | kMessageModeGet, |
| 33 | kMessageModeAdd, |
| 34 | kMessageModeDelete |
| 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 |
| 104 | RTNLMessage(MessageType type, |
| 105 | MessageMode mode, |
| 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 |
| 118 | MessageType type() { return type_; } |
| 119 | MessageMode mode() { return mode_; } |
| 120 | uint16 flags() { return flags_; } |
| 121 | uint32 seq() { return seq_; } |
| 122 | void set_seq(uint32 seq) { seq_ = seq; } |
| 123 | uint32 pid() { return pid_; } |
| 124 | uint32 interface_index() { return interface_index_; } |
| 125 | IPAddress::Family family() { return family_; } |
| 126 | |
| 127 | const LinkStatus &link_status() { return link_status_; } |
| 128 | void set_link_status(LinkStatus link_status) { link_status_ = link_status; } |
| 129 | const AddressStatus &address_status() { return address_status_; } |
| 130 | void set_address_status(AddressStatus address_status) { |
| 131 | address_status_ = address_status; |
| 132 | } |
| 133 | const RouteStatus &route_status() { return route_status_; } |
| 134 | void set_route_status(RouteStatus route_status) { |
| 135 | route_status_ = route_status; |
| 136 | } |
| 137 | // GLint hates "unsigned short", and I don't blame it, but that's the |
| 138 | // type that's used in the system headers. Use uint16 instead and hope |
| 139 | // that the conversion never ends up truncating on some strange platform. |
| 140 | bool HasAttribute(uint16 attr) { |
| 141 | return ContainsKey(attributes_, attr); |
| 142 | } |
| 143 | const ByteString GetAttribute(uint16 attr) { |
| 144 | return HasAttribute(attr) ? attributes_[attr] : ByteString(0); |
| 145 | } |
| 146 | void SetAttribute(uint16 attr, const ByteString &val) { |
| 147 | attributes_[attr] = val; |
| 148 | } |
| 149 | |
| 150 | private: |
| 151 | bool DecodeInternal(const ByteString &msg); |
| 152 | bool DecodeLink(const RTNLHeader *hdr, |
| 153 | MessageMode mode, |
| 154 | rtattr **attr_data, |
| 155 | int *attr_length); |
| 156 | bool DecodeAddress(const RTNLHeader *hdr, |
| 157 | MessageMode mode, |
| 158 | rtattr **attr_data, |
| 159 | int *attr_length); |
| 160 | bool DecodeRoute(const RTNLHeader *hdr, |
| 161 | MessageMode mode, |
| 162 | rtattr **attr_data, |
| 163 | int *attr_length); |
| 164 | void EncodeLink(RTNLHeader *hdr); |
| 165 | void EncodeAddress(RTNLHeader *hdr); |
| 166 | void EncodeRoute(RTNLHeader *hdr); |
| 167 | |
| 168 | MessageType type_; |
| 169 | MessageMode mode_; |
| 170 | uint16 flags_; |
| 171 | uint32 seq_; |
| 172 | uint32 pid_; |
| 173 | unsigned int interface_index_; |
| 174 | IPAddress::Family family_; |
| 175 | LinkStatus link_status_; |
| 176 | AddressStatus address_status_; |
| 177 | RouteStatus route_status_; |
| 178 | base::hash_map<uint16, ByteString> attributes_; |
| 179 | |
| 180 | DISALLOW_COPY_AND_ASSIGN(RTNLMessage); |
| 181 | }; |
| 182 | |
| 183 | } // namespace shill |
| 184 | |
| 185 | #endif // SHILL_RTNL_MESSAGE_ |