diff options
Diffstat (limited to 'src/net-proxy.c')
-rw-r--r-- | src/net-proxy.c | 104 |
1 files changed, 104 insertions, 0 deletions
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; +} |