diff options
author | Jiri Olsa <Jiri Olsa jolsa@redhat.com> | 2010-06-09 14:02:25 +0200 |
---|---|---|
committer | Jiri Olsa <Jiri Olsa jolsa@redhat.com> | 2010-06-09 14:02:25 +0200 |
commit | 8bbcc6562aec27abe13d87b12010a01e0c36cf12 (patch) | |
tree | eae7b35625f435eee976728ceeb90cabd1294d8d | |
parent | 2e22274937feb3b2a23f7d302f42b43886341ee5 (diff) | |
download | tsnif-8bbcc6562aec27abe13d87b12010a01e0c36cf12.tar.gz tsnif-8bbcc6562aec27abe13d87b12010a01e0c36cf12.tar.xz tsnif-8bbcc6562aec27abe13d87b12010a01e0c36cf12.zip |
initial change 3 - compile, server is running
needs continue on address client resolving
-rw-r--r-- | src/debug.h | 5 | ||||
-rw-r--r-- | src/intf.c | 14 | ||||
-rw-r--r-- | src/intf.h | 1 | ||||
-rw-r--r-- | src/net-proxy-udp.c | 66 | ||||
-rw-r--r-- | src/net-proxy.c | 106 | ||||
-rw-r--r-- | src/net-proxy.h | 18 | ||||
-rw-r--r-- | src/tsnif.c | 4 |
7 files changed, 176 insertions, 38 deletions
diff --git a/src/debug.h b/src/debug.h index 6e29935..ae4ace2 100644 --- a/src/debug.h +++ b/src/debug.h @@ -18,12 +18,14 @@ enum { DEBUG_INTF = BIT2NUM(2), DEBUG_TRANS = BIT2NUM(3), DEBUG_APP = BIT2NUM(4), - FLAGS_MAX = 5, + DEBUG_NP = BIT2NUM(5), + FLAGS_MAX = 6, VERBOSE_STORAGE = BIT2NUM(16), VERBOSE_FSM = BIT2NUM(17), VERBOSE_INTF = BIT2NUM(18), VERBOSE_TRANS = BIT2NUM(19), VERBOSE_APP = BIT2NUM(20), + VERBOSE_NP = BIT2NUM(21), }; static char *debug_flag_name[FLAGS_MAX] = { @@ -32,6 +34,7 @@ static char *debug_flag_name[FLAGS_MAX] = { "intf", "trans", "app", + "np", }; static inline void debug_print(int flag, char *file, int line, @@ -6,7 +6,7 @@ #include "fsm.h" #include "debug.h" -static struct tsnif_term *term_find(struct tsnif_handle *h, int type, int idx) +struct tsnif_term *term_find(struct tsnif_handle *h, int type, int idx) { struct tsnif_term *t; @@ -52,9 +52,6 @@ int tsnif_dispatch(struct tsnif_handle *handle, struct trans_msg *msg) TSNIF_DEBUG(INTF, "got cmd %d, type %d, idx %d\n", msg->cmd, msg->type, msg->idx); - /* XXX error checking */ - tsnif_np_dispatch(handle, msg); - /* do we have this term */ term = term_find(handle, msg->type, msg->idx); @@ -90,6 +87,9 @@ static int trans_cb(struct trans_handle *h, struct trans_msg *msg) handle = container_of(h, struct tsnif_handle, trans); + /* XXX error checking */ + tsnif_np_dispatch(handle, msg); + return tsnif_dispatch(handle, msg); } @@ -213,7 +213,7 @@ int tsnif_attach(struct tsnif_term *term) msg.idx = term->idx; msg.cmd = TSNIF_CMD_ATTACH; - ret = tsnif_np_send(h, &msg); + ret = tsnif_np_send_client(h, &msg); if (h->np_only) return ret; @@ -238,7 +238,7 @@ int tsnif_detach(struct tsnif_term *term) msg.idx = term->idx; msg.cmd = TSNIF_CMD_DETACH; - ret = tsnif_np_send(h, &msg); + ret = tsnif_np_send_client(h, &msg); if (h->np_only) return ret; @@ -255,7 +255,7 @@ int tsnif_list(struct tsnif_handle *h) memset(&msg, 0x0, sizeof(msg)); msg.cmd = TSNIF_CMD_TTY_LIST; - ret = tsnif_np_send(h, &msg); + ret = tsnif_np_send_client(h, &msg); if (h->np_only) return ret; @@ -80,6 +80,7 @@ int tsnif_term_del(struct tsnif_handle *h, struct tsnif_term *term); int tsnif_attach(struct tsnif_term *term); int tsnif_detach(struct tsnif_term *term); int tsnif_dispatch(struct tsnif_handle *h, struct trans_msg *msg); +struct tsnif_term *term_find(struct tsnif_handle *h, int type, int idx); #define tsnif_for_each(term, t, handle) \ list_for_each_entry_safe(term, t, (&handle->terms), list) diff --git a/src/net-proxy-udp.c b/src/net-proxy-udp.c index aa0a6d4..996b793 100644 --- a/src/net-proxy-udp.c +++ b/src/net-proxy-udp.c @@ -5,47 +5,57 @@ #include "intf.h" #include "net-proxy.h" +#include "debug.h" -static int init_fd(struct tsnif_handle *h, struct tsnif_np_args *args) +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; - int fd, ret; + int fd; fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); - if (-1 == fd) - return -1; - - 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); - - ret = bind(fd, &sa, sizeof(sa)); - if (-1 == ret) { - close(fd); + if (-1 == fd) { + TSNIF_DEBUG(NP, "failed to create udp socket\n"); return -1; } nph->fd_udp = fd; - return 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))) { + close(fd); + TSNIF_DEBUG(NP, "failed to bind to %d\n", args->port); + return -1; + } + } + + TSNIF_DEBUG(NP, "socket created %d\n", fd); + return 0; } int udp_init_server(struct tsnif_handle *h, struct tsnif_np_args *args) { - return init_fd(h, args); + TSNIF_DEBUG(NP, "creating server socket\n"); + return init_fd(h, args, 1); } int udp_init_client(struct tsnif_handle *h, struct tsnif_np_args *args) { - return init_fd(h, args); + TSNIF_DEBUG(NP, "creating client socket\n"); + return init_fd(h, args, 0); } int udp_close(struct tsnif_handle *h) { struct tsnif_np_handle *nph = &h->np; + + TSNIF_DEBUG(NP, "closing client socket\n"); close(nph->fd_udp); return 0; } @@ -56,6 +66,8 @@ int udp_send(struct tsnif_handle *h, struct trans_msg *msg) struct tsnif_np_handle *nph = &h->np; int size = sizeof(*msg); + TSNIF_DEBUG(NP, "sending cmd = %d\n", msg->cmd); + if (TSNIF_CMD_DATA == msg->cmd) size += msg->data.len; @@ -68,6 +80,10 @@ 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) + 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); } @@ -77,11 +93,22 @@ int udp_process(struct tsnif_handle *h) struct trans_msg msg; char *data = NULL; int ret; + struct sockaddr_in sa; + socklen_t sl = sizeof(sa); - ret = recv(nph->fd_udp, &msg, sizeof(msg), 0); + ret = recvfrom(nph->fd_udp, &msg, sizeof(msg), 0, + &sa, &sl); if (ret <= 0) return ret; + /* just one client supported for now */ + if (memcmp(&nph->udp_client.sa, &sa, sizeof(sa))) { + TSNIF_DEBUG(NP, "got new client\n"); + memset(&nph->udp_client, 0x0, sizeof(nph->udp_client)); + nph->udp_client.sa = sa; + nph->udp_client.connected = 1; + } + /* receive the data part */ if (TSNIF_CMD_DATA == msg.cmd) { @@ -90,7 +117,8 @@ int udp_process(struct tsnif_handle *h) if (!data) return -1; - ret = recv(nph->fd_udp, data, msg.data.len, 0); + ret = recvfrom(nph->fd_udp, data, msg.data.len, 0, + &sa, &sl); if (ret <= 0) { free(data); return ret; diff --git a/src/net-proxy.c b/src/net-proxy.c index 20dc78b..42bcc65 100644 --- a/src/net-proxy.c +++ b/src/net-proxy.c @@ -2,6 +2,7 @@ #include <errno.h> #include "intf.h" +#include "debug.h" extern struct tsnif_np_prot udp_prot; extern struct tsnif_np_prot tcp_prot; @@ -32,6 +33,11 @@ int tsnif_np_init(struct tsnif_handle *h, struct tsnif_np_args *np) int flags = np->flags; struct tsnif_np_handle *nph = &h->np; + TSNIF_DEBUG(NP, "flags = %x, client %d, udp %d\n", + np ? np->flags : 0, + np ? np->flags & TSNIF_NP_CLIENT : 0, + np ? np->flags & TSNIF_NP_UDP : 0 ); + /* no proxy requested */ if (!np && (!np->flags)) return 0; @@ -54,6 +60,7 @@ int tsnif_np_init(struct tsnif_handle *h, struct tsnif_np_args *np) if (TSNIF_NP_UDP & flags) ret = udp_init(h, np, flags); + TSNIF_DEBUG(NP, "ret = %d\n", ret); return ret; } @@ -96,30 +103,114 @@ int tsnif_np_close(struct tsnif_handle *h) return 0; } -int tsnif_np_send(struct tsnif_handle *h, struct trans_msg *msg) +int tsnif_np_send_client(struct tsnif_handle *h, struct trans_msg *msg) +{ + struct tsnif_np_handle *nph = &h->np; + int ret = 0; + + /* pro bile, cernochu!!! ;) */ + if (!(TSNIF_NP_CLIENT & nph->flags)) + return 0; + + if (TSNIF_NP_UDP & nph->flags) + ret = UDP->send(h, msg); + + return ret; +} + +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)) + return 0; + if (TSNIF_NP_UDP & nph->flags) ret = UDP->send(h, msg); return ret; } -int tsnif_np_server_send(struct tsnif_handle *h, struct trans_msg *msg) +static int dispatch_server_attach(struct tsnif_handle *h, struct trans_msg *msg) +{ + struct tsnif_np_handle *nph = &h->np; + struct tsnif_term *term; + + TSNIF_DEBUG(NP, "type = %d, idx = %d\n", msg->type, msg->idx); + + term = term_find(h, msg->type, msg->idx); + if (!term) { + TSNIF_DEBUG(NP, "term not found\n"); + msg->err = 1; + msg->error = -EINVAL; + return tsnif_np_send_server(h, msg); + } + + if (nph->flags & TSNIF_NP_UDP) + nph->udp_client.term = term; + + TSNIF_DEBUG(NP, "term attached\n"); + + msg->ack = 1; + return tsnif_np_send_server(h, msg); +} + +static int dispatch_server_detach(struct tsnif_handle *h, struct trans_msg *msg) { struct tsnif_np_handle *nph = &h->np; + struct tsnif_term *term; + + TSNIF_DEBUG(NP, "type = %d, idx = %d\n", msg->type, msg->idx); + + term = term_find(h, msg->type, msg->idx); + if ((!term) || + (term != nph->udp_client.term)) { + TSNIF_DEBUG(NP, "term not found\n"); + msg->err = 1; + msg->error = -EINVAL; + return tsnif_np_send_server(h, msg); + } - if (TSNIF_NP_SERVER & nph->flags) - return tsnif_np_send(h, msg); + if (nph->flags & TSNIF_NP_UDP) + memset(&nph->udp_client, 0x0, sizeof(nph->udp_client)); + TSNIF_DEBUG(NP, "term detached\n"); + + msg->ack = 1; + return tsnif_np_send_server(h, msg); +} + +static int dispatch_server_list(struct tsnif_handle *h, struct trans_msg *msg) +{ return 0; } static int dispatch_server(struct tsnif_handle *h, struct trans_msg *msg) { - return 0; + int ret = -EINVAL; + struct tsnif_np_handle *nph = &h->np; + + /* is there any client */ + if ((nph->flags & TSNIF_NP_UDP) && + (!nph->udp_client.connected)) + return 0; + + switch(msg->cmd) { + case TSNIF_CMD_ATTACH: + ret = dispatch_server_attach(h, msg); + break; + + case TSNIF_CMD_DETACH: + ret = dispatch_server_detach(h, msg); + break; + + case TSNIF_CMD_TTY_LIST: + ret = dispatch_server_list(h, msg); + break; + } + + return ret; } int tsnif_np_dispatch(struct tsnif_handle *h, struct trans_msg *msg) @@ -129,7 +220,10 @@ int tsnif_np_dispatch(struct tsnif_handle *h, struct trans_msg *msg) if (nph->flags & TSNIF_NP_CLIENT) return tsnif_dispatch(h, msg); - return dispatch_server(h, msg); + if (nph->flags & TSNIF_NP_SERVER) + return dispatch_server(h, msg); + + return 0; } /* XXX - finish parsing diff --git a/src/net-proxy.h b/src/net-proxy.h index 5365a25..27d7124 100644 --- a/src/net-proxy.h +++ b/src/net-proxy.h @@ -3,6 +3,7 @@ #define NET_PROXY_H #include <sys/select.h> +#include <arpa/inet.h> #include "trans.h" @@ -31,9 +32,20 @@ struct tsnif_np_args { int flags; }; +struct tsnif_udp_client { + struct tsnif_term *term; + struct sockaddr_in sa; + int connected; +}; + struct tsnif_np_handle { + /* udp properties */ int fd_udp; + struct tsnif_udp_client udp_client; + + /* tcp properties */ int fd_tcp; + int flags; }; @@ -62,9 +74,9 @@ int tsnif_np_init(struct tsnif_handle *h, struct tsnif_np_args *np); int tsnif_np_fd(struct tsnif_handle *h, fd_set *set); int tsnif_np_process(struct tsnif_handle *h, fd_set *set); int tsnif_np_close(struct tsnif_handle *h); -int tsnif_np_send(struct tsnif_handle *h, struct trans_msg *msg); -int tsnif_np_dispatch(struct tsnif_handle *h, struct trans_msg *msg); +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_dispatch(struct tsnif_handle *h, struct trans_msg *msg); #endif /* NET_PROXY_H */ diff --git a/src/tsnif.c b/src/tsnif.c index 0b5d631..56a26fd 100644 --- a/src/tsnif.c +++ b/src/tsnif.c @@ -174,11 +174,11 @@ static int get_args(int argc, char **argv) break; case 'u': - tsnif_np_args(&np_args, TSNIF_NP_UDP_SERVER, optarg); + tsnif_np_args(&np_args, TSNIF_NP_UDP_CLIENT, optarg); break; case 'T': - tsnif_np_args(&np_args, TSNIF_NP_TCP_SERVER, optarg); + tsnif_np_args(&np_args, TSNIF_NP_TCP_CLIENT, optarg); break; case 'V': |