diff options
author | Jiri Olsa <Jiri Olsa jolsa@redhat.com> | 2010-06-10 13:47:57 +0200 |
---|---|---|
committer | Jiri Olsa <Jiri Olsa jolsa@redhat.com> | 2010-06-10 13:47:57 +0200 |
commit | af10fc91903524328a3bb81daa8ac2cfb802a012 (patch) | |
tree | f0ef5ae2e245e7922f1ff1b25159e385c0cddf33 | |
parent | 8bbcc6562aec27abe13d87b12010a01e0c36cf12 (diff) | |
download | tsnif-net-proxy.tar.gz tsnif-net-proxy.tar.xz tsnif-net-proxy.zip |
initial change 4 - address resolvingnet-proxy
-rw-r--r-- | src/net-proxy-udp.c | 17 | ||||
-rw-r--r-- | src/net-proxy.c | 75 | ||||
-rw-r--r-- | src/net-proxy.h | 10 | ||||
-rw-r--r-- | src/tsnif.c | 8 | ||||
-rw-r--r-- | src/tsnifd.c | 8 |
5 files changed, 78 insertions, 40 deletions
diff --git a/src/net-proxy-udp.c b/src/net-proxy-udp.c index 996b793..70230fd 100644 --- a/src/net-proxy-udp.c +++ b/src/net-proxy-udp.c @@ -10,7 +10,7 @@ static int init_fd(struct tsnif_handle *h, struct tsnif_np_args *args, int server) { struct tsnif_np_handle *nph = &h->np; - struct sockaddr_in sa; + struct sockaddr_in *sa = &args->sa; int fd; fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); @@ -22,15 +22,9 @@ static int init_fd(struct tsnif_handle *h, struct tsnif_np_args *args, int serve nph->fd_udp = fd; if (server) { - memset((char *) &sa, 0, sizeof(sa)); - sa.sin_family = AF_INET; - sa.sin_port = htons(args->port); - /* XXX finish given host resolving */ - sa.sin_addr.s_addr = htonl(INADDR_ANY); - - if (-1 == bind(fd, &sa, sizeof(sa))) { + if (-1 == bind(fd, sa, sizeof(*sa))) { close(fd); - TSNIF_DEBUG(NP, "failed to bind to %d\n", args->port); + TSNIF_DEBUG(NP, "failed to bind\n"); return -1; } } @@ -80,11 +74,12 @@ int udp_send(struct tsnif_handle *h, struct trans_msg *msg) if (TSNIF_CMD_DATA == msg->cmd) memcpy(m->data, msg->data.ptr, msg->data.len); - if (TSNIF_NP_UDP_SERVER & nph->flags) + if (TSNIF_NP_UDP_SERVER & TSNIF_NP_FLAGS(nph)) return sendto(nph->fd_udp, m, size, 0, &nph->udp_client.sa, sizeof(nph->udp_client.sa)); - return send(nph->fd_udp, m, size, 0); + return sendto(nph->fd_udp, m, size, 0, + &nph->args->sa, sizeof(nph->args->sa)); } int udp_process(struct tsnif_handle *h) 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; } diff --git a/src/net-proxy.h b/src/net-proxy.h index 27d7124..4fb54d9 100644 --- a/src/net-proxy.h +++ b/src/net-proxy.h @@ -27,8 +27,7 @@ enum { #define TSNIF_NP_TCP (TSNIF_NP_TCP_CLIENT | TSNIF_NP_TCP_SERVER) struct tsnif_np_args { - char *host; - int port; + struct sockaddr_in sa; int flags; }; @@ -45,10 +44,11 @@ struct tsnif_np_handle { /* tcp properties */ int fd_tcp; - - int flags; + struct tsnif_np_args *args; }; +#define TSNIF_NP_FLAGS(h) (h->args->flags) + struct tsnif_np_msg { struct trans_msg msg; char data[0]; @@ -76,7 +76,7 @@ int tsnif_np_process(struct tsnif_handle *h, fd_set *set); int tsnif_np_close(struct tsnif_handle *h); int tsnif_np_send_client(struct tsnif_handle *h, struct trans_msg *msg); int tsnif_np_send_server(struct tsnif_handle *h, struct trans_msg *msg); -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); int tsnif_np_dispatch(struct tsnif_handle *h, struct trans_msg *msg); #endif /* NET_PROXY_H */ diff --git a/src/tsnif.c b/src/tsnif.c index 56a26fd..65256e8 100644 --- a/src/tsnif.c +++ b/src/tsnif.c @@ -174,11 +174,15 @@ static int get_args(int argc, char **argv) break; case 'u': - tsnif_np_args(&np_args, TSNIF_NP_UDP_CLIENT, optarg); + if (tsnif_np_args(&np_args, + TSNIF_NP_UDP_CLIENT, optarg)) + return -1; break; case 'T': - tsnif_np_args(&np_args, TSNIF_NP_TCP_CLIENT, optarg); + if (tsnif_np_args(&np_args, + TSNIF_NP_TCP_CLIENT, optarg)) + return -1; break; case 'V': diff --git a/src/tsnifd.c b/src/tsnifd.c index 270cba9..a69d40e 100644 --- a/src/tsnifd.c +++ b/src/tsnifd.c @@ -186,11 +186,15 @@ static int get_args(int argc, char **argv) break; case 'u': - tsnif_np_args(&np_args, TSNIF_NP_UDP_SERVER, optarg); + if (tsnif_np_args(&np_args, + TSNIF_NP_UDP_SERVER, optarg)) + return -1; break; case 't': - tsnif_np_args(&np_args, TSNIF_NP_TCP_SERVER, optarg); + if (tsnif_np_args(&np_args, + TSNIF_NP_TCP_SERVER, optarg)) + return -1; break; case 'V': |