LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 10-01-2008, 07:29 AM   #1
eddchr
LQ Newbie
 
Registered: Oct 2008
Posts: 9

Rep: Reputation: 0
Question [C/C++] Local client/server using raw socket /pcap


Hi,

We are trying to write a client/server program in c++. We have tried to use pcap to inject raw packets and sockets to receive packets. When the traffic are sent between two different computers everything seems to be ok. The problem occurs when the traffic are sent to the loopback. The packets are visible via Wireshark (driver level) but they never reaches the destination (application level).


Thanks in advance!
Best regards,
CC and EE
 
Old 10-01-2008, 10:08 AM   #2
David1357
Senior Member
 
Registered: Aug 2007
Location: South Carolina, U.S.A.
Distribution: Ubuntu, Fedora Core, Red Hat, SUSE, Gentoo, DSL, coLinux, uClinux
Posts: 1,302
Blog Entries: 1

Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by eddchr View Post
We have tried to use pcap to inject raw packets and sockets to receive packets.
Why are you using "pcap to inject raw packets"?

Why not just write a standard socket server and client? Make the server listen on INADDR_ANY:
Code:
/* Address to accept any incoming messages. */
#define	INADDR_ANY		((unsigned long int) 0x00000000)
and it will listen on all available interfaces.

Make the client connect to "127.0.0.1", and it can run on the same machine as the server.

If you need the ability to run the client on the same machine as the server and on other machines, make the default address "127.0.0.1" and add a command line option to specify the server address.
 
Old 10-01-2008, 03:01 PM   #3
eddchr
LQ Newbie
 
Registered: Oct 2008
Posts: 9

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by David1357 View Post
Why are you using "pcap to inject raw packets"?

Why not just write a standard socket server and client? Make the server listen on INADDR_ANY:
Code:
/* Address to accept any incoming messages. */
#define	INADDR_ANY		((unsigned long int) 0x00000000)
and it will listen on all available interfaces.

Make the client connect to "127.0.0.1", and it can run on the same machine as the server.

If you need the ability to run the client on the same machine as the server and on other machines, make the default address "127.0.0.1" and add a command line option to specify the server address.
The reason for not using standard sockets is that the client in this case has it's own TCP/IP-stack and therefore we need to use methods like raw sockets or pcap.

I'll try to describe the whole scenario a bit more.

The client (A) will try to establish a TCP connection to the server (B). B is listening using standard sockets while A first ensembles the whole packet internally via it's own stack and we must then make this packet appear at B. Since it is TCP we are using then we must establish the connection in a way so that the stack in linux, somehow, get's in the same stat as the stack inside A.

In short: Stack in A should be synced with linux stack (at B).
 
Old 10-01-2008, 04:38 PM   #4
David1357
Senior Member
 
Registered: Aug 2007
Location: South Carolina, U.S.A.
Distribution: Ubuntu, Fedora Core, Red Hat, SUSE, Gentoo, DSL, coLinux, uClinux
Posts: 1,302
Blog Entries: 1

Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by eddchr View Post
Wireshark (driver level) but they never reaches the destination (application level).
What device are you using to capture packets? Are you putting the device in promiscuous mode?
 
Old 10-02-2008, 03:50 AM   #5
eddchr
LQ Newbie
 
Registered: Oct 2008
Posts: 9

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by David1357 View Post
What device are you using to capture packets? Are you putting the device in promiscuous mode?
On the server-side we are using lo, which has a bunch aliases attached to it. We have also tried to use ethX, which results in the same.

Since we are using standard sockets (server-side) there is no need to use that mode for that purpose, but at the client-side it is necessary for receiving packets without having to bind to a specific port etc..

We have tried two different raw sockets, namely: link-layer and ip-layer.
When we are using the ip-layer the server receives, for example a TCP-syn, the packet successfully, but when the server answers with TCP-syn-ack it will be blocked by the TCP/IP-stack in the linux kernel, because there is no process which is listening/expecting such packet. Linux kernel will therefore send a TCP-rst back to the server, which destroys our connection.

Problem: How to get the linux kernel accept the TCP-syn-ack without listening via standard sockets (client-side)?

Problem: How to receive packets, locally, which are sent via PF_PACKET socket?
 
Old 10-02-2008, 10:26 AM   #6
David1357
Senior Member
 
Registered: Aug 2007
Location: South Carolina, U.S.A.
Distribution: Ubuntu, Fedora Core, Red Hat, SUSE, Gentoo, DSL, coLinux, uClinux
Posts: 1,302
Blog Entries: 1

Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by eddchr View Post
Problem: How to get the linux kernel accept the TCP-syn-ack without listening via standard sockets (client-side)?
Probably impossible without modifying the Linux kernel.

Quote:
Originally Posted by eddchr View Post
Problem: How to receive packets, locally, which are sent via PF_PACKET socket?
Same solution as above.

You are basically violating the rules used by the stack for determining how to respond to a SYN request. It sees that there is no corresponding local client and (properly) rejects it.
 
Old 10-02-2008, 03:38 PM   #7
eddchr
LQ Newbie
 
Registered: Oct 2008
Posts: 9

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by David1357 View Post
Probably impossible without modifying the Linux kernel.



Same solution as above.

You are basically violating the rules used by the stack for determining how to respond to a SYN request. It sees that there is no corresponding local client and (properly) rejects it.
Yes, we guess that is one solution. Though it is not the most beautiful one out there, but we'll investigate this further. I guess we need to try to grab the packet very early, and that is on the agenda for tomorrow!

Have you ever been in contact with UML (User mode linux) ? They must have solved this in some neat way, because they actually have two, or more, instances of the Linux's TCP/IP-stack. It seems that TAP is somewhat central around the UML, and perhaps that could be something to investigate as well?

Thank you very much for your feedback, it's really nice to have someone to share our thoughts with!
 
Old 10-02-2008, 03:43 PM   #8
David1357
Senior Member
 
Registered: Aug 2007
Location: South Carolina, U.S.A.
Distribution: Ubuntu, Fedora Core, Red Hat, SUSE, Gentoo, DSL, coLinux, uClinux
Posts: 1,302
Blog Entries: 1

Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by eddchr View Post
Have you ever been in contact with UML?
Not yet. If you learn something from its maintainers, please post your findings here so that others may benefit.
 
  


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
client server communication suing raw socket revanth Linux - Networking 1 03-13-2007 09:52 PM
client server raw socket without using command line argument edens_2001 Linux - Networking 1 03-13-2007 07:14 AM
cannot read data at server socket, though client socket sends it jacques83 Linux - Networking 0 11-15-2005 01:58 PM
How to Capture Raw Packets (no Decode) with PCAP kidskc Programming 1 11-02-2005 04:54 PM
Local webserver -- How to deny all client install their local web server--Please help b:z Linux - Networking 13 04-16-2005 07:11 PM

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

All times are GMT -5. The time now is 09:50 PM.

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