linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Socket listening listening on both interfaces
@ 2004-02-11 12:25 syedk
  2004-02-11 18:15 ` Glynn Clements
  0 siblings, 1 reply; 4+ messages in thread
From: syedk @ 2004-02-11 12:25 UTC (permalink / raw)
  To: linux-c-programming

Hi,
   i wrote a utility to capture packets on an interface using PF_PACKET
socket(on 2.4 kernel)

  int sock=socket(PF_PACKET,SOCK_RAW,htons(ETH_P_IP);

then i used ioctl to set the interface name for the socket:

   struct ifreq ifinfo;
   dev_name="eth1";
   strncpy(ifinfo.ifr_name,dev_name,strlen(dev_name));

  ioctl(sock,SIOCGIFFLAGS,&ifinfo);

But strangely i am able see packets from both eth0 and eth1 interfaces
present on my machine.Initially when i used eth0 it was ok.later when i
switched to eth1 then the socket is sniffing packets from both
interfaces.
How can the raw socket sniff packets from both eth1 and eth0 at the same
time even after i set interface name as either eth0 or eth1.
do i need to set somethings back to their original state before the
program terminates?

thanx in advance and regards
Syed SK



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Socket listening listening on both interfaces
  2004-02-11 12:25 Socket listening listening on both interfaces syedk
@ 2004-02-11 18:15 ` Glynn Clements
  2004-02-12  6:33   ` syedk
  0 siblings, 1 reply; 4+ messages in thread
From: Glynn Clements @ 2004-02-11 18:15 UTC (permalink / raw)
  To: syedk; +Cc: linux-c-programming


syedk wrote:

>    i wrote a utility to capture packets on an interface using PF_PACKET
> socket(on 2.4 kernel)
> 
>   int sock=socket(PF_PACKET,SOCK_RAW,htons(ETH_P_IP);
> 
> then i used ioctl to set the interface name for the socket:
> 
>    struct ifreq ifinfo;
>    dev_name="eth1";
>    strncpy(ifinfo.ifr_name,dev_name,strlen(dev_name));
> 
>   ioctl(sock,SIOCGIFFLAGS,&ifinfo);

Huh? SIOCGIFFLAGS retrieves the flags for a specific interface, in
this case eth1.

> But strangely i am able see packets from both eth0 and eth1 interfaces
> present on my machine.Initially when i used eth0 it was ok.later when i
> switched to eth1 then the socket is sniffing packets from both
> interfaces.
> How can the raw socket sniff packets from both eth1 and eth0 at the same
> time even after i set interface name as either eth0 or eth1.
> do i need to set somethings back to their original state before the
> program terminates?

It sounds as if you want:

	char name[] = "eth1";
	setsockopt(sock SOL_SOCKET, SO_BINDTODEVICE, name, sizeof(name));

-- 
Glynn Clements <glynn.clements@virgin.net>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Socket listening listening on both interfaces
  2004-02-11 18:15 ` Glynn Clements
@ 2004-02-12  6:33   ` syedk
  2004-02-12 22:38     ` Glynn Clements
  0 siblings, 1 reply; 4+ messages in thread
From: syedk @ 2004-02-12  6:33 UTC (permalink / raw)
  To: Glynn Clements; +Cc: linux-c-programming

my question is does'nt the ioctl call:
ioctl(sock,SIOCGIFFLAGS,&ifinfo);
bind the socket to eth1?
if it does'nt do  i need  to bind it using setsockopt?

thanks
syedk

On Wed, 2004-02-11 at 23:45, Glynn Clements wrote:
> 
> syedk wrote:
> 
> >    i wrote a utility to capture packets on an interface using PF_PACKET
> > socket(on 2.4 kernel)
> > 
> >   int sock=socket(PF_PACKET,SOCK_RAW,htons(ETH_P_IP);
> > 
> > then i used ioctl to set the interface name for the socket:
> > 
> >    struct ifreq ifinfo;
> >    dev_name="eth1";
> >    strncpy(ifinfo.ifr_name,dev_name,strlen(dev_name));
> > 
> >   ioctl(sock,SIOCGIFFLAGS,&ifinfo);
> 
> Huh? SIOCGIFFLAGS retrieves the flags for a specific interface, in
> this case eth1.
> 
> > But strangely i am able see packets from both eth0 and eth1 interfaces
> > present on my machine.Initially when i used eth0 it was ok.later when i
> > switched to eth1 then the socket is sniffing packets from both
> > interfaces.
> > How can the raw socket sniff packets from both eth1 and eth0 at the same
> > time even after i set interface name as either eth0 or eth1.
> > do i need to set somethings back to their original state before the
> > program terminates?
> 
> It sounds as if you want:
> 
> 	char name[] = "eth1";
> 	setsockopt(sock SOL_SOCKET, SO_BINDTODEVICE, name, sizeof(name));
> 
> -- 
> Glynn Clements <glynn.clements@virgin.net>



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Socket listening listening on both interfaces
  2004-02-12  6:33   ` syedk
@ 2004-02-12 22:38     ` Glynn Clements
  0 siblings, 0 replies; 4+ messages in thread
From: Glynn Clements @ 2004-02-12 22:38 UTC (permalink / raw)
  To: syedk; +Cc: linux-c-programming


syedk wrote:

> my question is does'nt the ioctl call:
> ioctl(sock,SIOCGIFFLAGS,&ifinfo);
> bind the socket to eth1?

No; it simply queries the flags for eth1.

> if it does'nt do  i need  to bind it using setsockopt?

Yes.

-- 
Glynn Clements <glynn.clements@virgin.net>

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2004-02-12 22:38 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-02-11 12:25 Socket listening listening on both interfaces syedk
2004-02-11 18:15 ` Glynn Clements
2004-02-12  6:33   ` syedk
2004-02-12 22:38     ` Glynn Clements

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).