User | Revision | Line number | New contents of line |
mbed714 |
0:d616ece2d859
|
1
|
/**
|
mbed714 |
0:d616ece2d859
|
2
|
* @file
|
mbed714 |
0:d616ece2d859
|
3
|
* Implementation of raw protocol PCBs for low-level handling of
|
mbed714 |
0:d616ece2d859
|
4
|
* different types of protocols besides (or overriding) those
|
mbed714 |
0:d616ece2d859
|
5
|
* already available in lwIP.
|
mbed714 |
0:d616ece2d859
|
6
|
*
|
mbed714 |
0:d616ece2d859
|
7
|
*/
|
mbed714 |
0:d616ece2d859
|
8
|
|
mbed714 |
0:d616ece2d859
|
9
|
/*
|
mbed714 |
0:d616ece2d859
|
10
|
* Copyright (c) 2001-2004 Swedish Institute of Computer Science.
|
mbed714 |
0:d616ece2d859
|
11
|
* All rights reserved.
|
mbed714 |
0:d616ece2d859
|
12
|
*
|
mbed714 |
0:d616ece2d859
|
13
|
* Redistribution and use in source and binary forms, with or without modification,
|
mbed714 |
0:d616ece2d859
|
14
|
* are permitted provided that the following conditions are met:
|
mbed714 |
0:d616ece2d859
|
15
|
*
|
mbed714 |
0:d616ece2d859
|
16
|
* 1. Redistributions of source code must retain the above copyright notice,
|
mbed714 |
0:d616ece2d859
|
17
|
* this list of conditions and the following disclaimer.
|
mbed714 |
0:d616ece2d859
|
18
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
mbed714 |
0:d616ece2d859
|
19
|
* this list of conditions and the following disclaimer in the documentation
|
mbed714 |
0:d616ece2d859
|
20
|
* and/or other materials provided with the distribution.
|
mbed714 |
0:d616ece2d859
|
21
|
* 3. The name of the author may not be used to endorse or promote products
|
mbed714 |
0:d616ece2d859
|
22
|
* derived from this software without specific prior written permission.
|
mbed714 |
0:d616ece2d859
|
23
|
*
|
mbed714 |
0:d616ece2d859
|
24
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
mbed714 |
0:d616ece2d859
|
25
|
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
mbed714 |
0:d616ece2d859
|
26
|
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
|
mbed714 |
0:d616ece2d859
|
27
|
* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
mbed714 |
0:d616ece2d859
|
28
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
|
mbed714 |
0:d616ece2d859
|
29
|
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
mbed714 |
0:d616ece2d859
|
30
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
mbed714 |
0:d616ece2d859
|
31
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
mbed714 |
0:d616ece2d859
|
32
|
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
|
mbed714 |
0:d616ece2d859
|
33
|
* OF SUCH DAMAGE.
|
mbed714 |
0:d616ece2d859
|
34
|
*
|
mbed714 |
0:d616ece2d859
|
35
|
* This file is part of the lwIP TCP/IP stack.
|
mbed714 |
0:d616ece2d859
|
36
|
*
|
mbed714 |
0:d616ece2d859
|
37
|
* Author: Adam Dunkels <adam@sics.se>
|
mbed714 |
0:d616ece2d859
|
38
|
*
|
mbed714 |
0:d616ece2d859
|
39
|
*/
|
mbed714 |
0:d616ece2d859
|
40
|
|
mbed714 |
0:d616ece2d859
|
41
|
#include "lwip/opt.h"
|
mbed714 |
0:d616ece2d859
|
42
|
|
mbed714 |
0:d616ece2d859
|
43
|
#if LWIP_RAW /* don't build if not configured for use in lwipopts.h */
|
mbed714 |
0:d616ece2d859
|
44
|
|
mbed714 |
0:d616ece2d859
|
45
|
#include "lwip/def.h"
|
mbed714 |
0:d616ece2d859
|
46
|
#include "lwip/memp.h"
|
mbed714 |
0:d616ece2d859
|
47
|
#include "lwip/ip_addr.h"
|
mbed714 |
0:d616ece2d859
|
48
|
#include "lwip/netif.h"
|
mbed714 |
0:d616ece2d859
|
49
|
#include "lwip/raw.h"
|
mbed714 |
0:d616ece2d859
|
50
|
#include "lwip/stats.h"
|
mbed714 |
0:d616ece2d859
|
51
|
#include "arch/perf.h"
|
mbed714 |
0:d616ece2d859
|
52
|
|
mbed714 |
0:d616ece2d859
|
53
|
#include <string.h>
|
mbed714 |
0:d616ece2d859
|
54
|
|
mbed714 |
0:d616ece2d859
|
55
|
/** The list of RAW PCBs */
|
mbed714 |
0:d616ece2d859
|
56
|
static struct raw_pcb *raw_pcbs;
|
mbed714 |
0:d616ece2d859
|
57
|
|
mbed714 |
0:d616ece2d859
|
58
|
/**
|
mbed714 |
0:d616ece2d859
|
59
|
* Determine if in incoming IP packet is covered by a RAW PCB
|
mbed714 |
0:d616ece2d859
|
60
|
* and if so, pass it to a user-provided receive callback function.
|
mbed714 |
0:d616ece2d859
|
61
|
*
|
mbed714 |
0:d616ece2d859
|
62
|
* Given an incoming IP datagram (as a chain of pbufs) this function
|
mbed714 |
0:d616ece2d859
|
63
|
* finds a corresponding RAW PCB and calls the corresponding receive
|
mbed714 |
0:d616ece2d859
|
64
|
* callback function.
|
mbed714 |
0:d616ece2d859
|
65
|
*
|
mbed714 |
0:d616ece2d859
|
66
|
* @param p pbuf to be demultiplexed to a RAW PCB.
|
mbed714 |
0:d616ece2d859
|
67
|
* @param inp network interface on which the datagram was received.
|
mbed714 |
0:d616ece2d859
|
68
|
* @return - 1 if the packet has been eaten by a RAW PCB receive
|
mbed714 |
0:d616ece2d859
|
69
|
* callback function. The caller MAY NOT not reference the
|
mbed714 |
0:d616ece2d859
|
70
|
* packet any longer, and MAY NOT call pbuf_free().
|
mbed714 |
0:d616ece2d859
|
71
|
* @return - 0 if packet is not eaten (pbuf is still referenced by the
|
mbed714 |
0:d616ece2d859
|
72
|
* caller).
|
mbed714 |
0:d616ece2d859
|
73
|
*
|
mbed714 |
0:d616ece2d859
|
74
|
*/
|
mbed714 |
0:d616ece2d859
|
75
|
u8_t
|
mbed714 |
0:d616ece2d859
|
76
|
raw_input(struct pbuf *p, struct netif *inp)
|
mbed714 |
0:d616ece2d859
|
77
|
{
|
mbed714 |
0:d616ece2d859
|
78
|
struct raw_pcb *pcb, *prev;
|
mbed714 |
0:d616ece2d859
|
79
|
struct ip_hdr *iphdr;
|
mbed714 |
0:d616ece2d859
|
80
|
s16_t proto;
|
mbed714 |
0:d616ece2d859
|
81
|
u8_t eaten = 0;
|
mbed714 |
0:d616ece2d859
|
82
|
|
mbed714 |
0:d616ece2d859
|
83
|
LWIP_UNUSED_ARG(inp);
|
mbed714 |
0:d616ece2d859
|
84
|
|
mbed714 |
0:d616ece2d859
|
85
|
iphdr = (struct ip_hdr *)p->payload;
|
mbed714 |
0:d616ece2d859
|
86
|
proto = IPH_PROTO(iphdr);
|
mbed714 |
0:d616ece2d859
|
87
|
|
mbed714 |
0:d616ece2d859
|
88
|
prev = NULL;
|
mbed714 |
0:d616ece2d859
|
89
|
pcb = raw_pcbs;
|
mbed714 |
0:d616ece2d859
|
90
|
/* loop through all raw pcbs until the packet is eaten by one */
|
mbed714 |
0:d616ece2d859
|
91
|
/* this allows multiple pcbs to match against the packet by design */
|
mbed714 |
0:d616ece2d859
|
92
|
while ((eaten == 0) && (pcb != NULL)) {
|
mbed714 |
0:d616ece2d859
|
93
|
if ((pcb->protocol == proto) &&
|
mbed714 |
0:d616ece2d859
|
94
|
(ip_addr_isany(&pcb->local_ip) ||
|
mbed714 |
0:d616ece2d859
|
95
|
ip_addr_cmp(&(pcb->local_ip), ¤t_iphdr_dest))) {
|
mbed714 |
0:d616ece2d859
|
96
|
#if IP_SOF_BROADCAST_RECV
|
mbed714 |
0:d616ece2d859
|
97
|
/* broadcast filter? */
|
mbed714 |
0:d616ece2d859
|
98
|
if ((pcb->so_options & SOF_BROADCAST) || !ip_addr_isbroadcast(¤t_iphdr_dest, inp))
|
mbed714 |
0:d616ece2d859
|
99
|
#endif /* IP_SOF_BROADCAST_RECV */
|
mbed714 |
0:d616ece2d859
|
100
|
{
|
mbed714 |
0:d616ece2d859
|
101
|
/* receive callback function available? */
|
mbed714 |
0:d616ece2d859
|
102
|
if (pcb->recv != NULL) {
|
mbed714 |
0:d616ece2d859
|
103
|
/* the receive callback function did not eat the packet? */
|
mbed714 |
0:d616ece2d859
|
104
|
if (pcb->recv(pcb->recv_arg, pcb, p, ip_current_src_addr()) != 0) {
|
mbed714 |
0:d616ece2d859
|
105
|
/* receive function ate the packet */
|
mbed714 |
0:d616ece2d859
|
106
|
p = NULL;
|
mbed714 |
0:d616ece2d859
|
107
|
eaten = 1;
|
mbed714 |
0:d616ece2d859
|
108
|
if (prev != NULL) {
|
mbed714 |
0:d616ece2d859
|
109
|
/* move the pcb to the front of raw_pcbs so that is
|
mbed714 |
0:d616ece2d859
|
110
|
found faster next time */
|
mbed714 |
0:d616ece2d859
|
111
|
prev->next = pcb->next;
|
mbed714 |
0:d616ece2d859
|
112
|
pcb->next = raw_pcbs;
|
mbed714 |
0:d616ece2d859
|
113
|
raw_pcbs = pcb;
|
mbed714 |
0:d616ece2d859
|
114
|
}
|
mbed714 |
0:d616ece2d859
|
115
|
}
|
mbed714 |
0:d616ece2d859
|
116
|
}
|
mbed714 |
0:d616ece2d859
|
117
|
/* no receive callback function was set for this raw PCB */
|
mbed714 |
0:d616ece2d859
|
118
|
}
|
mbed714 |
0:d616ece2d859
|
119
|
/* drop the packet */
|
mbed714 |
0:d616ece2d859
|
120
|
}
|
mbed714 |
0:d616ece2d859
|
121
|
prev = pcb;
|
mbed714 |
0:d616ece2d859
|
122
|
pcb = pcb->next;
|
mbed714 |
0:d616ece2d859
|
123
|
}
|
mbed714 |
0:d616ece2d859
|
124
|
return eaten;
|
mbed714 |
0:d616ece2d859
|
125
|
}
|
mbed714 |
0:d616ece2d859
|
126
|
|
mbed714 |
0:d616ece2d859
|
127
|
/**
|
mbed714 |
0:d616ece2d859
|
128
|
* Bind a RAW PCB.
|
mbed714 |
0:d616ece2d859
|
129
|
*
|
mbed714 |
0:d616ece2d859
|
130
|
* @param pcb RAW PCB to be bound with a local address ipaddr.
|
mbed714 |
0:d616ece2d859
|
131
|
* @param ipaddr local IP address to bind with. Use IP_ADDR_ANY to
|
mbed714 |
0:d616ece2d859
|
132
|
* bind to all local interfaces.
|
mbed714 |
0:d616ece2d859
|
133
|
*
|
mbed714 |
0:d616ece2d859
|
134
|
* @return lwIP error code.
|
mbed714 |
0:d616ece2d859
|
135
|
* - ERR_OK. Successful. No error occured.
|
mbed714 |
0:d616ece2d859
|
136
|
* - ERR_USE. The specified IP address is already bound to by
|
mbed714 |
0:d616ece2d859
|
137
|
* another RAW PCB.
|
mbed714 |
0:d616ece2d859
|
138
|
*
|
mbed714 |
0:d616ece2d859
|
139
|
* @see raw_disconnect()
|
mbed714 |
0:d616ece2d859
|
140
|
*/
|
mbed714 |
0:d616ece2d859
|
141
|
err_t
|
mbed714 |
0:d616ece2d859
|
142
|
raw_bind(struct raw_pcb *pcb, ip_addr_t *ipaddr)
|
mbed714 |
0:d616ece2d859
|
143
|
{
|
mbed714 |
0:d616ece2d859
|
144
|
ip_addr_set(&pcb->local_ip, ipaddr);
|
mbed714 |
0:d616ece2d859
|
145
|
return ERR_OK;
|
mbed714 |
0:d616ece2d859
|
146
|
}
|
mbed714 |
0:d616ece2d859
|
147
|
|
mbed714 |
0:d616ece2d859
|
148
|
/**
|
mbed714 |
0:d616ece2d859
|
149
|
* Connect an RAW PCB. This function is required by upper layers
|
mbed714 |
0:d616ece2d859
|
150
|
* of lwip. Using the raw api you could use raw_sendto() instead
|
mbed714 |
0:d616ece2d859
|
151
|
*
|
mbed714 |
0:d616ece2d859
|
152
|
* This will associate the RAW PCB with the remote address.
|
mbed714 |
0:d616ece2d859
|
153
|
*
|
mbed714 |
0:d616ece2d859
|
154
|
* @param pcb RAW PCB to be connected with remote address ipaddr and port.
|
mbed714 |
0:d616ece2d859
|
155
|
* @param ipaddr remote IP address to connect with.
|
mbed714 |
0:d616ece2d859
|
156
|
*
|
mbed714 |
0:d616ece2d859
|
157
|
* @return lwIP error code
|
mbed714 |
0:d616ece2d859
|
158
|
*
|
mbed714 |
0:d616ece2d859
|
159
|
* @see raw_disconnect() and raw_sendto()
|
mbed714 |
0:d616ece2d859
|
160
|
*/
|
mbed714 |
0:d616ece2d859
|
161
|
err_t
|
mbed714 |
0:d616ece2d859
|
162
|
raw_connect(struct raw_pcb *pcb, ip_addr_t *ipaddr)
|
mbed714 |
0:d616ece2d859
|
163
|
{
|
mbed714 |
0:d616ece2d859
|
164
|
ip_addr_set(&pcb->remote_ip, ipaddr);
|
mbed714 |
0:d616ece2d859
|
165
|
return ERR_OK;
|
mbed714 |
0:d616ece2d859
|
166
|
}
|
mbed714 |
0:d616ece2d859
|
167
|
|
mbed714 |
0:d616ece2d859
|
168
|
|
mbed714 |
0:d616ece2d859
|
169
|
/**
|
mbed714 |
0:d616ece2d859
|
170
|
* Set the callback function for received packets that match the
|
mbed714 |
0:d616ece2d859
|
171
|
* raw PCB's protocol and binding.
|
mbed714 |
0:d616ece2d859
|
172
|
*
|
mbed714 |
0:d616ece2d859
|
173
|
* The callback function MUST either
|
mbed714 |
0:d616ece2d859
|
174
|
* - eat the packet by calling pbuf_free() and returning non-zero. The
|
mbed714 |
0:d616ece2d859
|
175
|
* packet will not be passed to other raw PCBs or other protocol layers.
|
mbed714 |
0:d616ece2d859
|
176
|
* - not free the packet, and return zero. The packet will be matched
|
mbed714 |
0:d616ece2d859
|
177
|
* against further PCBs and/or forwarded to another protocol layers.
|
mbed714 |
0:d616ece2d859
|
178
|
*
|
mbed714 |
0:d616ece2d859
|
179
|
* @return non-zero if the packet was free()d, zero if the packet remains
|
mbed714 |
0:d616ece2d859
|
180
|
* available for others.
|
mbed714 |
0:d616ece2d859
|
181
|
*/
|
mbed714 |
0:d616ece2d859
|
182
|
void
|
mbed714 |
0:d616ece2d859
|
183
|
raw_recv(struct raw_pcb *pcb, raw_recv_fn recv, void *recv_arg)
|
mbed714 |
0:d616ece2d859
|
184
|
{
|
mbed714 |
0:d616ece2d859
|
185
|
/* remember recv() callback and user data */
|
mbed714 |
0:d616ece2d859
|
186
|
pcb->recv = recv;
|
mbed714 |
0:d616ece2d859
|
187
|
pcb->recv_arg = recv_arg;
|
mbed714 |
0:d616ece2d859
|
188
|
}
|
mbed714 |
0:d616ece2d859
|
189
|
|
mbed714 |
0:d616ece2d859
|
190
|
/**
|
mbed714 |
0:d616ece2d859
|
191
|
* Send the raw IP packet to the given address. Note that actually you cannot
|
mbed714 |
0:d616ece2d859
|
192
|
* modify the IP headers (this is inconsistent with the receive callback where
|
mbed714 |
0:d616ece2d859
|
193
|
* you actually get the IP headers), you can only specify the IP payload here.
|
mbed714 |
0:d616ece2d859
|
194
|
* It requires some more changes in lwIP. (there will be a raw_send() function
|
mbed714 |
0:d616ece2d859
|
195
|
* then.)
|
mbed714 |
0:d616ece2d859
|
196
|
*
|
mbed714 |
0:d616ece2d859
|
197
|
* @param pcb the raw pcb which to send
|
mbed714 |
0:d616ece2d859
|
198
|
* @param p the IP payload to send
|
mbed714 |
0:d616ece2d859
|
199
|
* @param ipaddr the destination address of the IP packet
|
mbed714 |
0:d616ece2d859
|
200
|
*
|
mbed714 |
0:d616ece2d859
|
201
|
*/
|
mbed714 |
0:d616ece2d859
|
202
|
err_t
|
mbed714 |
0:d616ece2d859
|
203
|
raw_sendto(struct raw_pcb *pcb, struct pbuf *p, ip_addr_t *ipaddr)
|
mbed714 |
0:d616ece2d859
|
204
|
{
|
mbed714 |
0:d616ece2d859
|
205
|
err_t err;
|
mbed714 |
0:d616ece2d859
|
206
|
struct netif *netif;
|
mbed714 |
0:d616ece2d859
|
207
|
ip_addr_t *src_ip;
|
mbed714 |
0:d616ece2d859
|
208
|
struct pbuf *q; /* q will be sent down the stack */
|
mbed714 |
0:d616ece2d859
|
209
|
|
mbed714 |
0:d616ece2d859
|
210
|
LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_TRACE, ("raw_sendto\n"));
|
mbed714 |
0:d616ece2d859
|
211
|
|
mbed714 |
0:d616ece2d859
|
212
|
/* not enough space to add an IP header to first pbuf in given p chain? */
|
mbed714 |
0:d616ece2d859
|
213
|
if (pbuf_header(p, IP_HLEN)) {
|
mbed714 |
0:d616ece2d859
|
214
|
/* allocate header in new pbuf */
|
mbed714 |
0:d616ece2d859
|
215
|
q = pbuf_alloc(PBUF_IP, 0, PBUF_RAM);
|
mbed714 |
0:d616ece2d859
|
216
|
/* new header pbuf could not be allocated? */
|
mbed714 |
0:d616ece2d859
|
217
|
if (q == NULL) {
|
mbed714 |
0:d616ece2d859
|
218
|
LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("raw_sendto: could not allocate header\n"));
|
mbed714 |
0:d616ece2d859
|
219
|
return ERR_MEM;
|
mbed714 |
0:d616ece2d859
|
220
|
}
|
mbed714 |
0:d616ece2d859
|
221
|
/* chain header q in front of given pbuf p */
|
mbed714 |
0:d616ece2d859
|
222
|
pbuf_chain(q, p);
|
mbed714 |
0:d616ece2d859
|
223
|
/* { first pbuf q points to header pbuf } */
|
mbed714 |
0:d616ece2d859
|
224
|
LWIP_DEBUGF(RAW_DEBUG, ("raw_sendto: added header pbuf %p before given pbuf %p\n", (void *)q, (void *)p));
|
mbed714 |
0:d616ece2d859
|
225
|
} else {
|
mbed714 |
0:d616ece2d859
|
226
|
/* first pbuf q equals given pbuf */
|
mbed714 |
0:d616ece2d859
|
227
|
q = p;
|
mbed714 |
0:d616ece2d859
|
228
|
if(pbuf_header(q, -IP_HLEN)) {
|
mbed714 |
0:d616ece2d859
|
229
|
LWIP_ASSERT("Can't restore header we just removed!", 0);
|
mbed714 |
0:d616ece2d859
|
230
|
return ERR_MEM;
|
mbed714 |
0:d616ece2d859
|
231
|
}
|
mbed714 |
0:d616ece2d859
|
232
|
}
|
mbed714 |
0:d616ece2d859
|
233
|
|
mbed714 |
0:d616ece2d859
|
234
|
if ((netif = ip_route(ipaddr)) == NULL) {
|
mbed714 |
0:d616ece2d859
|
235
|
LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_LEVEL_WARNING, ("raw_sendto: No route to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
|
mbed714 |
0:d616ece2d859
|
236
|
ip4_addr1_16(ipaddr), ip4_addr2_16(ipaddr), ip4_addr3_16(ipaddr), ip4_addr4_16(ipaddr)));
|
mbed714 |
0:d616ece2d859
|
237
|
/* free any temporary header pbuf allocated by pbuf_header() */
|
mbed714 |
0:d616ece2d859
|
238
|
if (q != p) {
|
mbed714 |
0:d616ece2d859
|
239
|
pbuf_free(q);
|
mbed714 |
0:d616ece2d859
|
240
|
}
|
mbed714 |
0:d616ece2d859
|
241
|
return ERR_RTE;
|
mbed714 |
0:d616ece2d859
|
242
|
}
|
mbed714 |
0:d616ece2d859
|
243
|
|
mbed714 |
0:d616ece2d859
|
244
|
#if IP_SOF_BROADCAST
|
mbed714 |
0:d616ece2d859
|
245
|
/* broadcast filter? */
|
mbed714 |
0:d616ece2d859
|
246
|
if (((pcb->so_options & SOF_BROADCAST) == 0) && ip_addr_isbroadcast(ipaddr, netif)) {
|
mbed714 |
0:d616ece2d859
|
247
|
LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_LEVEL_WARNING, ("raw_sendto: SOF_BROADCAST not enabled on pcb %p\n", (void *)pcb));
|
mbed714 |
0:d616ece2d859
|
248
|
/* free any temporary header pbuf allocated by pbuf_header() */
|
mbed714 |
0:d616ece2d859
|
249
|
if (q != p) {
|
mbed714 |
0:d616ece2d859
|
250
|
pbuf_free(q);
|
mbed714 |
0:d616ece2d859
|
251
|
}
|
mbed714 |
0:d616ece2d859
|
252
|
return ERR_VAL;
|
mbed714 |
0:d616ece2d859
|
253
|
}
|
mbed714 |
0:d616ece2d859
|
254
|
#endif /* IP_SOF_BROADCAST */
|
mbed714 |
0:d616ece2d859
|
255
|
|
mbed714 |
0:d616ece2d859
|
256
|
if (ip_addr_isany(&pcb->local_ip)) {
|
mbed714 |
0:d616ece2d859
|
257
|
/* use outgoing network interface IP address as source address */
|
mbed714 |
0:d616ece2d859
|
258
|
src_ip = &(netif->ip_addr);
|
mbed714 |
0:d616ece2d859
|
259
|
} else {
|
mbed714 |
0:d616ece2d859
|
260
|
/* use RAW PCB local IP address as source address */
|
mbed714 |
0:d616ece2d859
|
261
|
src_ip = &(pcb->local_ip);
|
mbed714 |
0:d616ece2d859
|
262
|
}
|
mbed714 |
0:d616ece2d859
|
263
|
|
mbed714 |
0:d616ece2d859
|
264
|
#if LWIP_NETIF_HWADDRHINT
|
mbed714 |
0:d616ece2d859
|
265
|
netif->addr_hint = &(pcb->addr_hint);
|
mbed714 |
0:d616ece2d859
|
266
|
#endif /* LWIP_NETIF_HWADDRHINT*/
|
mbed714 |
0:d616ece2d859
|
267
|
err = ip_output_if (q, src_ip, ipaddr, pcb->ttl, pcb->tos, pcb->protocol, netif);
|
mbed714 |
0:d616ece2d859
|
268
|
#if LWIP_NETIF_HWADDRHINT
|
mbed714 |
0:d616ece2d859
|
269
|
netif->addr_hint = NULL;
|
mbed714 |
0:d616ece2d859
|
270
|
#endif /* LWIP_NETIF_HWADDRHINT*/
|
mbed714 |
0:d616ece2d859
|
271
|
|
mbed714 |
0:d616ece2d859
|
272
|
/* did we chain a header earlier? */
|
mbed714 |
0:d616ece2d859
|
273
|
if (q != p) {
|
mbed714 |
0:d616ece2d859
|
274
|
/* free the header */
|
mbed714 |
0:d616ece2d859
|
275
|
pbuf_free(q);
|
mbed714 |
0:d616ece2d859
|
276
|
}
|
mbed714 |
0:d616ece2d859
|
277
|
return err;
|
mbed714 |
0:d616ece2d859
|
278
|
}
|
mbed714 |
0:d616ece2d859
|
279
|
|
mbed714 |
0:d616ece2d859
|
280
|
/**
|
mbed714 |
0:d616ece2d859
|
281
|
* Send the raw IP packet to the address given by raw_connect()
|
mbed714 |
0:d616ece2d859
|
282
|
*
|
mbed714 |
0:d616ece2d859
|
283
|
* @param pcb the raw pcb which to send
|
mbed714 |
0:d616ece2d859
|
284
|
* @param p the IP payload to send
|
mbed714 |
0:d616ece2d859
|
285
|
*
|
mbed714 |
0:d616ece2d859
|
286
|
*/
|
mbed714 |
0:d616ece2d859
|
287
|
err_t
|
mbed714 |
0:d616ece2d859
|
288
|
raw_send(struct raw_pcb *pcb, struct pbuf *p)
|
mbed714 |
0:d616ece2d859
|
289
|
{
|
mbed714 |
0:d616ece2d859
|
290
|
return raw_sendto(pcb, p, &pcb->remote_ip);
|
mbed714 |
0:d616ece2d859
|
291
|
}
|
mbed714 |
0:d616ece2d859
|
292
|
|
mbed714 |
0:d616ece2d859
|
293
|
/**
|
mbed714 |
0:d616ece2d859
|
294
|
* Remove an RAW PCB.
|
mbed714 |
0:d616ece2d859
|
295
|
*
|
mbed714 |
0:d616ece2d859
|
296
|
* @param pcb RAW PCB to be removed. The PCB is removed from the list of
|
mbed714 |
0:d616ece2d859
|
297
|
* RAW PCB's and the data structure is freed from memory.
|
mbed714 |
0:d616ece2d859
|
298
|
*
|
mbed714 |
0:d616ece2d859
|
299
|
* @see raw_new()
|
mbed714 |
0:d616ece2d859
|
300
|
*/
|
mbed714 |
0:d616ece2d859
|
301
|
void
|
mbed714 |
0:d616ece2d859
|
302
|
raw_remove(struct raw_pcb *pcb)
|
mbed714 |
0:d616ece2d859
|
303
|
{
|
mbed714 |
0:d616ece2d859
|
304
|
struct raw_pcb *pcb2;
|
mbed714 |
0:d616ece2d859
|
305
|
/* pcb to be removed is first in list? */
|
mbed714 |
0:d616ece2d859
|
306
|
if (raw_pcbs == pcb) {
|
mbed714 |
0:d616ece2d859
|
307
|
/* make list start at 2nd pcb */
|
mbed714 |
0:d616ece2d859
|
308
|
raw_pcbs = raw_pcbs->next;
|
mbed714 |
0:d616ece2d859
|
309
|
/* pcb not 1st in list */
|
mbed714 |
0:d616ece2d859
|
310
|
} else {
|
mbed714 |
0:d616ece2d859
|
311
|
for(pcb2 = raw_pcbs; pcb2 != NULL; pcb2 = pcb2->next) {
|
mbed714 |
0:d616ece2d859
|
312
|
/* find pcb in raw_pcbs list */
|
mbed714 |
0:d616ece2d859
|
313
|
if (pcb2->next != NULL && pcb2->next == pcb) {
|
mbed714 |
0:d616ece2d859
|
314
|
/* remove pcb from list */
|
mbed714 |
0:d616ece2d859
|
315
|
pcb2->next = pcb->next;
|
mbed714 |
0:d616ece2d859
|
316
|
}
|
mbed714 |
0:d616ece2d859
|
317
|
}
|
mbed714 |
0:d616ece2d859
|
318
|
}
|
mbed714 |
0:d616ece2d859
|
319
|
memp_free(MEMP_RAW_PCB, pcb);
|
mbed714 |
0:d616ece2d859
|
320
|
}
|
mbed714 |
0:d616ece2d859
|
321
|
|
mbed714 |
0:d616ece2d859
|
322
|
/**
|
mbed714 |
0:d616ece2d859
|
323
|
* Create a RAW PCB.
|
mbed714 |
0:d616ece2d859
|
324
|
*
|
mbed714 |
0:d616ece2d859
|
325
|
* @return The RAW PCB which was created. NULL if the PCB data structure
|
mbed714 |
0:d616ece2d859
|
326
|
* could not be allocated.
|
mbed714 |
0:d616ece2d859
|
327
|
*
|
mbed714 |
0:d616ece2d859
|
328
|
* @param proto the protocol number of the IPs payload (e.g. IP_PROTO_ICMP)
|
mbed714 |
0:d616ece2d859
|
329
|
*
|
mbed714 |
0:d616ece2d859
|
330
|
* @see raw_remove()
|
mbed714 |
0:d616ece2d859
|
331
|
*/
|
mbed714 |
0:d616ece2d859
|
332
|
struct raw_pcb *
|
mbed714 |
0:d616ece2d859
|
333
|
raw_new(u8_t proto)
|
mbed714 |
0:d616ece2d859
|
334
|
{
|
mbed714 |
0:d616ece2d859
|
335
|
struct raw_pcb *pcb;
|
mbed714 |
0:d616ece2d859
|
336
|
|
mbed714 |
0:d616ece2d859
|
337
|
LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_TRACE, ("raw_new\n"));
|
mbed714 |
0:d616ece2d859
|
338
|
|
mbed714 |
0:d616ece2d859
|
339
|
pcb = (struct raw_pcb *)memp_malloc(MEMP_RAW_PCB);
|
mbed714 |
0:d616ece2d859
|
340
|
/* could allocate RAW PCB? */
|
mbed714 |
0:d616ece2d859
|
341
|
if (pcb != NULL) {
|
mbed714 |
0:d616ece2d859
|
342
|
/* initialize PCB to all zeroes */
|
mbed714 |
0:d616ece2d859
|
343
|
memset(pcb, 0, sizeof(struct raw_pcb));
|
mbed714 |
0:d616ece2d859
|
344
|
pcb->protocol = proto;
|
mbed714 |
0:d616ece2d859
|
345
|
pcb->ttl = RAW_TTL;
|
mbed714 |
0:d616ece2d859
|
346
|
pcb->next = raw_pcbs;
|
mbed714 |
0:d616ece2d859
|
347
|
raw_pcbs = pcb;
|
mbed714 |
0:d616ece2d859
|
348
|
}
|
mbed714 |
0:d616ece2d859
|
349
|
return pcb;
|
mbed714 |
0:d616ece2d859
|
350
|
}
|
mbed714 |
0:d616ece2d859
|
351
|
|
mbed714 |
0:d616ece2d859
|
352
|
#endif /* LWIP_RAW */
|