Mr Aleph
Mr Aleph

Reputation: 1895

Retrieving Mac OS X Proxy IP address

I'm trying to programmatically get the proxy IP address or URL set on a system.
I found code that might work in a previous question here, but it's in Objective-C and what I am trying to use is plain C.

I've tried translating that obj-c code to C but no success.

Anyone knows how to get the system proxy in C?

Thank you

Upvotes: 6

Views: 2196

Answers (1)

user557219
user557219

Reputation:

This is a C translation of that answer:

CFDictionaryRef proxies = SCDynamicStoreCopyProxies(NULL);
if (proxies) {
    CFStringRef pacURL = (CFStringRef)CFDictionaryGetValue(proxies,
        kSCPropNetProxiesProxyAutoConfigURLString);

    if (pacURL) {
        char url[257] = {};
        CFStringGetCString(pacURL, url, sizeof url, kCFStringEncodingASCII);
        // do something with url
    }

    CFRelease(proxies);
}

It needs to be linked to two frameworks: SystemConfiguration and CoreFoundation.

Note that this code gets the URL for automatic proxy configuration (kSCPropNetProxiesProxyAutoConfigURLString), if any. There are several other possible proxies, e.g. HTTP proxy or HTTPS proxy. For a list of all possible proxies, see the SCSchemaDefinitions Reference.

Upvotes: 4

Related Questions