summaryrefslogtreecommitdiff
path: root/noncore/net/wellenreiter/gui/wellenreiter.cpp
blob: dd8a365285fc1babb24c32ec644340bd74beae57 (plain)
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/**********************************************************************
** Copyright (C) 2002 Michael 'Mickey' Lauer.  All rights reserved.
**
** This file is part of Opie Environment.
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
***********************************************************************/

// Qt

#include <qpushbutton.h>
#include <qmessagebox.h>
#include <qcombobox.h>
#include <qspinbox.h>
#include <qsocketnotifier.h>

// Qtopia

#ifdef QWS
#include <qpe/qpeapplication.h>
#include <qpe/global.h>
#endif

// Opie

#ifdef QWS
#include <opie/odevice.h>
using namespace Opie;
#endif

// Standard

#include <assert.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <fcntl.h>

// Local

#include "wellenreiter.h"
#include "scanlist.h"
#include "logwindow.h"
#include "hexwindow.h"
#include "configwindow.h"

#include "manufacturers.h"

#include <daemon/source/config.hh>
#include <libwellenreiter/source/wl_types.hh>
#include <libwellenreiter/source/wl_sock.hh>
#include <libwellenreiter/source/wl_proto.hh>

Wellenreiter::Wellenreiter( QWidget* parent )
    : WellenreiterBase( parent, 0, 0 ),
      daemonRunning( false ), manufacturerdb( 0 ), configwindow( 0 )
{

    //
    // construct manufacturer database
    //

    QString manufile;
    #ifdef QWS
    manufile.sprintf( "%s/share/wellenreiter/manufacturers.dat", (const char*) QPEApplication::qpeDir() );
    #else
    manufile.sprintf( "/usr/local/share/wellenreiter/manufacturers.dat" );
    #endif
    manufacturerdb = new ManufacturerDB( manufile );

    logwindow->log( "(i) Wellenreiter has been started." );

    //
    // detect operating system
    //

    #ifdef QWS
    QString sys;
    sys.sprintf( "(i) Running on '%s'.", (const char*) ODevice::inst()->systemString() );
    _system = ODevice::inst()->system();
    logwindow->log( sys );
    #endif

    //
    // setup socket for daemon communication, register socket notifier
    //

    // struct sockaddr_in sockaddr;
    daemon_fd = wl_setupsock( GUIADDR, GUIPORT, sockaddr );
    if ( daemon_fd == -1 )
    {
        logwindow->log( "(E) Couldn't get file descriptor for commsocket." );
    }
    else
    {
        int flags;
        flags = fcntl( daemon_fd, F_GETFL, 0 );
        fcntl( daemon_fd, F_SETFL, flags | O_NONBLOCK );
        QSocketNotifier *sn  = new QSocketNotifier( daemon_fd, QSocketNotifier::Read, parent );
        connect( sn, SIGNAL( activated( int ) ), this, SLOT( dataReceived() ) );
    }

    // setup GUI
    netview->setColumnWidthMode( 1, QListView::Manual );

    if ( manufacturerdb )
        netview->setManufacturerDB( manufacturerdb );

}

Wellenreiter::~Wellenreiter()
{
    // no need to delete child widgets, Qt does it all for us

    delete manufacturerdb;

    // X11-only - Hmm... Closing the socket here segfaults on exit,
    // Maybe because the notifier still has a handle to it!? Seems not to
    // occur on Qt/Embedded

    #ifdef QWS
    if ( daemon_fd != -1 )
    {
        qDebug( "closing comm socket" );
        close( daemon_fd );
    }
    #endif

}

void Wellenreiter::setConfigWindow( WellenreiterConfigWindow* cw )
{
    configwindow = cw;
}

void Wellenreiter::handleMessage()
{
    // FIXME: receive message and handle it

    qDebug( "received message from daemon." );

    /*char buffer[10000];
    memset( &buffer, 0, sizeof( buffer ) );*/
    
    char buffer[WL_SOCKBUF];

    // int result = #wl_recv( &daemon_fd, (char*) &buffer, sizeof(buffer) );
    
    /*
    
    struct sockaddr from;
    socklen_t len;
    
    int result = recvfrom( daemon_fd, &buffer, 8192, MSG_WAITALL, &from, &len );
    qDebug( "received %d from recv [%d bytes]", result, len );
    
    */
    
    int result = wl_recv( &daemon_fd, sockaddr, (char*) &buffer, WL_SOCKBUF );

    if ( result == -1 )
    {
        qDebug( "Warning: %s", strerror( errno ) );
        return;
    }

    int command = buffer[1] - 48;

/*
typedef struct {
  int net_type;     1 = Accesspoint ; 2 = Ad-Hoc
  int ssid_len;     Length of SSID
  int channel;      Channel
  int wep;          1 = WEP enabled ; 0 = disabled
  char mac[64];     MAC address of Accesspoint
  char bssid[128];  BSSID of Accesspoint
} wl_network_t;
*/

    qDebug( "Recv result: %d", ( result ) );
    qDebug( "Sniffer sent: '%s'", (const char*) buffer );
    hexwindow->log( (const char*) &buffer );

    if ( command == NETFOUND )  /* new network found */
    {
        qDebug( "Sniffer said: new network found." );
        wl_network_t n;
        get_network_found( &n, (char*) &buffer );
        
        qDebug( "Sniffer said: net_type is %d.", n.net_type );
        qDebug( "Sniffer said: MAC is %s", (const char*) &n.mac );

        //n.bssid[n.ssid_len] = "\0";

        QString type;
        
        if ( n.net_type == 1 )
            type = "managed";
        else
            type = "adhoc";

        netview->addNewItem( type, n.bssid, QString( (const char*) &n.mac ), n.wep, n.channel, 0 );

    }

    else

    {
        qDebug( "unknown sniffer command." );
    }

}

void Wellenreiter::dataReceived()
{
    logwindow->log( "(d) Received data from daemon" );
    handleMessage();
}

void Wellenreiter::startStopClicked()
{
    if ( daemonRunning )
    {
        daemonRunning = false;

        logwindow->log( "(i) Daemon has been stopped." );
        setCaption( tr( "Wellenreiter/Opie" ) );

        // Stop daemon - ugly for now... later better

        system( "killall wellenreiterd" );

        // get configuration from config window

        const QString& interface = configwindow->interfaceName->currentText();

        // reset the interface trying to get it into a usable state again

        QString cmdline;
        cmdline.sprintf( "iwpriv %s monitor 0; ifdown %s; ifup %s", (const char*) interface, (const char*) interface, (const char*) interface );
        system( cmdline );

        // message the user

        QMessageBox::information( this, "Wellenreiter/Opie", "Your wireless card\nshould now be usable again." );
    }

    else
    {

        // get configuration from config window

        const QString& interface = configwindow->interfaceName->currentText();
        const int cardtype = configwindow->daemonDeviceType();
        const int interval = configwindow->daemonHopInterval();

        if ( ( interface == "<select>" ) || ( cardtype == 0 ) )
        {
            QMessageBox::information( this, "Wellenreiter/Opie", "Your device is not\nptoperly configured. Please reconfigure!" );
            return;
        }

        // start wellenreiterd

        QString cmdline;
        cmdline.sprintf( "wellenreiterd %s %d &", (const char*) interface, cardtype );

        qDebug( "about to execute '%s' ...", (const char*) cmdline );
        system( cmdline );
        qDebug( "done!" );

        logwindow->log( "(i) Daemon has been started." );
        daemonRunning = true;
        setCaption( tr( "Scanning ..." ) );

    }
}