diff options
Diffstat (limited to 'src/net-proxy.c')
-rw-r--r-- | src/net-proxy.c | 75 |
1 files changed, 55 insertions, 20 deletions
diff --git a/src/net-proxy.c b/src/net-proxy.c index 42bcc65..d264ad7 100644 --- a/src/net-proxy.c +++ b/src/net-proxy.c @@ -1,5 +1,8 @@ #include <errno.h> +#include <sys/types.h> +#include <sys/socket.h> +#include <netdb.h> #include "intf.h" #include "debug.h" @@ -43,9 +46,9 @@ int tsnif_np_init(struct tsnif_handle *h, struct tsnif_np_args *np) return 0; memset(nph, 0x0, sizeof(*nph)); - nph->flags = flags; nph->fd_udp = -1; nph->fd_tcp = -1; + nph->args = np; if (flags & TSNIF_NP_CLIENT) { @@ -70,11 +73,11 @@ int tsnif_np_fd(struct tsnif_handle *h, fd_set *set) int fd_udp = nph->fd_udp; int fd_tcp = nph->fd_tcp; - if ((TSNIF_NP_UDP & nph->flags) && + if ((TSNIF_NP_UDP & TSNIF_NP_FLAGS(nph)) && (-1 != fd_udp)) FD_SET(fd_udp, set); - if ((TSNIF_NP_TCP & nph->flags) && + if ((TSNIF_NP_TCP & TSNIF_NP_FLAGS(nph)) && (-1 != fd_tcp)) FD_SET(fd_tcp, set); @@ -86,7 +89,7 @@ int tsnif_np_process(struct tsnif_handle *h, fd_set *set) struct tsnif_np_handle *nph = &h->np; int ret = 0; - if ((TSNIF_NP_UDP & nph->flags) && + if ((TSNIF_NP_UDP & TSNIF_NP_FLAGS(nph)) && FD_ISSET(nph->fd_udp, set)) ret = UDP->process(h); @@ -97,7 +100,7 @@ int tsnif_np_close(struct tsnif_handle *h) { struct tsnif_np_handle *nph = &h->np; - if (TSNIF_NP_UDP & nph->flags) + if (TSNIF_NP_UDP & TSNIF_NP_FLAGS(nph)) UDP->close(h); return 0; @@ -109,13 +112,13 @@ int tsnif_np_send_client(struct tsnif_handle *h, struct trans_msg *msg) int ret = 0; /* pro bile, cernochu!!! ;) */ - if (!(TSNIF_NP_CLIENT & nph->flags)) + if (!(TSNIF_NP_CLIENT & TSNIF_NP_FLAGS(nph))) return 0; - if (TSNIF_NP_UDP & nph->flags) + if (TSNIF_NP_UDP & TSNIF_NP_FLAGS(nph)) ret = UDP->send(h, msg); - return ret; + return ret > 0 ? 0 : ret; } int tsnif_np_send_server(struct tsnif_handle *h, struct trans_msg *msg) @@ -123,13 +126,13 @@ int tsnif_np_send_server(struct tsnif_handle *h, struct trans_msg *msg) struct tsnif_np_handle *nph = &h->np; int ret = 0; - if (!(TSNIF_NP_SERVER & nph->flags)) + if (!(TSNIF_NP_SERVER & TSNIF_NP_FLAGS(nph))) return 0; - if (TSNIF_NP_UDP & nph->flags) + if (TSNIF_NP_UDP & TSNIF_NP_FLAGS(nph)) ret = UDP->send(h, msg); - return ret; + return ret > 0 ? 0 : ret; } static int dispatch_server_attach(struct tsnif_handle *h, struct trans_msg *msg) @@ -147,7 +150,7 @@ static int dispatch_server_attach(struct tsnif_handle *h, struct trans_msg *msg) return tsnif_np_send_server(h, msg); } - if (nph->flags & TSNIF_NP_UDP) + if (TSNIF_NP_UDP & TSNIF_NP_FLAGS(nph)) nph->udp_client.term = term; TSNIF_DEBUG(NP, "term attached\n"); @@ -172,7 +175,7 @@ static int dispatch_server_detach(struct tsnif_handle *h, struct trans_msg *msg) return tsnif_np_send_server(h, msg); } - if (nph->flags & TSNIF_NP_UDP) + if (TSNIF_NP_UDP & TSNIF_NP_FLAGS(nph)) memset(&nph->udp_client, 0x0, sizeof(nph->udp_client)); TSNIF_DEBUG(NP, "term detached\n"); @@ -192,7 +195,7 @@ static int dispatch_server(struct tsnif_handle *h, struct trans_msg *msg) struct tsnif_np_handle *nph = &h->np; /* is there any client */ - if ((nph->flags & TSNIF_NP_UDP) && + if ((TSNIF_NP_UDP & TSNIF_NP_FLAGS(nph)) && (!nph->udp_client.connected)) return 0; @@ -217,16 +220,16 @@ int tsnif_np_dispatch(struct tsnif_handle *h, struct trans_msg *msg) { struct tsnif_np_handle *nph = &h->np; - if (nph->flags & TSNIF_NP_CLIENT) + if (TSNIF_NP_CLIENT & TSNIF_NP_FLAGS(nph)) return tsnif_dispatch(h, msg); - if (nph->flags & TSNIF_NP_SERVER) + if (TSNIF_NP_SERVER & TSNIF_NP_FLAGS(nph)) return dispatch_server(h, msg); return 0; } -/* XXX - finish parsing +/* * host is expected to be int format: * * hostname (default port is used) @@ -235,9 +238,41 @@ int tsnif_np_dispatch(struct tsnif_handle *h, struct trans_msg *msg) * ip:port * */ -void tsnif_np_args(struct tsnif_np_args *args, int flag, char *host) +int tsnif_np_args(struct tsnif_np_args *args, int flag, char *host) { + char *port; + struct addrinfo *res, *rp; + int ret; + +#define NP_STRINGIFY(macro_or_string) NP_STRINGIFY_ARG (macro_or_string) +#define NP_STRINGIFY_ARG(contents) #contents + + port = strchr(host, ':'); + if (port) + *port++ = 0x0; + else + port = NP_STRINGIFY(TSNIF_NP_PORT); + + ret = getaddrinfo(host, port, NULL, &res); + if (ret) { + printf("resolve failed: %s\n", gai_strerror(ret)); + return -1; + } + + /* IPv4 so far.. */ + for (rp = res; rp != NULL; rp = rp->ai_next) { + if (rp->ai_family == AF_INET) + break; + } + + if (!rp) { + freeaddrinfo(res); + return -1; + } + + memcpy(&args->sa, rp->ai_addr, sizeof(args->sa)); args->flags = flag; - args->host = host; - args->port = TSNIF_NP_PORT; + + freeaddrinfo(res); + return 0; } |