2010-09-21 06:11:24 +00:00
|
|
|
#include "lwip/sys.h"
|
|
|
|
#include "lwip/tcpip.h"
|
|
|
|
|
|
|
|
#include "rosip.h"
|
|
|
|
|
|
|
|
#include <debug.h>
|
|
|
|
|
2011-05-24 18:05:51 +00:00
|
|
|
static const char * const tcp_state_str[] = {
|
|
|
|
"CLOSED",
|
|
|
|
"LISTEN",
|
|
|
|
"SYN_SENT",
|
|
|
|
"SYN_RCVD",
|
|
|
|
"ESTABLISHED",
|
|
|
|
"FIN_WAIT_1",
|
|
|
|
"FIN_WAIT_2",
|
|
|
|
"CLOSE_WAIT",
|
|
|
|
"CLOSING",
|
|
|
|
"LAST_ACK",
|
|
|
|
"TIME_WAIT"
|
|
|
|
};
|
|
|
|
|
2010-09-21 06:11:24 +00:00
|
|
|
/* The way that lwIP does multi-threading is really not ideal for our purposes but
|
|
|
|
* we best go along with it unless we want another unstable TCP library. lwIP uses
|
|
|
|
* a thread called the "tcpip thread" which is the only one allowed to call raw API
|
|
|
|
* functions. Since this is the case, for each of our LibTCP* functions, we queue a request
|
|
|
|
* for a callback to "tcpip thread" which calls our LibTCP*Callback functions. Yes, this is
|
|
|
|
* a lot of unnecessary thread swapping and it could definitely be faster, but I don't want
|
|
|
|
* to going messing around in lwIP because I have no desire to create another mess like oskittcp */
|
|
|
|
|
|
|
|
extern KEVENT TerminationEvent;
|
|
|
|
|
|
|
|
static
|
|
|
|
BOOLEAN
|
|
|
|
WaitForEventSafely(PRKEVENT Event)
|
|
|
|
{
|
|
|
|
PVOID WaitObjects[] = {Event, &TerminationEvent};
|
|
|
|
|
|
|
|
if (KeWaitForMultipleObjects(2,
|
|
|
|
WaitObjects,
|
|
|
|
WaitAny,
|
|
|
|
Executive,
|
|
|
|
KernelMode,
|
|
|
|
FALSE,
|
|
|
|
NULL,
|
|
|
|
NULL) == STATUS_WAIT_0)
|
|
|
|
{
|
|
|
|
/* Signalled by the caller's event */
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
else /* if KeWaitForMultipleObjects() == STATUS_WAIT_1 */
|
|
|
|
{
|
|
|
|
/* Signalled by our termination event */
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
err_t
|
|
|
|
InternalSendEventHandler(void *arg, struct tcp_pcb *pcb, u16_t space)
|
|
|
|
{
|
2011-06-01 13:27:35 +00:00
|
|
|
DbgPrint("[lwIP, InternalSendEventHandler] SendEvent (0x%x, 0x%x, %d)\n",
|
2011-05-24 18:05:51 +00:00
|
|
|
arg, pcb, (unsigned int)space);
|
2010-09-21 06:11:24 +00:00
|
|
|
|
|
|
|
/* Make sure the socket didn't get closed */
|
|
|
|
if (!arg) return ERR_OK;
|
|
|
|
|
|
|
|
TCPSendEventHandler(arg, space);
|
2011-06-22 15:32:46 +00:00
|
|
|
|
|
|
|
DbgPrint("[lwIP, InternalSendEventHandler] Done\n");
|
2010-09-21 06:11:24 +00:00
|
|
|
|
|
|
|
return ERR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
err_t
|
|
|
|
InternalRecvEventHandler(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
|
|
|
|
{
|
|
|
|
u32_t len;
|
|
|
|
|
2011-06-22 15:32:46 +00:00
|
|
|
DbgPrint("[lwIP, InternalRecvEventHandler] RecvEvent (0x%x, pcb = 0x%x, pbuf = 0x%x, err = %d)\n",
|
2011-05-24 18:05:51 +00:00
|
|
|
arg, pcb, p, (unsigned int)err);
|
2010-09-21 06:11:24 +00:00
|
|
|
|
|
|
|
/* Make sure the socket didn't get closed */
|
|
|
|
if (!arg)
|
|
|
|
{
|
2011-06-22 15:32:46 +00:00
|
|
|
if (p)
|
|
|
|
pbuf_free(p);
|
|
|
|
|
|
|
|
DbgPrint("[lwIP, InternalRecvEventHandler] Done ERR_OK 0 - socket got closed on us\n");
|
|
|
|
|
2010-09-21 06:11:24 +00:00
|
|
|
return ERR_OK;
|
|
|
|
}
|
|
|
|
|
2011-06-12 19:21:56 +00:00
|
|
|
if (p)
|
2010-09-21 06:11:24 +00:00
|
|
|
{
|
2011-06-01 13:27:35 +00:00
|
|
|
DbgPrint("[lwIP, InternalRecvEventHandler] RECV - p:0x%x p->payload:0x%x p->len:%d p->tot_len:%d\n",
|
2011-05-24 18:05:51 +00:00
|
|
|
p, p->payload, p->len, p->tot_len);
|
2010-09-21 06:11:24 +00:00
|
|
|
|
2011-06-12 19:21:56 +00:00
|
|
|
len = TCPRecvEventHandler(arg, p);
|
|
|
|
if (len == p->tot_len)
|
|
|
|
{
|
|
|
|
tcp_recved(pcb, len);
|
|
|
|
|
|
|
|
pbuf_free(p);
|
|
|
|
|
2011-06-22 15:32:46 +00:00
|
|
|
DbgPrint("[lwIP, InternalRecvEventHandler] Done ERR_OK 1\n");
|
|
|
|
|
2011-06-12 19:21:56 +00:00
|
|
|
return ERR_OK;
|
|
|
|
}
|
|
|
|
else if (len != 0)
|
2010-09-21 06:11:24 +00:00
|
|
|
{
|
2011-06-12 19:21:56 +00:00
|
|
|
DbgPrint("UNTESTED CASE: NOT ALL DATA TAKEN! EXTRA DATA MAY BE LOST!\n");
|
|
|
|
|
|
|
|
tcp_recved(pcb, len);
|
|
|
|
|
|
|
|
/* Possible memory leak of pbuf here? */
|
2011-06-22 15:32:46 +00:00
|
|
|
DbgPrint("[lwIP, InternalRecvEventHandler] Done ERR_OK 2\n");
|
2011-06-12 19:21:56 +00:00
|
|
|
|
|
|
|
return ERR_OK;
|
2010-09-21 06:11:24 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-06-12 19:21:56 +00:00
|
|
|
/* We want lwIP to store the pbuf on its queue for later */
|
2011-06-22 15:32:46 +00:00
|
|
|
DbgPrint("[lwIP, InternalRecvEventHandler] Done ERR_TIMEOUT\n");
|
2011-06-12 19:21:56 +00:00
|
|
|
return ERR_TIMEOUT;
|
2010-09-21 06:11:24 +00:00
|
|
|
}
|
|
|
|
}
|
2011-06-12 19:21:56 +00:00
|
|
|
else if (err == ERR_OK)
|
|
|
|
{
|
2011-07-10 00:38:29 +00:00
|
|
|
/* Complete pending reads with 0 bytes to indicate a graceful closure,
|
|
|
|
* but note that send is still possible in this state so we don't close the
|
|
|
|
* whole socket here (by calling tcp_close()) as that would violate TCP specs
|
|
|
|
*/
|
2011-06-12 19:21:56 +00:00
|
|
|
TCPFinEventHandler(arg, ERR_OK);
|
|
|
|
}
|
2011-06-22 15:32:46 +00:00
|
|
|
|
|
|
|
DbgPrint("[lwIP, InternalRecvEventHandler] Done ERR_OK 3\n");
|
2010-09-21 06:11:24 +00:00
|
|
|
|
|
|
|
return ERR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
err_t
|
|
|
|
InternalAcceptEventHandler(void *arg, struct tcp_pcb *newpcb, err_t err)
|
|
|
|
{
|
2011-06-20 12:59:27 +00:00
|
|
|
DbgPrint("[lwIP, InternalAcceptEventHandler] AcceptEvent arg = 0x%x, newpcb = 0x%x, err = %d\n",
|
2011-05-24 18:05:51 +00:00
|
|
|
arg, newpcb, (unsigned int)err);
|
2010-09-21 06:11:24 +00:00
|
|
|
|
|
|
|
/* Make sure the socket didn't get closed */
|
2011-05-24 18:05:51 +00:00
|
|
|
if (!arg)
|
|
|
|
return ERR_ABRT;
|
|
|
|
|
2011-06-01 13:27:35 +00:00
|
|
|
DbgPrint("[lwIP, InternalAcceptEventHandler] newpcb->state = %s\n",
|
2011-05-24 18:05:51 +00:00
|
|
|
tcp_state_str[newpcb->state]);
|
2010-09-21 06:11:24 +00:00
|
|
|
|
|
|
|
TCPAcceptEventHandler(arg, newpcb);
|
|
|
|
|
|
|
|
/* Set in LibTCPAccept (called from TCPAcceptEventHandler) */
|
|
|
|
if (newpcb->callback_arg)
|
|
|
|
return ERR_OK;
|
|
|
|
else
|
|
|
|
return ERR_ABRT;
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
err_t
|
|
|
|
InternalConnectEventHandler(void *arg, struct tcp_pcb *pcb, err_t err)
|
|
|
|
{
|
2011-06-22 15:32:46 +00:00
|
|
|
DbgPrint("[lwIP, InternalConnectEventHandler] ConnectEvent (0x%x, pcb = 0x%x, err = %d)\n",
|
2011-05-24 18:05:51 +00:00
|
|
|
arg, pcb, (unsigned int)err);
|
2010-09-21 06:11:24 +00:00
|
|
|
|
|
|
|
/* Make sure the socket didn't get closed */
|
2011-05-24 18:05:51 +00:00
|
|
|
if (!arg)
|
|
|
|
return ERR_OK;
|
2010-09-21 06:11:24 +00:00
|
|
|
|
|
|
|
TCPConnectEventHandler(arg, err);
|
2011-06-22 15:32:46 +00:00
|
|
|
|
|
|
|
DbgPrint("[lwIP, InternalConnectEventHandler] Done\n");
|
2010-09-21 06:11:24 +00:00
|
|
|
|
|
|
|
return ERR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
void
|
|
|
|
InternalErrorEventHandler(void *arg, err_t err)
|
|
|
|
{
|
2011-06-01 13:27:35 +00:00
|
|
|
DbgPrint("[lwIP, InternalErrorEventHandler] ErrorEvent(0x%x, %d)\n",
|
|
|
|
arg, (unsigned int)err);
|
2010-09-21 06:11:24 +00:00
|
|
|
|
|
|
|
/* Make sure the socket didn't get closed */
|
|
|
|
if (!arg) return;
|
|
|
|
|
|
|
|
TCPFinEventHandler(arg, err);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct socket_callback_msg
|
|
|
|
{
|
|
|
|
/* Synchronization */
|
|
|
|
KEVENT Event;
|
|
|
|
|
|
|
|
/* Input */
|
|
|
|
PVOID Arg;
|
|
|
|
|
|
|
|
/* Output */
|
2011-06-20 15:59:49 +00:00
|
|
|
struct tcp_pcb *NewPcb;
|
2010-09-21 06:11:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static
|
|
|
|
void
|
|
|
|
LibTCPSocketCallback(void *arg)
|
|
|
|
{
|
|
|
|
struct socket_callback_msg *msg = arg;
|
|
|
|
|
|
|
|
ASSERT(msg);
|
2011-06-20 12:59:27 +00:00
|
|
|
|
|
|
|
DbgPrint("[lwIP, LibTCPSocketCallback] Called\n");
|
2010-09-21 06:11:24 +00:00
|
|
|
|
|
|
|
msg->NewPcb = tcp_new();
|
2011-06-20 12:59:27 +00:00
|
|
|
|
|
|
|
DbgPrint("[lwIP, LibTCPSocketCallback] Assigned new pcb = 0x%x\n", msg->NewPcb);
|
2010-09-21 06:11:24 +00:00
|
|
|
|
|
|
|
if (msg->NewPcb)
|
|
|
|
{
|
2011-06-20 15:59:49 +00:00
|
|
|
tcp_arg(msg->NewPcb, msg->Arg);
|
|
|
|
tcp_err(msg->NewPcb, InternalErrorEventHandler);
|
2010-09-21 06:11:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
KeSetEvent(&msg->Event, IO_NO_INCREMENT, FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct tcp_pcb *
|
|
|
|
LibTCPSocket(void *arg)
|
|
|
|
{
|
|
|
|
struct socket_callback_msg *msg = ExAllocatePool(NonPagedPool, sizeof(struct socket_callback_msg));
|
2011-06-20 15:59:49 +00:00
|
|
|
struct tcp_pcb *ret;
|
2011-06-20 12:59:27 +00:00
|
|
|
|
|
|
|
DbgPrint("[lwIP, LibTCPSocket] Called\n");
|
2010-09-21 06:11:24 +00:00
|
|
|
|
|
|
|
if (msg)
|
|
|
|
{
|
|
|
|
KeInitializeEvent(&msg->Event, NotificationEvent, FALSE);
|
|
|
|
msg->Arg = arg;
|
|
|
|
|
|
|
|
tcpip_callback_with_block(LibTCPSocketCallback, msg, 1);
|
|
|
|
|
|
|
|
if (WaitForEventSafely(&msg->Event))
|
|
|
|
ret = msg->NewPcb;
|
|
|
|
else
|
|
|
|
ret = NULL;
|
|
|
|
|
2011-06-20 12:59:27 +00:00
|
|
|
DbgPrint("[lwIP, LibTCPSocket] Connection( 0x%x )->SocketContext = pcb( 0x%x )\n", arg, ret);
|
|
|
|
|
|
|
|
DbgPrint("[lwIP, LibTCPSocket] Done\n");
|
2010-09-21 06:11:24 +00:00
|
|
|
|
|
|
|
ExFreePool(msg);
|
|
|
|
|
2011-06-20 15:59:49 +00:00
|
|
|
return ret;
|
2010-09-21 06:11:24 +00:00
|
|
|
}
|
2011-06-20 12:59:27 +00:00
|
|
|
|
|
|
|
DbgPrint("[lwIP, LibTCPSocket] Done\n");
|
2010-09-21 06:11:24 +00:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct bind_callback_msg
|
|
|
|
{
|
|
|
|
/* Synchronization */
|
|
|
|
KEVENT Event;
|
|
|
|
|
|
|
|
/* Input */
|
|
|
|
struct tcp_pcb *Pcb;
|
|
|
|
struct ip_addr *IpAddress;
|
|
|
|
u16_t Port;
|
|
|
|
|
|
|
|
/* Output */
|
|
|
|
err_t Error;
|
|
|
|
};
|
|
|
|
|
|
|
|
static
|
|
|
|
void
|
|
|
|
LibTCPBindCallback(void *arg)
|
|
|
|
{
|
|
|
|
struct bind_callback_msg *msg = arg;
|
|
|
|
|
|
|
|
ASSERT(msg);
|
|
|
|
|
|
|
|
msg->Error = tcp_bind(msg->Pcb, msg->IpAddress, ntohs(msg->Port));
|
|
|
|
|
|
|
|
KeSetEvent(&msg->Event, IO_NO_INCREMENT, FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
err_t
|
|
|
|
LibTCPBind(struct tcp_pcb *pcb, struct ip_addr *ipaddr, u16_t port)
|
|
|
|
{
|
|
|
|
struct bind_callback_msg *msg;
|
|
|
|
err_t ret;
|
|
|
|
|
|
|
|
if (!pcb)
|
|
|
|
return ERR_CLSD;
|
2011-06-01 13:27:35 +00:00
|
|
|
|
|
|
|
DbgPrint("[lwIP, LibTCPBind] Called\n");
|
2010-09-21 06:11:24 +00:00
|
|
|
|
|
|
|
msg = ExAllocatePool(NonPagedPool, sizeof(struct bind_callback_msg));
|
|
|
|
if (msg)
|
|
|
|
{
|
|
|
|
KeInitializeEvent(&msg->Event, NotificationEvent, FALSE);
|
|
|
|
msg->Pcb = pcb;
|
|
|
|
msg->IpAddress = ipaddr;
|
|
|
|
msg->Port = port;
|
|
|
|
|
|
|
|
tcpip_callback_with_block(LibTCPBindCallback, msg, 1);
|
|
|
|
|
|
|
|
if (WaitForEventSafely(&msg->Event))
|
|
|
|
ret = msg->Error;
|
|
|
|
else
|
|
|
|
ret = ERR_CLSD;
|
|
|
|
|
2011-06-01 13:27:35 +00:00
|
|
|
DbgPrint("[lwIP, LibTCPBind] pcb = 0x%x\n", pcb);
|
|
|
|
|
|
|
|
DbgPrint("[lwIP, LibTCPBind] Done\n");
|
2010-09-21 06:11:24 +00:00
|
|
|
|
|
|
|
ExFreePool(msg);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ERR_MEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct listen_callback_msg
|
|
|
|
{
|
|
|
|
/* Synchronization */
|
|
|
|
KEVENT Event;
|
|
|
|
|
|
|
|
/* Input */
|
|
|
|
struct tcp_pcb *Pcb;
|
|
|
|
u8_t Backlog;
|
|
|
|
|
|
|
|
/* Output */
|
|
|
|
struct tcp_pcb *NewPcb;
|
|
|
|
};
|
|
|
|
|
|
|
|
static
|
|
|
|
void
|
|
|
|
LibTCPListenCallback(void *arg)
|
|
|
|
{
|
|
|
|
struct listen_callback_msg *msg = arg;
|
|
|
|
|
|
|
|
ASSERT(msg);
|
2011-06-01 13:27:35 +00:00
|
|
|
|
|
|
|
DbgPrint("[lwIP, LibTCPListenCallback] Called\n");
|
2011-06-20 14:49:58 +00:00
|
|
|
|
2010-09-21 06:11:24 +00:00
|
|
|
msg->NewPcb = tcp_listen_with_backlog(msg->Pcb, msg->Backlog);
|
|
|
|
|
|
|
|
if (msg->NewPcb)
|
|
|
|
{
|
|
|
|
tcp_accept(msg->NewPcb, InternalAcceptEventHandler);
|
|
|
|
}
|
2011-06-01 13:27:35 +00:00
|
|
|
|
|
|
|
DbgPrint("[lwIP, LibTCPListenCallback] Done\n");
|
2010-09-21 06:11:24 +00:00
|
|
|
|
|
|
|
KeSetEvent(&msg->Event, IO_NO_INCREMENT, FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct tcp_pcb *
|
|
|
|
LibTCPListen(struct tcp_pcb *pcb, u8_t backlog)
|
|
|
|
{
|
|
|
|
struct listen_callback_msg *msg;
|
|
|
|
void *ret;
|
2011-06-01 13:27:35 +00:00
|
|
|
|
2011-06-20 12:59:27 +00:00
|
|
|
DbgPrint("[lwIP, LibTCPListen] Called on pcb = 0x%x\n", pcb);
|
2010-09-21 06:11:24 +00:00
|
|
|
|
|
|
|
if (!pcb)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
msg = ExAllocatePool(NonPagedPool, sizeof(struct listen_callback_msg));
|
|
|
|
if (msg)
|
|
|
|
{
|
|
|
|
KeInitializeEvent(&msg->Event, NotificationEvent, FALSE);
|
|
|
|
msg->Pcb = pcb;
|
|
|
|
msg->Backlog = backlog;
|
|
|
|
|
|
|
|
tcpip_callback_with_block(LibTCPListenCallback, msg, 1);
|
|
|
|
|
|
|
|
if (WaitForEventSafely(&msg->Event))
|
|
|
|
ret = msg->NewPcb;
|
|
|
|
else
|
|
|
|
ret = NULL;
|
|
|
|
|
2011-06-20 12:59:27 +00:00
|
|
|
DbgPrint("[lwIP, LibTCPListen] pcb = 0x%x, newpcb = 0x%x, sizeof(pcb) = %d \n",
|
|
|
|
pcb, ret, sizeof(struct tcp_pcb));
|
2011-06-01 13:27:35 +00:00
|
|
|
|
|
|
|
DbgPrint("[lwIP, LibTCPListen] Done\n");
|
2010-09-21 06:11:24 +00:00
|
|
|
|
|
|
|
ExFreePool(msg);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct send_callback_msg
|
|
|
|
{
|
|
|
|
/* Synchronization */
|
|
|
|
KEVENT Event;
|
|
|
|
|
|
|
|
/* Input */
|
|
|
|
struct tcp_pcb *Pcb;
|
|
|
|
void *Data;
|
|
|
|
u16_t DataLength;
|
|
|
|
|
|
|
|
/* Output */
|
|
|
|
err_t Error;
|
|
|
|
};
|
|
|
|
|
|
|
|
static
|
|
|
|
void
|
|
|
|
LibTCPSendCallback(void *arg)
|
|
|
|
{
|
|
|
|
struct send_callback_msg *msg = arg;
|
|
|
|
|
|
|
|
ASSERT(msg);
|
|
|
|
|
|
|
|
if (tcp_sndbuf(msg->Pcb) < msg->DataLength)
|
|
|
|
{
|
|
|
|
msg->Error = ERR_INPROGRESS;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
msg->Error = tcp_write(msg->Pcb, msg->Data, msg->DataLength, TCP_WRITE_FLAG_COPY);
|
|
|
|
|
|
|
|
tcp_output(msg->Pcb);
|
|
|
|
}
|
|
|
|
|
|
|
|
KeSetEvent(&msg->Event, IO_NO_INCREMENT, FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
err_t
|
2011-07-11 12:36:07 +00:00
|
|
|
LibTCPSend(struct tcp_pcb *pcb, const void *dataptr, const u16_t len, const int safe)
|
2010-09-21 06:11:24 +00:00
|
|
|
{
|
|
|
|
err_t ret;
|
|
|
|
|
|
|
|
if (!pcb)
|
|
|
|
return ERR_CLSD;
|
2011-07-11 12:36:07 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
If we're being called from a handler it means we're in the conetxt of teh tcpip
|
|
|
|
main thread. Therefore we don't have to queue our request via a callback and we
|
|
|
|
can execute immediately.
|
|
|
|
*/
|
|
|
|
if (safe)
|
2010-09-21 06:11:24 +00:00
|
|
|
{
|
2011-07-11 12:36:07 +00:00
|
|
|
if (tcp_sndbuf(pcb) < len)
|
|
|
|
{
|
|
|
|
ret = ERR_INPROGRESS;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = tcp_write(pcb, dataptr, len, TCP_WRITE_FLAG_COPY);
|
|
|
|
tcp_output(pcb);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
struct send_callback_msg *msg;
|
|
|
|
|
|
|
|
msg = ExAllocatePool(NonPagedPool, sizeof(struct send_callback_msg));
|
|
|
|
if (msg)
|
|
|
|
{
|
|
|
|
KeInitializeEvent(&msg->Event, NotificationEvent, FALSE);
|
|
|
|
msg->Pcb = pcb;
|
|
|
|
msg->Data = dataptr;
|
|
|
|
msg->DataLength = len;
|
2010-09-21 06:11:24 +00:00
|
|
|
|
2011-07-11 12:36:07 +00:00
|
|
|
tcpip_callback_with_block(LibTCPSendCallback, msg, 1);
|
2010-09-21 06:11:24 +00:00
|
|
|
|
2011-07-11 12:36:07 +00:00
|
|
|
if (WaitForEventSafely(&msg->Event))
|
|
|
|
ret = msg->Error;
|
|
|
|
else
|
|
|
|
ret = ERR_CLSD;
|
2010-09-21 06:11:24 +00:00
|
|
|
|
2011-07-11 12:36:07 +00:00
|
|
|
DbgPrint("LibTCPSend(0x%x)\n", pcb);
|
2010-09-21 06:11:24 +00:00
|
|
|
|
2011-07-11 12:36:07 +00:00
|
|
|
ExFreePool(msg);
|
2010-09-21 06:11:24 +00:00
|
|
|
|
2011-07-11 12:36:07 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2010-09-21 06:11:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ERR_MEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct connect_callback_msg
|
|
|
|
{
|
|
|
|
/* Synchronization */
|
|
|
|
KEVENT Event;
|
|
|
|
|
|
|
|
/* Input */
|
|
|
|
struct tcp_pcb *Pcb;
|
|
|
|
struct ip_addr *IpAddress;
|
|
|
|
u16_t Port;
|
|
|
|
|
|
|
|
/* Output */
|
|
|
|
err_t Error;
|
|
|
|
};
|
|
|
|
|
|
|
|
static
|
|
|
|
void
|
|
|
|
LibTCPConnectCallback(void *arg)
|
|
|
|
{
|
|
|
|
struct connect_callback_msg *msg = arg;
|
2011-05-24 18:05:51 +00:00
|
|
|
|
2011-06-01 13:27:35 +00:00
|
|
|
DbgPrint("[lwIP, LibTCPConnectCallback] Called\n");
|
2010-09-21 06:11:24 +00:00
|
|
|
|
|
|
|
ASSERT(arg);
|
|
|
|
|
2011-06-20 15:59:49 +00:00
|
|
|
tcp_recv(msg->Pcb, InternalRecvEventHandler);
|
|
|
|
tcp_sent(msg->Pcb, InternalSendEventHandler);
|
2011-07-11 12:36:07 +00:00
|
|
|
|
2011-06-23 07:57:59 +00:00
|
|
|
err_t Error = tcp_connect(msg->Pcb, msg->IpAddress, ntohs(msg->Port), InternalConnectEventHandler);
|
|
|
|
msg->Error = Error == ERR_OK ? ERR_INPROGRESS : Error;
|
2010-09-21 06:11:24 +00:00
|
|
|
|
|
|
|
KeSetEvent(&msg->Event, IO_NO_INCREMENT, FALSE);
|
2011-05-24 18:05:51 +00:00
|
|
|
|
2011-06-01 13:27:35 +00:00
|
|
|
DbgPrint("[lwIP, LibTCPConnectCallback] Done\n");
|
2010-09-21 06:11:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err_t
|
|
|
|
LibTCPConnect(struct tcp_pcb *pcb, struct ip_addr *ipaddr, u16_t port)
|
|
|
|
{
|
|
|
|
struct connect_callback_msg *msg;
|
|
|
|
err_t ret;
|
2011-06-01 13:27:35 +00:00
|
|
|
|
|
|
|
DbgPrint("[lwIP, LibTCPConnect] Called\n");
|
2010-09-21 06:11:24 +00:00
|
|
|
|
|
|
|
if (!pcb)
|
|
|
|
return ERR_CLSD;
|
|
|
|
|
|
|
|
msg = ExAllocatePool(NonPagedPool, sizeof(struct connect_callback_msg));
|
|
|
|
if (msg)
|
|
|
|
{
|
|
|
|
KeInitializeEvent(&msg->Event, NotificationEvent, FALSE);
|
|
|
|
msg->Pcb = pcb;
|
|
|
|
msg->IpAddress = ipaddr;
|
|
|
|
msg->Port = port;
|
|
|
|
|
|
|
|
tcpip_callback_with_block(LibTCPConnectCallback, msg, 1);
|
|
|
|
|
|
|
|
if (WaitForEventSafely(&msg->Event))
|
2011-06-22 15:32:46 +00:00
|
|
|
{
|
2010-09-21 06:11:24 +00:00
|
|
|
ret = msg->Error;
|
2011-06-22 15:32:46 +00:00
|
|
|
}
|
2010-09-21 06:11:24 +00:00
|
|
|
else
|
|
|
|
ret = ERR_CLSD;
|
|
|
|
|
|
|
|
ExFreePool(msg);
|
2011-06-01 13:27:35 +00:00
|
|
|
|
|
|
|
DbgPrint("[lwIP, LibTCPConnect] pcb = 0x%x\n", pcb);
|
|
|
|
|
|
|
|
DbgPrint("[lwIP, LibTCPConnect] Done\n");
|
2010-09-21 06:11:24 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ERR_MEM;
|
|
|
|
}
|
|
|
|
|
2011-06-12 18:25:16 +00:00
|
|
|
struct shutdown_callback_msg
|
|
|
|
{
|
|
|
|
/* Synchronization */
|
|
|
|
KEVENT Event;
|
|
|
|
|
|
|
|
/* Input */
|
|
|
|
struct tcp_pcb *Pcb;
|
|
|
|
int shut_rx;
|
|
|
|
int shut_tx;
|
|
|
|
|
|
|
|
/* Output */
|
|
|
|
err_t Error;
|
|
|
|
};
|
|
|
|
|
|
|
|
static
|
|
|
|
void
|
|
|
|
LibTCPShutdownCallback(void *arg)
|
|
|
|
{
|
|
|
|
struct shutdown_callback_msg *msg = arg;
|
|
|
|
|
|
|
|
msg->Error = tcp_shutdown(msg->Pcb, msg->shut_rx, msg->shut_tx);
|
|
|
|
|
|
|
|
KeSetEvent(&msg->Event, IO_NO_INCREMENT, FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
err_t
|
|
|
|
LibTCPShutdown(struct tcp_pcb *pcb, int shut_rx, int shut_tx)
|
|
|
|
{
|
|
|
|
struct shutdown_callback_msg *msg;
|
|
|
|
err_t ret;
|
|
|
|
|
|
|
|
DbgPrint("[lwIP, LibTCPShutdown] Called\n");
|
|
|
|
|
|
|
|
if (!pcb)
|
|
|
|
{
|
|
|
|
DbgPrint("[lwIP, LibTCPShutdown] Done... NO pcb\n");
|
|
|
|
return ERR_CLSD;
|
|
|
|
}
|
|
|
|
|
|
|
|
msg = ExAllocatePool(NonPagedPool, sizeof(struct shutdown_callback_msg));
|
|
|
|
if (msg)
|
|
|
|
{
|
|
|
|
KeInitializeEvent(&msg->Event, NotificationEvent, FALSE);
|
|
|
|
|
|
|
|
msg->Pcb = pcb;
|
|
|
|
msg->shut_rx = shut_rx;
|
|
|
|
msg->shut_tx = shut_tx;
|
|
|
|
|
|
|
|
tcpip_callback_with_block(LibTCPShutdownCallback, msg, 1);
|
|
|
|
|
|
|
|
if (WaitForEventSafely(&msg->Event))
|
|
|
|
ret = msg->Error;
|
|
|
|
else
|
|
|
|
ret = ERR_CLSD;
|
|
|
|
|
|
|
|
ExFreePool(msg);
|
|
|
|
|
|
|
|
DbgPrint("[lwIP, LibTCPShutdown] pcb = 0x%x\n", pcb);
|
|
|
|
|
|
|
|
DbgPrint("[lwIP, LibTCPShutdown] Done\n");
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ERR_MEM;
|
|
|
|
}
|
|
|
|
|
2010-09-21 06:11:24 +00:00
|
|
|
struct close_callback_msg
|
|
|
|
{
|
|
|
|
/* Synchronization */
|
|
|
|
KEVENT Event;
|
|
|
|
|
|
|
|
/* Input */
|
|
|
|
struct tcp_pcb *Pcb;
|
|
|
|
|
|
|
|
/* Output */
|
|
|
|
err_t Error;
|
|
|
|
};
|
|
|
|
|
|
|
|
static
|
|
|
|
void
|
|
|
|
LibTCPCloseCallback(void *arg)
|
|
|
|
{
|
|
|
|
struct close_callback_msg *msg = arg;
|
|
|
|
|
2011-06-20 21:37:32 +00:00
|
|
|
if (msg->Pcb->state == LISTEN)
|
|
|
|
{
|
|
|
|
DbgPrint("[lwIP, LibTCPCloseCallback] Closing a listener\n");
|
|
|
|
msg->Error = tcp_close(msg->Pcb);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DbgPrint("[lwIP, LibTCPCloseCallback] Aborting a connection\n");
|
|
|
|
tcp_abort(msg->Pcb);
|
|
|
|
msg->Error = ERR_OK;
|
|
|
|
}
|
2010-09-21 06:11:24 +00:00
|
|
|
|
|
|
|
KeSetEvent(&msg->Event, IO_NO_INCREMENT, FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
err_t
|
2011-07-11 12:36:07 +00:00
|
|
|
LibTCPClose(struct tcp_pcb *pcb, const int safe)
|
2010-09-21 06:11:24 +00:00
|
|
|
{
|
|
|
|
err_t ret;
|
2011-06-01 13:27:35 +00:00
|
|
|
|
2011-06-20 12:59:27 +00:00
|
|
|
DbgPrint("[lwIP, LibTCPClose] Called on pcb = 0x%x\n", pcb);
|
2010-09-21 06:11:24 +00:00
|
|
|
|
|
|
|
if (!pcb)
|
2011-06-01 13:27:35 +00:00
|
|
|
{
|
|
|
|
DbgPrint("[lwIP, LibTCPClose] Done... NO pcb\n");
|
2010-09-21 06:11:24 +00:00
|
|
|
return ERR_CLSD;
|
2011-06-01 13:27:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DbgPrint("[lwIP, LibTCPClose] Removing pcb callbacks\n");
|
2011-06-20 12:59:27 +00:00
|
|
|
|
|
|
|
DbgPrint("[lwIP, LibTCPClose] pcb->state = %s\n", tcp_state_str[pcb->state]);
|
|
|
|
|
2010-09-21 06:11:24 +00:00
|
|
|
tcp_arg(pcb, NULL);
|
2011-06-20 14:49:58 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
if this pcb is not in LISTEN state than it has
|
|
|
|
valid recv, send and err callbacks to cancel
|
|
|
|
*/
|
|
|
|
if (pcb->state != LISTEN)
|
|
|
|
{
|
|
|
|
tcp_recv(pcb, NULL);
|
|
|
|
tcp_sent(pcb, NULL);
|
|
|
|
tcp_err(pcb, NULL);
|
|
|
|
}
|
|
|
|
|
2010-09-21 06:11:24 +00:00
|
|
|
tcp_accept(pcb, NULL);
|
2011-06-01 13:27:35 +00:00
|
|
|
|
|
|
|
DbgPrint("[lwIP, LibTCPClose] Attempting to allocate memory for msg\n");
|
2011-07-11 12:36:07 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
If we're being called from a handler it means we're in the conetxt of teh tcpip
|
|
|
|
main thread. Therefore we don't have to queue our request via a callback and we
|
|
|
|
can execute immediately.
|
|
|
|
*/
|
|
|
|
if (safe)
|
2010-09-21 06:11:24 +00:00
|
|
|
{
|
2011-07-11 12:36:07 +00:00
|
|
|
if (pcb->state == LISTEN)
|
|
|
|
{
|
|
|
|
DbgPrint("[lwIP, LibTCPClose] Closing a listener\n");
|
|
|
|
ret = tcp_close(pcb);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DbgPrint("[lwIP, LibTCPClose] Aborting a connection\n");
|
|
|
|
tcp_abort(pcb);
|
|
|
|
ret = ERR_OK;
|
|
|
|
}
|
2011-06-01 13:27:35 +00:00
|
|
|
|
2011-07-11 12:36:07 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
struct close_callback_msg *msg;
|
2011-06-01 13:27:35 +00:00
|
|
|
|
2011-07-11 12:36:07 +00:00
|
|
|
msg = ExAllocatePool(NonPagedPool, sizeof(struct close_callback_msg));
|
|
|
|
if (msg)
|
|
|
|
{
|
|
|
|
DbgPrint("[lwIP, LibTCPClose] Initializing msg->Event\n");
|
|
|
|
KeInitializeEvent(&msg->Event, NotificationEvent, FALSE);
|
|
|
|
|
|
|
|
DbgPrint("[lwIP, LibTCPClose] Initializing msg->pcb = 0x%x\n", pcb);
|
|
|
|
msg->Pcb = pcb;
|
|
|
|
|
|
|
|
DbgPrint("[lwIP, LibTCPClose] Attempting to call LibTCPCloseCallback\n");
|
2010-09-21 06:11:24 +00:00
|
|
|
|
2011-07-11 12:36:07 +00:00
|
|
|
tcpip_callback_with_block(LibTCPCloseCallback, msg, 1);
|
2010-09-21 06:11:24 +00:00
|
|
|
|
2011-07-11 12:36:07 +00:00
|
|
|
if (WaitForEventSafely(&msg->Event))
|
|
|
|
ret = msg->Error;
|
|
|
|
else
|
|
|
|
ret = ERR_CLSD;
|
2010-09-21 06:11:24 +00:00
|
|
|
|
2011-07-11 12:36:07 +00:00
|
|
|
ExFreePool(msg);
|
2010-09-21 06:11:24 +00:00
|
|
|
|
2011-07-11 12:36:07 +00:00
|
|
|
DbgPrint("[lwIP, LibTCPClose] pcb = 0x%x\n", pcb);
|
2011-06-01 13:27:35 +00:00
|
|
|
|
2011-07-11 12:36:07 +00:00
|
|
|
DbgPrint("[lwIP, LibTCPClose] Done\n");
|
2010-09-21 06:11:24 +00:00
|
|
|
|
2011-07-11 12:36:07 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2010-09-21 06:11:24 +00:00
|
|
|
}
|
2011-06-01 13:27:35 +00:00
|
|
|
|
|
|
|
DbgPrint("[lwIP, LibTCPClose] Failed to allocate memory\n");
|
2010-09-21 06:11:24 +00:00
|
|
|
|
|
|
|
return ERR_MEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-05-26 17:42:00 +00:00
|
|
|
LibTCPAccept(struct tcp_pcb *pcb, struct tcp_pcb *listen_pcb, void *arg)
|
2010-09-21 06:11:24 +00:00
|
|
|
{
|
2011-06-01 13:27:35 +00:00
|
|
|
DbgPrint("[lwIP, LibTCPAccept] Called. (pcb, arg) = (0x%x, 0x%x)\n", pcb, arg);
|
2010-09-21 06:11:24 +00:00
|
|
|
|
|
|
|
ASSERT(arg);
|
|
|
|
|
|
|
|
tcp_arg(pcb, NULL);
|
|
|
|
tcp_recv(pcb, InternalRecvEventHandler);
|
|
|
|
tcp_sent(pcb, InternalSendEventHandler);
|
2011-06-20 15:59:49 +00:00
|
|
|
tcp_err(pcb, InternalErrorEventHandler);
|
2010-09-21 06:11:24 +00:00
|
|
|
tcp_arg(pcb, arg);
|
|
|
|
|
2011-05-26 17:42:00 +00:00
|
|
|
tcp_accepted(listen_pcb);
|
2011-05-24 18:05:51 +00:00
|
|
|
|
2011-06-01 13:27:35 +00:00
|
|
|
DbgPrint("[lwIP, LibTCPAccept] Done\n");
|
2010-09-21 06:11:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err_t
|
|
|
|
LibTCPGetHostName(struct tcp_pcb *pcb, struct ip_addr *ipaddr, u16_t *port)
|
|
|
|
{
|
2011-06-01 13:27:35 +00:00
|
|
|
DbgPrint("[lwIP, LibTCPGetHostName] Called. pcb = (0x%x)\n", pcb);
|
2010-09-21 06:11:24 +00:00
|
|
|
|
|
|
|
if (!pcb)
|
|
|
|
return ERR_CLSD;
|
|
|
|
|
|
|
|
*ipaddr = pcb->local_ip;
|
|
|
|
*port = pcb->local_port;
|
|
|
|
|
2011-06-01 13:27:35 +00:00
|
|
|
DbgPrint("[lwIP, LibTCPGetHostName] Got host port: %d\n", *port);
|
|
|
|
|
|
|
|
DbgPrint("[lwIP, LibTCPGetHostName] Done\n");
|
2010-09-21 06:11:24 +00:00
|
|
|
|
|
|
|
return ERR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
err_t
|
|
|
|
LibTCPGetPeerName(struct tcp_pcb *pcb, struct ip_addr *ipaddr, u16_t *port)
|
|
|
|
{
|
2011-06-01 13:27:35 +00:00
|
|
|
DbgPrint("[lwIP, LibTCPGetPeerName] pcb = (0x%x)\n", pcb);
|
2010-09-21 06:11:24 +00:00
|
|
|
|
|
|
|
if (!pcb)
|
|
|
|
return ERR_CLSD;
|
|
|
|
|
|
|
|
*ipaddr = pcb->remote_ip;
|
|
|
|
*port = pcb->remote_port;
|
|
|
|
|
2011-06-01 13:27:35 +00:00
|
|
|
DbgPrint("[lwIP, LibTCPGetPeerName] Got remote port: %d\n", *port);
|
2010-09-21 06:11:24 +00:00
|
|
|
|
|
|
|
return ERR_OK;
|
|
|
|
}
|