Stun Server  Compliant with the latest RFCs including 5389, 5769, and 5780
discover the local host's own external IP address
socketaddress.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_SOCK_ADDR_H
20 #define STUN_SOCK_ADDR_H
21 
22 #include "stuntypes.h"
23 
25 {
26  sockaddr addr;
27  sockaddr_in addr4;
28  sockaddr_in6 addr6;
29 };
30 
31 
33 {
35 
36  size_t GetIPImpl(void* pAddr, size_t length, bool fNetworkByteOrder) const; // returns the number of bytes copied, or Zero on error
37 
38 
39 public:
41 
42  CSocketAddress(const sockaddr& addr);
43  CSocketAddress(const sockaddr_storage& addr);
44  void CommonConstructor(const sockaddr& addr);
45 
46  CSocketAddress(const sockaddr_in& addr);
47  CSocketAddress(const sockaddr_in6& addr6);
48 
49  // both IP and PORT are passed in HOST BYTE ORDER
50  CSocketAddress(uint32_t ipHostByteOrder, uint16_t port);
51 
52 
53  uint16_t GetPort() const;
54  uint16_t GetPort_NBO() const; // network byte order
55  void SetPort(uint16_t);
56 
57  uint16_t GetIPLength() const; // either returns 4 or 16 for IPV4 and IPV6 respectively
58 
59  // not sure if IPv6 has a logical "network byte order" that's different from it's normal nomenclature
60  size_t GetIP(void* pAddr, size_t length) const; // returns the number of bytes copied, or Zero on error
61  size_t GetIP_NBO(void* pAddr, size_t length) const; // returns the number of bytes copied, or Zero on error
62 
63  uint16_t GetFamily() const;
64 
65  void ApplyStunXorMap(const StunTransactionId& id);
66 
67  const sockaddr* GetSockAddr() const;
68  socklen_t GetSockAddrLength() const;
69 
70  bool IsIPAddressZero() const;
71 
72 
73  bool IsSameIP(const CSocketAddress& other) const;
74  bool IsSameIP_and_Port(const CSocketAddress& other) const;
75 
76  void ToString(std::string* pStr) const;
77  HRESULT ToStringBuffer(char* pszAddrBytes, size_t length) const;
78 
79  static HRESULT GetLocalHost(uint16_t family, CSocketAddress* pAddr);
80 
81 
82 };
83 
84 
85 
86 #endif
sockaddr_in6 addr6
Definition: socketaddress.h:28
simple_sockaddr _address
Definition: socketaddress.h:34
int32_t HRESULT
Definition: hresult.h:22
sockaddr_in addr4
Definition: socketaddress.h:27