// Initialize Arp::Arp(net::Inet<Ethernet,IP4>& inet) noexcept: requests_rx_ {Statman::get().create(Stat::UINT32, inet.ifname() + ".arp.requests_rx").get_uint32()}, requests_tx_ {Statman::get().create(Stat::UINT32, inet.ifname() + ".arp.requests_tx").get_uint32()}, replies_rx_ {Statman::get().create(Stat::UINT32, inet.ifname() + ".arp.replies_rx").get_uint32()}, replies_tx_ {Statman::get().create(Stat::UINT32, inet.ifname() + ".arp.replies_tx").get_uint32()}, inet_ {inet}, mac_ (inet.link_addr()), linklayer_out_ {ignore} {}
static inline uint16_t udp_port_scan(net::Inet& inet) { for (uint16_t udp_port = 1; udp_port < 65535; udp_port++) { if (inet.udp().is_bound({inet.ip_addr(), udp_port})) { return udp_port; } } return 0; }
static void do_test(net::Inet& inet, std::vector<Name_request>& reqs) { for(auto& req : reqs) { if(req.server == 0) req.server = inet.dns_addr(); inet.resolve(req.name, req.server, [name = req.name, server = req.server] (auto res, const Error& err) { if (err) { print_error(name, server, err); } else { if (res) print_success(name, server, std::move(res)); else print_not_resolved(name); } }); } }
// Initialize Arp::Arp(net::Inet<Ethernet,IP4>& inet): inet_(inet), mac_(inet.link_addr()), ip_(inet.ip_addr()), linklayer_out_(downstream(ignore)) {}
static void start_acorn(net::Inet& inet) { /** SETUP LOGGER */ const int LOGBUFFER_LEN = 1024*16; static gsl::span<char> spanerino{new char[LOGBUFFER_LEN], LOGBUFFER_LEN}; logger_ = std::make_unique<Logger>(spanerino); logger_->flush(); logger_->log("LUL\n"); OS::add_stdout( [] (const char* data, size_t len) { // append timestamp auto entry = "[" + isotime::now() + "]" + std::string{data, len}; logger_->log(entry); }); disk = fs::shared_memdisk(); // init the first legit partition/filesystem disk->init_fs( [&inet] (fs::error_t err, auto& fs) { if (err) panic("Could not mount filesystem...\n"); // only works with synchronous disks (memdisk) list_static_content(fs); /** BUCKET SETUP */ // create squirrel bucket squirrels = std::make_shared<SquirrelBucket>(10); // set member name to be unique squirrels->add_index<std::string>("name", [](const Squirrel& s)->const auto& { return s.get_name(); }, SquirrelBucket::UNIQUE); // seed squirrels squirrels->spawn("Alfred"s, 1000U, "Wizard"s); squirrels->spawn("Alf"s, 6U, "Script Kiddie"s); squirrels->spawn("Andreas"s, 28U, "Code Monkey"s); squirrels->spawn("AnnikaH"s, 20U, "Fairy"s); squirrels->spawn("Ingve"s, 24U, "Integration Master"s); squirrels->spawn("Martin"s, 16U, "Build Master"s); squirrels->spawn("Rico"s, 28U, "Mad Scientist"s); // setup users bucket users = std::make_shared<UserBucket>(); users->spawn(); users->spawn(); /** ROUTES SETUP **/ using namespace mana; Router router; // setup Squirrel routes router.use("/api/squirrels", routes::Squirrels{squirrels}); // setup User routes router.use("/api/users", routes::Users{users}); // setup Language routes router.use("/api/languages", routes::Languages{}); /** DASHBOARD SETUP **/ dashboard_ = std::make_unique<dashboard::Dashboard>(8192); // Add singleton component dashboard_->add(dashboard::Memmap::instance()); dashboard_->add(dashboard::StackSampler::instance()); dashboard_->add(dashboard::Status::instance()); // Construct component dashboard_->construct<dashboard::Statman>(Statman::get()); dashboard_->construct<dashboard::TCP>(inet.tcp()); dashboard_->construct<dashboard::CPUsage>(); dashboard_->construct<dashboard::Logger>(*logger_, static_cast<size_t>(50)); // Add Dashboard routes to "/api/dashboard" router.use("/api/dashboard", dashboard_->router()); // Fallback route for angular application - serve index.html if route is not found router.on_get("/app/.*", [&fs](auto, auto res) { #ifdef VERBOSE_WEBSERVER printf("[@GET:/app/*] Fallback route - try to serve index.html\n"); #endif fs.cstat("/public/app/index.html", [res](auto err, const auto& entry) { if(err) { res->send_code(http::Not_Found); } else { // Serve index.html #ifdef VERBOSE_WEBSERVER printf("[@GET:/app/*] (Fallback) Responding with index.html. \n"); #endif res->send_file({disk, entry}); } }); }); INFO("Router", "Registered routes:\n%s", router.to_string().c_str()); /** SERVER SETUP **/ server_ = std::make_unique<Server>(inet.tcp()); // set routes and start listening server_->set_routes(router).listen(80); /** MIDDLEWARE SETUP **/ // custom middleware to serve static files auto opt = {"index.html"}; Middleware_ptr butler = std::make_shared<middleware::Butler>(disk, "/public", opt); server_->use(butler); // custom middleware to serve a webpage for a directory Middleware_ptr director = std::make_shared<middleware::Director>(disk, "/public/static"); server_->use("/static", director); Middleware_ptr parsley = std::make_shared<middleware::Parsley>(); server_->use(parsley); Middleware_ptr cookie_parser = std::make_shared<middleware::Cookie_parser>(); server_->use(cookie_parser); }); // < disk }