Network Configuration after BOOTP

Joel Sherrill <joel@OARcorp.com> joel.sherrill at OARcorp.com
Wed Nov 24 14:19:22 UTC 2004


I turned this into the Wiki entry:

http://www.rtems.com/phpwiki/index.php/Obtaining%20Interface%20Information

Thomas Rauscher wrote:
>   
> 
> 
>>-----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!
>  
> 


-- 
Joel Sherrill, Ph.D.             Director of Research & Development
joel at OARcorp.com                 On-Line Applications Research
Ask me about RTEMS: a free RTOS  Huntsville AL 35805
    Support Available             (256) 722-9985




More information about the users mailing list