Stun Server  Compliant with the latest RFCs including 5389, 5769, and 5780
discover the local host's own external IP address
stunreader.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 STUN_MESSAGE_READER_H
20 #define STUN_MESSAGE_READER_H
21 
22 
23 
24 #include "stuntypes.h"
25 #include "datastream.h"
26 #include "socketaddress.h"
27 #include "fasthash.h"
28 
29 
31 {
32 public:
34  {
39  };
40 
41 private:
43 
44  bool _fAllowLegacyFormat; // if true, allows for messages of type RFC 3489 (no magic cookie) to be accepted
45  bool _fMessageIsLegacyFormat; // set by readheader - true if the stun_magic_cookie is missing, but the message appears to be intact otherwise
46 
48 
49  static const size_t MAX_NUM_ATTRIBUTES = 30;
50 
51  typedef FastHash<uint16_t, StunAttribute, MAX_NUM_ATTRIBUTES, 53> AttributeHashTable; // 53 is a prime number for a reasonable table width
52 
53  AttributeHashTable _mapAttributes;
54 
55  // special index values for message integrity attribute validation
59 
60 
64  uint16_t _msgLength;
65 
67  HRESULT ReadBody();
68 
69  HRESULT GetAddressHelper(uint16_t attribType, CSocketAddress* pAddr);
70 
71  HRESULT ValidateMessageIntegrity(uint8_t* key, size_t keylength);
72 
73 public:
75 
76  void Reset();
77 
78  void SetAllowLegacyFormat(bool fAllowLegacyFormat);
79 
80  ReaderParseState AddBytes(const uint8_t* pData, uint32_t size);
81  uint16_t HowManyBytesNeeded();
83 
84  bool IsMessageLegacyFormat();
85 
88 
90  HRESULT ValidateMessageIntegrityShort(const char* pszPassword);
91  HRESULT ValidateMessageIntegrityLong(const char* pszUser, const char* pszRealm, const char* pszPassword);
92 
93  HRESULT GetAttributeByType(uint16_t attributeType, StunAttribute* pAttribute);
94  HRESULT GetAttributeByIndex(int index, StunAttribute* pAttribute);
95  int GetAttributeCount();
96 
97  void GetTransactionId(StunTransactionId* pTransId );
99  uint16_t GetMessageType();
100 
101  HRESULT GetResponsePort(uint16_t *pPort);
103  HRESULT GetPaddingAttributeSize(uint16_t* pSizePadding);
104  HRESULT GetErrorCode(uint16_t* pErrorNumber);
105 
106  HRESULT GetBuffer(CRefCountedBuffer* pRefBuffer);
107 
108 
113 
114  HRESULT GetStringAttributeByType(uint16_t attributeType, char* pszValue, /*in-out*/ size_t size);
115 
116 
118 
119 };
120 
121 #endif
FastHash< uint16_t, StunAttribute, MAX_NUM_ATTRIBUTES, 53 > AttributeHashTable
Definition: stunreader.h:51
HRESULT GetAddressHelper(uint16_t attribType, CSocketAddress *pAddr)
Definition: stunreader.cpp:489
HRESULT GetChangeRequest(StunChangeRequestAttribute *pChangeRequest)
Definition: stunreader.cpp:407
CDataStream & GetStream()
Definition: stunreader.cpp:855
uint16_t GetMessageType()
Definition: stunreader.cpp:839
HRESULT GetAttributeByType(uint16_t attributeType, StunAttribute *pAttribute)
Definition: stunreader.cpp:362
HRESULT ValidateMessageIntegrityLong(const char *pszUser, const char *pszRealm, const char *pszPassword)
Definition: stunreader.cpp:282
ReaderParseState _state
Definition: stunreader.h:47
HRESULT GetBuffer(CRefCountedBuffer *pRefBuffer)
Definition: stunreader.cpp:844
void GetTransactionId(StunTransactionId *pTransId)
Definition: stunreader.cpp:825
bool HasFingerprintAttribute()
Definition: stunreader.cpp:93
HRESULT GetMappedAddress(CSocketAddress *pAddress)
Definition: stunreader.cpp:505
StunMessageClass _msgClass
Definition: stunreader.h:63
ReaderParseState AddBytes(const uint8_t *pData, uint32_t size)
Definition: stunreader.cpp:750
bool IsMessageLegacyFormat()
Definition: stunreader.cpp:71
HRESULT GetOtherAddress(CSocketAddress *pAddress)
Definition: stunreader.cpp:513
uint16_t _msgLength
Definition: stunreader.h:64
CDataStream _stream
Definition: stunreader.h:42
uint16_t _msgTypeNormalized
Definition: stunreader.h:62
ReaderParseState GetState()
Definition: stunreader.cpp:820
HRESULT GetErrorCode(uint16_t *pErrorNumber)
Definition: stunreader.cpp:461
HRESULT ValidateMessageIntegrityShort(const char *pszPassword)
Definition: stunreader.cpp:277
int32_t HRESULT
Definition: hresult.h:22
HRESULT GetResponsePort(uint16_t *pPort)
Definition: stunreader.cpp:384
HRESULT GetPaddingAttributeSize(uint16_t *pSizePadding)
Definition: stunreader.cpp:442
StunMessageClass
Definition: stuntypes.h:93
HRESULT ValidateMessageIntegrity(uint8_t *key, size_t keylength)
Definition: stunreader.cpp:145
bool IsFingerprintAttributeValid()
Definition: stunreader.cpp:99
bool _fMessageIsLegacyFormat
Definition: stunreader.h:45
StunMessageClass GetMessageClass()
Definition: stunreader.cpp:834
bool HasMessageIntegrityAttribute()
Definition: stunreader.cpp:140
static const size_t MAX_NUM_ATTRIBUTES
Definition: stunreader.h:49
HRESULT ReadHeader()
Definition: stunreader.cpp:586
boost::shared_ptr< CBuffer > CRefCountedBuffer
Definition: buffer.h:65
void SetAllowLegacyFormat(bool fAllowLegacyFormat)
Definition: stunreader.cpp:66
bool _fAllowLegacyFormat
Definition: stunreader.h:44
HRESULT GetStringAttributeByType(uint16_t attributeType, char *pszValue, size_t size)
Definition: stunreader.cpp:565
StunTransactionId _transactionid
Definition: stunreader.h:61
AttributeHashTable _mapAttributes
Definition: stunreader.h:53
uint16_t HowManyBytesNeeded()
Definition: stunreader.cpp:76
HRESULT GetAttributeByIndex(int index, StunAttribute *pAttribute)
Definition: stunreader.cpp:346
HRESULT GetXorMappedAddress(CSocketAddress *pAddress)
Definition: stunreader.cpp:529
int _indexMessageIntegrity
Definition: stunreader.h:57
HRESULT GetResponseOriginAddress(CSocketAddress *pAddress)
Definition: stunreader.cpp:548