arp.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Assertions.h>
  7. #include <AK/IPv4Address.h>
  8. #include <AK/JsonObject.h>
  9. #include <AK/MACAddress.h>
  10. #include <AK/QuickSort.h>
  11. #include <AK/String.h>
  12. #include <AK/Types.h>
  13. #include <LibCore/ArgsParser.h>
  14. #include <LibCore/File.h>
  15. #include <net/route.h>
  16. #include <netinet/in.h>
  17. #include <sys/ioctl.h>
  18. #include <sys/socket.h>
  19. #include <unistd.h>
  20. int main(int argc, char** argv)
  21. {
  22. if (pledge("stdio rpath tty", nullptr) < 0) {
  23. perror("pledge");
  24. return 1;
  25. }
  26. if (unveil("/proc/net/arp", "r") < 0) {
  27. perror("unveil");
  28. return 1;
  29. }
  30. if (unveil(nullptr, nullptr) < 0) {
  31. perror("unveil");
  32. return 1;
  33. }
  34. static bool flag_set;
  35. static bool flag_delete;
  36. const char* value_ipv4_address = nullptr;
  37. const char* value_hw_address = nullptr;
  38. Core::ArgsParser args_parser;
  39. args_parser.set_general_help("Display or modify the system ARP cache");
  40. args_parser.add_option(flag_set, "Set an ARP table entry", "set", 's');
  41. args_parser.add_option(flag_delete, "Delete an ARP table entry", "delete", 'd');
  42. args_parser.add_positional_argument(value_ipv4_address, "IPv4 protocol address", "address", Core::ArgsParser::Required::No);
  43. args_parser.add_positional_argument(value_hw_address, "Hardware address", "hwaddress", Core::ArgsParser::Required::No);
  44. args_parser.parse(argc, argv);
  45. enum class Alignment {
  46. Left,
  47. Right
  48. };
  49. struct Column {
  50. String title;
  51. Alignment alignment { Alignment::Left };
  52. int width { 0 };
  53. String buffer;
  54. };
  55. Vector<Column> columns;
  56. int proto_address_column = -1;
  57. int hw_address_column = -1;
  58. auto add_column = [&](auto title, auto alignment, auto width) {
  59. columns.append({ title, alignment, width, {} });
  60. return columns.size() - 1;
  61. };
  62. proto_address_column = add_column("Address", Alignment::Left, 15);
  63. hw_address_column = add_column("HWaddress", Alignment::Left, 15);
  64. auto print_column = [](auto& column, auto& string) {
  65. if (!column.width) {
  66. out("{}", string);
  67. return;
  68. }
  69. if (column.alignment == Alignment::Right) {
  70. out("{:>{1}} "sv, string, column.width);
  71. } else {
  72. out("{:<{1}} "sv, string, column.width);
  73. }
  74. };
  75. for (auto& column : columns)
  76. print_column(column, column.title);
  77. outln();
  78. if (!flag_set && !flag_delete) {
  79. auto file = Core::File::construct("/proc/net/arp");
  80. if (!file->open(Core::OpenMode::ReadOnly)) {
  81. warnln("Failed to open {}: {}", file->name(), file->error_string());
  82. return 1;
  83. }
  84. auto file_contents = file->read_all();
  85. auto json = JsonValue::from_string(file_contents);
  86. VERIFY(json.has_value());
  87. Vector<JsonValue> sorted_regions = json.value().as_array().values();
  88. quick_sort(sorted_regions, [](auto& a, auto& b) {
  89. return a.as_object().get("ip_address").to_string() < b.as_object().get("ip_address").to_string();
  90. });
  91. for (auto& value : sorted_regions) {
  92. auto& if_object = value.as_object();
  93. auto ip_address = if_object.get("ip_address").to_string();
  94. auto mac_address = if_object.get("mac_address").to_string();
  95. if (proto_address_column != -1)
  96. columns[proto_address_column].buffer = ip_address;
  97. if (hw_address_column != -1)
  98. columns[hw_address_column].buffer = mac_address;
  99. for (auto& column : columns)
  100. print_column(column, column.buffer);
  101. outln();
  102. };
  103. }
  104. if (flag_set || flag_delete) {
  105. if (!value_ipv4_address || !value_hw_address) {
  106. warnln("No protocol address or hardware address specified.");
  107. return 1;
  108. }
  109. auto address = IPv4Address::from_string(value_ipv4_address);
  110. if (!address.has_value()) {
  111. warnln("Invalid IPv4 protocol address: '{}'", value_ipv4_address);
  112. return 1;
  113. }
  114. auto hw_address = MACAddress::from_string(value_hw_address);
  115. if (!hw_address.has_value()) {
  116. warnln("Invalid MACAddress: '{}'", value_hw_address);
  117. return 1;
  118. }
  119. int fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
  120. if (fd < 0) {
  121. perror("socket");
  122. return 1;
  123. }
  124. struct arpreq arp_req;
  125. memset(&arp_req, 0, sizeof(arp_req));
  126. arp_req.arp_pa.sa_family = AF_INET;
  127. ((sockaddr_in&)arp_req.arp_pa).sin_addr.s_addr = address.value().to_in_addr_t();
  128. *(MACAddress*)&arp_req.arp_ha.sa_data[0] = hw_address.value();
  129. int rc;
  130. if (flag_set)
  131. rc = ioctl(fd, SIOCSARP, &arp_req);
  132. if (flag_delete)
  133. rc = ioctl(fd, SIOCDARP, &arp_req);
  134. if (rc < 0) {
  135. perror("ioctl");
  136. return 1;
  137. }
  138. }
  139. return 0;
  140. }