lwip-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: RE : [lwip-users] Source MAC address


From: Jonathan Larmour
Subject: Re: RE : [lwip-users] Source MAC address
Date: Tue, 18 Dec 2007 15:42:13 +0000
User-agent: Thunderbird 1.5.0.12 (X11/20070530)

Nicolas Pinault wrote:
> Hi Per,
> 
> Thank's for your answer but that is not what I need. In my case, I need
> to know the MAC address of the sender.

It's a bit unofficial, but for ethernet, assuming PBUF_RAM or PBUF_POOL
pbufs, you should be able to look at the start of the pbuf (of the first
pbuf in the packet chain). The payload pointer in that first pbuf which you
will have been given will have skipped over the IP and UDP headers. You
could use something like the following. Note that at this point it's hard
to work out whether the IP header had options or not, so a little guesswork
is required. Something like the following (untested) code:

#include "lwip/pbuf.h"
#include "netif/etharp.h"
#include "lwip/ip.h"
#include "lwip/udp.h"


const u8_t *p = pbuf->payload;
const u8_t *udp_hdr, *ip_hdr;
const struct eth_hdr *eth_hdr;
struct eth_addr src_eth_mac;

udp_hdr = p - UDP_HLEN;
ip_hdr = udp_hdr - IP_HLEN;
/* Does it have options that make the header longer?
 * Could also check header checksum;
 */
while ( (IPH_V(ip_hdr) != 4) || (ip_hdr + IPH_HL(ip_hdr) != udp_hdr) )
{
  ip_hdr--;
  if (ip_hdr <= (u8_t*)pbuf+sizeof(struct pbuf))
    return SOME_ERR; /* reached start of pbuf without finding header */
}
eth_hdr = (const struct eth_hdr*)(ip_hdr - sizeof(struct eth_hdr));
src_eth_mac = eth_hdr->src;


An alternative would be to jump directly to:
eth_hdr = (const struct eth_hdr*)(pbuf+sizeof(pbuf));

But I feel a bit more uneasy about that.

Hope this helps,

Jifl
-- 
eCosCentric Limited      http://www.eCosCentric.com/     The eCos experts
Barnwell House, Barnwell Drive, Cambridge, UK.       Tel: +44 1223 245571
Registered in England and Wales: Reg No 4422071.
------["Si fractum non sit, noli id reficere"]------       Opinions==mine




reply via email to

[Prev in Thread] Current Thread [Next in Thread]