Network Configuration after BOOTP

Thomas Rauscher trauscher at loytec.com
Wed Nov 24 09:12:48 UTC 2004


  

> -----Original Message-----
> From: Kirspel, Kevin {Engineering - Osmetech} 
> [mailto:kevin.kirspel at osmetech.com] 
> Sent: Tuesday, November 23, 2004 6:23 PM
> To: rtems-users at rtems.com
> Subject: Network Configuration after BOOTP
> 
> What is the best way to obtain my IP address, MAC Address, 
> Network Mask,
> and Default Gateway after the 
> rtems_bsdnet_initialize_network() call?  I
> want to be able to access this information from software.
> 
> 
> Kevin Kirspel
> 
> 
> 


#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <net/if.h>
#include <netinet/in.h>

int get_ifaddr(const char *ifname, struct in_addr *addr)
{
    int fd;
    int rc;
    struct ifreq ifreq;
    struct sockaddr_in *sockaddr;

    fd = socket(AF_INET, SOCK_DGRAM, 0);
    if(fd<0) {
        return -1;
    }

    strcpy(ifreq.ifr_name, ifname);

    rc = ioctl(fd, SIOCGIFADDR, &ifreq);
    if(rc == 0) 
    {
        sockaddr = (struct sockaddr_in *) &ifreq.ifr_ifru.ifru_addr;
        memcpy(addr, &sockaddr->sin_addr, sizeof(*addr));
    }

    close(fd);
    return (rc==0) ? 0 : -1;
}




should do the trick. 'ifname' should contain the name of your interface,
e.g. "eth0", 'addr' then contains the result.


Regards,
Thomas Rauscher


--
Thomas Rauscher
LOYTEC electronics GmbH
Stolzenthalergasse 24/3
A-1080 Wien
Austria/Europe
trauscher at loytec.com
www.loytec.com
Phone: +43-1-4020805-15
FAX:   +43-1-4020805-99

Networks under control!
LOYTEC and NEC Electronics Join Forces in Building Automation!
 




More information about the users mailing list