72 |
* Determine if in incoming IP packet is covered by a RAW pcb and |
* Determine if in incoming IP packet is covered by a RAW pcb and |
73 |
* and process it if possible |
* and process it if possible |
74 |
* |
* |
75 |
* Given an incoming UDP datagram (as a chain of pbufs) this function |
* Given an incoming IP datagram (as a chain of pbufs) this function |
76 |
* finds a corresponding UDP PCB and |
* finds a corresponding RAW PCB and |
77 |
* |
* |
78 |
* @param pbuf pbuf to be demultiplexed to a UDP PCB. |
* @param pbuf pbuf to be demultiplexed to a RAW PCB. |
79 |
* @param netif network interface on which the datagram was received. |
* @param netif network interface on which the datagram was received. |
80 |
* @return 0 if packet cannot be handled (pbuf needs to be freed then) |
* @return 0 if packet is not eated (pbuf needs to be freed then) |
81 |
* or 1 if the packet has been processed |
* or 1 if the packet has been eaten (pbuf needs not to be freed |
82 |
|
* then) |
83 |
* |
* |
84 |
*/ |
*/ |
85 |
int |
int |
96 |
for(pcb = raw_pcbs; pcb != NULL; pcb = pcb->next) { |
for(pcb = raw_pcbs; pcb != NULL; pcb = pcb->next) { |
97 |
if (pcb->protocol == proto) { |
if (pcb->protocol == proto) { |
98 |
if (pcb->recv) { |
if (pcb->recv) { |
99 |
pcb->recv(pcb->recv_arg, pcb, p, &(iphdr->src)); |
if (!pcb->recv(pcb->recv_arg, pcb, p, &(iphdr->src))) |
100 |
|
return 0; |
101 |
} |
} |
102 |
pbuf_free(p); |
pbuf_free(p); |
103 |
rc = 1; |
rc = 1; |
128 |
return ERR_OK; |
return ERR_OK; |
129 |
} |
} |
130 |
|
|
131 |
/*-----------------------------------------------------------------------------------*/ |
/** |
132 |
|
* Connect an RAW PCB. This function is required by upper layers |
133 |
|
* of lwip. Using the raw api you could use raw_send_to() instead |
134 |
|
* |
135 |
|
* This will associate the RAW PCB with the remote address. |
136 |
|
* |
137 |
|
* @param pcb RAW PCB to be connected with remote address ipaddr and port. |
138 |
|
* @param ipaddr remote IP address to connect with. |
139 |
|
* |
140 |
|
* @return lwIP error code |
141 |
|
* |
142 |
|
* @see raw_disconnect() and raw_send_to() |
143 |
|
*/ |
144 |
|
err_t |
145 |
|
raw_connect(struct raw_pcb *pcb, struct ip_addr *ipaddr) |
146 |
|
{ |
147 |
|
ip_addr_set(&pcb->remote_ip, ipaddr); |
148 |
|
return ERR_OK; |
149 |
|
} |
150 |
|
|
151 |
|
|
152 |
|
/** |
153 |
|
* Set the callback function if a RAW packet with the pcb's protocol |
154 |
|
* is received. If the callback function returns a value unequal 0 |
155 |
|
* the raw packet is "eaten" and not forwarded to any other raw pcb |
156 |
|
* including lwip itself |
157 |
|
*/ |
158 |
void |
void |
159 |
raw_recv(struct raw_pcb *pcb, |
raw_recv(struct raw_pcb *pcb, |
160 |
void (* recv)(void *arg, struct raw_pcb *upcb, struct pbuf *p, |
int (* recv)(void *arg, struct raw_pcb *upcb, struct pbuf *p, |
161 |
struct ip_addr *addr), |
struct ip_addr *addr), |
162 |
void *recv_arg) |
void *recv_arg) |
163 |
{ |
{ |
164 |
/* remember recv() callback and user data */ |
/* remember recv() callback and user data */ |
165 |
pcb->recv = recv; |
pcb->recv = recv; |
179 |
* |
* |
180 |
*/ |
*/ |
181 |
err_t |
err_t |
182 |
raw_send_payload(struct raw_pcb *pcb, struct pbuf *p, struct ip_addr *ipaddr) |
raw_send_to(struct raw_pcb *pcb, struct pbuf *p, struct ip_addr *ipaddr) |
183 |
{ |
{ |
184 |
err_t err; |
err_t err; |
185 |
struct netif *netif; |
struct netif *netif; |
186 |
struct ip_addr *src_ip; |
struct ip_addr *src_ip; |
187 |
|
struct pbuf *q; /* q will be sent down the stack */ |
188 |
LWIP_DEBUGF(UDP_DEBUG | DBG_TRACE | 3, ("raw_send_payload\n")); |
|
189 |
|
LWIP_DEBUGF(RAW_DEBUG | DBG_TRACE | 3, ("raw_send_to\n")); |
190 |
|
|
191 |
|
/* not enough space to add an IP header to first pbuf in given p chain? */ |
192 |
|
if (pbuf_header(p, IP_HLEN)) { |
193 |
|
/* allocate header in new pbuf */ |
194 |
|
q = pbuf_alloc(PBUF_IP, 0, PBUF_RAM); |
195 |
|
/* new header pbuf could not be allocated? */ |
196 |
|
if (q == NULL) { |
197 |
|
LWIP_DEBUGF(RAW_DEBUG | DBG_TRACE | 2, ("raw_send_to: could not allocate header\n")); |
198 |
|
return ERR_MEM; |
199 |
|
} |
200 |
|
/* chain header q in front of given pbuf p */ |
201 |
|
pbuf_chain(q, p); |
202 |
|
/* { first pbuf q points to header pbuf } */ |
203 |
|
LWIP_DEBUGF(RAW_DEBUG, ("raw_send_to: added header pbuf %p before given pbuf %p\n", (void *)q, (void *)p)); |
204 |
|
} else { |
205 |
|
/* first pbuf q equals given pbuf */ |
206 |
|
q = p; |
207 |
|
pbuf_header(q, -IP_HLEN); |
208 |
|
} |
209 |
|
|
210 |
if ((netif = ip_route(ipaddr)) == NULL) { |
if ((netif = ip_route(ipaddr)) == NULL) { |
211 |
LWIP_DEBUGF(UDP_DEBUG | 1, ("raw_send_payload: No route to 0x%lx\n", ipaddr->addr)); |
LWIP_DEBUGF(RAW_DEBUG | 1, ("raw_send_to: No route to 0x%lx\n", ipaddr->addr)); |
212 |
#ifdef RAW_STATS |
#ifdef RAW_STATS |
213 |
/* ++lwip_stats.raw.rterr;*/ |
/* ++lwip_stats.raw.rterr;*/ |
214 |
#endif /* UDP_STATS */ |
#endif /* RAW_STATS */ |
215 |
|
if (q != p) { |
216 |
|
pbuf_free(q); |
217 |
|
} |
218 |
return ERR_RTE; |
return ERR_RTE; |
219 |
} |
} |
220 |
|
|
222 |
/* use outgoing network interface IP address as source address */ |
/* use outgoing network interface IP address as source address */ |
223 |
src_ip = &(netif->ip_addr); |
src_ip = &(netif->ip_addr); |
224 |
} else { |
} else { |
225 |
/* use UDP PCB local IP address as source address */ |
/* use RAW PCB local IP address as source address */ |
226 |
src_ip = &(pcb->local_ip); |
src_ip = &(pcb->local_ip); |
227 |
} |
} |
228 |
|
|
229 |
err = ip_output_if (p, src_ip, ipaddr, 64, pcb->tos, pcb->protocol, netif); |
err = ip_output_if (q, src_ip, ipaddr, pcb->ttl, pcb->tos, pcb->protocol, netif); |
230 |
|
|
231 |
return ERR_OK; |
/* did we chain a header earlier? */ |
232 |
|
if (q != p) { |
233 |
|
/* free the header */ |
234 |
|
pbuf_free(q); |
235 |
|
} |
236 |
|
return err; |
237 |
|
} |
238 |
|
|
239 |
|
/** |
240 |
|
* Send the raw IP packet to the address given by raw_connect() |
241 |
|
* |
242 |
|
* @param pcb the raw pcb which to send |
243 |
|
* @param p the ip payload to send |
244 |
|
* @param ipaddr the destination address of the whole IP packet |
245 |
|
* |
246 |
|
*/ |
247 |
|
err_t |
248 |
|
raw_send(struct raw_pcb *pcb, struct pbuf *p) |
249 |
|
{ |
250 |
|
return raw_send_to(pcb,p,&pcb->remote_ip); |
251 |
} |
} |
252 |
|
|
253 |
/** |
/** |
268 |
raw_pcbs = raw_pcbs->next; |
raw_pcbs = raw_pcbs->next; |
269 |
/* pcb not 1st in list */ |
/* pcb not 1st in list */ |
270 |
} else for(pcb2 = raw_pcbs; pcb2 != NULL; pcb2 = pcb2->next) { |
} else for(pcb2 = raw_pcbs; pcb2 != NULL; pcb2 = pcb2->next) { |
271 |
/* find pcb in udp_pcbs list */ |
/* find pcb in raw_pcbs list */ |
272 |
if (pcb2->next != NULL && pcb2->next == pcb) { |
if (pcb2->next != NULL && pcb2->next == pcb) { |
273 |
/* remove pcb from list */ |
/* remove pcb from list */ |
274 |
pcb2->next = pcb->next; |
pcb2->next = pcb->next; |
299 |
/* initialize PCB to all zeroes */ |
/* initialize PCB to all zeroes */ |
300 |
memset(pcb, 0, sizeof(struct raw_pcb)); |
memset(pcb, 0, sizeof(struct raw_pcb)); |
301 |
pcb->protocol = proto; |
pcb->protocol = proto; |
302 |
|
pcb->ttl = RAW_TTL; |
303 |
pcb->next = raw_pcbs; |
pcb->next = raw_pcbs; |
304 |
raw_pcbs = pcb; |
raw_pcbs = pcb; |
305 |
} |
} |