Added feed item to notify a blacklisted ip address.

git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@8346 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
thunder2 2015-06-02 21:36:26 +00:00
parent bfe59bedf0
commit 9bef4502b1
17 changed files with 1324 additions and 106 deletions

View File

@ -188,10 +188,10 @@ bool p3Notify::GetFeedItem(RsFeedItem &item)
}
bool p3Notify::AddFeedItem(uint32_t type, const std::string& id1, const std::string& id2, const std::string& id3, const std::string& id4)
bool p3Notify::AddFeedItem(uint32_t type, const std::string& id1, const std::string& id2, const std::string& id3, const std::string& id4, uint32_t result1)
{
RsStackMutex stack(noteMtx); /************* LOCK MUTEX ************/
pendingNewsFeed.push_back(RsFeedItem(type, id1, id2, id3, id4));
pendingNewsFeed.push_back(RsFeedItem(type, id1, id2, id3, id4, result1));
return true;
}

View File

@ -90,7 +90,7 @@ class p3Notify: public RsNotify
bool AddPopupMessage(uint32_t ptype, const std::string& name, const std::string& title, const std::string& msg);
bool AddSysMessage(uint32_t sysid, uint32_t type, const std::string& title, const std::string& msg);
bool AddLogMessage(uint32_t sysid, uint32_t type, const std::string& title, const std::string& msg);
bool AddFeedItem(uint32_t type, const std::string& id1, const std::string& id2 = "", const std::string& id3 = "", const std::string& id4 = "");
bool AddFeedItem(uint32_t type, const std::string& id1, const std::string& id2 = "", const std::string& id3 = "", const std::string& id4 = "", uint32_t result1 = 0);
bool ClearFeedItems(uint32_t type);
// Notifications of clients. Can be called from anywhere inside libretroshare.

View File

@ -42,6 +42,8 @@
#include <retroshare/rsdht.h>
#include <retroshare/rsbanlist.h>
#include "rsserver/p3face.h"
const int pqisslzone = 37714;
/*********
@ -1316,6 +1318,7 @@ int pqissl::Authorise_SSL_Connection()
if(!rsBanList->isAddressAccepted(remote_addr,RSBANLIST_CHECKING_FLAGS_BLACKLIST,&check_result))
{
std::cerr << "(SS) connection attempt from banned IP address. Refusing it. Reason: " << check_result << ". Attack??" << std::endl;
RsServer::notify()->AddFeedItem(RS_FEED_ITEM_SEC_IP_BLACKLISTED, PeerId().toStdString(), sockaddr_storage_iptostring(remote_addr), "", "", check_result);
reset_locked();
return 0 ;
}
@ -1359,6 +1362,7 @@ int pqissl::accept_locked(SSL *ssl, int fd, const struct sockaddr_storage &forei
{
std::cerr << "(SS) refusing incoming SSL connection from blacklisted foreign address " << sockaddr_storage_iptostring(foreign_addr)
<< ". Reason: " << check_result << "." << std::endl;
RsServer::notify()->AddFeedItem(RS_FEED_ITEM_SEC_IP_BLACKLISTED, PeerId().toStdString(), sockaddr_storage_iptostring(foreign_addr), "", "", check_result);
reset_locked();
return -1;
}

View File

@ -90,6 +90,7 @@ const uint32_t RS_FEED_ITEM_SEC_WRONG_SIGNATURE = RS_FEED_TYPE_SECURITY | 0
const uint32_t RS_FEED_ITEM_SEC_BAD_CERTIFICATE = RS_FEED_TYPE_SECURITY | 0x0006;
const uint32_t RS_FEED_ITEM_SEC_INTERNAL_ERROR = RS_FEED_TYPE_SECURITY | 0x0007;
const uint32_t RS_FEED_ITEM_SEC_MISSING_CERTIFICATE = RS_FEED_TYPE_SECURITY | 0x0008;
const uint32_t RS_FEED_ITEM_SEC_IP_BLACKLISTED = RS_FEED_TYPE_SECURITY | 0x0010;
const uint32_t RS_FEED_ITEM_CHANNEL_NEW = RS_FEED_TYPE_CHANNEL | 0x0001;
//const uint32_t RS_FEED_ITEM_CHANNEL_UPDATE = RS_FEED_TYPE_CHANNEL | 0x0002;
@ -147,13 +148,14 @@ const uint32_t NOTIFY_HASHTYPE_SAVE_FILE_INDEX = 4; /* Hashing file */
class RsFeedItem
{
public:
RsFeedItem(uint32_t type, const std::string& id1, const std::string& id2 = "", const std::string& id3 = "", const std::string& id4 = "")
:mType(type), mId1(id1), mId2(id2), mId3(id3), mId4(id4) {}
RsFeedItem(uint32_t type, const std::string& id1, const std::string& id2, const std::string& id3, const std::string& id4, uint32_t result1)
:mType(type), mId1(id1), mId2(id2), mId3(id3), mId4(id4), mResult1(result1) {}
RsFeedItem() :mType(0) { return; }
RsFeedItem() :mType(0), mResult1(0) { return; }
uint32_t mType;
std::string mId1, mId2, mId3, mId4;
uint32_t mResult1;
};
// This class implements a generic notify client. To have your own components being notified by

View File

@ -32,6 +32,7 @@
#include <retroshare/rsposted.h>
#include <retroshare/rsmsgs.h>
#include <retroshare/rsplugin.h>
#include <retroshare/rsbanlist.h>
#include "feeds/GxsChannelGroupItem.h"
#include "feeds/GxsChannelPostItem.h"
@ -52,6 +53,7 @@
#include "feeds/PeerItem.h"
#include "feeds/ChatMsgItem.h"
#include "feeds/SecurityItem.h"
#include "feeds/SecurityIpItem.h"
#include "feeds/NewsFeedUserNotify.h"
#include "settings/rsharesettings.h"
@ -246,6 +248,11 @@ void NewsFeed::updateDisplay()
addFeedItemSecurityUnknownOut(fi);
break;
case RS_FEED_ITEM_SEC_IP_BLACKLISTED:
if (flags & RS_FEED_TYPE_SECURITY)
addFeedItemSecurityIpBlacklisted(fi, false);
break;
case RS_FEED_ITEM_CHANNEL_NEW:
if (flags & RS_FEED_TYPE_CHANNEL)
addFeedItemChannelNew(fi);
@ -410,6 +417,11 @@ void NewsFeed::testFeeds(uint notifyFlags)
instance->addFeedItemSecurityAuthDenied(fi);
instance->addFeedItemSecurityUnknownIn(fi);
instance->addFeedItemSecurityUnknownOut(fi);
fi.mId1 = rsPeers->getOwnId().toStdString();
fi.mId2 = "0.0.0.0";
fi.mResult1 = RSBANLIST_CHECK_RESULT_BLACKLISTED;
instance->addFeedItemSecurityIpBlacklisted(fi, true);
break;
case RS_FEED_TYPE_CHANNEL:
@ -872,10 +884,16 @@ void NewsFeed::addFeedItem(FeedItem *item)
struct AddFeedItemIfUniqueData
{
AddFeedItemIfUniqueData(int type, const RsPeerId &sslId) : mType(type), mSslId(sslId) {}
AddFeedItemIfUniqueData(FeedItem *feedItem, int type, const RsPeerId &sslId) : mType(type), mSslId(sslId)
{
mSecItem = dynamic_cast<SecurityItem*>(feedItem);
mSecurityIpItem = dynamic_cast<SecurityIpItem*>(feedItem);
}
int mType;
const RsPeerId &mSslId;
SecurityItem *mSecItem;
SecurityIpItem *mSecurityIpItem;
};
static bool addFeedItemIfUniqueCallback(FeedItem *feedItem, void *data)
@ -885,13 +903,20 @@ static bool addFeedItemIfUniqueCallback(FeedItem *feedItem, void *data)
return false;
}
SecurityItem *secitem = dynamic_cast<SecurityItem*>(feedItem);
if (!secitem) {
if (findData->mSecItem) {
SecurityItem *secitem = dynamic_cast<SecurityItem*>(feedItem);
if (secitem && secitem->isSame(findData->mSslId, findData->mType)) {
return true;
}
return false;
}
if (secitem->isSame(findData->mSslId, findData->mType)) {
return true;
if (findData->mSecurityIpItem) {
SecurityIpItem *securityIpItem = dynamic_cast<SecurityIpItem*>(feedItem);
if (securityIpItem && securityIpItem->isSame(findData->mSslId)) {
return true;
}
return false;
}
return false;
@ -899,7 +924,7 @@ static bool addFeedItemIfUniqueCallback(FeedItem *feedItem, void *data)
void NewsFeed::addFeedItemIfUnique(FeedItem *item, int itemType, const RsPeerId &sslId, bool replace)
{
AddFeedItemIfUniqueData data(itemType, sslId);
AddFeedItemIfUniqueData data(item, itemType, sslId);
FeedItem *feedItem = ui->feedWidget->findFeedItem(addFeedItemIfUniqueCallback, &data);
if (feedItem) {
@ -1026,6 +1051,20 @@ void NewsFeed::addFeedItemSecurityUnknownOut(const RsFeedItem &fi)
#endif
}
void NewsFeed::addFeedItemSecurityIpBlacklisted(const RsFeedItem &fi, bool isTest)
{
/* make new widget */
SecurityIpItem *pi = new SecurityIpItem(this, RsPeerId(fi.mId1), fi.mId2, fi.mResult1, isTest);
/* add to layout */
addFeedItemIfUnique(pi, 0, RsPeerId(fi.mId1), false);
#ifdef NEWS_DEBUG
std::cerr << "NewsFeed::addFeedItemSecurityIpBlacklisted()";
std::cerr << std::endl;
#endif
}
void NewsFeed::addFeedItemChannelNew(const RsFeedItem &fi)
{
RsGxsGroupId grpId(fi.mId1);

View File

@ -94,6 +94,7 @@ private:
void addFeedItemSecurityAuthDenied(const RsFeedItem &fi);
void addFeedItemSecurityUnknownIn(const RsFeedItem &fi);
void addFeedItemSecurityUnknownOut(const RsFeedItem &fi);
void addFeedItemSecurityIpBlacklisted(const RsFeedItem &fi, bool isTest);
void addFeedItemChannelNew(const RsFeedItem &fi);
// void addFeedItemChannelUpdate(const RsFeedItem &fi);

View File

@ -0,0 +1,61 @@
/****************************************************************
* This file is distributed under the following license:
*
* Copyright (c) 2015, RetroShare Team
*
* 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 <QCoreApplication>
#include <retroshare/rsbanlist.h>
#include "RsBanListDefs.h"
//const QString RsBanListDefs::resultState(uint32_t result)
//{
// switch (result) {
// case RSBANLIST_CHECK_RESULT_UNKNOWN:
// break;
// case RSBANLIST_CHECK_RESULT_NOCHECK:
// return QCoreApplication::translate("RsBanListDefs", "not checked");
// case RSBANLIST_CHECK_RESULT_BLACKLISTED:
// return QCoreApplication::translate("RsBanListDefs", "blacklisted");
// case RSBANLIST_CHECK_RESULT_NOT_WHITELISTED:
// return QCoreApplication::translate("RsBanListDefs", "not whitelisted");
// case RSBANLIST_CHECK_RESULT_ACCEPTED:
// return QCoreApplication::translate("RsBanListDefs", "accepted");
// }
// return QCoreApplication::translate("RsBanListDefs", "Unknown");
//}
const QString RsBanListDefs::resultString(uint32_t result)
{
switch (result) {
case RSBANLIST_CHECK_RESULT_UNKNOWN:
break;
case RSBANLIST_CHECK_RESULT_NOCHECK:
return QCoreApplication::translate("RsBanListDefs", "IP address not checked");
case RSBANLIST_CHECK_RESULT_BLACKLISTED:
return QCoreApplication::translate("RsBanListDefs", "IP address is blacklisted");
case RSBANLIST_CHECK_RESULT_NOT_WHITELISTED:
return QCoreApplication::translate("RsBanListDefs", "IP address is not whitelisted");
case RSBANLIST_CHECK_RESULT_ACCEPTED:
return QCoreApplication::translate("RsBanListDefs", "IP address accepted");
}
return QCoreApplication::translate("RsBanListDefs", "Unknown");
}

View File

@ -0,0 +1,35 @@
/****************************************************************
* This file is distributed under the following license:
*
* Copyright (c) 2015, RetroShare Team
*
* 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.
****************************************************************/
#ifndef _RSBANLISTDEFS_H
#define _RSBANLISTDEFS_H
#include <QString>
class RsBanListDefs
{
public:
// static const QString resultState(uint32_t result);
static const QString resultString(uint32_t result);
};
#endif

View File

@ -0,0 +1,165 @@
/****************************************************************
* RetroShare is distributed under the following license:
*
* Copyright (C) 2015, RetroShare Team
*
* 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 <QMenu>
#include "RsBanListToolButton.h"
#include "util/RsNetUtil.h"
#include <retroshare/rsbanlist.h>
/* Use MenuButtonPopup, because the arrow of InstantPopup is too small */
#define USE_MENUBUTTONPOPUP
RsBanListToolButton::RsBanListToolButton(QWidget *parent) :
QToolButton(parent)
{
mList = LIST_WHITELIST;
mMode = MODE_ADD;
setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
#ifdef USE_MENUBUTTONPOPUP
connect(this, SIGNAL(clicked()), this, SLOT(showMenu()));
#endif
updateUi();
}
void RsBanListToolButton::setMode(List list, Mode mode)
{
mList = list;
mMode = mode;
updateUi();
}
bool RsBanListToolButton::setIpAddress(const QString &ipAddress)
{
mIpAddress.clear();
if (ipAddress.isEmpty()) {
updateUi();
return false;
}
struct sockaddr_storage addr;
int bytes ;
if (!RsNetUtil::parseAddrFromQString(ipAddress, addr, bytes) || bytes != 0) {
updateUi();
return false;
}
mIpAddress = ipAddress;
updateUi();
return true;
}
void RsBanListToolButton::updateUi()
{
#ifdef USE_MENUBUTTONPOPUP
setPopupMode(QToolButton::MenuButtonPopup);
#else
setPopupMode(QToolButton::InstantPopup);
#endif
switch (mList) {
case LIST_WHITELIST:
switch (mMode) {
case MODE_ADD:
setText(tr("Add IP to whitelist"));
break;
case MODE_REMOVE:
setText(tr("Remove IP from whitelist"));
break;
}
break;
case LIST_BLACKLIST:
switch (mMode) {
case MODE_ADD:
setText(tr("Add IP to blacklist"));
break;
case MODE_REMOVE:
setText(tr("Remove IP from blacklist"));
break;
}
break;
}
if (!mIpAddress.isEmpty()) {
sockaddr_storage addr ;
int masked_bytes ;
if (RsNetUtil::parseAddrFromQString(mIpAddress, addr, masked_bytes)) {
QMenu *m = new QMenu;
m->addAction(QString("%1 %2").arg(tr("Only IP"), RsNetUtil::printAddrRange(addr, 0)), this, SLOT(applyIp()))->setData(0);
m->addAction(QString("%1 %2").arg(tr("Entire range"), RsNetUtil::printAddrRange(addr, 1)), this, SLOT(applyIp()))->setData(1);
m->addAction(QString("%1 %2").arg(tr("Entire range"), RsNetUtil::printAddrRange(addr, 2)), this, SLOT(applyIp()))->setData(2);
setMenu(m);
} else {
setMenu(NULL);
}
setToolTip(mIpAddress);
} else {
setMenu(NULL);
setToolTip("");
}
}
void RsBanListToolButton::applyIp()
{
QAction *action = dynamic_cast<QAction*>(sender());
if (!action) {
return;
}
sockaddr_storage addr ;
int masked_bytes;
if (!RsNetUtil::parseAddrFromQString(mIpAddress, addr, masked_bytes)) {
return;
}
uint32_t list_type;
switch (mList) {
case LIST_WHITELIST:
list_type = RSBANLIST_TYPE_WHITELIST;
break;
case LIST_BLACKLIST:
list_type = RSBANLIST_TYPE_BLACKLIST;
break;
}
masked_bytes = action->data().toUInt();
switch (mMode) {
case MODE_ADD:
rsBanList->addIpRange(addr, masked_bytes, list_type, "");
break;
case MODE_REMOVE:
rsBanList->removeIpRange(addr, masked_bytes, list_type);
break;
}
}

View File

@ -0,0 +1,61 @@
/****************************************************************
* RetroShare is distributed under the following license:
*
* Copyright (C) 2015, RetroShare Team
*
* 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.
****************************************************************/
#ifndef _RSBANLISTTOOLBUTTON_H
#define _RSBANLISTTOOLBUTTON_H
#include <QToolButton>
class RsBanListToolButton : public QToolButton
{
Q_OBJECT
public:
enum List
{
LIST_WHITELIST, // default
LIST_BLACKLIST
};
enum Mode
{
MODE_ADD, // default
MODE_REMOVE
};
public:
explicit RsBanListToolButton(QWidget *parent = 0);
void setMode(List list, Mode mode);
bool setIpAddress(const QString &ipAddress);
private:
void updateUi();
private slots:
void applyIp();
private:
Mode mMode;
List mList;
QString mIpAddress;
};
#endif

View File

@ -0,0 +1,214 @@
/****************************************************************
* RetroShare is distributed under the following license:
*
* Copyright (C) 2015, RetroShare Team
*
* 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 <QDateTime>
#include <QTimer>
#include "SecurityIpItem.h"
#include "FeedHolder.h"
#include"ui_SecurityIpItem.h"
#include "retroshare-gui/RsAutoUpdatePage.h"
#include "gui/connect/ConfCertDialog.h"
#include "util/DateTime.h"
#include "gui/common/PeerDefs.h"
#include "gui/common/RsBanListDefs.h"
#include <retroshare/rspeers.h>
#include <retroshare/rsbanlist.h>
/*****
* #define DEBUG_ITEM 1
****/
/** Constructor */
SecurityIpItem::SecurityIpItem(FeedHolder *parent, const RsPeerId &sslId, const std::string &ipAddr, uint32_t result, bool isTest) :
FeedItem(NULL), mParent(parent), mSslId(sslId), mIPAddr(ipAddr), mResult(result), mIsTest(isTest),
ui(new(Ui::SecurityIpItem))
{
/* Invoke the Qt Designer generated object setup routine */
ui->setupUi(this);
ui->peerDetailsButton->setEnabled(false);
/* general ones */
connect(ui->expandButton, SIGNAL(clicked(void)), this, SLOT(toggle(void)));
connect(ui->clearButton, SIGNAL(clicked(void)), this, SLOT(removeItem(void)));
/* specific ones */
connect(ui->peerDetailsButton, SIGNAL(clicked()), this, SLOT(peerDetails()));
ui->avatar->setId(ChatId(mSslId));
ui->rsBanListButton->setMode(RsBanListToolButton::LIST_WHITELIST, RsBanListToolButton::MODE_ADD);
ui->expandFrame->hide();
updateItemStatic();
updateItem();
}
bool SecurityIpItem::isSame(const RsPeerId &sslId)
{
if ((mSslId == sslId)) {
return true;
}
return false;
}
void SecurityIpItem::updateItemStatic()
{
if (!rsPeers)
return;
/* fill in */
#ifdef DEBUG_ITEM
std::cerr << "SecurityIpItem::updateItemStatic()";
std::cerr << std::endl;
#endif
QDateTime currentTime = QDateTime::currentDateTime();
ui->timeLabel->setText(DateTime::formatLongDateTime(currentTime.toTime_t()));
}
void SecurityIpItem::updateItem()
{
if (!rsPeers)
return;
/* fill in */
#ifdef DEBUG_ITEM
std::cerr << "SecurityIpItem::updateItem()";
std::cerr << std::endl;
#endif
if(!RsAutoUpdatePage::eventsLocked()) {
ui->titleLabel->setText(RsBanListDefs::resultString(mResult));
ui->ipAddr->setText(QString::fromStdString(mIPAddr));
if (mIsTest) {
ui->rsBanListButton->setEnabled(false);
} else {
switch (mResult) {
case RSBANLIST_CHECK_RESULT_NOCHECK:
case RSBANLIST_CHECK_RESULT_NOT_WHITELISTED:
case RSBANLIST_CHECK_RESULT_ACCEPTED:
ui->rsBanListButton->hide();
break;
case RSBANLIST_CHECK_RESULT_BLACKLISTED:
ui->rsBanListButton->setVisible(ui->rsBanListButton->setIpAddress(QString::fromStdString(mIPAddr)));
break;
default:
ui->rsBanListButton->hide();
}
}
RsPeerDetails details;
if (!rsPeers->getPeerDetails(mSslId, details))
{
/* set peer name */
ui->peer->setText(tr("Unknown Peer"));
/* expanded Info */
ui->peerID->setText(QString::fromStdString(mSslId.toStdString()));
ui->peerName->setText(tr("Unknown Peer"));
ui->locationLabel->setText(tr("Unknown Peer"));
} else {
/* set peer name */
ui->peer->setText(PeerDefs::nameWithLocation(details));
/* expanded Info */
ui->peerID->setText(QString::fromStdString(details.id.toStdString()));
ui->peerName->setText(QString::fromUtf8(details.name.c_str()));
ui->location->setText(QString::fromUtf8(details.location.c_str()));
/* Buttons */
ui->peerDetailsButton->setEnabled(true);
}
}
/* slow Tick */
int msec_rate = 10129;
QTimer::singleShot( msec_rate, this, SLOT(updateItem(void)));
}
void SecurityIpItem::toggle()
{
expand(ui->expandFrame->isHidden());
}
void SecurityIpItem::expand(bool open)
{
if (mParent) {
mParent->lockLayout(this, true);
}
if (open)
{
ui->expandFrame->show();
ui->expandButton->setIcon(QIcon(":/images/edit_remove24.png"));
ui->expandButton->setToolTip(tr("Hide"));
}
else
{
ui->expandFrame->hide();
ui->expandButton->setIcon(QIcon(":/images/edit_add24.png"));
ui->expandButton->setToolTip(tr("Expand"));
}
emit sizeChanged(this);
if (mParent) {
mParent->lockLayout(this, false);
}
}
void SecurityIpItem::removeItem()
{
#ifdef DEBUG_ITEM
std::cerr << "SecurityIpItem::removeItem()";
std::cerr << std::endl;
#endif
mParent->lockLayout(this, true);
hide();
mParent->lockLayout(this, false);
if (mParent)
{
mParent->deleteFeedItem(this, mFeedId);
}
}
///*********** SPECIFIC FUNCTIONS ***********************/
void SecurityIpItem::peerDetails()
{
#ifdef DEBUG_ITEM
std::cerr << "SecurityIpItem::peerDetails()";
std::cerr << std::endl;
#endif
RsPeerDetails details;
if (rsPeers->getPeerDetails(mSslId, details)) {
ConfCertDialog::showIt(mSslId, ConfCertDialog::PageDetails);
}
}

View File

@ -0,0 +1,71 @@
/****************************************************************
* RetroShare is distributed under the following license:
*
* Copyright (C) 2015, RetroShare Team
*
* 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.
****************************************************************/
#ifndef _SECURITYIPITEM_H
#define _SECURITYIPITEM_H
#include "retroshare/rstypes.h"
#include "FeedItem.h"
#include <stdint.h>
namespace Ui {
class SecurityIpItem;
}
class FeedHolder;
class SecurityIpItem : public FeedItem
{
Q_OBJECT
public:
/** Default Constructor */
SecurityIpItem(FeedHolder *parent, const RsPeerId &sslId, const std::string& ipAddr, uint32_t result, bool isTest);
void updateItemStatic();
bool isSame(const RsPeerId &sslId);
/* FeedItem */
virtual void expand(bool open);
private slots:
/* default stuff */
void removeItem();
void toggle();
void peerDetails();
void updateItem();
private:
FeedHolder *mParent;
uint32_t mFeedId;
RsPeerId mSslId;
std::string mIPAddr;
uint32_t mResult;
bool mIsTest;
/** Qt Designer generated object */
Ui::SecurityIpItem *ui;
};
#endif

View File

@ -0,0 +1,492 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>SecurityIpItem</class>
<widget class="QWidget" name="SecurityIpItem">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>763</width>
<height>161</height>
</rect>
</property>
<layout class="QGridLayout">
<property name="leftMargin">
<number>1</number>
</property>
<property name="topMargin">
<number>1</number>
</property>
<property name="rightMargin">
<number>1</number>
</property>
<property name="bottomMargin">
<number>1</number>
</property>
<item row="0" column="0">
<widget class="QFrame" name="frame">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="palette">
<palette>
<active>
<colorrole role="Base">
<brush brushstyle="SolidPattern">
<color alpha="255">
<red>207</red>
<green>150</green>
<blue>103</blue>
</color>
</brush>
</colorrole>
<colorrole role="Window">
<brush brushstyle="SolidPattern">
<color alpha="255">
<red>207</red>
<green>150</green>
<blue>103</blue>
</color>
</brush>
</colorrole>
</active>
<inactive>
<colorrole role="Base">
<brush brushstyle="SolidPattern">
<color alpha="255">
<red>207</red>
<green>150</green>
<blue>103</blue>
</color>
</brush>
</colorrole>
<colorrole role="Window">
<brush brushstyle="SolidPattern">
<color alpha="255">
<red>207</red>
<green>150</green>
<blue>103</blue>
</color>
</brush>
</colorrole>
</inactive>
<disabled>
<colorrole role="Base">
<brush brushstyle="SolidPattern">
<color alpha="255">
<red>240</red>
<green>240</green>
<blue>240</blue>
</color>
</brush>
</colorrole>
<colorrole role="Window">
<brush brushstyle="SolidPattern">
<color alpha="255">
<red>207</red>
<green>150</green>
<blue>103</blue>
</color>
</brush>
</colorrole>
</disabled>
</palette>
</property>
<property name="autoFillBackground">
<bool>true</bool>
</property>
<property name="frameShape">
<enum>QFrame::Box</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Sunken</enum>
</property>
<layout class="QGridLayout">
<property name="verticalSpacing">
<number>0</number>
</property>
<item row="0" column="0">
<widget class="QFrame" name="headFrame">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="maximumSize">
<size>
<width>16777215</width>
<height>70</height>
</size>
</property>
<layout class="QGridLayout">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<property name="verticalSpacing">
<number>0</number>
</property>
<item row="0" column="0" rowspan="2">
<widget class="AvatarWidget" name="avatar">
<property name="minimumSize">
<size>
<width>70</width>
<height>70</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>70</width>
<height>70</height>
</size>
</property>
<property name="alignment">
<set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop</set>
</property>
</widget>
</item>
<item row="1" column="1" colspan="2">
<layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
<widget class="QToolButton" name="peerDetailsButton">
<property name="focusPolicy">
<enum>Qt::NoFocus</enum>
</property>
<property name="text">
<string>Peer details</string>
</property>
<property name="icon">
<iconset resource="../images.qrc">
<normaloff>:/images/informations_24x24.png</normaloff>:/images/informations_24x24.png</iconset>
</property>
<property name="iconSize">
<size>
<width>16</width>
<height>16</height>
</size>
</property>
<property name="toolButtonStyle">
<enum>Qt::ToolButtonTextBesideIcon</enum>
</property>
<property name="autoRaise">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="RsBanListToolButton" name="rsBanListButton">
<property name="focusPolicy">
<enum>Qt::NoFocus</enum>
</property>
<property name="text">
<string notr="true">Add to whitelist</string>
</property>
<property name="iconSize">
<size>
<width>16</width>
<height>16</height>
</size>
</property>
<property name="autoRaise">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="expandButton">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="focusPolicy">
<enum>Qt::NoFocus</enum>
</property>
<property name="toolTip">
<string>Expand</string>
</property>
<property name="icon">
<iconset resource="../images.qrc">
<normaloff>:/images/edit_add24.png</normaloff>:/images/edit_add24.png</iconset>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="clearButton">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="focusPolicy">
<enum>Qt::NoFocus</enum>
</property>
<property name="toolTip">
<string>Remove Item</string>
</property>
<property name="icon">
<iconset resource="../images.qrc">
<normaloff>:/images/close_normal.png</normaloff>:/images/close_normal.png</iconset>
</property>
</widget>
</item>
</layout>
</item>
<item row="0" column="2">
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QLabel" name="timeLabel">
<property name="font">
<font>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string notr="true">TextLabel</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTop|Qt::AlignTrailing</set>
</property>
</widget>
</item>
</layout>
</item>
<item row="0" column="1">
<layout class="QGridLayout">
<property name="horizontalSpacing">
<number>6</number>
</property>
<property name="verticalSpacing">
<number>0</number>
</property>
<item row="1" column="1">
<widget class="QLabel" name="ipAddr">
<property name="sizePolicy">
<sizepolicy hsizetype="MinimumExpanding" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string notr="true">IP Address</string>
</property>
<property name="textInteractionFlags">
<set>Qt::LinksAccessibleByMouse|Qt::TextSelectableByKeyboard|Qt::TextSelectableByMouse</set>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="StyledLabel" name="ipAddrLabel">
<property name="sizePolicy">
<sizepolicy hsizetype="Maximum" vsizetype="Minimum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>IP address:</string>
</property>
<property name="alignment">
<set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter</set>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
</widget>
</item>
<item row="0" column="0" colspan="2">
<widget class="QLabel" name="titleLabel">
<property name="font">
<font>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string notr="true">Title</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QLabel" name="peer">
<property name="sizePolicy">
<sizepolicy hsizetype="MinimumExpanding" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string notr="true">Name (Location)</string>
</property>
<property name="textInteractionFlags">
<set>Qt::LinksAccessibleByMouse|Qt::TextSelectableByKeyboard|Qt::TextSelectableByMouse</set>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="StyledLabel" name="peerLabel">
<property name="text">
<string notr="true">Peer:</string>
</property>
<property name="alignment">
<set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter</set>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
</item>
<item row="1" column="0">
<widget class="QFrame" name="expandFrame">
<layout class="QGridLayout">
<item row="0" column="0">
<widget class="QLabel" name="peerIDLabel">
<property name="font">
<font>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>Peer ID:</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QLabel" name="location">
<property name="sizePolicy">
<sizepolicy hsizetype="MinimumExpanding" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string notr="true">Location</string>
</property>
<property name="textInteractionFlags">
<set>Qt::LinksAccessibleByMouse|Qt::TextSelectableByKeyboard|Qt::TextSelectableByMouse</set>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLabel" name="peerID">
<property name="sizePolicy">
<sizepolicy hsizetype="MinimumExpanding" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string notr="true">Peer ID</string>
</property>
<property name="textInteractionFlags">
<set>Qt::LinksAccessibleByMouse|Qt::TextSelectableByKeyboard|Qt::TextSelectableByMouse</set>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="locationLabel">
<property name="font">
<font>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>Location:</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="peerNameLabel">
<property name="font">
<font>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>Peer Name:</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLabel" name="peerName">
<property name="text">
<string notr="true">Peer Name</string>
</property>
<property name="textInteractionFlags">
<set>Qt::LinksAccessibleByMouse|Qt::TextSelectableByKeyboard|Qt::TextSelectableByMouse</set>
</property>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
<action name="actionNew_Message">
<property name="icon">
<iconset resource="../images.qrc">
<normaloff>:/images/mail_send.png</normaloff>:/images/mail_send.png</iconset>
</property>
<property name="text">
<string>Write Message</string>
</property>
</action>
</widget>
<customwidgets>
<customwidget>
<class>StyledLabel</class>
<extends>QLabel</extends>
<header>gui/common/StyledLabel.h</header>
</customwidget>
<customwidget>
<class>AvatarWidget</class>
<extends>QLabel</extends>
<header>gui/common/AvatarWidget.h</header>
<container>1</container>
</customwidget>
<customwidget>
<class>RsBanListToolButton</class>
<extends>QToolButton</extends>
<header>gui/common/RsBanListToolButton.h</header>
</customwidget>
</customwidgets>
<resources>
<include location="../images.qrc"/>
</resources>
<connections/>
</ui>

View File

@ -23,6 +23,7 @@
#include "rshare.h"
#include "rsharesettings.h"
#include "util/RsNetUtil.h"
#include <iostream>
@ -116,56 +117,13 @@ ServerPage::ServerPage(QWidget * parent, Qt::WindowFlags flags)
std::cerr << std::endl;
#endif
}
static bool parseAddrFromQString(const QString& s,struct sockaddr_storage& addr,int& bytes )
{
QStringList lst = s.split(".") ;
bytes = 0 ;
memset(&addr,0,sizeof(sockaddr_storage)) ;
addr.ss_family = AF_INET ;
QStringList::const_iterator it = lst.begin();
bool ok ;
uint32_t s1 = (*it).toInt(&ok) ; ++it ; if(!ok) return false ; if(it ==lst.end()) return false ;
uint32_t s2 = (*it).toInt(&ok) ; ++it ; if(!ok) return false ; if(it ==lst.end()) return false ;
uint32_t s3 = (*it).toInt(&ok) ; ++it ; if(!ok) return false ; if(it ==lst.end()) return false ;
QStringList lst2 = (*it).split("/") ;
it = lst2.begin();
uint32_t s4 ;
s4 = (*it).toInt(&ok) ; ++it ; if(!ok) return false ;
if(it != lst2.end())
{
uint32_t x = (*it).toInt(&ok) ; if(!ok) return false ;
if(x%8 != 0)
return false ;
if(x != 16 && x != 24)
return false ;
bytes = 4 - x/8 ;
}
const sockaddr_in *in = (const sockaddr_in*)&addr ;
((uint8_t*)&in->sin_addr.s_addr)[0] = s1 ;
((uint8_t*)&in->sin_addr.s_addr)[1] = s2 ;
((uint8_t*)&in->sin_addr.s_addr)[2] = s3 ;
((uint8_t*)&in->sin_addr.s_addr)[3] = s4 ;
return true;
}
void ServerPage::checkIpRange(const QString& ipstr)
{
QColor color;
struct sockaddr_storage addr;
int bytes ;
if(!parseAddrFromQString(ipstr,addr,bytes) || bytes != 0)
if(!RsNetUtil::parseAddrFromQString(ipstr,addr,bytes) || bytes != 0)
{
std::cout << "setting palette 1" << std::endl ;
color = QApplication::palette().color(QPalette::Disabled, QPalette::Base);
@ -189,7 +147,7 @@ void ServerPage::addIpRangeToBlackList()
sockaddr_storage addr ;
int bytes = 0 ;
if(!parseAddrFromQString(ipstr,addr,bytes) || bytes != 0)
if(!RsNetUtil::parseAddrFromQString(ipstr,addr,bytes) || bytes != 0)
return ;
bytes = 4 - ui.ipInputRange_SB->value()/8;
@ -203,7 +161,7 @@ void ServerPage::addIpRangeToWhiteList()
sockaddr_storage addr ;
int bytes = 0 ;
if(!parseAddrFromQString(ipstr,addr,bytes) || bytes != 0)
if(!RsNetUtil::parseAddrFromQString(ipstr,addr,bytes) || bytes != 0)
return ;
bytes = 4 - ui.ipInputRange_SB->value()/8;
@ -359,41 +317,6 @@ void ServerPage::load()
updateTorOutProxyIndicator();
}
static std::string print_addr(const struct sockaddr_storage& addr)
{
const sockaddr_in *in = (const sockaddr_in*)&addr ;
char str[100];
uint8_t *bytes = (uint8_t *) &(in->sin_addr.s_addr);
sprintf(str, "%u.%u.%u.%u", (int) bytes[0], (int) bytes[1], (int) bytes[2], (int) bytes[3]);
return std::string(str) ;
}
std::string print_addr_range(const struct sockaddr_storage& addr,uint8_t masked_bytes)
{
const sockaddr_in *in = (const sockaddr_in*)&addr ;
char str[100];
uint8_t *bytes = (uint8_t *) &(in->sin_addr.s_addr);
str[0] = 0 ;
switch(masked_bytes)
{
case 0: sprintf(str, "%u.%u.%u.%u", (int) bytes[0], (int) bytes[1], (int) bytes[2], (int) bytes[3]);
break ;
case 1: sprintf(str, "%u.%u.%u.255/24", (int) bytes[0], (int) bytes[1], (int) bytes[2]);
break ;
case 2: sprintf(str, "%u.%u.255.255/16", (int) bytes[0], (int) bytes[1]);
break ;
default:
std::cerr << "ERROR: Wrong format : masked_bytes = " << masked_bytes << std::endl;
return std::string("Invalid format") ;
}
return std::string(str) ;
}
void ServerPage::toggleAutoIncludeFriends(bool b)
{
rsBanList->enableIPsFromFriends(b) ;
@ -471,7 +394,7 @@ void ServerPage::updateSelectedBlackListIP(int row,int,int,int)
sockaddr_storage addr ;
int masked_bytes ;
if(!parseAddrFromQString(addr_string,addr,masked_bytes))
if(!RsNetUtil::parseAddrFromQString(addr_string,addr,masked_bytes))
{
std::cerr <<"Cannot parse IP \"" << addr_string.toStdString() << "\"" << std::endl;
return ;
@ -488,7 +411,7 @@ void ServerPage::updateSelectedWhiteListIP(int row, int,int,int)
sockaddr_storage addr ;
int masked_bytes ;
if(!parseAddrFromQString(addr_string,addr,masked_bytes))
if(!RsNetUtil::parseAddrFromQString(addr_string,addr,masked_bytes))
{
std::cerr <<"Cannot parse IP \"" << addr_string.toStdString() << "\"" << std::endl;
return ;
@ -501,7 +424,7 @@ void ServerPage::updateSelectedWhiteListIP(int row, int,int,int)
void ServerPage::addPeerToIPTable(QTableWidget *table,int row,const BanListPeer& blp)
{
table->setItem(row,COLUMN_RANGE,new QTableWidgetItem(QString::fromStdString(print_addr_range(blp.addr,blp.masked_bytes)))) ;
table->setItem(row,COLUMN_RANGE,new QTableWidgetItem(RsNetUtil::printAddrRange(blp.addr,blp.masked_bytes))) ;
if( blp.state )
table->setItem(row,COLUMN_STATUS,new QTableWidgetItem(QString("active"))) ;
@ -564,15 +487,15 @@ void ServerPage::ipFilterContextMenu(const QPoint& point)
sockaddr_storage addr ;
int masked_bytes ;
if(!parseAddrFromQString(addr_string,addr,masked_bytes))
if(!RsNetUtil::parseAddrFromQString(addr_string,addr,masked_bytes))
{
std::cerr <<"Cannot parse IP \"" << addr_string.toStdString() << "\"" << std::endl;
return ;
}
QString range0 = QString::fromStdString(print_addr_range(addr,0)) ;
QString range1 = QString::fromStdString(print_addr_range(addr,1)) ;
QString range2 = QString::fromStdString(print_addr_range(addr,2)) ;
QString range0 = RsNetUtil::printAddrRange(addr,0) ;
QString range1 = RsNetUtil::printAddrRange(addr,1) ;
QString range2 = RsNetUtil::printAddrRange(addr,2) ;
if(reason == RSBANLIST_REASON_USER)
contextMenu.addAction(tr("Remove"),this,SLOT(removeBannedIp()));
@ -594,7 +517,7 @@ bool ServerPage::removeCurrentRowFromBlackList(sockaddr_storage& collected_addr,
QString addr_string = ui.filteredIpsTable->item(row,COLUMN_RANGE)->text() ;
if(!parseAddrFromQString(addr_string,collected_addr,masked_bytes))
if(!RsNetUtil::parseAddrFromQString(addr_string,collected_addr,masked_bytes))
{
std::cerr <<"Cannot parse IP \"" << addr_string.toStdString() << "\"" << std::endl;
return false;
@ -614,7 +537,7 @@ bool ServerPage::removeCurrentRowFromWhiteList(sockaddr_storage& collected_addr,
QString addr_string = ui.whiteListIpsTable->item(row,COLUMN_RANGE)->text() ;
if(!parseAddrFromQString(addr_string,collected_addr,masked_bytes))
if(!RsNetUtil::parseAddrFromQString(addr_string,collected_addr,masked_bytes))
{
std::cerr <<"Cannot parse IP \"" << addr_string.toStdString() << "\"" << std::endl;
return false;
@ -672,15 +595,15 @@ void ServerPage::ipWhiteListContextMenu(const QPoint& point)
sockaddr_storage addr ;
int masked_bytes ;
if(!parseAddrFromQString(addr_string,addr,masked_bytes))
if(!RsNetUtil::parseAddrFromQString(addr_string,addr,masked_bytes))
{
std::cerr <<"Cannot parse IP \"" << addr_string.toStdString() << "\"" << std::endl;
return ;
}
QString range0 = QString::fromStdString(print_addr_range(addr,0)) ;
QString range1 = QString::fromStdString(print_addr_range(addr,1)) ;
QString range2 = QString::fromStdString(print_addr_range(addr,2)) ;
QString range0 = RsNetUtil::printAddrRange(addr,0) ;
QString range1 = RsNetUtil::printAddrRange(addr,1) ;
QString range2 = RsNetUtil::printAddrRange(addr,2) ;
// contextMenu.addAction(QObject::tr("Whitelist only IP " )+range0,this,SLOT(enableBannedIp()))->setEnabled(false) ;
//#warning UNIMPLEMENTED CODE

View File

@ -365,6 +365,7 @@ HEADERS += rshare.h \
lang/languagesupport.h \
util/RsProtectedTimer.h \
util/stringutil.h \
util/RsNetUtil.h \
util/DateTime.h \
util/win32.h \
util/RetroStyleLabel.h \
@ -477,6 +478,7 @@ HEADERS += rshare.h \
gui/common/PeerDefs.h \
gui/common/FilesDefs.h \
gui/common/PopularityDefs.h \
gui/common/RsBanListDefs.h \
gui/common/GroupTreeWidget.h \
gui/common/RSTreeView.h \
gui/common/AvatarWidget.h \
@ -495,6 +497,7 @@ HEADERS += rshare.h \
gui/common/UIStateHelper.h \
gui/common/FloatingHelpBrowser.h \
gui/common/SubscribeToolButton.h \
gui/common/RsBanListToolButton.h \
gui/common/FlowLayout.h \
gui/common/PictureFlow.h \
gui/common/StyledLabel.h \
@ -531,6 +534,7 @@ HEADERS += rshare.h \
gui/feeds/SubFileItem.h \
gui/feeds/AttachFileItem.h \
gui/feeds/SecurityItem.h \
gui/feeds/SecurityIpItem.h \
gui/feeds/NewsFeedUserNotify.h \
gui/connect/ConnectFriendWizard.h \
gui/connect/ConnectProgressDialog.h \
@ -636,6 +640,7 @@ FORMS += gui/StartDialog.ui \
gui/feeds/SubFileItem.ui \
gui/feeds/AttachFileItem.ui \
gui/feeds/SecurityItem.ui \
gui/feeds/SecurityIpItem.ui \
gui/im_history/ImHistoryBrowser.ui \
gui/groups/CreateGroup.ui \
gui/common/GroupTreeWidget.ui \
@ -716,6 +721,7 @@ SOURCES += main.cpp \
lang/languagesupport.cpp \
util/RsProtectedTimer.cpp \
util/stringutil.cpp \
util/RsNetUtil.cpp \
util/DateTime.cpp \
util/win32.cpp \
util/RetroStyleLabel.cpp \
@ -784,6 +790,7 @@ SOURCES += main.cpp \
gui/common/PeerDefs.cpp \
gui/common/FilesDefs.cpp \
gui/common/PopularityDefs.cpp \
gui/common/RsBanListDefs.cpp \
gui/common/GroupTreeWidget.cpp \
gui/common/RSTreeView.cpp \
gui/common/AvatarWidget.cpp \
@ -802,6 +809,7 @@ SOURCES += main.cpp \
gui/common/UIStateHelper.cpp \
gui/common/FloatingHelpBrowser.cpp \
gui/common/SubscribeToolButton.cpp \
gui/common/RsBanListToolButton.cpp \
gui/common/FlowLayout.cpp \
gui/common/PictureFlow.cpp \
gui/common/StyledLabel.cpp \
@ -870,6 +878,7 @@ SOURCES += main.cpp \
gui/feeds/SubFileItem.cpp \
gui/feeds/AttachFileItem.cpp \
gui/feeds/SecurityItem.cpp \
gui/feeds/SecurityIpItem.cpp \
gui/feeds/NewsFeedUserNotify.cpp \
gui/connect/ConnectFriendWizard.cpp \
gui/connect/ConnectProgressDialog.cpp \

View File

@ -0,0 +1,105 @@
/****************************************************************
* This file is distributed under the following license:
*
* Copyright (c) 2015, RetroShare Team
*
* 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 <QCoreApplication>
#include <QStringList>
#include "RsNetUtil.h"
#include <iostream>
#ifdef WINDOWS_SYS
#include <ws2tcpip.h>
#else
#include <netinet/in.h>
#endif
bool RsNetUtil::parseAddrFromQString(const QString& s, struct sockaddr_storage& addr, int& bytes)
{
QStringList lst = s.split(".") ;
bytes = 0 ;
memset(&addr,0,sizeof(sockaddr_storage)) ;
addr.ss_family = AF_INET ;
QStringList::const_iterator it = lst.begin();
bool ok ;
uint32_t s1 = (*it).toInt(&ok) ; ++it ; if(!ok) return false ; if(it ==lst.end()) return false ;
uint32_t s2 = (*it).toInt(&ok) ; ++it ; if(!ok) return false ; if(it ==lst.end()) return false ;
uint32_t s3 = (*it).toInt(&ok) ; ++it ; if(!ok) return false ; if(it ==lst.end()) return false ;
QStringList lst2 = (*it).split("/") ;
it = lst2.begin();
uint32_t s4 ;
s4 = (*it).toInt(&ok) ; ++it ; if(!ok) return false ;
if(it != lst2.end())
{
uint32_t x = (*it).toInt(&ok) ; if(!ok) return false ;
if(x%8 != 0)
return false ;
if(x != 16 && x != 24)
return false ;
bytes = 4 - x/8 ;
}
const sockaddr_in *in = (const sockaddr_in*)&addr ;
((uint8_t*)&in->sin_addr.s_addr)[0] = s1 ;
((uint8_t*)&in->sin_addr.s_addr)[1] = s2 ;
((uint8_t*)&in->sin_addr.s_addr)[2] = s3 ;
((uint8_t*)&in->sin_addr.s_addr)[3] = s4 ;
return true;
}
QString RsNetUtil::printAddr(const struct sockaddr_storage& addr)
{
const sockaddr_in *in = (const sockaddr_in*)&addr ;
uint8_t *bytes = (uint8_t *) &(in->sin_addr.s_addr);
return QString("%1.%2.%3.%4").arg(bytes[0]).arg(bytes[1]).arg(bytes[2]).arg(bytes[3]);
}
QString RsNetUtil::printAddrRange(const struct sockaddr_storage& addr, uint8_t masked_bytes)
{
const sockaddr_in *in = (const sockaddr_in*)&addr ;
uint8_t *bytes = (uint8_t *) &(in->sin_addr.s_addr);
switch(masked_bytes)
{
case 0:
return QString("%1.%2.%3.%4").arg(bytes[0]).arg(bytes[1]).arg(bytes[2]).arg(bytes[3]);
case 1:
return QString("%1.%2.%3.255/24").arg(bytes[0]).arg(bytes[1]).arg(bytes[2]);
case 2:
return QString("%1.%2.255.255/16").arg(bytes[0]).arg(bytes[1]);
}
std::cerr << "ERROR: Wrong format : masked_bytes = " << masked_bytes << std::endl;
return QCoreApplication::translate("RsNetUtil", "Invalid format");
}

View File

@ -0,0 +1,36 @@
/****************************************************************
* This file is distributed under the following license:
*
* Copyright (c) 2015, RetroShare Team
*
* 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.
****************************************************************/
#ifndef _RSNETUTIL_H
#define _RSNETUTIL_H
#include <QString>
#include <inttypes.h>
class RsNetUtil
{
public:
static bool parseAddrFromQString(const QString& s, struct sockaddr_storage& addr, int& bytes);
static QString printAddr(const struct sockaddr_storage& addr);
static QString printAddrRange(const struct sockaddr_storage& addr, uint8_t masked_bytes);
};
#endif