All pastes #961404 Raw Edit

my_raw

public c v1 · immutable
#961404 ·published 2008-03-28 21:44 UTC
rendered paste body
#include <stdio.h>#include <pcap.h>#include <stdlib.h>#include <string.h>#include <pcap.h>#include "nw_hdr.h"void caught_packet(u_char *user_args,const struct pcap_pkthdr *header,const u_char *packet);int main(int arc,char *argv[]){	u_char *packet;	u_char *device;	struct pcap_pkthdr header;	pcap_t *pcap_handle;	char errbuff[PCAP_ERRBUF_SIZE];	device="wlan0";	if((pcap_handle=pcap_open_live(device,4096,1,0,errbuff))==NULL)	{		printf("pcap_open_live: %s\n",errbuff);		exit(1);	}	pcap_loop(pcap_handle,-1,caught_packet,NULL);}void caught_packet(u_char *user_args,const struct pcap_pkthdr *header,const u_char *packet){			int i;		struct ethernet_header *ether_hdr;	struct ip_header *ip_hdr;		ether_hdr=(struct ethernet_header *)packet;	ip_hdr=(struct ip_header *)packet+ETHER_HDR_LEN;		printf("ETHERNET HEADER:\n");	printf("Source: ");		for(i=0;i<ETHER_ADR_LEN;i++)	{		printf("%02x",ether_hdr->source_host[i]);		if(i!=ETHER_ADR_LEN-1)						printf(":");	}	printf("\n");	printf("Dest: ");	for(i=0;i<ETHER_ADR_LEN;i++)	{		printf("%02x",ether_hdr->dest_host[i]);		if(i!=ETHER_ADR_LEN-1)			printf(":");	}		printf("\n\n");		printf("source ip: %s\n",inet_ntoa(ip_hdr->source_ip));	printf("dest ip: %s\n",inet_ntoa(ip_hdr->dest_ip));	printf("ID: %i\n",ip_hdr->ident);	printf("Protocol: %i\n",ip_hdr->protocol);	printf("Length: %i\n",ntohs(ip_hdr->total_len));		memset(ether_hdr,'\0',sizeof(struct ethernet_header));	memset(ip_hdr,'\0',sizeof(struct ip_header));	}nw_hdr.h:void dump(const u_char *packet,int len){	int i;	int a;	int interval=0;	char ascii[16];	printf("packet length: %i\n",len);		for(i=0;i<=len;i++)	{		if(interval==16 || i==len)		{			ascii[interval]='\0';			for(a=interval;a<16;a++)				printf("   ");					printf(" | %s\n",ascii);						interval=0;		}				if(packet[i]>31 && packet[i]<127)			ascii[interval]=packet[i];		else			ascii[interval]='.';				if(i<len) 			printf("%02x ",packet[i]);					interval++;	}}#define ETHER_HDR_LEN 14#define ETHER_ADR_LEN 6struct ethernet_header{	u_int8_t dest_host[ETHER_ADR_LEN];	u_int8_t source_host[ETHER_ADR_LEN];	u_int16_t ether_type;};struct ip_header{	u_int8_t version_n_hdr_len;	u_int8_t type_of_service;	u_int16_t total_len;	u_int16_t ident;	u_int16_t flags_n_frag_offset;	u_int8_t time_to_live;	u_int8_t protocol;	u_int16_t headr_checksum;	u_int32_t source_ip;	u_int32_t dest_ip;};