5 #include <QHostAddress>
13 #include "askdialog.h"
18 #define SOUND_CMD "/usr/bin/aplay"
19 #define SOUND_ARG "/home/ketmar/k8prj/usermode_fw/snd/flaps.wav"
21 SockReactor::SockReactor (QObject
*parent
) : QObject(parent
), mServer(0), mQueue(0) {
22 /* remove unix socket */
23 QFile
fl(UNIX_SOCKET_PATH
);
26 mRCPath
= QApplication::applicationDirPath();
27 if (mRCPath
.isEmpty()) mRCPath
= "./";
28 else if (mRCPath
.at(mRCPath
.length()-1) != '/') mRCPath
.append('/');
31 mRulePath
.append("rules.rc");
32 mRules
.loadFrom(mRulePath
);
34 mServer
= new QLocalServer (this);
35 connect(mServer
, SIGNAL(newConnection()), this, SLOT(onConnection()));
36 mServer
->listen(UNIX_SOCKET_PATH
);
37 qDebug() << "server name:" << mServer
->fullServerName();
38 qDebug() << "listening:" << mServer
->isListening();
40 if (QSystemTrayIcon::isSystemTrayAvailable()) {
41 mTrayIcon
= new QSystemTrayIcon(this);
42 mTrayIcon
->setIcon(QIcon(":/res/blackshield.png"));
43 mTrayIcon
->setToolTip("<b>UserModeFirewall</b><br>control center");
44 /*mTrayIcon->setContextMenu(trayMenu);*/
45 connect(mTrayIcon
, SIGNAL(activated(QSystemTrayIcon::ActivationReason
)), this, SLOT(trayActivated(QSystemTrayIcon::ActivationReason
)));
51 SockReactor::~SockReactor () {
55 void SockReactor::trayActivated (QSystemTrayIcon::ActivationReason reason
) {
57 case QSystemTrayIcon::MiddleClick
:
58 /*QCoreApplication::quit();*/
66 void SockReactor::onConnection (void) {
67 qDebug() << "connection comes!";
68 processNextConnection();
72 void SockReactor::playSound (void) {
74 if (cp
!= 0) return; // fail or parent
76 execl(SOUND_CMD
, SOUND_CMD
, SOUND_ARG
, (char *)NULL
);
82 void SockReactor::processQueueStep (void) {
83 AskQueueItem
*i
= mQueue
.get();
87 pr
.version
= UPROTO_VERSION
;
90 if (!mRules
.getAction(i
->mAppName
, i
->mIP
, i
->mPort
, i
->mProto
, &allow
, &flags
)) {
92 dlg
.setInfo(i
->mProto
, i
->mAction
, i
->mPid
, i
->mIPStr
, i
->mPort
, i
->mAppName
, "");
95 QProcess *playPrc = new QProcess(0);
96 playPrc->start(SOUND_CMD, SOUND_ARG);
100 qApp
->alert(&dlg
, 0);
101 switch (dlg
.exec()) {
102 case QDialog::Accepted
: pr
.allow
= IPCR_ALLOW
; break;
103 default: pr
.allow
= IPCR_DENY
;
106 if (dlg
.getOnlySession() || dlg
.getRemember()) {
107 pr
.flags
= IPCR_FLAG_IP
| IPCR_FLAG_PORT
;
108 if (dlg
.getAnyIP()) pr
.flags
|= IPCR_FLAG_ANYIP
;
109 if (dlg
.getAnyPort()) pr
.flags
|= IPCR_FLAG_ANYPORT
;
110 if (i
->mProto
== IPCQ_PROTO_TCP
) pr
.flags
|= IPCR_FLAG_TCP
; else pr
.flags
|= IPCR_FLAG_UDP
;
112 if (!dlg
.getOnlySession() && dlg
.getRemember()) {
113 /* create new rule */
114 QString
appn(i
->mAppName
);
115 if (dlg
.getAnyApp()) appn
= "*";
116 mRules
.addAction(dlg
.getDescr(), appn
, dlg
.getAnyIP()?0:i
->mIP
, dlg
.getAnyPort()?0:i
->mPort
, i
->mProto
, pr
.allow
);
117 mRules
.saveTo(mRulePath
);
118 qDebug() << "rule added";
120 //qDebug() << "allow:" << pr.allow;
126 i
->mSk
->write((char *)&pr
, sizeof(pr
));
127 i
->mSk
->waitForBytesWritten(1500);
129 qDebug() << "request complete";
132 if (mQueue
.count() > 0) QTimer::singleShot(1, this, SLOT(processQueueStep()));
136 void SockReactor::processNextConnection (void) {
137 QLocalSocket
*sk
= mServer
->nextPendingConnection();
141 qDebug() << "waiting...";
142 if (!sk
->waitForReadyRead(1500)) {
143 qDebug() << "waiting failed";
147 qDebug() << sizeof(pq
);
148 if (sk
->read((char *)&pq
, sizeof(pq
)) != sizeof(pq
)) {
149 qDebug() << "can't read data from local socket";
152 if (pq
.version
!= UPROTO_VERSION
) {
153 qDebug() << "invalid protocol version";
157 mQueue
.append(sk
, pq
);
158 if (mQueue
.count() == 1) QTimer::singleShot(1, this, SLOT(processQueueStep()));
163 qDebug() << "processNextConnection(): some error occured";
169 int main (int argc
, char *argv
[]) {
170 QApplication
app(argc
, argv
);
175 AskDialog *dlg = new AskDialog();
176 dlg->setInfo(23801, "127.0.0.1");