]> git.sesse.net Git - vlc/blob - src/darwin/netconf.c
fix error in getting proxy setting on darwin.
[vlc] / src / darwin / netconf.c
1 /*****************************************************************************
2  * netconf.c : Network configuration
3  *****************************************************************************
4  * Copyright (C) 2013 VLC authors and VideoLAN
5  * $Id$
6  *
7  * Authors: Felix Paul Kühne <fkuehne # videolan org>
8  *
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include <vlc_common.h>
29 #include <vlc_network.h>
30
31 #import <TargetConditionals.h>
32 #include <CoreFoundation/CoreFoundation.h>
33 #if TARGET_OS_IPHONE
34 #include <CFNetwork/CFProxySupport.h>
35 #else
36 #include <SystemConfiguration/SystemConfiguration.h>
37 #endif
38
39 /**
40  * Determines the network proxy server to use (if any).
41  * @param url absolute URL for which to get the proxy server (not used)
42  * @return proxy URL, NULL if no proxy or error
43  */
44 char *vlc_getProxyUrl(const char *url)
45 {
46     VLC_UNUSED(url);
47 #if TARGET_OS_IPHONE
48     char *proxy_url = NULL;
49     CFDictionaryRef dicRef = CFNetworkCopySystemProxySettings();
50     if (NULL != dicRef) {
51         const CFStringRef proxyCFstr = (const CFStringRef)CFDictionaryGetValue(
52             dicRef, (const void*)kCFNetworkProxiesHTTPProxy);
53         const CFNumberRef portCFnum = (const CFNumberRef)CFDictionaryGetValue(
54             dicRef, (const void*)kCFNetworkProxiesHTTPPort);
55         if (NULL != proxyCFstr && NULL != portCFnum) {
56             int port = 0;
57             if (!CFNumberGetValue(portCFnum, kCFNumberIntType, &port)) {
58                 CFRelease(dicRef);
59                 return NULL;
60             }
61
62             char host_buffer[4096];
63             memset(host_buffer, 0, sizeof(host_buffer));
64             if (CFStringGetCString(proxyCFstr, host_buffer, sizeof(host_buffer)
65                                    - 1, kCFStringEncodingUTF8)) {
66                 char buffer[4096];
67                 memset(host_buffer, 0, sizeof(host_buffer));
68                 sprintf(buffer, "%s:%d", host_buffer, port);
69                 proxy_url = strdup(buffer);
70             }
71         }
72
73         CFRelease(dicRef);
74     }
75
76     return proxy_url;
77 #else
78     CFDictionaryRef proxies = SCDynamicStoreCopyProxies(NULL);
79     char *proxy_url = NULL;
80
81     if (proxies) {
82         CFNumberRef cfn_httpProxyOn =
83             (CFNumberRef)CFDictionaryGetValue(proxies,
84                                               kSCPropNetProxiesHTTPEnable);
85         if (cfn_httpProxyOn) {
86             int i_httpProxyOn;
87             CFNumberGetValue(cfn_httpProxyOn, kCFNumberIntType, &i_httpProxyOn);
88             CFRelease(cfn_httpProxyOn);
89
90             if (i_httpProxyOn == 1) // http proxy is on
91             {
92                 CFStringRef httpProxy =
93                     (CFStringRef)CFDictionaryGetValue(proxies,
94                                                       kSCPropNetProxiesHTTPProxy);
95
96                 if (httpProxy) {
97                     CFNumberRef cfn_httpProxyPort =
98                         (CFNumberRef)CFDictionaryGetValue(proxies,
99                                                         kSCPropNetProxiesHTTPPort);
100                     int i_httpProxyPort;
101                     CFNumberGetValue(cfn_httpProxyPort,
102                                      kCFNumberIntType,
103                                      &i_httpProxyPort);
104                     CFRelease(cfn_httpProxyPort);
105
106                     CFMutableStringRef outputURL =
107                         CFStringCreateMutableCopy(kCFAllocatorDefault,
108                                                   0,
109                                                   httpProxy);
110                     if (i_httpProxyPort > 0)
111                         CFStringAppendFormat(outputURL,
112                                              NULL,
113                                              CFSTR(":%i"),
114                                              i_httpProxyPort);
115
116                     char buffer[4096];
117                     if (CFStringGetCString(outputURL, buffer, sizeof(buffer),
118                         kCFStringEncodingUTF8))
119                         proxy_url = strdup(buffer);
120
121                     CFRelease(outputURL);
122                 }
123                 CFRelease(httpProxy);
124             }
125         }
126         CFRelease(proxies);
127     }
128
129     return proxy_url;
130 #endif
131 }