egalite is an open source project powered by Assembla

Assembla offers free public and private SVN/Git repositories and project hosting with bug/issue tracking and collaboration tools.

Branchmaster
/src
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/****************************************************************
 * This file is distributed under the following license:
 *
 * Copyright (C) 2010, Bernd Stramm
 *
 *  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 the Free Software Foundation; either version 2
 *  of the License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor,
 *  Boston, MA  02110-1301, USA.
 ****************************************************************/

#include "direct-listener.h"
#include "symmetric-socket.h"
#include "deliberate.h"
#include <QFile>
#include <QSslConfiguration>
#include <QHostInfo>
#include <QMessageBox>
#include <QTimer>
#include "delib-debug.h"

namespace egalite
{

DirectListener::DirectListener (QObject *parent)
  :QTcpServer(parent)
{}

DirectListener::~DirectListener ()
{
  Close ();
  SocketList::iterator sockit;
  for (sockit = sockets.begin(); sockit != sockets.end(); sockit++) {
    (*sockit)->Done();
    delete *sockit;
  }
}

bool
DirectListener::Listen (const QString &thisHost, int port)
{
  QHostAddress addr;
  if (deliberate::IsIp6Address (thisHost)
     || deliberate::IsIp4Address (thisHost)) {
    addr = QHostAddress (thisHost);
  } else {
    QHostInfo hinfo = QHostInfo::fromName (thisHost);
qDebug () << " host info for " << thisHost;
qDebug () << " first address " << hinfo.addresses();
    QList<QHostAddress> addrList = hinfo.addresses();
    if (!addrList.isEmpty()) {
      addr = hinfo.addresses().first ();
    }
  }
  bool good = listen (addr,port);
qDebug () << " listen at " << addr << " port " << port;
  if (!good) {
    QMessageBox  cantListen ;
    QString msg (tr("Cannot listen at ") + addr.toString()
                 + QString(" port %1").arg (port));
    cantListen.setText (msg);
    QString longMsg;
    longMsg += tr("The Listener specified as\n");
    longMsg += thisHost;
    longMsg += tr("\nwas resolved to address\n");
    longMsg += addr.toString();
    cantListen.setDetailedText (longMsg);
    QTimer::singleShot (30000, &cantListen, SLOT (accept()));
    cantListen.exec ();
  }
  hostName = thisHost;
  hostAddress = addr.toString();
  hostPort = port;
  return good;
}

QString
DirectListener::HostName ()
{
  return hostName;
}

QString
DirectListener::Address ()
{
  return hostAddress;
}

int
DirectListener::Port ()
{
  return hostPort;
}

void
DirectListener::Close ()
{
  close ();
}

void
DirectListener::incomingConnection (int socketDescriptor)
{
  qDebug () << " Direct Listener " << this << "new connection " << socketDescriptor;
  qDebug () << " Listener adding mCert " << mCert;
  SymmetricSocket * newsock = new SymmetricSocket (socketDescriptor,
      mKey,mCert) ;
  connect (newsock, SIGNAL (Exiting (SymmetricSocket*)),
           this, SLOT (SocketExit (SymmetricSocket*)));
  connect (newsock, SIGNAL (Ready (SymmetricSocket*)),
           this, SLOT (IsReady (SymmetricSocket*)));
  newsock->Init ();
  newsock->Socket()->setPeerVerifyMode (QSslSocket::VerifyPeer);
  newsock->Socket()->startServerEncryption ();
  sockets << newsock;
  qDebug () << " new server side sock has certs: " << newsock->caCertificates();
//  qDebug () << " new server side sock has conf: " ;
//  ShowConfig (newsock->Socket()->sslConfiguration());
}

void
DirectListener::Init (QString certHost, QString pass)
{
  QFile keyfile (QString ("/home/bernd/ssl-mCert/%1/mKey.pem").arg(certHost));
  keyfile.open (QFile::ReadOnly);
  QSslKey skey (&keyfile,QSsl::Rsa,
                QSsl::Pem, QSsl::PrivateKey, pass.toUtf8());
  keyfile.close();
  mKey = skey;

  QFile certfile (QString ("/home/bernd/ssl-mCert/%1/mCert.pem").arg(certHost));
  certfile.open (QFile::ReadOnly);
  QSslCertificate scert (&certfile);
  certfile.close();
  mCert = scert;
}

void
DirectListener::Init (QString iname, QSslKey ikey, QSslCertificate icert)
{
  mName = iname;
  mKey = ikey;
  mCert = icert;
}

void
DirectListener::IsReady (SymmetricSocket * sock)
{
  qDebug () << " Listener socket ready " << sock;
  bool isMine = TakeSocket (sock);
  if (isMine) {
    emit SocketReady (sock, mName);
  }
}

bool
DirectListener::TakeSocket (SymmetricSocket * sock)
{
  if (sockets.contains (sock)) {
    sockets.removeAll (sock);
    return true;
  }
  return false;
}


void
DirectListener::SocketExit (SymmetricSocket *sock)
{
  emit SocketClosed (sock);
}


} // namespace
Ajax-loader Loading, please wait...