⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ip.c.svn-base

📁 realtek的8186芯片ADSL路由AP源代码
💻 SVN-BASE
📖 第 1 页 / 共 2 页
字号:
 * an IP packet is received. The function does the basic checks of the * IP header such as packet size being at least larger than the header * size etc. If the packet was not destined for us, the packet is * forwarded (using ip_forward). The IP checksum is always checked. * * Finally, the packet is sent to the upper layer protocol input function. *//*-----------------------------------------------------------------------------------*/// does not use this functions as the input function#if 0err_tip_input(struct pbuf *p, struct netif *inp) {  static struct ip_hdr *iphdr;  static struct netif *netif;  static u8_t hl;    #ifdef IP_STATS  ++stats.ip.recv;#endif /* IP_STATS */  /* identify the IP header */  iphdr = p->payload;  if(IPH_V(iphdr) != 4) {    DEBUGF(IP_DEBUG, ("IP packet dropped due to bad version number %d\n", IPH_V(iphdr)));#if IP_DEBUG    ip_debug_print(p);#endif /* IP_DEBUG */    pbuf_free(p);#ifdef IP_STATS    ++stats.ip.err;    ++stats.ip.drop;#endif /* IP_STATS */    return ERR_OK;  }    hl = IPH_HL(iphdr);    if(hl * 4 > p->len) {    DEBUGF(IP_DEBUG, ("IP packet dropped due to too short packet %d\n", p->len));    pbuf_free(p);#ifdef IP_STATS    ++stats.ip.lenerr;    ++stats.ip.drop;#endif /* IP_STATS */    return ERR_OK;  }  /* verify checksum */  if(inet_chksum(iphdr, hl * 4) != 0) {    DEBUGF(IP_DEBUG, ("IP packet dropped due to failing checksum 0x%x\n", inet_chksum(iphdr, hl * 4)));#if IP_DEBUG    ip_debug_print(p);#endif /* IP_DEBUG */    pbuf_free(p);#ifdef IP_STATS    ++stats.ip.chkerr;    ++stats.ip.drop;#endif /* IP_STATS */    return ERR_OK;  }    /* Trim pbuf. This should have been done at the netif layer,     but we'll do it anyway just to be sure that its done. */  pbuf_realloc(p, ntohs(IPH_LEN(iphdr)));  /* is this packet for us? */  for(netif = netif_list; netif != NULL; netif = netif->next) {    DEBUGF(IP_DEBUG, ("ip_input: iphdr->dest 0x%lx netif->ip_addr 0x%lx (0x%lx, 0x%lx, 0x%lx)\n",		      iphdr->dest.addr, netif->ip_addr.addr,		      iphdr->dest.addr & netif->netmask.addr,		      netif->ip_addr.addr & netif->netmask.addr,		      iphdr->dest.addr & ~(netif->netmask.addr)));    if(ip_addr_isany(&(netif->ip_addr)) ||       ip_addr_cmp(&(iphdr->dest), &(netif->ip_addr)) ||       (ip_addr_isbroadcast(&(iphdr->dest), &(netif->netmask)) &&	ip_addr_maskcmp(&(iphdr->dest), &(netif->ip_addr), &(netif->netmask))) ||       ip_addr_cmp(&(iphdr->dest), IP_ADDR_BROADCAST)) {      break;    }  }#if LWIP_DHCP  /* If a DHCP packet has arrived on the interface, we pass it up the     stack regardless of destination IP address. The reason is that     DHCP replies are sent to the IP adress that will be given to this     node (as recommended by RFC 1542 section 3.1.1, referred by RFC     2131). */  if(IPH_PROTO(iphdr) == IP_PROTO_UDP &&     ((struct udp_hdr *)((u8_t *)iphdr + IPH_HL(iphdr) * 4/sizeof(u8_t)))->src ==     DHCP_SERVER_PORT) {    netif = inp;  }  #endif /* LWIP_DHCP */	    if(netif == NULL) {    /* packet not for us, route or discard */    DEBUGF(IP_DEBUG, ("ip_input: packet not for us.\n"));#if IP_FORWARD    if(!ip_addr_isbroadcast(&(iphdr->dest), &(inp->netmask))) {      ip_forward(p, iphdr, inp);    }#endif /* IP_FORWARD */    pbuf_free(p);    return ERR_OK;  }#if IP_REASSEMBLY  if((IPH_OFFSET(iphdr) & htons(IP_OFFMASK | IP_MF)) != 0) {    p = ip_reass(p);    if(p == NULL) {      return ERR_OK;    }    iphdr = p->payload;  }#else /* IP_REASSEMBLY */  if((IPH_OFFSET(iphdr) & htons(IP_OFFMASK | IP_MF)) != 0) {    pbuf_free(p);    DEBUGF(IP_DEBUG, ("IP packet dropped since it was fragmented (0x%x).\n",		      ntohs(IPH_OFFSET(iphdr))));#ifdef IP_STATS    ++stats.ip.opterr;    ++stats.ip.drop;#endif /* IP_STATS */    return ERR_OK;  }#endif /* IP_REASSEMBLY */  #if IP_OPTIONS == 0  if(hl * 4 > IP_HLEN) {    DEBUGF(IP_DEBUG, ("IP packet dropped since there were IP options.\n"));    pbuf_free(p);    #ifdef IP_STATS    ++stats.ip.opterr;    ++stats.ip.drop;#endif /* IP_STATS */    return ERR_OK;  }  #endif /* IP_OPTIONS == 0 */  /* send to upper layers */#if IP_DEBUG  DEBUGF(IP_DEBUG, ("ip_input: \n"));  ip_debug_print(p);  DEBUGF(IP_DEBUG, ("ip_input: p->len %d p->tot_len %d\n", p->len, p->tot_len));#endif /* IP_DEBUG */     switch(IPH_PROTO(iphdr)) {#if LWIP_UDP > 0      case IP_PROTO_UDP:    udp_input(p, inp);    break;#endif /* LWIP_UDP */#if LWIP_TCP > 0      case IP_PROTO_TCP:    tcp_input(p, inp);    break;#endif /* LWIP_TCP */  case IP_PROTO_ICMP:    icmp_input(p, inp);    break;  default:    /* send ICMP destination protocol unreachable unless is was a broadcast */    if(!ip_addr_isbroadcast(&(iphdr->dest), &(inp->netmask)) &&       !ip_addr_ismulticast(&(iphdr->dest))) {      p->payload = iphdr;      icmp_dest_unreach(p, ICMP_DUR_PROTO);    }    pbuf_free(p);    DEBUGF(IP_DEBUG, ("Unsupported transportation protocol %d\n", IPH_PROTO(iphdr)));#ifdef IP_STATS    ++stats.ip.proterr;    ++stats.ip.drop;#endif /* IP_STATS */  }  return ERR_OK;}#endif/*-----------------------------------------------------------------------------------*//* ip_output_if: * * Sends an IP packet on a network interface. This function constructs * the IP header and calculates the IP header checksum. If the source * IP address is NULL, the IP address of the outgoing network * interface is filled in as source address. *//*-----------------------------------------------------------------------------------*/#include "../inc/board.h"struct memp {  struct memp *next;};struct mem {  mem_size_t next, prev;  u8_t used;#if MEM_ALIGNMENT == 2  u8_t dummy;  u16_t dummy2;#endif /* MEM_ALIGNEMNT == 2 */};struct IPData {	EthHeader		ehdr;	unsigned char	data[TCP_MSS+4];};//static	struct IPData	sendPkt;static	struct IPData	* const sendPkt = (struct IPData *)(((unsigned char*)MEM_ALIGN(DRAM_MAP_DLOAD_BUF_ADDR))+															MEM_ALIGN_SIZE((sizeof(struct memp*)+															sizeof(u16_t)*2)															*MEMP_MAX)+(MEMP_NUM_PBUF *															MEM_ALIGN_SIZE(sizeof(struct pbuf) +															sizeof(struct memp)) +															MEMP_NUM_TCP_PCB *															MEM_ALIGN_SIZE(sizeof(struct tcp_pcb) +															sizeof(struct memp)) +															MEMP_NUM_TCP_PCB_LISTEN *															MEM_ALIGN_SIZE(sizeof(struct tcp_pcb_listen) +															sizeof(struct memp)) +															MEMP_NUM_TCP_SEG *															MEM_ALIGN_SIZE(sizeof(struct tcp_seg) +															sizeof(struct memp)))+															(PBUF_POOL_SIZE*															MEM_ALIGN_SIZE(PBUF_POOL_BUFSIZE + 															sizeof(struct pbuf))) +															MEM_ALIGN_SIZE(MEM_SIZE + 															sizeof(struct mem))															);err_tip_output_if(struct pbuf *p, u32_t *src, u32_t *dest,		   u8_t ttl, u8_t proto){	u16_t	len;	struct pbuf *next;	IPpkt	*ip;	TCPpkt	*tcp;	if(pbuf_header(p, IP_HLEN)) {		DEBUGF(IP_DEBUG, ("ip_output: not enough room for IP header in pbuf\n"));#ifdef IP_STATS		++stats.ip.err;#endif /* IP_STATS */		pbuf_free(p);		return ERR_BUF;	}	len = 0;	for (next=p;next!=NULL;next=next->next) {//		printf("pbuf len is %d.\n", next->len);//		bcopy(((unsigned char*)next->payload), &sendPkt.data[len], next->len);		bcopy(((unsigned char*)next->payload), &sendPkt->data[len], next->len);		len += next->len;	}//	ip = (IPpkt*)&sendPkt;//	tcp = (TCPpkt*)&sendPkt;	ip = (IPpkt*)sendPkt;	tcp = (TCPpkt*)sendPkt;//	printf("ip version is %d, ip head len is %d, ip len is %d, src ip is %d.%d.%d.%d, dst ip is %d.%d.%d.%d.\n",//		(ip->vlt>>12)&0xf, ((ip->vlt>>8)&0xf)<<2, ip->length,//		(ip->src_ip>>24)&0xff, (ip->src_ip>>16)&0xff, (ip->src_ip>>8)&0xff, (ip->src_ip)&0xff, //		(ip->dst_ip>>24)&0xff, (ip->dst_ip>>16)&0xff, (ip->dst_ip>>8)&0xff, (ip->dst_ip)&0xff);//	printf("tcp src port is %d, tcp dst port is %d, tcp head len is %d.\n", tcp->src_port, tcp->dst_port,//		((tcp->_offset_flags>>12)&0xf)<<2);//	printf("len is %d, p total len is %d.\n", len, p->tot_len);//	if(len!=p->tot_len) {//		printf("Error in %d, of file %s\n", __LINE__, __FILE__);//	} else {//		printf("Send here.\n");//		sendIP((IPpkt*)&sendPkt, (unsigned short)len-IP_HLEN, proto, *dest);		sendIP((IPpkt*)sendPkt, (unsigned short)len-IP_HLEN, proto, *dest);//	}}/*-----------------------------------------------------------------------------------*//* ip_output: * * Simple interface to ip_output_if. It finds the outgoing network * interface and calls upon ip_output_if to do the actual work. *//*-----------------------------------------------------------------------------------*/err_tip_output(struct pbuf *p, u32_t *src_ip, u32_t *dest_ip,		u8_t ttl, u8_t proto){  return ip_output_if(p, src_ip, dest_ip, ttl, proto);}/*-----------------------------------------------------------------------------------*/#if IP_DEBUGvoidip_debug_print(struct pbuf *p){  struct ip_hdr *iphdr = p->payload;  u8_t *payload;  payload = (u8_t *)iphdr + IP_HLEN/sizeof(u8_t);    DEBUGF(IP_DEBUG, ("IP header:\n"));  DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));  DEBUGF(IP_DEBUG, ("|%2d |%2d |   %2d  |      %4d     | (v, hl, tos, len)\n",		    IPH_V(iphdr),		    IPH_HL(iphdr),		    IPH_TOS(iphdr),		    ntohs(IPH_LEN(iphdr))));  DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));  DEBUGF(IP_DEBUG, ("|    %5d      |%d%d%d|    %4d   | (id, flags, offset)\n",		    ntohs(IPH_ID(iphdr)),		    ntohs(IPH_OFFSET(iphdr)) >> 15 & 1,		    ntohs(IPH_OFFSET(iphdr)) >> 14 & 1,		    ntohs(IPH_OFFSET(iphdr)) >> 13 & 1,		    ntohs(IPH_OFFSET(iphdr)) & IP_OFFMASK));  DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));  DEBUGF(IP_DEBUG, ("|   %2d  |   %2d  |    0x%04x     | (ttl, proto, chksum)\n",		    IPH_TTL(iphdr),		    IPH_PROTO(iphdr),		    ntohs(IPH_CHKSUM(iphdr))));  DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));  DEBUGF(IP_DEBUG, ("|  %3ld  |  %3ld  |  %3ld  |  %3ld  | (src)\n",		    ntohl(iphdr->src.addr) >> 24 & 0xff,		    ntohl(iphdr->src.addr) >> 16 & 0xff,		    ntohl(iphdr->src.addr) >> 8 & 0xff,		    ntohl(iphdr->src.addr) & 0xff));  DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));  DEBUGF(IP_DEBUG, ("|  %3ld  |  %3ld  |  %3ld  |  %3ld  | (dest)\n",		    ntohl(iphdr->dest.addr) >> 24 & 0xff,		    ntohl(iphdr->dest.addr) >> 16 & 0xff,		    ntohl(iphdr->dest.addr) >> 8 & 0xff,		    ntohl(iphdr->dest.addr) & 0xff));  DEBUGF(IP_DEBUG, ("+-------------------------------+\n"));}#endif /* IP_DEBUG *//*-----------------------------------------------------------------------------------*/#if BYTE_ORDER==LITTLE_ENDIANu16_thtons(u16_t n){  return ((n & 0xff) << 8) | ((n & 0xff00) >> 8);}/*-----------------------------------------------------------------------------------*/u16_tntohs(u16_t n){  return htons(n);}/*-----------------------------------------------------------------------------------*/u32_thtonl(u32_t n){  return ((n & 0xff) << 24) |    ((n & 0xff00) << 8) |    ((n & 0xff0000) >> 8) |    ((n & 0xff000000) >> 24);}/*-----------------------------------------------------------------------------------*/u32_tntohl(u32_t n){  return htonl(n);}/*-----------------------------------------------------------------------------------*/#endif/*voidbcopy(const void *src, void *dst, unsigned int size){  char *csrc, *cdst;  unsigned int i;  csrc = (char *)src;  cdst = dst;    for(i = 0; i < size; ++i) {    cdst[i] = csrc[i];  }    }*//*-----------------------------------------------------------------------------------*///void//xzero(void *s, int n)//{//  for(--n ;n >= 0; --n) {//    ((char *)s)[n] = 0;//  }//}/*-------------------------------------------------------------------------  /*-----------------------------------------------------------------------------------*//* chksum: * * Sums up all 16 bit words in a memory portion. Also includes any odd byte. * This function is used by the other checksum functions. * * For now, this is not optimized. Must be optimized for the particular processor * arcitecture on which it is to run. Preferebly coded in assembler. *//*-----------------------------------------------------------------------------------*/static unsigned int chksum(void *dataptr, u16_t len){	unsigned int acc;	for(acc = 0; len > 1; len -= 2) {		acc += *((u16_t *)dataptr)++;	}	/* add up any odd byte */	if(len == 1) {		acc += ((u16_t)((*(u8_t *)dataptr) & 0xff) << 8);	}	return acc;}/*-----------------------------------------------------------------------------------*//* inet_chksum_pseudo: * * Calculates the pseudo Internet checksum used by TCP and UDP for a pbuf chain. *//*-----------------------------------------------------------------------------------*/u16_tinet_chksum_pseudo(struct pbuf *p,		   u32_t *src, u32_t *dest,		   u8_t proto, u32_t proto_len){  u32_t acc;  struct pbuf *q;  u8_t swapped, i;  acc = 0;  swapped = 0;  for(q = p; q != NULL; q = q->next) {        acc += chksum(q->payload, q->len);    while(acc >> 16) {      acc = (acc & 0xffff) + (acc >> 16);    }    if(q->len % 2 != 0) {      swapped = 1 - swapped;      acc = ((acc & 0xff) << 8) | ((acc & 0xff00) >> 8);    }  }  if(swapped) {    acc = ((acc & 0xff) << 8) | ((acc & 0xff00) >> 8);  }    acc += (*src&0xffff);  acc += ((*src>>16)&0xffff);  acc += (*dest&0xffff);  acc += ((*dest>>16)&0xffff);  acc += (u32_t)htons((u16_t)proto);  acc += (u32_t)htons(proto_len);   while(acc >> 16) {    acc = (acc & 0xffff) + (acc >> 16);  }  return ~(acc & 0xffff);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -