void pcap_callback(uint8_t *args, const struct pcap_pkthdr *header, const uint8_t *packet) { struct pkt p; int rc; bzero(&p, sizeof(p)); p.raw_packet = (uint8_t *)packet; p.pos = (uint8_t *)packet; p.len = header->caplen; p.ifc = (struct iface_config *) args; p.pcap_header = header; rc = parse_packet(&p); if (rc < 0) return; if (p.arp) { if (!check_arp(&p)) process_arp(&p); } else if (p.ns) { if (!check_ns(&p)) process_ns(&p); } else if (p.na) { if (!check_na(&p)) process_na(&p); } }
int process_eth(struct net_iface *iface, void *data) { int result; struct pkt_buff pkt; struct ioq_header *ioq; unsigned int size; struct ether_header *eth; result = 0; ioq = data; size = ntohs(ioq->byte_length); pkt_fill(&pkt, data, size + sizeof(struct ioq_header)); eth = pkt_pull(&pkt, ETHER_HDR_LEN); result = process_arp(iface, ioq, eth, &pkt); // switch (eth->ether_type) // { // case ETHERTYPE_ARP: // result = process_arp(iface, ioq, eth, &pkt); // break; // default: // result = 1; // break; // } return result; }
int process_eth(struct net_iface *iface, t_addr *data) { int result; struct ioq_header *ioq; unsigned int size; struct ether_header *eth; struct ether_arp *etharp; result = 0; ioq = data; size = ntohs(ioq->byte_length); eth = (struct ether_header*) (data + sizeof(struct ioq_header)); etharp = (struct ether_arp*) (data + (sizeof(struct ioq_header) + sizeof(struct ether_header))); switch (ntohs(eth->ether_type)) { case ETHERTYPE_ARP: //result = process_arp(iface, ioq, eth, &pkt); result = process_arp(iface, ioq, eth, etharp, data); break; default: result = 1; break; } return result; }
/*--------------------------------------------------------------------- * Method: sr_handlepacket(uint8_t* p,char* interface) * Scope: Global * * This method is called each time the router receives a packet on the * interface. The packet buffer, the packet length and the receiving * interface are passed in as parameters. The packet is complete with * ethernet headers. * * Note: Both the packet buffer and the character's memory are handled * by sr_vns_comm.c that means do NOT delete either. Make a copy of the * packet instead if you intend to keep it around beyond the scope of * the method call. * *---------------------------------------------------------------------*/ void sr_handlepacket(struct sr_instance* sr, uint8_t * packet/* lent */, unsigned int len, char* interface/* lent */) { /* REQUIRES */ assert(sr); assert(packet); assert(interface); printf("*** -> Received packet of length %d \n",len); /* Ensure that the length is at least enough for an ethernet header */ if (len < sizeof(struct sr_ethernet_hdr)) return; /* Handle ARP packet */ if (ethertype(packet) == ethertype_arp) { process_arp(sr, packet, len, interface); /* Handle IP packet */ } else { process_ip(sr, packet, len, interface); } }/* end sr_ForwardPacket */
int main(int argc, char** argv) { unsigned char flags = 0; init_cpu(); hw_init_flags(); hw_write_flags(0); link_init(); POINTER c = (POINTER)(ADDR_BASE_XIL + 0x06); *c = 0x100; UINT16 len = 0; /* IP address */ localmachine.localip = 0xC0A80146 + flags; /* 192.168.0.* */ /* Default gateway */ localmachine.defgw = 0xC0A80101; /* 192.168.0.1 */ /* Subnet mask */ localmachine.netmask = 0xFFFFFF00; /* Ethernet (MAC) address */ localmachine.localHW[5] = 0x00; localmachine.localHW[4] = 0x33; localmachine.localHW[3] = 0xCC; localmachine.localHW[2] = 0xAB; localmachine.localHW[1] = 0xBA; localmachine.localHW[0] = 0x11 + flags; data_port = UDP_DATA_PORT + flags; timer_pool_init(); NE2000Init(&localmachine.localHW[0]); arp_init(); udp_init(); tcp_init(); /* Initialize applications */ tcps_init(); udps_init(); while(1) { if (NETWORK_CHECK_IF_RECEIVED() == TRUE) { switch( received_frame.protocol) { case PROTOCOL_ARP: process_arp(&received_frame); break; case PROTOCOL_IP: len = process_ip_in(&received_frame); if(len < 0) break; switch(received_ip_packet.protocol){ case IP_ICMP: process_icmp_in (&received_ip_packet, len); break; case IP_UDP: process_udp_in (&received_ip_packet,len); break; case IP_TCP: process_tcp_in (&received_ip_packet, len); break; } break; } /* discard received frame */ NETWORK_RECEIVE_END(); } /* Application main loops */ /* Do not forget this!!! These don't get invoked magically :-) */ tcps_run(); udps_run(); /* manage arp cache tables */ arp_manage(); /* manage opened TCP connections (retransmissions, timeouts,...)*/ tcp_poll(); } return (EXIT_SUCCESS); }