summaryrefslogtreecommitdiff
path: root/modules/natpmp/libnatpmp.c
blob: 796900752323a49c6c86aaf3bc26dce1d8a24785 (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
/**
 * @file libnatpmp.c NAT-PMP Client library
 *
 * Copyright (C) 2010 Creytiv.com
 */
#include <string.h>
#include <re.h>
#include <baresip.h>
#include "libnatpmp.h"


enum {
	NATPMP_DELAY   =  250,
	NATPMP_MAXTX   =    9,
};

struct natpmp_req {
	struct natpmp_req **npp;
	struct udp_sock *us;
	struct tmr tmr;
	struct mbuf *mb;
	struct sa srv;
	unsigned n;
	natpmp_resp_h *resph;
	void *arg;
};


static void completed(struct natpmp_req *np, int err,
		      const struct natpmp_resp *resp)
{
	natpmp_resp_h *resph = np->resph;
	void *arg = np->arg;

	tmr_cancel(&np->tmr);

	if (np->npp) {
		*np->npp = NULL;
		np->npp = NULL;
	}

	np->resph = NULL;

	/* must be destroyed before calling handler */
	mem_deref(np);

	if (resph)
		resph(err, resp, arg);
}


static void destructor(void *arg)
{
	struct natpmp_req *np = arg;

	tmr_cancel(&np->tmr);
	mem_deref(np->us);
	mem_deref(np->mb);
}


static void timeout(void *arg)
{
	struct natpmp_req *np = arg;
	int err;

	if (np->n > NATPMP_MAXTX) {
		completed(np, ETIMEDOUT, NULL);
		return;
	}

	tmr_start(&np->tmr, NATPMP_DELAY<<np->n, timeout, arg);

#if 1
	debug("natpmp: {n=%u} tx %u bytes\n", np->n, np->mb->end);
#endif

	np->n++;

	np->mb->pos = 0;
	err = udp_send(np->us, &np->srv, np->mb);
	if (err) {
		completed(np, err, NULL);
	}
}


static int resp_decode(struct natpmp_resp *resp, struct mbuf *mb)
{
	resp->vers   =       mbuf_read_u8(mb);
	resp->op     =       mbuf_read_u8(mb);
	resp->result = ntohs(mbuf_read_u16(mb));
	resp->epoch  = ntohl(mbuf_read_u32(mb));

	if (!(resp->op & 0x80))
		return EPROTO;
	resp->op &= ~0x80;

	switch (resp->op) {

	case NATPMP_OP_EXTERNAL:
		resp->u.ext_addr = ntohl(mbuf_read_u32(mb));
		break;

	case NATPMP_OP_MAPPING_UDP:
	case NATPMP_OP_MAPPING_TCP:
		resp->u.map.int_port = ntohs(mbuf_read_u16(mb));
		resp->u.map.ext_port = ntohs(mbuf_read_u16(mb));
		resp->u.map.lifetime = ntohl(mbuf_read_u32(mb));
		break;

	default:
		warning("natmap: unknown opcode %d\n", resp->op);
		return EBADMSG;
	}

	return 0;
}


static void udp_recv(const struct sa *src, struct mbuf *mb, void *arg)
{
	struct natpmp_req *np = arg;
	struct natpmp_resp resp;

	if (!sa_cmp(src, &np->srv, SA_ALL))
		return;

	if (resp_decode(&resp, mb))
		return;

	completed(np, 0, &resp);
}


static int natpmp_init(struct natpmp_req *np, const struct sa *srv,
		       uint8_t opcode, natpmp_resp_h *resph, void *arg)
{
	int err;

	if (!np || !srv)
		return EINVAL;

	/* a new UDP socket for each NAT-PMP request */
	err = udp_listen(&np->us, NULL, udp_recv, np);
	if (err)
		return err;

	np->srv   = *srv;
	np->resph = resph;
	np->arg   = arg;

	udp_connect(np->us, srv);

	np->mb = mbuf_alloc(512);
	if (!np->mb)
		return ENOMEM;

	err |= mbuf_write_u8(np->mb, NATPMP_VERSION);
	err |= mbuf_write_u8(np->mb, opcode);

	return err;
}


int natpmp_external_request(struct natpmp_req **npp, const struct sa *srv,
			    natpmp_resp_h *resph, void *arg)
{
	struct natpmp_req *np;
	int err;

	np = mem_zalloc(sizeof(*np), destructor);
	if (!np)
		return ENOMEM;

	err = natpmp_init(np, srv, NATPMP_OP_EXTERNAL, resph, arg);
	if (err)
		goto out;

	timeout(np);

 out:
	if (err)
		mem_deref(np);
	else if (npp) {
		np->npp = npp;
		*npp = np;
	}
	else {
		/* Destroy the transaction now */
		mem_deref(np);
	}

	return err;
}


int natpmp_mapping_request(struct natpmp_req **npp, const struct sa *srv,
			   uint16_t int_port, uint16_t ext_port,
			   uint32_t lifetime, natpmp_resp_h *resph, void *arg)
{
	struct natpmp_req *np;
	int err;

	np = mem_zalloc(sizeof(*np), destructor);
	if (!np)
		return ENOMEM;

	err = natpmp_init(np, srv, NATPMP_OP_MAPPING_UDP, resph, arg);
	if (err)
		goto out;

	err |= mbuf_write_u16(np->mb, 0x0000);
	err |= mbuf_write_u16(np->mb, htons(int_port));
	err |= mbuf_write_u16(np->mb, htons(ext_port));
	err |= mbuf_write_u32(np->mb, htonl(lifetime));
	if (err)
		goto out;

	timeout(np);

 out:
	if (err)
		mem_deref(np);
	else if (npp) {
		np->npp = npp;
		*npp = np;
	}
	else {
		/* Destroy the transaction now */
		mem_deref(np);
	}

	return err;
}