blob: f626cd0fb774fb345c93a446002c3a1742ff8702 [file] [log] [blame]
Paul Stewartdd7df792011-07-15 11:09:50 -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_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
15struct rtattr;
16
17namespace shill {
18
19struct RTNLHeader;
20
21class RTNLMessage {
22 public:
Paul Stewart9a908082011-08-31 12:18:48 -070023 enum Type {
24 kTypeUnknown,
25 kTypeLink,
26 kTypeAddress,
27 kTypeRoute
Paul Stewartdd7df792011-07-15 11:09:50 -070028 };
29
Paul Stewart9a908082011-08-31 12:18:48 -070030 enum Mode {
31 kModeUnknown,
32 kModeGet,
33 kModeAdd,
34 kModeDelete
Paul Stewartdd7df792011-07-15 11:09:50 -070035 };
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 Stewart9a908082011-08-31 12:18:48 -0700104 RTNLMessage(Type type,
105 Mode mode,
Paul Stewartdd7df792011-07-15 11:09:50 -0700106 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 Stewart9a908082011-08-31 12:18:48 -0700118 Type type() const { return type_; }
119 Mode mode() const { return mode_; }
Chris Masone2aa97072011-08-09 17:35:08 -0700120 uint16 flags() const { return flags_; }
121 uint32 seq() const { return seq_; }
Paul Stewartdd7df792011-07-15 11:09:50 -0700122 void set_seq(uint32 seq) { seq_ = seq; }
Chris Masone2aa97072011-08-09 17:35:08 -0700123 uint32 pid() const { return pid_; }
124 uint32 interface_index() const { return interface_index_; }
125 IPAddress::Family family() const { return family_; }
Paul Stewartdd7df792011-07-15 11:09:50 -0700126
Chris Masone2aa97072011-08-09 17:35:08 -0700127 const LinkStatus &link_status() const { return link_status_; }
Paul Stewart9a908082011-08-31 12:18:48 -0700128 void set_link_status(const LinkStatus &link_status) {
129 link_status_ = link_status;
130 }
Chris Masone2aa97072011-08-09 17:35:08 -0700131 const AddressStatus &address_status() const { return address_status_; }
Paul Stewart9a908082011-08-31 12:18:48 -0700132 void set_address_status(const AddressStatus &address_status) {
Paul Stewartdd7df792011-07-15 11:09:50 -0700133 address_status_ = address_status;
134 }
Chris Masone2aa97072011-08-09 17:35:08 -0700135 const RouteStatus &route_status() const { return route_status_; }
Paul Stewart9a908082011-08-31 12:18:48 -0700136 void set_route_status(const RouteStatus &route_status) {
Paul Stewartdd7df792011-07-15 11:09:50 -0700137 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 Masone2aa97072011-08-09 17:35:08 -0700142 bool HasAttribute(uint16 attr) const {
Paul Stewartdd7df792011-07-15 11:09:50 -0700143 return ContainsKey(attributes_, attr);
144 }
Chris Masone2aa97072011-08-09 17:35:08 -0700145 const ByteString GetAttribute(uint16 attr) const {
146 return HasAttribute(attr) ?
147 attributes_.find(attr)->second : ByteString(0);
Paul Stewartdd7df792011-07-15 11:09:50 -0700148 }
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 Stewart9a908082011-08-31 12:18:48 -0700156 Mode mode,
Paul Stewartdd7df792011-07-15 11:09:50 -0700157 rtattr **attr_data,
158 int *attr_length);
159 bool DecodeAddress(const RTNLHeader *hdr,
Paul Stewart9a908082011-08-31 12:18:48 -0700160 Mode mode,
Paul Stewartdd7df792011-07-15 11:09:50 -0700161 rtattr **attr_data,
162 int *attr_length);
163 bool DecodeRoute(const RTNLHeader *hdr,
Paul Stewart9a908082011-08-31 12:18:48 -0700164 Mode mode,
Paul Stewartdd7df792011-07-15 11:09:50 -0700165 rtattr **attr_data,
166 int *attr_length);
167 void EncodeLink(RTNLHeader *hdr);
168 void EncodeAddress(RTNLHeader *hdr);
169 void EncodeRoute(RTNLHeader *hdr);
170
Paul Stewart9a908082011-08-31 12:18:48 -0700171 Type type_;
172 Mode mode_;
Paul Stewartdd7df792011-07-15 11:09:50 -0700173 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 Stefanoved2c28c2011-11-29 15:37:30 -0800181 base::hash_map<uint16, ByteString> attributes_; // NOLINT
182 // NOLINT above: hash_map from base, no need to #include <hash_map>
Paul Stewartdd7df792011-07-15 11:09:50 -0700183
184 DISALLOW_COPY_AND_ASSIGN(RTNLMessage);
185};
186
187} // namespace shill
188
189#endif // SHILL_RTNL_MESSAGE_