Squid Web Cache master
Loading...
Searching...
No Matches
tools.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2025 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9/* DEBUG: section 21 Misc Functions */
10
11#include "squid.h"
12#include "compat/socket.h"
13#include "compat/unistd.h"
14#include "debug/Messages.h"
15#include "ip/Address.h"
16#include "ip/tools.h"
17
18#if HAVE_NETINET_IN_H
19#include <netinet/in.h>
20#endif
21#if HAVE_NETINET_IN6_H
22#include <netinet/in6.h>
23#endif
24
26
27void
29{
30 // check for usable IPv6 sockets
31 auto s = xsocket(PF_INET6, SOCK_STREAM, 0);
32 if (s < 0) {
33 debugs(3, 2, "IPv6 not supported on this machine. Auto-Disabled.");
35 return;
36 }
37
38 // Test for v4-mapping capability
39 // (AKA. the operating system supports RFC 3493 section 5.3)
40#if defined(IPV6_V6ONLY)
41 int tos = 0;
42 if (xsetsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, &tos, sizeof(int)) == 0) {
43 debugs(3, 2, "Detected IPv6 hybrid or v4-mapping stack...");
45 } else {
46 debugs(3, 2, "Detected split IPv4 and IPv6 stacks ...");
48 }
49#else
50 // compliance here means they at least supply the option for compilers building code
51 // even if possibly to return hard-coded -1 on use.
52 debugs(3, 2, "Missing RFC 3493 compliance - attempting split IPv4 and IPv6 stacks ...");
54#endif
55
56 // Test for IPv6 loopback/localhost address binding
57 Ip::Address ip;
58 ip.setLocalhost();
59 if (ip.isIPv6()) { // paranoid; always succeeds if we got this far
60 struct sockaddr_in6 sin;
61 ip.getSockAddr(sin);
62 if (xbind(s, reinterpret_cast<struct sockaddr *>(&sin), sizeof(sin)) != 0) {
63 debugs(3, Critical(66), "WARNING: BCP 177 violation. Detected non-functional IPv6 loopback.");
65 } else {
66 debugs(3, 2, "Detected functional IPv6 loopback ...");
67 }
68 }
69
70 xclose(s);
71
72#if USE_IPV6
73 debugs(3, 2, "IPv6 transport " << (EnableIpv6?"Enabled":"Disabled"));
74#else
75 debugs(3, 2, "IPv6 transport " << (EnableIpv6?"Available":"Disabled"));
76 if (EnableIpv6 != IPV6_OFF) {
77 debugs(3, Critical(67), "WARNING: BCP 177 violation. IPv6 transport forced OFF by build parameters.");
79 }
80#endif
81}
82
void getSockAddr(struct sockaddr_storage &addr, const int family) const
Definition Address.cc:936
void setLocalhost()
Definition Address.cc:275
bool isIPv6() const
Definition Address.cc:184
#define Critical(id)
Definition Messages.h:92
#define debugs(SECTION, LEVEL, CONTENT)
Definition Stream.h:192
#define IPV6_SPECIAL_SPLITSTACK
Definition tools.h:22
#define IPV6_SPECIAL_V4MAPPING
Definition tools.h:21
#define IPV6_OFF
Definition tools.h:19
void ProbeTransport(void)
Probe to discover IPv6 capabilities.
int xsocket(int domain, int type, int protocol)
POSIX socket(2) equivalent.
Definition socket.h:128
int xbind(int socketFd, const struct sockaddr *sa, socklen_t saLength)
POSIX bind(2) equivalent.
Definition socket.h:68
int xsetsockopt(int socketFd, int level, int option, const void *value, socklen_t valueLength)
POSIX setsockopt(2) equivalent.
Definition socket.h:122
int EnableIpv6
Whether IPv6 is supported and type of support.
Definition tools.h:25
int xclose(int fd)
POSIX close(2) equivalent.
Definition unistd.h:43