aboutsummaryrefslogtreecommitdiffstats
path: root/package/tapi_sip/src/stun.c
blob: 5c6b24067965e77b3b41b511d58f0f3491db54c2 (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 <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>

#include <poll.h>


struct stun_client {
	struct addrinfo *serverinfo;
};

struct stun_response {
	struct sockaddr addr;
};

struct stun_header {
	uint16_t type;
	uint16_t length;
	uint32_t cookie;
	uint32_t id[3];
} __attribute((packed));

struct stun_packet {
	struct stun_header header;
	uint8_t data[0];
} __attribute((packed));

#define STUN_CLASS(c0, c1) (((c0) << 4) | ((c1) << 8))

#define STUN_CLASS_REQUEST	STUN_CLASS(0, 0)
#define STUN_CLASS_INDICATION	STUN_CLASS(0, 1)
#define STUN_CLASS_SUCCESS	STUN_CLASS(1, 0)
#define STUN_CLASS_ERROR	STUN_CLASS(1, 1)

#define STUN_CLASS_MASK STUN_CLASS(1, 1)

#define STUN_MESSAGE(msg) (((msg & 0xf10) << 2) | ((msg & 0x70) << 1) | (msg & 0xf))
#define STUN_MESSAGE_BIND STUN_MESSAGE(1)

#define STUN_COOKIE 0x2112a442

enum {
	STUN_ATTR_TYPE_MAPPED_ADDRESS = 0x1,
	STUN_ATTR_TYPE_XOR_MAPPED_ADDRESS = 0x20,
	STUN_ATTR_TYPE_XOR_MAPPED_ADDRESS2 = 0x8020,
};

static inline uint16_t get_unaligned_be16(const uint8_t *buf)
{
	return (buf[0] << 8) | buf[1];
}

static inline uint16_t get_unaligned_be32(const uint8_t *buf)
{
	return (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
}

static int stun_parse_xor_mapped_address(struct stun_response *response,
	const uint8_t *buf, int length)
{
	uint8_t fam = buf[1];
	uint16_t port = get_unaligned_be16(&buf[2]);
	struct sockaddr_in *sin = (struct sockaddr_in *)&response->addr;
	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&response->addr;


	switch (fam) {
	case 0x1:
		sin->sin_family = AF_INET;
		sin->sin_port = htons((port ^ (uint16_t)((STUN_COOKIE & 0xffff0000) >> 16)));
		memcpy(&sin->sin_addr.s_addr, buf + 4, 4);
		sin->sin_addr.s_addr ^= htonl(STUN_COOKIE);
		printf("xor port: %d\n", sin->sin_port);
		break;
	case 0x2:
		sin6->sin6_family = AF_INET6;
		sin->sin_port = htons((port ^ (uint16_t)((STUN_COOKIE & 0xffff0000) >> 16)));
		memcpy(sin6->sin6_addr.s6_addr, buf + 4, 16);
		break;
	}

	return 0;
}

static int stun_parse_mapped_address(struct stun_response *response,
	const uint8_t *buf, int length)
{
	uint8_t fam = buf[1];
	uint16_t port = get_unaligned_be16(&buf[2]);
	struct sockaddr_in *sin = (struct sockaddr_in *)&response->addr;
	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&response->addr;

	printf("port: %d\n", port);

	switch (fam) {
	case 0x1:
		sin->sin_family = AF_INET;
		sin->sin_port = htons(port);
		memcpy(&sin->sin_addr.s_addr, buf + 4, 4);
		break;
	case 0x2:
		sin6->sin6_family = AF_INET6;
		sin6->sin6_port = htons(port);
		memcpy(sin6->sin6_addr.s6_addr, buf + 4, 16);
		break;
	}

	return 0;
}

static int stun_parse_response(struct stun_response *response,
	const struct stun_packet *packet)
{
	uint16_t attr_type, attr_length;
	const uint8_t *buf;
	int length = ntohs(packet->header.length);
	int ret;
	int i = 0;

	if (packet->header.cookie != htonl(STUN_COOKIE))
		return -1;

	if (packet->header.length < 4)
		return 0;

	buf = packet->data;

	do {
		attr_type = get_unaligned_be16(&buf[i]);
		attr_length = get_unaligned_be16(&buf[i + 2]);
		i += 4;

		if (i + attr_length > length)
			break;

		switch (attr_type) {
		case STUN_ATTR_TYPE_MAPPED_ADDRESS:
			ret = stun_parse_mapped_address(response, &buf[i], attr_length);
			break;
		case STUN_ATTR_TYPE_XOR_MAPPED_ADDRESS:
		case STUN_ATTR_TYPE_XOR_MAPPED_ADDRESS2:
			ret = stun_parse_xor_mapped_address(response, &buf[i], attr_length);
			break;
		}

		i += attr_length;

	} while (i < length && ret == 0);

	return 0;
}

static struct stun_packet *stun_packet_alloc(size_t data_size)
{
	return malloc(sizeof(struct stun_packet) + data_size);
}

int stun_client_resolve(struct stun_client *stun, int sockfd, struct sockaddr *addr)
{
	struct stun_packet *packet = stun_packet_alloc(200);
	struct stun_response response;
	int ret;
	int retries = 4;
	int timeout = 500;
	struct pollfd pollfd;

	pollfd.events = POLLIN;
	pollfd.fd = sockfd;

	packet->header.type = htons(STUN_CLASS_REQUEST | STUN_MESSAGE_BIND);
	packet->header.cookie = htonl(STUN_COOKIE);
	packet->header.id[0] = 0x12345678;
	packet->header.id[1] = 0x12345678;
	packet->header.id[2] = 0x12345678;
	packet->header.length = 0;

	while (retries--) {
		ret = sendto(sockfd, packet, sizeof(struct stun_header) + packet->header.length,
			0, stun->serverinfo->ai_addr, stun->serverinfo->ai_addrlen);

		ret = poll(&pollfd, 1, timeout);
		switch (ret) {
		case 0:
			timeout <<= 1;
		case -EINTR:
			printf("retry\n");
			continue;
		default:
			retries = 0;
		}
		ret = recvfrom(sockfd, packet, 200, 0, NULL, NULL);
	}

	if (ret <= 0)
		return ret ? ret : -ETIMEDOUT;

	memset(&response, 0, sizeof(response));
	ret = stun_parse_response(&response, packet);

	*addr = response.addr;

	return ret;
}

struct stun_client *stun_client_alloc(const char *hostname, uint16_t port)
{
    struct addrinfo hints;
	struct stun_client *stun;
	int ret;
	char p[6];


	stun = malloc(sizeof(*stun));
	if (!stun)
		return NULL;

    memset(&hints, 0, sizeof(hints));
    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_DGRAM;
	hints.ai_flags = AI_NUMERICSERV;

	snprintf(p, sizeof(p), "%d", port);
    if ((ret = getaddrinfo(hostname, p, &hints, &stun->serverinfo)) != 0) {
        fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(ret));
        return NULL;
    }


	return stun;
}

void stun_client_free(struct stun_client *stun)
{
	freeaddrinfo(stun->serverinfo);
	free(stun);
}