ifiter_ioctl.c revision 27e48d2eea8a6231697719dbaca341603072c89d
/*
* Copyright (C) 1999 Internet Software Consortium.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
* ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
* CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
* SOFTWARE.
*/
/*
* Obtain the list of network interfaces using the SIOCGIFCONF ioctl.
* See netintro(4).
*/
struct isc_interfaceiter {
unsigned int magic; /* Magic number. */
int socket;
void *buf; /* Buffer for sysctl data. */
unsigned int bufsize; /* Bytes allocated. */
unsigned int pos; /* Current offset in
SIOCGIFCONF data */
};
/*
* Size of buffer for SIOCGIFCONF, in bytes. We assume no sane system
* will have more than a megabyte of interface configuration data.
*/
#define IFCONF_BUFSIZE_INITIAL 4096
#define IFCONF_BUFSIZE_MAX 1048576
{
return (ISC_R_NOMEMORY);
/* Create an unbound datagram socket to do the SIOCGIFADDR ioctl on. */
"making interface scan socket: %s",
goto socket_failure;
}
/*
* Get the interface configuration, allocating more memory if
* necessary.
*/
for (;;) {
goto alloc_failure;
}
"get interface configuration: %s",
goto ioctl_failure;
}
/* EINVAL. Retry with a bigger buffer. */
} else {
/*
* The ioctl succeeded.
* Some OS's just return what will fit rather
* than set EINVAL if the buffer is too small
* to fit all the interfaces in. If
* ifc.ifc_len is too near to the end of the
* buffer we will grow it just in case and
* retry.
*/
break;
}
"get interface configuration: "
"maximum buffer size exceeded");
goto ioctl_failure;
}
}
/*
* A newly created iterator has an undefined position
* until isc_interfaceiter_first() is called.
*/
return (ISC_R_SUCCESS);
return (result);
}
/*
* Get information about the current interface to iter->current.
* If successful, return ISC_R_SUCCESS.
* If the interface has an unsupported address family,
* return ISC_R_FAILURE. In case of other failure,
* return ISC_R_UNEXPECTED.
*/
static isc_result_t
int family;
return (ISC_R_FAILURE);
/* Get interface flags. */
"%s: getting interface flags: %s",
return (ISC_R_UNEXPECTED);
}
/* If the interface is point-to-point, get the destination address. */
"%s: getting destination address: %s",
return (ISC_R_UNEXPECTED);
}
&ifreq.ifr_dstaddr);
}
/* Get the network mask. */
"%s: getting netmask: %s",
return (ISC_R_UNEXPECTED);
}
return (ISC_R_SUCCESS);
}
/*
* Step the iterator to the next interface. Unlike
* isc_interfaceiter_next(), this may leave the iterator
* positioned on an interface that will ultimately
* be ignored. Return ISC_R_NOMORE if there are no more
* interfaces, otherwise ISC_R_SUCCESS.
*/
static isc_result_t
#ifdef ISC_NET_HAVESALEN
else
#endif
return (ISC_R_NOMORE);
return (ISC_R_SUCCESS);
}
static void
}