Stun Server  Compliant with the latest RFCs including 5389, 5769, and 5780
discover the local host's own external IP address
stuntypes.h
Go to the documentation of this file.
1 /*
2  Copyright 2011 John Selbie
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 */
16 
17 
18 
19 #ifndef STUNTYPES_H
20 #define STUNTYPES_H
21 
22 // RFC 5389: STUN RFC
23 // RFC 3489: OLD STUN RFC. Obsolete, but newer specs reference it
24 // RFC 5769 - test vectors for STUN
25 // RFC 5780 - Nat behavior discovery using STUN
26 
27 
28 
29 const uint16_t DEFAULT_STUN_PORT = 3478;
30 const uint16_t DEFAULT_STUN_TLS_PORT = 5349;
31 
32 
33 const uint16_t STUN_ATTRIBUTE_MAPPEDADDRESS = 0x0001;
34 const uint16_t STUN_ATTRIBUTE_RESPONSEADDRESS = 0x0002;
35 const uint16_t STUN_ATTRIBUTE_CHANGEREQUEST = 0x0003;
36 const uint16_t STUN_ATTRIBUTE_SOURCEADDRESS = 0x0004;
37 const uint16_t STUN_ATTRIBUTE_CHANGEDADDRESS = 0x0005; // this is the legacy "other address" from rfc 3489, superceded by STUN_ATTRIBUTE_OTHERADDRESS below
38 
39 const uint16_t STUN_ATTRIBUTE_USERNAME = 0x0006;
40 
41 const uint16_t STUN_ATTRIBUTE_LEGACY_PASSWORD = 0x0007; // old rfc
42 
43 
44 const uint16_t STUN_ATTRIBUTE_MESSAGEINTEGRITY = 0x0008;
45 const uint16_t STUN_ATTRIBUTE_ERRORCODE = 0x0009;
46 const uint16_t STUN_ATTRIBUTE_UNKNOWNATTRIBUTES = 0x000A;
47 
48 const uint16_t STUN_ATTRIBUTE_REFLECTEDFROM = 0x000B; // old rfc
49 
50 
51 const uint16_t STUN_ATTRIBUTE_REALM = 0x0014;
52 const uint16_t STUN_ATTRIBUTE_NONCE = 0x0015;
53 const uint16_t STUN_ATTRIBUTE_XORMAPPEDADDRESS = 0x0020;
54 
55 const uint16_t STUN_ATTRIBUTE_PADDING = 0x0026;
56 const uint16_t STUN_ATTRIBUTE_RESPONSE_PORT = 0x0027;
57 
58 
59 
60 // This attribute is sent by the server to legacy clients
61 // 0x8020 is is not defined in any RFC, but is the value that Vovida server uses
63 
64 const uint16_t STUN_ATTRIBUTE_SOFTWARE = 0x8022;
65 const uint16_t STUN_ATTRIBUTE_ALTERNATESERVER = 0x8023;
66 
67 const uint16_t STUN_ATTRIBUTE_FINGERPRINT = 0x8028;
68 
69 const uint16_t STUN_ATTRIBUTE_RESPONSE_ORIGIN = 0x802b;
70 const uint16_t STUN_ATTRIBUTE_OTHER_ADDRESS = 0x802c;
71 
72 
73 
74 
75 const uint16_t STUN_TRANSACTION_ID_LENGTH = 16;
76 
77 const uint8_t STUN_ATTRIBUTE_FIELD_IPV4 = 1;
78 const uint8_t STUN_ATTRIBUTE_FIELD_IPV6 = 2;
79 
80 const uint16_t STUN_IPV4_LENGTH = 4;
81 const uint16_t STUN_IPV6_LENGTH = 16;
82 
83 
84 const uint16_t STUN_ERROR_TRYALTERNATE = 300;
85 const uint16_t STUN_ERROR_BADREQUEST = 400;
86 const uint16_t STUN_ERROR_UNAUTHORIZED = 401;
87 const uint16_t STUN_ERROR_UNKNOWNATTRIB = 420;
88 const uint16_t STUN_ERROR_STALENONCE = 438;
89 const uint16_t STUN_ERROR_SERVERERROR = 500;
90 
91 
92 
94 {
100 };
101 
103 {
106 };
107 
109 {
110  uint16_t attributeType;
111  uint16_t size;
112  uint16_t offset;
113 };
114 
116 {
117  uint8_t zero;
118  uint8_t family;
119  uint16_t xport;
120  uint32_t xip;
121 };
122 
124 {
125  uint8_t zero;
126  uint8_t family;
127  uint16_t xport;
128  uint8_t xip[16];
129 };
130 
132 {
133  bool fChangeIP;
135 };
136 
137 
142 
143 
144 
145 #define STUN_IS_REQUEST(msg_type) (((msg_type) & 0x0110) == 0x0000)
146 #define STUN_IS_INDICATION(msg_type) (((msg_type) & 0x0110) == 0x0010)
147 #define STUN_IS_SUCCESS_RESP(msg_type) (((msg_type) & 0x0110) == 0x0100)
148 #define STUN_IS_ERR_RESP(msg_type) (((msg_type) & 0x0110) == 0x0110)
149 
150 
151 
153 {
154  uint8_t id[STUN_TRANSACTION_ID_LENGTH]; // first four bytes is usually the magic cookie field
155 };
156 
157 inline bool operator==(const StunTransactionId &id1, const StunTransactionId &id2)
158 {
159  return (0 == memcmp(id1.id, id2.id, STUN_TRANSACTION_ID_LENGTH));
160 }
161 
162 
163 
164 
165 const uint32_t STUN_COOKIE = 0x2112A442;
166 const uint8_t STUN_COOKIE_B1 = 0x21;
167 const uint8_t STUN_COOKIE_B2 = 0x12;
168 const uint8_t STUN_COOKIE_B3 = 0xA4;
169 const uint8_t STUN_COOKIE_B4 = 0x42;
170 
171 
172 const uint32_t STUN_FINGERPRINT_XOR = 0x5354554e;
173 
174 const uint16_t STUN_XOR_PORT_COOKIE = 0x2112;
175 
176 const uint32_t STUN_HEADER_SIZE = 20;
177 
178 const uint32_t MAX_STUN_MESSAGE_SIZE = 800; // some reasonable length
179 const uint32_t MAX_STUN_ATTRIBUTE_SIZE = 780; // more than reasonable
180 
181 
182 #endif
const uint32_t MAX_STUN_MESSAGE_SIZE
Definition: stuntypes.h:178
const uint16_t STUN_ATTRIBUTE_USERNAME
Definition: stuntypes.h:39
uint16_t offset
Definition: stuntypes.h:112
const uint16_t STUN_ATTRIBUTE_MAPPEDADDRESS_SIZE_IPV6
Definition: stuntypes.h:139
const uint8_t STUN_COOKIE_B3
Definition: stuntypes.h:168
const uint16_t STUN_ATTRIBUTE_ERRORCODE
Definition: stuntypes.h:45
const uint16_t STUN_ERROR_STALENONCE
Definition: stuntypes.h:88
const uint16_t STUN_ERROR_UNAUTHORIZED
Definition: stuntypes.h:86
const uint16_t STUN_IPV6_LENGTH
Definition: stuntypes.h:81
const uint16_t STUN_ATTRIBUTE_PADDING
Definition: stuntypes.h:55
const uint16_t STUN_ATTRIBUTE_RESPONSEADDRESS
Definition: stuntypes.h:34
const uint16_t STUN_ATTRIBUTE_RESPONSE_ORIGIN
Definition: stuntypes.h:69
const uint8_t STUN_COOKIE_B1
Definition: stuntypes.h:166
const uint32_t STUN_COOKIE
Definition: stuntypes.h:165
const uint16_t STUN_ATTRIBUTE_REALM
Definition: stuntypes.h:51
const uint8_t STUN_COOKIE_B2
Definition: stuntypes.h:167
const uint8_t STUN_COOKIE_B4
Definition: stuntypes.h:169
const uint16_t STUN_ATTRIBUTE_FINGERPRINT
Definition: stuntypes.h:67
const uint16_t STUN_ATTRIBUTE_NONCE
Definition: stuntypes.h:52
const uint16_t STUN_ERROR_BADREQUEST
Definition: stuntypes.h:85
const uint16_t STUN_ATTRIBUTE_UNKNOWNATTRIBUTES
Definition: stuntypes.h:46
const uint16_t STUN_ATTRIBUTE_CHANGEREQUEST
Definition: stuntypes.h:35
const uint16_t STUN_ATTRIBUTE_XORMAPPEDADDRESS
Definition: stuntypes.h:53
bool operator==(const StunTransactionId &id1, const StunTransactionId &id2)
Definition: stuntypes.h:157
const uint16_t STUN_ATTRIBUTE_RESPONSE_PORT
Definition: stuntypes.h:56
const uint16_t STUN_IPV4_LENGTH
Definition: stuntypes.h:80
uint16_t attributeType
Definition: stuntypes.h:110
const uint16_t STUN_ATTRIBUTE_MAPPEDADDRESS
Definition: stuntypes.h:33
const uint16_t STUN_ATTRIBUTE_RESPONSE_PORT_SIZE
Definition: stuntypes.h:140
const uint16_t STUN_ATTRIBUTE_REFLECTEDFROM
Definition: stuntypes.h:48
const uint16_t DEFAULT_STUN_PORT
Definition: stuntypes.h:29
const uint16_t STUN_XOR_PORT_COOKIE
Definition: stuntypes.h:174
const uint16_t STUN_ATTRIBUTE_SOFTWARE
Definition: stuntypes.h:64
StunMessageClass
Definition: stuntypes.h:93
const uint16_t STUN_ATTRIBUTE_CHANGEDADDRESS
Definition: stuntypes.h:37
const uint16_t STUN_ERROR_UNKNOWNATTRIB
Definition: stuntypes.h:87
const uint32_t MAX_STUN_ATTRIBUTE_SIZE
Definition: stuntypes.h:179
const uint8_t STUN_ATTRIBUTE_FIELD_IPV4
Definition: stuntypes.h:77
StunMessageType
Definition: stuntypes.h:102
const uint16_t STUN_ATTRIBUTE_XORMAPPEDADDRESS_OPTIONAL
Definition: stuntypes.h:62
uint8_t id[STUN_TRANSACTION_ID_LENGTH]
Definition: stuntypes.h:154
const uint16_t DEFAULT_STUN_TLS_PORT
Definition: stuntypes.h:30
const uint16_t STUN_ATTRIBUTE_SOURCEADDRESS
Definition: stuntypes.h:36
const uint16_t STUN_TRANSACTION_ID_LENGTH
Definition: stuntypes.h:75
const uint16_t STUN_ATTRIBUTE_CHANGEREQUEST_SIZE
Definition: stuntypes.h:141
const uint16_t STUN_ATTRIBUTE_MESSAGEINTEGRITY
Definition: stuntypes.h:44
const uint16_t STUN_ERROR_SERVERERROR
Definition: stuntypes.h:89
const uint16_t STUN_ATTRIBUTE_MAPPEDADDRESS_SIZE_IPV4
Definition: stuntypes.h:138
const uint32_t STUN_FINGERPRINT_XOR
Definition: stuntypes.h:172
const uint16_t STUN_ATTRIBUTE_LEGACY_PASSWORD
Definition: stuntypes.h:41
const uint16_t STUN_ATTRIBUTE_ALTERNATESERVER
Definition: stuntypes.h:65
const uint16_t STUN_ERROR_TRYALTERNATE
Definition: stuntypes.h:84
uint16_t size
Definition: stuntypes.h:111
const uint32_t STUN_HEADER_SIZE
Definition: stuntypes.h:176
const uint8_t STUN_ATTRIBUTE_FIELD_IPV6
Definition: stuntypes.h:78
const uint16_t STUN_ATTRIBUTE_OTHER_ADDRESS
Definition: stuntypes.h:70