Ejemplo n.º 1
0
int pcap_io_recv_blocking(void* packet, int max_len)
{
	int res;
	struct pcap_pkthdr *header;
	const u_char *pkt_data;

	if(pcap_io_running<=0)
		return -1;

	if((res = pcap_next_ex(adhandle, &header, &pkt_data)) > 0)
	{
		ethernet_header *ph=(ethernet_header*)pkt_data;
		if((mac_compare(ph->dst,virtual_mac)!=0)&&(mac_compare(ph->dst,broadcast_mac)!=0))
		{
			return 0;
		}

		memcpy(packet,pkt_data,header->len);

		if(dump_pcap)
			pcap_dump((u_char*)dump_pcap,header,(u_char*)packet);

		if(packet_log)
		{
			int i=0;
			int n=0;
			int plen=header->len;

			fprintf(packet_log,"PACKET RECV: %d BYTES\n",plen);
			for(i=0,n=0;i<plen;i++)
			{
				fprintf(packet_log,"%02x",((unsigned char*)packet)[i]);
				n++;
				if(n==16)
				{
					fprintf(packet_log,"\n");
					n=0;
				}
				else
				fprintf(packet_log," ");
			}
			fprintf(packet_log,"\n");
		}

		return header->len;
	}

	return -1;
}
Ejemplo n.º 2
0
int pcap_io_send(void* packet, int plen)
{
	struct pcap_pkthdr ph;

	if(pcap_io_running<=0)
		return -1;
	//printf("WINPCAP:  * pcap io: Sending %d byte packet.\n",plen);

	if(dump_pcap)
	{
		gettimeofday(&ph.ts,NULL);
		ph.caplen=plen;
		ph.len=plen;
		pcap_dump((u_char*)dump_pcap,&ph,(u_char*)packet);
	}

	if(packet_log)
	{
		int i=0;
		int n=0;

		fprintf(packet_log,"PACKET SEND: %d BYTES\n",plen);
		for(i=0,n=0;i<plen;i++)
		{
			fprintf(packet_log,"%02x",((unsigned char*)packet)[i]);
			n++;
			if(n==16)
			{
				fprintf(packet_log,"\n");
				n=0;
			}
			else
			fprintf(packet_log," ");
		}
		fprintf(packet_log,"\n");
	}

	if(mac_compare(((ethernet_header*)packet)->dst,broadcast_mac)==0)
	{
		static u_char pack[65536];
		memcpy(pack,packet,plen);

		((ethernet_header*)packet)->dst=host_mac;
		pcap_sendpacket(adhandle, pack, plen);
	}

	return pcap_sendpacket(adhandle, (const u_char*)packet, plen);
}
Ejemplo n.º 3
0
int pcap_io_send(void* packet, int plen)
{
	struct pcap_pkthdr ph;

	if(pcap_io_running<=0)
		return -1;
	emu_printf(" * pcap io: Sending %d byte packet.\n",plen);

	if (pcap_mode==bridged)
	{
		if(((ethernet_header*)packet)->protocol == 0x0008) //IP
		{
#ifndef PLOT_VERSION
			virtual_ip = ((ip_header*)((u8*)packet+sizeof(ethernet_header)))->src;
#endif
			virtual_mac = ((ethernet_header*)packet)->src;
		}
		if(((ethernet_header*)packet)->protocol == 0x0608) //ARP
		{
#ifndef PLOT_VERSION
			virtual_ip = ((arp_packet*)((u8*)packet+sizeof(ethernet_header)))->p_src;
#endif
			virtual_mac = ((ethernet_header*)packet)->src;

			((arp_packet*)((u8*)packet+sizeof(ethernet_header)))->h_src = host_mac;
		}
		((ethernet_header*)packet)->src = host_mac;
	}

	if(dump_pcap)
	{
		gettimeofday(&ph.ts,NULL);
		ph.caplen=plen;
		ph.len=plen;
		pcap_dump((u_char*)dump_pcap,&ph,(u_char*)packet);
	}

	if(packet_log)
	{
		int i=0;
		int n=0;

		fprintf(packet_log,"PACKET SEND: %d BYTES\n",plen);
		for(i=0,n=0;i<plen;i++)
		{
			fprintf(packet_log,"%02x",((unsigned char*)packet)[i]);
			n++;
			if(n==16)
			{
				fprintf(packet_log,"\n");
				n=0;
			}
			else
			fprintf(packet_log," ");
		}
		fprintf(packet_log,"\n");
	}

	if (pcap_mode==switched)
	{
	if(mac_compare(((ethernet_header*)packet)->dst,broadcast_mac)==0)
	{
		static char pack[65536];
		memcpy(pack,packet,plen);

		((ethernet_header*)packet)->dst=host_mac;
		pcap_sendpacket(adhandle, (u_char*)pack, plen);
	}
	}

	return pcap_sendpacket(adhandle, (u_char*)packet, plen);
}