-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
issue-1444: move netlink helpers to core/libs/netlink
- Loading branch information
1 parent
ba8f76a
commit 8b360b4
Showing
7 changed files
with
248 additions
and
179 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
#include <cloud/storage/core/libs/common/error.h> | ||
|
||
#include <netlink/genl/ctrl.h> | ||
#include <netlink/genl/genl.h> | ||
|
||
namespace NCloud::NNetlink { | ||
|
||
//////////////////////////////////////////////////////////////////////////////// | ||
|
||
class TNestedAttribute | ||
{ | ||
private: | ||
nl_msg* Message; | ||
nlattr* Attribute; | ||
|
||
public: | ||
TNestedAttribute(nl_msg* message, int attribute) | ||
: Message(message) | ||
{ | ||
Attribute = nla_nest_start(message, attribute); | ||
if (!Attribute) { | ||
throw TServiceError(E_FAIL) << "unable to nest attribute"; | ||
} | ||
} | ||
|
||
~TNestedAttribute() | ||
{ | ||
nla_nest_end(Message, Attribute); | ||
} | ||
}; | ||
|
||
//////////////////////////////////////////////////////////////////////////////// | ||
|
||
class TNetlinkMessage | ||
{ | ||
private: | ||
nl_msg* Message; | ||
|
||
public: | ||
TNetlinkMessage(int family, int command, int flags = 0, int version = 0) | ||
{ | ||
Message = nlmsg_alloc(); | ||
if (Message == nullptr) { | ||
throw TServiceError(E_FAIL) << "unable to allocate message"; | ||
} | ||
genlmsg_put( | ||
Message, | ||
NL_AUTO_PORT, | ||
NL_AUTO_SEQ, | ||
family, | ||
0, // hdrlen | ||
flags, | ||
command, | ||
version); | ||
} | ||
|
||
~TNetlinkMessage() | ||
{ | ||
nlmsg_free(Message); | ||
} | ||
|
||
operator nl_msg*() const | ||
{ | ||
return Message; | ||
} | ||
|
||
template <typename T> | ||
void Put(int attribute, T data) | ||
{ | ||
if (int err = nla_put(Message, attribute, sizeof(T), &data)) { | ||
throw TServiceError(E_FAIL) | ||
<< "unable to put attribute " << attribute << ": " | ||
<< nl_geterror(err); | ||
} | ||
} | ||
|
||
TNestedAttribute Nest(int attribute) | ||
{ | ||
return TNestedAttribute(Message, attribute); | ||
} | ||
}; | ||
} // namespace NCloud::NNetlink | ||
|
Oops, something went wrong.