2020-01-18 03:38:21 -05:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 04:24:48 -04:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 03:38:21 -05:00
|
|
|
*/
|
|
|
|
|
2020-08-24 21:35:19 -04:00
|
|
|
#include <AK/Singleton.h>
|
2019-04-02 09:46:44 -04:00
|
|
|
#include <Kernel/Net/LoopbackAdapter.h>
|
|
|
|
|
2020-02-15 19:27:42 -05:00
|
|
|
namespace Kernel {
|
|
|
|
|
2021-06-04 00:43:16 -04:00
|
|
|
static bool s_loopback_initialized = false;
|
2020-08-24 21:35:19 -04:00
|
|
|
|
2023-04-10 20:44:55 -04:00
|
|
|
ErrorOr<NonnullRefPtr<LoopbackAdapter>> LoopbackAdapter::try_create()
|
2019-04-02 09:46:44 -04:00
|
|
|
{
|
2023-08-11 17:41:18 -04:00
|
|
|
return TRY(adopt_nonnull_ref_or_enomem(new (nothrow) LoopbackAdapter("loop"sv)));
|
2019-04-02 09:46:44 -04:00
|
|
|
}
|
|
|
|
|
2023-08-11 17:41:18 -04:00
|
|
|
LoopbackAdapter::LoopbackAdapter(StringView interface_name)
|
|
|
|
: NetworkAdapter(interface_name)
|
2019-04-02 09:46:44 -04:00
|
|
|
{
|
2021-06-04 00:43:16 -04:00
|
|
|
VERIFY(!s_loopback_initialized);
|
|
|
|
s_loopback_initialized = true;
|
2023-11-25 09:38:19 -05: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 06:03:07 -05:00
|
|
|
set_mac_address({ 19, 85, 2, 9, 0x55, 0xaa });
|
2019-04-02 09:46:44 -04:00
|
|
|
}
|
|
|
|
|
2022-03-16 15:15:15 -04:00
|
|
|
LoopbackAdapter::~LoopbackAdapter() = default;
|
2019-04-02 09:46:44 -04:00
|
|
|
|
2020-07-28 14:19:22 -04:00
|
|
|
void LoopbackAdapter::send_raw(ReadonlyBytes payload)
|
2019-04-02 09:46:44 -04:00
|
|
|
{
|
2023-06-17 04:37:47 -04:00
|
|
|
dbgln_if(LOOPBACK_DEBUG, "LoopbackAdapter: Sending {} byte(s) to myself.", payload.size());
|
2020-07-28 14:19:22 -04:00
|
|
|
did_receive(payload);
|
2019-04-02 09:46:44 -04:00
|
|
|
}
|
2020-02-15 19:27:42 -05:00
|
|
|
|
|
|
|
}
|