ReactOS  0.4.14-dev-593-g1793dcc
tcpip.c File Reference
#include "lwip/opt.h"
#include "lwip/sys.h"
#include "lwip/memp.h"
#include "lwip/mem.h"
#include "lwip/pbuf.h"
#include "lwip/tcpip.h"
#include "lwip/init.h"
#include "lwip/ip.h"
#include "netif/etharp.h"
#include "netif/ppp_oe.h"
Include dependency graph for tcpip.c:

Go to the source code of this file.

Functions

static void tcpip_thread (void *arg)
 
err_t tcpip_input (struct pbuf *p, struct netif *inp)
 
err_t tcpip_callback_with_block (tcpip_callback_fn function, void *ctx, u8_t block)
 
struct tcpip_callback_msg * tcpip_callbackmsg_new (tcpip_callback_fn function, void *ctx)
 
void tcpip_callbackmsg_delete (struct tcpip_callback_msg *msg)
 
err_t tcpip_trycallback (struct tcpip_callback_msg *msg)
 
void tcpip_init (tcpip_init_done_fn initfunc, void *arg)
 
static void pbuf_free_int (void *p)
 
err_t pbuf_free_callback (struct pbuf *p)
 
err_t mem_free_callback (void *m)
 

Variables

static tcpip_init_done_fn tcpip_init_done
 
static voidtcpip_init_done_arg
 
static sys_mbox_t mbox
 

Detailed Description

Sequential API Main thread module

Definition in file tcpip.c.

Function Documentation

◆ mem_free_callback()

err_t mem_free_callback ( void m)

A simple wrapper function that allows you to free heap memory from interrupt context.

Parameters
mthe heap memory to free
Returns
ERR_OK if callback could be enqueued, an err_t if not

Definition at line 507 of file tcpip.c.

508 {
510 }
err_t tcpip_callback_with_block(tcpip_callback_fn function, void *ctx, u8_t block)
Definition: tcpip.c:212
const GLfloat * m
Definition: glext.h:10848
#define mem_free(ptr, bsize)
Definition: types.h:124

◆ pbuf_free_callback()

err_t pbuf_free_callback ( struct pbuf p)

A simple wrapper function that allows you to free a pbuf from interrupt context.

Parameters
pThe pbuf (chain) to be dereferenced.
Returns
ERR_OK if callback could be enqueued, an err_t if not

Definition at line 494 of file tcpip.c.

495 {
497 }
static void pbuf_free_int(void *p)
Definition: tcpip.c:481
err_t tcpip_callback_with_block(tcpip_callback_fn function, void *ctx, u8_t block)
Definition: tcpip.c:212
GLfloat GLfloat p
Definition: glext.h:8902

Referenced by LibTCPGetDataFromConnectionQueue().

◆ pbuf_free_int()

static void pbuf_free_int ( void p)
static

Simple callback function used with tcpip_callback to free a pbuf (pbuf_free has a wrong signature for tcpip_callback)

Parameters
pThe pbuf (chain) to be dereferenced.

Definition at line 481 of file tcpip.c.

482 {
483  struct pbuf *q = (struct pbuf *)p;
484  pbuf_free(q);
485 }
u8_t pbuf_free(struct pbuf *p)
Definition: pbuf.c:618
Definition: pbuf.h:79
GLdouble GLdouble GLdouble GLdouble q
Definition: gl.h:2063
GLfloat GLfloat p
Definition: glext.h:8902

Referenced by pbuf_free_callback().

◆ tcpip_callback_with_block()

err_t tcpip_callback_with_block ( tcpip_callback_fn  function,
void ctx,
u8_t  block 
)

Call a specific function in the thread context of tcpip_thread for easy access synchronization. A function called in that way may access lwIP core code without fearing concurrent access.

Parameters
fthe function to call
ctxparameter passed to f
block1 to block until the request is posted, 0 to non-blocking mode
Returns
ERR_OK if the function was called, another err_t if not

Definition at line 212 of file tcpip.c.

213 {
214  struct tcpip_msg *msg;
215 
216  if (sys_mbox_valid(&mbox)) {
217  msg = (struct tcpip_msg *)memp_malloc(MEMP_TCPIP_MSG_API);
218  if (msg == NULL) {
219  return ERR_MEM;
220  }
221 
222  msg->type = TCPIP_MSG_CALLBACK;
223  msg->msg.cb.function = function;
224  msg->msg.cb.ctx = ctx;
225  if (block) {
227  } else {
228  if (sys_mbox_trypost(&mbox, msg) != ERR_OK) {
229  memp_free(MEMP_TCPIP_MSG_API, msg);
230  return ERR_MEM;
231  }
232  }
233  return ERR_OK;
234  }
235  return ERR_VAL;
236 }
static unsigned int block
Definition: xmlmemory.c:118
int sys_mbox_valid(sys_mbox_t *mbox)
Definition: sys_arch.c:146
void memp_free(memp_t type, void *mem)
Definition: memp.c:435
#define ERR_VAL
Definition: err.h:58
smooth NULL
Definition: ftsmooth.c:416
#define ERR_OK
Definition: err.h:52
#define ERR_MEM
Definition: fontsub.h:52
err_t sys_mbox_trypost(sys_mbox_t *mbox, void *msg)
Definition: sys_arch.c:252
void * ctx
Definition: tcpip.h:149
#define msg(x)
Definition: auth_time.c:54
void sys_mbox_post(sys_mbox_t *mbox, void *msg)
Definition: sys_arch.c:165
void * memp_malloc(memp_t type)
Definition: memp.c:390
static sys_mbox_t mbox
Definition: tcpip.c:56

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

◆ tcpip_callbackmsg_delete()

void tcpip_callbackmsg_delete ( struct tcpip_callback_msg *  msg)

Free a callback message allocated by tcpip_callbackmsg_new().

Parameters
msgthe message to free

Definition at line 426 of file tcpip.c.

427 {
428  memp_free(MEMP_TCPIP_MSG_API, msg);
429 }
void memp_free(memp_t type, void *mem)
Definition: memp.c:435
#define msg(x)
Definition: auth_time.c:54

◆ tcpip_callbackmsg_new()

struct tcpip_callback_msg* tcpip_callbackmsg_new ( tcpip_callback_fn  function,
void ctx 
)

Allocate a structure for a static callback message and initialize it. This is intended to be used to send "static" messages from interrupt context.

Parameters
functionthe function to call
ctxparameter passed to function
Returns
a struct pointer to pass to tcpip_trycallback().

Definition at line 409 of file tcpip.c.

410 {
411  struct tcpip_msg *msg = (struct tcpip_msg *)memp_malloc(MEMP_TCPIP_MSG_API);
412  if (msg == NULL) {
413  return NULL;
414  }
416  msg->msg.cb.function = function;
417  msg->msg.cb.ctx = ctx;
418  return (struct tcpip_callback_msg*)msg;
419 }
smooth NULL
Definition: ftsmooth.c:416
void * ctx
Definition: tcpip.h:149
#define msg(x)
Definition: auth_time.c:54
void * memp_malloc(memp_t type)
Definition: memp.c:390

◆ tcpip_init()

void tcpip_init ( tcpip_init_done_fn  initfunc,
void arg 
)

Initialize this module:

  • initialize all sub modules
  • start the tcpip_thread
Parameters
initfunca function to call when tcpip_thread is running and finished initializing
argargument to pass to initfunc

Definition at line 456 of file tcpip.c.

457 {
458  lwip_init();
459 
460  tcpip_init_done = initfunc;
463  LWIP_ASSERT("failed to create tcpip_thread mbox", 0);
464  }
465 #if LWIP_TCPIP_CORE_LOCKING
466  if(sys_mutex_new(&lock_tcpip_core) != ERR_OK) {
467  LWIP_ASSERT("failed to create lock_tcpip_core", 0);
468  }
469 #endif /* LWIP_TCPIP_CORE_LOCKING */
470 
472 }
#define TCPIP_THREAD_NAME
Definition: opt.h:1241
sys_thread_t sys_thread_new(const char *name, lwip_thread_fn thread, void *arg, int stacksize, int prio)
Definition: sys_arch.c:280
err_t sys_mutex_new(sys_mutex_t *mutex)
#define TCPIP_THREAD_PRIO
Definition: opt.h:1259
void * arg
Definition: msvc.h:10
static void tcpip_thread(void *arg)
Definition: tcpip.c:75
#define TCPIP_MBOX_SIZE
Definition: opt.h:1268
#define LWIP_ASSERT(message, assertion)
Definition: debug.h:66
smooth NULL
Definition: ftsmooth.c:416
void lwip_init(void)
Definition: init.c:286
err_t sys_mbox_new(sys_mbox_t *mbox, int size)
Definition: sys_arch.c:133
#define ERR_OK
Definition: err.h:52
static tcpip_init_done_fn tcpip_init_done
Definition: tcpip.c:54
static void * tcpip_init_done_arg
Definition: tcpip.c:55
#define TCPIP_THREAD_STACKSIZE
Definition: opt.h:1250
static sys_mbox_t mbox
Definition: tcpip.c:56

Referenced by LibIPInitialize().

◆ tcpip_input()

err_t tcpip_input ( struct pbuf p,
struct netif inp 
)

Pass a received packet to tcpip_thread for input processing

Parameters
pthe received packet, p->payload pointing to the Ethernet header or to an IP header (if inp doesn't have NETIF_FLAG_ETHARP or NETIF_FLAG_ETHERNET flags)
inpthe network interface on which the packet was received

Definition at line 162 of file tcpip.c.

163 {
164 #if LWIP_TCPIP_CORE_LOCKING_INPUT
165  err_t ret;
166  LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_input: PACKET %p/%p\n", (void *)p, (void *)inp));
167  LOCK_TCPIP_CORE();
168 #if LWIP_ETHERNET
169  if (inp->flags & (NETIF_FLAG_ETHARP | NETIF_FLAG_ETHERNET)) {
170  ret = ethernet_input(p, inp);
171  } else
172 #endif /* LWIP_ETHERNET */
173  {
174  ret = ip_input(p, inp);
175  }
177  return ret;
178 #else /* LWIP_TCPIP_CORE_LOCKING_INPUT */
179  struct tcpip_msg *msg;
180 
181  if (!sys_mbox_valid(&mbox)) {
182  return ERR_VAL;
183  }
184  msg = (struct tcpip_msg *)memp_malloc(MEMP_TCPIP_MSG_INPKT);
185  if (msg == NULL) {
186  return ERR_MEM;
187  }
188 
189  msg->type = TCPIP_MSG_INPKT;
190  msg->msg.inp.p = p;
191  msg->msg.inp.netif = inp;
192  if (sys_mbox_trypost(&mbox, msg) != ERR_OK) {
193  memp_free(MEMP_TCPIP_MSG_INPKT, msg);
194  return ERR_MEM;
195  }
196  return ERR_OK;
197 #endif /* LWIP_TCPIP_CORE_LOCKING_INPUT */
198 }
#define UNLOCK_TCPIP_CORE()
Definition: tcpip.h:68
int sys_mbox_valid(sys_mbox_t *mbox)
Definition: sys_arch.c:146
err_t ip_input(struct pbuf *p, struct netif *inp)
Definition: ip.c:305
void memp_free(memp_t type, void *mem)
Definition: memp.c:435
#define ERR_VAL
Definition: err.h:58
smooth NULL
Definition: ftsmooth.c:416
s8_t err_t
Definition: err.h:47
#define LWIP_DEBUGF(debug, message)
Definition: debug.h:95
#define NETIF_FLAG_ETHARP
Definition: netif.h:88
#define ERR_OK
Definition: err.h:52
#define NETIF_FLAG_ETHERNET
Definition: netif.h:92
#define LOCK_TCPIP_CORE()
Definition: tcpip.h:67
int ret
#define ERR_MEM
Definition: fontsub.h:52
err_t sys_mbox_trypost(sys_mbox_t *mbox, void *msg)
Definition: sys_arch.c:252
struct tcpip_msg::@3947::@3948 inp
#define msg(x)
Definition: auth_time.c:54
void * memp_malloc(memp_t type)
Definition: memp.c:390
GLfloat GLfloat p
Definition: glext.h:8902
static sys_mbox_t mbox
Definition: tcpip.c:56
#define TCPIP_DEBUG
Definition: lwipopts.h:206

Referenced by netif_init(), and TCPRegisterInterface().

◆ tcpip_thread()

static void tcpip_thread ( void arg)
static

The main lwIP thread. This thread has exclusive access to lwIP core functions (unless access to them is not locked). Other threads communicate with this thread using message boxes.

It also starts all the timers to make sure they are running in the right thread context.

Parameters
argunused argument

Definition at line 75 of file tcpip.c.

76 {
77  struct tcpip_msg *msg;
79 
80  if (tcpip_init_done != NULL) {
82  }
83 
85  while (1) { /* MAIN Loop */
88  /* wait for a message, timeouts are processed while waiting */
89  sys_timeouts_mbox_fetch(&mbox, (void **)&msg);
91  switch (msg->type) {
92 #if LWIP_NETCONN
93  case TCPIP_MSG_API:
94  LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: API message %p\n", (void *)msg));
95  msg->msg.apimsg->function(&(msg->msg.apimsg->msg));
96  break;
97 #endif /* LWIP_NETCONN */
98 
99 #if !LWIP_TCPIP_CORE_LOCKING_INPUT
100  case TCPIP_MSG_INPKT:
101  LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: PACKET %p\n", (void *)msg));
102 #if LWIP_ETHERNET
103  if (msg->msg.inp.netif->flags & (NETIF_FLAG_ETHARP | NETIF_FLAG_ETHERNET)) {
104  ethernet_input(msg->msg.inp.p, msg->msg.inp.netif);
105  } else
106 #endif /* LWIP_ETHERNET */
107  {
108  ip_input(msg->msg.inp.p, msg->msg.inp.netif);
109  }
110  memp_free(MEMP_TCPIP_MSG_INPKT, msg);
111  break;
112 #endif /* LWIP_TCPIP_CORE_LOCKING_INPUT */
113 
114 #if LWIP_NETIF_API
115  case TCPIP_MSG_NETIFAPI:
116  LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: Netif API message %p\n", (void *)msg));
117  msg->msg.netifapimsg->function(&(msg->msg.netifapimsg->msg));
118  break;
119 #endif /* LWIP_NETIF_API */
120 
121 #if LWIP_TCPIP_TIMEOUT
122  case TCPIP_MSG_TIMEOUT:
123  LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: TIMEOUT %p\n", (void *)msg));
124  sys_timeout(msg->msg.tmo.msecs, msg->msg.tmo.h, msg->msg.tmo.arg);
125  memp_free(MEMP_TCPIP_MSG_API, msg);
126  break;
127  case TCPIP_MSG_UNTIMEOUT:
128  LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: UNTIMEOUT %p\n", (void *)msg));
129  sys_untimeout(msg->msg.tmo.h, msg->msg.tmo.arg);
130  memp_free(MEMP_TCPIP_MSG_API, msg);
131  break;
132 #endif /* LWIP_TCPIP_TIMEOUT */
133 
134  case TCPIP_MSG_CALLBACK:
135  LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: CALLBACK %p\n", (void *)msg));
136  msg->msg.cb.function(msg->msg.cb.ctx);
137  memp_free(MEMP_TCPIP_MSG_API, msg);
138  break;
139 
141  LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: CALLBACK_STATIC %p\n", (void *)msg));
142  msg->msg.cb.function(msg->msg.cb.ctx);
143  break;
144 
145  default:
146  LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: invalid message: %d\n", msg->type));
147  LWIP_ASSERT("tcpip_thread: invalid message", 0);
148  break;
149  }
150  }
151 }
#define UNLOCK_TCPIP_CORE()
Definition: tcpip.h:68
err_t ip_input(struct pbuf *p, struct netif *inp)
Definition: ip.c:305
#define LWIP_TCPIP_THREAD_ALIVE()
Definition: tcpip.h:54
#define LWIP_ASSERT(message, assertion)
Definition: debug.h:66
void memp_free(memp_t type, void *mem)
Definition: memp.c:435
smooth NULL
Definition: ftsmooth.c:416
#define LWIP_DEBUGF(debug, message)
Definition: debug.h:95
#define NETIF_FLAG_ETHARP
Definition: netif.h:88
void sys_untimeout(sys_timeout_handler handler, void *arg)
void sys_timeout(u32_t msecs, sys_timeout_handler handler, void *arg)
#define NETIF_FLAG_ETHERNET
Definition: netif.h:92
#define LOCK_TCPIP_CORE()
Definition: tcpip.h:67
static tcpip_init_done_fn tcpip_init_done
Definition: tcpip.c:54
static void * tcpip_init_done_arg
Definition: tcpip.c:55
void sys_timeouts_mbox_fetch(sys_mbox_t *mbox, void **msg)
#define msg(x)
Definition: auth_time.c:54
static sys_mbox_t mbox
Definition: tcpip.c:56
#define LWIP_UNUSED_ARG(x)
Definition: arch.h:73
#define TCPIP_DEBUG
Definition: lwipopts.h:206

Referenced by tcpip_init().

◆ tcpip_trycallback()

err_t tcpip_trycallback ( struct tcpip_callback_msg *  msg)

Try to post a callback-message to the tcpip_thread mbox This is intended to be used to send "static" messages from interrupt context.

Parameters
msgpointer to the message to post
Returns
sys_mbox_trypost() return code

Definition at line 439 of file tcpip.c.

440 {
441  if (!sys_mbox_valid(&mbox)) {
442  return ERR_VAL;
443  }
444  return sys_mbox_trypost(&mbox, msg);
445 }
int sys_mbox_valid(sys_mbox_t *mbox)
Definition: sys_arch.c:146
#define ERR_VAL
Definition: err.h:58
err_t sys_mbox_trypost(sys_mbox_t *mbox, void *msg)
Definition: sys_arch.c:252
#define msg(x)
Definition: auth_time.c:54
static sys_mbox_t mbox
Definition: tcpip.c:56

Variable Documentation

◆ mbox

◆ tcpip_init_done

tcpip_init_done_fn tcpip_init_done
static

Definition at line 54 of file tcpip.c.

Referenced by tcpip_init(), and tcpip_thread().

◆ tcpip_init_done_arg

void* tcpip_init_done_arg
static

Definition at line 55 of file tcpip.c.

Referenced by tcpip_init(), and tcpip_thread().