arp.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 <LibCore/System.h>
  16. #include <LibMain/Main.h>
  17. #include <arpa/inet.h>
  18. #include <net/if_arp.h>
  19. #include <net/route.h>
  20. #include <netdb.h>
  21. #include <netinet/in.h>
  22. #include <string.h>
  23. #include <sys/ioctl.h>
  24. #include <sys/socket.h>
  25. #include <unistd.h>
  26. ErrorOr<int> serenity_main(Main::Arguments arguments)
  27. {
  28. TRY(Core::System::pledge("stdio rpath tty inet unix"));
  29. TRY(Core::System::unveil("/sys/kernel/net/arp", "r"));
  30. TRY(Core::System::unveil("/tmp/portal/lookup", "rw"));
  31. TRY(Core::System::unveil(nullptr, nullptr));
  32. static bool flag_set;
  33. static bool flag_delete;
  34. static bool flag_numeric;
  35. StringView value_ipv4_address;
  36. StringView value_hw_address;
  37. Core::ArgsParser args_parser;
  38. args_parser.set_general_help("Display or modify the system ARP cache");
  39. args_parser.add_option(flag_set, "Set an ARP table entry", "set", 's');
  40. args_parser.add_option(flag_delete, "Delete an ARP table entry", "delete", 'd');
  41. args_parser.add_option(flag_numeric, "Display numerical addresses. Don't resolve hostnames", "numeric", 'n');
  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(arguments);
  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("/sys/kernel/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_or_error = JsonValue::from_string(file_contents);
  86. if (json_or_error.is_error()) {
  87. warnln("Failed to decode JSON: {}", json_or_error.error());
  88. return 1;
  89. }
  90. auto json = json_or_error.release_value();
  91. Vector<JsonValue> sorted_regions = json.as_array().values();
  92. quick_sort(sorted_regions, [](auto& a, auto& b) {
  93. return a.as_object().get("ip_address"sv).to_string() < b.as_object().get("ip_address"sv).to_string();
  94. });
  95. for (auto& value : sorted_regions) {
  96. auto& if_object = value.as_object();
  97. auto ip_address = if_object.get("ip_address"sv).to_string();
  98. if (!flag_numeric) {
  99. auto from_string = IPv4Address::from_string(ip_address);
  100. auto addr = from_string.value().to_in_addr_t();
  101. auto* hostent = gethostbyaddr(&addr, sizeof(in_addr), AF_INET);
  102. if (hostent != nullptr) {
  103. StringView host_name { hostent->h_name, strlen(hostent->h_name) };
  104. if (!host_name.is_empty())
  105. ip_address = host_name;
  106. }
  107. }
  108. auto mac_address = if_object.get("mac_address"sv).to_string();
  109. if (proto_address_column != -1)
  110. columns[proto_address_column].buffer = ip_address;
  111. if (hw_address_column != -1)
  112. columns[hw_address_column].buffer = mac_address;
  113. for (auto& column : columns)
  114. print_column(column, column.buffer);
  115. outln();
  116. };
  117. }
  118. if (flag_set || flag_delete) {
  119. if (value_ipv4_address.is_empty() || value_hw_address.is_empty()) {
  120. warnln("No protocol address or hardware address specified.");
  121. return 1;
  122. }
  123. auto address = IPv4Address::from_string(value_ipv4_address);
  124. if (!address.has_value()) {
  125. warnln("Invalid IPv4 protocol address: '{}'", value_ipv4_address);
  126. return 1;
  127. }
  128. auto hw_address = MACAddress::from_string(value_hw_address);
  129. if (!hw_address.has_value()) {
  130. warnln("Invalid MACAddress: '{}'", value_hw_address);
  131. return 1;
  132. }
  133. int fd = TRY(Core::System::socket(AF_INET, SOCK_DGRAM, IPPROTO_IP));
  134. struct arpreq arp_req;
  135. memset(&arp_req, 0, sizeof(arp_req));
  136. arp_req.arp_pa.sa_family = AF_INET;
  137. ((sockaddr_in&)arp_req.arp_pa).sin_addr.s_addr = address.value().to_in_addr_t();
  138. *(MACAddress*)&arp_req.arp_ha.sa_data[0] = hw_address.value();
  139. if (flag_set)
  140. TRY(Core::System::ioctl(fd, SIOCSARP, &arp_req));
  141. if (flag_delete)
  142. TRY(Core::System::ioctl(fd, SIOCDARP, &arp_req));
  143. }
  144. return 0;
  145. }