Subversion Repositories tpanel

Rev

Rev 464 | Rev 472 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 464 Rev 465
Line 16... Line 16...
16
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
16
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
17
 */
17
 */
18
 
18
 
19
#include <chrono>
19
#include <chrono>
20
#include <thread>
20
#include <thread>
-
 
21
#include <cstring>
-
 
22
#include <sstream>
21
 
23
 
22
#include "tsocket.h"
24
#include "tsocket.h"
23
#include "terror.h"
25
#include "terror.h"
24
#include "tconfig.h"
26
#include "tconfig.h"
25
#include "texcept.h"
27
#include "texcept.h"
26
 
28
 
27
#include <string.h>
-
 
28
#include <strings.h>
29
#include <strings.h>
29
#include <sys/socket.h>
30
#include <sys/socket.h>
30
#include <sys/types.h>
31
#include <sys/types.h>
31
#include <unistd.h>
32
#include <unistd.h>
32
#include <stdlib.h>
33
#include <stdlib.h>
Line 39... Line 40...
39
#include <arpa/inet.h>
40
#include <arpa/inet.h>
40
#include <signal.h>
41
#include <signal.h>
41
#include <poll.h>
42
#include <poll.h>
42
#include <sys/ioctl.h>
43
#include <sys/ioctl.h>
43
#include <net/if.h>
44
#include <net/if.h>
-
 
45
#include <ifaddrs.h>
44
 
46
 
45
using std::string;
47
using std::string;
-
 
48
using std::stringstream;
46
 
49
 
47
int _cert_callback(int preverify_ok, X509_STORE_CTX *ctx);
50
int _cert_callback(int preverify_ok, X509_STORE_CTX *ctx);
48
 
51
 
49
TSocket::TSocket()
52
TSocket::TSocket()
50
{
53
{
Line 166... Line 169...
166
 
169
 
167
    if (ainfo && ainfo->ai_family == AF_INET)
170
    if (ainfo && ainfo->ai_family == AF_INET)
168
    {
171
    {
169
        char str[INET_ADDRSTRLEN];
172
        char str[INET_ADDRSTRLEN];
170
        struct sockaddr_in addr;
173
        struct sockaddr_in addr;
171
        struct ifreq ninfo;
-
 
172
        socklen_t len = sizeof(addr);
174
        socklen_t len = sizeof(addr);
173
        getsockname(sock, (struct sockaddr *)&addr, &len);
175
        getsockname(sock, (struct sockaddr *)&addr, &len);
174
        mMyIP.assign(inet_ntop(AF_INET, &(addr.sin_addr), str, INET_ADDRSTRLEN));
176
        mMyIP.assign(inet_ntop(AF_INET, &(addr.sin_addr), str, INET_ADDRSTRLEN));
175
 
-
 
176
        if (ioctl(sock, SIOCGIFNETMASK, &ninfo))
177
        determineNetmask(sock);
177
        {
-
 
178
            MSG_ERROR("Error getting netmask: " << strerror(errno));
-
 
179
        }
-
 
180
        else
-
 
181
            mMyNetmask.assign(inet_ntop(AF_INET, &(ninfo.ifr_ifru.ifru_netmask), str, INET_ADDRSTRLEN));
-
 
182
    }
178
    }
183
    else
179
    else
184
    {
180
    {
185
        char str[INET6_ADDRSTRLEN];
181
        char str[INET6_ADDRSTRLEN];
186
        struct sockaddr_in6 addr;
182
        struct sockaddr_in6 addr;
187
        struct ifreq ninfo;
-
 
188
        socklen_t len = sizeof(addr);
183
        socklen_t len = sizeof(addr);
189
        getsockname(sock, (struct sockaddr *)&addr, &len);
184
        getsockname(sock, (struct sockaddr *)&addr, &len);
190
        mMyIP.assign(inet_ntop(AF_INET6, &(addr.sin6_addr), str, INET6_ADDRSTRLEN));
185
        mMyIP.assign(inet_ntop(AF_INET6, &(addr.sin6_addr), str, INET6_ADDRSTRLEN));
191
 
-
 
192
        if (ioctl(sock, SIOCGIFNETMASK, &ninfo))
186
        determineNetmask(sock);
193
        {
-
 
194
            MSG_ERROR("Error getting netmask: " << strerror(errno));
-
 
195
        }
-
 
196
        else
-
 
197
            mMyNetmask.assign(inet_ntop(AF_INET6, &(ninfo.ifr_ifru.ifru_netmask), str, INET6_ADDRSTRLEN));
-
 
198
    }
187
    }
199
 
188
 
200
    MSG_DEBUG("Client IP: " << mMyIP);
189
    MSG_DEBUG("Client IP: " << mMyIP);
201
 
190
 
202
    if (ainfo == nullptr)
191
    if (ainfo == nullptr)
Line 591... Line 580...
591
 
580
 
592
    memset (&hints, 0, sizeof (hints));
581
    memset (&hints, 0, sizeof (hints));
593
    hints.ai_family = AF_INET;
582
    hints.ai_family = AF_INET;
594
    hints.ai_protocol = IPPROTO_TCP;
583
    hints.ai_protocol = IPPROTO_TCP;
595
    hints.ai_socktype = SOCK_STREAM;
584
    hints.ai_socktype = SOCK_STREAM;
596
    hints.ai_flags = AI_CANONNAME;
585
    hints.ai_flags = AI_CANONNAME | AI_CANONIDN;
597
    snprintf(sport, sizeof(sport), "%d", port);
586
    snprintf(sport, sizeof(sport), "%d", port);
598
    int ret = 0;
587
    int ret = 0;
599
 
588
 
600
    if ((ret = getaddrinfo (host.c_str(), sport, &hints, &res)) != 0)
589
    if ((ret = getaddrinfo (host.c_str(), sport, &hints, &res)) != 0)
601
    {
590
    {
602
        MSG_ERROR("[" << mHost << "] Getaddrinfo: " << gai_strerror(ret));
591
        MSG_ERROR("[" << mHost << "] Getaddrinfo: " << gai_strerror(ret));
603
        return nullptr;
592
        return nullptr;
604
    }
593
    }
605
 
594
 
-
 
595
    if (res->ai_canonname)
-
 
596
    {
-
 
597
        MSG_DEBUG("Canonical name: " << res->ai_canonname);
-
 
598
    }
-
 
599
 
606
    return res;
600
    return res;
607
}
601
}
608
 
602
 
-
 
603
bool TSocket::determineNetmask(int socket)
-
 
604
{
-
 
605
    DECL_TRACER("TSocket::determineNetmask(int socket)");
-
 
606
 
-
 
607
    struct ifaddrs *ifaddr;
-
 
608
    char str[INET6_ADDRSTRLEN];
-
 
609
    int s, family;
-
 
610
    char host[NI_MAXHOST];
-
 
611
 
-
 
612
    if (getifaddrs(&ifaddr) == -1)
-
 
613
    {
-
 
614
        MSG_ERROR("Error getting devices: " << strerror(errno));
-
 
615
        return false;
-
 
616
    }
-
 
617
 
-
 
618
    for (struct ifaddrs *ifa = ifaddr; ifa != nullptr; ifa = ifa->ifa_next)
-
 
619
    {
-
 
620
        if (ifa->ifa_addr == nullptr)
-
 
621
            continue;
-
 
622
 
-
 
623
        family = ifa->ifa_addr->sa_family;
-
 
624
        size_t addrSize = family == AF_INET ? sizeof(struct sockaddr_in) : sizeof(sockaddr_in6);
-
 
625
 
-
 
626
        if (family != AF_INET && family != AF_INET6)
-
 
627
            continue;
-
 
628
 
-
 
629
        s = getnameinfo(ifa->ifa_addr, addrSize, host, NI_MAXHOST, nullptr, 0, NI_NUMERICHOST);
-
 
630
 
-
 
631
        if (s != 0)
-
 
632
        {
-
 
633
            MSG_ERROR("Nameinfo failed: " << gai_strerror(s));
-
 
634
            freeifaddrs(ifaddr);
-
 
635
            return false;
-
 
636
        }
-
 
637
 
-
 
638
        MSG_DEBUG("Comparing \"" << host << "\" with \"" << mMyIP << "\"");
-
 
639
 
-
 
640
        if (mMyIP.compare(host) == 0)
-
 
641
        {
-
 
642
            struct ifreq ninfo;
-
 
643
 
-
 
644
            MSG_DEBUG("Device: " << ifa->ifa_name);
-
 
645
            memset(&ninfo, 0, sizeof(ninfo));
-
 
646
            strncpy(ninfo.ifr_ifrn.ifrn_name, ifa->ifa_name, IFNAMSIZ);
-
 
647
            ninfo.ifr_ifrn.ifrn_name[IFNAMSIZ-1] = 0;
-
 
648
 
-
 
649
            if (ioctl(socket, SIOCGIFNETMASK, &ninfo))
-
 
650
            {
-
 
651
                MSG_ERROR("Error getting netmask: " << strerror(errno));
-
 
652
                freeifaddrs(ifaddr);
-
 
653
                return false;
-
 
654
            }
-
 
655
            else
-
 
656
            {
-
 
657
                if (family == AF_INET)
-
 
658
                {
-
 
659
                    stringstream str;
-
 
660
                    int mask = ((struct sockaddr_in *)&ninfo.ifr_addr)->sin_addr.s_addr;
-
 
661
 
-
 
662
                    str << (mask & 0xff) << "." << ((mask >> 8) & 0xff) << "." << ((mask >> 16) & 0xff) << "." << ((mask >> 24) & 0xff);
-
 
663
                    mMyNetmask = str.str();
-
 
664
                }
-
 
665
                else
-
 
666
                {
-
 
667
                    const char *had = nullptr;
-
 
668
 
-
 
669
                    had = inet_ntop(AF_INET6, ((struct sockaddr_in6 *)&ninfo.ifr_addr)->sin6_addr.s6_addr, str, INET6_ADDRSTRLEN);
-
 
670
                    mMyNetmask.assign(had);
-
 
671
                }
-
 
672
 
-
 
673
                MSG_DEBUG("Netmask: " << mMyNetmask);
-
 
674
                freeifaddrs(ifaddr);
-
 
675
                return true;
-
 
676
            }
-
 
677
        }
-
 
678
    }
-
 
679
 
-
 
680
    freeifaddrs(ifaddr);
-
 
681
    return false;
-
 
682
}
-
 
683
 
609
void TSocket::initSSL()
684
void TSocket::initSSL()
610
{
685
{
611
    DECL_TRACER("TSocket::initSSL()");
686
    DECL_TRACER("TSocket::initSSL()");
612
 
687
 
613
    if (mSSLInitialized)
688
    if (mSSLInitialized)