main.cpp 3.3 KB

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