public inbox for ecos-discuss@sourceware.org
 help / color / mirror / Atom feed
* [ECOS] Problem reading broadcast IP
@ 2008-01-18  9:01 Emmanuel Coullien
  2008-01-18  9:32 ` Andrew Lunn
  0 siblings, 1 reply; 6+ messages in thread
From: Emmanuel Coullien @ 2008-01-18  9:01 UTC (permalink / raw)
  To: ecos-discuss

Hi,

I' am trying to get broadcast frame from Ethernet with the following code :

void Test_system_UDP2(void)
{
    int n_read;
    struct timeval tv;
    int srecept;
    struct sockaddr_in srv_recept;
    int FrameErrCountSocket1=0;
    int DataErrCountSocket1=0;
	int RecvError=0;
    int ErrSendSocket=0;
	int CountSocket1=0;
    int i;
    unsigned char j;
    int StartCycle =1;
    /* for select on socket */
    int ret;
    fd_set file_set;

	int FlgON = 1;   // Utiliser pour activer des Options dans SetSockOpt
	
    /*** Init de la socket de reception ***/
    bzero(&srv_recept, sizeof(srv_recept));
    srv_recept.sin_family = AF_INET;
    srv_recept.sin_port = htons(PORT_TO_SEND);
//    srv_recept.sin_addr.s_addr=INADDR_ANY;
    srv_recept.sin_addr.s_addr=inet_addr("192.168.1.3");

    if ((srecept = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
    {
        perror("socket");
        return;
    }
    // positionner le socket en mode BROADCAST
    if (setsockopt(srecept, SOL_SOCKET, SO_BROADCAST, &FlgON, sizeof(FlgON)))
    {
        perror(" : setsockopt");
        return;
    }

    if (bind(srecept, (struct sockaddr *)&srv_recept, sizeof(srv_recept)) < 0)
    {
        printf("Erreur :bind");
    }

    tv.tv_sec = TIMEOUT_RECV;
    tv.tv_usec = 0;
    while (1)
    {
            FD_ZERO (&file_set);
            FD_SET (srecept, &file_set);
            ret = select (srecept+1, &file_set, NULL, NULL, &tv);
            if (ret >0)
            {
                /* we have data in the socket */
                n_read = recvfrom(srecept, Bufrecept, MAXBUF, 0,NULL, NULL);
                if (n_read < 0)
                     printf("Erreur lecture socket : n_read +%d\n",n_read);
                else
                {   Bufrecept[n_read]='\0';
                    printf(" Reception OK (%s)\n",Bufrecept);
                 }
            }
            else
            {
                printf("Erreur Timeout : ret +%d\n",ret);
            }
    }
}

If I use "srv_recept.sin_addr.s_addr=INADDR_ANY;", then it works fine,
I receive my broadcast frame.
But if I use  "srv_recept.sin_addr.s_addr=inet_addr("192.168.1.3");"
even with  "setsockopt(srecept, SOL_SOCKET, SO_BROADCAST, &FlgON,
sizeof(FlgON)" ,
then on eCos I can't receive any broadcast.
I need to use inet_addr("192.168.1.3") to select this interface
because I have 3 Ethernet interface and I don't want to receive from
the other interface.

I tried on a PC and I haven't any problem with
srv_recept.sin_addr.s_addr=inet_addr("192.168.1.3");. I received the
broadcast frame.


Does someone know why I didn't received the broadcast frame in that
case on eCos ?
Is there something I missed or an other socket option to set ?

-- 
Emmanuel Coullien

-- 
Before posting, please read the FAQ: http://ecos.sourceware.org/fom/ecos
and search the list archive: http://ecos.sourceware.org/ml/ecos-discuss

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

* Re: [ECOS] Problem reading broadcast IP
  2008-01-18  9:01 [ECOS] Problem reading broadcast IP Emmanuel Coullien
@ 2008-01-18  9:32 ` Andrew Lunn
  2008-01-18  9:50   ` Emmanuel Coullien
  0 siblings, 1 reply; 6+ messages in thread
From: Andrew Lunn @ 2008-01-18  9:32 UTC (permalink / raw)
  To: Emmanuel Coullien; +Cc: ecos-discuss

On Fri, Jan 18, 2008 at 10:00:56AM +0100, Emmanuel Coullien wrote:
> Hi,
> 
> I' am trying to get broadcast frame from Ethernet with the following code :
> 
> void Test_system_UDP2(void)
> {
>     int n_read;
>     struct timeval tv;
>     int srecept;
>     struct sockaddr_in srv_recept;
>     int FrameErrCountSocket1=0;
>     int DataErrCountSocket1=0;
> 	int RecvError=0;
>     int ErrSendSocket=0;
> 	int CountSocket1=0;
>     int i;
>     unsigned char j;
>     int StartCycle =1;
>     /* for select on socket */
>     int ret;
>     fd_set file_set;
> 
> 	int FlgON = 1;   // Utiliser pour activer des Options dans SetSockOpt
> 	
>     /*** Init de la socket de reception ***/
>     bzero(&srv_recept, sizeof(srv_recept));
>     srv_recept.sin_family = AF_INET;
>     srv_recept.sin_port = htons(PORT_TO_SEND);
> //    srv_recept.sin_addr.s_addr=INADDR_ANY;
>     srv_recept.sin_addr.s_addr=inet_addr("192.168.1.3");

Just a guess, but try binding to the broadcast address on that
interface, not the interface address itself.

> I tried on a PC and I haven't any problem with
> srv_recept.sin_addr.s_addr=inet_addr("192.168.1.3");. I received the
> broadcast frame.

A PC runing FreeBSD? 

  Andrew

-- 
Before posting, please read the FAQ: http://ecos.sourceware.org/fom/ecos
and search the list archive: http://ecos.sourceware.org/ml/ecos-discuss

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

* Re: [ECOS] Problem reading broadcast IP
  2008-01-18  9:32 ` Andrew Lunn
@ 2008-01-18  9:50   ` Emmanuel Coullien
  2008-01-18 11:48     ` Andrew Lunn
  0 siblings, 1 reply; 6+ messages in thread
From: Emmanuel Coullien @ 2008-01-18  9:50 UTC (permalink / raw)
  To: Andrew Lunn; +Cc: ecos-discuss

Ok for binding broadcast adress but how do you specify the interface
(ex: eth0) ?

thank you,

2008/1/18, Andrew Lunn <andrew@lunn.ch>:
> On Fri, Jan 18, 2008 at 10:00:56AM +0100, Emmanuel Coullien wrote:
> > Hi,
> >
> > I' am trying to get broadcast frame from Ethernet with the following code :
> >
> > void Test_system_UDP2(void)
> > {
> >     int n_read;
> >     struct timeval tv;
> >     int srecept;
> >     struct sockaddr_in srv_recept;
> >     int FrameErrCountSocket1=0;
> >     int DataErrCountSocket1=0;
> >       int RecvError=0;
> >     int ErrSendSocket=0;
> >       int CountSocket1=0;
> >     int i;
> >     unsigned char j;
> >     int StartCycle =1;
> >     /* for select on socket */
> >     int ret;
> >     fd_set file_set;
> >
> >       int FlgON = 1;   // Utiliser pour activer des Options dans SetSockOpt
> >
> >     /*** Init de la socket de reception ***/
> >     bzero(&srv_recept, sizeof(srv_recept));
> >     srv_recept.sin_family = AF_INET;
> >     srv_recept.sin_port = htons(PORT_TO_SEND);
> > //    srv_recept.sin_addr.s_addr=INADDR_ANY;
> >     srv_recept.sin_addr.s_addr=inet_addr("192.168.1.3");
>
> Just a guess, but try binding to the broadcast address on that
> interface, not the interface address itself.
>
> > I tried on a PC and I haven't any problem with
> > srv_recept.sin_addr.s_addr=inet_addr("192.168.1.3");. I received the
> > broadcast frame.
>
> A PC runing FreeBSD?
>
>   Andrew
>


-- 
Emmanuel Coullien

-- 
Before posting, please read the FAQ: http://ecos.sourceware.org/fom/ecos
and search the list archive: http://ecos.sourceware.org/ml/ecos-discuss

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

* Re: [ECOS] Problem reading broadcast IP
  2008-01-18  9:50   ` Emmanuel Coullien
@ 2008-01-18 11:48     ` Andrew Lunn
  2008-01-18 12:36       ` Emmanuel Coullien
  0 siblings, 1 reply; 6+ messages in thread
From: Andrew Lunn @ 2008-01-18 11:48 UTC (permalink / raw)
  To: Emmanuel Coullien; +Cc: ecos-discuss

On Fri, Jan 18, 2008 at 10:49:59AM +0100, Emmanuel Coullien wrote:
> Ok for binding broadcast adress but how do you specify the interface
> (ex: eth0) ?

Take for example:

eth0      Link encap:Ethernet  HWaddr 00:60:97:b0:86:6e
          inet addr:192.168.9.3  Bcast:192.168.9.255  Mask:255.255.255.0

The IP broadcast address is 192.168.9.255. Try binding to that.  Only
one interface will have this address, so it implicitly specifies eth0.

In my limited experience, getting broadcasting to work in a portable
way is a bit of a black art.

  Andrew

-- 
Before posting, please read the FAQ: http://ecos.sourceware.org/fom/ecos
and search the list archive: http://ecos.sourceware.org/ml/ecos-discuss

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

* Re: [ECOS] Problem reading broadcast IP
  2008-01-18 11:48     ` Andrew Lunn
@ 2008-01-18 12:36       ` Emmanuel Coullien
  2008-01-18 13:03         ` Andrew Lunn
  0 siblings, 1 reply; 6+ messages in thread
From: Emmanuel Coullien @ 2008-01-18 12:36 UTC (permalink / raw)
  To: Andrew Lunn; +Cc: ecos-discuss

I thank you for your answer. For your information, here are what I tested :

srv_recept.sin_addr.s_addr=inet_addr("255.255.255.255"); --> OK,
Broadcast received
srv_recept.sin_addr.s_addr=inet_addr("192.255.255.255"); --> Doesn't
receive any Broadcast
srv_recept.sin_addr.s_addr=inet_addr("192.168.255.255"); --> Doesn't
receive any Broadcast
srv_recept.sin_addr.s_addr=inet_addr("192.168.1.255"); --> Doesn't
receive any Broadcast

It seems to be strange because when I do that on a PC, it works fine,
and then I expected that it will work on eCos too.

2008/1/18, Andrew Lunn <andrew@lunn.ch>:
> On Fri, Jan 18, 2008 at 10:49:59AM +0100, Emmanuel Coullien wrote:
> > Ok for binding broadcast adress but how do you specify the interface
> > (ex: eth0) ?
>
> Take for example:
>
> eth0      Link encap:Ethernet  HWaddr 00:60:97:b0:86:6e
>           inet addr:192.168.9.3  Bcast:192.168.9.255  Mask:255.255.255.0
>
> The IP broadcast address is 192.168.9.255. Try binding to that.  Only
> one interface will have this address, so it implicitly specifies eth0.
>
> In my limited experience, getting broadcasting to work in a portable
> way is a bit of a black art.
>
>   Andrew
>


-- 
Emmanuel Coullien

-- 
Before posting, please read the FAQ: http://ecos.sourceware.org/fom/ecos
and search the list archive: http://ecos.sourceware.org/ml/ecos-discuss

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

* Re: [ECOS] Problem reading broadcast IP
  2008-01-18 12:36       ` Emmanuel Coullien
@ 2008-01-18 13:03         ` Andrew Lunn
  0 siblings, 0 replies; 6+ messages in thread
From: Andrew Lunn @ 2008-01-18 13:03 UTC (permalink / raw)
  To: Emmanuel Coullien; +Cc: ecos-discuss

On Fri, Jan 18, 2008 at 01:36:16PM +0100, Emmanuel Coullien wrote:
> I thank you for your answer. For your information, here are what I tested :
> 
> srv_recept.sin_addr.s_addr=inet_addr("255.255.255.255"); --> OK,
> Broadcast received
> srv_recept.sin_addr.s_addr=inet_addr("192.255.255.255"); --> Doesn't
> receive any Broadcast
> srv_recept.sin_addr.s_addr=inet_addr("192.168.255.255"); --> Doesn't
> receive any Broadcast
> srv_recept.sin_addr.s_addr=inet_addr("192.168.1.255"); --> Doesn't
> receive any Broadcast
> 
> It seems to be strange because when I do that on a PC, it works fine,
> and then I expected that it will work on eCos too.

What OS is the PC running? 

Like i said, broadcast is an interesting subject which different OSs
do differently. eCos uses the FreeBSD TCP/IP stack, so you should look
at documentation from FreeBSD.

   Andrew

-- 
Before posting, please read the FAQ: http://ecos.sourceware.org/fom/ecos
and search the list archive: http://ecos.sourceware.org/ml/ecos-discuss

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

end of thread, other threads:[~2008-01-18 13:03 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-01-18  9:01 [ECOS] Problem reading broadcast IP Emmanuel Coullien
2008-01-18  9:32 ` Andrew Lunn
2008-01-18  9:50   ` Emmanuel Coullien
2008-01-18 11:48     ` Andrew Lunn
2008-01-18 12:36       ` Emmanuel Coullien
2008-01-18 13:03         ` Andrew Lunn

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 read-only IMAP folder(s) and NNTP newsgroup(s).