LinuxQuestions.org
Help answer threads with 0 replies.
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 11-07-2015, 03:54 PM   #1
elbarto333
LQ Newbie
 
Registered: Nov 2015
Posts: 2

Rep: Reputation: Disabled
Post packet raw socket - C sniffer


Hi guys... I make this program in C for sniffing my lan network. I use Raw Packet socket (AF_PACKET) why i need see the layer 2 info of each packet, but the variable "len" return only "-1". Why??? (I use "lo" interface for testing only). This is the code:

#include <stdio.h>
#include <sys/socket.h>
#include <net/ethernet.h>
#include <linux/if_packet.h>
#include <string.h>
#include <net/if.h>
#include <sys/ioctl.h>

main()
{
struct sockaddr_ll sll;
struct ifreq if_idx;

unsigned char *buffer;
int x, len;

int con = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));


memset(&if_idx, 0, sizeof(struct ifreq));
strncpy(if_idx.ifr_name, "lo", IFNAMSIZ-1);
ioctl(con, SIOCGIFINDEX, &if_idx);

memset(&sll, 0, sizeof(sll));
sll.sll_family = AF_PACKET;
sll.sll_ifindex = if_idx.ifr_ifindex;
sll.sll_protocol = htons(ETH_P_ALL);

bind(con, (const struct sockaddr *) &sll, sizeof(sll));

len = read(con, buffer, 4096);
printf("%i\n\n", len);
for (x = 0; x < len; x++)
{
printf("%x ", buffer[x]);
}
}


TANKS for all.

Last edited by elbarto333; 11-07-2015 at 11:59 PM. Reason: solved
 
Old 11-07-2015, 08:05 PM   #2
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
I am ignorant about socket, let alone raw socket programming, but are you sure the socket(), ioctl() and bind() system calls succeed? You never check.

In addition to checking the return value, also look at errno when an error condition is detected. Also for the read() call.

Last edited by berndbausch; 11-07-2015 at 11:47 PM.
 
Old 11-07-2015, 11:58 PM   #3
elbarto333
LQ Newbie
 
Registered: Nov 2015
Posts: 2

Original Poster
Rep: Reputation: Disabled
(SOLVED)

The problem was the not malloc for *buffer pointer, this does the read() error.

Regards.
 
  


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
force kernel 2.6.33.3 to send the packet with MF flag set on through raw socket samiran.linux Linux - Networking 3 08-02-2013 02:08 PM
Receive Ethernet VLAN frame on the native ethernet interface (raw packet socket) scottbiker Linux - Networking 0 07-13-2009 03:49 AM
raw socket/ip packet help shouup Programming 14 04-24-2006 04:54 PM
What is the best for Packet Sniffer alexr186 Linux - Software 5 02-07-2004 08:34 AM
Suggestions for packet sniffer w/ packet viewing? TruckStuff Linux - Networking 5 05-31-2002 09:50 AM

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

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