30 #define STUN_MAX_STRING 256
31 #define STUN_MAX_UNKNOWN_ATTRIBUTES 8
32 #define STUN_MAX_MESSAGE_SIZE 2048
33 #define STUN_PORT 3478
37 #include "IPEndPoint.h"
38 #include "Uninetwork.h"
57 NAT_OpenInternet = 255,
62 NAT_SymmetricUdpFirewall = 20,
77 NAT_RestrictedCone = 150,
85 NAT_PortRestrictedCone = 100,
106 ::std::list<sp2plib::UInt16> MappedPorts;
120 static StunResponse Query(Socket sock,
char* host, sp2plib::UInt16 port = STUN_PORT , sp2plib::UInt16 StunResponseTimeout = 450);
127 static StunResponse Query(Socket sock,
IP4Address srvAddr, sp2plib::UInt16 port = STUN_PORT , sp2plib::UInt16 StunResponseTimeout = 450);
135 static int PortQuery(::std::map< ::std::string,IPEndPoint> *result, Socket s,
IPEndPoint stunEP,
int maxWait = 2000);
143 static int PortQueryUniquePorts(::std::set<sp2plib::UInt16> *result, Socket s,
IPEndPoint stunEP,
int maxWait = 2000);
147 static ::std::string NATTypeName(
NAT_Type NType);
151 typedef enum StunAttributeType {
152 SAT_MappedAddress = 0x0001,
153 SAT_ResponseAddress = 0x0002,
154 SAT_ChangeRequest = 0x0003,
155 SAT_SourceAddress = 0x0004,
156 SAT_ChangedAddress = 0x0005,
157 SAT_Username = 0x0006,
158 SAT_Password = 0x0007,
159 SAT_MessageIntegrity = 0x0008,
160 SAT_ErrorCode = 0x0009,
161 SAT_UnknownAttribute = 0x000A,
162 SAT_ReflectedFrom = 0x000B,
163 SAT_XorMappedAddress = 0x8020,
164 SAT_XorOnly = 0x0021,
165 SAT_ServerName = 0x8022,
166 SAT_SecondaryAddress = 0x8050
169 typedef enum StunMessageType {
170 SMT_BindRequestMsg = 0x0001,
171 SMT_BindResponseMsg = 0x0101,
172 SMT_BindErrorResponseMsg = 0x0111,
173 SMT_SharedSecretRequestMsg = 0x0002,
174 SMT_SharedSecretResponseMsg = 0x0102,
175 SMT_SharedSecretErrorResponseMsg = 0x0112
180 sp2plib::UInt16 Type;
181 sp2plib::UInt16 Length;
188 sp2plib::UInt16 Type;
189 sp2plib::UInt16 Length;
190 } StunAttributeHeader;
196 sp2plib::UInt8 family;
198 } StunAttributeAddress;
202 sp2plib::UInt32 value;
203 } StunAttributeChangeRequest;
208 sp2plib::UInt8 errorClass;
209 sp2plib::UInt8 number;
210 char reason[STUN_MAX_STRING];
211 sp2plib::UInt16 sizeReason;
212 } StunAttributeError;
216 sp2plib::UInt16 attrType[STUN_MAX_UNKNOWN_ATTRIBUTES];
217 sp2plib::UInt16 numAttributes;
218 } StunAttributeUnknown;
222 char value[STUN_MAX_STRING];
223 sp2plib::UInt16 sizeValue;
224 } StunAttributeString;
228 StunMessageHeader Header;
230 bool hasMappedAddress;
231 StunAttributeAddress mappedAddress;
233 bool hasResponseAddress;
234 StunAttributeAddress responseAddress;
236 bool hasChangeRequest;
237 StunAttributeChangeRequest changeRequest;
239 bool hasSourceAddress;
240 StunAttributeAddress sourceAddress;
242 bool hasChangedAddress;
243 StunAttributeAddress changedAddress;
246 StunAttributeString username;
249 StunAttributeString password;
252 StunAttributeError errorCode;
254 bool hasUnknownAttributes;
255 StunAttributeUnknown unknownAttributes;
257 bool hasReflectedFrom;
258 StunAttributeAddress reflectedFrom;
260 bool hasXorMappedAddress;
261 StunAttributeAddress xorMappedAddress;
266 StunAttributeString serverName;
268 bool hasSecondaryAddress;
269 StunAttributeAddress secondaryAddress;
273 static const sp2plib::UInt32 ChangeIpFlag = 0x04;
274 static const sp2plib::UInt32 ChangePortFlag = 0x02;
275 static const sp2plib::UInt8 IPv4Family = 0x01;
276 static const sp2plib::UInt8 IPv6Family = 0x02;
277 static const int HeaderSize = 20;
280 static StunMessage * new_DoTransaction(StunMessage * request, Socket sock, IPEndPoint *ServerEndPoint,sp2plib::UInt16 StunResponseTimeout);
281 static char* encode16(
char* buf, sp2plib::UInt16 data);
282 static char* encode32(
char* buf, sp2plib::UInt32 data);
283 static char* encode(
char* buf,
const char* data,
unsigned int length);
284 static char* encodeAddress(
char* ptr, sp2plib::UInt16 type,StunAttributeAddress Attribute);
285 static char* encodeChangeRequest(
char* ptr, StunAttributeChangeRequest Attribute );
286 static char* encodeError(
char* ptr, StunAttributeError Attribute);
287 static char* encodeUnknown(
char* ptr, StunAttributeUnknown Attribute);
288 static char* encodeXorOnly(
char* ptr);
289 static char* encodeString(
char* ptr, sp2plib::UInt16 type, StunAttributeString Attribute);
290 static unsigned int serializeMessage(
const StunMessage &message,
292 unsigned int buffLength,
293 const StunAttributeString *password = NULL
295 static bool ParseAddress(
char* body,
unsigned int headerLen, StunAttributeAddress &result );
296 static bool ParseChangeRequest(
char* body,
unsigned int headerLen, StunAttributeChangeRequest &result );
297 static bool ParseError(
char* body,
unsigned int headerLen, StunAttributeError &result );
298 static bool ParseUnknown(
char* body,
unsigned int headerLen, StunAttributeUnknown &result );
299 static bool ParseString(
char* body,
unsigned int headerLen, StunAttributeString &result );
300 static bool ParseMessage(
char* buff,
unsigned int buffLength, StunMessage& message);
301 static bool CreateRequest( StunMessage* message,
bool changePort,
bool changeIp, sp2plib::UInt128 *ID = NULL);