33 #include "private/ArnServer_p.hpp" 43 #include <QNetworkInterface> 44 #include <QHostAddress> 54 QHostAddress remoteAddr =
socket->peerAddress();
60 _arnServer = arnServer;
61 _socket->setParent(
this);
62 _arnNetSync =
new ArnSync( _socket,
false,
this);
63 _arnNetSync->setSessionHandler(
this);
64 _arnNetSync->setArnLogin( _arnServer->arnLogin());
71 foreach (
const QString& path, _arnServer->
freePaths()) {
72 _arnNetSync->addFreePath( path);
74 _arnNetSync->setWhoIAm( _arnServer->whoIAm());
76 _arnNetEar =
new ArnItemNetEar(
this);
77 _arnNetEar->open(
"/");
79 connect( _arnNetSync, SIGNAL(stateChanged(
int)),
this, SLOT(doSyncStateChanged(
int)));
80 connect( _arnNetSync, SIGNAL(destroyed(QObject*)),
this, SLOT(shutdown()));
81 connect( _arnNetSync, SIGNAL(xcomDelete(QString)),
this, SLOT(onCommandDelete(QString)));
86 connect( _arnNetEar, SIGNAL(arnTreeDestroyed(QString,
bool)),
87 this, SLOT(doDestroyArnTree(QString,
bool)));
91 void ArnServerSession::shutdown()
93 _arnNetSync = arnNullptr;
99 void ArnServerSession::doDestroyArnTree(
const QString& path,
bool isGlobal)
103 if (!_arnNetSync)
return;
105 _arnNetSync->sendDelete( path);
109 void ArnServerSession::onCommandDelete(
const QString& path)
111 if (!_arnNetSync)
return;
118 void ArnServerSession::doSyncStateChanged(
int state)
120 if (!_arnNetSync)
return;
123 ArnSync::State syncState = ArnSync::State::fromInt( state);
124 if (syncState == syncState.Normal) {
141 return XStringMap( _arnNetSync->remoteWhoIAm());
147 if (!_arnNetSync)
return QString();
149 return _arnNetSync->loginUserName();
157 return _arnNetSync->getAllow();
163 if (!_arnNetSync)
return;
165 _arnNetSync->sendMessage( type, data);
171 if (!_arnNetSync)
return false;
173 _arnNetSync->getTraffic( in, out);
181 _tcpServerActive =
false;
182 _isDemandLogin =
false;
183 _tcpServer =
new QTcpServer;
184 _arnLogin =
new ArnSyncLogin;
185 _newSession = arnNullptr;
186 _serverType = serverType;
191 ArnServerPrivate::~ArnServerPrivate()
200 , d_ptr( new ArnServerPrivate( serverType))
223 switch (d->_serverType) {
228 ArnM::errorLog( QString(tr(
"Unknown Arn server Type: ")) + QString::number( d->_serverType),
234 if (d->_tcpServer->listen( listenAddr,
port)) {
235 d->_tcpServerActive =
true;
237 connect( d->_tcpServer, SIGNAL(newConnection()),
this, SLOT(tcpConnection()));
240 ArnM::errorLog( QString(tr(
"Failed start Arn Server Port: ")) + QString::number(
port),
250 return d->_tcpServer->serverPort();
258 QHostAddress addr = d->_tcpServer->serverAddress();
267 d->_arnLogin->addAccess( userName, password, allow);
275 return d->_isDemandLogin;
299 return d->_noLoginNets;
307 QHostAddress remoteAddrV6 = QHostAddress( remoteAddr.toIPv6Address());
309 foreach (
const QString& noLoginNet, d->_noLoginNets) {
310 bool chkLocalHost = noLoginNet ==
"localhost";
311 if (chkLocalHost || (noLoginNet ==
"localnet")) {
312 if (remoteAddrV6 == QHostAddress( QHostAddress::LocalHostIPv6))
315 foreach (QNetworkInterface interface, QNetworkInterface::allInterfaces()) {
316 QNetworkInterface::InterfaceFlags flags = interface.flags();
317 if (flags.testFlag( QNetworkInterface::IsPointToPoint)
318 || flags.testFlag( QNetworkInterface::IsLoopBack))
321 foreach (QNetworkAddressEntry entry, interface.addressEntries()) {
322 QHostAddress entryIp = entry.ip();
323 QAbstractSocket::NetworkLayerProtocol prot = entryIp.protocol();
324 if ((prot != QAbstractSocket::IPv4Protocol) && (prot != QAbstractSocket::IPv6Protocol))
327 QHostAddress entryIpV6 = QHostAddress( entryIp.toIPv6Address());
328 int prefixOffs = (prot == QAbstractSocket::IPv4Protocol) ? 96 : 0;
329 int prefixV6 = entry.prefixLength() + prefixOffs;
331 if (entry.prefixLength() < 0) {
333 prefixV6 = 24 + prefixOffs;
334 qWarning() <<
"Bad netmask: nif=" << interface.humanReadableName()
335 <<
", asume prefixV6Len(=" << prefixV6;
338 if (entryIpV6 == remoteAddrV6)
341 if (!chkLocalHost && remoteAddrV6.isInSubnet( entryIpV6, prefixV6))
346 else if (noLoginNet ==
"any") {
350 QPair<QHostAddress, int> subnet = QHostAddress::parseSubnet( noLoginNet);
351 QHostAddress subnetIpV6 = QHostAddress( subnet.first.toIPv6Address());
352 int prefixOffs = (subnet.first.protocol() == QAbstractSocket::IPv4Protocol) ? 96 : 0;
353 int prefixV6 = subnet.second + prefixOffs;
354 if (remoteAddrV6.isInSubnet( subnetIpV6, prefixV6))
367 if (!d->_freePathTab.contains( path))
368 d->_freePathTab += path;
376 return d->_freePathTab;
380 ArnSyncLogin* ArnServer::arnLogin()
const 392 return d->_newSession;
396 QByteArray ArnServer::whoIAm()
const 412 void ArnServer::tcpConnection()
416 QTcpSocket* socket = d->_tcpServer->nextPendingConnection();
417 if (socket->peerPort() == 0) {
418 socket->deleteLater();
422 switch (d->_serverType) {
426 d->_newSession = arnNullptr;
QStringList noLoginNets() const
Get the nets not demanding login.
Arn::XStringMap remoteWhoIAm() const
QTcpSocket * socket() const
void start(int port=-1, QHostAddress listenAddr=QHostAddress::Any)
Start the Arn server
void setWhoIAm(const Arn::XStringMap &whoIAmXsm)
Set servers human readable identification information.
Class for making an Arn Server.
Container class with string representation for serialized data.
QString fullPath(const QString &path)
Convert a path to a full absolute path.
bool getTraffic(quint64 &in, quint64 &out) const
const QString pathLocalSys
void setNoLoginNets(const QStringList &noLoginNets)
Set the nets not demanding login.
const quint16 defaultTcpPort
ArnServer(Type serverType, QObject *parent=arnNullptr)
Create an Arn server object.
bool isDemandLoginNet(const QHostAddress &remoteAddr) const
Return if a host address demands login.
QStringList freePaths() const
Returns current list of freePaths.
ArnServerSession(QTcpSocket *socket, ArnServer *arnServer)
void addFreePath(const QString &path)
Add a new "freePath".
void messageReceived(int type, const QByteArray &data)
QByteArray toXString() const
QHostAddress listenAddress()
Address of the interface used to listening for connections to the Arn server
void addAccess(const QString &userName, const QString &password, Arn::Allow allow)
Add an access entry.
void infoReceived(int type)
bool isDemandLogin() const
Get servers demand for login.
int port()
Port number of the Arn server
QString loginUserName() const
static void errorLog(QString errText, ArnError err=ArnError::Undef, void *reference=arnNullptr)
static void destroyLink(const QString &path, bool isGlobal=true)
Destroy the Arn Data Object at path
void sendMessage(int type, const QByteArray &data=QByteArray())
void setDemandLogin(bool isDemandLogin)
Set servers demand for login.
Arn::Allow getAllow() const