Changes between Version 1 and Version 2 of TBR/UserManual/Obtaining_Interface_Information


Ignore:
Timestamp:
Apr 14, 2008, 5:17:45 PM (13 years ago)
Author:
Vdcappel
Comment:

Added code to obtain MAC address

Legend:

Unmodified
Added
Removed
Modified
  • TBR/UserManual/Obtaining_Interface_Information

    v1 v2  
    22
    33
    4 This is based on an answer posted to the RTEMS User's mailing list by [wiki:ThomasRauscher ThomasRauscher] http://www.rtems.com/ml/rtems-users/2004/november/msg00255.html.
     4This is based on an answer posted to the RTEMS User's mailing list by [wiki:ThomasRauscher ThomasRauscher] http://www.rtems.com/ml/rtems-users/2004/november/msg00255.html, updated with an answer posted by [wiki:User:Vdcappel Arnout Vandecappelle]
    55
    66
     
    99
    1010
    11 '''Answer:'''  This should do the trick. 'ifname' should contain the name of your interface, e.g. "eth0", 'addr' then contains the result.
     11'''Answer:'''  The following code retrieves the IP address for an interface. 'ifname' should contain the name of your interface, e.g. "eth0", 'addr' then contains the result.  Replace _SIOCGIFADDR_ by _SIOCGIFBRDADDR_ for the broadcast address, _SIOCGIFNETMASK_ for the net mask, and _SIOCGIFDSTADDR_ for the other side of a point-to-point link.
    1212
    1313
     
    4444}
    4545}}}
     46
     47Finding the MAC address is a little more complex.  The code below does the trick. 'ifname' should contain the name of your interface, e.g. "eth0", 'addr' then contains the result. 'addr' should be pre-allocated and large enough to store the Ethernet address (i.e. 6 bytes).
     48
     49Note that it is necessary to hard-code the number 18 because the AF_LINK definition is not visible outside of the bsdnet kernel.
     50
     51{{{
     52#include <sys/socket.h>  /* For sockaddr */
     53#include <net/if.h>      /* For struct ifreq, required by SIO* */
     54#include <net/if_types.h> /* For IFT_ETHER */
     55#include <net/if_dl.h>    /* For sockaddr_dl */
     56#include <sys/sockio.h>  /* For SIO* */
     57
     58int get_macaddr(const char *ifname, char *addr)
     59{
     60  int i;
     61  char buf[1024];
     62  int sock;
     63  struct ifconf ifc;
     64  struct ifreq *ifr, *lifr;
     65  struct sockaddr_dl *sdl;
     66
     67  ifc.ifc_buf = buf;
     68  ifc.ifc_len = sizeof(buf);
     69
     70  if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
     71    return sock;
     72  }
     73  i =  ioctl(sock, SIOCGIFCONF, (char *)&ifc);
     74  close(sock);
     75  if (i < 0) {
     76    return i;
     77  }
     78
     79  ifr = ifc.ifc_req;
     80  lifr = (struct ifreq *)&ifc.ifc_buf[ifc.ifc_len];
     81
     82  while (ifr < lifr) {
     83    struct sockaddr *sa;
     84    sa = &ifr->ifr_addr;
     85    if (!strcmp(ifr->ifr_name, ifname)) {
     86      sdl = (struct sockaddr_dl *)sa;
     87      if (sdl->sdl_family == 18) { /* AF_LINK is made invisiable in socket.h */
     88        memcpy (addr, LLADDR(sdl), sdl->sdl_alen);
     89        return sdl->sdl_alen;
     90      }
     91    }
     92    if (sa->sa_len < sizeof(*sa))
     93      ifr = (struct ifreq *)(((char *)sa) + sizeof(*sa));
     94    else
     95      ifr = (struct ifreq *)(((char *)sa) + sa->sa_len);
     96  }
     97  return -1;
     98}
     99}}}
     100
     101For the default gateway, no simple solution is found yet.