2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 11:38:21 +03:00
|
|
|
*/
|
|
|
|
|
2020-08-25 04:35:19 +03:00
|
|
|
#include <AK/Singleton.h>
|
2019-04-02 16:46:44 +03:00
|
|
|
#include <Kernel/Net/LoopbackAdapter.h>
|
|
|
|
|
2020-02-16 03:27:42 +03:00
|
|
|
namespace Kernel {
|
|
|
|
|
2021-06-04 07:43:16 +03:00
|
|
|
static bool s_loopback_initialized = false;
|
2020-08-25 04:35:19 +03:00
|
|
|
|
2023-04-11 03:44:55 +03:00
|
|
|
ErrorOr<NonnullRefPtr<LoopbackAdapter>> LoopbackAdapter::try_create()
|
2019-04-02 16:46:44 +03:00
|
|
|
{
|
2023-08-12 00:41:18 +03:00
|
|
|
return TRY(adopt_nonnull_ref_or_enomem(new (nothrow) LoopbackAdapter("loop"sv)));
|
2019-04-02 16:46:44 +03:00
|
|
|
}
|
|
|
|
|
2023-08-12 00:41:18 +03:00
|
|
|
LoopbackAdapter::LoopbackAdapter(StringView interface_name)
|
|
|
|
: NetworkAdapter(interface_name)
|
2019-04-02 16:46:44 +03:00
|
|
|
{
|
2021-06-04 07:43:16 +03:00
|
|
|
VERIFY(!s_loopback_initialized);
|
|
|
|
s_loopback_initialized = true;
|
2023-11-25 17:38:19 +03:00
|
|
|
// The networking subsystem currently assumes all adapters are Ethernet adapters, including the LoopbackAdapter,
|
|
|
|
// so all packets are pre-pended with an Ethernet Frame header. Since the MTU must not include any overhead added
|
|
|
|
// by the data-link (Ethernet in this case) or physical layers, we need to subtract it from the MTU.
|
|
|
|
set_mtu(65536 - sizeof(EthernetFrameHeader));
|
2020-02-09 14:03:07 +03:00
|
|
|
set_mac_address({ 19, 85, 2, 9, 0x55, 0xaa });
|
2019-04-02 16:46:44 +03:00
|
|
|
}
|
|
|
|
|
2022-03-16 22:15:15 +03:00
|
|
|
LoopbackAdapter::~LoopbackAdapter() = default;
|
2019-04-02 16:46:44 +03:00
|
|
|
|
2020-07-28 21:19:22 +03:00
|
|
|
void LoopbackAdapter::send_raw(ReadonlyBytes payload)
|
2019-04-02 16:46:44 +03:00
|
|
|
{
|
2023-06-17 11:37:47 +03:00
|
|
|
dbgln_if(LOOPBACK_DEBUG, "LoopbackAdapter: Sending {} byte(s) to myself.", payload.size());
|
2020-07-28 21:19:22 +03:00
|
|
|
did_receive(payload);
|
2019-04-02 16:46:44 +03:00
|
|
|
}
|
2020-02-16 03:27:42 +03:00
|
|
|
|
|
|
|
}
|