Unix Programming - broadcast/multicast addresses

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > November 2005 > broadcast/multicast addresses





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author broadcast/multicast addresses
Roman Mashak

2005-11-28, 2:52 am

Hello, All!

What is the guaranteed method to detect multicast and broadcast addresses?
By now I ended up with the following:

#include <netinet/if_ether.h>

#define IS_BROADCAST(addr) ((addr->h_dest[0] & 0xFF) == 0xFF \
&& (addr->h_dest[1]
& 0xFF) == 0xFF \
&& (addr->h_dest[2]
& 0xFF) == 0xFF \
&& (addr->h_dest[3]
& 0xFF) == 0xFF \
&& (addr->h_dest[4]
& 0xFF) == 0xFF \
&& (addr->h_dest[5]
& 0xFF) == 0xFF)
#define IS_MULTICAST(addr) (addr->h_dest[0] & 0x01)

struct ethhdr *ether;
....
if ( IS_BROADCAST(ether) ) ...

Two points I worry about :
1) apparently these macros are not quite effective and optimized(?)
2) IS_MULTICAST will also select broadcast address which isn't desirable. On
the other hand, one can consider multicast as a special case for broadcast,
right?

With best regards, Roman Mashak. E-mail: mrv@tusur.ru


Robert Harris

2005-11-28, 7:50 am

Roman Mashak wrote:
> Hello, All!
>
> What is the guaranteed method to detect multicast and broadcast addresses?
> By now I ended up with the following:
>
> #include <netinet/if_ether.h>
>
> #define IS_BROADCAST(addr) ((addr->h_dest[0] & 0xFF) == 0xFF \
> && (addr->h_dest[1]
> & 0xFF) == 0xFF \
> && (addr->h_dest[2]
> & 0xFF) == 0xFF \
> && (addr->h_dest[3]
> & 0xFF) == 0xFF \
> && (addr->h_dest[4]
> & 0xFF) == 0xFF \
> && (addr->h_dest[5]
> & 0xFF) == 0xFF)
> #define IS_MULTICAST(addr) (addr->h_dest[0] & 0x01)
>
> struct ethhdr *ether;
> ...
> if ( IS_BROADCAST(ether) ) ...
>
> Two points I worry about :
> 1) apparently these macros are not quite effective and optimized(?)

Optimisation is the compiler's job. But it might help a little if you
define something like:

const unsigned char broadcast_addr[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF};
#define IS_BROADCAST(addr) (!memcmp(addr->h_dest, broadcast_addr, 6))

> 2) IS_MULTICAST will also select broadcast address which isn't desirable. On
> the other hand, one can consider multicast as a special case for broadcast,
> right?

#define IS_MULTICAST(addr) ((addr->h_dest[0] & 0x01) && !IS_BROADCAST(addr))

if you don't always test for broadcast addresses first.
>
> With best regards, Roman Mashak. E-mail: mrv@tusur.ru
>
>

Måns Rullgård

2005-11-28, 6:04 pm

Robert Harris <robert.f.harris@blueyonder.co.uk> writes:

> Optimisation is the compiler's job. But it might help a little if you
> define something like:
>
> const unsigned char broadcast_addr[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
> 0xFF};
> #define IS_BROADCAST(addr) (!memcmp(addr->h_dest, broadcast_addr, 6))


Or just use the macros in netinet/in.h:
#define IN_MULTICAST(a) ((((in_addr_t)(a)) & 0xf0000000) == 0xe0000000)
#define INADDR_BROADCAST ((in_addr_t) 0xffffffff)

--
Måns Rullgård
mru@inprovide.com
Roman Mashak

2005-11-29, 2:50 am

Hello, Robert!
You wrote on Mon, 28 Nov 2005 10:55:02 GMT:

RH> Optimisation is the compiler's job. But it might help a little if you
RH> define something like:

RH> const unsigned char broadcast_addr[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
RH> 0xFF};
RH> #define IS_BROADCAST(addr) (!memcmp(addr->h_dest, broadcast_addr, 6))
Thanks a lot, really very nice solution.

With best regards, Roman Mashak. E-mail: mrv@tusur.ru


Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com