Esempio n. 1
0
void
arpModel() {
	int n;
	// puts("----->ARP: Ready for requests...");
	while (1) {
		// rset = allset;
		FD_ZERO(&rset);
		FD_SET(un_listenfd, &rset);
		FD_SET(if_sockfd, &rset);
		maxfd = max(un_listenfd, if_sockfd);
		if (un_connfd != -1){
			printf("Add un_connfd to fs_set %d\n", un_connfd);
			FD_SET(un_connfd, &rset);
			maxfd = (maxfd, un_connfd);
		}
		Select(maxfd + 1, &rset, NULL, NULL, NULL);

		if (FD_ISSET(un_listenfd, &rset)) {
			// puts("----->ARP: Request to establish domain socket connection.");
			accept_conn(un_listenfd);
		}

		if (un_connfd != -1 && FD_ISSET(un_connfd, &rset)) {
			struct hwaddr hwaddr_info;
			bzero(&hwaddr_info, sizeof(struct hwaddr));
			if ((n = recv_un(un_connfd, &hwaddr_info)) > 0) {
				// puts("----->ARP: Incoming un_packet. Handing over...");
				// printf("Requested IP is: %s\n", hwaddr_info.sll_ip);
				process_un(un_connfd, &hwaddr_info);
			} else if(n == 0) {
				puts("----->ARP: Time out.");
				close(un_connfd);
				un_connfd = -1;
			}
		}

		if (FD_ISSET(if_sockfd, &rset)) {
			struct arp_msg msg;
			bzero(&msg, sizeof(struct arp_msg));
			if ((n = recv_arp(if_sockfd, &msg)) == 0) {
				// puts("*********MSG received***********");
				// print_msg(&msg);
				if (msg.op == ARP_REQ) {
					// puts("----->ARP: Incoming PF_PACKET: ARP request. Handing over...");
					process_arp_request(if_sockfd, &msg);
				} else if (msg.op == ARP_REP) {
					// puts("----->ARP: Incoming PF_PACKET: ARP reply. Handing over...");
					process_arp_reply(if_sockfd, &msg);
				}
			}
		}
	}
}
Esempio n. 2
0
int get_remote_mac(pcap_t *cap_dev, const u_char *if_addr, u_long sip, u_long dip, u_char *remote_mac) {
	
	u_char arp_packet[sizeof(struct ethhdr) + sizeof(arphdr_ether)];
	struct pcap_pkthdr *header;
	const u_char *pkt_data;
	int res = 0;

	/* In case we fail */
	memset(remote_mac, 0, ETH_ALEN);

	/* Build the ARP request packet */
	generate_arp_request(arp_packet, if_addr, sip, dip);
	/* Send the ARP request */
	/* There's a race condition here because the reply might arrive before we capture it */
	if (pcap_sendpacket(cap_dev, arp_packet, sizeof(arp_packet)) != 0)
		return -1;
	int timeouts = 0;
	int packets = 0;
	/* Start the capture */
 	while ((res = pcap_next_ex(cap_dev, &header, &pkt_data)) >= 0)
	{

		//static int timeouts = 0;
		//static int packets = 0;

		/* Timeout elapsed? */
		if (res == 0) {
			if (++timeouts > CAP_MAX_TIMEOUTS)
				break;
		}
		else {
			/* Look for the ARP reply (source and destination are reversed) */
			if (process_arp_reply(header, pkt_data, dip, sip, remote_mac) == 0)
			{
				printf("t:%d, p:%d\t",timeouts,packets);
				return 0;
			}
			/* Seen too many packets without finding ours? */
			if (++packets >= CAP_MAX_PACKETS)
						break;
		}
	}
	/* Didn't receive the appropriate ARP reply */
	return -2;
}
Esempio n. 3
0
void process_arp_packet( struct sr_instance *sr, const uint8_t *packet, unsigned int len, const char *interface) {

	assert(sr);
	assert(packet);
	assert(interface);

	arp_hdr *arp_packet = get_arp_hdr(packet, len);
	switch(ntohs(arp_packet->arp_op)) {

		case ARP_OP_REQUEST:
			process_arp_request(sr, packet, len, interface);
			break;

		case ARP_OP_REPLY:
			process_arp_reply(sr, packet, len, interface);
			break;

		default: return;
	}
}