LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 03-04-2009, 07:13 AM   #1
kalps
LQ Newbie
 
Registered: Mar 2009
Posts: 11

Rep: Reputation: 0
Unhappy 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.
 
Old 03-04-2009, 09:09 PM   #2
kalps
LQ Newbie
 
Registered: Mar 2009
Posts: 11

Original Poster
Rep: Reputation: 0
Hai everybody,
Thanks for viewing my post.Can anyone suggest me a good e-book on libpcap??

I am doing programs in c.
 
  


Reply



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
LXer: DIS 29500/OOXML - What you can do LXer Syndicated Linux News 0 03-14-2008 04:10 PM
Dis montylee Linux - Newbie 0 01-06-2007 09:17 AM
my own lin dis. ewt3y Linux - Distributions 2 08-11-2005 04:27 AM
Dis Read Failed? sheridansellers Mandriva 0 12-14-2004 06:51 PM
Trash Can Up And Dis... opioid Linux - Software 6 05-12-2004 04:51 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 01:20 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