summaryrefslogtreecommitdiff
path: root/include/stun.h
blob: 126e7f5d0ceb1a9424650d9d740df73e1c12f2c3 (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
/*
 * liburtc
 * Copyright (C) 2019 Chris Hiszpanski
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#ifndef URTC_STUN_H
#define URTC_STUN_H

#include <stdint.h>

#ifdef __cplusplus
extern "C" {
#endif

#define SIZEOF_STUN_HDR				20

// STUN Classes
#define STUN_CLASS_REQUEST			0
#define STUN_CLASS_INDICTATION		1
#define STUN_CLASS_SUCCESS			2
#define STUN_CLASS_ERROR			3

// STUN Methods
#define STUN_METHOD_RESERVED		0
#define STUN_METHOD_BINDING			1


/////////////////////////////  TYPE DEFINITIONS  /////////////////////////////

typedef struct {
	int rc;								// Retransmission count
	int rto;							// Retransmission timeout (in ms)
	int sockfd;							// Socket file descriptor
} stun_client_t;

typedef struct {
	char *server;
} stun_client_config_t;

// STUN Message Header
typedef struct __attribute__((__packed__)) {
	uint16_t reserved:2;				// Upper two bits are 0
	uint16_t type:14;					// 14-bit type
	uint16_t msglen;					// Message length
	uint32_t cookie;					// Magic cookie
	uint8_t  txid[12];					// Transaction ID
} stun_msg_hdr_t;

// STUN Attribute
typedef struct {
	uint16_t type;						// Attribute type
	uint16_t len;						// Value length, prior to padding
	uint8_t *val;						// Pointer to attribute value
} stun_attr_t;

// STUN Attribute: MAPPED-ADDRESS
typedef struct __attribute__((__packed__)) {
	uint8_t reserved;
	uint8_t family;
	uint16_t port;
	union {
		uint8_t ipv4[4];				// IPv4 address is 32-bits
		uint8_t ipv6[16];				// IPv6 address is 128-bits
	};
} stun_attr_mapped_address_t;

// STUN Attribute: XOR-MAPPED-ADDRESS
typedef stun_attr_mapped_address_t stun_attr_xor_mapped_address_t;

// STUN Attribute: USERNAME


//////////////////////////////////  MACROS  //////////////////////////////////

// Get class from message header type
#define CLASS(typ) (((typ >> 7) & 0x2) | ((typ >> 4) & 0x1))

// Get method from message header type
#define METHOD(typ) (((typ >> 2) & 0xF80) | ((typ >> 1) & 0x70) | (typ & 0xF))

#define TYPE(cls, mthd) ( \
		((mthd & 0xF80) << 2) | \
		((cls & 0x2) << 7) | \
		((mthd & 0x70) << 1) | \
		((cls & 0x1) << 4) | \
		(mthd & 0xF) \
)


///////////////////////////  FORWARD DECLARATIONS  ///////////////////////////

/**
 * (callback) Binding request response handler
 *
 * @param[in] name
 * @param[in] arg  User argument
 */
typedef void (*stun_client_binding_response_callback)(
	const char *name,
	      void *arg
);

/**
 * Sends a STUN binding request to the specified server
 *
 * @param[in] srv  Server (hostname or IP address)
 * @param[in] cb   Handler to call when a response is received
 * @param[in] arg  Optional user argument to pass to callback
 */
err_t stun_client_binding_request(
	runloop_t *rl,
	const char *srv,
	stun_client_binding_response_callback cb,
	void *arg
);

#ifdef __cplusplus
}
#endif

#endif /* URTC_STUN_H */