LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 01-27-2005, 12:04 PM   #1
bassdemon
LQ Newbie
 
Registered: Sep 2004
Location: USA
Distribution: gentoo
Posts: 16

Rep: Reputation: 0
raw packets


Hello everyone. I have a networking question. It's been a while since i've had to send packets in user space. But what I want to do is send raw packets. completely raw. I've built all the headers from the ethernet layer up. Everything. checksums etc. All I need to do is put it on the wire. How can I do this? Every send function seems to require sockaddr structs with destinations or requires a connection. I just want to put the data on the wire and let the switches/routers handle it. I can do this in the kernel no problem...but how the heck do i do it in user space? Thanks.

bassdemon
 
Old 01-27-2005, 12:32 PM   #2
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 118Reputation: 118
This may or may not help: http://unixhelp.ed.ac.uk/CGI/man-cgi?packet+7
 
Old 01-27-2005, 12:36 PM   #3
bassdemon
LQ Newbie
 
Registered: Sep 2004
Location: USA
Distribution: gentoo
Posts: 16

Original Poster
Rep: Reputation: 0
hey matir. thanks for the response. Yeah i've actually looked at that. It's what I'm trying to use right now...but how exactly do I send packets through it. I would assume i'd need to use either send,sendto,or sendmsg. send needs a connection...and sendto/sendmsg need destination data which i'd rather not have to create.

bassdemon
 
Old 01-27-2005, 03:56 PM   #4
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 118Reputation: 118
Sorry I can't help any more... I even suck at normal network work... wish I could find a REALLY good tutorial for it, but I haven't yet... oh well.
 
Old 01-27-2005, 04:31 PM   #5
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,536

Rep: Reputation: 148Reputation: 148
sendmsg/recvmsg are the right functions.
You need to have destination, without it the packet will not reach itd destination.
 
Old 01-27-2005, 05:17 PM   #6
bassdemon
LQ Newbie
 
Registered: Sep 2004
Location: USA
Distribution: gentoo
Posts: 16

Original Poster
Rep: Reputation: 0
ok so i'll check into sendmsg some more...but i'm really looking for something that can send packets on particular device. If i create the packets properly and send them on the proper device then I shouldn't need to supply the destination. The router will handle that. Basically I'm just messing with packet forwarding. I want to receive packets on a device (which i'm doing with pcap) and then change the packets some and forward them back on the same device. Hence why the destination isn't important to me and will be a small headache to extract and use in sendmsg or sendto. is there any send over device functions?

bassdemon
 
Old 01-27-2005, 11:08 PM   #7
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,499

Rep: Reputation: 355Reputation: 355Reputation: 355Reputation: 355
This should help you:
http://www.packetfactory.net/projects/libnet/
 
Old 01-28-2005, 12:23 AM   #8
bassdemon
LQ Newbie
 
Registered: Sep 2004
Location: USA
Distribution: gentoo
Posts: 16

Original Poster
Rep: Reputation: 0
ok well thanks for all the help everyone. after some more googling i was able to piece together this code that works just fine for putting raw packets on the wire. I posted the sample in case anyone else was curious how to do it. thanks again

bassdemon

Code:
#include <sys/socket.h>
#include <netpacket/packet.h>
#include <net/ethernet.h>
#include <netinet/in.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <iostream>

int main(int argc, char * argv[])
{
        //create a raw packet_socket
        int ps = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
        if(ps==-1)
        {
                perror("packet_socket: ");
                exit(1);
        }
        unsigned char buffer[100];
        //sockaddr info
        sockaddr_ll socket_address;
        socket_address.sll_family   = PF_PACKET;
        socket_address.sll_protocol = htons(ETH_P_IP);  
        socket_address.sll_hatype   = 1;           
        socket_address.sll_pkttype  = PACKET_OTHERHOST;
        socket_address.sll_halen    = ETH_ALEN;
        //interface info
        ifreq ifr;
        strncpy(ifr.ifr_name, argv[1], IFNAMSIZ);
        if (ioctl(ps, SIOCGIFINDEX, &ifr) == -1) {
                perror("SIOCGIFINDEX");
                exit(1);
        }
        //set interface
        socket_address.sll_ifindex=ifr.ifr_ifindex;
        //fill in junk data 
        for(int counter=0;counter<100;counter++)
        {
                buffer[counter]=counter;
        }
        //send it
        int send_result = sendto(ps, buffer, 100, 0, (struct sockaddr*)&socket_address, sizeof(socket_address));
        return(0);
}

Last edited by bassdemon; 01-28-2005 at 12:56 AM.
 
  


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
How to Capture Raw Packets (no Decode) with PCAP kidskc Programming 1 11-02-2005 04:54 PM
packets sent VS packets received fsasya Linux - Networking 0 07-18-2004 07:11 PM
encapsulating TCP packets in UDP packets... yoshi95 Programming 3 06-03-2004 02:53 PM
RAW packets not permitted? (mandrake 9.1) core Linux - Networking 2 08-16-2003 07:48 AM
raw cd copy hjles Slackware 2 05-02-2003 08:47 PM


All times are GMT -5. The time now is 01:26 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration