LinuxQuestions.org
Help answer threads with 0 replies.
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 04-05-2009, 06:04 AM   #1
route
LQ Newbie
 
Registered: Feb 2009
Posts: 22

Rep: Reputation: 0
how to send packets using c program


i want to send all the packets that i am recieving on port 80 to port 4000.
i am able to scan all the packets using libpcap library i.e able to extract the ip,tcp,udp headers etc.but i dont know how to send a packet from one port to another port.I am able to send data using send() say "hello world" from one port to another but i want to send the complete packet that i recieved through internet on port 80 without changing its source and destination.Is it possible.
please anybody reply soon.....
 
Old 04-05-2009, 01:50 PM   #2
kirukan
Senior Member
 
Registered: Jun 2008
Location: Eelam
Distribution: Redhat, Solaris, Suse
Posts: 1,278

Rep: Reputation: 148Reputation: 148
No idea about C program but iptable will help you to redirect the port, Your sound like demanding people to drive the solution.
 
Old 04-05-2009, 01:53 PM   #3
frieza
Senior Member
 
Registered: Feb 2002
Location: harvard, il
Distribution: Ubuntu 11.4,DD-WRT micro plus ssh,lfs-6.6,Fedora 15,Fedora 16
Posts: 3,233

Rep: Reputation: 406Reputation: 406Reputation: 406Reputation: 406Reputation: 406
http://beej.us/guide/bgnet/
might help get you going
considering iptables was probably written in C or c++ it certainly is possible
 
Old 04-05-2009, 06:17 PM   #4
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
iptables is the userland application that configures the netfilter kernel module. What the original poster is asking for is a specific purpose of netfilter/iptables, and is called port-forwarding. A Google search for 'port forward iptables' yields a plethora of useful links.
--- rod.
 
Old 04-06-2009, 01:54 AM   #5
route
LQ Newbie
 
Registered: Feb 2009
Posts: 22

Original Poster
Rep: Reputation: 0
Is it possible to do it without ip-table ? using c program
 
Old 04-06-2009, 03:58 AM   #6
frieza
Senior Member
 
Registered: Feb 2002
Location: harvard, il
Distribution: Ubuntu 11.4,DD-WRT micro plus ssh,lfs-6.6,Fedora 15,Fedora 16
Posts: 3,233

Rep: Reputation: 406Reputation: 406Reputation: 406Reputation: 406Reputation: 406
possible? yes? practical, prolly not, iptables is a C program afterall, perhaps get the source code for iptables and find out how iptables does it.
 
Old 04-06-2009, 11:36 AM   #7
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Quote:
Is it possible to do it without ip-table ? using c program
Why would you want to? If it has to be built into some other application, then use the C system() function to call iptables. This will result in a much more efficient system, as the packet filtering takes place right in the kernel where all packets are handled anyway. No need to re-invent any wheels.

--- rod.
 
Old 04-07-2009, 02:18 AM   #8
route
LQ Newbie
 
Registered: Feb 2009
Posts: 22

Original Poster
Rep: Reputation: 0
I have to write c program for it because its my project so i cant use iptables for it.if anybody knows any good tutorial plz forward me the link.
 
Old 04-07-2009, 08:31 AM   #9
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Anybody who needs to do port forwarding will have used iptables. If you need to do so, and you cannot use iptables, then the best reference would be the source code for iptables. A distant second best would be the source code for netfilter.
Having looked at your original question, I'm not even sure you can do what you are asking from a userspace application. When you say 'i want to send the complete packet that i recieved through internet on port 80', what exactly do you mean? Do you have multiple interfaces, and you want to send the packets to another host that is listening on port 4000? Do you want the packet to be sent to localhost:4000? Can you be more specific?
--- rod.
 
Old 04-08-2009, 05:51 AM   #10
route
LQ Newbie
 
Registered: Feb 2009
Posts: 22

Original Poster
Rep: Reputation: 0
'i want to send the complete packet that i recieved through internet on port 80'.By this i mean i want to transfer the packet as it is without modifying its source/dest ip and port.Yes i do have multiple NICs on my pc.Actually what i have to do is i have to scan an incoming packet from any interface using pcap library.based on the destination of the packet i have to forward it to the corresponding interface.for ex-i have two NIC eth0 connected to internet and eth1 to local network.A packet comes from local network for internet so pcap will scan this packet and forward it to eth0.I know it is possible with iptables but i have to do it using my own program.
so any body has any idea how to forward packet using sockets or pcap library from an interface to a specific port.
 
Old 04-08-2009, 08:54 AM   #11
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
So, then if your host receives a packet that is addressed to it, and then re-transmits it without changing the destination address, it will simply be received once again by the same host. This cycle will then continue ad infinitum. If you do not modify the packet, how will the new destination port be encoded into the packet? It is not possible to do what you are suggesting, even with iptables. The netfilter module provides something called a mangle table, and its purpose is to modify the packet according to the requirements, such as port-forwarding.
I think you need to re-think your objectives. Is this homework?
--- rod.
 
Old 04-09-2009, 04:36 AM   #12
route
LQ Newbie
 
Registered: Feb 2009
Posts: 22

Original Poster
Rep: Reputation: 0
thats ok.But there will be a function for handling packets whose destination and host address are same.So that it will not go in infinite loop.in simple i just have to forward packets between two ports on same machine using c program.The destination port will be decided by the information extracted from packet header using libpcap library.
so please if anybody knows how to forward packets using c program between two ports please let me know.reply asap.
 
Old 04-09-2009, 08:25 AM   #13
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Perhaps Portfwd - Portfwd Forwarding Daemon is a good starting point.
--- rod.
 
Old 04-10-2009, 03:14 AM   #14
route
LQ Newbie
 
Registered: Feb 2009
Posts: 22

Original Poster
Rep: Reputation: 0
thanks for reply we try to forward with that
 
  


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
Send packets to yourself, out one interface and in another algol68 Linux - Networking 3 12-22-2008 02:02 PM
Send Packets Out even if Link Down? htlin Linux - Networking 1 09-22-2005 08:48 PM
Send packets manually Ephracis Programming 4 04-02-2005 04:56 AM
How do you send packets in ASM? Qwirt Programming 4 11-13-2004 03:11 PM
recieve and send IP packets! Farhang Linux - Networking 1 07-25-2004 02:47 PM

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

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