/* $Id$ */ /* MiniUPnP project * http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/ * (c) 2006 Thomas Bernard * This software is subject to the conditions detailed * in the LICENCE file provided within the distribution */ #include #include #include #include #include #include #include #include #include #include #include #include #if defined(sun) #include #endif #include "getifaddr.h" int getifaddr(const char * ifname, char * buf, int len) { /* SIOCGIFADDR struct ifreq * */ int s; struct ifreq ifr; int ifrlen; struct sockaddr_in * addr; ifrlen = sizeof(ifr); s = socket(PF_INET, SOCK_DGRAM, 0); if(s < 0) { syslog(LOG_ERR, "socket(PF_INET, SOCK_DGRAM): %m"); return -1; } strncpy(ifr.ifr_name, ifname, IFNAMSIZ); if(ioctl(s, SIOCGIFADDR, &ifr, &ifrlen) < 0) { syslog(LOG_ERR, "ioctl(s, SIOCGIFADDR, ...): %m"); close(s); return -1; } addr = (struct sockaddr_in *)&ifr.ifr_addr; if(!inet_ntop(AF_INET, &addr->sin_addr, buf, len)) { syslog(LOG_ERR, "inet_ntop(): %m"); close(s); return -1; } close(s); return 0; } int getsysaddr(char * buf, int len) { char hn[256]; struct in_addr *addr; struct hostent *host; memset(buf, '\0', len); gethostname(hn, sizeof(hn)); host = gethostbyname(hn); if( !host ) return -1; addr = (struct in_addr*)host->h_addr; strncpy(buf, inet_ntoa(*addr), len); return 0; } int getifhwaddr(const char * ifname, char * buf, int len) { /* SIOCGIFADDR struct ifreq * */ int s; struct ifreq ifr; int ifrlen; unsigned char addr[6]; char mac_string[4]; int i; ifrlen = sizeof(ifr); if( len < 12 ) { return -2; } s = socket(AF_INET, SOCK_DGRAM, 0); if(s < 0) { syslog(LOG_ERR, "socket(PF_INET, SOCK_DGRAM): %m"); return -1; } strncpy(ifr.ifr_name, ifname, IFNAMSIZ); if(ioctl(s, SIOCGIFHWADDR, &ifr, &ifrlen) < 0) { syslog(LOG_ERR, "ioctl(s, SIOCGIFHWADDR, ...): %m"); close(s); return -1; } close(s); memmove( addr, ifr.ifr_hwaddr.sa_data, 6); for (i=0; i<6; ++i) { sprintf(mac_string, "%2.2x", addr[i]); strcat(buf, mac_string); } return 0; }