ReactOS  0.4.15-dev-3308-g9455def
rostcp.c
Go to the documentation of this file.
1 #include "lwip/sys.h"
2 #include "lwip/netif.h"
3 #include "lwip/tcpip.h"
4 
5 #include "rosip.h"
6 
7 #include <debug.h>
8 
9 static const char * const tcp_state_str[] = {
10  "CLOSED",
11  "LISTEN",
12  "SYN_SENT",
13  "SYN_RCVD",
14  "ESTABLISHED",
15  "FIN_WAIT_1",
16  "FIN_WAIT_2",
17  "CLOSE_WAIT",
18  "CLOSING",
19  "LAST_ACK",
20  "TIME_WAIT"
21 };
22 
23 /* The way that lwIP does multi-threading is really not ideal for our purposes but
24  * we best go along with it unless we want another unstable TCP library. lwIP uses
25  * a thread called the "tcpip thread" which is the only one allowed to call raw API
26  * functions. Since this is the case, for each of our LibTCP* functions, we queue a request
27  * for a callback to "tcpip thread" which calls our LibTCP*Callback functions. Yes, this is
28  * a lot of unnecessary thread swapping and it could definitely be faster, but I don't want
29  * to going messing around in lwIP because I have no desire to create another mess like oskittcp */
30 
34 
35 /* Required for ERR_T to NTSTATUS translation in receive error handling */
37 
38 void
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 }
52 
53 static
54 void
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 }
75 
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 }
88 
90 {
92  PQUEUE_ENTRY qp = NULL;
93 
94  if (IsListEmpty(&Connection->PacketQueue)) return NULL;
95 
96  Entry = RemoveHeadList(&Connection->PacketQueue);
97 
98  qp = CONTAINING_RECORD(Entry, QUEUE_ENTRY, ListEntry);
99 
100  return qp;
101 }
102 
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 }
176 
177 static
178 BOOLEAN
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 }
201 
202 static
203 err_t
205 {
206  /* Make sure the socket didn't get closed */
207  if (!arg) return ERR_OK;
208 
210 
211  return ERR_OK;
212 }
213 
214 static
215 err_t
216 InternalRecvEventHandler(void *arg, PTCP_PCB pcb, struct pbuf *p, const err_t err)
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 }
265 
266 /* This function MUST return an error value that is not ERR_ABRT or ERR_OK if the connection
267  * is not accepted to avoid leaking the new PCB */
268 static
269 err_t
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 }
284 
285 static
286 err_t
288 {
289  /* Make sure the socket didn't get closed */
290  if (!arg)
291  return ERR_OK;
292 
294 
295  return ERR_OK;
296 }
297 
298 static
299 void
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 }
318 
319 static
320 void
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 }
337 
338 struct tcp_pcb *
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 }
363 
364 static
365 void
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 }
377 
379 {
380  struct lwip_callback_msg msg;
381 
383  msg.Input.FreeSocket.pcb = pcb;
384 
386 
387  WaitForEventSafely(&msg.Event);
388 }
389 
390 
391 static
392 void
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 }
416 
417 err_t
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 }
445 
446 static
447 void
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 }
470 
471 PTCP_PCB
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 }
498 
499 static
500 void
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 }
558 
559 err_t
560 LibTCPSend(PCONNECTION_ENDPOINT Connection, void *const dataptr, const u16_t len, u32_t *sent, const int safe)
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 }
595 
596 static
597 void
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 }
623 
624 err_t
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 }
654 
655 static
656 void
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 }
718 
719 err_t
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 }
748 
749 static
750 void
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 }
799 
800 err_t
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 }
831 
832 void
833 LibTCPAccept(PTCP_PCB pcb, struct tcp_pcb *listen_pcb, void *arg)
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 }
845 
846 err_t
847 LibTCPGetHostName(PTCP_PCB pcb, struct ip_addr *const ipaddr, u16_t *const port)
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 }
857 
858 err_t
859 LibTCPGetPeerName(PTCP_PCB pcb, struct ip_addr * const ipaddr, u16_t * const port)
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 }
869 
870 void
872  PTCP_PCB pcb,
873  BOOLEAN Set)
874 {
875  if (Set)
876  pcb->flags |= TF_NODELAY;
877  else
878  pcb->flags &= ~TF_NODELAY;
879 }
880 
881 void
883  PTCP_PCB pcb,
884  PULONG State)
885 {
886  /* Translate state from enum tcp_state -> MIB_TCP_STATE */
887  *State = pcb->state + 1;
888 }
struct _QUEUE_ENTRY * PQUEUE_ENTRY
err_t LibTCPClose(PCONNECTION_ENDPOINT Connection, const int safe, const int callback)
Definition: rostcp.c:801
err_t LibTCPBind(PCONNECTION_ENDPOINT Connection, struct ip_addr *const ipaddr, const u16_t port)
Definition: rostcp.c:418
static const char *const tcp_state_str[]
Definition: rostcp.c:9
struct pbuf * p
Definition: rosip.h:19
struct LOOKASIDE_ALIGN _NPAGED_LOOKASIDE_LIST NPAGED_LOOKASIDE_LIST
NTSTATUS TCPTranslateError(const err_t err)
Definition: tcp.c:267
static err_t InternalRecvEventHandler(void *arg, PTCP_PCB pcb, struct pbuf *p, const err_t err)
Definition: rostcp.c:216
#define DbgPrint
Definition: hal.h:12
struct _Entry Entry
Definition: kefuncs.h:627
static void LibTCPEmptyQueue(PCONNECTION_ENDPOINT Connection)
Definition: rostcp.c:55
PTCP_PCB LibTCPListen(PCONNECTION_ENDPOINT Connection, const u8_t backlog)
Definition: rostcp.c:472
err_t LibTCPGetPeerName(PTCP_PCB pcb, struct ip_addr *const ipaddr, u16_t *const port)
Definition: rostcp.c:859
STREAM tcp_recv(STREAM s, uint32 length)
Definition: tcp.c:344
ULONG Offset
Definition: rosip.h:20
#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
FORCEINLINE VOID InsertHeadList(_Inout_ PLIST_ENTRY ListHead, _Inout_ __drv_aliasesMem PLIST_ENTRY Entry)
Definition: rtlfuncs.h:201
LIST_ENTRY ListEntry
Definition: rosip.h:21
unsigned char * PUCHAR
Definition: retypes.h:3
LONG NTSTATUS
Definition: precomp.h:26
static BOOLEAN WaitForEventSafely(PRKEVENT Event)
Definition: rostcp.c:179
struct sock * tcp_accept(struct sock *sk, int flags, int *err)
VOID TCPRecvEventHandler(void *arg)
Definition: event.c:380
_In_ PVOID _In_ ULONG _In_ PVOID _In_ ULONG PayloadLength
Definition: iotypes.h:471
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
NTSTATUS ReceiveShutdownStatus
Definition: titypes.h:266
PCONNECTION_ENDPOINT Connection
Definition: rosip.h:38
void * arg
Definition: msvc.h:10
err_t LibTCPConnect(PCONNECTION_ENDPOINT Connection, struct ip_addr *const ipaddr, const u16_t port)
Definition: rostcp.c:625
#define InsertTailList(ListHead, Entry)
LONG NTAPI KeSetEvent(IN PKEVENT Event, IN KPRIORITY Increment, IN BOOLEAN Wait)
Definition: eventobj.c:159
_In_ USHORT _In_ ULONG _In_ PSOCKADDR _In_ PSOCKADDR _Reserved_ ULONG _In_opt_ PVOID SocketContext
Definition: wsk.h:182
void LibTCPFreeSocket(PTCP_PCB pcb)
Definition: rostcp.c:378
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
static BOOL Set
Definition: pageheap.c:10
#define DereferenceObject(Object)
Definition: titypes.h:24
T MIN(T a, T b)
Definition: polytest.cpp:79
VOID TCPConnectEventHandler(void *arg, const err_t err)
Definition: event.c:420
#define STATUS_WAIT_0
Definition: ntstatus.h:237
void LibTCPAccept(PTCP_PCB pcb, struct tcp_pcb *listen_pcb, void *arg)
Definition: rostcp.c:833
Definition: rosip.h:17
#define FALSE
Definition: types.h:117
PQUEUE_ENTRY LibTCPDequeuePacket(PCONNECTION_ENDPOINT Connection)
Definition: rostcp.c:89
KEVENT TerminationEvent
Definition: sys_arch.c:14
static void LibTCPFreeSocketCallback(void *arg)
Definition: rostcp.c:366
void LibTCPEnqueuePacket(PCONNECTION_ENDPOINT Connection, struct pbuf *p)
Definition: rostcp.c:76
_In_ PVOID _In_ ULONG Event
Definition: iotypes.h:467
struct tcp_pcb * pcb
Definition: rosip.h:35
unsigned char BOOLEAN
FORCEINLINE PLIST_ENTRY RemoveHeadList(_Inout_ PLIST_ENTRY ListHead)
Definition: rtlfuncs.h:128
u8_t pbuf_free(struct pbuf *p)
Definition: pbuf.c:618
s8_t err_t
Definition: err.h:47
ULONG ReadLength
VOID TCPAcceptEventHandler(void *arg, PTCP_PCB newpcb)
Definition: event.c:231
static err_t InternalAcceptEventHandler(void *arg, PTCP_PCB newpcb, const err_t err)
Definition: rostcp.c:270
BOOLEAN SendShutdown
Definition: titypes.h:264
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 ERR_OK
Definition: err.h:52
Status
Definition: gdiplustypes.h:24
static err_t InternalSendEventHandler(void *arg, PTCP_PCB pcb, const u16_t space)
Definition: rostcp.c:204
#define SOF_REUSEADDR
Definition: ip.h:99
void LibTCPDumpPcb(PVOID SocketContext)
Definition: rostcp.c:39
#define ASSERT(a)
Definition: mode.c:44
static void LibTCPShutdownCallback(void *arg)
Definition: rostcp.c:657
Definition: pbuf.h:79
unsigned long u32_t
Definition: cc.h:25
#define STATUS_PENDING
Definition: ntstatus.h:82
struct tcp_pcb * PTCP_PCB
Definition: rosip.h:15
NTSTATUS LibTCPGetDataFromConnectionQueue(PCONNECTION_ENDPOINT Connection, PUCHAR RecvBuffer, UINT RecvLen, UINT *Received)
Definition: rostcp.c:103
BOOL Error
Definition: chkdsk.c:66
RD_BOOL tcp_connect(char *server)
Definition: tcp.c:717
void tcp_shutdown(struct sock *sk, int how)
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
static err_t InternalConnectEventHandler(void *arg, PTCP_PCB pcb, const err_t err)
Definition: rostcp.c:287
unsigned char UCHAR
Definition: xmlstorage.h:181
int ret
GLenum const GLvoid * addr
Definition: glext.h:9621
Definition: msg.h:34
void LibTCPSetNoDelay(PTCP_PCB pcb, BOOLEAN Set)
Definition: rostcp.c:871
void tcp_close(struct sock *sk, long timeout)
#define ntohs(x)
Definition: module.h:208
GLenum GLsizei len
Definition: glext.h:6722
#define ReferenceObject(Object)
Definition: titypes.h:14
err_t LibTCPSend(PCONNECTION_ENDPOINT Connection, void *const dataptr, const u16_t len, u32_t *sent, const int safe)
Definition: rostcp.c:560
Definition: typedefs.h:119
#define err(...)
static void LibTCPSendCallback(void *arg)
Definition: rostcp.c:501
_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 KeInitializeEvent(pEvt, foo, foo2)
Definition: env_spec_w32.h:477
#define UnlockObject(Object)
Definition: titypes.h:44
BOOLEAN ReceiveShutdown
Definition: titypes.h:265
static void LibTCPSocketCallback(void *arg)
Definition: rostcp.c:321
#define ERR_MEM
Definition: fontsub.h:52
static void LibTCPCloseCallback(void *arg)
Definition: rostcp.c:751
err_t LibTCPGetHostName(PTCP_PCB pcb, struct ip_addr *const ipaddr, u16_t *const port)
Definition: rostcp.c:847
unsigned char u8_t
Definition: cc.h:23
struct tcp_pcb * LibTCPSocket(void *arg)
Definition: rostcp.c:339
unsigned int * PULONG
Definition: retypes.h:1
unsigned int UINT
Definition: ndis.h:50
#define NULL
Definition: types.h:112
err_t LibTCPShutdown(PCONNECTION_ENDPOINT Connection, const int shut_rx, const int shut_tx)
Definition: rostcp.c:720
#define ntohl(x)
Definition: module.h:203
static void LibTCPListenCallback(void *arg)
Definition: rostcp.c:448
static void InternalErrorEventHandler(void *arg, const err_t err)
Definition: rostcp.c:300
#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
int const JOCTET * dataptr
Definition: jpeglib.h:1030
static void LibTCPBindCallback(void *arg)
Definition: rostcp.c:393
unsigned short u16_t
Definition: cc.h:24
VOID TCPSendEventHandler(void *arg, const u16_t space)
Definition: event.c:294
#define STATUS_SUCCESS
Definition: shellext.h:65
static IPrintDialogCallback callback
Definition: printdlg.c:326
GLfloat GLfloat p
Definition: glext.h:8902
NPAGED_LOOKASIDE_LIST QueueEntryLookasideList
Definition: sys_arch.c:16
USHORT port
Definition: uri.c:228
void LibTCPGetSocketStatus(PTCP_PCB pcb, PULONG State)
Definition: rostcp.c:882
UINT Received
Definition: arping.c:40
base of all file and directory entries
Definition: entries.h:82
NPAGED_LOOKASIDE_LIST MessageLookasideList
Definition: sys_arch.c:15
#define STATUS_FILE_CLOSED
Definition: ntstatus.h:532
#define LockObject(Object)
Definition: titypes.h:34