net.c revision 1a69a1a78cfaa86f3b68bbc965232b7876d4da2a
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington/*
499b34cea04a46823d003d4c0520c8b03e8513cbBrian Wellington * Copyright (C) 1999, 2000 Internet Software Consortium.
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence *
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington * Permission to use, copy, modify, and distribute this software for any
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington * purpose with or without fee is hereby granted, provided that the above
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington * copyright notice and this permission notice appear in all copies.
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence *
15a44745412679c30a6d022733925af70a38b715David Lawrence * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
15a44745412679c30a6d022733925af70a38b715David Lawrence * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
15a44745412679c30a6d022733925af70a38b715David Lawrence * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
15a44745412679c30a6d022733925af70a38b715David Lawrence * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
15a44745412679c30a6d022733925af70a38b715David Lawrence * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
15a44745412679c30a6d022733925af70a38b715David Lawrence * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
15a44745412679c30a6d022733925af70a38b715David Lawrence * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
15a44745412679c30a6d022733925af70a38b715David Lawrence * SOFTWARE.
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington */
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington#include <config.h>
4b171ebd702d72200a4d7609f11c5f79d6b6f964Brian Wellington
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington#include <errno.h>
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington#include <unistd.h>
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington
564c1b8f42d33a66c0f5c67d66bf0cd063c8b047Brian Wellington#include <isc/net.h>
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington#include <isc/once.h>
0f80bfec687db08a6e6ce945ef1d818da06c7ca9Brian Wellington#include <isc/string.h>
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#include <isc/util.h>
6734ac097f78601a8194b06360e62e043dcffdb8Brian Wellington
100d0d2ec64ab1a85f8c0d2da9b47ae411a10b21Brian Wellington#if defined(ISC_PLATFORM_HAVEIPV6) && defined(ISC_PLATFORM_NEEDIN6ADDRANY)
6028d1ce0380d0ba7f6c6ecd1ad20b31ddd1becbDavid Lawrenceconst struct in6_addr isc_net_in6addrany = IN6ADDR_ANY_INIT;
364a82f7c25b62967678027043425201a5e5171aBob Halley#endif
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellingtonstatic isc_once_t once = ISC_ONCE_INIT;
a5e73f3e9d909e86baca18713c5697f52aca87caBrian Wellingtonstatic isc_result_t ipv4_result = ISC_R_NOTFOUND;
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellingtonstatic isc_result_t ipv6_result = ISC_R_NOTFOUND;
a49acbf201a411a47e18d136b38bbea8cf283adaBrian Wellington
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellingtonstatic isc_result_t
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellingtontry_proto(int domain) {
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington int s;
0f80bfec687db08a6e6ce945ef1d818da06c7ca9Brian Wellington
09f22ac5b09e70bc526015f37168ba33e21ea91fDavid Lawrence s = socket(domain, SOCK_STREAM, 0);
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington if (s == -1) {
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington switch (errno) {
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington#ifdef EAFNOSUPPORT
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington case EAFNOSUPPORT:
92ef1a9b9dbd48ecb507b42ac62c15afefdaf838David Lawrence#endif
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#ifdef EPROTONOSUPPORT
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington case EPROTONOSUPPORT:
b984520acca2532d048eae929dc0682dd334c7a3Brian Wellington#endif
a00c5e2151cc03c06bae5cdd3b40a5de05664059Brian Wellington#ifdef EINVAL
a00c5e2151cc03c06bae5cdd3b40a5de05664059Brian Wellington case EINVAL:
0e5d6900bdfcbeef8919e6fb453ca6c44f62ccd8Brian Wellington#endif
0e5d6900bdfcbeef8919e6fb453ca6c44f62ccd8Brian Wellington return (ISC_R_NOTFOUND);
f2338a0d6aa0327372eb20ab5dc29502bc8c71efBrian Wellington default:
1e2749dba8aae3233b8962f1efe15385e92a77d9Brian Wellington UNEXPECTED_ERROR(__FILE__, __LINE__,
1e2749dba8aae3233b8962f1efe15385e92a77d9Brian Wellington "socket() failed: %s",
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson strerror(errno));
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson return (ISC_R_UNEXPECTED);
1e2749dba8aae3233b8962f1efe15385e92a77d9Brian Wellington }
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson }
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson close (s);
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson return (ISC_R_SUCCESS);
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson}
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafssonstatic void
1e2749dba8aae3233b8962f1efe15385e92a77d9Brian Wellingtoninitialize_action(void) {
1e2749dba8aae3233b8962f1efe15385e92a77d9Brian Wellington ipv4_result = try_proto(PF_INET);
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson#ifdef ISC_PLATFORM_HAVEIPV6
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson#ifdef ISC_PLATFORM_HAVEIN6PKTINFO
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson ipv6_result = try_proto(PF_INET6);
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson#else
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson ipv6_result = 0;
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson#endif
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson#endif
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson}
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafssonstatic void
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafssoninitialize(void) {
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson RUNTIME_CHECK(isc_once_do(&once, initialize_action) == ISC_R_SUCCESS);
a00c5e2151cc03c06bae5cdd3b40a5de05664059Brian Wellington}
a00c5e2151cc03c06bae5cdd3b40a5de05664059Brian Wellington
e61793f0865117ad87a19d6e245bea8f3b712d1bDanny Mayerisc_result_t
a00c5e2151cc03c06bae5cdd3b40a5de05664059Brian Wellingtonisc_net_probeipv4(void) {
0e5d6900bdfcbeef8919e6fb453ca6c44f62ccd8Brian Wellington
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson /*
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson * Check if the system's kernel supports IPv4.
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson */
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson initialize();
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson return (ipv4_result);
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson}
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafssonisc_result_t
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafssonisc_net_probeipv6(void) {
0e5d6900bdfcbeef8919e6fb453ca6c44f62ccd8Brian Wellington
0e5d6900bdfcbeef8919e6fb453ca6c44f62ccd8Brian Wellington /*
e61793f0865117ad87a19d6e245bea8f3b712d1bDanny Mayer * Check if the system's kernel supports IPv6.
0e5d6900bdfcbeef8919e6fb453ca6c44f62ccd8Brian Wellington */
b984520acca2532d048eae929dc0682dd334c7a3Brian Wellington
809fdfba224a83fcc5a8ae4394cf2a477d13dc91Brian Wellington initialize();
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington return (ipv6_result);
76c8294c81fb48b1da6e1fc5b83322a4cedb8e58Andreas Gustafsson}
76c8294c81fb48b1da6e1fc5b83322a4cedb8e58Andreas Gustafsson