📄 rtfifoif.c
字号:
char buf[1500]; char *bufptr; unsigned char size_of_packet[4]; int packet_size; /* Obtain the size of the packet and put it into the "len" variable. */ rtf_get(rtfifoif->readfd, &size_of_packet, 4); packet_size = size_of_packet[0] + size_of_packet[1] + size_of_packet[2] + size_of_packet[3]; /* Wait for a packet to arrive. */ while(rtf_isempty(rtfifoif->readfd)){ usleep(10); } len = rtf_get(rtfifoif->readfd, buf,(int) packet_size); /* We allocate a pbuf chain of pbufs from the pool. */ p = pbuf_alloc(PBUF_LINK, len, PBUF_POOL); if(p != NULL) { /* We iterate over the pbuf chain until we have read the entire packet into the pbuf. */ bufptr = &buf[0]; for(q = p; q != NULL; q = q->next) { /* Read enough bytes to fill this pbuf in the chain. The avaliable data in the pbuf is given by the q->len variable. */ /* read data into(q->payload, q->len); */ bcopy(bufptr, q->payload, q->len); bufptr += q->len; } /* acknowledge that packet has been read(); */ } else { rtl_printf("rtfifoif: Can't allocate memory for pbuf\n"); } return p; }/*-----------------------------------------------------------------------------------*//* * fifoif_output(): * * This function is called by the TCP/IP stack when an IP packet * should be sent. It calls the function called low_level_output() to * do the actuall transmission of the packet. * *//*-----------------------------------------------------------------------------------*/static err_trtfifoif_output(struct netif *netif, struct pbuf *p, struct ip_addr *ipaddr){ struct rtfifoif *rtfifoif; struct pbuf *q; struct eth_hdr *ethhdr; struct eth_addr *dest, mcastaddr; struct ip_addr *queryaddr; err_t err; u8_t i; rtfifoif = netif->state; /* Make room for Ethernet header. */ if(pbuf_header(p, sizeof(struct eth_hdr)) != 0) { /* The pbuf_header() call shouldn't fail, but we allocate an extra pbuf just in case. */ q = pbuf_alloc(PBUF_LINK, sizeof(struct eth_hdr), PBUF_RAM); if(q == NULL) { return ERR_MEM; } pbuf_chain(q, p); p = q; } /* Construct Ethernet header. Start with looking up deciding which MAC address to use as a destination address. Broadcasts and multicasts are special, all other addresses are looked up in the ARP table. */ queryaddr = ipaddr; if(ip_addr_isany(ipaddr) || ip_addr_isbroadcast(ipaddr, &(netif->netmask))) { dest = (struct eth_addr *)ðbroadcast; } else if(ip_addr_ismulticast(ipaddr)) { /* Hash IP multicast address to MAC address. */ mcastaddr.addr[0] = 0x01; mcastaddr.addr[1] = 0x0; mcastaddr.addr[2] = 0x5e; mcastaddr.addr[3] = ip4_addr2(ipaddr) & 0x7f; mcastaddr.addr[4] = ip4_addr3(ipaddr); mcastaddr.addr[5] = ip4_addr4(ipaddr); dest = &mcastaddr; } else { if(ip_addr_maskcmp(ipaddr, &(netif->ip_addr), &(netif->netmask))) { /* Use destination IP address if the destination is on the same subnet as we are. */ queryaddr = ipaddr; } else { /* Otherwise we use the default router as the address to send the Ethernet frame to. */ queryaddr = &(netif->gw); } dest = arp_lookup(queryaddr); } /* If the arp_lookup() didn't find an address, we send out an ARP query for the IP address. */ if(dest == NULL) { q = arp_query(netif, rtfifoif->ethaddr, queryaddr); if(q != NULL) { err = low_level_output(rtfifoif, q); pbuf_free(q); return err; } return ERR_MEM; } ethhdr = p->payload; for(i = 0; i < 6; i++) { ethhdr->dest.addr[i] = dest->addr[i]; ethhdr->src.addr[i] = rtfifoif->ethaddr->addr[i]; } ethhdr->type = htons(ETHTYPE_IP); return low_level_output(rtfifoif, p);}/*-----------------------------------------------------------------------------------*//* * rtfifoif_input(): * * This function should be called when a packet is ready to be read * from the interface. It uses the function low_level_input() that * should handle the actual reception of bytes from the network * interface. * *//*-----------------------------------------------------------------------------------*/static voidrtfifoif_input(struct netif *netif){ struct rtfifoif *rtfifoif; struct eth_hdr *ethhdr; struct pbuf *p; rtfifoif = netif->state; p = low_level_input(rtfifoif); if(p == NULL) { return; } ethhdr = p->payload; switch(htons(ethhdr->type)) { case ETHTYPE_IP: arp_ip_input(netif, p); pbuf_header(p, -14); if(ip_lookup(p->payload, netif)) { netif->input(p, netif); } break; case ETHTYPE_ARP: p = arp_arp_input(netif, rtfifoif->ethaddr, p); if(p != NULL) { low_level_output(rtfifoif, p); pbuf_free(p); } break; default: pbuf_free(p); break; }}/*-----------------------------------------------------------------------------------*/static voidarp_timer(void *arg){ arp_tmr(); sys_timeout(ARP_TMR_INTERVAL, (sys_timeout_handler)arp_timer, NULL);}/*-----------------------------------------------------------------------------------*//* * rtfifoif_init(): * * Should be called at the beginning of the program to set up the * network interface. It calls the function low_level_init() to do the * actual setup of the hardware. * *//*-----------------------------------------------------------------------------------*/voidrtfifoif_init(struct netif *netif){ struct rtfifoif *rtfifoif; rtfifoif = mem_malloc(sizeof(struct rtfifoif)); netif->state = rtfifoif; netif->name[0] = IFNAME0; netif->name[1] = IFNAME1; netif->output = rtfifoif_output; netif->linkoutput = low_level_output; rtfifoif->ethaddr = (struct eth_addr *)&(netif->hwaddr[0]); low_level_init(netif); arp_init(); _timeout(ARP_TMR_INTERVAL, (sys_timeout_handler)arp_timer, NULL);}/*-----------------------------------------------------------------------------------*/#endif //__RTFIFOOPTS__
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -