Serenity Operating System
at hosted 192 lines 7.4 kB view raw
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 27#include <AK/JsonArray.h> 28#include <AK/JsonObject.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 41String si_bytes(unsigned bytes) 42{ 43 if (bytes >= GB) 44 return String::format("%fGiB", (double)bytes / (double)GB); 45 if (bytes >= MB) 46 return String::format("%fMiB", (double)bytes / (double)MB); 47 if (bytes >= KB) 48 return String::format("%fKiB", (double)bytes / (double)KB); 49 return String::format("%dB", bytes); 50} 51 52int main(int argc, char** argv) 53{ 54 const char* value_ipv4 = nullptr; 55 const char* value_adapter = nullptr; 56 const char* value_gateway = nullptr; 57 const char* value_mask = nullptr; 58 59 Core::ArgsParser args_parser; 60 args_parser.add_option(value_ipv4, "Set the IP address of the selected network", "ipv4", 'i', "The new IP of the network"); 61 args_parser.add_option(value_adapter, "Select a specific network adapter to configure", "adapter", 'a', "The name of a network adapter"); 62 args_parser.add_option(value_gateway, "Set the default gateway of the selected network", "gateway", 'g', "The new IP of the gateway"); 63 args_parser.add_option(value_mask, "Set the network mask of the selected network", "mask", 'm', "The new network mask"); 64 args_parser.parse(argc, argv); 65 66 if (!value_ipv4 && !value_adapter && !value_gateway && !value_mask) { 67 68 auto file = Core::File::construct("/proc/net/adapters"); 69 if (!file->open(Core::IODevice::ReadOnly)) { 70 fprintf(stderr, "Error: %s\n", file->error_string()); 71 return 1; 72 } 73 74 auto file_contents = file->read_all(); 75 auto json = JsonValue::from_string(file_contents).as_array(); 76 json.for_each([](auto& value) { 77 auto if_object = value.as_object(); 78 79 auto name = if_object.get("name").to_string(); 80 auto class_name = if_object.get("class_name").to_string(); 81 auto mac_address = if_object.get("mac_address").to_string(); 82 auto ipv4_address = if_object.get("ipv4_address").to_string(); 83 auto gateway = if_object.get("ipv4_gateway").to_string(); 84 auto netmask = if_object.get("ipv4_netmask").to_string(); 85 auto packets_in = if_object.get("packets_in").to_u32(); 86 auto bytes_in = if_object.get("bytes_in").to_u32(); 87 auto packets_out = if_object.get("packets_out").to_u32(); 88 auto bytes_out = if_object.get("bytes_out").to_u32(); 89 auto mtu = if_object.get("mtu").to_u32(); 90 91 printf("%s:\n", name.characters()); 92 printf("\tmac: %s\n", mac_address.characters()); 93 printf("\tipv4: %s\n", ipv4_address.characters()); 94 printf("\tnetmask: %s\n", netmask.characters()); 95 printf("\tgateway: %s\n", gateway.characters()); 96 printf("\tclass: %s\n", class_name.characters()); 97 printf("\tRX: %u packets %u bytes (%s)\n", packets_in, bytes_in, si_bytes(bytes_in).characters()); 98 printf("\tTX: %u packets %u bytes (%s)\n", packets_out, bytes_out, si_bytes(bytes_out).characters()); 99 printf("\tMTU: %u\n", mtu); 100 printf("\n"); 101 }); 102 } else { 103 104 if (!value_adapter) { 105 fprintf(stderr, "No network adapter was specified.\n"); 106 return 1; 107 } 108 109 String ifname = value_adapter; 110 111 if (value_ipv4) { 112 auto address = IPv4Address::from_string(value_ipv4); 113 114 if (!address.has_value()) { 115 fprintf(stderr, "Invalid IPv4 address: '%s'\n", value_ipv4); 116 return 1; 117 } 118 119 int fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP); 120 if (fd < 0) { 121 perror("socket"); 122 return 1; 123 } 124 125 struct ifreq ifr; 126 memset(&ifr, 0, sizeof(ifr)); 127 128 strncpy(ifr.ifr_name, ifname.characters(), IFNAMSIZ); 129 ifr.ifr_addr.sa_family = AF_INET; 130 ((sockaddr_in&)ifr.ifr_addr).sin_addr.s_addr = address.value().to_in_addr_t(); 131 132 int rc = ioctl(fd, SIOCSIFADDR, &ifr); 133 if (rc < 0) { 134 perror("ioctl(SIOCSIFADDR)"); 135 return 1; 136 } 137 } 138 139 if (value_mask) { 140 auto address = IPv4Address::from_string(value_mask); 141 142 if (!address.has_value()) { 143 fprintf(stderr, "Invalid IPv4 mask: '%s'\n", value_mask); 144 return 1; 145 } 146 147 int fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP); 148 if (fd < 0) { 149 perror("socket"); 150 return 1; 151 } 152 153 struct ifreq ifr; 154 memset(&ifr, 0, sizeof(ifr)); 155 156 strncpy(ifr.ifr_name, ifname.characters(), IFNAMSIZ); 157 ifr.ifr_netmask.sa_family = AF_INET; 158 ((sockaddr_in&)ifr.ifr_netmask).sin_addr.s_addr = address.value().to_in_addr_t(); 159 160 int rc = ioctl(fd, SIOCSIFNETMASK, &ifr); 161 if (rc < 0) { 162 perror("ioctl(SIOCSIFNETMASK)"); 163 return 1; 164 } 165 } 166 167 if (value_gateway) { 168 auto address = IPv4Address::from_string(value_gateway); 169 170 int fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP); 171 if (fd < 0) { 172 perror("socket"); 173 return 1; 174 } 175 176 struct rtentry rt; 177 memset(&rt, 0, sizeof(rt)); 178 179 rt.rt_dev = const_cast<char*>(ifname.characters()); 180 rt.rt_gateway.sa_family = AF_INET; 181 ((sockaddr_in&)rt.rt_gateway).sin_addr.s_addr = address.value().to_in_addr_t(); 182 rt.rt_flags = RTF_UP | RTF_GATEWAY; 183 184 int rc = ioctl(fd, SIOCADDRT, &rt); 185 if (rc < 0) { 186 perror("ioctl(SIOCADDRT)"); 187 return 1; 188 } 189 } 190 } 191 return 0; 192}