summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJiri Olsa <Jiri Olsa jolsa@redhat.com>2010-06-08 10:14:05 +0200
committerJiri Olsa <Jiri Olsa jolsa@redhat.com>2010-06-08 10:14:05 +0200
commitce45ac49228b51c54094cb521eb018644f3e0fb1 (patch)
tree119084ece1c558ed06fa7173264925c9271ad829
parent23367a9167c0ee1efb700a474e1a40ac28856da1 (diff)
downloadtsnif-ce45ac49228b51c54094cb521eb018644f3e0fb1.tar.gz
tsnif-ce45ac49228b51c54094cb521eb018644f3e0fb1.tar.xz
tsnif-ce45ac49228b51c54094cb521eb018644f3e0fb1.zip
initial change - compiles, does not work
-rw-r--r--src/Makefile5
-rw-r--r--src/intf.c61
-rw-r--r--src/intf.h11
-rw-r--r--src/net-proxy-tcp.c34
-rw-r--r--src/net-proxy-udp.c34
-rw-r--r--src/net-proxy.c104
-rw-r--r--src/net-proxy.h59
-rw-r--r--src/trans-libnl.c5
-rw-r--r--src/trans.h2
-rw-r--r--src/tsnif.c23
-rw-r--r--src/tsnifd.c20
11 files changed, 326 insertions, 32 deletions
diff --git a/src/Makefile b/src/Makefile
index fb38799..3802fc1 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -2,7 +2,10 @@
INTF_OBJS= \
src/intf.o \
src/fsm.o \
- src/trans-libnl.o
+ src/trans-libnl.o \
+ src/net-proxy.o \
+ src/net-proxy-udp.o \
+ src/net-proxy-tcp.o \
STORAGE_OBJS= \
src/storage-mmap.o
diff --git a/src/intf.c b/src/intf.c
index e03b41a..d684416 100644
--- a/src/intf.c
+++ b/src/intf.c
@@ -95,7 +95,7 @@ static int init_mgroup(struct tsnif_handle *h)
return trans_send(&h->trans, &msg);
}
-int tsnif_init(struct tsnif_handle *h, struct tsnif_ops *ops)
+int tsnif_init(struct tsnif_handle *h, struct tsnif_ops *ops, struct tsnif_np_args *np)
{
int err;
@@ -106,6 +106,10 @@ int tsnif_init(struct tsnif_handle *h, struct tsnif_ops *ops)
h->ops = ops;
INIT_LIST_HEAD(&h->terms);
+ err = tsnif_np_init(h, np);
+ if (h->np_only)
+ return err;
+
err = trans_init(&h->trans, trans_cb);
if (err)
return err;
@@ -115,17 +119,42 @@ int tsnif_init(struct tsnif_handle *h, struct tsnif_ops *ops)
int tsnif_close(struct tsnif_handle *h)
{
+ int err;
+
+ err = tsnif_np_close(h);
+ if (h->np_only)
+ return err;
+
return trans_close(&h->trans);
}
-int tsnif_process(struct tsnif_handle *h)
+int tsnif_process(struct tsnif_handle *h, fd_set *set)
{
- return trans_process(&h->trans);
+ int err;
+
+ err = tsnif_np_process(h, set);
+
+ if (h->np_only)
+ return err;
+
+ return trans_process(&h->trans, set);
}
-int tsnif_fd(struct tsnif_handle *h)
+int tsnif_fd(struct tsnif_handle *h, fd_set *set)
{
- return trans_fd(&h->trans);
+ int fd_np;
+ int fd_trans;
+
+ FD_ZERO(set);
+
+ fd_np = tsnif_np_fd(h, set);
+ if (h->np_only)
+ return fd_np;
+
+ fd_trans = trans_fd(&h->trans);
+ FD_SET(fd_trans, set);
+
+ return (fd_trans > fd_np ? fd_trans : fd_np);
}
int tsnif_term_add(struct tsnif_handle *h, struct tsnif_term *term,
@@ -153,10 +182,16 @@ int tsnif_term_del(struct tsnif_handle *h, struct tsnif_term *term)
return 0;
}
+enum {
+ TSNIF_NP_ONLY,
+ TSNIF_NP_TRANS,
+};
+
int tsnif_attach(struct tsnif_term *term)
{
struct trans_msg msg;
struct tsnif_handle *h = term->handle;
+ int ret;
TSNIF_DEBUG(INTF, "type %d, idx %d\n", term->type, term->idx);
@@ -170,6 +205,10 @@ int tsnif_attach(struct tsnif_term *term)
msg.idx = term->idx;
msg.cmd = TSNIF_CMD_ATTACH;
+ ret = tsnif_np_send(h, &msg);
+ if (h->np_only)
+ return ret;
+
return trans_send(&h->trans, &msg);
}
@@ -177,6 +216,7 @@ int tsnif_detach(struct tsnif_term *term)
{
struct trans_msg msg;
struct tsnif_handle *h = term->handle;
+ int ret;
TSNIF_DEBUG(INTF, "type %d, idx %d\n", term->type, term->idx);
@@ -190,18 +230,27 @@ int tsnif_detach(struct tsnif_term *term)
msg.idx = term->idx;
msg.cmd = TSNIF_CMD_DETACH;
+ ret = tsnif_np_send(h, &msg);
+ if (h->np_only)
+ return ret;
+
return trans_send(&h->trans, &msg);
}
int tsnif_list(struct tsnif_handle *h)
{
struct trans_msg msg;
+ int ret;
TSNIF_DEBUG(INTF, "sending list command\n");
memset(&msg, 0x0, sizeof(msg));
msg.cmd = TSNIF_CMD_TTY_LIST;
+ ret = tsnif_np_send(h, &msg);
+ if (h->np_only)
+ return ret;
+
return trans_send(&h->trans, &msg);
}
@@ -216,5 +265,5 @@ int tsnif_enum(struct tsnif_handle *h, cb_tsnif_enum_t cb)
break;
}
- return err;;
+ return err;
}
diff --git a/src/intf.h b/src/intf.h
index c136b6b..96f5fd1 100644
--- a/src/intf.h
+++ b/src/intf.h
@@ -8,6 +8,7 @@
#include "list.h"
#include "trans.h"
+#include "net-proxy.h"
enum {
/* no state - notify state */
@@ -57,15 +58,19 @@ struct tsnif_handle {
struct list_head terms;
struct tsnif_ops *ops;
struct trans_handle trans;
+
+ struct tsnif_np_handle np;
+ int np_only;
};
typedef int(*cb_tsnif_enum_t)(struct tsnif_term *term);
/* handle functions */
-int tsnif_init(struct tsnif_handle *h, struct tsnif_ops *ops);
+int tsnif_init(struct tsnif_handle *h, struct tsnif_ops *ops,
+ struct tsnif_np_args *np);
int tsnif_close(struct tsnif_handle *h);
-int tsnif_process(struct tsnif_handle *h);
-int tsnif_fd(struct tsnif_handle *h);
+int tsnif_process(struct tsnif_handle *h, fd_set *set);
+int tsnif_fd(struct tsnif_handle *h, fd_set *set);
int tsnif_list(struct tsnif_handle *h);
/* term functions */
diff --git a/src/net-proxy-tcp.c b/src/net-proxy-tcp.c
new file mode 100644
index 0000000..fbd0c77
--- /dev/null
+++ b/src/net-proxy-tcp.c
@@ -0,0 +1,34 @@
+#include "net-proxy.h"
+
+int tcp_init_server(struct tsnif_handle *h, struct tsnif_np_args *args)
+{
+ return 0;
+}
+
+int tcp_init_client(struct tsnif_handle *h, struct tsnif_np_args *args)
+{
+ return 0;
+}
+
+int tcp_close(struct tsnif_handle *h)
+{
+ return 0;
+}
+
+int tcp_send(struct tsnif_handle *h, struct trans_msg *msg)
+{
+ return 0;
+}
+
+int tcp_process(struct tsnif_handle *h)
+{
+ return 0;
+}
+
+struct tsnif_np_prot tcp_prot = {
+ .init_server = tcp_init_server,
+ .init_client = tcp_init_client,
+ .close = tcp_close,
+ .send = tcp_send,
+ .process = tcp_process,
+};
diff --git a/src/net-proxy-udp.c b/src/net-proxy-udp.c
new file mode 100644
index 0000000..0aa7551
--- /dev/null
+++ b/src/net-proxy-udp.c
@@ -0,0 +1,34 @@
+#include "net-proxy.h"
+
+int udp_init_server(struct tsnif_handle *h, struct tsnif_np_args *args)
+{
+ return 0;
+}
+
+int udp_init_client(struct tsnif_handle *h, struct tsnif_np_args *args)
+{
+ return 0;
+}
+
+int udp_close(struct tsnif_handle *h)
+{
+ return 0;
+}
+
+int udp_send(struct tsnif_handle *h, struct trans_msg *msg)
+{
+ return 0;
+}
+
+int udp_process(struct tsnif_handle *h)
+{
+ return 0;
+}
+
+struct tsnif_np_prot udp_prot = {
+ .init_server = udp_init_server,
+ .init_client = udp_init_client,
+ .close = udp_close,
+ .send = udp_send,
+ .process = udp_process,
+};
diff --git a/src/net-proxy.c b/src/net-proxy.c
new file mode 100644
index 0000000..8d9aa96
--- /dev/null
+++ b/src/net-proxy.c
@@ -0,0 +1,104 @@
+
+#include <errno.h>
+
+#include "intf.h"
+
+extern struct tsnif_np_prot udp_prot;
+extern struct tsnif_np_prot tcp_prot;
+
+struct tsnif_np_prot *prot[TSNIF_NP_PROT_MAX] = {
+ &udp_prot,
+ &tcp_prot,
+};
+
+#define UDP (prot[TSNIF_NP_PROT_UDP])
+#define TCP (prot[TSNIF_NP_PROT_TCP])
+
+static int udp_init(struct tsnif_handle *h, struct tsnif_np_args *np, int flags)
+{
+ int ret;
+
+ if (flags & TSNIF_NP_UDP_CLIENT)
+ ret = UDP->init_client(h, np);
+ else
+ ret = UDP->init_server(h, np);
+
+ return ret;
+}
+
+int tsnif_np_init(struct tsnif_handle *h, struct tsnif_np_args *np)
+{
+ int ret;
+ int flags = np->flags;
+ struct tsnif_np_handle *nph = &h->np;
+
+ memset(nph, 0x0, sizeof(*nph));
+ nph->flags = np->flags;
+ nph->fd_udp = -1;
+ nph->fd_tcp = -1;
+
+ if (flags & TSNIF_NP_CLIENT) {
+
+ /* only one client protocol allowed */
+ if ((flags & TSNIF_NP_UDP_CLIENT) &&
+ (flags & TSNIF_NP_TCP_CLIENT))
+ return -EINVAL;
+
+ h->np_only = 1;
+ }
+
+ if (TSNIF_NP_UDP & flags)
+ ret = udp_init(h, np, flags);
+
+ return ret;
+}
+
+int tsnif_np_fd(struct tsnif_handle *h, fd_set *set)
+{
+ struct tsnif_np_handle *nph = &h->np;
+ int fd_udp = nph->fd_udp;
+ int fd_tcp = nph->fd_tcp;
+
+ if ((TSNIF_NP_UDP & nph->flags) &&
+ (-1 != fd_udp))
+ FD_SET(fd_udp, set);
+
+ if ((TSNIF_NP_TCP & nph->flags) &&
+ (-1 != fd_tcp))
+ FD_SET(fd_tcp, set);
+
+ return (fd_udp > fd_tcp ? fd_udp : fd_tcp);
+}
+
+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) &&
+ FD_ISSET(nph->fd_udp, set))
+ ret = UDP->process(h);
+
+ return ret;
+}
+
+int tsnif_np_close(struct tsnif_handle *h)
+{
+ struct tsnif_np_handle *nph = &h->np;
+
+ if (TSNIF_NP_UDP & nph->flags)
+ UDP->close(h);
+
+ return 0;
+}
+
+int tsnif_np_send(struct tsnif_handle *h, struct trans_msg *msg)
+{
+ struct tsnif_np_handle *nph = &h->np;
+ int ret = 0;
+
+ if (TSNIF_NP_UDP & nph->flags)
+ ret = UDP->send(h, msg);
+
+ return ret;
+}
diff --git a/src/net-proxy.h b/src/net-proxy.h
new file mode 100644
index 0000000..bb5da7b
--- /dev/null
+++ b/src/net-proxy.h
@@ -0,0 +1,59 @@
+
+#ifndef NET_PROXY_H
+#define NET_PROXY_H
+
+#include <sys/select.h>
+
+enum {
+ TSNIF_NP_UDP_CLIENT = 0x1,
+ TSNIF_NP_UDP_SERVER = 0x2,
+ TSNIF_NP_TCP_CLIENT = 0x4,
+ TSNIF_NP_TCP_SERVER = 0x8,
+};
+
+enum {
+ TSNIF_NP_PROT_UDP,
+ TSNIF_NP_PROT_TCP,
+ TSNIF_NP_PROT_MAX,
+};
+
+#define TSNIF_NP_CLIENT (TSNIF_NP_UDP_CLIENT | TSNIF_NP_TCP_CLIENT)
+#define TSNIF_NP_UDP (TSNIF_NP_UDP_CLIENT | TSNIF_NP_UDP_SERVER)
+#define TSNIF_NP_TCP (TSNIF_NP_TCP_CLIENT | TSNIF_NP_TCP_SERVER)
+
+struct tsnif_np_args {
+ char *host;
+ int port;
+ int flags;
+};
+
+struct tsnif_np_handle {
+ int fd_udp;
+ int fd_tcp;
+ int flags;
+};
+
+struct tsnif_handle;
+struct trans_msg;
+
+typedef int(*init_server_t)(struct tsnif_handle*, struct tsnif_np_args *args);
+typedef int(*init_client_t)(struct tsnif_handle*, struct tsnif_np_args *args);
+typedef int(*close_t)(struct tsnif_handle*);
+typedef int(*send_t)(struct tsnif_handle*, struct trans_msg *msg);
+typedef int(*process_t)(struct tsnif_handle*);
+
+struct tsnif_np_prot {
+ init_server_t init_server;
+ init_client_t init_client;
+ close_t close;
+ send_t send;
+ process_t process;
+};
+
+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);
+
+#endif /* NET_PROXY_H */
diff --git a/src/trans-libnl.c b/src/trans-libnl.c
index c623375..6d12428 100644
--- a/src/trans-libnl.c
+++ b/src/trans-libnl.c
@@ -158,10 +158,13 @@ int trans_close(struct trans_handle *h)
return 0;
}
-int trans_process(struct trans_handle *h)
+int trans_process(struct trans_handle *h, fd_set *set)
{
int err;
+ if (!FD_ISSET(trans_fd(h), set))
+ return 0;
+
err = nl_recvmsgs_default(h->sock);
if (err)
TSNIF_DEBUG(TRANS, "got error: %s\n", nl_geterror());
diff --git a/src/trans.h b/src/trans.h
index e7c38bc..8217177 100644
--- a/src/trans.h
+++ b/src/trans.h
@@ -42,7 +42,7 @@ struct trans_msg {
int trans_init(struct trans_handle *h, trans_cb_t cb);
int trans_close(struct trans_handle *h);
-int trans_process(struct trans_handle *h);
+int trans_process(struct trans_handle *h, fd_set *set);
int trans_send(struct trans_handle *h, struct trans_msg *msg);
int trans_group(struct trans_handle *h, int group);
int trans_fd(struct trans_handle *h);
diff --git a/src/tsnif.c b/src/tsnif.c
index ef83561..d68a8bb 100644
--- a/src/tsnif.c
+++ b/src/tsnif.c
@@ -16,6 +16,7 @@
static struct tsnif_handle handle;
static struct tsnif_term term;
+static struct tsnif_np_args np_args;
static struct tsnif_storage_opts storage_opts = {
.flags = TSNIF_STORAGE_OPT_WRITE,
@@ -125,6 +126,8 @@ static int get_args(int argc, char **argv)
{
int ret = 0;
+ memset(&np_args, 0x0, sizeof(np_args));
+
while (1) {
int c;
int option_index = 0;
@@ -132,6 +135,9 @@ static int get_args(int argc, char **argv)
{"type", required_argument, 0, 't'},
{"idx", required_argument, 0, 'i'},
{"store", required_argument, 0, 's'},
+ {"list", required_argument, 0, 'l'},
+ {"udp", required_argument, 0, 'u'},
+ {"tcp", required_argument, 0, 't'},
{"version", no_argument, 0, 'V'},
{"debug", required_argument, 0, 'd'},
{"verbose", required_argument, 0, 'v'},
@@ -227,7 +233,7 @@ int main(int argc, char **argv)
if (get_args(argc, argv))
usage();
- err = tsnif_init(&handle, &ops);
+ err = tsnif_init(&handle, &ops, &np_args);
if (err)
return err;
@@ -271,25 +277,22 @@ int main(int argc, char **argv)
while(!killed) {
fd_set rfds;
struct timeval tv = { 1, 0};
- int ts_fd = tsnif_fd(&handle);
+ int max_fd;
int in_fd = 0;
- FD_ZERO(&rfds);
- FD_SET(ts_fd, &rfds);
+ max_fd = tsnif_fd(&handle, &rfds);
FD_SET(in_fd, &rfds);
- err = select(ts_fd + 1, &rfds, NULL, NULL, &tv);
+ err = select(max_fd + 1, &rfds, NULL, NULL, &tv);
if (err == -1) {
perror("select()");
continue;
} else if (!err)
continue;
- if (FD_ISSET(ts_fd, &rfds)) {
- err = tsnif_process(&handle);
- if (err)
- longjmp(env, 3);
- }
+ err = tsnif_process(&handle, &rfds);
+ if (err)
+ longjmp(env, 3);
if (FD_ISSET(in_fd, &rfds)) {
err = process_input();
diff --git a/src/tsnifd.c b/src/tsnifd.c
index 52393d5..f0a50f3 100644
--- a/src/tsnifd.c
+++ b/src/tsnifd.c
@@ -20,6 +20,7 @@
#include "tsnifd.h"
static struct tsnif_handle handle;
+static struct tsnif_np_args np_args;
static int foreground = 0;
static int killed = 0;
@@ -151,6 +152,8 @@ static int get_args(int argc, char **argv)
{
int ret = 0;
+ memset(&np_args, 0x0, sizeof(np_args));
+
while (1) {
int c;
int option_index = 0;
@@ -212,7 +215,7 @@ int main(int argc, char **argv)
if (get_args(argc, argv))
usage();
- err = tsnif_init(&handle, &ops);
+ err = tsnif_init(&handle, &ops, &np_args);
if (err)
return err;
@@ -235,23 +238,20 @@ int main(int argc, char **argv)
while(!killed) {
fd_set rfds;
struct timeval tv = { 1, 0};
- int ts_fd = tsnif_fd(&handle);
+ int max_fd;
- FD_ZERO(&rfds);
- FD_SET(ts_fd, &rfds);
+ max_fd = tsnif_fd(&handle, &rfds);
- err = select(ts_fd + 1, &rfds, NULL, NULL, &tv);
+ err = select(max_fd + 1, &rfds, NULL, NULL, &tv);
if (err == -1) {
perror("select()");
continue;
} else if (!err)
continue;
- if (FD_ISSET(ts_fd, &rfds)) {
- err = tsnif_process(&handle);
- if (err)
- longjmp(env, 2);
- }
+ err = tsnif_process(&handle, &rfds);
+ if (err)
+ longjmp(env, 2);
}
longjmp(env, 2);