Stun Server  Compliant with the latest RFCs including 5389, 5769, and 5780
discover the local host's own external IP address
stunutils.cpp
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 #include "commonincludes.hpp"
20 #include "stuntypes.h"
21 #include "socketaddress.h"
22 #include "buffer.h"
23 #include "datastream.h"
24 
25 
27 {
28  uint8_t zerobytes[sizeof(transid.id)] = {}; // zero-init
29  return (0 != memcmp(&transid.id, zerobytes, sizeof(zerobytes)));
30 }
31 
32 
33 HRESULT GetMappedAddress(uint8_t* pData, size_t size, CSocketAddress* pAddr)
34 {
35  uint16_t port;
36  HRESULT hr = S_OK;
37  uint8_t attributeid;
38  uint8_t ip6[STUN_IPV6_LENGTH];
39  uint32_t ip4;
40 
41 
42  CRefCountedBuffer spBuffer(new CBuffer(pData, size, false));
43  CDataStream stream(spBuffer);
44 
45  ChkIfA(pAddr==NULL, E_INVALIDARG);
46 
47  Chk(stream.SeekDirect(1)); // skip over the zero byte
48 
49  Chk(stream.ReadUint8(&attributeid));
50  Chk(stream.ReadUint16(&port));
51  port = ntohs(port);
52 
53  if (attributeid == STUN_ATTRIBUTE_FIELD_IPV4)
54  {
55  Chk(stream.ReadUint32(&ip4));
56  ip4 = ntohl(ip4);
57  *pAddr = CSocketAddress(ip4, port);
58  }
59  else
60  {
61  sockaddr_in6 addr6={};
62  Chk(stream.Read(ip6, STUN_IPV6_LENGTH));
63  addr6.sin6_family = AF_INET6;
64  addr6.sin6_port = htons(port);
65  memcpy(&addr6.sin6_addr, ip6, STUN_IPV6_LENGTH);
66  *pAddr = CSocketAddress(addr6);
67  }
68 
69 Cleanup:
70  return hr;
71 }
72 
73 HRESULT GetXorMappedAddress(uint8_t* pData, size_t size, StunTransactionId &transid, CSocketAddress* pAddr)
74 {
75 
76  HRESULT hr = S_OK;
77 
78  Chk(GetMappedAddress(pData, size, pAddr));
79 
80  pAddr->ApplyStunXorMap(transid);
81 
82 Cleanup:
83  return hr;
84 
85 
86 }
87 
#define S_OK
Definition: hresult.h:46
HRESULT ReadUint8(uint8_t *pVal)
Definition: datastream.h:58
const uint16_t STUN_IPV6_LENGTH
Definition: stuntypes.h:81
#define Chk(expr)
Definition: chkmacros.h:53
HRESULT Read(void *data, size_t size)
Definition: datastream.cpp:67
void ApplyStunXorMap(const StunTransactionId &id)
HRESULT ReadUint16(uint16_t *pVal)
Definition: datastream.h:59
Definition: buffer.h:27
HRESULT GetXorMappedAddress(uint8_t *pData, size_t size, StunTransactionId &transid, CSocketAddress *pAddr)
Definition: stunutils.cpp:73
int32_t HRESULT
Definition: hresult.h:22
HRESULT GetMappedAddress(uint8_t *pData, size_t size, CSocketAddress *pAddr)
Definition: stunutils.cpp:33
const uint8_t STUN_ATTRIBUTE_FIELD_IPV4
Definition: stuntypes.h:77
bool IsTransactionIdValid(StunTransactionId &transid)
Definition: stunutils.cpp:26
#define E_INVALIDARG
Definition: hresult.h:51
uint8_t id[STUN_TRANSACTION_ID_LENGTH]
Definition: stuntypes.h:154
HRESULT SeekDirect(size_t pos)
Definition: datastream.cpp:187
HRESULT ReadUint32(uint32_t *pVal)
Definition: datastream.h:60
boost::shared_ptr< CBuffer > CRefCountedBuffer
Definition: buffer.h:65
#define ChkIfA(expr, hrerror)
Definition: chkmacros.h:84