Added full IPv6 support.

This commit is contained in:
XMRig 2018-03-07 16:38:58 +07:00
parent 79779b51da
commit 8a6988d381
10 changed files with 200 additions and 77 deletions

View file

@ -4,8 +4,8 @@
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2016-2017 XMRig <support@xmrig.com>
*
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2016-2018 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -41,6 +41,7 @@ Url::Url() :
m_host(nullptr),
m_password(nullptr),
m_user(nullptr),
m_url(nullptr),
m_port(kDefaultPort)
{
}
@ -63,6 +64,7 @@ Url::Url(const char *url) :
m_host(nullptr),
m_password(nullptr),
m_user(nullptr),
m_url(nullptr),
m_port(kDefaultPort)
{
parse(url);
@ -74,6 +76,7 @@ Url::Url(const char *host, uint16_t port, const char *user, const char *password
m_nicehash(nicehash),
m_password(password ? strdup(password) : nullptr),
m_user(user ? strdup(user) : nullptr),
m_url(nullptr),
m_port(port)
{
m_host = strdup(host);
@ -85,6 +88,10 @@ Url::~Url()
free(m_host);
free(m_password);
free(m_user);
if (m_url) {
delete [] m_url;
}
}
@ -105,6 +112,10 @@ bool Url::parse(const char *url)
return false;
}
if (base[0] == '[') {
return parseIPv6(base);
}
const char *port = strchr(base, ':');
if (!port) {
m_host = strdup(base);
@ -112,9 +123,8 @@ bool Url::parse(const char *url)
}
const size_t size = port++ - base + 1;
m_host = static_cast<char*>(malloc(size));
m_host = new char[size]();
memcpy(m_host, base, size - 1);
m_host[size - 1] = '\0';
m_port = (uint16_t) strtol(port, nullptr, 10);
return true;
@ -139,6 +149,19 @@ bool Url::setUserpass(const char *userpass)
}
const char *Url::url() const
{
if (!m_url) {
const size_t size = strlen(m_host) + 8;
m_url = new char[size];
snprintf(m_url, size - 1, "%s:%d", m_host, m_port);
}
return m_url;
}
void Url::applyExceptions()
{
if (!isValid()) {
@ -178,6 +201,20 @@ void Url::setUser(const char *user)
}
bool Url::operator==(const Url &other) const
{
if (m_port != other.m_port || m_keepAlive != other.m_keepAlive || m_nicehash != other.m_nicehash) {
return false;
}
if (strcmp(host(), other.host()) != 0 || strcmp(user(), other.user()) != 0 || strcmp(password(), other.password()) != 0) {
return false;
}
return true;
}
Url &Url::operator=(const Url *other)
{
m_keepAlive = other->m_keepAlive;
@ -192,3 +229,25 @@ Url &Url::operator=(const Url *other)
return *this;
}
bool Url::parseIPv6(const char *addr)
{
const char *end = strchr(addr, ']');
if (!end) {
return false;
}
const char *port = strchr(end, ':');
if (!port) {
return false;
}
const size_t size = end - addr;
m_host = new char[size]();
memcpy(m_host, addr + 1, size - 1);
m_port = (uint16_t) strtol(port + 1, nullptr, 10);
return true;
}