retargeted to the newest k8jam
[umfw.git] / umfwGUI / src / main.cpp
blobacf2562babe146060dac5b5de41f0e960a3b1260
1 #include <sys/types.h>
2 #include <unistd.h>
4 #include <QFile>
5 #include <QHostAddress>
6 #include <QProcess>
7 #include <QString>
8 #include <QTimer>
9 #include <QVector>
11 #include "uproto.h"
13 #include "askdialog.h"
15 #include "main.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);
24 fl.remove();
26 mRCPath = QApplication::applicationDirPath();
27 if (mRCPath.isEmpty()) mRCPath = "./";
28 else if (mRCPath.at(mRCPath.length()-1) != '/') mRCPath.append('/');
30 mRulePath = mRCPath;
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)));
46 mTrayIcon->show();
47 } else mTrayIcon = 0;
51 SockReactor::~SockReactor () {
55 void SockReactor::trayActivated (QSystemTrayIcon::ActivationReason reason) {
56 switch (reason) {
57 case QSystemTrayIcon::MiddleClick:
58 /*QCoreApplication::quit();*/
59 QApplication::quit();
60 break;
61 default: ;
66 void SockReactor::onConnection (void) {
67 qDebug() << "connection comes!";
68 processNextConnection();
72 void SockReactor::playSound (void) {
73 pid_t cp = fork();
74 if (cp != 0) return; // fail or parent
75 // child
76 execl(SOUND_CMD, SOUND_CMD, SOUND_ARG, (char *)NULL);
77 // on error
78 exit(0);
82 void SockReactor::processQueueStep (void) {
83 AskQueueItem *i = mQueue.get();
84 if (!i) return;
86 tIPCReply pr;
87 pr.version = UPROTO_VERSION;
89 quint8 allow, flags;
90 if (!mRules.getAction(i->mAppName, i->mIP, i->mPort, i->mProto, &allow, &flags)) {
91 AskDialog dlg;
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);
98 playSound();
100 qApp->alert(&dlg, 0);
101 switch (dlg.exec()) {
102 case QDialog::Accepted: pr.allow = IPCR_ALLOW; break;
103 default: pr.allow = IPCR_DENY;
105 pr.flags = 0;
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;
121 } else {
122 pr.allow = allow;
123 pr.flags = flags;
126 i->mSk->write((char *)&pr, sizeof(pr));
127 i->mSk->waitForBytesWritten(1500);
129 qDebug() << "request complete";
130 delete i;
132 if (mQueue.count() > 0) QTimer::singleShot(1, this, SLOT(processQueueStep()));
136 void SockReactor::processNextConnection (void) {
137 QLocalSocket *sk = mServer->nextPendingConnection();
138 do {
139 tIPCQuery pq;
141 qDebug() << "waiting...";
142 if (!sk->waitForReadyRead(1500)) {
143 qDebug() << "waiting failed";
144 break;
147 qDebug() << sizeof(pq);
148 if (sk->read((char *)&pq, sizeof(pq)) != sizeof(pq)) {
149 qDebug() << "can't read data from local socket";
150 break;
152 if (pq.version != UPROTO_VERSION) {
153 qDebug() << "invalid protocol version";
154 break;
157 mQueue.append(sk, pq);
158 if (mQueue.count() == 1) QTimer::singleShot(1, this, SLOT(processQueueStep()));
159 return;
160 } while (0);
162 /* error */
163 qDebug() << "processNextConnection(): some error occured";
164 delete sk;
165 return;
169 int main (int argc, char *argv[]) {
170 QApplication app(argc, argv);
172 SockReactor rc;
175 AskDialog *dlg = new AskDialog();
176 dlg->setInfo(23801, "127.0.0.1");
177 dlg->show();
180 return app.exec();