Stun Server  Compliant with the latest RFCs including 5389, 5769, and 5780
discover the local host's own external IP address
resolvehostname.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 "socketaddress.h"
21 #include "stringhelper.h"
22 
23 
24 HRESULT ResolveHostName(const char* pszHostName, int family, bool fNumericOnly, CSocketAddress* pAddr)
25 {
26 
27  int ret;
28  HRESULT hr = S_OK;
29  addrinfo* pResultList = NULL;
30 
31  addrinfo hints = {};
32 
33  std::string strHostName(pszHostName);
34  StringHelper::Trim(strHostName);
35 
36  ChkIf(strHostName.length() == 0, E_INVALIDARG);
37  ChkIf(pAddr==NULL, E_INVALIDARG);
38 
39  hints.ai_family = family;
40  if (fNumericOnly)
41  {
42  hints.ai_flags = AI_NUMERICHOST;
43  }
44 
45  // without a socktype hint, getaddrinfo will return 3x the number of addresses (SOCK_STREAM, SOCK_DGRAM, and SOCK_RAW)
46  hints.ai_socktype = SOCK_STREAM;
47 
48  ret = getaddrinfo(strHostName.c_str(), NULL, &hints, &pResultList);
49 
50  ChkIf(ret != 0, ERRNO_TO_HRESULT(ret));
51  ChkIf(pResultList==NULL, E_FAIL)
52 
53  // just pick the first one found
54  *pAddr = CSocketAddress(*(pResultList->ai_addr));
55 
56 Cleanup:
57 
58  if (pResultList != NULL) // android will crash if passed NULL
59  {
60  ::freeaddrinfo(pResultList);
61  }
62 
63  return hr;
64 
65 }
66 
67 HRESULT NumericIPToAddress(int family, const char* pszIP, CSocketAddress* pAddr)
68 {
69  HRESULT hr = S_OK;
70 
71  ChkIf((family != AF_INET) && (family != AF_INET6), E_INVALIDARG);
72 
73  if (family == AF_INET)
74  {
75  sockaddr_in addr4 = {};
76  ChkIf(0 == ::inet_pton(family, pszIP, &addr4.sin_addr), E_FAIL);
77  addr4.sin_family = family;
78  *pAddr = CSocketAddress(addr4);
79  }
80  else
81  {
82  sockaddr_in6 addr6 = {};
83  ChkIf(0 == ::inet_pton(family, pszIP, &addr6.sin6_addr), E_FAIL);
84  addr6.sin6_family = family;
85  *pAddr = CSocketAddress(addr6);
86  }
87 
88 Cleanup:
89 
90  return hr;
91 
92 }
93 
94 
95 
96 
#define S_OK
Definition: hresult.h:46
HRESULT ResolveHostName(const char *pszHostName, int family, bool fNumericOnly, CSocketAddress *pAddr)
#define ERRNO_TO_HRESULT(err)
Definition: hresult.h:41
#define ChkIf(expr, hrerror)
Definition: chkmacros.h:63
HRESULT NumericIPToAddress(int family, const char *pszIP, CSocketAddress *pAddr)
void Trim(std::string &str)
int32_t HRESULT
Definition: hresult.h:22
#define E_INVALIDARG
Definition: hresult.h:51
#define E_FAIL
Definition: hresult.h:56