ifiter_sysctl.c revision 461a00bbdee50a46ba7fcfb3f9f2847d3bda5ad3
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * Copyright (C) 1999, 2000 Internet Software Consortium.
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * Permission to use, copy, modify, and distribute this software for any
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * purpose with or without fee is hereby granted, provided that the above
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * copyright notice and this permission notice appear in all copies.
96f55bdc736f8559b3a57260db6f0e964c44070dBob Halley * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
9c3531d72aeaad6c5f01efe6a1c82023e1379e4dDavid Lawrence * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
96f55bdc736f8559b3a57260db6f0e964c44070dBob Halley * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
96f55bdc736f8559b3a57260db6f0e964c44070dBob Halley * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26cf4737b3e84c3a686a5eacebf22ac39e57d4caMark Andrews * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
96f55bdc736f8559b3a57260db6f0e964c44070dBob Halley * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
96f55bdc736f8559b3a57260db6f0e964c44070dBob Halley * Obtain the list of network interfaces using sysctl.
96f55bdc736f8559b3a57260db6f0e964c44070dBob Halley * See TCP/IP Illustrated Volume 2, sections 19.8, 19.14,
9cd6d409b78a6f833b681c13a68fbdc7c024fe66David Lawrence/* XXX what about Alpha? */
bed86971bf7eb315e9c64f75bba331917f4557cfBob Halley#define ROUNDUP(a) ((a) > 0 ? \
ae8b7e02a8e5d7febba7d79b2c759add95a48f60Brian Wellington (1 + (((a) - 1) | (sizeof(__uint64_t) - 1))) : \
eefea43215016bce437ab4a7441b2851fd182960David Lawrence#define ROUNDUP(a) ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) \
ae8b7e02a8e5d7febba7d79b2c759add95a48f60Brian Wellington : sizeof(long))
b99bfa184bc9375421b5df915eea7dfac6a68a99Evan Hunt#define VALID_IFITER(t) ((t) != NULL && (t)->magic == IFITER_MAGIC)
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 unsigned int bufsize; /* Bytes allocated. */
b99bfa184bc9375421b5df915eea7dfac6a68a99Evan Hunt sysctl data. */
ae8b7e02a8e5d7febba7d79b2c759add95a48f60Brian Wellington isc_interface_t current; /* Current interface data. */
ae8b7e02a8e5d7febba7d79b2c759add95a48f60Brian Wellington isc_result_t result; /* Last result code. */
1b32bc7da1da9059abd68d6dd15b23e8a442afa3Brian Wellington 0, /* Any address family. */
b99bfa184bc9375421b5df915eea7dfac6a68a99Evan Hunt 0 /* Flags. */
b99bfa184bc9375421b5df915eea7dfac6a68a99Evan Huntisc_interfaceiter_create(isc_mem_t *mctx, isc_interfaceiter_t **iterp)
b99bfa184bc9375421b5df915eea7dfac6a68a99Evan Hunt /* Determine the amount of memory needed. */
62f016d5d301713c72a59e83d3ab41170a77f674Mark Andrews if (sysctl(mib, 6, NULL, &bufsize, NULL, (size_t) 0) < 0) {
62f016d5d301713c72a59e83d3ab41170a77f674Mark Andrews "getting interface list size: sysctl: %s",
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 iter->buf = isc_mem_get(iter->mctx, iter->bufsize);
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 if (sysctl(mib, 6, iter->buf, &bufused, NULL, (size_t) 0) < 0) {
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 "getting interface list: sysctl: %s",
d7201de09b85929a86b157f4b2d91667c68c6b52Automatic Updater * A newly created iterator has an undefined position
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 * until isc_interfaceiter_first() is called.
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 isc_mem_put(mctx, iter->buf, iter->bufsize);
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 * Get information about the current interface to iter->current.
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 * If successful, return ISC_R_SUCCESS.
62f016d5d301713c72a59e83d3ab41170a77f674Mark Andrews * If the interface has an unsupported address family,
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 * return ISC_R_IGNORE. In case of other failure,
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 * return ISC_R_UNEXPECTED.
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉internal_current(isc_interfaceiter_t *iter) {
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 REQUIRE (iter->pos < (unsigned int) iter->bufused);
62f016d5d301713c72a59e83d3ab41170a77f674Mark Andrews ifam = (struct ifa_msghdr *) ((char *) iter->buf + iter->pos);
b99bfa184bc9375421b5df915eea7dfac6a68a99Evan Hunt ifam_end = (struct ifa_msghdr *) ((char *) iter->buf + iter->bufused);
b99bfa184bc9375421b5df915eea7dfac6a68a99Evan Hunt struct if_msghdr *ifm = (struct if_msghdr *) ifam;
b99bfa184bc9375421b5df915eea7dfac6a68a99Evan Hunt struct sockaddr_dl *sdl = (struct sockaddr_dl *) (ifm + 1);
b99bfa184bc9375421b5df915eea7dfac6a68a99Evan Hunt unsigned int namelen;
8fec8134ea13c2c082c3e63f1ce0afd851e45a91Tatuya JINMEI 神明達哉 memset(&iter->current, 0, sizeof(iter->current));
03dd96d177e4ed6771be7fb5f86a3a9d5f17be4eBob Halley memcpy(iter->current.name, sdl->sdl_data, namelen);
2f012d936b5ccdf6520c96a4de23721dc58a2221Automatic Updater * This is not an interface address.
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 * Force another iteration.
dd324bd791a766c48d90ce9e43d1ab1446378983Bob Halley struct sockaddr *sa = (struct sockaddr *)(ifam + 1);
4b87939256ede703385e9cab92d3c58d03c31098Mark Andrews for (i = 0; i < RTAX_MAX; i++)
4b87939256ede703385e9cab92d3c58d03c31098Mark Andrews switch (i) {
dd324bd791a766c48d90ce9e43d1ab1446378983Bob Halley case RTAX_BRD: /* Broadcast or destination address */
b99bfa184bc9375421b5df915eea7dfac6a68a99Evan Hunt /* Do as the contributed SGI code does. */
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 /* XXX untested. */
96f55bdc736f8559b3a57260db6f0e964c44070dBob Halley get_addr(family, &iter->current.address, addr_sa);
b99bfa184bc9375421b5df915eea7dfac6a68a99Evan Hunt get_addr(family, &iter->current.netmask, mask_sa);
a829555ed724caa56b1ff7716d7eda2266491eafBob Halley get_addr(family, &iter->current.dstaddress, dst_sa);
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 printf("warning: unexpected interface list message type\n");
b99bfa184bc9375421b5df915eea7dfac6a68a99Evan Hunt * Step the iterator to the next interface. Unlike
b99bfa184bc9375421b5df915eea7dfac6a68a99Evan Hunt * isc_interfaceiter_next(), this may leave the iterator
634a52966f2324e6d5ceda191fd873ba1cfeb936Evan Hunt * positioned on an interface that will ultimately
b99bfa184bc9375421b5df915eea7dfac6a68a99Evan Hunt * be ignored. Return ISC_R_NOMORE if there are no more
b99bfa184bc9375421b5df915eea7dfac6a68a99Evan Hunt * interfaces, otherwise ISC_R_SUCCESS.
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 REQUIRE (iter->pos < (unsigned int) iter->bufused);
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 ifam = (struct ifa_msghdr *) ((char *) iter->buf + iter->pos);
03dd96d177e4ed6771be7fb5f86a3a9d5f17be4eBob Halley /* Do nothing. */