Serenity Operating System
at hosted 101 lines 3.4 kB view raw
1/* 2 * Copyright (c) 2020, The SerenityOS developers. 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 "DHCPv4Client.h" 28#include <AK/JsonArray.h> 29#include <AK/JsonObject.h> 30#include <AK/String.h> 31#include <AK/Types.h> 32#include <LibCore/EventLoop.h> 33#include <LibCore/File.h> 34#include <LibCore/LocalServer.h> 35#include <stdio.h> 36#include <string.h> 37 38static u8 mac_part(const Vector<String>& parts, size_t index) 39{ 40 auto chars = parts.at(index).characters(); 41 return (chars[0] - '0') * 16 + (chars[1] - '0'); 42} 43 44static MACAddress mac_from_string(const String& str) 45{ 46 auto chunks = str.split(':'); 47 ASSERT(chunks.size() == 6); // should we...worry about this? 48 return { 49 mac_part(chunks, 0), mac_part(chunks, 1), mac_part(chunks, 2), 50 mac_part(chunks, 3), mac_part(chunks, 4), mac_part(chunks, 5) 51 }; 52} 53 54int main(int argc, char** argv) 55{ 56 (void)argc; 57 (void)argv; 58 59 if (pledge("stdio unix inet cpath rpath fattr", nullptr) < 0) { 60 perror("pledge"); 61 return 1; 62 } 63 64 Core::EventLoop event_loop; 65 66 if (unveil("/proc/net/", "r") < 0) { 67 perror("unveil"); 68 return 1; 69 } 70 71 unveil(nullptr, nullptr); 72 73 auto file = Core::File::construct("/proc/net/adapters"); 74 if (!file->open(Core::IODevice::ReadOnly)) { 75 fprintf(stderr, "Error: %s\n", file->error_string()); 76 return 1; 77 } 78 79 auto file_contents = file->read_all(); 80 auto json = JsonValue::from_string(file_contents).as_array(); 81 Vector<InterfaceDescriptor> ifnames; 82 json.for_each([&ifnames](auto& value) { 83 auto if_object = value.as_object(); 84 85 if (if_object.get("class_name").to_string() == "LoopbackAdapter") 86 return; 87 88 auto name = if_object.get("name").to_string(); 89 auto mac = if_object.get("mac_address").to_string(); 90 ifnames.append({ name, mac_from_string(mac) }); 91 }); 92 93 auto client = DHCPv4Client::construct(move(ifnames)); 94 95 if (pledge("stdio inet", nullptr) < 0) { 96 perror("pledge"); 97 return 1; 98 } 99 100 return event_loop.exec(); 101}