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

📄 ether.c

📁 mips架构的bootloader,99左右的版本 但源代码现在没人更新了
💻 C
📖 第 1 页 / 共 2 页
字号:
the one's comp value into the chksum field. */tm->icmp_cksum = ~((~m->icmp_cksum) - 0x800);#ifndef MIPSEBunswapPkt(te);#endif/* compute csum of ip hdr -> t */ti->ip_sum = 0;sp = (Ushort *) ti;for (n=0,t=0;n<(sizeof(struct ip)/sizeof(Ushort));n++,sp++) t += *sp;t = (t&0xffff)+(t>>16);ti->ip_sum = ~t;if (vflag) printMem((char *)te,i->ip_len+sizeof(struct ether_header));#ifdef VERBOSEif (vflag) printPkt(te,64);#endifif (re_ether) swap32n(te,i->ip_len+sizeof(struct ether_header));ether_driver(ETHER_TBARDY,0,0);return(1);}/**************************************************************  arpRequest(e)*/arpRequest(re)struct ether_header *re;{struct arphdr *ra,*ta;Ulong myip,tpa;struct ether_header *te;int ipi[4],len;assert(re != 0);ra = (struct arphdr *)(re+1);if (!(ra->ar_hrd == ARPHRD_ETHER && ra->ar_pro == 0x800 && ra->ar_hln == 6 &&       ra->ar_pln == 4 && ra->ar_op == ARPOP_REQUEST)) return;/* check the IP address */sscanf(getMonEnv("ipaddr"),"%d.%d.%d.%d", &ipi[0],&ipi[1],&ipi[2],&ipi[3]);myip = ((ipi[0]<<24) | (ipi[1]<<16) | (ipi[2]<<8) | ipi[3]);bcopy(ra->ar_tpa,&tpa,4);if (tpa != myip) return; /* not me */len = 64;te = (struct ether_header *) ether_driver(ETHER_GETTBA,0,&len);te->ether_dhost = re->ether_shost;bcopy(EnetAddr,&(te->ether_shost),6);te->ether_type = re->ether_type;ta = (struct arphdr *) (te+1);ta->ar_hrd = ra->ar_hrd;ta->ar_pro = ra->ar_pro;ta->ar_hln = ra->ar_hln;ta->ar_pln = ra->ar_pln;ta->ar_op = ARPOP_REPLY;bcopy(EnetAddr,ta->ar_sha,6);bcopy(ra->ar_tpa,ta->ar_spa,4);bcopy(ra->ar_sha,ta->ar_tha,6);bcopy(ra->ar_spa,ta->ar_tpa,4);#ifndef MIPSEBunswapPkt(te);#endifif (vflag) printMem((char *)te,64);#ifdef VERBOSEif (vflag) printPkt(te,64);#endifif (re_ether) swap32n(te,64);ether_driver(ETHER_TBARDY,0,0);}/**************************************************************  printMem(p,n)*	print 'n' bytes of memory starting at 'p'.*/printMem(p,n)char *p;int n;{int i,len;char ascii[17];#ifdef HDRONLYif (n > 80) n = 80;#endifprintf("\n%08x  ",p);len = 0;for (i=0;;i++) {	if (i && i%16 == 0) {		ascii[len] = 0;		printf("  %s\n",ascii);		if (i>=n) break;		len = 0;		printf("%08x  ",&p[i]);		}	if (i>=n) {		printf("   ");		ascii[len++] = ' ';		}	else {		printf("%02x ",0xff&p[i]);		ascii[len++] = (isprint(p[i]))?p[i]:'.';		}	}}#ifdef VERBOSE/**************************************************************  printPkt(e,len)*	print the contents of an Ethernet header*	If the next item is an IP header, print that also.*/printPkt(e,len)struct ether_header *e;int len;{printMem((char *)e,len);printf("dhost=%02x:%02x:%02x:%02x:%02x:%02x   ",	e->ether_dhost.ether_addr_octet[0],	e->ether_dhost.ether_addr_octet[1],	e->ether_dhost.ether_addr_octet[2],	e->ether_dhost.ether_addr_octet[3],	e->ether_dhost.ether_addr_octet[4],	e->ether_dhost.ether_addr_octet[5]);printf("shost=%02x:%02x:%02x:%02x:%02x:%02x\n",	e->ether_shost.ether_addr_octet[0],	e->ether_shost.ether_addr_octet[1],	e->ether_shost.ether_addr_octet[2],	e->ether_shost.ether_addr_octet[3],	e->ether_shost.ether_addr_octet[4],	e->ether_shost.ether_addr_octet[5]);switch (e->ether_type) {	case ETHERTYPE_IP : printIp(e+1); break;	case ETHERTYPE_PUP: printf("type=PUP, not impl\n"); break;	case ETHERTYPE_ARP: printArp(e+1); break;	case ETHERTYPE_REVARP: printf("type=REVARP, not impl\n"); break;	default : printf("type=%04x\n",e->ether_type);	}}/**************************************************************  printIp(p)*	print the contents of an IP header*	If the next item is a UDP header, print that also.*/static printIp(p)struct ip *p;{struct ip q;bcopy(p,&q,sizeof(struct ip));printf("ip: ver=%02x len=%04x id=%04x ttl=%02x proto=%02x src=%08x dst=%08x\n", 		getIP_V(q.ip_vhl),q.ip_len,q.ip_id,q.ip_ttl,q.ip_p,		q.ip_src.s_addr,q.ip_dst.s_addr);switch (q.ip_p) {	case IP_IP:	printf("ip protocol\n"); break;	case IP_ICMP:	printf("icmp protocol\n"); break;	case IP_IGMP:	printf("igmp protocol\n"); break;	case IP_GGP:	printf("ggp protocol\n"); break;	case IP_TCP:	printf("tcp protocol\n"); break;	case IP_PUP:	printf("pup protocol\n"); break;	case IP_UDP:	printUdp(p+1); break;	default : 		printf("%02x: unknown ip protocol\n",q.ip_p);		return(1);	}}/**************************************************************  printUdp(p)*	print the contents of a UDP header*/static printUdp(p)struct udphdr *p;{printf("udp: sport=%04x dport=%04x ulen=%04x sum=%04x type=%04x seq=%04x ",	p->uh_sport,p->uh_dport,p->uh_ulen,p->uh_sum,p->uh_type,p->uh_seqno);switch (p->uh_type) {	case UDP_OPEN : printf("open \"%s\"\n",&(p->uh_seqno)); break;	case UDP_ACK : printf("ack\n"); break;	case UDP_DATA : printf("data\n"); break;	default : printf("%4x: unknown udp type\n",p->uh_type);	}}/**************************************************************  printArp(p)*	print the contents of a ARP header*/static printArp(p)struct arphdr *p;{printf("arp: hrd=%04x pro=%04x hln=%02x pln=%02x op=%04x \n",	p->ar_hrd,p->ar_pro,p->ar_hln,p->ar_pln,p->ar_op);printf("sha=%02x:%02x:%02x:%02x:%02x:%02x ",	p->ar_sha[0], p->ar_sha[1], p->ar_sha[2], p->ar_sha[3],	p->ar_sha[4], p->ar_sha[5]);printf("spa=%02x.%02x.%02x.%02x\n",	p->ar_spa[0], p->ar_spa[1], p->ar_spa[2], p->ar_spa[3]);printf("tha=%02x:%02x:%02x:%02x:%02x:%02x ",	p->ar_tha[0], p->ar_tha[1], p->ar_tha[2], p->ar_tha[3],	p->ar_tha[4], p->ar_tha[5]);printf("tpa=%02x.%02x.%02x.%02x\n",	p->ar_tpa[0], p->ar_tpa[1], p->ar_tpa[2], p->ar_tpa[3]);}#endif/**************************************************************  swap32n(p,n)*	Byte-swap the contents of 'n', 32-bit words pointed to by 'p'.*/swap32n(p,n)long *p;int n;{int i;if (n%4) n = (n/4)+1; /* 971115 */else n /= 4;for (i=0;i<n;i++) p[i] = swap32(p[i]);}#ifndef MIPSEB/**************************************************************  int swapPkt(e)*	Byte-swap the headers of an Ethernet packet*/int swapPkt(e)struct ether_header *e;{struct ip *i;struct udphdr *u;swapEh(e);if (e->ether_type == ETHERTYPE_ARP) {	swapArp((struct arphdr *)(e+1));	return(1);	}if (e->ether_type != ETHERTYPE_IP) return(0);i = (struct ip *)(e+1);swapIp(i);if (i->ip_p == IP_ICMP) swapIcmp((struct icmp *)(i+1));else if (i->ip_p == IP_UDP) swapUdp((struct udphdr *)(i+1));else printf("swapPkt: %02x: bad ip_p\n",i->ip_p);return(1);}/**************************************************************  unswapPkt(e)*	unByte-swap the headers of an Ethernet packet*/unswapPkt(e)struct ether_header *e;{struct ip *i;struct udphdr *u;if (e->ether_type == ETHERTYPE_ARP) {	swapArp((struct arphdr *)(e+1));	swapEh(e);	return;	}if (e->ether_type != ETHERTYPE_IP) {	printf("unswapPkt: %04x: bad ether_type\n",e->ether_type);	return;	}i = (struct ip *)(e+1);if (i->ip_p == IP_ICMP) {	swapIcmp((struct icmp *)(i+1));	swapIp(i);	swapEh(e);	return;	}if (i->ip_p != IP_UDP) {	printf("unswapPkt: %02x: bad ip_p\n",i->ip_p);	return;	}u = (struct udphdr *)(i+1);swapUdp(u);swapIp(i);swapEh(e);}/**************************************************************  swapEh(e)*	Byte-swap the Ethernet header*/swapEh(e)struct ether_header *e;{e->ether_type = swap16(e->ether_type);}/**************************************************************  swapIp(i)*	Byte-swap the IP header*/swapIp(i)struct ip *i;{Ulong t;i->ip_len = swap16(i->ip_len);i->ip_id = swap16(i->ip_id);i->ip_off = swap16(i->ip_off);i->ip_sum = swap16(i->ip_sum);bcopy(&(i->ip_src.s_addr),&t,4);t = swap32(t);bcopy(&t,&(i->ip_src.s_addr),4);bcopy(&(i->ip_dst.s_addr),&t,4);t = swap32(t);bcopy(&t,&(i->ip_dst.s_addr),4);}/**************************************************************  swapUdp(u)*	Byte-swap the UDP header*/swapUdp(u)struct udphdr *u;{u->uh_sport = swap16(u->uh_sport);u->uh_dport = swap16(u->uh_dport);u->uh_ulen = swap16(u->uh_ulen);u->uh_sum = swap16(u->uh_sum);u->uh_type = swap16(u->uh_type);u->uh_seqno = swap16(u->uh_seqno);}/**************************************************************  swapArp(a)*/swapArp(a)struct arphdr *a;{Ulong t;a->ar_hrd = swap16(a->ar_hrd);a->ar_pro = swap16(a->ar_pro);a->ar_op = swap16(a->ar_op);bcopy(a->ar_spa,&t,4);t = swap32(t);bcopy(&t,a->ar_spa,4);bcopy(a->ar_tpa,&t,4);t = swap32(t);bcopy(&t,a->ar_tpa,4);}/**************************************************************  swapIcmp(a)*/swapIcmp(m)struct icmp *m;{m->icmp_cksum = swap16(m->icmp_cksum);m->icmp_id = swap16(m->icmp_id);m->icmp_seq = swap16(m->icmp_seq);}#endif#else /* Tasking tools don't like empty files (sigh) */ether_foobar() {}#endif /* ETHERNET */

⌨️ 快捷键说明

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