Qt 4.8
Functions
qnetworkproxy_mac.cpp File Reference
#include "qnetworkproxy.h"
#include <CoreFoundation/CoreFoundation.h>
#include <SystemConfiguration/SystemConfiguration.h>
#include <QtCore/QRegExp>
#include <QtCore/QStringList>
#include <QtCore/QUrl>
#include <QtCore/qendian.h>
#include <QtCore/qstringlist.h>
#include "private/qcore_mac_p.h"

Go to the source code of this file.

Functions

const char * cfurlErrorDescription (SInt32 errorCode)
 
static bool isHostExcluded (CFDictionaryRef dict, const QString &host)
 
QList< QNetworkProxymacQueryInternal (const QNetworkProxyQuery &query)
 
static QNetworkProxy proxyFromDictionary (CFDictionaryRef dict, QNetworkProxy::ProxyType type, CFStringRef enableKey, CFStringRef hostKey, CFStringRef portKey)
 
static QNetworkProxy proxyFromDictionary (CFDictionaryRef dict)
 

Function Documentation

◆ cfurlErrorDescription()

const char* cfurlErrorDescription ( SInt32  errorCode)

Definition at line 184 of file qnetworkproxy_mac.cpp.

Referenced by macQueryInternal().

185 {
186  switch (errorCode) {
187  case kCFURLUnknownError:
188  return "Unknown Error";
189  case kCFURLUnknownSchemeError:
190  return "Unknown Scheme";
191  case kCFURLResourceNotFoundError:
192  return "Resource Not Found";
193  case kCFURLResourceAccessViolationError:
194  return "Resource Access Violation";
195  case kCFURLRemoteHostUnavailableError:
196  return "Remote Host Unavailable";
197  case kCFURLImproperArgumentsError:
198  return "Improper Arguments";
199  case kCFURLUnknownPropertyKeyError:
200  return "Unknown Property Key";
201  case kCFURLPropertyKeyUnavailableError:
202  return "Property Key Unavailable";
203  case kCFURLTimeoutError:
204  return "Timeout";
205  default:
206  return "Really Unknown Error";
207  }
208 }

◆ isHostExcluded()

static bool isHostExcluded ( CFDictionaryRef  dict,
const QString host 
)
static

Definition at line 82 of file qnetworkproxy_mac.cpp.

Referenced by Maemo::ProxyConfPrivate::flush(), and macQueryInternal().

83 {
84  if (host.isEmpty())
85  return true;
86 
87  bool isSimple = !host.contains(QLatin1Char('.')) && !host.contains(QLatin1Char(':'));
88  CFNumberRef excludeSimples;
89  if (isSimple &&
90  (excludeSimples = (CFNumberRef)CFDictionaryGetValue(dict, kSCPropNetProxiesExcludeSimpleHostnames))) {
91  int enabled;
92  if (CFNumberGetValue(excludeSimples, kCFNumberIntType, &enabled) && enabled)
93  return true;
94  }
95 
96  QHostAddress ipAddress;
97  bool isIpAddress = ipAddress.setAddress(host);
98 
99  // not a simple host name
100  // does it match the list of exclusions?
101  CFArrayRef exclusionList = (CFArrayRef)CFDictionaryGetValue(dict, kSCPropNetProxiesExceptionsList);
102  if (!exclusionList)
103  return false;
104 
105  CFIndex size = CFArrayGetCount(exclusionList);
106  for (CFIndex i = 0; i < size; ++i) {
107  CFStringRef cfentry = (CFStringRef)CFArrayGetValueAtIndex(exclusionList, i);
108  QString entry = QCFString::toQString(cfentry);
109 
110  if (isIpAddress && ipAddress.isInSubnet(QHostAddress::parseSubnet(entry))) {
111  return true; // excluded
112  } else {
113  // do wildcard matching
115  if (rx.exactMatch(host))
116  return true;
117  }
118  }
119 
120  // host was not excluded
121  return false;
122 }
QBool contains(QChar c, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Definition: qstring.h:904
const struct __CFString * CFStringRef
The QRegExp class provides pattern matching using regular expressions.
Definition: qregexp.h:61
static QString toQString(CFStringRef cfstr)
Definition: qcore_mac.cpp:47
The QString class provides a Unicode character string.
Definition: qstring.h:83
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
static QPair< QHostAddress, int > parseSubnet(const QString &subnet)
Parses the IP and subnet information contained in subnet and returns the network prefix for that netw...
bool isInSubnet(const QHostAddress &subnet, int netmask) const
Returns true if this IP is in the subnet described by the network prefix subnet and netmask netmask...
void setAddress(quint32 ip4Addr)
Set the IPv4 address specified by ip4Addr.
The QHostAddress class provides an IP address.
Definition: qhostaddress.h:70
const struct __CFArray * CFArrayRef
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
#define enabled

◆ macQueryInternal()

QList<QNetworkProxy> macQueryInternal ( const QNetworkProxyQuery query)

Definition at line 210 of file qnetworkproxy_mac.cpp.

211 {
212  QList<QNetworkProxy> result;
213 
214  // obtain a dictionary to the proxy settings:
215  CFDictionaryRef dict = SCDynamicStoreCopyProxies(NULL);
216  if (!dict) {
217  qWarning("QNetworkProxyFactory::systemProxyForQuery: SCDynamicStoreCopyProxies returned NULL");
218  return result; // failed
219  }
220 
221  if (isHostExcluded(dict, query.peerHostName())) {
222  CFRelease(dict);
223  return result; // no proxy for this host
224  }
225 
226  // is there a PAC enabled? If so, use it first.
227  CFNumberRef pacEnabled;
228  if ((pacEnabled = (CFNumberRef)CFDictionaryGetValue(dict, kSCPropNetProxiesProxyAutoConfigEnable))) {
229  int enabled;
230  if (CFNumberGetValue(pacEnabled, kCFNumberIntType, &enabled) && enabled) {
231  // PAC is enabled
232  CFStringRef cfPacLocation = (CFStringRef)CFDictionaryGetValue(dict, kSCPropNetProxiesProxyAutoConfigURLString);
233 
234 #if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5)
236  QCFType<CFDataRef> pacData;
237  QCFType<CFURLRef> pacUrl = CFURLCreateWithString(kCFAllocatorDefault, cfPacLocation, NULL);
238  SInt32 errorCode;
239  if (!CFURLCreateDataAndPropertiesFromResource(kCFAllocatorDefault, pacUrl, &pacData, NULL, NULL, &errorCode)) {
240  QString pacLocation = QCFString::toQString(cfPacLocation);
241  qWarning("Unable to get the PAC script at \"%s\" (%s)", qPrintable(pacLocation), cfurlErrorDescription(errorCode));
242  return result;
243  }
244 
245  QCFType<CFStringRef> pacScript = CFStringCreateFromExternalRepresentation(kCFAllocatorDefault, pacData, kCFStringEncodingISOLatin1);
246  if (!pacScript) {
247  // This should never happen, but the documentation says it may return NULL if there was a problem creating the object.
248  QString pacLocation = QCFString::toQString(cfPacLocation);
249  qWarning("Unable to read the PAC script at \"%s\"", qPrintable(pacLocation));
250  return result;
251  }
252 
253  QByteArray encodedURL = query.url().toEncoded(); // converted to UTF-8
254  if (encodedURL.isEmpty()) {
255  return result; // Invalid URL, abort
256  }
257 
258  QCFType<CFURLRef> targetURL = CFURLCreateWithBytes(kCFAllocatorDefault, (UInt8*)encodedURL.data(), encodedURL.size(), kCFStringEncodingUTF8, NULL);
259  if (!targetURL) {
260  return result; // URL creation problem, abort
261  }
262 
263  QCFType<CFErrorRef> pacError;
264  QCFType<CFArrayRef> proxies = CFNetworkCopyProxiesForAutoConfigurationScript(pacScript, targetURL, &pacError);
265  if (!proxies) {
266  QString pacLocation = QCFString::toQString(cfPacLocation);
267  QCFType<CFStringRef> pacErrorDescription = CFErrorCopyDescription(pacError);
268  qWarning("Execution of PAC script at \"%s\" failed: %s", qPrintable(pacLocation), qPrintable(QCFString::toQString(pacErrorDescription)));
269  return result;
270  }
271 
272  CFIndex size = CFArrayGetCount(proxies);
273  for (CFIndex i = 0; i < size; ++i) {
274  CFDictionaryRef proxy = (CFDictionaryRef)CFArrayGetValueAtIndex(proxies, i);
275  result << proxyFromDictionary(proxy);
276  }
277  return result;
278  } else
279 #endif
280  {
281  QString pacLocation = QCFString::toQString(cfPacLocation);
282  qWarning("Mac system proxy: PAC script at \"%s\" not handled", qPrintable(pacLocation));
283  }
284  }
285  }
286 
287  // no PAC, decide which proxy we're looking for based on the query
288  bool isHttps = false;
289  QString protocol = query.protocolTag().toLower();
290 
291  // try the protocol-specific proxy
292  QNetworkProxy protocolSpecificProxy;
293  if (protocol == QLatin1String("ftp")) {
294  protocolSpecificProxy =
296  kSCPropNetProxiesFTPEnable,
297  kSCPropNetProxiesFTPProxy,
298  kSCPropNetProxiesFTPPort);
299  } else if (protocol == QLatin1String("http")) {
300  protocolSpecificProxy =
302  kSCPropNetProxiesHTTPEnable,
303  kSCPropNetProxiesHTTPProxy,
304  kSCPropNetProxiesHTTPPort);
305  } else if (protocol == QLatin1String("https")) {
306  isHttps = true;
307  protocolSpecificProxy =
309  kSCPropNetProxiesHTTPSEnable,
310  kSCPropNetProxiesHTTPSProxy,
311  kSCPropNetProxiesHTTPSPort);
312  }
313  if (protocolSpecificProxy.type() != QNetworkProxy::DefaultProxy)
314  result << protocolSpecificProxy;
315 
316  // let's add SOCKSv5 if present too
318  kSCPropNetProxiesSOCKSEnable,
319  kSCPropNetProxiesSOCKSProxy,
320  kSCPropNetProxiesSOCKSPort);
321  if (socks5.type() != QNetworkProxy::DefaultProxy)
322  result << socks5;
323 
324  // let's add the HTTPS proxy if present (and if we haven't added
325  // yet)
326  if (!isHttps) {
328  kSCPropNetProxiesHTTPSEnable,
329  kSCPropNetProxiesHTTPSProxy,
330  kSCPropNetProxiesHTTPSPort);
331  if (https.type() != QNetworkProxy::DefaultProxy && https != protocolSpecificProxy)
332  result << https;
333  }
334 
335  CFRelease(dict);
336  return result;
337 }
QNetworkProxy::ProxyType type() const
Returns the proxy type for this instance.
const char * cfurlErrorDescription(SInt32 errorCode)
const struct __CFString * CFStringRef
char * data()
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:429
QString peerHostName() const
Returns the host name or IP address being of the outgoing connection being requested, or an empty string if the remote hostname is not known.
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
static QString toQString(CFStringRef cfstr)
Definition: qcore_mac.cpp:47
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QString class provides a Unicode character string.
Definition: qstring.h:83
The QNetworkProxy class provides a network layer proxy.
QString protocolTag() const
Returns the protocol tag for this QNetworkProxyQuery object, or an empty QString in case the protocol...
Q_CORE_EXPORT void qWarning(const char *,...)
static bool isHostExcluded(CFDictionaryRef dict, const QString &host)
QByteArray toEncoded(FormattingOptions options=None) const
Returns the encoded representation of the URL if it&#39;s valid; otherwise an empty QByteArray is returne...
Definition: qurl.cpp:5949
QString toLower() const Q_REQUIRED_RESULT
Returns a lowercase copy of the string.
Definition: qstring.cpp:5389
int size() const
Returns the number of bytes in this byte array.
Definition: qbytearray.h:402
QUrl url() const
Returns the URL component of this QNetworkProxyQuery object in case of a query of type QNetworkProxyQ...
static const MacVersion MacintoshVersion
the version of the Macintosh operating system on which the application is run (Mac only)...
Definition: qglobal.h:1646
bool isEmpty() const
Returns true if the byte array has size 0; otherwise returns false.
Definition: qbytearray.h:421
static QNetworkProxy proxyFromDictionary(CFDictionaryRef dict, QNetworkProxy::ProxyType type, CFStringRef enableKey, CFStringRef hostKey, CFStringRef portKey)
#define qPrintable(string)
Definition: qglobal.h:1750
#define enabled

◆ proxyFromDictionary() [1/2]

static QNetworkProxy proxyFromDictionary ( CFDictionaryRef  dict,
QNetworkProxy::ProxyType  type,
CFStringRef  enableKey,
CFStringRef  hostKey,
CFStringRef  portKey 
)
static

Definition at line 124 of file qnetworkproxy_mac.cpp.

Referenced by macQueryInternal().

127 {
128  CFNumberRef protoEnabled;
129  CFNumberRef protoPort;
130  CFStringRef protoHost;
131  if (enableKey
132  && (protoEnabled = (CFNumberRef)CFDictionaryGetValue(dict, enableKey))
133  && (protoHost = (CFStringRef)CFDictionaryGetValue(dict, hostKey))
134  && (protoPort = (CFNumberRef)CFDictionaryGetValue(dict, portKey))) {
135  int enabled;
136  if (CFNumberGetValue(protoEnabled, kCFNumberIntType, &enabled) && enabled) {
137  QString host = QCFString::toQString(protoHost);
138 
139  int port;
140  CFNumberGetValue(protoPort, kCFNumberIntType, &port);
141 
142  return QNetworkProxy(type, host, port);
143  }
144  }
145 
146  // proxy not enabled
147  return QNetworkProxy();
148 }
const struct __CFString * CFStringRef
int type
Definition: qmetatype.cpp:239
static QString toQString(CFStringRef cfstr)
Definition: qcore_mac.cpp:47
The QString class provides a Unicode character string.
Definition: qstring.h:83
The QNetworkProxy class provides a network layer proxy.
#define enabled

◆ proxyFromDictionary() [2/2]

static QNetworkProxy proxyFromDictionary ( CFDictionaryRef  dict)
static

Definition at line 151 of file qnetworkproxy_mac.cpp.

152 {
154  QString hostName;
155  quint16 port = 0;
156  QString user;
157  QString password;
158 
159  CFStringRef cfProxyType = (CFStringRef)CFDictionaryGetValue(dict, kCFProxyTypeKey);
160  if (CFStringCompare(cfProxyType, kCFProxyTypeNone, 0) == kCFCompareEqualTo) {
161  proxyType = QNetworkProxy::NoProxy;
162  } else if (CFStringCompare(cfProxyType, kCFProxyTypeFTP, 0) == kCFCompareEqualTo) {
163  proxyType = QNetworkProxy::FtpCachingProxy;
164  } else if (CFStringCompare(cfProxyType, kCFProxyTypeHTTP, 0) == kCFCompareEqualTo) {
165  proxyType = QNetworkProxy::HttpProxy;
166  } else if (CFStringCompare(cfProxyType, kCFProxyTypeHTTPS, 0) == kCFCompareEqualTo) {
167  proxyType = QNetworkProxy::HttpProxy;
168  } else if (CFStringCompare(cfProxyType, kCFProxyTypeSOCKS, 0) == kCFCompareEqualTo) {
169  proxyType = QNetworkProxy::Socks5Proxy;
170  }
171 
172  hostName = QCFString::toQString((CFStringRef)CFDictionaryGetValue(dict, kCFProxyHostNameKey));
173  user = QCFString::toQString((CFStringRef)CFDictionaryGetValue(dict, kCFProxyUsernameKey));
174  password = QCFString::toQString((CFStringRef)CFDictionaryGetValue(dict, kCFProxyPasswordKey));
175 
176  CFNumberRef portNumber = (CFNumberRef)CFDictionaryGetValue(dict, kCFProxyPortNumberKey);
177  if (portNumber) {
178  CFNumberGetValue(portNumber, kCFNumberSInt16Type, &port);
179  }
180 
181  return QNetworkProxy(proxyType, hostName, port, user, password);
182 }
const struct __CFString * CFStringRef
static QString toQString(CFStringRef cfstr)
Definition: qcore_mac.cpp:47
The QString class provides a Unicode character string.
Definition: qstring.h:83
The QNetworkProxy class provides a network layer proxy.
unsigned short quint16
Definition: qglobal.h:936
ProxyType
This enum describes the types of network proxying provided in Qt.