diff options
-rw-r--r-- | AUTHORS | 0 | ||||
l--------- | COPYING | 1 | ||||
-rw-r--r-- | ChangeLog | 0 | ||||
-rw-r--r-- | INSTALL | 234 | ||||
-rw-r--r-- | Makefile.am | 1 | ||||
-rw-r--r-- | NEWS | 0 | ||||
-rw-r--r-- | README | 0 | ||||
-rw-r--r-- | configure.ac | 34 | ||||
-rw-r--r-- | plugins/envvar.c | 27 | ||||
-rw-r--r-- | src/Makefile.am | 6 | ||||
-rw-r--r-- | src/proxy.h | 18 | ||||
-rw-r--r-- | src/proxy_factory.c | 256 | ||||
-rw-r--r-- | src/proxy_plugin.h | 27 |
13 files changed, 604 insertions, 0 deletions
@@ -0,0 +1 @@ +/usr/share/automake-1.10/COPYING
\ No newline at end of file diff --git a/ChangeLog b/ChangeLog new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/ChangeLog @@ -0,0 +1,234 @@ +Installation Instructions +************************* + +Copyright (C) 1994, 1995, 1996, 1999, 2000, 2001, 2002, 2004, 2005, +2006 Free Software Foundation, Inc. + +This file is free documentation; the Free Software Foundation gives +unlimited permission to copy, distribute and modify it. + +Basic Installation +================== + +Briefly, the shell commands `./configure; make; make install' should +configure, build, and install this package. The following +more-detailed instructions are generic; see the `README' file for +instructions specific to this package. + + The `configure' shell script attempts to guess correct values for +various system-dependent variables used during compilation. It uses +those values to create a `Makefile' in each directory of the package. +It may also create one or more `.h' files containing system-dependent +definitions. Finally, it creates a shell script `config.status' that +you can run in the future to recreate the current configuration, and a +file `config.log' containing compiler output (useful mainly for +debugging `configure'). + + It can also use an optional file (typically called `config.cache' +and enabled with `--cache-file=config.cache' or simply `-C') that saves +the results of its tests to speed up reconfiguring. Caching is +disabled by default to prevent problems with accidental use of stale +cache files. + + If you need to do unusual things to compile the package, please try +to figure out how `configure' could check whether to do them, and mail +diffs or instructions to the address given in the `README' so they can +be considered for the next release. If you are using the cache, and at +some point `config.cache' contains results you don't want to keep, you +may remove or edit it. + + The file `configure.ac' (or `configure.in') is used to create +`configure' by a program called `autoconf'. You need `configure.ac' if +you want to change it or regenerate `configure' using a newer version +of `autoconf'. + +The simplest way to compile this package is: + + 1. `cd' to the directory containing the package's source code and type + `./configure' to configure the package for your system. + + Running `configure' might take a while. While running, it prints + some messages telling which features it is checking for. + + 2. Type `make' to compile the package. + + 3. Optionally, type `make check' to run any self-tests that come with + the package. + + 4. Type `make install' to install the programs and any data files and + documentation. + + 5. You can remove the program binaries and object files from the + source code directory by typing `make clean'. To also remove the + files that `configure' created (so you can compile the package for + a different kind of computer), type `make distclean'. There is + also a `make maintainer-clean' target, but that is intended mainly + for the package's developers. If you use it, you may have to get + all sorts of other programs in order to regenerate files that came + with the distribution. + +Compilers and Options +===================== + +Some systems require unusual options for compilation or linking that the +`configure' script does not know about. Run `./configure --help' for +details on some of the pertinent environment variables. + + You can give `configure' initial values for configuration parameters +by setting variables in the command line or in the environment. Here +is an example: + + ./configure CC=c99 CFLAGS=-g LIBS=-lposix + + *Note Defining Variables::, for more details. + +Compiling For Multiple Architectures +==================================== + +You can compile the package for more than one kind of computer at the +same time, by placing the object files for each architecture in their +own directory. To do this, you can use GNU `make'. `cd' to the +directory where you want the object files and executables to go and run +the `configure' script. `configure' automatically checks for the +source code in the directory that `configure' is in and in `..'. + + With a non-GNU `make', it is safer to compile the package for one +architecture at a time in the source code directory. After you have +installed the package for one architecture, use `make distclean' before +reconfiguring for another architecture. + +Installation Names +================== + +By default, `make install' installs the package's commands under +`/usr/local/bin', include files under `/usr/local/include', etc. You +can specify an installation prefix other than `/usr/local' by giving +`configure' the option `--prefix=PREFIX'. + + You can specify separate installation prefixes for +architecture-specific files and architecture-independent files. If you +pass the option `--exec-prefix=PREFIX' to `configure', the package uses +PREFIX as the prefix for installing programs and libraries. +Documentation and other data files still use the regular prefix. + + In addition, if you use an unusual directory layout you can give +options like `--bindir=DIR' to specify different values for particular +kinds of files. Run `configure --help' for a list of the directories +you can set and what kinds of files go in them. + + If the package supports it, you can cause programs to be installed +with an extra prefix or suffix on their names by giving `configure' the +option `--program-prefix=PREFIX' or `--program-suffix=SUFFIX'. + +Optional Features +================= + +Some packages pay attention to `--enable-FEATURE' options to +`configure', where FEATURE indicates an optional part of the package. +They may also pay attention to `--with-PACKAGE' options, where PACKAGE +is something like `gnu-as' or `x' (for the X Window System). The +`README' should mention any `--enable-' and `--with-' options that the +package recognizes. + + For packages that use the X Window System, `configure' can usually +find the X include and library files automatically, but if it doesn't, +you can use the `configure' options `--x-includes=DIR' and +`--x-libraries=DIR' to specify their locations. + +Specifying the System Type +========================== + +There may be some features `configure' cannot figure out automatically, +but needs to determine by the type of machine the package will run on. +Usually, assuming the package is built to be run on the _same_ +architectures, `configure' can figure that out, but if it prints a +message saying it cannot guess the machine type, give it the +`--build=TYPE' option. TYPE can either be a short name for the system +type, such as `sun4', or a canonical name which has the form: + + CPU-COMPANY-SYSTEM + +where SYSTEM can have one of these forms: + + OS KERNEL-OS + + See the file `config.sub' for the possible values of each field. If +`config.sub' isn't included in this package, then this package doesn't +need to know the machine type. + + If you are _building_ compiler tools for cross-compiling, you should +use the option `--target=TYPE' to select the type of system they will +produce code for. + + If you want to _use_ a cross compiler, that generates code for a +platform different from the build platform, you should specify the +"host" platform (i.e., that on which the generated programs will +eventually be run) with `--host=TYPE'. + +Sharing Defaults +================ + +If you want to set default values for `configure' scripts to share, you +can create a site shell script called `config.site' that gives default +values for variables like `CC', `cache_file', and `prefix'. +`configure' looks for `PREFIX/share/config.site' if it exists, then +`PREFIX/etc/config.site' if it exists. Or, you can set the +`CONFIG_SITE' environment variable to the location of the site script. +A warning: not all `configure' scripts look for a site script. + +Defining Variables +================== + +Variables not defined in a site shell script can be set in the +environment passed to `configure'. However, some packages may run +configure again during the build, and the customized values of these +variables may be lost. In order to avoid this problem, you should set +them in the `configure' command line, using `VAR=value'. For example: + + ./configure CC=/usr/local2/bin/gcc + +causes the specified `gcc' to be used as the C compiler (unless it is +overridden in the site shell script). + +Unfortunately, this technique does not work for `CONFIG_SHELL' due to +an Autoconf bug. Until the bug is fixed you can use this workaround: + + CONFIG_SHELL=/bin/bash /bin/bash ./configure CONFIG_SHELL=/bin/bash + +`configure' Invocation +====================== + +`configure' recognizes the following options to control how it operates. + +`--help' +`-h' + Print a summary of the options to `configure', and exit. + +`--version' +`-V' + Print the version of Autoconf used to generate the `configure' + script, and exit. + +`--cache-file=FILE' + Enable the cache: use and save the results of the tests in FILE, + traditionally `config.cache'. FILE defaults to `/dev/null' to + disable caching. + +`--config-cache' +`-C' + Alias for `--cache-file=config.cache'. + +`--quiet' +`--silent' +`-q' + Do not print messages saying which checks are being made. To + suppress all normal output, redirect it to `/dev/null' (any error + messages will still be shown). + +`--srcdir=DIR' + Look for the package's source code in directory DIR. Usually + `configure' can determine that directory automatically. + +`configure' also accepts some other, not widely useful, options. Run +`configure --help' for more details. + diff --git a/Makefile.am b/Makefile.am new file mode 100644 index 0000000..af437a6 --- /dev/null +++ b/Makefile.am @@ -0,0 +1 @@ +SUBDIRS = src diff --git a/configure.ac b/configure.ac new file mode 100644 index 0000000..aae511a --- /dev/null +++ b/configure.ac @@ -0,0 +1,34 @@ +# -*- Autoconf -*- +# Process this file with autoconf to produce a configure script. + +AC_PREREQ(2.61) +AC_INIT([libproxy],[0.1],[nathaniel@natemccallum.com]) +AC_CONFIG_SRCDIR([src/proxy_factory.c]) +AM_INIT_AUTOMAKE + +# Checks for programs. +AC_PROG_CC +AC_PROG_CPP +AC_PROG_INSTALL +AC_PROG_LIBTOOL +AC_PROG_LN_S +AC_PROG_MAKE_SET + +# Checks for libraries. + +# Checks for header files. +AC_HEADER_STDC +#AC_CHECK_HEADERS([netdb.h stdlib.h string.h sys/socket.h unistd.h]) + +# Checks for typedefs, structures, and compiler characteristics. +AC_HEADER_STDBOOL +AC_C_CONST +AC_TYPE_SIZE_T +CFLAGS="-std=c99 $CFLAGS" + +# Checks for library functions. +AC_FUNC_MALLOC +#AC_CHECK_FUNCS([gethostbyname gethostname memset socket strdup strstr]) + +AC_CONFIG_FILES([Makefile src/Makefile]) +AC_OUTPUT diff --git a/plugins/envvar.c b/plugins/envvar.c new file mode 100644 index 0000000..2b3e8ee --- /dev/null +++ b/plugins/envvar.c @@ -0,0 +1,27 @@ +#include <stdlib.h> +#include <string.h> + +#include "proxy_plugin.h" + +PXConfig *get_config_cb(PXProxyFactory *self) +{ + char *proxy = getenv("http_proxy"); + if (!proxy) return NULL; + PXConfig *config = malloc(sizeof(PXConfig)); + if (!config) return NULL; + config->url = strdup(proxy); + if (!config->url) { free(config); return NULL; } + config->ignore = strdup(""); // TODO: Make this pay attention to 'no_proxy' later + if (!config->ignore) { free(config->url); free(config); return NULL; } + return config; +} + +void on_proxyfactory_instantiate(PXProxyFactory *self) +{ + px_proxy_factory_config_add(self, PX_CONFIG_BACKEND_ENVVAR, get_config_cb); +} + +void on_proxyfactory_destantiate(PXProxyFactory *self) +{ + px_proxy_factory_config_del(self, PX_CONFIG_BACKEND_ENVVAR); +} diff --git a/src/Makefile.am b/src/Makefile.am new file mode 100644 index 0000000..01e902e --- /dev/null +++ b/src/Makefile.am @@ -0,0 +1,6 @@ +lib_LTLIBRARIES = libproxy.la +libproxy_la_SOURCES = proxy_factory.c proxy.h proxy_plugin.h +libproxy_la_CFLAGS = -fvisibility=hidden +libproxy_la_LDFLAGS = -lm + +include_HEADERS = proxy.h diff --git a/src/proxy.h b/src/proxy.h new file mode 100644 index 0000000..097eb4c --- /dev/null +++ b/src/proxy.h @@ -0,0 +1,18 @@ +enum _PXConfigBackend { + PX_CONFIG_BACKEND_AUTO = 0, + PX_CONFIG_BACKEND_NONE = 0, + PX_CONFIG_BACKEND_WIN32 = (1 << 0), + PX_CONFIG_BACKEND_KCONFIG = (1 << 1), + PX_CONFIG_BACKEND_GCONF = (1 << 2), + PX_CONFIG_BACKEND_USER = (1 << 3), + PX_CONFIG_BACKEND_SYSTEM = (1 << 4), + PX_CONFIG_BACKEND_ENVVAR = (1 << 5), + PX_CONFIG_BACKEND__LAST = PX_CONFIG_BACKEND_ENVVAR +}; + +typedef struct _PXProxyFactory PXProxyFactory; +typedef enum _PXConfigBackend PXConfigBackend; + +PXProxyFactory *px_proxy_factory_new (PXConfigBackend config); +char *px_proxy_factory_get_proxy(PXProxyFactory *self, char *url); +void px_proxy_factory_free (PXProxyFactory *self); diff --git a/src/proxy_factory.c b/src/proxy_factory.c new file mode 100644 index 0000000..0dffc94 --- /dev/null +++ b/src/proxy_factory.c @@ -0,0 +1,256 @@ +#include "proxy_plugin.h" + +#include <stdlib.h> +#include <assert.h> +#include <string.h> +#include <sys/types.h> +#include <dirent.h> +#include <stdio.h> +#include <dlfcn.h> +#include <math.h> + +#ifdef WIN32 +# define PROXY_IMPORT __declspec(dllimport) +# define PROXY_EXPORT __declspec(dllexport) +#else +# define PROXY_IMPORT __attribute__((visibility("default"))) +# define PROXY_EXPORT __attribute__((visibility("default"))) +#endif + +#define PLUGIN_DIR "/usr/lib/proxy/" VERSION "/plugins" + +struct _PXProxyFactory { + PXConfigBackend mask; + void **plugins; + PXProxyFactoryCallback *configs; + PXProxyFactoryCallback *on_get_proxy; + PXPACRunnerCallback *pac_runners; +}; + +static void *proxy_malloc (size_t size) +{ + void *tmp = malloc(size); + assert(tmp != NULL); + memset(tmp, 0, size); + return tmp; +} + +static void proxy_free (void *mem) +{ + if (!mem) return; + free(mem); +} + +PROXY_EXPORT PXProxyFactory *px_proxy_factory_new (PXConfigBackend config) +{ + PXProxyFactory *self = proxy_malloc(sizeof(PXProxyFactory)); + self->configs = proxy_malloc(sizeof(PXProxyFactoryCallback) * (((int) log2(PX_CONFIG_BACKEND__LAST)) + 1)); + self->mask = config; + unsigned int i; + + // Open the plugin dir + DIR *plugindir = opendir(PLUGIN_DIR); + if (!plugindir) + { + px_proxy_factory_free(self); + return NULL; + } + + // Count the number of plugins + for (i=0 ; readdir(plugindir) ; i++); + self->plugins = (void **) proxy_malloc(sizeof(void *) * (i + 1)); + rewinddir(plugindir); + + // For each plugin... + struct dirent *ent; + for (i=0 ; ent = readdir(plugindir) ; i++) + { + // Load the plugin + char *tmp = proxy_malloc(strlen(PLUGIN_DIR) + strlen(ent->d_name) + 2); + sprintf(tmp, PLUGIN_DIR "/%s", ent->d_name); + self->plugins[i] = dlopen(tmp, RTLD_LOCAL); + proxy_free(tmp); + if (!(self->plugins[i])) + { + i--; + continue; + } + + // Call the instantiation hook + PXProxyFactoryCallback instantiate; + instantiate = dlsym(self->plugins[i], "on_proxyfactory_instantiate"); + if (instantiate) + instantiate(self); + } + closedir(plugindir); + + // Make sure we have at least one config + if (px_proxy_factory_config_get_active(self) == PX_CONFIG_BACKEND_NONE) + { + px_proxy_factory_free(self); + return NULL; + } + + return self; +} + +PROXY_EXPORT void px_proxy_factory_config_set (PXProxyFactory *self, PXConfigBackend config, PXProxyFactoryCallback callback) +{ + self->configs[((int) log2(config))] = callback; +} + +PROXY_EXPORT PXConfigBackend px_proxy_factory_config_get_active (PXProxyFactory *self) +{ + // Find the first provided config + for (unsigned int i=0 ; i <= ((int) log2(PX_CONFIG_BACKEND__LAST)) ; i++) + if (self->configs[i]) + return (1 << i); + + // No valid config found! + return PX_CONFIG_BACKEND_NONE; +} + +PROXY_EXPORT char *px_proxy_factory_get_proxy (PXProxyFactory *self, char *url) +{ + return NULL; +} + +PROXY_EXPORT void px_proxy_factory_on_get_proxy_add (PXProxyFactory *self, PXProxyFactoryCallback callback) +{ + unsigned int length = 0; + PXProxyFactoryCallback *tmp = NULL; + + // Backup our current callbacks (NULL if none) + tmp = self->on_get_proxy; + + // If we have any callbacks, count them + // If user is trying to add an already existing callback, just ignore it + if (self->on_get_proxy) + for (length=0 ; self->on_get_proxy[length] ; length++) + if (callback == self->on_get_proxy[length]) + return; + + // Allocate enough space for the old + new callbacks + self->on_get_proxy = proxy_malloc(sizeof(PXProxyFactoryCallback) * (length + 2)); + + // Copy old callbacks into new memory (if any) + if (tmp) + memcpy(self->on_get_proxy, tmp, length); + + // Free old memory (if any) + proxy_free(tmp); +} + +PROXY_EXPORT void px_proxy_factory_on_get_proxy_del (PXProxyFactory *self, PXProxyFactoryCallback callback) +{ + unsigned int length = 0, count = 0; + PXProxyFactoryCallback *tmp = NULL; + + // Sanity checks, get length and count + if (!self->on_get_proxy) return; + for (length=0,count=0 ; self->on_get_proxy[length] ; length++) + if (self->on_get_proxy[length] == callback) + count++; + if (length < 1 || count < 1) return; + + // Backup current callbacks + tmp = self->on_get_proxy; + + // Allocate new array + self->on_get_proxy = proxy_malloc(sizeof(PXProxyFactoryCallback) * (length - count + 1)); + + // Copy in new values + int i,j; + for (i=0,j=0 ; tmp[i] ; i++) + if (tmp[i] != callback) + self->on_get_proxy[j++] = tmp[i]; + + // Free old memory + proxy_free(tmp); +} + +PROXY_EXPORT void px_proxy_factory_pac_runner_add (PXProxyFactory *self, PXPACRunnerCallback callback) +{ + unsigned int length = 0; + PXPACRunnerCallback *tmp = NULL; + + // Backup our current callbacks (NULL if none) + tmp = self->pac_runners; + + // If we have any callbacks, count them + // If user is trying to add an already existing callback, just ignore it + if (self->pac_runners) + for (length=0 ; self->pac_runners[length] ; length++) + if (callback == self->pac_runners[length]) + return; + + // Allocate enough space for the old + new callbacks + self->pac_runners = proxy_malloc(sizeof(PXPACRunnerCallback) * (length + 2)); + + // Copy old callbacks into new memory (if any) + if (tmp) + memcpy(self->pac_runners, tmp, length); + + // Free old memory (if any) + proxy_free(tmp); +} + +PROXY_EXPORT void px_proxy_factory_pac_runner_del (PXProxyFactory *self, PXPACRunnerCallback callback) +{ + unsigned int length = 0, count = 0; + PXPACRunnerCallback *tmp = NULL; + + // Sanity checks, get length and count + if (!self->pac_runners) return; + for (length=0,count=0 ; self->pac_runners[length] ; length++) + if (self->pac_runners[length] == callback) + count++; + if (length < 1 || count < 1) return; + + // Backup current callbacks + tmp = self->pac_runners; + + // Allocate new array + self->pac_runners = proxy_malloc(sizeof(PXPACRunnerCallback) * (length - count + 1)); + + // Copy in new values + int i,j; + for (i=0,j=0 ; tmp[i] ; i++) + if (tmp[i] != callback) + self->pac_runners[j++] = tmp[i]; + + // Free old memory + proxy_free(tmp); +} + +PROXY_EXPORT void px_proxy_factory_free (PXProxyFactory *self) +{ + unsigned int i; + + if (!self) return; + + // Free the plugins + if (self->plugins) + { + for (i=0 ; self->plugins[i] ; i++) + { + // Call the destantiation hook + PXProxyFactoryCallback destantiate; + destantiate = dlsym(self->plugins[i], "on_proxyfactory_destantiate"); + if (destantiate) + destantiate(self); + + // Unload the plugin + dlclose(self->plugins[i]); + self->plugins[i] = NULL; + } + proxy_free(self->plugins); + } + + // Free everything else + if (self->configs) proxy_free(self->configs); + if (self->on_get_proxy) proxy_free(self->on_get_proxy); + if (self->pac_runners) proxy_free(self->pac_runners); + proxy_free(self); +} + diff --git a/src/proxy_plugin.h b/src/proxy_plugin.h new file mode 100644 index 0000000..5d87b67 --- /dev/null +++ b/src/proxy_plugin.h @@ -0,0 +1,27 @@ +#include "proxy.h" + +// URLs look like this: +// http://host:port +// socks://host:port +// pac+http://pac_host:port/path/to/pac +// wpad:// +// direct:// +// TODO: ignore syntax TBD +struct _PXConfig { + char *url; + char *ignore; +}; +typedef struct _PXConfig PXConfig; + +typedef void *(*PXProxyFactoryCallback) (PXProxyFactory *self); +typedef char *(*PXPACRunnerCallback) (PXProxyFactory *self, const char *pac, const char *url, const char *hostname); + +void px_proxy_factory_config_set (PXProxyFactory *self, PXConfigBackend config, PXProxyFactoryCallback callback); +void px_proxy_factory_config_changed (PXProxyFactory *self); +PXConfigBackend px_proxy_factory_config_get_active(PXProxyFactory *self); + +void px_proxy_factory_network_changed (PXProxyFactory *self); +void px_proxy_factory_on_get_proxy_add (PXProxyFactory *self, PXProxyFactoryCallback callback); +void px_proxy_factory_on_get_proxy_del (PXProxyFactory *self, PXProxyFactoryCallback callback); +void px_proxy_factory_pac_runner_add (PXProxyFactory *self, PXPACRunnerCallback callback); +void px_proxy_factory_pac_runner_del (PXProxyFactory *self, PXPACRunnerCallback callback); |