| 1 |
/* runtime-ipv6 - Check that system is able to set IPv6 address. |
| 2 |
Copyright (C) 2020-2026 Free Software Foundation, Inc. |
| 3 |
|
| 4 |
This file is part of GNU Inetutils. |
| 5 |
|
| 6 |
GNU Inetutils 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 3 of the License, or (at |
| 9 |
your option) any later version. |
| 10 |
|
| 11 |
GNU Inetutils is distributed in the hope that it will be useful, but |
| 12 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
General Public License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU General Public License |
| 17 |
along with this program. If not, see `http://www.gnu.org/licenses/'. */ |
| 18 |
|
| 19 |
/* Written by Mats Erik Andersson. */ |
| 20 |
|
| 21 |
/* Runtime-ipv6 determines by default whether IPv6 address ::1 can |
| 22 |
* be selected. The switch `-6' is identical, whereas `-4' probes |
| 23 |
* for 127.0.0.1. |
| 24 |
* |
| 25 |
* Invocation: |
| 26 |
* |
| 27 |
* runtime-ipv6 [-4] [-6] |
| 28 |
* |
| 29 |
* Return value is 0 (zero) when successful, otherwise -1. |
| 30 |
* On failure a message is printed to stderr: `IPv# disabled in system'. |
| 31 |
* |
| 32 |
* By defining the shell environment variable VERBOSE, a short affirmative |
| 33 |
* message is printed also in case of successful testing. |
| 34 |
*/ |
| 35 |
|
| 36 |
#include <config.h> |
| 37 |
|
| 38 |
#include <stdio.h> |
| 39 |
#include <unistd.h> |
| 40 |
#include <stdlib.h> |
| 41 |
#include <string.h> |
| 42 |
#include <sys/types.h> |
| 43 |
#include <sys/socket.h> |
| 44 |
#include <netdb.h> |
| 45 |
|
| 46 |
#define RESOLVE_OK 0 |
| 47 |
#define RESOLVE_FAIL -1 |
| 48 |
|
| 49 |
/* The default action is to investigate localhost as IPv6. */ |
| 50 |
int tested_family = AF_INET6; |
| 51 |
char *tested_family_name = "IPv6"; |
| 52 |
char *tested_localhost = "::1"; |
| 53 |
|
| 54 |
int |
| 55 |
main (int argc, char *argv[]) |
| 56 |
{ |
| 57 |
int err, ch; |
| 58 |
struct addrinfo hints, *aiptr; |
| 59 |
|
| 60 |
while ((ch = getopt (argc, argv, "46")) != -1) |
| 61 |
{ |
| 62 |
switch (ch) |
| 63 |
{ |
| 64 |
case '4': |
| 65 |
tested_family = AF_INET; |
| 66 |
tested_family_name = "IPv4"; |
| 67 |
tested_localhost = "127.0.0.1"; |
| 68 |
break; |
| 69 |
|
| 70 |
case '6': |
| 71 |
tested_family = AF_INET6; |
| 72 |
tested_family_name = "IPv6"; |
| 73 |
tested_localhost = "::1"; |
| 74 |
break; |
| 75 |
|
| 76 |
default: |
| 77 |
fprintf (stderr, "Usage: %s [-4 | -6]\n" |
| 78 |
"Determines presence of selected INET address family.\n", |
| 79 |
argv[0]); |
| 80 |
return EXIT_FAILURE; |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
memset (&hints, 0, sizeof (hints)); |
| 85 |
hints.ai_family = tested_family; |
| 86 |
hints.ai_socktype = SOCK_DGRAM; |
| 87 |
hints.ai_flags = AI_NUMERICHOST | AI_NUMERICSERV; |
| 88 |
#ifdef AI_ADDRCONFIG |
| 89 |
hints.ai_flags |= AI_ADDRCONFIG; |
| 90 |
#endif |
| 91 |
|
| 92 |
err = getaddrinfo (tested_localhost, "1237", &hints, &aiptr); |
| 93 |
if (!err) |
| 94 |
{ |
| 95 |
/* Should not really happen. */ |
| 96 |
if (aiptr == NULL) |
| 97 |
#ifdef EAI_NODATA |
| 98 |
err = EAI_NODATA; |
| 99 |
#else |
| 100 |
err = EAI_FAIL; |
| 101 |
#endif |
| 102 |
|
| 103 |
freeaddrinfo (aiptr); |
| 104 |
} |
| 105 |
|
| 106 |
if (err) |
| 107 |
{ |
| 108 |
/* Not able to select localhost as IPv6. */ |
| 109 |
fprintf (stderr, "%s is disabled in this running system: \"%s\" %s\n", |
| 110 |
tested_family_name, tested_localhost, gai_strerror (err)); |
| 111 |
|
| 112 |
return RESOLVE_FAIL; |
| 113 |
} |
| 114 |
else if (getenv ("VERBOSE")) |
| 115 |
printf ("Detection of %s address %s was successful.\n", |
| 116 |
tested_family_name, tested_localhost); |
| 117 |
|
| 118 |
return RESOLVE_OK; |
| 119 |
} |