/* * Loop indefinitely listening for RARP requests on the * interfaces in 'iflist'. */ void rarp_loop(void) { int cc, fd, numfd = 0, i; u_int bufsize; struct pollfd *pfd; u_char *buf, *bp, *ep; struct if_info *ii; if (iflist == 0) { error(FATAL, "no interfaces"); /* NOTREACHED */ } if (ioctl(iflist->ii_fd, BIOCGBLEN, (caddr_t)&bufsize) < 0) { error(FATAL, "BIOCGBLEN: %s", strerror(errno)); /* NOTREACHED */ } arptab_init(); if (pledge("stdio rpath dns", NULL) == -1) error(FATAL, "pledge"); buf = malloc((size_t) bufsize); if (buf == 0) { error(FATAL, "malloc: %s", strerror(errno)); /* NOTREACHED */ } /* * Initialize the set of descriptors to listen to. */ for (ii = iflist; ii; ii = ii->ii_next) numfd++; pfd = reallocarray(NULL, numfd, sizeof(*pfd)); if (pfd == NULL) { error(FATAL, "reallocarray: %s", strerror(errno)); /* NOTREACHED */ } for (i = 0, ii = iflist; ii; ii = ii->ii_next, i++) { pfd[i].fd = ii->ii_fd; pfd[i].events = POLLIN; } while (1) { if (poll(pfd, numfd, -1) == -1) { if (errno == EINTR) continue; error(FATAL, "poll: %s", strerror(errno)); /* NOTREACHED */ } for (i = 0, ii = iflist; ii; ii = ii->ii_next, i++) { if (pfd[i].revents == 0) continue; fd = ii->ii_fd; again: cc = read(fd, (char *)buf, bufsize); /* Don't choke when we get ptraced */ if (cc < 0 && errno == EINTR) goto again; if (cc < 0) { error(FATAL, "read: %s", strerror(errno)); /* NOTREACHED */ } /* Loop through the packet(s) */ #define bhp ((struct bpf_hdr *)bp) bp = buf; ep = bp + cc; while (bp < ep) { int caplen, hdrlen; caplen = bhp->bh_caplen; hdrlen = bhp->bh_hdrlen; if (rarp_check(bp + hdrlen, caplen)) rarp_process(ii, bp + hdrlen); bp += BPF_WORDALIGN(hdrlen + caplen); } } } free(pfd); }
int main(void) { unsigned int num_bytes ; WDTCTL = WDTPW + WDTHOLD ; Init_CLK() ; Init_PSPI() ; P9DIR |= IrDA_OUT; P9OUT &=~IrDA_OUT; MAIN_POWER_ON ; /* for(;;){ for(i=0;i<40;i++) { P9OUT |= IrDA_OUT; __delay_cycles(20000); P9OUT &=~IrDA_OUT; __delay_cycles(20000); } __delay_cycles(5000000); } */ /* for(;;){ for(j=0;j<40;j++){ for(i=0;i<50;i++) { P9OUT &=~IrDA_OUT; __delay_cycles(270); P9OUT |= IrDA_OUT; __delay_cycles(270); } __delay_cycles(20000); } __delay_cycles(5000000); } */ myNode.node.ip = IP4_ADDR(103,1,168,192) ; myNode.node.mask = IP4_ADDR(0,255,255,255) ; myNode.node.gate = IP4_ADDR(1,1,168,192) ; myNode.node.mac[0] = 0x00 ; myNode.node.mac[1] = 0x60 ; myNode.node.mac[2] = 0x97 ; myNode.node.mac[3] = 0x48 ; myNode.node.mac[4] = 0xF4 ; myNode.node.mac[5] = 0xAB ; encInit() ; nicSetMacAddress() ; encPhyWrite(0x14,0x0470) ; arptab_init() ; ENC_SLEEP() ;// TEST ENC_WAKEUP();// TEST while(1) { num_bytes = encPacketReceive(&rxdnetbuf); if(num_bytes > 0) { if(rxdnetbuf.etherframe.protocal==ARP_PACKET) // 收到一个AR请求包 { if(rxdnetbuf.arpframe.operation==0x0100) arp_answer() ; // ARP request,处理arp数据包 else if(rxdnetbuf.arpframe.operation==0x0200) arp_process() ; // ARP answer } else if( (rxdnetbuf.etherframe.protocal == IP_PACKET) // 收到一个IP包 && ((rxdnetbuf.ipframe.verandihl&0xf0) == 0x40) // IP V4 && (rxdnetbuf.ipframe.destip[0] == myNode.nodebytes.ipbytes[0]) && (rxdnetbuf.ipframe.destip[1] == myNode.nodebytes.ipbytes[1]) && (rxdnetbuf.ipframe.destip[2] == myNode.nodebytes.ipbytes[2]) && (rxdnetbuf.ipframe.destip[3] == myNode.nodebytes.ipbytes[3])) { arp_ip_mac() ; if(rxdnetbuf.ipframe.protocal == 1) // ICMP { if(rxdnetbuf.icmpframe.type == 8) // ECHO ping_answer() ; } else if(rxdnetbuf.ipframe.protocal == 17) // UDP { // net_udp_rcv() ; _NOP(); // Clear_UPD_Buf() ; } /* else if(rxdnetbuf.ipframe.protocal == 6) // TCP tcp_rcve(&rxdnetbuf) ; updatearptab() ; */ } } } }