ifconfig.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/JsonArray.h>
  27. #include <AK/JsonObject.h>
  28. #include <AK/NumberFormat.h>
  29. #include <AK/String.h>
  30. #include <AK/Types.h>
  31. #include <LibCore/ArgsParser.h>
  32. #include <LibCore/File.h>
  33. #include <net/if.h>
  34. #include <net/route.h>
  35. #include <netinet/in.h>
  36. #include <stdio.h>
  37. #include <string.h>
  38. #include <sys/ioctl.h>
  39. #include <sys/socket.h>
  40. int main(int argc, char** argv)
  41. {
  42. const char* value_ipv4 = nullptr;
  43. const char* value_adapter = nullptr;
  44. const char* value_gateway = nullptr;
  45. const char* value_mask = nullptr;
  46. Core::ArgsParser args_parser;
  47. args_parser.add_option(value_ipv4, "Set the IP address of the selected network", "ipv4", 'i', "The new IP of the network");
  48. args_parser.add_option(value_adapter, "Select a specific network adapter to configure", "adapter", 'a', "The name of a network adapter");
  49. args_parser.add_option(value_gateway, "Set the default gateway of the selected network", "gateway", 'g', "The new IP of the gateway");
  50. args_parser.add_option(value_mask, "Set the network mask of the selected network", "mask", 'm', "The new network mask");
  51. args_parser.parse(argc, argv);
  52. if (!value_ipv4 && !value_adapter && !value_gateway && !value_mask) {
  53. auto file = Core::File::construct("/proc/net/adapters");
  54. if (!file->open(Core::IODevice::ReadOnly)) {
  55. fprintf(stderr, "Error: %s\n", file->error_string());
  56. return 1;
  57. }
  58. auto file_contents = file->read_all();
  59. auto json = JsonValue::from_string(file_contents);
  60. ASSERT(json.has_value());
  61. json.value().as_array().for_each([](auto& value) {
  62. auto if_object = value.as_object();
  63. auto name = if_object.get("name").to_string();
  64. auto class_name = if_object.get("class_name").to_string();
  65. auto mac_address = if_object.get("mac_address").to_string();
  66. auto ipv4_address = if_object.get("ipv4_address").to_string();
  67. auto gateway = if_object.get("ipv4_gateway").to_string();
  68. auto netmask = if_object.get("ipv4_netmask").to_string();
  69. auto packets_in = if_object.get("packets_in").to_u32();
  70. auto bytes_in = if_object.get("bytes_in").to_u32();
  71. auto packets_out = if_object.get("packets_out").to_u32();
  72. auto bytes_out = if_object.get("bytes_out").to_u32();
  73. auto mtu = if_object.get("mtu").to_u32();
  74. printf("%s:\n", name.characters());
  75. printf("\tmac: %s\n", mac_address.characters());
  76. printf("\tipv4: %s\n", ipv4_address.characters());
  77. printf("\tnetmask: %s\n", netmask.characters());
  78. printf("\tgateway: %s\n", gateway.characters());
  79. printf("\tclass: %s\n", class_name.characters());
  80. printf("\tRX: %u packets %u bytes (%s)\n", packets_in, bytes_in, human_readable_size(bytes_in).characters());
  81. printf("\tTX: %u packets %u bytes (%s)\n", packets_out, bytes_out, human_readable_size(bytes_out).characters());
  82. printf("\tMTU: %u\n", mtu);
  83. printf("\n");
  84. });
  85. } else {
  86. if (!value_adapter) {
  87. fprintf(stderr, "No network adapter was specified.\n");
  88. return 1;
  89. }
  90. String ifname = value_adapter;
  91. if (value_ipv4) {
  92. auto address = IPv4Address::from_string(value_ipv4);
  93. if (!address.has_value()) {
  94. fprintf(stderr, "Invalid IPv4 address: '%s'\n", value_ipv4);
  95. return 1;
  96. }
  97. int fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
  98. if (fd < 0) {
  99. perror("socket");
  100. return 1;
  101. }
  102. struct ifreq ifr;
  103. memset(&ifr, 0, sizeof(ifr));
  104. bool fits = ifname.copy_characters_to_buffer(ifr.ifr_name, IFNAMSIZ);
  105. if (!fits) {
  106. fprintf(stderr, "Interface name '%s' is too long\n", ifname.characters());
  107. return 1;
  108. }
  109. ifr.ifr_addr.sa_family = AF_INET;
  110. ((sockaddr_in&)ifr.ifr_addr).sin_addr.s_addr = address.value().to_in_addr_t();
  111. int rc = ioctl(fd, SIOCSIFADDR, &ifr);
  112. if (rc < 0) {
  113. perror("ioctl(SIOCSIFADDR)");
  114. return 1;
  115. }
  116. }
  117. if (value_mask) {
  118. auto address = IPv4Address::from_string(value_mask);
  119. if (!address.has_value()) {
  120. fprintf(stderr, "Invalid IPv4 mask: '%s'\n", value_mask);
  121. return 1;
  122. }
  123. int fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
  124. if (fd < 0) {
  125. perror("socket");
  126. return 1;
  127. }
  128. struct ifreq ifr;
  129. memset(&ifr, 0, sizeof(ifr));
  130. bool fits = ifname.copy_characters_to_buffer(ifr.ifr_name, IFNAMSIZ);
  131. if (!fits) {
  132. fprintf(stderr, "Interface name '%s' is too long\n", ifname.characters());
  133. return 1;
  134. }
  135. ifr.ifr_netmask.sa_family = AF_INET;
  136. ((sockaddr_in&)ifr.ifr_netmask).sin_addr.s_addr = address.value().to_in_addr_t();
  137. int rc = ioctl(fd, SIOCSIFNETMASK, &ifr);
  138. if (rc < 0) {
  139. perror("ioctl(SIOCSIFNETMASK)");
  140. return 1;
  141. }
  142. }
  143. if (value_gateway) {
  144. auto address = IPv4Address::from_string(value_gateway);
  145. int fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
  146. if (fd < 0) {
  147. perror("socket");
  148. return 1;
  149. }
  150. struct rtentry rt;
  151. memset(&rt, 0, sizeof(rt));
  152. rt.rt_dev = const_cast<char*>(ifname.characters());
  153. rt.rt_gateway.sa_family = AF_INET;
  154. ((sockaddr_in&)rt.rt_gateway).sin_addr.s_addr = address.value().to_in_addr_t();
  155. rt.rt_flags = RTF_UP | RTF_GATEWAY;
  156. int rc = ioctl(fd, SIOCADDRT, &rt);
  157. if (rc < 0) {
  158. perror("ioctl(SIOCADDRT)");
  159. return 1;
  160. }
  161. }
  162. }
  163. return 0;
  164. }