ReactOS  0.4.15-dev-4873-gb671b1c
raw.c
Go to the documentation of this file.
1 
9 /*
10  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
11  * All rights reserved.
12  *
13  * Redistribution and use in source and binary forms, with or without modification,
14  * are permitted provided that the following conditions are met:
15  *
16  * 1. Redistributions of source code must retain the above copyright notice,
17  * this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright notice,
19  * this list of conditions and the following disclaimer in the documentation
20  * and/or other materials provided with the distribution.
21  * 3. The name of the author may not be used to endorse or promote products
22  * derived from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
25  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
27  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
29  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
32  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
33  * OF SUCH DAMAGE.
34  *
35  * This file is part of the lwIP TCP/IP stack.
36  *
37  * Author: Adam Dunkels <adam@sics.se>
38  *
39  */
40 
41 #include "lwip/opt.h"
42 
43 #if LWIP_RAW /* don't build if not configured for use in lwipopts.h */
44 
45 #include "lwip/def.h"
46 #include "lwip/memp.h"
47 #include "lwip/ip_addr.h"
48 #include "lwip/netif.h"
49 #include "lwip/raw.h"
50 #include "lwip/stats.h"
51 #include "arch/perf.h"
52 
53 #include <string.h>
54 
56 static struct raw_pcb *raw_pcbs;
57 
75 u8_t
76 raw_input(struct pbuf *p, struct netif *inp)
77 {
78  struct raw_pcb *pcb, *prev;
79  struct ip_hdr *iphdr;
80  s16_t proto;
81  u8_t eaten = 0;
82 
83  LWIP_UNUSED_ARG(inp);
84 
85  iphdr = (struct ip_hdr *)p->payload;
86  proto = IPH_PROTO(iphdr);
87 
88  prev = NULL;
89  pcb = raw_pcbs;
90  /* loop through all raw pcbs until the packet is eaten by one */
91  /* this allows multiple pcbs to match against the packet by design */
92  while ((eaten == 0) && (pcb != NULL)) {
93  if ((pcb->protocol == proto) &&
94  (ip_addr_isany(&pcb->local_ip) ||
95  ip_addr_cmp(&(pcb->local_ip), &current_iphdr_dest))) {
96 #if IP_SOF_BROADCAST_RECV
97  /* broadcast filter? */
99 #endif /* IP_SOF_BROADCAST_RECV */
100  {
101  /* receive callback function available? */
102  if (pcb->recv != NULL) {
103  /* the receive callback function did not eat the packet? */
104  if (pcb->recv(pcb->recv_arg, pcb, p, ip_current_src_addr()) != 0) {
105  /* receive function ate the packet */
106  p = NULL;
107  eaten = 1;
108  if (prev != NULL) {
109  /* move the pcb to the front of raw_pcbs so that is
110  found faster next time */
111  prev->next = pcb->next;
112  pcb->next = raw_pcbs;
113  raw_pcbs = pcb;
114  }
115  }
116  }
117  /* no receive callback function was set for this raw PCB */
118  }
119  /* drop the packet */
120  }
121  prev = pcb;
122  pcb = pcb->next;
123  }
124  return eaten;
125 }
126 
141 err_t
142 raw_bind(struct raw_pcb *pcb, ip_addr_t *ipaddr)
143 {
144  ip_addr_set(&pcb->local_ip, ipaddr);
145  return ERR_OK;
146 }
147 
161 err_t
162 raw_connect(struct raw_pcb *pcb, ip_addr_t *ipaddr)
163 {
164  ip_addr_set(&pcb->remote_ip, ipaddr);
165  return ERR_OK;
166 }
167 
168 
182 void
183 raw_recv(struct raw_pcb *pcb, raw_recv_fn recv, void *recv_arg)
184 {
185  /* remember recv() callback and user data */
186  pcb->recv = recv;
187  pcb->recv_arg = recv_arg;
188 }
189 
202 err_t
203 raw_sendto(struct raw_pcb *pcb, struct pbuf *p, ip_addr_t *ipaddr)
204 {
205  err_t err;
206  struct netif *netif;
207  ip_addr_t *src_ip;
208  struct pbuf *q; /* q will be sent down the stack */
209 
210  LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_TRACE, ("raw_sendto\n"));
211 
212  /* not enough space to add an IP header to first pbuf in given p chain? */
213  if (pbuf_header(p, IP_HLEN)) {
214  /* allocate header in new pbuf */
215  q = pbuf_alloc(PBUF_IP, 0, PBUF_RAM);
216  /* new header pbuf could not be allocated? */
217  if (q == NULL) {
218  LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("raw_sendto: could not allocate header\n"));
219  return ERR_MEM;
220  }
221  if (p->tot_len != 0) {
222  /* chain header q in front of given pbuf p */
223  pbuf_chain(q, p);
224  }
225  /* { first pbuf q points to header pbuf } */
226  LWIP_DEBUGF(RAW_DEBUG, ("raw_sendto: added header pbuf %p before given pbuf %p\n", (void *)q, (void *)p));
227  } else {
228  /* first pbuf q equals given pbuf */
229  q = p;
230  if(pbuf_header(q, -IP_HLEN)) {
231  LWIP_ASSERT("Can't restore header we just removed!", 0);
232  return ERR_MEM;
233  }
234  }
235 
236  if ((netif = ip_route(ipaddr)) == NULL) {
237  LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_LEVEL_WARNING, ("raw_sendto: No route to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
238  ip4_addr1_16(ipaddr), ip4_addr2_16(ipaddr), ip4_addr3_16(ipaddr), ip4_addr4_16(ipaddr)));
239  /* free any temporary header pbuf allocated by pbuf_header() */
240  if (q != p) {
241  pbuf_free(q);
242  }
243  return ERR_RTE;
244  }
245 
246 #if IP_SOF_BROADCAST
247  /* broadcast filter? */
248  if (!ip_get_option(pcb, SOF_BROADCAST) && ip_addr_isbroadcast(ipaddr, netif)) {
249  LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_LEVEL_WARNING, ("raw_sendto: SOF_BROADCAST not enabled on pcb %p\n", (void *)pcb));
250  /* free any temporary header pbuf allocated by pbuf_header() */
251  if (q != p) {
252  pbuf_free(q);
253  }
254  return ERR_VAL;
255  }
256 #endif /* IP_SOF_BROADCAST */
257 
258  if (ip_addr_isany(&pcb->local_ip)) {
259  /* use outgoing network interface IP address as source address */
260  src_ip = &(netif->ip_addr);
261  } else {
262  /* use RAW PCB local IP address as source address */
263  src_ip = &(pcb->local_ip);
264  }
265 
266  NETIF_SET_HWADDRHINT(netif, &pcb->addr_hint);
267  err = ip_output_if (q, src_ip, ipaddr, pcb->ttl, pcb->tos, pcb->protocol, netif);
269 
270  /* did we chain a header earlier? */
271  if (q != p) {
272  /* free the header */
273  pbuf_free(q);
274  }
275  return err;
276 }
277 
285 err_t
286 raw_send(struct raw_pcb *pcb, struct pbuf *p)
287 {
288  return raw_sendto(pcb, p, &pcb->remote_ip);
289 }
290 
299 void
300 raw_remove(struct raw_pcb *pcb)
301 {
302  struct raw_pcb *pcb2;
303  /* pcb to be removed is first in list? */
304  if (raw_pcbs == pcb) {
305  /* make list start at 2nd pcb */
306  raw_pcbs = raw_pcbs->next;
307  /* pcb not 1st in list */
308  } else {
309  for(pcb2 = raw_pcbs; pcb2 != NULL; pcb2 = pcb2->next) {
310  /* find pcb in raw_pcbs list */
311  if (pcb2->next != NULL && pcb2->next == pcb) {
312  /* remove pcb from list */
313  pcb2->next = pcb->next;
314  }
315  }
316  }
317  memp_free(MEMP_RAW_PCB, pcb);
318 }
319 
330 struct raw_pcb *
331 raw_new(u8_t proto)
332 {
333  struct raw_pcb *pcb;
334 
335  LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_TRACE, ("raw_new\n"));
336 
337  pcb = (struct raw_pcb *)memp_malloc(MEMP_RAW_PCB);
338  /* could allocate RAW PCB? */
339  if (pcb != NULL) {
340  /* initialize PCB to all zeroes */
341  memset(pcb, 0, sizeof(struct raw_pcb));
342  pcb->protocol = proto;
343  pcb->ttl = RAW_TTL;
344  pcb->next = raw_pcbs;
345  raw_pcbs = pcb;
346  }
347  return pcb;
348 }
349 
350 #endif /* LWIP_RAW */
#define ERR_RTE
Definition: err.h:56
#define IPH_PROTO(hdr)
Definition: ip.h:153
#define SOF_BROADCAST
Definition: ip.h:102
u8_t pbuf_header(struct pbuf *p, s16_t header_size_increment)
Definition: pbuf.c:511
#define ip4_addr1_16(ipaddr)
Definition: ip_addr.h:226
typedefPACK_STRUCT_END struct ip_addr ip_addr_t
Definition: ip_addr.h:64
#define RAW_TTL
Definition: opt.h:670
#define LWIP_ASSERT(message, assertion)
Definition: debug.h:66
void memp_free(memp_t type, void *mem)
Definition: memp.c:435
struct pbuf * pbuf_alloc(pbuf_layer layer, u16_t length, pbuf_type type)
Definition: pbuf.c:207
Definition: pbuf.h:58
struct netif * ip_route(ip_addr_t *dest)
Definition: ip.c:124
ip_addr_t current_iphdr_dest
Definition: ip.c:109
#define ip_addr_isbroadcast(ipaddr, netif)
Definition: ip_addr.h:202
#define ip_current_src_addr()
Definition: ip.h:200
#define ERR_VAL
Definition: err.h:58
u8_t pbuf_free(struct pbuf *p)
Definition: pbuf.c:618
s8_t err_t
Definition: err.h:47
#define IP_HLEN
Definition: ip.h:50
#define LWIP_DEBUGF(debug, message)
Definition: debug.h:95
err_t ip_output_if(struct pbuf *p, ip_addr_t *src, ip_addr_t *dest, u8_t ttl, u8_t tos, u8_t proto, struct netif *netif)
Definition: ip.c:641
#define ip4_addr2_16(ipaddr)
Definition: ip_addr.h:227
#define U16_F
Definition: cc.h:36
#define ERR_OK
Definition: err.h:52
void pbuf_chain(struct pbuf *h, struct pbuf *t)
Definition: pbuf.c:786
#define RAW_DEBUG
Definition: opt.h:1975
#define NETIF_SET_HWADDRHINT(netif, hint)
Definition: netif.h:321
Definition: pbuf.h:79
Definition: netif.h:136
GLdouble GLdouble GLdouble GLdouble q
Definition: gl.h:2063
ip_addr_t ip_addr
Definition: netif.h:141
#define LWIP_DBG_LEVEL_WARNING
Definition: debug.h:46
#define ip4_addr3_16(ipaddr)
Definition: ip_addr.h:228
#define err(...)
#define ip4_addr4_16(ipaddr)
Definition: ip_addr.h:229
#define ip_get_option(pcb, opt)
Definition: ip.h:205
#define ip_addr_set(dest, src)
Definition: ip_addr.h:164
#define ip_addr_isany(addr1)
Definition: ip_addr.h:200
Definition: ip.h:116
#define LWIP_DBG_LEVEL_SERIOUS
Definition: debug.h:47
#define ip_addr_cmp(addr1, addr2)
Definition: ip_addr.h:198
#define ERR_MEM
Definition: fontsub.h:52
struct define * next
Definition: compiler.c:65
unsigned char u8_t
Definition: cc.h:23
#define LWIP_DBG_TRACE
Definition: debug.h:57
#define NULL
Definition: types.h:112
Definition: pbuf.h:52
void * memp_malloc(memp_t type)
Definition: memp.c:390
GLfloat GLfloat p
Definition: glext.h:8902
#define memset(x, y, z)
Definition: compat.h:39
#define LWIP_UNUSED_ARG(x)
Definition: arch.h:73
INT WSAAPI recv(IN SOCKET s, OUT CHAR FAR *buf, IN INT len, IN INT flags)
Definition: recv.c:23
signed short s16_t
Definition: cc.h:29