net.c revision 9c3531d72aeaad6c5f01efe6a1c82023e1379e4d
4b01d45a49f1c2cc4bddc9a1b4c78232867de134Andreas Gustafsson/*
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * Copyright (C) 1999, 2000 Internet Software Consortium.
4b01d45a49f1c2cc4bddc9a1b4c78232867de134Andreas Gustafsson *
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.
4b01d45a49f1c2cc4bddc9a1b4c78232867de134Andreas Gustafsson *
2eeb74d1cf5355dd98f6d507a10086e16bb08c4bTinderbox User * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
14a656f94b1fd0ababd84a772228dfa52276ba15Evan Hunt * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
14a656f94b1fd0ababd84a772228dfa52276ba15Evan Hunt * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
14a656f94b1fd0ababd84a772228dfa52276ba15Evan Hunt * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14a656f94b1fd0ababd84a772228dfa52276ba15Evan Hunt * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
14a656f94b1fd0ababd84a772228dfa52276ba15Evan Hunt * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
14a656f94b1fd0ababd84a772228dfa52276ba15Evan Hunt * SOFTWARE.
14a656f94b1fd0ababd84a772228dfa52276ba15Evan Hunt */
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein
4b01d45a49f1c2cc4bddc9a1b4c78232867de134Andreas Gustafsson/* $Id: net.c,v 1.10 2000/06/22 21:58:43 tale Exp $ */
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein#include <config.h>
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein#include <errno.h>
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein#include <unistd.h>
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein#include <isc/net.h>
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein#include <isc/once.h>
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein#include <isc/string.h>
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein#include <isc/util.h>
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein#if defined(ISC_PLATFORM_HAVEIPV6) && defined(ISC_PLATFORM_NEEDIN6ADDRANY)
194e2dfffa6a167b8eef0ad11864026b423a1c30Mark Andrewsconst struct in6_addr isc_net_in6addrany = IN6ADDR_ANY_INIT;
194e2dfffa6a167b8eef0ad11864026b423a1c30Mark Andrews#endif
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austeinstatic isc_once_t once = ISC_ONCE_INIT;
c1a883f2e04d94e99c433b1f6cfd0c0338f4ed85Mark Andrewsstatic isc_result_t ipv4_result = ISC_R_NOTFOUND;
938440694b33cd752e9e4b71a526368b4811c177Tinderbox Userstatic isc_result_t ipv6_result = ISC_R_NOTFOUND;
19c7b1a0293498a3e36692c59646ed6e15ffc8d0Tinderbox User
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrewsstatic isc_result_t
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austeintry_proto(int domain) {
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein int s;
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein s = socket(domain, SOCK_STREAM, 0);
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein if (s == -1) {
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein switch (errno) {
4b01d45a49f1c2cc4bddc9a1b4c78232867de134Andreas Gustafsson#ifdef EAFNOSUPPORT
4b01d45a49f1c2cc4bddc9a1b4c78232867de134Andreas Gustafsson case EAFNOSUPPORT:
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein#endif
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein#ifdef EPROTONOSUPPORT
14a656f94b1fd0ababd84a772228dfa52276ba15Evan Hunt case EPROTONOSUPPORT:
30eec077db2bdcb6f2a0dc388a3cdde2ede75ec1Mark Andrews#endif
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein#ifdef EINVAL
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein case EINVAL:
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein#endif
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein return (ISC_R_NOTFOUND);
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein default:
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein UNEXPECTED_ERROR(__FILE__, __LINE__,
821350367e2c7313c02eb275e8e05d5193b47cfdJeremy C. Reed "socket() failed: %s",
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein strerror(errno));
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein return (ISC_R_UNEXPECTED);
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein }
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein }
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein close (s);
14a656f94b1fd0ababd84a772228dfa52276ba15Evan Hunt
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein return (ISC_R_SUCCESS);
14a656f94b1fd0ababd84a772228dfa52276ba15Evan Hunt}
30eec077db2bdcb6f2a0dc388a3cdde2ede75ec1Mark Andrews
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austeinstatic void
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austeininitialize_action(void) {
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein ipv4_result = try_proto(PF_INET);
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein#ifdef ISC_PLATFORM_HAVEIPV6
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein#ifdef ISC_PLATFORM_HAVEIN6PKTINFO
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein ipv6_result = try_proto(PF_INET6);
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein#else
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein ipv6_result = 0;
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein#endif
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein#endif
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein}
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austeinstatic void
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austeininitialize(void) {
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein RUNTIME_CHECK(isc_once_do(&once, initialize_action) == ISC_R_SUCCESS);
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein}
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austeinisc_result_t
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austeinisc_net_probeipv4(void) {
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein /*
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein * Check if the system's kernel supports IPv4.
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein */
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein initialize();
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein return (ipv4_result);
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein}
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austeinisc_result_t
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austeinisc_net_probeipv6(void) {
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein /*
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein * Check if the system's kernel supports IPv6.
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein */
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein initialize();
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein return (ipv6_result);
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein}
268a4475065fe6a8cd7cc707820982cf5e98f430Rob Austein