summaryrefslogtreecommitdiffstats
path: root/src/net-proxy.c
blob: 42bcc653e6005e87f2c2d85dee5f7187092f36a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243

#include <errno.h>

#include "intf.h"
#include "debug.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;

	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;

	memset(nph, 0x0, sizeof(*nph));
	nph->flags  = 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);

	TSNIF_DEBUG(NP, "ret = %d\n", ret);
	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_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;
}

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 (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)
{
	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)
{
	struct tsnif_np_handle *nph = &h->np;

	if (nph->flags & TSNIF_NP_CLIENT)
		return tsnif_dispatch(h, msg);

	if (nph->flags & TSNIF_NP_SERVER)
		return dispatch_server(h, msg);

	return 0;
}

/* XXX - finish parsing
 * host is expected to be int format:
 *
 *   hostname      (default port is used)
 *   ip            (default port is used)
 *   hostname:port
 *         ip:port
 *
 */
void tsnif_np_args(struct tsnif_np_args *args, int flag, char *host)
{
	args->flags = flag;
	args->host = host;
	args->port = TSNIF_NP_PORT;
}