Netlink.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * Copyright (C) 2017 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <ios>
  17. #include <linux/netlink.h>
  18. #include "netdutils/Math.h"
  19. #include "netdutils/Netlink.h"
  20. namespace android {
  21. namespace netdutils {
  22. void forEachNetlinkMessage(const Slice buf,
  23. const std::function<void(const nlmsghdr&, const Slice)>& onMsg) {
  24. Slice tail = buf;
  25. while (tail.size() >= sizeof(nlmsghdr)) {
  26. nlmsghdr hdr = {};
  27. extract(tail, hdr);
  28. const auto len = std::max<size_t>(hdr.nlmsg_len, sizeof(hdr));
  29. onMsg(hdr, drop(take(tail, len), sizeof(hdr)));
  30. tail = drop(tail, align(len, 2));
  31. }
  32. }
  33. void forEachNetlinkAttribute(const Slice buf,
  34. const std::function<void(const nlattr&, const Slice)>& onAttr) {
  35. Slice tail = buf;
  36. while (tail.size() >= sizeof(nlattr)) {
  37. nlattr hdr = {};
  38. extract(tail, hdr);
  39. const auto len = std::max<size_t>(hdr.nla_len, sizeof(hdr));
  40. onAttr(hdr, drop(take(tail, len), sizeof(hdr)));
  41. tail = drop(tail, align(len, 2));
  42. }
  43. }
  44. } // namespace netdutils
  45. } // namespace android
  46. bool operator==(const sockaddr_nl& lhs, const sockaddr_nl& rhs) {
  47. return (lhs.nl_family == rhs.nl_family) && (lhs.nl_pid == rhs.nl_pid) &&
  48. (lhs.nl_groups == rhs.nl_groups);
  49. }
  50. bool operator!=(const sockaddr_nl& lhs, const sockaddr_nl& rhs) {
  51. return !(lhs == rhs);
  52. }
  53. std::ostream& operator<<(std::ostream& os, const nlmsghdr& hdr) {
  54. return os << std::hex << "nlmsghdr["
  55. << "len: 0x" << hdr.nlmsg_len << ", type: 0x" << hdr.nlmsg_type << ", flags: 0x"
  56. << hdr.nlmsg_flags << ", seq: 0x" << hdr.nlmsg_seq << ", pid: 0x" << hdr.nlmsg_pid
  57. << "]" << std::dec;
  58. }
  59. std::ostream& operator<<(std::ostream& os, const nlattr& attr) {
  60. return os << std::hex << "nlattr["
  61. << "len: 0x" << attr.nla_len << ", type: 0x" << attr.nla_type << "]" << std::dec;
  62. }
  63. std::ostream& operator<<(std::ostream& os, const sockaddr_nl& addr) {
  64. return os << std::hex << "sockaddr_nl["
  65. << "family: " << addr.nl_family << ", pid: " << addr.nl_pid
  66. << ", groups: " << addr.nl_groups << "]" << std::dec;
  67. }