rewrite comments into one, unified style
This commit is contained in:
1 parent
6260de0085
commit
467a58c9c6
163 files changed
+3560
-2556
No files matched your search
@@ -1,4 +1,4 @@
|
||||
// lwip_stack.c - lwIP network stack provider driver.
|
||||
// lwIP stack provider glue. This file bridges the kernel network-device model into lwIP.
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
@@ -130,6 +130,7 @@ static const NET_STACK_PROVIDER gLwipProvider = {
|
||||
LwipProviderResolveHostIpv4,
|
||||
};
|
||||
|
||||
// Implements the err To Status operation.
|
||||
static KSTATUS LwipErrToStatus(err_t err) {
|
||||
switch (err) {
|
||||
case ERR_OK: return STATUS_SUCCESS;
|
||||
@@ -150,10 +151,12 @@ static KSTATUS LwipErrToStatus(err_t err) {
|
||||
}
|
||||
}
|
||||
|
||||
// Implements the ascii Is Digit operation.
|
||||
static int LwipAsciiIsDigit(char c) {
|
||||
return c >= '0' && c <= '9';
|
||||
}
|
||||
|
||||
// Parses ipv4 Part.
|
||||
static int LwipParseIpv4Part(const char** cursor, uint8_t* out) {
|
||||
const char* p = *cursor;
|
||||
uint32_t value = 0;
|
||||
@@ -175,6 +178,7 @@ static int LwipParseIpv4Part(const char** cursor, uint8_t* out) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Parses ipv4.
|
||||
static int LwipParseIpv4(const char* text, ip4_addr_t* out) {
|
||||
if (!text || !out)
|
||||
return 0;
|
||||
@@ -205,6 +209,7 @@ static KSTATUS LwipGetSettingIpv4(const char* section, const char* key,
|
||||
return STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
// Copies section Name.
|
||||
static KSTATUS LwipCopySectionName(char* out, size_t outSize, const char* prefix, const char* name) {
|
||||
if (!out || !outSize || !prefix || !name)
|
||||
return STATUS_INVALID_PARAMETER;
|
||||
@@ -219,6 +224,7 @@ static KSTATUS LwipCopySectionName(char* out, size_t outSize, const char* prefix
|
||||
return STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
// Loads config From Section.
|
||||
static KSTATUS LwipLoadConfigFromSection(const char* section, LWIP_NETIF_CONFIG* cfg) {
|
||||
if (!section || !cfg)
|
||||
return STATUS_INVALID_PARAMETER;
|
||||
@@ -262,6 +268,7 @@ static KSTATUS LwipLoadConfigFromSection(const char* section, LWIP_NETIF_CONFIG*
|
||||
return STATUS_INVALID_PARAMETER;
|
||||
}
|
||||
|
||||
// Loads netif Config.
|
||||
static KSTATUS LwipLoadNetifConfig(const NET_DEVICE* device, LWIP_NETIF_CONFIG* cfg) {
|
||||
if (!device || !cfg)
|
||||
return STATUS_INVALID_PARAMETER;
|
||||
@@ -291,17 +298,20 @@ static KSTATUS LwipLoadNetifConfig(const NET_DEVICE* device, LWIP_NETIF_CONFIG*
|
||||
return STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
// Implements the socket Lock operation.
|
||||
static KSTATUS LwipSocketLock(LWIP_PROVIDER_SOCKET* s) {
|
||||
if (!s || !s->Lock)
|
||||
return STATUS_INVALID_PARAMETER;
|
||||
return KeWaitSemaphore(s->Lock);
|
||||
}
|
||||
|
||||
// Implements the socket Unlock operation.
|
||||
static void LwipSocketUnlock(LWIP_PROVIDER_SOCKET* s) {
|
||||
if (s && s->Lock)
|
||||
KeSignalSemaphore(s->Lock);
|
||||
}
|
||||
|
||||
// Implements the socket Callback operation.
|
||||
static void LwipSocketCallback(struct netconn* conn, enum netconn_evt evt, u16_t len) {
|
||||
(void)len;
|
||||
if (!conn)
|
||||
@@ -362,10 +372,12 @@ static KSTATUS LwipSocketAllocateWithConn(uint32_t domain, uint32_t type, uint32
|
||||
return STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
// Implements the socket Make Ip operation.
|
||||
static void LwipSocketMakeIp(const SYS_SOCKET_ADDRESS_IPV4_K* address, ip_addr_t* out) {
|
||||
IP_ADDR4(out, address->Address[0], address->Address[1], address->Address[2], address->Address[3]);
|
||||
}
|
||||
|
||||
// Implements the socket Fill Address operation.
|
||||
static void LwipSocketFillAddress(SYS_SOCKET_ADDRESS_IPV4_K* out, const ip_addr_t* ip, u16_t port) {
|
||||
if (!out)
|
||||
return;
|
||||
@@ -378,6 +390,7 @@ static void LwipSocketFillAddress(SYS_SOCKET_ADDRESS_IPV4_K* out, const ip_addr_
|
||||
out->Address[3] = (uint8_t)ip4_addr4(ip_2_ip4(ip));
|
||||
}
|
||||
|
||||
// Implements the low Level Output operation.
|
||||
static err_t LwipLowLevelOutput(struct netif* netif, struct pbuf* p) {
|
||||
LWIP_BINDING* b = (LWIP_BINDING*)netif->state;
|
||||
if (!b || !b->Device || !p || p->tot_len < NET_ETHERNET_HEADER_LENGTH)
|
||||
@@ -405,6 +418,7 @@ static err_t LwipLowLevelOutput(struct netif* netif, struct pbuf* p) {
|
||||
return KSUCCESS(st) ? ERR_OK : ERR_IF;
|
||||
}
|
||||
|
||||
// Implements the netif Init operation.
|
||||
static err_t LwipNetifInit(struct netif* netif) {
|
||||
LWIP_BINDING* b = (LWIP_BINDING*)netif->state;
|
||||
if (!b || !b->Device)
|
||||
@@ -422,6 +436,7 @@ static err_t LwipNetifInit(struct netif* netif) {
|
||||
return ERR_OK;
|
||||
}
|
||||
|
||||
// Attaches callback.
|
||||
static void LwipAttachCallback(void* arg) {
|
||||
LWIP_ATTACH_CTX* ctx = (LWIP_ATTACH_CTX*)arg;
|
||||
LWIP_BINDING* b = ctx->Binding;
|
||||
@@ -490,11 +505,13 @@ static void LwipAttachCallback(void* arg) {
|
||||
KeSignalSemaphore(ctx->Done);
|
||||
}
|
||||
|
||||
// Initializes done.
|
||||
static void LwipInitDone(void* arg) {
|
||||
SEMAPHORE* sem = (SEMAPHORE*)arg;
|
||||
KeSignalSemaphore(sem);
|
||||
}
|
||||
|
||||
// Implements the stack Initialize operation.
|
||||
static KSTATUS LwipStackInitialize(const HAL_API* hal) {
|
||||
if (gLwipInitialized)
|
||||
return STATUS_SUCCESS;
|
||||
@@ -519,6 +536,7 @@ static KSTATUS LwipStackInitialize(const HAL_API* hal) {
|
||||
return STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
// Implements the provider Attach Device operation.
|
||||
static KSTATUS LwipProviderAttachDevice(NET_DEVICE* device) {
|
||||
if (!gLwipInitialized)
|
||||
return STATUS_NOT_SUPPORTED;
|
||||
@@ -576,6 +594,7 @@ static KSTATUS LwipProviderAttachDevice(NET_DEVICE* device) {
|
||||
return STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
// Implements the provider Receive Frame operation.
|
||||
static KSTATUS LwipProviderReceiveFrame(NET_DEVICE* device, const void* frame, size_t length) {
|
||||
if (!gLwipInitialized)
|
||||
return STATUS_NOT_SUPPORTED;
|
||||
@@ -609,6 +628,7 @@ static KSTATUS LwipProviderReceiveFrame(NET_DEVICE* device, const void* frame, s
|
||||
return STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
// Implements the provider Socket Create operation.
|
||||
static KSTATUS LwipProviderSocketCreate(uint32_t domain, uint32_t type, uint32_t protocol, void** outSocket) {
|
||||
if (!outSocket)
|
||||
return STATUS_INVALID_PARAMETER;
|
||||
@@ -655,6 +675,7 @@ static KSTATUS LwipProviderSocketCreate(uint32_t domain, uint32_t type, uint32_t
|
||||
return STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
// Implements the provider Socket Close operation.
|
||||
static void LwipProviderSocketClose(void* socket) {
|
||||
LWIP_PROVIDER_SOCKET* s = (LWIP_PROVIDER_SOCKET*)socket;
|
||||
if (!s)
|
||||
@@ -681,6 +702,7 @@ static void LwipProviderSocketClose(void* socket) {
|
||||
MmFreePool(s);
|
||||
}
|
||||
|
||||
// Implements the provider Bind operation.
|
||||
static KSTATUS LwipProviderBind(void* socket, const SYS_SOCKET_ADDRESS_IPV4_K* address, size_t addressSize) {
|
||||
LWIP_PROVIDER_SOCKET* s = (LWIP_PROVIDER_SOCKET*)socket;
|
||||
if (!s || !address)
|
||||
@@ -711,6 +733,7 @@ static KSTATUS LwipProviderBind(void* socket, const SYS_SOCKET_ADDRESS_IPV4_K* a
|
||||
return LwipErrToStatus(err);
|
||||
}
|
||||
|
||||
// Implements the provider Listen operation.
|
||||
static KSTATUS LwipProviderListen(void* socket, uint32_t backlog) {
|
||||
LWIP_PROVIDER_SOCKET* s = (LWIP_PROVIDER_SOCKET*)socket;
|
||||
if (!s)
|
||||
@@ -739,6 +762,7 @@ static KSTATUS LwipProviderListen(void* socket, uint32_t backlog) {
|
||||
return LwipErrToStatus(err);
|
||||
}
|
||||
|
||||
// Implements the provider Accept operation.
|
||||
static KSTATUS LwipProviderAccept(void* socket, void** acceptedOut, SYS_SOCKET_ADDRESS_IPV4_K* remoteOut) {
|
||||
LWIP_PROVIDER_SOCKET* s = (LWIP_PROVIDER_SOCKET*)socket;
|
||||
if (!s || !acceptedOut)
|
||||
@@ -795,6 +819,7 @@ static KSTATUS LwipProviderAccept(void* socket, void** acceptedOut, SYS_SOCKET_A
|
||||
return STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
// Implements the provider Connect operation.
|
||||
static KSTATUS LwipProviderConnect(void* socket, const SYS_SOCKET_ADDRESS_IPV4_K* address, size_t addressSize) {
|
||||
LWIP_PROVIDER_SOCKET* s = (LWIP_PROVIDER_SOCKET*)socket;
|
||||
if (!s || !address)
|
||||
@@ -830,6 +855,7 @@ static KSTATUS LwipProviderConnect(void* socket, const SYS_SOCKET_ADDRESS_IPV4_K
|
||||
return LwipErrToStatus(err);
|
||||
}
|
||||
|
||||
// Implements the provider Send operation.
|
||||
static KSTATUS LwipProviderSend(void* socket, const void* buffer, size_t length, uint32_t flags, size_t* sentOut) {
|
||||
LWIP_PROVIDER_SOCKET* s = (LWIP_PROVIDER_SOCKET*)socket;
|
||||
if (sentOut)
|
||||
@@ -913,6 +939,7 @@ static KSTATUS LwipReceiveFromBuffered(LWIP_PROVIDER_SOCKET* s, uint8_t* out,
|
||||
return STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
// Implements the provider Receive operation.
|
||||
static KSTATUS LwipProviderReceive(void* socket, void* buffer, size_t length, uint32_t flags, size_t* receivedOut) {
|
||||
LWIP_PROVIDER_SOCKET* s = (LWIP_PROVIDER_SOCKET*)socket;
|
||||
if (receivedOut)
|
||||
@@ -1103,6 +1130,7 @@ static KSTATUS LwipProviderReceiveFrom(void* socket, void* buffer, size_t length
|
||||
return LwipErrToStatus(err);
|
||||
}
|
||||
|
||||
// Implements the provider Set Option operation.
|
||||
static KSTATUS LwipProviderSetOption(void* socket, uint32_t option, const void* value, size_t valueSize) {
|
||||
LWIP_PROVIDER_SOCKET* s = (LWIP_PROVIDER_SOCKET*)socket;
|
||||
if (!s || !value)
|
||||
@@ -1141,6 +1169,7 @@ static KSTATUS LwipProviderSetOption(void* socket, uint32_t option, const void*
|
||||
return STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
// Implements the provider Get Option operation.
|
||||
static KSTATUS LwipProviderGetOption(void* socket, uint32_t option, void* value, size_t valueSize, size_t* actualSizeOut) {
|
||||
LWIP_PROVIDER_SOCKET* s = (LWIP_PROVIDER_SOCKET*)socket;
|
||||
if (actualSizeOut)
|
||||
@@ -1177,6 +1206,7 @@ static KSTATUS LwipProviderGetOption(void* socket, uint32_t option, void* value,
|
||||
return STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
// Implements the provider Poll operation.
|
||||
static KSTATUS LwipProviderPoll(void* socket, uint32_t events, uint32_t* returnedEventsOut) {
|
||||
LWIP_PROVIDER_SOCKET* s = (LWIP_PROVIDER_SOCKET*)socket;
|
||||
if (!s || !returnedEventsOut)
|
||||
@@ -1215,6 +1245,7 @@ static KSTATUS LwipProviderPoll(void* socket, uint32_t events, uint32_t* returne
|
||||
return STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
// Implements the provider Shutdown operation.
|
||||
static KSTATUS LwipProviderShutdown(void* socket, uint32_t how) {
|
||||
LWIP_PROVIDER_SOCKET* s = (LWIP_PROVIDER_SOCKET*)socket;
|
||||
if (!s)
|
||||
@@ -1240,6 +1271,7 @@ static KSTATUS LwipProviderShutdown(void* socket, uint32_t how) {
|
||||
return LwipErrToStatus(err);
|
||||
}
|
||||
|
||||
// Implements the provider Resolve Host Ipv4 operation.
|
||||
static KSTATUS LwipProviderResolveHostIpv4(const char* name, SYS_SOCKET_ADDRESS_IPV4_K* addressOut) {
|
||||
if (!name || !addressOut)
|
||||
return STATUS_INVALID_PARAMETER;
|
||||
@@ -1263,6 +1295,7 @@ static KSTATUS LwipProviderResolveHostIpv4(const char* name, SYS_SOCKET_ADDRESS_
|
||||
return STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
// Implements the driver Entry operation.
|
||||
KSTATUS DriverEntry(const HAL_API* hal) {
|
||||
DbgPrint("lwIP: driver loading...\n");
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
// lwip_sys_arch.c - lwIP OS abstraction layer for the kernel.
|
||||
// lwIP OS-abstraction layer. This file maps lwIP synchronization and timing primitives onto kernel services.
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include <stdbool.h>
|
||||
@@ -42,18 +43,20 @@ struct sys_mbox {
|
||||
|
||||
|
||||
static uint64_t (*gLwipQueryTicksUs)(void) = 0;
|
||||
static uint32_t gRandState = 0x4b4e4554u; /* 'KNET' */
|
||||
static uint32_t gRandState = 0x4b4e4554u; // 'KNET'
|
||||
|
||||
// Implements the port Set Timer Query operation.
|
||||
void LwipPortSetTimerQuery(uint64_t (*query_ticks_us)(void)) {
|
||||
gLwipQueryTicksUs = query_ticks_us;
|
||||
}
|
||||
|
||||
unsigned int lwip_port_rand(void) {
|
||||
/* Tiny deterministic LCG. Good enough for ephemeral port randomisation. */
|
||||
// Tiny deterministic LCG. Good enough for ephemeral port randomisation.
|
||||
gRandState = gRandState * 1664525u + 1013904223u;
|
||||
return gRandState;
|
||||
}
|
||||
|
||||
// Implements the sys init operation.
|
||||
void sys_init(void) {
|
||||
}
|
||||
|
||||
@@ -67,22 +70,20 @@ u32_t sys_jiffies(void) {
|
||||
return sys_now();
|
||||
}
|
||||
|
||||
/*
|
||||
* lwIP's core/sys.c provides sys_msleep() when it is not a macro. Do not
|
||||
* define it here as well, or MSVC links both sys.obj and lwip_sys_arch.obj
|
||||
* with a duplicate sys_msleep symbol.
|
||||
*/
|
||||
// lwIP's core/sys.c provides sys_msleep() when it is not a macro. Do not
|
||||
// define it here as well, or MSVC links both sys.obj and lwip_sys_arch.obj
|
||||
// with a duplicate sys_msleep symbol.
|
||||
|
||||
/*
|
||||
* First bring-up errno storage for lwIP's socket/netdb compatibility layer.
|
||||
* This is global, not per-thread; the kernel socket-object wrapper should
|
||||
* eventually translate lwIP errors to KSTATUS instead of exposing this.
|
||||
*/
|
||||
// fuckass microsoft
|
||||
// First bring-up errno storage for lwIP's socket/netdb compatibility layer.
|
||||
// This is global, not per-thread; the kernel socket-object wrapper should
|
||||
// eventually translate lwIP errors to KSTATUS instead of exposing this.
|
||||
|
||||
// fuckass microsoft.
|
||||
#ifdef errno
|
||||
#undef errno
|
||||
#endif
|
||||
int errno;
|
||||
// Implements the errno operation.
|
||||
int* __cdecl _errno(void) { return &errno; }
|
||||
|
||||
err_t sys_sem_new(sys_sem_t* sem, u8_t count) {
|
||||
@@ -105,6 +106,7 @@ err_t sys_sem_new(sys_sem_t* sem, u8_t count) {
|
||||
return ERR_OK;
|
||||
}
|
||||
|
||||
// Implements the sys sem signal operation.
|
||||
void sys_sem_signal(sys_sem_t* sem) {
|
||||
if (sem && *sem && (*sem)->Sem)
|
||||
KeSignalSemaphore((*sem)->Sem);
|
||||
@@ -132,6 +134,7 @@ u32_t sys_arch_sem_wait(sys_sem_t* sem, u32_t timeout) {
|
||||
}
|
||||
}
|
||||
|
||||
// Implements the sys sem free operation.
|
||||
void sys_sem_free(sys_sem_t* sem) {
|
||||
if (!sem || !*sem)
|
||||
return;
|
||||
@@ -161,16 +164,19 @@ err_t sys_mutex_new(sys_mutex_t* mutex) {
|
||||
return ERR_OK;
|
||||
}
|
||||
|
||||
// Implements the sys mutex lock operation.
|
||||
void sys_mutex_lock(sys_mutex_t* mutex) {
|
||||
if (mutex && *mutex && (*mutex)->Sem)
|
||||
(void)KeWaitSemaphore((*mutex)->Sem);
|
||||
}
|
||||
|
||||
// Implements the sys mutex unlock operation.
|
||||
void sys_mutex_unlock(sys_mutex_t* mutex) {
|
||||
if (mutex && *mutex && (*mutex)->Sem)
|
||||
KeSignalSemaphore((*mutex)->Sem);
|
||||
}
|
||||
|
||||
// Implements the sys mutex free operation.
|
||||
void sys_mutex_free(sys_mutex_t* mutex) {
|
||||
if (!mutex || !*mutex)
|
||||
return;
|
||||
@@ -180,6 +186,7 @@ void sys_mutex_free(sys_mutex_t* mutex) {
|
||||
*mutex = 0;
|
||||
}
|
||||
|
||||
// Implements the mbox Try Post operation.
|
||||
static err_t LwipMboxTryPost(sys_mbox_t* mbox, void* msg) {
|
||||
if (!mbox || !*mbox || !(*mbox)->Valid)
|
||||
return ERR_ARG;
|
||||
@@ -232,8 +239,9 @@ err_t sys_mbox_new(sys_mbox_t* mbox, int size) {
|
||||
return ERR_OK;
|
||||
}
|
||||
|
||||
// Implements the sys mbox post operation.
|
||||
void sys_mbox_post(sys_mbox_t* mbox, void* msg) {
|
||||
/* lwIP promises this is not called from ISR context. */
|
||||
// lwIP promises this is not called from ISR context.
|
||||
while (LwipMboxTryPost(mbox, msg) != ERR_OK)
|
||||
KeYield();
|
||||
}
|
||||
@@ -246,6 +254,7 @@ err_t sys_mbox_trypost_fromisr(sys_mbox_t* mbox, void* msg) {
|
||||
return LwipMboxTryPost(mbox, msg);
|
||||
}
|
||||
|
||||
// Implements the mbox Pop After Item operation.
|
||||
static u32_t LwipMboxPopAfterItem(sys_mbox_t* mbox, void** msg, u32_t waited) {
|
||||
struct sys_mbox* m = *mbox;
|
||||
uintptr_t flags = KeAcquireSpinlock(&m->Lock);
|
||||
@@ -299,6 +308,7 @@ u32_t sys_arch_mbox_tryfetch(sys_mbox_t* mbox, void** msg) {
|
||||
return LwipMboxPopAfterItem(mbox, msg, 0);
|
||||
}
|
||||
|
||||
// Implements the sys mbox free operation.
|
||||
void sys_mbox_free(sys_mbox_t* mbox) {
|
||||
if (!mbox || !*mbox)
|
||||
return;
|
||||
@@ -327,18 +337,17 @@ sys_thread_t sys_thread_new(const char* name, lwip_thread_fn thread, void* arg,
|
||||
else if (prio <= 0)
|
||||
kp = PriorityLow;
|
||||
|
||||
/*
|
||||
* lwIP thread entries already use the same ABI as kernel worker threads:
|
||||
*
|
||||
* void entry(void* arg)
|
||||
*
|
||||
* Do not bounce through a heap-allocated thunk. The first scaffold used a
|
||||
* tiny { function, argument } heap block and a wrapper, but an early boot
|
||||
* crash showed the wrapper sometimes loaded a bogus function pointer and
|
||||
* jumped into lwIP's .rdata strings. Passing the lwIP entry directly keeps
|
||||
* the initial thread stack simple: KeKernelThreadEntry calls exactly the
|
||||
* function pointer that lwIP supplied, with exactly the argument it supplied.
|
||||
*/
|
||||
// lwIP thread entries already use the same ABI as kernel worker threads:
|
||||
|
||||
// void entry(void* arg)
|
||||
|
||||
// Do not bounce through a heap-allocated thunk. The first scaffold used a
|
||||
// tiny { function, argument } heap block and a wrapper, but an early boot
|
||||
// crash showed the wrapper sometimes loaded a bogus function pointer and
|
||||
// jumped into lwIP's .rdata strings. Passing the lwIP entry directly keeps
|
||||
// the initial thread stack simple: KeKernelThreadEntry calls exactly the
|
||||
// function pointer that lwIP supplied, with exactly the argument it supplied.
|
||||
|
||||
THREAD* t = 0;
|
||||
KSTATUS st = KeCreateKernelThread((uintptr_t)thread,
|
||||
(uintptr_t)arg,
|
||||
@@ -356,6 +365,7 @@ sys_prot_t sys_arch_protect(void) {
|
||||
return (sys_prot_t)flags;
|
||||
}
|
||||
|
||||
// Implements the sys arch unprotect operation.
|
||||
void sys_arch_unprotect(sys_prot_t pval) {
|
||||
if (((uintptr_t)pval) & 0x200u)
|
||||
_enable();
|
||||
|
||||
Reference in new issue
Block a user