128 lines
3.3 KiB
C
128 lines
3.3 KiB
C
|
|
/****************************************************************************
|
|||
|
|
Copyright (c) 2014 Lijunlin - Jason lee
|
|||
|
|
|
|||
|
|
Created by Lijunlin - Jason lee on 2014
|
|||
|
|
|
|||
|
|
jason.lee.c@foxmail.com
|
|||
|
|
http://www.cocos2d-x.org
|
|||
|
|
|
|||
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|||
|
|
of this software and associated documentation files (the "Software"), to deal
|
|||
|
|
in the Software without restriction, including without limitation the rights
|
|||
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|||
|
|
copies of the Software, and to permit persons to whom the Software is
|
|||
|
|
furnished to do so, subject to the following conditions:
|
|||
|
|
|
|||
|
|
The above copyright notice and this permission notice shall be included in
|
|||
|
|
all copies or substantial portions of the Software.
|
|||
|
|
|
|||
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|||
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|||
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|||
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|||
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|||
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|||
|
|
THE SOFTWARE.
|
|||
|
|
****************************************************************************/
|
|||
|
|
#ifndef __CCNET_INETADDRESS_H__
|
|||
|
|
#define __CCNET_INETADDRESS_H__
|
|||
|
|
|
|||
|
|
#include "cocos2d.h"
|
|||
|
|
#include "NetDefine.h"
|
|||
|
|
#include <string>
|
|||
|
|
|
|||
|
|
NS_CC_BEGIN
|
|||
|
|
|
|||
|
|
// dns parse
|
|||
|
|
static std::string domainToIP(const char* pDomain)
|
|||
|
|
{
|
|||
|
|
std::vector<std::string> ips;
|
|||
|
|
|
|||
|
|
struct addrinfo addr;
|
|||
|
|
struct addrinfo* pAddr = &addr;
|
|||
|
|
|
|||
|
|
struct addrinfo hints;
|
|||
|
|
memset(&hints, 0x00, sizeof(hints));
|
|||
|
|
hints.ai_flags = AI_PASSIVE;
|
|||
|
|
hints.ai_family = AF_UNSPEC;
|
|||
|
|
hints.ai_socktype = SOCK_STREAM;
|
|||
|
|
hints.ai_protocol = IPPROTO_IP;
|
|||
|
|
|
|||
|
|
int nRet = getaddrinfo(pDomain, nullptr, &hints, &pAddr);
|
|||
|
|
|
|||
|
|
do
|
|||
|
|
{
|
|||
|
|
// 信息获取失败
|
|||
|
|
if (nRet != 0)
|
|||
|
|
break;
|
|||
|
|
|
|||
|
|
// ipv4
|
|||
|
|
if (pAddr->ai_family == AF_INET)
|
|||
|
|
{
|
|||
|
|
struct sockaddr_in* ipv4 = nullptr;
|
|||
|
|
char str[16] = { 0 };
|
|||
|
|
for (auto iter = pAddr; iter != nullptr; iter = iter->ai_next)
|
|||
|
|
{
|
|||
|
|
ipv4 = (struct sockaddr_in*)iter->ai_addr;
|
|||
|
|
inet_ntop(AF_INET, &ipv4->sin_addr, str, 32);
|
|||
|
|
ips.push_back(str);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// ipv6
|
|||
|
|
else if (pAddr->ai_family == AF_INET6)
|
|||
|
|
{
|
|||
|
|
struct sockaddr_in6* ipv6 = nullptr;
|
|||
|
|
char str[40] = { 0 };
|
|||
|
|
for (auto iter = pAddr; iter != nullptr; iter = iter->ai_next)
|
|||
|
|
{
|
|||
|
|
ipv6 = (struct sockaddr_in6*)iter->ai_addr;
|
|||
|
|
inet_ntop(AF_INET6, &ipv6->sin6_addr, str, 32);
|
|||
|
|
ips.push_back(str);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
break;
|
|||
|
|
} while (0);
|
|||
|
|
|
|||
|
|
if (ips.size() == 0)
|
|||
|
|
{
|
|||
|
|
return "";
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return ips[0];
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* calss : CCInetAddress
|
|||
|
|
* author : Jason lee
|
|||
|
|
* email : jason.lee.c@foxmail.com
|
|||
|
|
* descpt : address define
|
|||
|
|
*/
|
|||
|
|
class ClientInetAddress : public sockaddr_in
|
|||
|
|
{
|
|||
|
|
public:
|
|||
|
|
ClientInetAddress();
|
|||
|
|
ClientInetAddress(const char* ip, unsigned short port);
|
|||
|
|
ClientInetAddress(const struct sockaddr* addr);
|
|||
|
|
virtual ~ClientInetAddress(void);
|
|||
|
|
|
|||
|
|
public:
|
|||
|
|
operator struct sockaddr*();
|
|||
|
|
operator const struct sockaddr*() const;
|
|||
|
|
const char* getHostAddress() const;
|
|||
|
|
const char* getIp() const;
|
|||
|
|
unsigned short getPort() const;
|
|||
|
|
void setIp(const char* ip);
|
|||
|
|
void setIp(unsigned int ip);
|
|||
|
|
void setPort(unsigned short port);
|
|||
|
|
void setHost(const char* name);
|
|||
|
|
|
|||
|
|
public:
|
|||
|
|
int getLength();
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
NS_CC_END
|
|||
|
|
|
|||
|
|
|
|||
|
|
#endif //__CCNET_INETADDRESS_H__
|