Serenity Operating System
at portability 113 lines 4.3 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/File.h> 32#include <net/if.h> 33#include <netinet/in.h> 34#include <stdio.h> 35#include <sys/ioctl.h> 36#include <sys/socket.h> 37 38String si_bytes(unsigned bytes) 39{ 40 if (bytes >= GB) 41 return String::format("%fGiB", (double)bytes / (double)GB); 42 if (bytes >= MB) 43 return String::format("%fMiB", (double)bytes / (double)MB); 44 if (bytes >= KB) 45 return String::format("%fKiB", (double)bytes / (double)KB); 46 return String::format("%dB", bytes); 47} 48 49int main(int argc, char** argv) 50{ 51 if (argc == 3) { 52 String ifname = argv[1]; 53 auto address = IPv4Address::from_string(argv[2]); 54 55 if (!address.has_value()) { 56 fprintf(stderr, "Invalid IPv4 address: '%s'\n", argv[2]); 57 return 1; 58 } 59 60 int fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP); 61 if (fd < 0) { 62 perror("socket"); 63 return 1; 64 } 65 66 struct ifreq ifr; 67 memset(&ifr, 0, sizeof(ifr)); 68 69 strncpy(ifr.ifr_name, ifname.characters(), IFNAMSIZ); 70 ifr.ifr_addr.sa_family = AF_INET; 71 ((sockaddr_in&)ifr.ifr_addr).sin_addr.s_addr = address.value().to_in_addr_t(); 72 73 int rc = ioctl(fd, SIOCSIFADDR, &ifr); 74 if (rc < 0) { 75 perror("ioctl(SIOCSIFADDR)"); 76 return 1; 77 } 78 return 0; 79 } 80 81 auto file = Core::File::construct("/proc/net/adapters"); 82 if (!file->open(Core::IODevice::ReadOnly)) { 83 fprintf(stderr, "Error: %s\n", file->error_string()); 84 return 1; 85 } 86 87 auto file_contents = file->read_all(); 88 auto json = JsonValue::from_string(file_contents).as_array(); 89 json.for_each([](auto& value) { 90 auto if_object = value.as_object(); 91 92 auto name = if_object.get("name").to_string(); 93 auto class_name = if_object.get("class_name").to_string(); 94 auto mac_address = if_object.get("mac_address").to_string(); 95 auto ipv4_address = if_object.get("ipv4_address").to_string(); 96 auto packets_in = if_object.get("packets_in").to_u32(); 97 auto bytes_in = if_object.get("bytes_in").to_u32(); 98 auto packets_out = if_object.get("packets_out").to_u32(); 99 auto bytes_out = if_object.get("bytes_out").to_u32(); 100 auto mtu = if_object.get("mtu").to_u32(); 101 102 printf("%s:\n", name.characters()); 103 printf(" mac: %s\n", mac_address.characters()); 104 printf(" ipv4: %s\n", ipv4_address.characters()); 105 printf(" class: %s\n", class_name.characters()); 106 printf(" RX: %u packets %u bytes (%s)\n", packets_in, bytes_in, si_bytes(bytes_in).characters()); 107 printf(" TX: %u packets %u bytes (%s)\n", packets_out, bytes_out, si_bytes(bytes_out).characters()); 108 printf(" MTU: %u\n", mtu); 109 printf("\n"); 110 }); 111 112 return 0; 113}