LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-05-2009, 01:25 AM   #1
kalps
LQ Newbie
 
Registered: Mar 2009
Posts: 11

Rep: Reputation: 0
how to send a tcp packet using libpcap?


Hi Everybody,

Iam new to linux and libpcap.In the prog below I try to send a TCP SYN packet. But it gives segmentation fault on execution.can anyone explain wats happening??




#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#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 = (struct tcphdr *) packet1;
tcp->source=htons(sport);
tcp->dest = htons(dport);
tcp->seq = 12345; //htonl(rand());
tcp->ack_seq = 0;
tcp->doff = 5;
tcp->res1=0;
tcp->window = 512;
tcp->syn = 1;
tcp->check=0;


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;

chpacket=(char *)packet;


bcopy(packet1,chpacket+sizeof(struct iphdr), sizeof(packet1));
printf("%s",chpacket);
if(pcap_sendpacket(descr,chpacket,packet_size)==-1)
perror("pcap_sendpacket");
else
printf("packet sent");

}

Last edited by kalps; 03-05-2009 at 01:31 AM.
 
Old 03-05-2009, 02:41 AM   #2
naghi32
Member
 
Registered: Dec 2008
Distribution: Slackware
Posts: 39

Rep: Reputation: 17
As much as i`m aware of libpcap was used only for capturing packets passing thru diferent interfaces.
Am i wrong ?
 
Old 03-05-2009, 03:05 AM   #3
kalps
LQ Newbie
 
Registered: Mar 2009
Posts: 11

Original Poster
Rep: Reputation: 0
Thanks

Hai naghi32,

U r right buddy.using pcap_open_live method i jus try to open ethernet interface(eth0). The prob is I know only to fill values in iphdr and tcp.(pointers to iphdr and tcphdr).how to pack these into a single packet and send?
Should I add any more info??

Thank u and continue helping.
 
Old 05-07-2009, 04:53 PM   #4
jjorge
LQ Newbie
 
Registered: May 2009
Posts: 1

Rep: Reputation: 0
libpcap programming

Hi kalps, I was looking for this code but additionally I was trying to capture the acknowledgments, then when I send a TCP packet I also want to capture the ACK confirmation to know delays and so on. Do you thinks that it is possible with this code? help me please, thanks.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Packet injected with Libpcap to localhost does not reach application MTsoul Linux - Networking 0 02-22-2009 11:43 PM
how can use libpcap to send packets,just like winpcap do? tassadaque Fedora 1 06-05-2008 11:43 AM
possible to send size zero tcp packet? jackandking Linux - Networking 3 07-05-2007 02:27 AM
problem in TCP packet live_dont_exist Programming 3 05-16-2005 06:00 AM
what is the method of reading the data in a packet using Libpcap gajaykrishnan Linux - Networking 0 02-20-2005 02:29 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:04 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration