1 /* $Id: getifaddr.c,v 1.9 2008/10/15 10:16:28 nanard Exp $ */
3 * http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/
4 * (c) 2006-2008 Thomas Bernard
5 * This software is subject to the conditions detailed
6 * in the LICENCE file provided within the distribution */
11 #include <sys/ioctl.h>
12 #include <sys/types.h>
13 #include <sys/socket.h>
15 #include <arpa/inet.h>
16 #include <netinet/in.h>
18 #include <sys/sockio.h>
21 #include "getifaddr.h"
24 getifaddr(const char * ifname
, char * buf
, int len
)
26 /* SIOCGIFADDR struct ifreq * */
30 struct sockaddr_in
* addr
;
32 if(!ifname
|| ifname
[0]=='\0')
34 s
= socket(PF_INET
, SOCK_DGRAM
, 0);
37 syslog(LOG_ERR
, "socket(PF_INET, SOCK_DGRAM): %m");
40 strncpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
);
41 if(ioctl(s
, SIOCGIFADDR
, &ifr
, &ifrlen
) < 0)
43 syslog(LOG_ERR
, "ioctl(s, SIOCGIFADDR, ...): %m");
47 addr
= (struct sockaddr_in
*)&ifr
.ifr_addr
;
48 if(!inet_ntop(AF_INET
, &addr
->sin_addr
, buf
, len
))
50 syslog(LOG_ERR
, "inet_ntop(): %m");