ReactOS  0.4.15-dev-3181-g4acf100
rostcp.c File Reference
#include "lwip/sys.h"
#include "lwip/netif.h"
#include "lwip/tcpip.h"
#include "rosip.h"
#include <debug.h>
Include dependency graph for rostcp.c:

Go to the source code of this file.

Functions

NTSTATUS TCPTranslateError (const err_t err)
 
void LibTCPDumpPcb (PVOID SocketContext)
 
static void LibTCPEmptyQueue (PCONNECTION_ENDPOINT Connection)
 
void LibTCPEnqueuePacket (PCONNECTION_ENDPOINT Connection, struct pbuf *p)
 
PQUEUE_ENTRY LibTCPDequeuePacket (PCONNECTION_ENDPOINT Connection)
 
NTSTATUS LibTCPGetDataFromConnectionQueue (PCONNECTION_ENDPOINT Connection, PUCHAR RecvBuffer, UINT RecvLen, UINT *Received)
 
static BOOLEAN WaitForEventSafely (PRKEVENT Event)
 
static err_t InternalSendEventHandler (void *arg, PTCP_PCB pcb, const u16_t space)
 
static err_t InternalRecvEventHandler (void *arg, PTCP_PCB pcb, struct pbuf *p, const err_t err)
 
static err_t InternalAcceptEventHandler (void *arg, PTCP_PCB newpcb, const err_t err)
 
static err_t InternalConnectEventHandler (void *arg, PTCP_PCB pcb, const err_t err)
 
static void InternalErrorEventHandler (void *arg, const err_t err)
 
static void LibTCPSocketCallback (void *arg)
 
struct tcp_pcb * LibTCPSocket (void *arg)
 
static void LibTCPFreeSocketCallback (void *arg)
 
void LibTCPFreeSocket (PTCP_PCB pcb)
 
static void LibTCPBindCallback (void *arg)
 
err_t LibTCPBind (PCONNECTION_ENDPOINT Connection, struct ip_addr *const ipaddr, const u16_t port)
 
static void LibTCPListenCallback (void *arg)
 
PTCP_PCB LibTCPListen (PCONNECTION_ENDPOINT Connection, const u8_t backlog)
 
static void LibTCPSendCallback (void *arg)
 
err_t LibTCPSend (PCONNECTION_ENDPOINT Connection, void *const dataptr, const u16_t len, u32_t *sent, const int safe)
 
static void LibTCPConnectCallback (void *arg)
 
err_t LibTCPConnect (PCONNECTION_ENDPOINT Connection, struct ip_addr *const ipaddr, const u16_t port)
 
static void LibTCPShutdownCallback (void *arg)
 
err_t LibTCPShutdown (PCONNECTION_ENDPOINT Connection, const int shut_rx, const int shut_tx)
 
static void LibTCPCloseCallback (void *arg)
 
err_t LibTCPClose (PCONNECTION_ENDPOINT Connection, const int safe, const int callback)
 
void LibTCPAccept (PTCP_PCB pcb, struct tcp_pcb *listen_pcb, void *arg)
 
err_t LibTCPGetHostName (PTCP_PCB pcb, struct ip_addr *const ipaddr, u16_t *const port)
 
err_t LibTCPGetPeerName (PTCP_PCB pcb, struct ip_addr *const ipaddr, u16_t *const port)
 
void LibTCPSetNoDelay (PTCP_PCB pcb, BOOLEAN Set)
 
void LibTCPGetSocketStatus (PTCP_PCB pcb, PULONG State)
 

Variables

static const char *const tcp_state_str []
 
KEVENT TerminationEvent
 
NPAGED_LOOKASIDE_LIST MessageLookasideList
 
NPAGED_LOOKASIDE_LIST QueueEntryLookasideList
 

Function Documentation

◆ InternalAcceptEventHandler()

static err_t InternalAcceptEventHandler ( void arg,
PTCP_PCB  newpcb,
const err_t  err 
)
static

Definition at line 270 of file rostcp.c.

271 {
272  /* Make sure the socket didn't get closed */
273  if (!arg)
274  return ERR_CLSD;
275 
276  TCPAcceptEventHandler(arg, newpcb);
277 
278  /* Set in LibTCPAccept (called from TCPAcceptEventHandler) */
279  if (newpcb->callback_arg)
280  return ERR_OK;
281  else
282  return ERR_CLSD;
283 }
#define ERR_CLSD
Definition: err.h:67
VOID TCPAcceptEventHandler(void *arg, PTCP_PCB newpcb)
Definition: event.c:231
#define ERR_OK
Definition: err.h:52

Referenced by LibTCPListenCallback().

◆ InternalConnectEventHandler()

static err_t InternalConnectEventHandler ( void arg,
PTCP_PCB  pcb,
const err_t  err 
)
static

Definition at line 287 of file rostcp.c.

288 {
289  /* Make sure the socket didn't get closed */
290  if (!arg)
291  return ERR_OK;
292 
294 
295  return ERR_OK;
296 }
VOID TCPConnectEventHandler(void *arg, const err_t err)
Definition: event.c:420
#define ERR_OK
Definition: err.h:52
#define err(...)

Referenced by LibTCPConnectCallback().

◆ InternalErrorEventHandler()

static void InternalErrorEventHandler ( void arg,
const err_t  err 
)
static

Definition at line 300 of file rostcp.c.

301 {
302  PCONNECTION_ENDPOINT Connection = arg;
303 
304  /* Make sure the socket didn't get closed */
305  if (!arg || Connection->SocketContext == NULL) return;
306 
307  /* The PCB is dead now */
308  Connection->SocketContext = NULL;
309 
310  /* Give them one shot to receive the remaining data */
311  Connection->ReceiveShutdown = TRUE;
313  TCPRecvEventHandler(Connection);
314 
315  /* Terminate the connection */
316  TCPFinEventHandler(Connection, err);
317 }
NTSTATUS TCPTranslateError(const err_t err)
Definition: tcp.c:267
#define TRUE
Definition: types.h:120
VOID TCPFinEventHandler(void *arg, const err_t err)
Definition: event.c:182
VOID TCPRecvEventHandler(void *arg)
Definition: event.c:380
NTSTATUS ReceiveShutdownStatus
Definition: titypes.h:266
void * arg
Definition: msvc.h:10
#define err(...)
BOOLEAN ReceiveShutdown
Definition: titypes.h:265
#define NULL
Definition: types.h:112

Referenced by LibTCPAccept(), and LibTCPSocketCallback().

◆ InternalRecvEventHandler()

static err_t InternalRecvEventHandler ( void arg,
PTCP_PCB  pcb,
struct pbuf p,
const err_t  err 
)
static

Definition at line 216 of file rostcp.c.

217 {
218  PCONNECTION_ENDPOINT Connection = arg;
219 
220  /* Make sure the socket didn't get closed */
221  if (!arg)
222  {
223  if (p)
224  pbuf_free(p);
225 
226  return ERR_OK;
227  }
228 
229  if (p)
230  {
231  LibTCPEnqueuePacket(Connection, p);
232 
233  tcp_recved(pcb, p->tot_len);
234 
236  }
237  else if (err == ERR_OK)
238  {
239  /* Complete pending reads with 0 bytes to indicate a graceful closure,
240  * but note that send is still possible in this state so we don't close the
241  * whole socket here (by calling tcp_close()) as that would violate TCP specs
242  */
243  Connection->ReceiveShutdown = TRUE;
245 
246  /* If we already did a send shutdown, we're in TIME_WAIT so we can't use this PCB anymore */
247  if (Connection->SendShutdown)
248  {
249  Connection->SocketContext = NULL;
250  tcp_arg(pcb, NULL);
251  }
252 
253  /* Indicate the graceful close event */
255 
256  /* If the PCB is gone, clean up the connection */
257  if (Connection->SendShutdown)
258  {
259  TCPFinEventHandler(Connection, ERR_CLSD);
260  }
261  }
262 
263  return ERR_OK;
264 }
#define ERR_CLSD
Definition: err.h:67
#define TRUE
Definition: types.h:120
VOID TCPFinEventHandler(void *arg, const err_t err)
Definition: event.c:182
VOID TCPRecvEventHandler(void *arg)
Definition: event.c:380
NTSTATUS ReceiveShutdownStatus
Definition: titypes.h:266
void * arg
Definition: msvc.h:10
void LibTCPEnqueuePacket(PCONNECTION_ENDPOINT Connection, struct pbuf *p)
Definition: rostcp.c:76
u8_t pbuf_free(struct pbuf *p)
Definition: pbuf.c:618
BOOLEAN SendShutdown
Definition: titypes.h:264
#define ERR_OK
Definition: err.h:52
#define err(...)
BOOLEAN ReceiveShutdown
Definition: titypes.h:265
#define NULL
Definition: types.h:112
#define STATUS_SUCCESS
Definition: shellext.h:65
GLfloat GLfloat p
Definition: glext.h:8902

Referenced by LibTCPAccept(), and LibTCPConnectCallback().

◆ InternalSendEventHandler()

static err_t InternalSendEventHandler ( void arg,
PTCP_PCB  pcb,
const u16_t  space 
)
static

Definition at line 204 of file rostcp.c.

205 {
206  /* Make sure the socket didn't get closed */
207  if (!arg) return ERR_OK;
208 
210 
211  return ERR_OK;
212 }
#define ERR_OK
Definition: err.h:52
VOID TCPSendEventHandler(void *arg, const u16_t space)
Definition: event.c:294

Referenced by LibTCPAccept(), and LibTCPConnectCallback().

◆ LibTCPAccept()

void LibTCPAccept ( PTCP_PCB  pcb,
struct tcp_pcb *  listen_pcb,
void arg 
)

Definition at line 833 of file rostcp.c.

834 {
835  ASSERT(arg);
836 
837  tcp_arg(pcb, NULL);
839  tcp_sent(pcb, InternalSendEventHandler);
840  tcp_err(pcb, InternalErrorEventHandler);
841  tcp_arg(pcb, arg);
842 
843  tcp_accepted(listen_pcb);
844 }
static err_t InternalRecvEventHandler(void *arg, PTCP_PCB pcb, struct pbuf *p, const err_t err)
Definition: rostcp.c:216
STREAM tcp_recv(STREAM s, uint32 length)
Definition: tcp.c:344
static err_t InternalSendEventHandler(void *arg, PTCP_PCB pcb, const u16_t space)
Definition: rostcp.c:204
#define ASSERT(a)
Definition: mode.c:44
#define NULL
Definition: types.h:112
static void InternalErrorEventHandler(void *arg, const err_t err)
Definition: rostcp.c:300

Referenced by TCPAcceptEventHandler().

◆ LibTCPBind()

err_t LibTCPBind ( PCONNECTION_ENDPOINT  Connection,
struct ip_addr *const  ipaddr,
const u16_t  port 
)

Definition at line 418 of file rostcp.c.

419 {
420  struct lwip_callback_msg *msg;
421  err_t ret;
422 
423  msg = ExAllocateFromNPagedLookasideList(&MessageLookasideList);
424  if (msg)
425  {
427  msg->Input.Bind.Connection = Connection;
428  msg->Input.Bind.IpAddress = ipaddr;
429  msg->Input.Bind.Port = port;
430 
432 
433  if (WaitForEventSafely(&msg->Event))
434  ret = msg->Output.Bind.Error;
435  else
436  ret = ERR_CLSD;
437 
438  ExFreeToNPagedLookasideList(&MessageLookasideList, msg);
439 
440  return ret;
441  }
442 
443  return ERR_MEM;
444 }
#define ERR_CLSD
Definition: err.h:67
static BOOLEAN WaitForEventSafely(PRKEVENT Event)
Definition: rostcp.c:179
err_t tcpip_callback_with_block(tcpip_callback_fn function, void *ctx, u8_t block)
Definition: tcpip.c:212
PCONNECTION_ENDPOINT Connection
Definition: rosip.h:38
#define FALSE
Definition: types.h:117
s8_t err_t
Definition: err.h:47
int ret
#define KeInitializeEvent(pEvt, foo, foo2)
Definition: env_spec_w32.h:477
#define ERR_MEM
Definition: fontsub.h:52
#define msg(x)
Definition: auth_time.c:54
static void LibTCPBindCallback(void *arg)
Definition: rostcp.c:393
USHORT port
Definition: uri.c:228
NPAGED_LOOKASIDE_LIST MessageLookasideList
Definition: sys_arch.c:15

Referenced by TCPConnect(), and TCPListen().

◆ LibTCPBindCallback()

static void LibTCPBindCallback ( void arg)
static

Definition at line 393 of file rostcp.c.

394 {
395  struct lwip_callback_msg *msg = arg;
396  PTCP_PCB pcb = msg->Input.Bind.Connection->SocketContext;
397 
398  ASSERT(msg);
399 
400  if (!msg->Input.Bind.Connection->SocketContext)
401  {
402  msg->Output.Bind.Error = ERR_CLSD;
403  goto done;
404  }
405 
406  /* We're guaranteed that the local address is valid to bind at this point */
407  pcb->so_options |= SOF_REUSEADDR;
408 
409  msg->Output.Bind.Error = tcp_bind(pcb,
410  msg->Input.Bind.IpAddress,
411  ntohs(msg->Input.Bind.Port));
412 
413 done:
414  KeSetEvent(&msg->Event, IO_NO_INCREMENT, FALSE);
415 }
#define ERR_CLSD
Definition: err.h:67
void * arg
Definition: msvc.h:10
LONG NTAPI KeSetEvent(IN PKEVENT Event, IN KPRIORITY Increment, IN BOOLEAN Wait)
Definition: eventobj.c:159
#define FALSE
Definition: types.h:117
struct tcp_pcb * pcb
Definition: rosip.h:35
#define SOF_REUSEADDR
Definition: ip.h:99
#define ASSERT(a)
Definition: mode.c:44
struct tcp_pcb * PTCP_PCB
Definition: rosip.h:15
#define ntohs(x)
Definition: module.h:208
#define msg(x)
Definition: auth_time.c:54
#define IO_NO_INCREMENT
Definition: iotypes.h:598

Referenced by LibTCPBind().

◆ LibTCPClose()

err_t LibTCPClose ( PCONNECTION_ENDPOINT  Connection,
const int  safe,
const int  callback 
)

Definition at line 801 of file rostcp.c.

802 {
803  err_t ret;
804  struct lwip_callback_msg *msg;
805 
806  msg = ExAllocateFromNPagedLookasideList(&MessageLookasideList);
807  if (msg)
808  {
810 
811  msg->Input.Close.Connection = Connection;
812  msg->Input.Close.Callback = callback;
813 
814  if (safe)
816  else
818 
819  if (WaitForEventSafely(&msg->Event))
820  ret = msg->Output.Close.Error;
821  else
822  ret = ERR_CLSD;
823 
824  ExFreeToNPagedLookasideList(&MessageLookasideList, msg);
825 
826  return ret;
827  }
828 
829  return ERR_MEM;
830 }
#define ERR_CLSD
Definition: err.h:67
static BOOLEAN WaitForEventSafely(PRKEVENT Event)
Definition: rostcp.c:179
err_t tcpip_callback_with_block(tcpip_callback_fn function, void *ctx, u8_t block)
Definition: tcpip.c:212
PCONNECTION_ENDPOINT Connection
Definition: rosip.h:38
#define FALSE
Definition: types.h:117
s8_t err_t
Definition: err.h:47
int ret
#define KeInitializeEvent(pEvt, foo, foo2)
Definition: env_spec_w32.h:477
#define ERR_MEM
Definition: fontsub.h:52
static void LibTCPCloseCallback(void *arg)
Definition: rostcp.c:751
#define msg(x)
Definition: auth_time.c:54
static IPrintDialogCallback callback
Definition: printdlg.c:326
NPAGED_LOOKASIDE_LIST MessageLookasideList
Definition: sys_arch.c:15

Referenced by TCPAcceptEventHandler(), and TCPClose().

◆ LibTCPCloseCallback()

static void LibTCPCloseCallback ( void arg)
static

Definition at line 751 of file rostcp.c.

752 {
753  struct lwip_callback_msg *msg = arg;
754  PTCP_PCB pcb = msg->Input.Close.Connection->SocketContext;
755 
756  /* Empty the queue even if we're already "closed" */
757  LibTCPEmptyQueue(msg->Input.Close.Connection);
758 
759  /* Check if we've already been closed */
760  if (msg->Input.Close.Connection->Closing)
761  {
762  msg->Output.Close.Error = ERR_OK;
763  goto done;
764  }
765 
766  /* Enter "closing" mode if we're doing a normal close */
767  if (msg->Input.Close.Callback)
768  msg->Input.Close.Connection->Closing = TRUE;
769 
770  /* Check if the PCB was already "closed" but the client doesn't know it yet */
771  if (!msg->Input.Close.Connection->SocketContext)
772  {
773  msg->Output.Close.Error = ERR_OK;
774  goto done;
775  }
776 
777  /* Clear the PCB pointer and stop callbacks */
778  msg->Input.Close.Connection->SocketContext = NULL;
779  tcp_arg(pcb, NULL);
780 
781  /* This may generate additional callbacks but we don't care,
782  * because they're too inconsistent to rely on */
783  msg->Output.Close.Error = tcp_close(pcb);
784 
785  if (msg->Output.Close.Error)
786  {
787  /* Restore the PCB pointer */
788  msg->Input.Close.Connection->SocketContext = pcb;
789  msg->Input.Close.Connection->Closing = FALSE;
790  }
791  else if (msg->Input.Close.Callback)
792  {
793  TCPFinEventHandler(msg->Input.Close.Connection, ERR_CLSD);
794  }
795 
796 done:
797  KeSetEvent(&msg->Event, IO_NO_INCREMENT, FALSE);
798 }
static void LibTCPEmptyQueue(PCONNECTION_ENDPOINT Connection)
Definition: rostcp.c:55
#define ERR_CLSD
Definition: err.h:67
#define TRUE
Definition: types.h:120
VOID TCPFinEventHandler(void *arg, const err_t err)
Definition: event.c:182
void * arg
Definition: msvc.h:10
LONG NTAPI KeSetEvent(IN PKEVENT Event, IN KPRIORITY Increment, IN BOOLEAN Wait)
Definition: eventobj.c:159
#define FALSE
Definition: types.h:117
struct tcp_pcb * pcb
Definition: rosip.h:35
#define ERR_OK
Definition: err.h:52
struct tcp_pcb * PTCP_PCB
Definition: rosip.h:15
void tcp_close(struct sock *sk, long timeout)
#define NULL
Definition: types.h:112
#define msg(x)
Definition: auth_time.c:54
#define IO_NO_INCREMENT
Definition: iotypes.h:598

Referenced by LibTCPClose().

◆ LibTCPConnect()

err_t LibTCPConnect ( PCONNECTION_ENDPOINT  Connection,
struct ip_addr *const  ipaddr,
const u16_t  port 
)

Definition at line 625 of file rostcp.c.

626 {
627  struct lwip_callback_msg *msg;
628  err_t ret;
629 
630  msg = ExAllocateFromNPagedLookasideList(&MessageLookasideList);
631  if (msg)
632  {
634  msg->Input.Connect.Connection = Connection;
635  msg->Input.Connect.IpAddress = ipaddr;
636  msg->Input.Connect.Port = port;
637 
639 
640  if (WaitForEventSafely(&msg->Event))
641  {
642  ret = msg->Output.Connect.Error;
643  }
644  else
645  ret = ERR_CLSD;
646 
647  ExFreeToNPagedLookasideList(&MessageLookasideList, msg);
648 
649  return ret;
650  }
651 
652  return ERR_MEM;
653 }
#define ERR_CLSD
Definition: err.h:67
static BOOLEAN WaitForEventSafely(PRKEVENT Event)
Definition: rostcp.c:179
err_t tcpip_callback_with_block(tcpip_callback_fn function, void *ctx, u8_t block)
Definition: tcpip.c:212
static void LibTCPConnectCallback(void *arg)
Definition: rostcp.c:598
PCONNECTION_ENDPOINT Connection
Definition: rosip.h:38
#define FALSE
Definition: types.h:117
s8_t err_t
Definition: err.h:47
int ret
#define KeInitializeEvent(pEvt, foo, foo2)
Definition: env_spec_w32.h:477
#define ERR_MEM
Definition: fontsub.h:52
#define msg(x)
Definition: auth_time.c:54
USHORT port
Definition: uri.c:228
NPAGED_LOOKASIDE_LIST MessageLookasideList
Definition: sys_arch.c:15

Referenced by TCPConnect().

◆ LibTCPConnectCallback()

static void LibTCPConnectCallback ( void arg)
static

Definition at line 598 of file rostcp.c.

599 {
600  struct lwip_callback_msg *msg = arg;
601  err_t Error;
602 
603  ASSERT(arg);
604 
605  if (!msg->Input.Connect.Connection->SocketContext)
606  {
607  msg->Output.Connect.Error = ERR_CLSD;
608  goto done;
609  }
610 
611  tcp_recv((PTCP_PCB)msg->Input.Connect.Connection->SocketContext, InternalRecvEventHandler);
612  tcp_sent((PTCP_PCB)msg->Input.Connect.Connection->SocketContext, InternalSendEventHandler);
613 
614  Error = tcp_connect((PTCP_PCB)msg->Input.Connect.Connection->SocketContext,
615  msg->Input.Connect.IpAddress, ntohs(msg->Input.Connect.Port),
617 
618  msg->Output.Connect.Error = Error == ERR_OK ? ERR_INPROGRESS : Error;
619 
620 done:
621  KeSetEvent(&msg->Event, IO_NO_INCREMENT, FALSE);
622 }
static err_t InternalRecvEventHandler(void *arg, PTCP_PCB pcb, struct pbuf *p, const err_t err)
Definition: rostcp.c:216
STREAM tcp_recv(STREAM s, uint32 length)
Definition: tcp.c:344
#define ERR_CLSD
Definition: err.h:67
void * arg
Definition: msvc.h:10
LONG NTAPI KeSetEvent(IN PKEVENT Event, IN KPRIORITY Increment, IN BOOLEAN Wait)
Definition: eventobj.c:159
#define FALSE
Definition: types.h:117
s8_t err_t
Definition: err.h:47
#define ERR_OK
Definition: err.h:52
static err_t InternalSendEventHandler(void *arg, PTCP_PCB pcb, const u16_t space)
Definition: rostcp.c:204
#define ASSERT(a)
Definition: mode.c:44
struct tcp_pcb * PTCP_PCB
Definition: rosip.h:15
BOOL Error
Definition: chkdsk.c:66
RD_BOOL tcp_connect(char *server)
Definition: tcp.c:717
static err_t InternalConnectEventHandler(void *arg, PTCP_PCB pcb, const err_t err)
Definition: rostcp.c:287
#define ntohs(x)
Definition: module.h:208
#define ERR_INPROGRESS
Definition: err.h:57
#define msg(x)
Definition: auth_time.c:54
#define IO_NO_INCREMENT
Definition: iotypes.h:598

Referenced by LibTCPConnect().

◆ LibTCPDequeuePacket()

PQUEUE_ENTRY LibTCPDequeuePacket ( PCONNECTION_ENDPOINT  Connection)

Definition at line 89 of file rostcp.c.

90 {
92  PQUEUE_ENTRY qp = NULL;
93 
94  if (IsListEmpty(&Connection->PacketQueue)) return NULL;
95 
97 
98  qp = CONTAINING_RECORD(Entry, QUEUE_ENTRY, ListEntry);
99 
100  return qp;
101 }
struct _Entry Entry
Definition: kefuncs.h:627
PCONNECTION_ENDPOINT Connection
Definition: rosip.h:38
_Must_inspect_result_ FORCEINLINE BOOLEAN IsListEmpty(_In_ const LIST_ENTRY *ListHead)
Definition: rtlfuncs.h:57
LIST_ENTRY PacketQueue
Definition: titypes.h:256
Definition: rosip.h:17
FORCEINLINE PLIST_ENTRY RemoveHeadList(_Inout_ PLIST_ENTRY ListHead)
Definition: rtlfuncs.h:128
PFLT_MESSAGE_WAITER_QUEUE CONTAINING_RECORD(Csq, DEVICE_EXTENSION, IrpQueue)) -> WaiterQ.mLock) _IRQL_raises_(DISPATCH_LEVEL) VOID NTAPI FltpAcquireMessageWaiterLock(_In_ PIO_CSQ Csq, _Out_ PKIRQL Irql)
Definition: Messaging.c:560
Definition: typedefs.h:119
#define NULL
Definition: types.h:112
base of all file and directory entries
Definition: entries.h:82

Referenced by LibTCPGetDataFromConnectionQueue().

◆ LibTCPDumpPcb()

void LibTCPDumpPcb ( PVOID  SocketContext)

Definition at line 39 of file rostcp.c.

40 {
41  struct tcp_pcb *pcb = (struct tcp_pcb*)SocketContext;
42  unsigned int addr = ntohl(pcb->remote_ip.addr);
43 
44  DbgPrint("\tState: %s\n", tcp_state_str[pcb->state]);
45  DbgPrint("\tRemote: (%d.%d.%d.%d, %d)\n",
46  (addr >> 24) & 0xFF,
47  (addr >> 16) & 0xFF,
48  (addr >> 8) & 0xFF,
49  addr & 0xFF,
50  pcb->remote_port);
51 }
static const char *const tcp_state_str[]
Definition: rostcp.c:9
#define DbgPrint
Definition: hal.h:12
_In_ USHORT _In_ ULONG _In_ PSOCKADDR _In_ PSOCKADDR _Reserved_ ULONG _In_opt_ PVOID SocketContext
Definition: wsk.h:182
GLenum const GLvoid * addr
Definition: glext.h:9621
#define ntohl(x)
Definition: module.h:203

Referenced by LogActiveObjects().

◆ LibTCPEmptyQueue()

static void LibTCPEmptyQueue ( PCONNECTION_ENDPOINT  Connection)
static

Definition at line 55 of file rostcp.c.

56 {
58  PQUEUE_ENTRY qp = NULL;
59 
60  ReferenceObject(Connection);
61 
62  while (!IsListEmpty(&Connection->PacketQueue))
63  {
64  Entry = RemoveHeadList(&Connection->PacketQueue);
65  qp = CONTAINING_RECORD(Entry, QUEUE_ENTRY, ListEntry);
66 
67  /* We're in the tcpip thread here so this is safe */
68  pbuf_free(qp->p);
69 
70  ExFreeToNPagedLookasideList(&QueueEntryLookasideList, qp);
71  }
72 
73  DereferenceObject(Connection);
74 }
struct pbuf * p
Definition: rosip.h:19
struct _Entry Entry
Definition: kefuncs.h:627
_Must_inspect_result_ FORCEINLINE BOOLEAN IsListEmpty(_In_ const LIST_ENTRY *ListHead)
Definition: rtlfuncs.h:57
LIST_ENTRY PacketQueue
Definition: titypes.h:256
#define DereferenceObject(Object)
Definition: titypes.h:24
Definition: rosip.h:17
FORCEINLINE PLIST_ENTRY RemoveHeadList(_Inout_ PLIST_ENTRY ListHead)
Definition: rtlfuncs.h:128
u8_t pbuf_free(struct pbuf *p)
Definition: pbuf.c:618
PFLT_MESSAGE_WAITER_QUEUE CONTAINING_RECORD(Csq, DEVICE_EXTENSION, IrpQueue)) -> WaiterQ.mLock) _IRQL_raises_(DISPATCH_LEVEL) VOID NTAPI FltpAcquireMessageWaiterLock(_In_ PIO_CSQ Csq, _Out_ PKIRQL Irql)
Definition: Messaging.c:560
#define ReferenceObject(Object)
Definition: titypes.h:14
Definition: typedefs.h:119
#define NULL
Definition: types.h:112
NPAGED_LOOKASIDE_LIST QueueEntryLookasideList
Definition: sys_arch.c:16
base of all file and directory entries
Definition: entries.h:82

Referenced by LibTCPCloseCallback().

◆ LibTCPEnqueuePacket()

void LibTCPEnqueuePacket ( PCONNECTION_ENDPOINT  Connection,
struct pbuf p 
)

Definition at line 76 of file rostcp.c.

77 {
78  PQUEUE_ENTRY qp;
79 
80  qp = (PQUEUE_ENTRY)ExAllocateFromNPagedLookasideList(&QueueEntryLookasideList);
81  qp->p = p;
82  qp->Offset = 0;
83 
84  LockObject(Connection);
85  InsertTailList(&Connection->PacketQueue, &qp->ListEntry);
86  UnlockObject(Connection);
87 }
struct _QUEUE_ENTRY * PQUEUE_ENTRY
struct pbuf * p
Definition: rosip.h:19
ULONG Offset
Definition: rosip.h:20
LIST_ENTRY ListEntry
Definition: rosip.h:21
#define InsertTailList(ListHead, Entry)
LIST_ENTRY PacketQueue
Definition: titypes.h:256
Definition: rosip.h:17
#define UnlockObject(Object)
Definition: titypes.h:44
GLfloat GLfloat p
Definition: glext.h:8902
NPAGED_LOOKASIDE_LIST QueueEntryLookasideList
Definition: sys_arch.c:16
#define LockObject(Object)
Definition: titypes.h:34

Referenced by InternalRecvEventHandler().

◆ LibTCPFreeSocket()

void LibTCPFreeSocket ( PTCP_PCB  pcb)

Definition at line 378 of file rostcp.c.

379 {
380  struct lwip_callback_msg msg;
381 
383  msg.Input.FreeSocket.pcb = pcb;
384 
386 
387  WaitForEventSafely(&msg.Event);
388 }
static BOOLEAN WaitForEventSafely(PRKEVENT Event)
Definition: rostcp.c:179
err_t tcpip_callback_with_block(tcpip_callback_fn function, void *ctx, u8_t block)
Definition: tcpip.c:212
#define FALSE
Definition: types.h:117
static void LibTCPFreeSocketCallback(void *arg)
Definition: rostcp.c:366
struct tcp_pcb * pcb
Definition: rosip.h:35
#define KeInitializeEvent(pEvt, foo, foo2)
Definition: env_spec_w32.h:477
#define msg(x)
Definition: auth_time.c:54

◆ LibTCPFreeSocketCallback()

static void LibTCPFreeSocketCallback ( void arg)
static

Definition at line 366 of file rostcp.c.

367 {
368  struct lwip_callback_msg *msg = arg;
369 
370  ASSERT(msg);
371 
372  /* Calling tcp_close will free it */
373  tcp_close(msg->Input.FreeSocket.pcb);
374 
375  KeSetEvent(&msg->Event, IO_NO_INCREMENT, FALSE);
376 }
void * arg
Definition: msvc.h:10
LONG NTAPI KeSetEvent(IN PKEVENT Event, IN KPRIORITY Increment, IN BOOLEAN Wait)
Definition: eventobj.c:159
#define FALSE
Definition: types.h:117
#define ASSERT(a)
Definition: mode.c:44
void tcp_close(struct sock *sk, long timeout)
#define msg(x)
Definition: auth_time.c:54
#define IO_NO_INCREMENT
Definition: iotypes.h:598

Referenced by LibTCPFreeSocket().

◆ LibTCPGetDataFromConnectionQueue()

NTSTATUS LibTCPGetDataFromConnectionQueue ( PCONNECTION_ENDPOINT  Connection,
PUCHAR  RecvBuffer,
UINT  RecvLen,
UINT Received 
)

Definition at line 103 of file rostcp.c.

104 {
105  PQUEUE_ENTRY qp;
106  struct pbuf* p;
109 
110  (*Received) = 0;
111 
112  LockObject(Connection);
113 
114  if (!IsListEmpty(&Connection->PacketQueue))
115  {
116  while ((qp = LibTCPDequeuePacket(Connection)) != NULL)
117  {
118  p = qp->p;
119 
120  /* Calculate the payload length first */
121  PayloadLength = p->tot_len;
122  PayloadLength -= qp->Offset;
123  Offset = qp->Offset;
124 
125  /* Check if we're reading the whole buffer */
126  ReadLength = MIN(PayloadLength, RecvLen);
127  ASSERT(ReadLength != 0);
128  if (ReadLength != PayloadLength)
129  {
130  /* Save this one for later */
131  qp->Offset += ReadLength;
132  InsertHeadList(&Connection->PacketQueue, &qp->ListEntry);
133  qp = NULL;
134  }
135 
136  Copied = pbuf_copy_partial(p, RecvBuffer, ReadLength, Offset);
137  ASSERT(Copied == ReadLength);
138 
139  /* Update trackers */
140  RecvLen -= ReadLength;
141  RecvBuffer += ReadLength;
142  (*Received) += ReadLength;
143 
144  if (qp != NULL)
145  {
146  /* Use this special pbuf free callback function because we're outside tcpip thread */
147  pbuf_free_callback(qp->p);
148 
149  ExFreeToNPagedLookasideList(&QueueEntryLookasideList, qp);
150  }
151  else
152  {
153  /* If we get here, it means we've filled the buffer */
154  ASSERT(RecvLen == 0);
155  }
156 
157  ASSERT((*Received) != 0);
159 
160  if (!RecvLen)
161  break;
162  }
163  }
164  else
165  {
166  if (Connection->ReceiveShutdown)
167  Status = Connection->ReceiveShutdownStatus;
168  else
170  }
171 
172  UnlockObject(Connection);
173 
174  return Status;
175 }
struct pbuf * p
Definition: rosip.h:19
ULONG Offset
Definition: rosip.h:20
FORCEINLINE VOID InsertHeadList(_Inout_ PLIST_ENTRY ListHead, _Inout_ __drv_aliasesMem PLIST_ENTRY Entry)
Definition: rtlfuncs.h:201
LIST_ENTRY ListEntry
Definition: rosip.h:21
LONG NTSTATUS
Definition: precomp.h:26
_In_ PVOID _In_ ULONG _In_ PVOID _In_ ULONG PayloadLength
Definition: iotypes.h:471
NTSTATUS ReceiveShutdownStatus
Definition: titypes.h:266
err_t pbuf_free_callback(struct pbuf *p)
Definition: tcpip.c:494
_Must_inspect_result_ FORCEINLINE BOOLEAN IsListEmpty(_In_ const LIST_ENTRY *ListHead)
Definition: rtlfuncs.h:57
LIST_ENTRY PacketQueue
Definition: titypes.h:256
T MIN(T a, T b)
Definition: polytest.cpp:79
Definition: rosip.h:17
PQUEUE_ENTRY LibTCPDequeuePacket(PCONNECTION_ENDPOINT Connection)
Definition: rostcp.c:89
ULONG ReadLength
Status
Definition: gdiplustypes.h:24
#define ASSERT(a)
Definition: mode.c:44
Definition: pbuf.h:79
#define STATUS_PENDING
Definition: ntstatus.h:82
_In_ ULONG _In_ ULONG Offset
Definition: ntddpcm.h:101
u16_t pbuf_copy_partial(struct pbuf *buf, void *dataptr, u16_t len, u16_t offset)
Definition: pbuf.c:918
#define UnlockObject(Object)
Definition: titypes.h:44
BOOLEAN ReceiveShutdown
Definition: titypes.h:265
unsigned int UINT
Definition: ndis.h:50
#define NULL
Definition: types.h:112
#define STATUS_SUCCESS
Definition: shellext.h:65
GLfloat GLfloat p
Definition: glext.h:8902
NPAGED_LOOKASIDE_LIST QueueEntryLookasideList
Definition: sys_arch.c:16
UINT Received
Definition: arping.c:40
#define LockObject(Object)
Definition: titypes.h:34

Referenced by TCPReceiveData(), and TCPRecvEventHandler().

◆ LibTCPGetHostName()

err_t LibTCPGetHostName ( PTCP_PCB  pcb,
struct ip_addr *const  ipaddr,
u16_t *const  port 
)

Definition at line 847 of file rostcp.c.

848 {
849  if (!pcb)
850  return ERR_CLSD;
851 
852  *ipaddr = pcb->local_ip;
853  *port = pcb->local_port;
854 
855  return ERR_OK;
856 }
#define ERR_CLSD
Definition: err.h:67
#define ERR_OK
Definition: err.h:52
USHORT port
Definition: uri.c:228

Referenced by TCPGetSockAddress().

◆ LibTCPGetPeerName()

err_t LibTCPGetPeerName ( PTCP_PCB  pcb,
struct ip_addr *const  ipaddr,
u16_t *const  port 
)

Definition at line 859 of file rostcp.c.

860 {
861  if (!pcb)
862  return ERR_CLSD;
863 
864  *ipaddr = pcb->remote_ip;
865  *port = pcb->remote_port;
866 
867  return ERR_OK;
868 }
#define ERR_CLSD
Definition: err.h:67
#define ERR_OK
Definition: err.h:52
USHORT port
Definition: uri.c:228

Referenced by TCPCheckPeerForAccept(), and TCPGetSockAddress().

◆ LibTCPGetSocketStatus()

void LibTCPGetSocketStatus ( PTCP_PCB  pcb,
PULONG  State 
)

Definition at line 882 of file rostcp.c.

885 {
886  /* Translate state from enum tcp_state -> MIB_TCP_STATE */
887  *State = pcb->state + 1;
888 }

Referenced by TCPGetSocketStatus().

◆ LibTCPListen()

PTCP_PCB LibTCPListen ( PCONNECTION_ENDPOINT  Connection,
const u8_t  backlog 
)

Definition at line 472 of file rostcp.c.

473 {
474  struct lwip_callback_msg *msg;
475  PTCP_PCB ret;
476 
477  msg = ExAllocateFromNPagedLookasideList(&MessageLookasideList);
478  if (msg)
479  {
481  msg->Input.Listen.Connection = Connection;
482  msg->Input.Listen.Backlog = backlog;
483 
485 
486  if (WaitForEventSafely(&msg->Event))
487  ret = msg->Output.Listen.NewPcb;
488  else
489  ret = NULL;
490 
491  ExFreeToNPagedLookasideList(&MessageLookasideList, msg);
492 
493  return ret;
494  }
495 
496  return NULL;
497 }
static BOOLEAN WaitForEventSafely(PRKEVENT Event)
Definition: rostcp.c:179
err_t tcpip_callback_with_block(tcpip_callback_fn function, void *ctx, u8_t block)
Definition: tcpip.c:212
PCONNECTION_ENDPOINT Connection
Definition: rosip.h:38
#define FALSE
Definition: types.h:117
struct tcp_pcb * PTCP_PCB
Definition: rosip.h:15
int ret
#define KeInitializeEvent(pEvt, foo, foo2)
Definition: env_spec_w32.h:477
#define NULL
Definition: types.h:112
static void LibTCPListenCallback(void *arg)
Definition: rostcp.c:448
#define msg(x)
Definition: auth_time.c:54
NPAGED_LOOKASIDE_LIST MessageLookasideList
Definition: sys_arch.c:15

Referenced by TCPListen().

◆ LibTCPListenCallback()

static void LibTCPListenCallback ( void arg)
static

Definition at line 448 of file rostcp.c.

449 {
450  struct lwip_callback_msg *msg = arg;
451 
452  ASSERT(msg);
453 
454  if (!msg->Input.Listen.Connection->SocketContext)
455  {
456  msg->Output.Listen.NewPcb = NULL;
457  goto done;
458  }
459 
460  msg->Output.Listen.NewPcb = tcp_listen_with_backlog((PTCP_PCB)msg->Input.Listen.Connection->SocketContext, msg->Input.Listen.Backlog);
461 
462  if (msg->Output.Listen.NewPcb)
463  {
464  tcp_accept(msg->Output.Listen.NewPcb, InternalAcceptEventHandler);
465  }
466 
467 done:
468  KeSetEvent(&msg->Event, IO_NO_INCREMENT, FALSE);
469 }
struct sock * tcp_accept(struct sock *sk, int flags, int *err)
void * arg
Definition: msvc.h:10
LONG NTAPI KeSetEvent(IN PKEVENT Event, IN KPRIORITY Increment, IN BOOLEAN Wait)
Definition: eventobj.c:159
#define FALSE
Definition: types.h:117
static err_t InternalAcceptEventHandler(void *arg, PTCP_PCB newpcb, const err_t err)
Definition: rostcp.c:270
#define ASSERT(a)
Definition: mode.c:44
struct tcp_pcb * PTCP_PCB
Definition: rosip.h:15
#define NULL
Definition: types.h:112
#define msg(x)
Definition: auth_time.c:54
#define IO_NO_INCREMENT
Definition: iotypes.h:598

Referenced by LibTCPListen().

◆ LibTCPSend()

err_t LibTCPSend ( PCONNECTION_ENDPOINT  Connection,
void *const  dataptr,
const u16_t  len,
u32_t sent,
const int  safe 
)

Definition at line 560 of file rostcp.c.

561 {
562  err_t ret;
563  struct lwip_callback_msg *msg;
564 
565  msg = ExAllocateFromNPagedLookasideList(&MessageLookasideList);
566  if (msg)
567  {
569  msg->Input.Send.Connection = Connection;
570  msg->Input.Send.Data = dataptr;
571  msg->Input.Send.DataLength = len;
572 
573  if (safe)
575  else
577 
578  if (WaitForEventSafely(&msg->Event))
579  ret = msg->Output.Send.Error;
580  else
581  ret = ERR_CLSD;
582 
583  if (ret == ERR_OK)
584  *sent = msg->Output.Send.Information;
585  else
586  *sent = 0;
587 
588  ExFreeToNPagedLookasideList(&MessageLookasideList, msg);
589 
590  return ret;
591  }
592 
593  return ERR_MEM;
594 }
#define ERR_CLSD
Definition: err.h:67
static BOOLEAN WaitForEventSafely(PRKEVENT Event)
Definition: rostcp.c:179
err_t tcpip_callback_with_block(tcpip_callback_fn function, void *ctx, u8_t block)
Definition: tcpip.c:212
PCONNECTION_ENDPOINT Connection
Definition: rosip.h:38
#define FALSE
Definition: types.h:117
s8_t err_t
Definition: err.h:47
#define ERR_OK
Definition: err.h:52
int ret
Definition: msg.h:34
GLenum GLsizei len
Definition: glext.h:6722
static void LibTCPSendCallback(void *arg)
Definition: rostcp.c:501
#define KeInitializeEvent(pEvt, foo, foo2)
Definition: env_spec_w32.h:477
#define ERR_MEM
Definition: fontsub.h:52
#define msg(x)
Definition: auth_time.c:54
int const JOCTET * dataptr
Definition: jpeglib.h:1030
NPAGED_LOOKASIDE_LIST MessageLookasideList
Definition: sys_arch.c:15

Referenced by TCPSendData(), and TCPSendEventHandler().

◆ LibTCPSendCallback()

static void LibTCPSendCallback ( void arg)
static

Definition at line 501 of file rostcp.c.

502 {
503  struct lwip_callback_msg *msg = arg;
504  PTCP_PCB pcb = msg->Input.Send.Connection->SocketContext;
505  ULONG SendLength;
506  UCHAR SendFlags;
507 
508  ASSERT(msg);
509 
510  if (!msg->Input.Send.Connection->SocketContext)
511  {
512  msg->Output.Send.Error = ERR_CLSD;
513  goto done;
514  }
515 
516  if (msg->Input.Send.Connection->SendShutdown)
517  {
518  msg->Output.Send.Error = ERR_CLSD;
519  goto done;
520  }
521 
522  SendFlags = TCP_WRITE_FLAG_COPY;
523  SendLength = msg->Input.Send.DataLength;
524  if (tcp_sndbuf(pcb) == 0)
525  {
526  /* No buffer space so return pending */
527  msg->Output.Send.Error = ERR_INPROGRESS;
528  goto done;
529  }
530  else if (tcp_sndbuf(pcb) < SendLength)
531  {
532  /* We've got some room so let's send what we can */
533  SendLength = tcp_sndbuf(pcb);
534 
535  /* Don't set the push flag */
536  SendFlags |= TCP_WRITE_FLAG_MORE;
537  }
538 
539  msg->Output.Send.Error = tcp_write(pcb,
540  msg->Input.Send.Data,
541  SendLength,
542  SendFlags);
543  if (msg->Output.Send.Error == ERR_OK)
544  {
545  /* Queued successfully so try to send it */
546  tcp_output((PTCP_PCB)msg->Input.Send.Connection->SocketContext);
547  msg->Output.Send.Information = SendLength;
548  }
549  else if (msg->Output.Send.Error == ERR_MEM)
550  {
551  /* The queue is too long */
552  msg->Output.Send.Error = ERR_INPROGRESS;
553  }
554 
555 done:
556  KeSetEvent(&msg->Event, IO_NO_INCREMENT, FALSE);
557 }
#define ERR_CLSD
Definition: err.h:67
void * arg
Definition: msvc.h:10
LONG NTAPI KeSetEvent(IN PKEVENT Event, IN KPRIORITY Increment, IN BOOLEAN Wait)
Definition: eventobj.c:159
#define FALSE
Definition: types.h:117
struct tcp_pcb * pcb
Definition: rosip.h:35
#define ERR_OK
Definition: err.h:52
#define ASSERT(a)
Definition: mode.c:44
struct tcp_pcb * PTCP_PCB
Definition: rosip.h:15
unsigned char UCHAR
Definition: xmlstorage.h:181
#define ERR_MEM
Definition: fontsub.h:52
#define ERR_INPROGRESS
Definition: err.h:57
#define msg(x)
Definition: auth_time.c:54
unsigned int ULONG
Definition: retypes.h:1
#define IO_NO_INCREMENT
Definition: iotypes.h:598

Referenced by LibTCPSend().

◆ LibTCPSetNoDelay()

void LibTCPSetNoDelay ( PTCP_PCB  pcb,
BOOLEAN  Set 
)

Definition at line 871 of file rostcp.c.

874 {
875  if (Set)
876  pcb->flags |= TF_NODELAY;
877  else
878  pcb->flags &= ~TF_NODELAY;
879 }
static BOOL Set
Definition: pageheap.c:10
struct tcp_pcb * pcb
Definition: rosip.h:35

Referenced by TCPSetNoDelay().

◆ LibTCPShutdown()

err_t LibTCPShutdown ( PCONNECTION_ENDPOINT  Connection,
const int  shut_rx,
const int  shut_tx 
)

Definition at line 720 of file rostcp.c.

721 {
722  struct lwip_callback_msg *msg;
723  err_t ret;
724 
725  msg = ExAllocateFromNPagedLookasideList(&MessageLookasideList);
726  if (msg)
727  {
729 
730  msg->Input.Shutdown.Connection = Connection;
731  msg->Input.Shutdown.shut_rx = shut_rx;
732  msg->Input.Shutdown.shut_tx = shut_tx;
733 
735 
736  if (WaitForEventSafely(&msg->Event))
737  ret = msg->Output.Shutdown.Error;
738  else
739  ret = ERR_CLSD;
740 
741  ExFreeToNPagedLookasideList(&MessageLookasideList, msg);
742 
743  return ret;
744  }
745 
746  return ERR_MEM;
747 }
#define ERR_CLSD
Definition: err.h:67
static BOOLEAN WaitForEventSafely(PRKEVENT Event)
Definition: rostcp.c:179
err_t tcpip_callback_with_block(tcpip_callback_fn function, void *ctx, u8_t block)
Definition: tcpip.c:212
PCONNECTION_ENDPOINT Connection
Definition: rosip.h:38
#define FALSE
Definition: types.h:117
s8_t err_t
Definition: err.h:47
static void LibTCPShutdownCallback(void *arg)
Definition: rostcp.c:657
int ret
#define KeInitializeEvent(pEvt, foo, foo2)
Definition: env_spec_w32.h:477
#define ERR_MEM
Definition: fontsub.h:52
#define msg(x)
Definition: auth_time.c:54
NPAGED_LOOKASIDE_LIST MessageLookasideList
Definition: sys_arch.c:15

Referenced by DisconnectWorker(), and TCPDisconnect().

◆ LibTCPShutdownCallback()

static void LibTCPShutdownCallback ( void arg)
static

Definition at line 657 of file rostcp.c.

658 {
659  struct lwip_callback_msg *msg = arg;
660  PTCP_PCB pcb = msg->Input.Shutdown.Connection->SocketContext;
661 
662  if (!msg->Input.Shutdown.Connection->SocketContext)
663  {
664  msg->Output.Shutdown.Error = ERR_CLSD;
665  goto done;
666  }
667 
668  /* LwIP makes the (questionable) assumption that SHUTDOWN_RDWR is equivalent to tcp_close().
669  * This assumption holds even if the shutdown calls are done separately (even through multiple
670  * WinSock shutdown() calls). This assumption means that lwIP has the right to deallocate our
671  * PCB without telling us if we shutdown TX and RX. To avoid these problems, we'll clear the
672  * socket context if we have called shutdown for TX and RX.
673  */
674  if (msg->Input.Shutdown.shut_rx != msg->Input.Shutdown.shut_tx) {
675  if (msg->Input.Shutdown.shut_rx) {
676  msg->Output.Shutdown.Error = tcp_shutdown(pcb, TRUE, FALSE);
677  }
678  if (msg->Input.Shutdown.shut_tx) {
679  msg->Output.Shutdown.Error = tcp_shutdown(pcb, FALSE, TRUE);
680  }
681  }
682  else if (msg->Input.Shutdown.shut_rx) {
683  /* We received both RX and TX requests, which seems to mean closing connection from TDI.
684  * So call tcp_close, otherwise we risk to be put in TCP_WAIT_* states, which makes further
685  * attempts to close the socket to fail in this state.
686  */
687  msg->Output.Shutdown.Error = tcp_close(pcb);
688  }
689  else {
690  /* This case shouldn't happen */
691  DbgPrint("Requested socket shutdown(0, 0) !\n");
692  }
693 
694  if (!msg->Output.Shutdown.Error)
695  {
696  if (msg->Input.Shutdown.shut_rx)
697  {
698  msg->Input.Shutdown.Connection->ReceiveShutdown = TRUE;
699  msg->Input.Shutdown.Connection->ReceiveShutdownStatus = STATUS_FILE_CLOSED;
700  }
701 
702  if (msg->Input.Shutdown.shut_tx)
703  msg->Input.Shutdown.Connection->SendShutdown = TRUE;
704 
705  if (msg->Input.Shutdown.Connection->ReceiveShutdown &&
706  msg->Input.Shutdown.Connection->SendShutdown)
707  {
708  /* The PCB is not ours anymore */
709  msg->Input.Shutdown.Connection->SocketContext = NULL;
710  tcp_arg(pcb, NULL);
711  TCPFinEventHandler(msg->Input.Shutdown.Connection, ERR_CLSD);
712  }
713  }
714 
715 done:
716  KeSetEvent(&msg->Event, IO_NO_INCREMENT, FALSE);
717 }
#define DbgPrint
Definition: hal.h:12
#define ERR_CLSD
Definition: err.h:67
#define TRUE
Definition: types.h:120
VOID TCPFinEventHandler(void *arg, const err_t err)
Definition: event.c:182
void * arg
Definition: msvc.h:10
LONG NTAPI KeSetEvent(IN PKEVENT Event, IN KPRIORITY Increment, IN BOOLEAN Wait)
Definition: eventobj.c:159
#define FALSE
Definition: types.h:117
struct tcp_pcb * pcb
Definition: rosip.h:35
struct tcp_pcb * PTCP_PCB
Definition: rosip.h:15
void tcp_shutdown(struct sock *sk, int how)
void tcp_close(struct sock *sk, long timeout)
#define NULL
Definition: types.h:112
#define msg(x)
Definition: auth_time.c:54
#define IO_NO_INCREMENT
Definition: iotypes.h:598
#define STATUS_FILE_CLOSED
Definition: ntstatus.h:532

Referenced by LibTCPShutdown().

◆ LibTCPSocket()

struct tcp_pcb* LibTCPSocket ( void arg)

Definition at line 339 of file rostcp.c.

340 {
341  struct lwip_callback_msg *msg = ExAllocateFromNPagedLookasideList(&MessageLookasideList);
342  struct tcp_pcb *ret;
343 
344  if (msg)
345  {
347  msg->Input.Socket.Arg = arg;
348 
350 
351  if (WaitForEventSafely(&msg->Event))
352  ret = msg->Output.Socket.NewPcb;
353  else
354  ret = NULL;
355 
356  ExFreeToNPagedLookasideList(&MessageLookasideList, msg);
357 
358  return ret;
359  }
360 
361  return NULL;
362 }
static BOOLEAN WaitForEventSafely(PRKEVENT Event)
Definition: rostcp.c:179
err_t tcpip_callback_with_block(tcpip_callback_fn function, void *ctx, u8_t block)
Definition: tcpip.c:212
void * arg
Definition: msvc.h:10
#define FALSE
Definition: types.h:117
int ret
#define KeInitializeEvent(pEvt, foo, foo2)
Definition: env_spec_w32.h:477
static void LibTCPSocketCallback(void *arg)
Definition: rostcp.c:321
#define NULL
Definition: types.h:112
#define msg(x)
Definition: auth_time.c:54
NPAGED_LOOKASIDE_LIST MessageLookasideList
Definition: sys_arch.c:15

Referenced by TCPSocket().

◆ LibTCPSocketCallback()

static void LibTCPSocketCallback ( void arg)
static

Definition at line 321 of file rostcp.c.

322 {
323  struct lwip_callback_msg *msg = arg;
324 
325  ASSERT(msg);
326 
327  msg->Output.Socket.NewPcb = tcp_new();
328 
329  if (msg->Output.Socket.NewPcb)
330  {
331  tcp_arg(msg->Output.Socket.NewPcb, msg->Input.Socket.Arg);
332  tcp_err(msg->Output.Socket.NewPcb, InternalErrorEventHandler);
333  }
334 
335  KeSetEvent(&msg->Event, IO_NO_INCREMENT, FALSE);
336 }
void * arg
Definition: msvc.h:10
LONG NTAPI KeSetEvent(IN PKEVENT Event, IN KPRIORITY Increment, IN BOOLEAN Wait)
Definition: eventobj.c:159
#define FALSE
Definition: types.h:117
#define ASSERT(a)
Definition: mode.c:44
static void InternalErrorEventHandler(void *arg, const err_t err)
Definition: rostcp.c:300
#define msg(x)
Definition: auth_time.c:54
#define IO_NO_INCREMENT
Definition: iotypes.h:598

Referenced by LibTCPSocket().

◆ TCPTranslateError()

NTSTATUS TCPTranslateError ( const err_t  err)

Definition at line 267 of file tcp.c.

268 {
270 
271  switch (err)
272  {
273  case ERR_OK: Status = STATUS_SUCCESS; return Status; //0
274  case ERR_MEM: Status = STATUS_INSUFFICIENT_RESOURCES; break; //-1
275  case ERR_BUF: Status = STATUS_BUFFER_TOO_SMALL; break; //-2
276  case ERR_TIMEOUT: Status = STATUS_TIMEOUT; break; // -3
277  case ERR_RTE: Status = STATUS_NETWORK_UNREACHABLE; break; //-4
278  case ERR_INPROGRESS: Status = STATUS_PENDING; return Status; //-5
279  case ERR_VAL: Status = STATUS_INVALID_PARAMETER; break; //-6
280  case ERR_WOULDBLOCK: Status = STATUS_CANT_WAIT; break; //-7
281  case ERR_USE: Status = STATUS_ADDRESS_ALREADY_EXISTS; break; //-8
282  case ERR_ISCONN: Status = STATUS_UNSUCCESSFUL; break; //-9 (FIXME)
283  case ERR_ABRT: Status = STATUS_LOCAL_DISCONNECT; break; //-10
284  case ERR_RST: Status = STATUS_REMOTE_DISCONNECT; break; //-11
285  case ERR_CLSD: Status = STATUS_FILE_CLOSED; break; //-12
286  case ERR_CONN: Status = STATUS_INVALID_CONNECTION; break; //-13
287  case ERR_ARG: Status = STATUS_INVALID_PARAMETER; break; //-14
288  case ERR_IF: Status = STATUS_UNEXPECTED_NETWORK_ERROR; break; //-15
289  default:
290  DbgPrint("Invalid error value: %d\n", err);
291  ASSERT(FALSE);
293  break;
294  }
295 
296  TI_DbgPrint(DEBUG_TCP,("TCP operation failed: 0x%x (%d)\n", Status, err));
297 
298  return Status;
299 }
#define ERR_CONN
Definition: err.h:68
#define TI_DbgPrint(_t_, _x_)
Definition: debug.h:45
#define STATUS_INVALID_CONNECTION
Definition: ntstatus.h:556
#define ERR_ARG
Definition: err.h:70
#define ERR_ISCONN
Definition: err.h:61
#define STATUS_INSUFFICIENT_RESOURCES
Definition: udferr_usr.h:158
#define DbgPrint
Definition: hal.h:12
#define ERR_RTE
Definition: err.h:56
#define ERR_CLSD
Definition: err.h:67
#define STATUS_INVALID_PARAMETER
Definition: udferr_usr.h:135
#define ERR_USE
Definition: err.h:60
LONG NTSTATUS
Definition: precomp.h:26
#define DEBUG_TCP
Definition: debug.h:28
#define STATUS_ADDRESS_ALREADY_EXISTS
Definition: ntstatus.h:654
#define STATUS_BUFFER_TOO_SMALL
Definition: shellext.h:69
#define ERR_TIMEOUT
Definition: err.h:55
#define STATUS_TIMEOUT
Definition: ntstatus.h:81
#define FALSE
Definition: types.h:117
#define STATUS_REMOTE_DISCONNECT
Definition: ntstatus.h:552
#define ERR_VAL
Definition: err.h:58
#define ERR_OK
Definition: err.h:52
Status
Definition: gdiplustypes.h:24
#define ASSERT(a)
Definition: mode.c:44
#define STATUS_PENDING
Definition: ntstatus.h:82
#define STATUS_UNSUCCESSFUL
Definition: udferr_usr.h:132
#define ERR_RST
Definition: err.h:66
#define err(...)
#define ERR_MEM
Definition: fontsub.h:52
#define ERR_BUF
Definition: err.h:54
#define STATUS_LOCAL_DISCONNECT
Definition: ntstatus.h:551
#define ERR_INPROGRESS
Definition: err.h:57
#define ERR_WOULDBLOCK
Definition: err.h:59
#define STATUS_UNEXPECTED_NETWORK_ERROR
Definition: ntstatus.h:432
#define ERR_ABRT
Definition: err.h:65
#define ERR_IF
Definition: err.h:72
#define STATUS_SUCCESS
Definition: shellext.h:65
#define STATUS_NETWORK_UNREACHABLE
Definition: ntstatus.h:704
#define STATUS_CANT_WAIT
Definition: ntstatus.h:452
#define STATUS_FILE_CLOSED
Definition: ntstatus.h:532

Referenced by DisconnectWorker(), InternalErrorEventHandler(), TCPConnect(), TCPDisconnect(), TCPGetSockAddress(), and TCPSendData().

◆ WaitForEventSafely()

static BOOLEAN WaitForEventSafely ( PRKEVENT  Event)
static

Definition at line 179 of file rostcp.c.

180 {
181  PVOID WaitObjects[] = {Event, &TerminationEvent};
182 
184  WaitObjects,
185  WaitAny,
186  Executive,
187  KernelMode,
188  FALSE,
189  NULL,
190  NULL) == STATUS_WAIT_0)
191  {
192  /* Signalled by the caller's event */
193  return TRUE;
194  }
195  else /* if KeWaitForMultipleObjects() == STATUS_WAIT_1 */
196  {
197  /* Signalled by our termination event */
198  return FALSE;
199  }
200 }
#define TRUE
Definition: types.h:120
#define STATUS_WAIT_0
Definition: ntstatus.h:237
#define FALSE
Definition: types.h:117
KEVENT TerminationEvent
Definition: sys_arch.c:14
_In_ PVOID _In_ ULONG Event
Definition: iotypes.h:467
NTSTATUS NTAPI KeWaitForMultipleObjects(IN ULONG Count, IN PVOID Object[], IN WAIT_TYPE WaitType, IN KWAIT_REASON WaitReason, IN KPROCESSOR_MODE WaitMode, IN BOOLEAN Alertable, IN PLARGE_INTEGER Timeout OPTIONAL, OUT PKWAIT_BLOCK WaitBlockArray OPTIONAL)
Definition: wait.c:586
#define NULL
Definition: types.h:112

Referenced by LibTCPBind(), LibTCPClose(), LibTCPConnect(), LibTCPFreeSocket(), LibTCPListen(), LibTCPSend(), LibTCPShutdown(), and LibTCPSocket().

Variable Documentation

◆ MessageLookasideList

◆ QueueEntryLookasideList

◆ tcp_state_str

const char* const tcp_state_str[]
static
Initial value:
= {
"CLOSED",
"LISTEN",
"SYN_SENT",
"SYN_RCVD",
"ESTABLISHED",
"FIN_WAIT_1",
"FIN_WAIT_2",
"CLOSE_WAIT",
"CLOSING",
"LAST_ACK",
"TIME_WAIT"
}

Definition at line 9 of file rostcp.c.

Referenced by LibTCPDumpPcb().

◆ TerminationEvent

KEVENT TerminationEvent