3 * libproxy - A library for proxy configuration
5 * Copyright (C) 2010-2011 Intel Corporation. All rights reserved.
7 * This library is free software; you can redistribute it and/or modify
8 * it under the terms and conditions of the GNU Lesser General Public
9 * License version 2.1 as published by the Free Software Foundation.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
30 #include <dbus/dbus.h>
34 struct _pxProxyFactory {
38 pxProxyFactory *px_proxy_factory_new(void)
40 pxProxyFactory *factory;
42 factory = malloc(sizeof(*factory));
46 memset(factory, 0, sizeof(*factory));
48 factory->conn = dbus_bus_get_private(DBUS_BUS_SYSTEM, NULL);
49 if (factory->conn == NULL) {
54 dbus_connection_set_exit_on_disconnect(factory->conn, FALSE);
59 void px_proxy_factory_free(pxProxyFactory *factory)
64 dbus_connection_close(factory->conn);
69 static char *parse_result(const char *str)
76 if (strcasecmp(str, "DIRECT") == 0)
77 return strdup("direct://");
79 if (strncasecmp(str, "PROXY ", 6) == 0) {
83 } else if (strncasecmp(str, "SOCKS ", 6) == 0) {
87 } else if (strncasecmp(str, "SOCKS4 ", 7) == 0) {
91 } else if (strncasecmp(str, "SOCKS5 ", 7) == 0) {
96 return strdup("direct://");
98 len = strlen(str + prefix_len) + len;
100 result = malloc(len);
102 sprintf(result, "%s://%s", protocol, str + prefix_len);
107 static char **append_result(char **prev_results, int *size, char *result)
111 results = realloc(prev_results, sizeof(char *) * (*size + 2));
112 if (results == NULL) {
117 results[*size] = result;
118 results[*size + 1] = NULL;
125 static char **extract_results(const char *str)
127 char *copy_str, *lookup, *pos, *c, *result;
128 char **results = NULL;
131 copy_str = strdup(str);
132 if (copy_str == NULL)
138 if (pos == NULL || *pos == '\0' || strlen(pos) < 6)
143 c = strchr(pos, ';');
146 c != NULL && *c == ' '; *c = '\0', c++);
151 result = parse_result(lookup);
153 results = append_result(results, &nb_results, result);
161 char **px_proxy_factory_get_proxies(pxProxyFactory *factory, const char *url)
163 DBusMessage *msg, *reply;
164 const char *str = NULL;
165 char *scheme, *host, *port, *path, **result;
173 msg = dbus_message_new_method_call("org.pacrunner",
174 "/org/pacrunner/client", "org.pacrunner.Client",
179 scheme = strdup(url);
180 if (scheme == NULL) {
181 dbus_message_unref(msg);
185 host = strstr(scheme, "://");
190 dbus_message_unref(msg);
194 path = strchr(host, '/');
198 port = strrchr(host, ':');
201 int tmp __attribute__ ((unused));
203 tmp = strtol(port + 1, &end, 10);
208 dbus_message_append_args(msg, DBUS_TYPE_STRING, &url,
209 DBUS_TYPE_STRING, &host, DBUS_TYPE_INVALID);
212 reply = dbus_connection_send_with_reply_and_block(factory->conn,
215 dbus_message_unref(msg);
220 dbus_message_get_args(reply, NULL, DBUS_TYPE_STRING, &str,
223 if (str == NULL || strlen(str) == 0)
226 result = extract_results(str);
228 dbus_message_unref(reply);
236 result = malloc(sizeof(char *) * 2);
240 result[0] = strdup("direct://");