1
/* $Id: xp_get_iface_addr.cc,v 1.2 2003/04/22 20:00:06 fygrave Exp $ */
3
** Copyright (C) 2001 Fyodor Yarochkin <fygrave@tigerteam.net>,
4
** Ofir Arkin <ofir@sys-security.com>
6
** This program is free software; you can redistribute it and/or modify
7
** it under the terms of the GNU General Public License as published by
8
** the Free Software Foundation; either version 2 of the License, or
9
** (at your option) any later version.
12
** This program is distributed in the hope that it will be useful,
13
** but WITHOUT ANY WARRANTY; without even the implied warranty of
14
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
** GNU General Public License for more details.
17
** You should have received a copy of the GNU General Public License
18
** along with this program; if not, write to the Free Software
19
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22
/* nifty idea stolen from 'skin'.. better than my own roting sockets
26
#include "xp_get_iface_addr.h"
28
struct in_addr xp_get_iface_addr(char *iname) {
31
struct in_addr retval;
32
struct sockaddr_in *sinaddr;
35
retval.s_addr = 0xffffffff; /* error */
39
if((sd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
44
memset((void *)&ifr, 0, sizeof(struct ifreq));
46
strncpy(ifr.ifr_name, iname, sizeof(ifr.ifr_name));
48
if (ioctl(sd, SIOCGIFADDR,(char *)&ifr) < 0) {
49
perror("ioctl(SIOCGIFADDR)");
51
exit(1); /* interface doesn't exist or your kernel is flacky */
54
sinaddr = (struct sockaddr_in *) &ifr.ifr_addr;
55
memcpy((void *)&retval, (void *)&(sinaddr->sin_addr.s_addr),