ifconfig.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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/ByteBuffer.h>
  27. #include <AK/JsonArray.h>
  28. #include <AK/JsonObject.h>
  29. #include <AK/NumberFormat.h>
  30. #include <AK/String.h>
  31. #include <AK/Types.h>
  32. #include <LibCore/ArgsParser.h>
  33. #include <LibCore/File.h>
  34. #include <net/if.h>
  35. #include <net/route.h>
  36. #include <netinet/in.h>
  37. #include <stdio.h>
  38. #include <string.h>
  39. #include <sys/ioctl.h>
  40. #include <sys/socket.h>
  41. int main(int argc, char** argv)
  42. {
  43. const char* value_ipv4 = nullptr;
  44. const char* value_adapter = nullptr;
  45. const char* value_gateway = nullptr;
  46. const char* value_mask = nullptr;
  47. Core::ArgsParser args_parser;
  48. args_parser.add_option(value_ipv4, "Set the IP address of the selected network", "ipv4", 'i', "The new IP of the network");
  49. args_parser.add_option(value_adapter, "Select a specific network adapter to configure", "adapter", 'a', "The name of a network adapter");
  50. args_parser.add_option(value_gateway, "Set the default gateway of the selected network", "gateway", 'g', "The new IP of the gateway");
  51. args_parser.add_option(value_mask, "Set the network mask of the selected network", "mask", 'm', "The new network mask");
  52. args_parser.parse(argc, argv);
  53. if (!value_ipv4 && !value_adapter && !value_gateway && !value_mask) {
  54. auto file = Core::File::construct("/proc/net/adapters");
  55. if (!file->open(Core::IODevice::ReadOnly)) {
  56. fprintf(stderr, "Error: %s\n", file->error_string());
  57. return 1;
  58. }
  59. auto file_contents = file->read_all();
  60. auto json = JsonValue::from_string(file_contents);
  61. ASSERT(json.has_value());
  62. json.value().as_array().for_each([](auto& value) {
  63. auto if_object = value.as_object();
  64. auto name = if_object.get("name").to_string();
  65. auto class_name = if_object.get("class_name").to_string();
  66. auto mac_address = if_object.get("mac_address").to_string();
  67. auto ipv4_address = if_object.get("ipv4_address").to_string();
  68. auto gateway = if_object.get("ipv4_gateway").to_string();
  69. auto netmask = if_object.get("ipv4_netmask").to_string();
  70. auto packets_in = if_object.get("packets_in").to_u32();
  71. auto bytes_in = if_object.get("bytes_in").to_u32();
  72. auto packets_out = if_object.get("packets_out").to_u32();
  73. auto bytes_out = if_object.get("bytes_out").to_u32();
  74. auto mtu = if_object.get("mtu").to_u32();
  75. printf("%s:\n", name.characters());
  76. printf("\tmac: %s\n", mac_address.characters());
  77. printf("\tipv4: %s\n", ipv4_address.characters());
  78. printf("\tnetmask: %s\n", netmask.characters());
  79. printf("\tgateway: %s\n", gateway.characters());
  80. printf("\tclass: %s\n", class_name.characters());
  81. printf("\tRX: %u packets %u bytes (%s)\n", packets_in, bytes_in, human_readable_size(bytes_in).characters());
  82. printf("\tTX: %u packets %u bytes (%s)\n", packets_out, bytes_out, human_readable_size(bytes_out).characters());
  83. printf("\tMTU: %u\n", mtu);
  84. printf("\n");
  85. });
  86. } else {
  87. if (!value_adapter) {
  88. fprintf(stderr, "No network adapter was specified.\n");
  89. return 1;
  90. }
  91. String ifname = value_adapter;
  92. if (value_ipv4) {
  93. auto address = IPv4Address::from_string(value_ipv4);
  94. if (!address.has_value()) {
  95. fprintf(stderr, "Invalid IPv4 address: '%s'\n", value_ipv4);
  96. return 1;
  97. }
  98. int fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
  99. if (fd < 0) {
  100. perror("socket");
  101. return 1;
  102. }
  103. struct ifreq ifr;
  104. memset(&ifr, 0, sizeof(ifr));
  105. bool fits = ifname.copy_characters_to_buffer(ifr.ifr_name, IFNAMSIZ);
  106. if (!fits) {
  107. fprintf(stderr, "Interface name '%s' is too long\n", ifname.characters());
  108. return 1;
  109. }
  110. ifr.ifr_addr.sa_family = AF_INET;
  111. ((sockaddr_in&)ifr.ifr_addr).sin_addr.s_addr = address.value().to_in_addr_t();
  112. int rc = ioctl(fd, SIOCSIFADDR, &ifr);
  113. if (rc < 0) {
  114. perror("ioctl(SIOCSIFADDR)");
  115. return 1;
  116. }
  117. }
  118. if (value_mask) {
  119. auto address = IPv4Address::from_string(value_mask);
  120. if (!address.has_value()) {
  121. fprintf(stderr, "Invalid IPv4 mask: '%s'\n", value_mask);
  122. return 1;
  123. }
  124. int fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
  125. if (fd < 0) {
  126. perror("socket");
  127. return 1;
  128. }
  129. struct ifreq ifr;
  130. memset(&ifr, 0, sizeof(ifr));
  131. bool fits = ifname.copy_characters_to_buffer(ifr.ifr_name, IFNAMSIZ);
  132. if (!fits) {
  133. fprintf(stderr, "Interface name '%s' is too long\n", ifname.characters());
  134. return 1;
  135. }
  136. ifr.ifr_netmask.sa_family = AF_INET;
  137. ((sockaddr_in&)ifr.ifr_netmask).sin_addr.s_addr = address.value().to_in_addr_t();
  138. int rc = ioctl(fd, SIOCSIFNETMASK, &ifr);
  139. if (rc < 0) {
  140. perror("ioctl(SIOCSIFNETMASK)");
  141. return 1;
  142. }
  143. }
  144. if (value_gateway) {
  145. auto address = IPv4Address::from_string(value_gateway);
  146. int fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
  147. if (fd < 0) {
  148. perror("socket");
  149. return 1;
  150. }
  151. struct rtentry rt;
  152. memset(&rt, 0, sizeof(rt));
  153. rt.rt_dev = const_cast<char*>(ifname.characters());
  154. rt.rt_gateway.sa_family = AF_INET;
  155. ((sockaddr_in&)rt.rt_gateway).sin_addr.s_addr = address.value().to_in_addr_t();
  156. rt.rt_flags = RTF_UP | RTF_GATEWAY;
  157. int rc = ioctl(fd, SIOCADDRT, &rt);
  158. if (rc < 0) {
  159. perror("ioctl(SIOCADDRT)");
  160. return 1;
  161. }
  162. }
  163. }
  164. return 0;
  165. }