File: runtime-ipv6.c

package info (click to toggle)
inetutils 2%3A2.7-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 18,588 kB
  • sloc: ansic: 132,363; sh: 12,498; yacc: 1,651; makefile: 725; perl: 72
file content (119 lines) | stat: -rw-r--r-- 3,143 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/* runtime-ipv6 - Check that system is able to set IPv6 address.
  Copyright (C) 2020-2025 Free Software Foundation, Inc.

  This file is part of GNU Inetutils.

  GNU Inetutils is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or (at
  your option) any later version.

  GNU Inetutils is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see `http://www.gnu.org/licenses/'. */

/* Written by Mats Erik Andersson.  */

/* Runtime-ipv6 determines by default whether IPv6 address ::1 can
 * be selected.  The switch `-6' is identical, whereas `-4' probes
 * for 127.0.0.1.
 *
 * Invocation:
 *
 *   runtime-ipv6 [-4] [-6]
 *
 * Return value is 0 (zero) when successful, otherwise -1.
 * On failure a message is printed to stderr: `IPv# disabled in system'.
 *
 * By defining the shell environment variable VERBOSE, a short affirmative
 * message is printed also in case of successful testing.
 */

#include <config.h>

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>

#define RESOLVE_OK            0
#define RESOLVE_FAIL         -1

/* The default action is to investigate localhost as IPv6.  */
int tested_family = AF_INET6;
char *tested_family_name = "IPv6";
char *tested_localhost = "::1";

int
main (int argc, char *argv[])
{
  int err, ch;
  struct addrinfo hints, *aiptr;

  while ((ch = getopt (argc, argv, "46")) != -1)
    {
      switch (ch)
	{
	case '4':
	  tested_family = AF_INET;
	  tested_family_name = "IPv4";
	  tested_localhost = "127.0.0.1";
	  break;

	case '6':
	  tested_family = AF_INET6;
	  tested_family_name = "IPv6";
	  tested_localhost = "::1";
	  break;

	default:
	  fprintf (stderr, "Usage: %s [-4 | -6]\n"
		   "Determines presence of selected INET address family.\n",
		   argv[0]);
	  return EXIT_FAILURE;
	}
    }

  memset (&hints, 0, sizeof (hints));
  hints.ai_family = tested_family;
  hints.ai_socktype = SOCK_DGRAM;
  hints.ai_flags = AI_NUMERICHOST | AI_NUMERICSERV;
#ifdef AI_ADDRCONFIG
  hints.ai_flags |= AI_ADDRCONFIG;
#endif

  err = getaddrinfo (tested_localhost, "1237", &hints, &aiptr);
  if (!err)
    {
      /* Should not really happen.  */
      if (aiptr == NULL)
#ifdef EAI_NODATA
	err = EAI_NODATA;
#else
	err = EAI_FAIL;
#endif

      freeaddrinfo (aiptr);
    }

  if (err)
    {
      /* Not able to select localhost as IPv6.  */
      fprintf (stderr, "%s is disabled in this running system: \"%s\" %s\n",
	       tested_family_name, tested_localhost, gai_strerror (err));

      return RESOLVE_FAIL;
    }
  else if (getenv ("VERBOSE"))
    printf ("Detection of %s address %s was successful.\n",
	    tested_family_name, tested_localhost);

  return RESOLVE_OK;
}