remove const from TPL_OpenTPLFromMemory since the memory is altered
[libogc.git] / libdb / uIP / uip_icmp.c
blob11f67586738660b7c0681ce9c1c5bb7afc47e6d6
1 #include <stdlib.h>
2 #include <string.h>
4 #include "uip_ip.h"
5 #include "uip_pbuf.h"
6 #include "uip_netif.h"
7 #include "uip_icmp.h"
9 #if UIP_LOGGING == 1
10 #include <stdio.h>
11 #define UIP_LOG(m) uip_log(__FILE__,__LINE__,m)
12 #else
13 #define UIP_LOG(m)
14 #endif /* UIP_LOGGING == 1 */
16 #if UIP_STATISTICS == 1
17 struct uip_stats uip_stat;
18 #define UIP_STAT(s) s
19 #else
20 #define UIP_STAT(s)
21 #endif /* UIP_STATISTICS == 1 */
23 void uip_icmpinput(struct uip_pbuf *p,struct uip_netif *inp)
25 u8_t code,type;
26 u16_t hlen;
27 struct uip_ip_addr tmpaddr;
28 struct uip_ip_hdr *iphdr;
29 struct uip_icmp_echo_hdr *iecho;
31 iphdr = p->payload;
32 hlen = UIP_IPH_HL(iphdr)*4;
33 if(uip_pbuf_header(p,-((s16_t)hlen)) || p->tot_len<sizeof(u16_t)*2) {
34 UIP_LOG("uip_icmpinput: short ICMP received.\n");
35 uip_pbuf_free(p);
36 return;
39 type = *((u8_t*)p->payload);
40 code = *((u8_t*)p->payload+1);
41 switch(type) {
42 case UIP_ICMP_ECHO:
43 if(ip_addr_isbroadcast(&iphdr->dst,inp) || ip_addr_ismulticast(&iphdr->dst)) {
44 UIP_LOG("uip_icmpinput: Not echoing to broadcast pings.\n");
45 uip_pbuf_free(p);
46 return;
49 if(p->tot_len<sizeof(struct uip_icmp_echo_hdr)) {
50 UIP_LOG("uip_icmpinput: bad ICMP echo received.\n");
51 uip_pbuf_free(p);
52 return;
55 iecho = p->payload;
56 if(uip_ipchksum_pbuf(p)!=0) {
57 UIP_LOG("uip_icmpinput: checksum failed for received ICMP echo.\n");
58 uip_pbuf_free(p);
59 return;
62 tmpaddr.addr = iphdr->src.addr;
63 iphdr->src.addr = iphdr->dst.addr;
64 iphdr->dst.addr = tmpaddr.addr;
65 UIP_ICMPH_TYPE_SET(iecho,UIP_ICMP_ER);
67 if(iecho->chksum>=htons(0xffff-(UIP_ICMP_ECHO<<8))) iecho->chksum += htons(UIP_ICMP_ECHO<<8)+1;
68 else iecho->chksum += htons(UIP_ICMP_ECHO<<8);
70 uip_pbuf_header(p,hlen);
71 uip_ipoutput_if(p,&iphdr->src,NULL,UIP_IPH_TTL(iphdr),0,UIP_PROTO_ICMP,inp);
72 break;
73 default:
74 UIP_LOG("uip_icmpinput: ICMP type/code not supported.\n");
75 break;
77 uip_pbuf_free(p);
80 void uip_icmp_destunreach(struct uip_pbuf *p,enum uip_icmp_dur_type t)
82 struct uip_pbuf *q;
83 struct uip_ip_hdr *iphdr;
84 struct uip_icmp_dur_hdr *idur;
86 q = uip_pbuf_alloc(UIP_PBUF_IP,sizeof(struct uip_icmp_dur_hdr)+UIP_IP_HLEN+8,UIP_PBUF_RAM);
88 iphdr = p->payload;
89 idur = q->payload;
91 UIP_ICMPH_TYPE_SET(idur,UIP_ICMP_DUR);
92 UIP_ICMPH_CODE_SET(idur,t);
94 UIP_MEMCPY((u8_t*)q->payload+sizeof(struct uip_icmp_dur_hdr),p->payload,UIP_IP_HLEN+8);
96 idur->chksum = 0;
97 idur->chksum = uip_ipchksum(idur,q->len);
99 uip_ipoutput(q,NULL,&iphdr->src,UIP_ICMP_TTL,0,UIP_PROTO_ICMP);
100 uip_pbuf_free(q);