mirror of
https://github.com/g4klx/MMDVMHost
synced 2025-12-21 15:09:23 +08:00
rewrite with getaddrinfo() API to support IPv6
Currently LCDproc seems to be IPv6 unsupported, but this code is modified for the future IPv6-supported LCDproc.
This commit is contained in:
57
LCDproc.cpp
57
LCDproc.cpp
@@ -121,44 +121,53 @@ CLCDproc::~CLCDproc()
|
|||||||
|
|
||||||
bool CLCDproc::open()
|
bool CLCDproc::open()
|
||||||
{
|
{
|
||||||
const char *server;
|
int err;
|
||||||
unsigned int port, localPort;
|
unsigned int addrlen;
|
||||||
struct sockaddr_in serverAddress, clientAddress;
|
std::string port, localPort;
|
||||||
struct hostent *h;
|
struct sockaddr_storage serverAddress, clientAddress;
|
||||||
|
struct addrinfo hints, *res;
|
||||||
|
|
||||||
server = m_address.c_str();
|
port = std::to_string(m_port);
|
||||||
port = m_port;
|
localPort = std::to_string(m_localPort);
|
||||||
localPort = m_localPort;
|
memset(&hints, 0, sizeof(hints));
|
||||||
|
|
||||||
|
/* Lookup the hostname address */
|
||||||
|
hints.ai_flags = AI_NUMERICSERV;
|
||||||
|
hints.ai_socktype = SOCK_STREAM;
|
||||||
|
err = getaddrinfo(m_address.c_str(), port.c_str(), &hints, &res);
|
||||||
|
if (err) {
|
||||||
|
LogError("LCDproc, cannot lookup server");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
memcpy(&serverAddress, res->ai_addr, addrlen = res->ai_addrlen);
|
||||||
|
freeaddrinfo(res);
|
||||||
|
|
||||||
|
/* Lookup the client address (random port - need to specify manual port from ini file) */
|
||||||
|
hints.ai_flags = AI_NUMERICSERV | AI_PASSIVE;
|
||||||
|
hints.ai_family = serverAddress.ss_family;
|
||||||
|
err = getaddrinfo(NULL, localPort.c_str(), &hints, &res);
|
||||||
|
if (err) {
|
||||||
|
LogError("LCDproc, cannot lookup client");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
memcpy(&clientAddress, res->ai_addr, res->ai_addrlen);
|
||||||
|
freeaddrinfo(res);
|
||||||
|
|
||||||
/* Create TCP socket */
|
/* Create TCP socket */
|
||||||
m_socketfd = socket(AF_INET, SOCK_STREAM, 0);
|
m_socketfd = socket(clientAddress.ss_family, SOCK_STREAM, 0);
|
||||||
if (m_socketfd == -1) {
|
if (m_socketfd == -1) {
|
||||||
LogError("LCDproc, failed to create socket");
|
LogError("LCDproc, failed to create socket");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Sets client address (random port - need to specify manual port from ini file?) */
|
|
||||||
clientAddress.sin_family = AF_INET;
|
|
||||||
clientAddress.sin_addr.s_addr = htonl(INADDR_ANY);
|
|
||||||
//clientAddress.sin_port = htons(0);
|
|
||||||
clientAddress.sin_port = htons(localPort);
|
|
||||||
|
|
||||||
/* Bind the address to the socket */
|
/* Bind the address to the socket */
|
||||||
if (bind(m_socketfd, (struct sockaddr *)&clientAddress, sizeof(clientAddress)) == -1) {
|
if (bind(m_socketfd, (struct sockaddr *)&clientAddress, addrlen) == -1) {
|
||||||
LogError("LCDproc, error whilst binding address");
|
LogError("LCDproc, error whilst binding address");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Lookup the hostname address */
|
/* Connect to server */
|
||||||
h = gethostbyname(server);
|
if (connect(m_socketfd, (struct sockaddr *)&serverAddress, addrlen) == -1) {
|
||||||
|
|
||||||
/* Sets server address */
|
|
||||||
serverAddress.sin_family = h->h_addrtype;
|
|
||||||
memcpy((char*)&serverAddress.sin_addr.s_addr, h->h_addr_list[0], h->h_length);
|
|
||||||
serverAddress.sin_port = htons(port);
|
|
||||||
|
|
||||||
if (connect(m_socketfd, (struct sockaddr * )&serverAddress, sizeof(serverAddress))==-1) {
|
|
||||||
LogError("LCDproc, cannot connect to server");
|
LogError("LCDproc, cannot connect to server");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user