Can anyone help me to understand dis?
Hai everybody,
Im a newbie.New to libpcap and linux.
I have to send a tcp packet into the network.All I know is to fill iphdr values and tcphdr values. Sendpacket method is used to send the packet. but how can i correlate all these and do what i want to.
#include <netdb.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <netinet/if_ether.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <pcap.h>
#define srcip "10.142.17.127"
#define dstip "10.142.17.127"
#define sport 1111
#define dport 23
main()
{
pcap_t *descr;
char error[PCAP_ERRBUF_SIZE];
int i;
char *dev;
struct iphdr *iphdr;
struct tcphdr *tcp;
// u_char packet[sizeof(struct iphdr)+sizeof(struct tcphdr)+sizeof(struct ethhdr)];
char *packet,*packet1,*chpacket;
int data_size = 20;
int packet_size;
int tcp_opt_size = 0;
dev=pcap_lookupdev(error);
/*Open the device */
if((descr = pcap_open_live(dev, 100, 1, 1000, error) ) == NULL)
{
fprintf(stderr,"\nError opening device: %s\n", error);
return;
}
tcp_header = (struct tcphdr*) packet;
tcp_header->source = sport;
tcp_header->dest = dport;
tcp_header->seq = seqnum;
tcp_header->ack_seq = acknum;
tcp_header->doff = 5; /* data offset */
tcp_header->res1 = 0; /* reserved for future use (must be 0) */
tcp_header->fin = 0; /* FIN flag */
tcp_header->syn = 1; /* SYN flag */
tcp_header->rst = 0; /* RST flag */
tcp_header->psh = 0; /* PSH flag */
tcp_header->ack = 0; /* ACK flag */
tcp_header->urg = 0; /* URG flag */
tcp_header->res2 = 0; /* reserved (must be 0) */
// this i hope is right
iphdr=(struct iphdr*)packet;
iphdr->ihl = 20;
iphdr->version = 4;
iphdr->tot_len = htons(sizeof (struct iphdr));
iphdr->id = 1234;
iphdr->ttl = 250;
iphdr->frag_off= htons((unsigned short) 0x2000);
iphdr->protocol = 6;
iphdr->saddr =inet_addr(srcip);
iphdr->daddr =inet_addr(dstip);
iphdr->check = 0;
//upto this only i know
//this is my try
chpacket=(char *)packet;
bcopy(packet1,chpacket+sizeof(struct iphdr), sizeof(packet1));
printf("%s",chpacket);
if(pcap_sendpacket(descr,packet,packet_size)==-1)
perror("pcap_sendpacket");
else
printf("packet sent");
}
when i run this, i get segmentation fault.I dint know what should i do.
Sorry if i have don some blunder mistake. Hope u all will make me clear.
|