1
0
mirror of https://github.com/nmap/nmap.git synced 2025-12-27 09:59:04 +00:00

Refactored the proxy object model to enforce speration between operations and data.

This commit is contained in:
henri
2013-04-22 19:36:47 +00:00
parent 03ff0651c7
commit c8f7d4b7ac
4 changed files with 57 additions and 54 deletions

View File

@@ -78,13 +78,13 @@ static void proxy_parser_delete(struct proxy_parser *parser);
/* --- Implemented proxy backends --- */
extern const struct proxy_op proxy_http_ops;
extern const struct proxy_op proxy_socks4_ops;
extern const struct proxy_spec ProxySpecHttp;
extern const struct proxy_spec ProxySpecSocks4;
const static struct proxy_op *ProxyBackends[] = {
&proxy_http_ops,
&proxy_socks4_ops,
const static struct proxy_spec *ProxyBackends[] = {
&ProxySpecHttp,
&ProxySpecSocks4,
NULL
};
@@ -129,7 +129,7 @@ void nsock_proxychain_delete(nsock_proxychain chain) {
struct proxy_node *node;
while ((node = (struct proxy_node *)gh_list_pop(&pchain->nodes)) != NULL) {
node->ops->node_delete(node);
node->spec->ops->node_delete(node);
}
gh_list_free(&pchain->nodes);
@@ -364,10 +364,10 @@ static struct proxy_node *proxy_node_new(char *proxystr) {
int i;
for (i = 0; ProxyBackends[i] != NULL; i++) {
const struct proxy_op *pxop;
const struct proxy_spec *pspec;
pxop = ProxyBackends[i];
if (strncasecmp(proxystr, pxop->prefix, strlen(pxop->prefix)) == 0) {
pspec = ProxyBackends[i];
if (strncasecmp(proxystr, pspec->prefix, strlen(pspec->prefix)) == 0) {
struct proxy_node *proxy = NULL;
struct uri uri;
@@ -376,7 +376,7 @@ static struct proxy_node *proxy_node_new(char *proxystr) {
if (parse_uri(proxystr, &uri) < 0)
break;
if (pxop->node_new(&proxy, &uri) < 0)
if (pspec->ops->node_new(&proxy, &uri) < 0)
proxy = NULL;
uri_free(&uri);
@@ -454,7 +454,7 @@ void nsock_proxy_ev_dispatch(nsock_pool nspool, nsock_event nsevent, void *udata
current = proxy_ctx_node_current(nse->iod->px_ctx);
assert(current);
current->ops->handler(nspool, nsevent, udata);
current->spec->ops->handler(nspool, nsevent, udata);
} else {
forward_event(nspool, nsevent, udata);
}

View File

@@ -60,6 +60,7 @@
#include "gh_list.h"
#include <nsock.h>
#include <errno.h>
/* ------------------- CONSTANTS ------------------- */
@@ -98,9 +99,7 @@ struct uri {
* parsing the proxy specification string given by user. Those structures are
* then read-only and stored in the nsock_pool. */
struct proxy_node {
enum nsock_proxy_type px_type;
const struct proxy_op *ops;
const struct proxy_spec *spec;
struct sockaddr_storage ss;
size_t sslen;
@@ -134,13 +133,17 @@ struct proxy_chain_context {
};
struct proxy_op {
const char *prefix;
enum nsock_proxy_type type;
int (*node_new)(struct proxy_node **node, const struct uri *uri);
void (*node_delete)(struct proxy_node *node);
void (*handler)(nsock_pool nspool, nsock_event nsevent, void *udata);
};
struct proxy_spec {
const char *prefix;
enum nsock_proxy_type type;
const struct proxy_op *ops;
};
/* ------------------- UTIL FUNCTIONS ------------------- */
int proxy_resolve(const char *host, struct sockaddr *addr, size_t *addrlen);

View File

@@ -69,28 +69,15 @@
extern struct timeval nsock_tod;
/* ---- PROTOTYPES ---- */
static int proxy_http_node_new(struct proxy_node **node, const struct uri *uri);
static void proxy_http_node_delete(struct proxy_node *node);
static void proxy_http_handler(nsock_pool nspool, nsock_event nsevent, void *udata);
const struct proxy_spec ProxySpecHttp;
/* ---- PROXY DEFINITION ---- */
const struct proxy_op proxy_http_ops = {
.prefix = "http://",
.type = PROXY_TYPE_HTTP,
.node_new = proxy_http_node_new,
.node_delete = proxy_http_node_delete,
.handler = proxy_http_handler,
};
int proxy_http_node_new(struct proxy_node **node, const struct uri *uri) {
static int proxy_http_node_new(struct proxy_node **node, const struct uri *uri) {
int rc;
struct proxy_node *proxy;
proxy = (struct proxy_node *)safe_zalloc(sizeof(struct proxy_node));
proxy->ops = &proxy_http_ops;
proxy->spec = &ProxySpecHttp;
rc = proxy_resolve(uri->host, (struct sockaddr *)&proxy->ss, &proxy->sslen);
if (rc < 0) {
@@ -116,7 +103,7 @@ int proxy_http_node_new(struct proxy_node **node, const struct uri *uri) {
return 1;
}
void proxy_http_node_delete(struct proxy_node *node) {
static void proxy_http_node_delete(struct proxy_node *node) {
if (!node)
return;
@@ -187,7 +174,7 @@ static int handle_state_tcp_connected(mspool *nsp, msevent *nse, void *udata) {
return 0;
}
void proxy_http_handler(nsock_pool nspool, nsock_event nsevent, void *udata) {
static void proxy_http_handler(nsock_pool nspool, nsock_event nsevent, void *udata) {
int rc = 0;
mspool *nsp = (mspool *)nspool;
msevent *nse = (msevent *)nsevent;
@@ -216,3 +203,17 @@ void proxy_http_handler(nsock_pool nspool, nsock_event nsevent, void *udata) {
}
}
/* ---- PROXY DEFINITION ---- */
static const struct proxy_op ProxyOpsHttp = {
.node_new = proxy_http_node_new,
.node_delete = proxy_http_node_delete,
.handler = proxy_http_handler,
};
const struct proxy_spec ProxySpecHttp = {
.prefix = "http://",
.type = PROXY_TYPE_HTTP,
.ops = &ProxyOpsHttp,
};

View File

@@ -70,6 +70,8 @@
extern struct timeval nsock_tod;
const struct proxy_spec ProxySpecSocks4;
struct socks4_data {
char version;
char type;
@@ -79,28 +81,12 @@ struct socks4_data {
} __attribute__((packed));
/* ---- PROTOTYPES ---- */
static int proxy_socks4_node_new(struct proxy_node **node, const struct uri *uri);
static void proxy_socks4_node_delete(struct proxy_node *node);
static void proxy_socks4_handler(nsock_pool nspool, nsock_event nsevent, void *udata);
/* ---- PROXY DEFINITION ---- */
const struct proxy_op proxy_socks4_ops = {
.prefix = "socks4://",
.type = PROXY_TYPE_SOCKS4,
.node_new = proxy_socks4_node_new,
.node_delete = proxy_socks4_node_delete,
.handler = proxy_socks4_handler,
};
int proxy_socks4_node_new(struct proxy_node **node, const struct uri *uri) {
static int proxy_socks4_node_new(struct proxy_node **node, const struct uri *uri) {
int rc;
struct proxy_node *proxy;
proxy = (struct proxy_node *)safe_zalloc(sizeof(struct proxy_node));
proxy->ops = &proxy_socks4_ops;
proxy->spec = &ProxySpecSocks4;
rc = proxy_resolve(uri->host, (struct sockaddr *)&proxy->ss, &proxy->sslen);
if (rc < 0)
@@ -134,7 +120,7 @@ err_out:
return rc;
}
void proxy_socks4_node_delete(struct proxy_node *node) {
static void proxy_socks4_node_delete(struct proxy_node *node) {
if (!node)
return;
@@ -219,7 +205,7 @@ static int handle_state_tcp_connected(mspool *nsp, msevent *nse, void *udata) {
return 0;
}
void proxy_socks4_handler(nsock_pool nspool, nsock_event nsevent, void *udata) {
static void proxy_socks4_handler(nsock_pool nspool, nsock_event nsevent, void *udata) {
int rc = 0;
mspool *nsp = (mspool *)nspool;
msevent *nse = (msevent *)nsevent;
@@ -248,3 +234,16 @@ void proxy_socks4_handler(nsock_pool nspool, nsock_event nsevent, void *udata) {
}
}
/* ---- PROXY DEFINITION ---- */
static const struct proxy_op ProxyOpsSocks4 = {
.node_new = proxy_socks4_node_new,
.node_delete = proxy_socks4_node_delete,
.handler = proxy_socks4_handler,
};
const struct proxy_spec ProxySpecSocks4 = {
.prefix = "socks4://",
.type = PROXY_TYPE_SOCKS4,
.ops = &ProxyOpsSocks4,
};