File: kismet_client.cc

package info (click to toggle)
kismet 2008-05-R1-4
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 3,232 kB
  • ctags: 3,998
  • sloc: cpp: 33,568; sh: 5,544; ansic: 459; makefile: 457; perl: 62; sql: 41
file content (1014 lines) | stat: -rw-r--r-- 33,040 bytes parent folder | download | duplicates (2)
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
/*
    This file is part of Kismet

    Kismet 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.

    Kismet 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 Kismet; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include "config.h"

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "getopt.h"
#include "tcpclient.h"
#include "frontend.h"
#include "cursesfront.h"
#include "panelfront.h"
#include "configfile.h"
#include "speech.h"
#include "timetracker.h"

// Make gpsd happy
Timetracker timetracker;

#ifndef exec_name
char *exec_name;
#endif

enum gui_type {
    gui_ncurses, gui_panel
    };

const char *config_base = "kismet.conf";
const char *uiconfig_base = "kismet_ui.conf";

TcpClient kismet_serv;
Frontend *gui;
gui_type guitype;
char *configfile;
char *uiconfigfile;
char *server = NULL;
int sound = -1;
int speech = -1;
string voice;
int flite = 0, darwinsay = 0;
int speech_encoding = 0;
string speech_sentence_encrypted, speech_sentence_unencrypted;
unsigned int metric = 0;
unsigned int reconnect = 0;

int group_track = 0;
string configdir, groupfile;
FILE *group_file = NULL;

// Pipe file descriptor pairs and fd's
int soundpair[2];
int speechpair[2];
pid_t soundpid = -1, speechpid = -1;

// Catch our interrupt
void CatchShutdown(int sig) {

    // Kill our sound players
    if (soundpid > 0)
        kill(soundpid, 9);
    if (speechpid > 0)
        kill(speechpid, 9);

    if (group_track) {
        if ((group_file = fopen(groupfile.c_str(), "w")) == NULL) {
            fprintf(stderr, "WARNING: Unable to open '%s' for writing, groups will not be saved.\n",
                    groupfile.c_str());
        } else {
            gui->WriteGroupMap(group_file);
        }
    }

    int ret = 0;
    if (gui != NULL && sig != SIGHUP)
        gui->EndDisplay();

    if (kismet_serv.Valid() == 0)
        ret = 5;

    exit(ret);
}

int Usage(char *argv) {
    printf("Usage: %s [OPTION]\n", argv);
    printf("Most (or all) of these options can (and should) be configured via the\n"
           "kismet_ui.conf global config file, but can be overridden here.\n");
    printf(
           "  -f, --config-file <file>     Use alternate config file\n"
           "  -u, --ui-config-file <file>  Use alternate UI config file\n"
           "  -q, --quiet                  Don't play sounds\n"
           "  -s, --server <host:port>     Connect to Kismet host and port\n"
           "  -g, --gui <type>             GUI type to create (curses, panel)\n"
           "  -c, --columns <list>         Columns to display initially (comma seperated)\n"
           "  -r, --reconnect              Try to reconnect after the client/server connection\n"
           "                               fails.\n"
           "  -C, --client-columns <list>  Columns to display for client info\n"
           "  -v, --version                Kismet version\n"
           "  -h, --help                   What do you think you're reading?\n");
    exit(1);
}

// sigpipe handler
void PipeHandler(int sig) {
    exit(0);
}

// Subprocess sound handler
void SoundHandler(int *fds, const char *player, map<string, string> soundmap) {
    int read_sock = fds[0];

    close(fds[1]);

    signal(SIGPIPE, PipeHandler);

    fd_set rset;

    char data[1024];

    pid_t sndpid = -1;
    int harvested = 1;

    while (1) {
        FD_ZERO(&rset);
        FD_SET(read_sock, &rset);
        char *end;

        memset(data, 0, 1024);

        if (harvested == 0) {
            // We consider a wait error to be a sign that the child pid died
            // so we flag it as harvested and keep on going
            pid_t harvestpid = waitpid(sndpid, NULL, WNOHANG);
            if (harvestpid == -1 || harvestpid == sndpid)
                harvested = 1;
        }

        struct timeval tim;
        tim.tv_sec = 1;
        tim.tv_usec = 0;

        if (select(read_sock + 1, &rset, NULL, NULL, &tim) < 0) {
            if (errno != EINTR) {
                exit(1);
            }
        }

        if (FD_ISSET(read_sock, &rset)) {
            int ret;
            ret = read(read_sock, data, 1024);

            // We'll die off if we get a read error, and we'll let kismet on the
            // other side detact that it died
            if (ret <= 0 && (errno != EAGAIN && errno != EPIPE))
                exit(1);

            if ((end = strstr(data, "\n")) == NULL)
                continue;

            end[0] = '\0';
        }

        if (data[0] == '\0')
            continue;

        // If we've harvested the process, spawn a new one and watch it
        // instead.  Otherwise, we just let go of the data we read
        if (harvested == 1) {
            // Only take the first line
            char *nl;
            if ((nl = strchr(data, '\n')) != NULL)
                *nl = '\0';

            char snd[1024];

            if (soundmap.size() == 0)
                snprintf(snd, 1024, "%s", data);
            if (soundmap.find(data) != soundmap.end())
                snprintf(snd, 1024, "%s", soundmap[data].c_str());
            else
                continue;

            char plr[1024];
            snprintf(plr, 1024, "%s", player);

            harvested = 0;
            if ((sndpid = fork()) == 0) {
                // Suppress errors
                int nulfd = open("/dev/null", O_RDWR);
                dup2(nulfd, 1);
                dup2(nulfd, 2);

                char * const echoarg[] = { plr, snd, NULL };
                execve(echoarg[0], echoarg, NULL);
            }
        }

        data[0] = '\0';
    }
}

// Subprocess speech handler
void SpeechHandler(int *fds, const char *player) {
    int read_sock = fds[0];
    close(fds[1]);

    fd_set rset;

    char data[1024];

    pid_t sndpid = -1;
    int harvested = 1;

    while (1) {
        FD_ZERO(&rset);
        FD_SET(read_sock, &rset);
        //char *end;

        memset(data, 0, 1024);

        struct timeval tim;
        tim.tv_sec = 1;
        tim.tv_usec = 0;

        if (select(read_sock + 1, &rset, NULL, NULL, &tim) < 0) {
            if (errno != EINTR) {
                exit(1);
            }
        }

        if (harvested == 0) {
            // We consider a wait error to be a sign that the child pid died
            // so we flag it as harvested and keep on going
            pid_t harvestpid = waitpid(sndpid, NULL, WNOHANG);
            if (harvestpid == -1 || harvestpid == sndpid)
                harvested = 1;
        }

        if (FD_ISSET(read_sock, &rset)) {
            int ret;
            ret = read(read_sock, data, 1024);

            // We'll die off if we get a read error, and we'll let kismet on the
            // other side detact that it died
            if (ret <= 0 && (errno != EAGAIN && errno != EPIPE))
                exit(1);

            data[ret] = '\0';

        }

        if (data[0] == '\0')
            continue;

        // If we've harvested the process, spawn a new one and watch it
        // instead.  Otherwise, we just let go of the data we read
        if (harvested == 1) {
            harvested = 0;
            if ((sndpid = fork()) == 0) {
                // Only take the first line
                char *nl;
                if ((nl = strchr(data, '\n')) != NULL)
                    *nl = '\0';

                // Make sure it's shell-clean
                MungeToShell(data, strlen(data));
                char spk_call[1024];
				char voiceopt[128] = "";

				if (voice != "default") {
					if (darwinsay)
						snprintf(voiceopt, 128, "-v %s",
								 MungeToShell(voice).c_str());
				}

                snprintf(spk_call, 1024, "echo \"(%s\\\"%s\\\")\" | %s %s "
						 ">/dev/null 2>/dev/null",
						 (flite || darwinsay) ? "": "SayText ", 
						 data, player, voiceopt);

                system(spk_call);

                exit(0);
            }
        }

        data[0] = '\0';
    }
}

int PlaySound(string in_sound) {

    char snd[1024];

    snprintf(snd, 1024, "%s\n", in_sound.c_str());

    if (write(soundpair[1], snd, strlen(snd)) < 0) {
        char status[STATUS_MAX];
        snprintf(status, STATUS_MAX,
                 "ERROR: Could not write to sound pipe.  Stopping sound.");
        gui->WriteStatus(status);

        return 0;
    }

    return 1;
}

int SayText(string in_text) {
    char snd[1024];

    snprintf(snd, 1024, "%s\n", in_text.c_str());

    if (write(speechpair[1], snd, strlen(snd)) < 0) {
        char status[STATUS_MAX];
        snprintf(status, STATUS_MAX,
                 "ERROR: Could not write to speech pipe.  Stopping speech.");
        gui->WriteStatus(status);

        return 0;
    }

    return 1;
}

int main(int argc, char *argv[]) {
    exec_name = argv[0];

    time_t last_draw = time(0);
    char status[STATUS_MAX];

    char *reqgui = NULL;

    string sndplay;
    const char *festival = NULL;

    char *columns = NULL;
    char *clientcolumns = NULL;

    map<string, string> wav_map;

    char guihost[1024];
    int guiport = -1;

    int gpsmode = -1;

    configfile = NULL;
    uiconfigfile = NULL;

    char *ap_manuf_name = NULL, *client_manuf_name = NULL;
    FILE *manuf_data;

    static struct option long_options[] = {   /* options table */
        { "config-file", required_argument, 0, 'f' },
        { "ui-config-file", required_argument, 0, 'u' },
        { "quiet", no_argument, 0, 'q' },
        { "server", required_argument, 0, 's' },
        { "help", no_argument, 0, 'h' },
        { "version", no_argument, 0, 'v' },
        { "columns", required_argument, 0, 'c' },
        { "client-columns", required_argument, 0, 'C'},
        { "reconnct", no_argument, 0, 'r' },
        { "gui", required_argument, 0, 'g'},
        { 0, 0, 0, 0 }
    };
    int option_index;
    int decay = 5;

    // Catch the interrupt handler to shut down
    signal(SIGINT, CatchShutdown);
    signal(SIGTERM, CatchShutdown);
    signal(SIGHUP, CatchShutdown);
    signal(SIGPIPE, SIG_IGN);

    while(1) {
        int r = getopt_long(argc, argv, "f:c:qs:hvg:u:r",
                            long_options, &option_index);
        if (r < 0) break;
        switch(r) {
        case 'f':
            // Config path
            configfile = optarg;
            fprintf(stderr, "Using alternate config file: %s\n", configfile);
            break;
        case 'u':
            // UI config path
            uiconfigfile = optarg;
            fprintf(stderr, "Using alternate UI config file: %s\n", uiconfigfile);
            break;
        case 'q':
            sound = 0;
            break;
        case 'g':
            reqgui = strdup(optarg);
            break;
        case 'v':
            fprintf(stderr, "Kismet curses %s.%s.%s\n", VERSION_MAJOR, VERSION_MINOR, VERSION_TINY);
            break;
        case 's':
            server = optarg;
            fprintf(stderr, "Using server: %s\n", server);
            break;
        case 'c':
            columns = strdup(optarg);
            break;
        case 'C':
            clientcolumns = strdup(optarg);
            break;
        case 'r':
            reconnect = 1;
            break;
        default:
            Usage(argv[0]);
            break;
        }
    }

    ConfigFile *server_conf = new ConfigFile;
    ConfigFile *gui_conf = new ConfigFile;

    // If we haven't gotten a command line config option...
    if (configfile == NULL) {
        configfile = (char *) malloc(1024*sizeof(char));
        snprintf(configfile, 1024, "%s/%s", getenv("KISMET_CONF") != NULL ? getenv("KISMET_CONF") : SYSCONF_LOC, config_base);
    }

    if (uiconfigfile == NULL) {
        uiconfigfile = (char *) malloc(1024*sizeof(char));
        snprintf(uiconfigfile, 1024, "%s/%s", getenv("KISMET_CONF") != NULL ? getenv("KISMET_CONF") : SYSCONF_LOC, uiconfig_base);
    }

    // Parse the config and load all the values from it and/or our command
    // line options.  This is a little soupy but it does the trick.
    if (server_conf->ParseConfig(configfile) < 0) {
        fprintf(stderr, "Unable to parse config file.");
        exit(1);
    }

    if (gui_conf->ParseConfig(uiconfigfile) < 0) {
        fprintf(stderr, "Unable to parse ui config file.");
        exit(1);
    }

    if (server_conf->FetchOpt("configdir") != "") {
        configdir = server_conf->ExpandLogPath(server_conf->FetchOpt("configdir"), "", "", 0, 1);
    } else {
        fprintf(stderr, "FATAL:  No 'configdir' option in the config file.\n");
        exit(1);
    }

    if (server_conf->FetchOpt("groupmap") != "") {
        // Explode the group map path
        groupfile = server_conf->ExpandLogPath(configdir + server_conf->FetchOpt("groupmap"),
                                              "", "", 0, 1);
        group_track = 1;
    }

    if (server_conf->FetchOpt("metric") == "true") {
        metric = 1;
    }

    if (reqgui == NULL) {
        if (gui_conf->FetchOpt("gui") == "") {
            fprintf(stderr, "ERROR:  No gui given in config file and none given on the command line.\n");
            exit(1);
        }

        reqgui = strdup(gui_conf->FetchOpt("gui").c_str());
    }

    if (!strcasecmp(reqgui, "curses")) {
#if defined(HAVE_LIBNCURSES) && defined(BUILD_CURSES)
        gui = new NCurseFront;
        guitype = gui_ncurses;
#else
        fprintf(stderr, "ERROR:  Curses support not compiled in.\n");
        exit(1);
#endif
    } else if (!strcasecmp(reqgui, "panel")) {
#if defined(HAVE_LIBPANEL) && defined(BUILD_PANEL) && defined(HAVE_LIBNCURSES)
        gui = new PanelFront;
        guitype = gui_panel;
#else
        fprintf(stderr, "ERROR:  Panels support not compiled in.\n");
        exit(1);
#endif
    } else {
        fprintf(stderr, "ERROR:  Unknown GUI type requested ('%s')\n", reqgui);
        exit(1);
    }
    free(reqgui);

    if (columns == NULL) {
        if (gui_conf->FetchOpt("columns") == "") {
            fprintf(stderr, "FATAL:  No columns in the config file and none given on the command line.\n");
            exit(1);
        }

        columns = strdup(gui_conf->FetchOpt("columns").c_str());
    }

    if (clientcolumns == NULL) {
        if (gui_conf->FetchOpt("clientcolumns") == "") {
            fprintf(stderr, "FATAL: No client columns in the config file and none given on the command line.\n");
            exit(1);
        }

        clientcolumns = strdup(gui_conf->FetchOpt("clientcolumns").c_str());
    }

    if (server == NULL) {
        server = strdup(gui_conf->FetchOpt("host").c_str());
    }

    if (gui_conf->FetchOpt("sound") == "true" && sound == -1) {
        if (gui_conf->FetchOpt("soundplay") != "") {
            sndplay = gui_conf->FetchOpt("soundplay");
            sound = 1;

            if (gui_conf->FetchOpt("soundopts") != "")
                sndplay += " " + gui_conf->FetchOpt("soundopts");

            if (gui_conf->FetchOpt("sound_new") != "")
                wav_map["new"] = gui_conf->FetchOpt("sound_new");
            if (gui_conf->FetchOpt("sound_new_wep") != "")
                wav_map["new_wep"] = gui_conf->FetchOpt("sound_new_wep");
            if (gui_conf->FetchOpt("sound_traffic") != "")
                wav_map["traffic"] = gui_conf->FetchOpt("sound_traffic");
            if (gui_conf->FetchOpt("sound_junktraffic") != "")
                wav_map["junktraffic"] = gui_conf->FetchOpt("sound_junktraffic");
            if (gui_conf->FetchOpt("sound_gpslock") != "")
                wav_map["gpslock"] = gui_conf->FetchOpt("sound_gpslock");
            if (gui_conf->FetchOpt("sound_gpslost") != "")
                wav_map["gpslost"] = gui_conf->FetchOpt("sound_gpslost");
            if (gui_conf->FetchOpt("sound_alert") != "")
                wav_map["alert"] = gui_conf->FetchOpt("sound_alert");

        } else {
            fprintf(stderr, "ERROR:  Sound alerts enabled but no sound playing binary specified.\n");
            sound = 0;
        }
    } else if (sound == -1)
        sound = 0;

    /* Added by Shaw Innes 17/2/02 */
    if (gui_conf->FetchOpt("speech") == "true" && speech == -1) {
        if (gui_conf->FetchOpt("festival") != "") {
            festival = strdup(gui_conf->FetchOpt("festival").c_str());
            speech = 1;

			if (gui_conf->FetchOpt("flite") == "true")
				flite = 1;

#ifdef SYS_DARWIN
			if (gui_conf->FetchOpt("darwinsay") == "true") {
				festival = strdup("/usr/bin/say");
				darwinsay = 1;
			}
#endif

			voice = gui_conf->FetchOpt("speech_voice");

			if (voice == "")
				voice = "default";

            string speechtype = gui_conf->FetchOpt("speech_type");

            if (!strcasecmp(speechtype.c_str(), "nato"))
                speech_encoding = SPEECH_ENCODING_NATO;
            else if (!strcasecmp(speechtype.c_str(), "spell"))
                speech_encoding = SPEECH_ENCODING_SPELL;
            else
                speech_encoding = SPEECH_ENCODING_NORMAL;

            // Make sure we have encrypted text lines
            if (gui_conf->FetchOpt("speech_encrypted") == "" || 
				gui_conf->FetchOpt("speech_unencrypted") == "") {
                fprintf(stderr, "ERROR:  Speech request but speech_encrypted or "
						"speech_unencrypted line missing.\n");
                speech = 0;
            }

            speech_sentence_encrypted = gui_conf->FetchOpt("speech_encrypted");
            speech_sentence_unencrypted = gui_conf->FetchOpt("speech_unencrypted");

        } else {
            fprintf(stderr, "ERROR: Speech alerts enabled but no path to festival "
					"has been specified.\n");
            speech = 0;
        }
    } else if (speech == -1)
        speech = 0;

    if (gui_conf->FetchOpt("decay") != "") {
        if (sscanf(gui_conf->FetchOpt("decay").c_str(), "%d", &decay) != 1) {
            fprintf(stderr, "FATAL:  Illegal config file value for decay.\n");
            exit(1);
        }
    }

    if (server_conf->FetchOpt("ap_manuf") != "") {
        ap_manuf_name = strdup(server_conf->FetchOpt("ap_manuf").c_str());
    } else {
        fprintf(stderr, "WARNING:  No ap_manuf file specified, AP manufacturers and defaults will not be detected.\n");
    }

    if (server_conf->FetchOpt("client_manuf") != "") {
        client_manuf_name = strdup(server_conf->FetchOpt("client_manuf").c_str());
    } else {
        fprintf(stderr, "WARNING:  No client_manuf file specified.  Client manufacturers will not be detected.\n");
    }

    if (sscanf(server, "%1023[^:]:%d", guihost, &guiport) != 2) {
        fprintf(stderr, "FATAL:  Invalid server (%s) specified (host:port required)\n",
               server);
        exit(1);
    }

    // handle the config bits
    struct stat fstat;
    if (stat(configdir.c_str(), &fstat) == -1) {
        fprintf(stderr, "NOTICE: configdir '%s' does not exist, making it.\n",
                configdir.c_str());
        if (mkdir(configdir.c_str(), S_IRUSR | S_IWUSR | S_IXUSR) < 0) {
            fprintf(stderr, "FATAL:  Could not make configdir: %s\n",
                    strerror(errno));
            exit(1);
        }
    } else if (! S_ISDIR(fstat.st_mode)) {
        fprintf(stderr, "FATAL: configdir '%s' exists but is not a directory.\n",
                configdir.c_str());
        exit(1);
    }

    if (group_track) {
        if (stat(groupfile.c_str(), &fstat) == -1) {
            fprintf(stderr, "NOTICE:  Group file did not exist, it will be created.\n");
        } else {
            if ((group_file = fopen(groupfile.c_str(), "r")) == NULL) {
                fprintf(stderr, "FATAL:  Could not open group file '%s': %s\n",
                        groupfile.c_str(), strerror(errno));
                exit(1);
            }

            gui->ReadGroupMap(group_file);

            fclose(group_file);
        }
    }

    // Fork and find the sound options
    if (sound) {
        if (pipe(soundpair) == -1) {
            fprintf(stderr, "WARNING:  Unable to create pipe for audio.  Disabling sound.\n");
            sound = 0;
        } else {
            soundpid = fork();

            if (soundpid < 0) {
                fprintf(stderr, "WARNING:  Unable to fork for audio.  Disabling sound.\n");
                sound = 0;
            } else if (soundpid == 0) {
                SoundHandler(soundpair, sndplay.c_str(), wav_map);
                exit(0);
            }

            close(soundpair[0]);
        }
    }

    if (speech) {
        if (pipe(speechpair) == -1) {
            fprintf(stderr, "WARNING:  Unable to create pipe for speech.  Disabling speech.\n");
            speech = 0;
        } else {
            speechpid = fork();

            if (speechpid < 0) {
                fprintf(stderr, "WARNING:  Unable to fork for speech.  Disabling speech.\n");
                speech = 0;
            } else if (speechpid == 0) {
                SpeechHandler(speechpair, festival);
                exit(0);
            }

            close(speechpair[0]);
        }
    }

    if (kismet_serv.Connect(guiport, guihost) < 0) {
        fprintf(stderr, "FATAL:  Could not connect to %s:%d.\n", guihost, guiport);
        CatchShutdown(-1);
    }

    time_t serv_start = 0;
    // Spin for 15 seconds until we get a header from the server, or die
    fprintf(stderr, "Looking for startup info from %s:%d...", guihost, guiport);
    int header_count = 0;

    while (1) {

        fprintf(stderr, ".");

        serv_start = kismet_serv.FetchStart();

		if (serv_start)
			break;

        if (kismet_serv.Valid())
            kismet_serv.Poll();

        if (header_count++ >= 20) {
            fprintf(stderr, " failed.\nFATAL:  Did not get startup info from %s:%d within 20 seconds.\n",
                    guihost, guiport);
            CatchShutdown(-1);
        }

        sleep(1);
    }

    fprintf(stderr, " found.\nConnected to Kismet server %s.%s.%s on %s:%d\n",
            kismet_serv.FetchMajor(), kismet_serv.FetchMinor(), kismet_serv.FetchTiny(),
            guihost, guiport);

    map<string, string> prefs;

    prefs["showintro"] = gui_conf->FetchOpt("showintro");
    
    prefs["columns"] = columns;
    prefs["clientcolumns"] = clientcolumns;

    if (gui_conf->FetchOpt("apm") == "true")
        prefs["apm"] = "true";

    if (gui_conf->FetchOpt("autogroup_probe") == "true")
        prefs["autogroup_probe"] = "true";

    if (gui_conf->FetchOpt("autogroup_data") == "true")
        prefs["autogroup_data"] = "true";

	if (gui_conf->FetchOpt("autogroup_adhoc") == "true")
		prefs["autogroup_adhoc"] = "true";

    prefs["simpleborders"] = gui_conf->FetchOpt("simpleborders");

    prefs["color"] = gui_conf->FetchOpt("color");
    prefs["backgroundcolor"] = gui_conf->FetchOpt("backgroundcolor");
    prefs["textcolor"] = gui_conf->FetchOpt("textcolor");
    prefs["bordercolor"] = gui_conf->FetchOpt("bordercolor");
    prefs["titlecolor"] = gui_conf->FetchOpt("titlecolor");
    prefs["wepcolor"] = gui_conf->FetchOpt("wepcolor");
    prefs["factorycolor"] = gui_conf->FetchOpt("factorycolor");
    prefs["cloakcolor"] = gui_conf->FetchOpt("cloakcolor");
    prefs["opencolor"] = gui_conf->FetchOpt("opencolor");
    prefs["monitorcolor"] = gui_conf->FetchOpt("monitorcolor");

    // We're done with the config files, delete their memory allocations
    delete server_conf;
    delete gui_conf;
    server_conf = gui_conf = NULL;

    gui->AddPrefs(prefs);

    free(columns);
    free(clientcolumns);

    if (ap_manuf_name != NULL) {
        char pathname[1024];

        if (strchr(ap_manuf_name, '/') == NULL)
            snprintf(pathname, 1024, "%s/%s", getenv("KISMET_CONF") != NULL ? getenv("KISMET_CONF") : SYSCONF_LOC, ap_manuf_name);
        else
            snprintf(pathname, 1024, "%s", ap_manuf_name);

        if ((manuf_data = fopen(pathname, "r")) == NULL) {
            fprintf(stderr, "WARNING:  Unable to open '%s' for reading (%s), AP manufacturers and defaults will not be detected.\n",
                    pathname, strerror(errno));
        } else {
            fprintf(stderr, "Reading AP manufacturer data and defaults from %s\n", pathname);
            gui->ReadAPManufMap(manuf_data);
            fclose(manuf_data);
        }

        free(ap_manuf_name);
    }

    if (client_manuf_name != NULL) {
        char pathname[1024];

        if (strchr(client_manuf_name, '/') == NULL)
            snprintf(pathname, 1024, "%s/%s", getenv("KISMET_CONF") != NULL ? getenv("KISMET_CONF") : SYSCONF_LOC, client_manuf_name);
        else
            snprintf(pathname, 1024, "%s", client_manuf_name);

        if ((manuf_data = fopen(pathname, "r")) == NULL) {
            fprintf(stderr, "WARNING:  Unable to open '%s' for reading (%s), client manufacturers will not be detected.\n",
                    pathname, strerror(errno));
        } else {
            fprintf(stderr, "Reading client manufacturer data and defaults from %s\n", pathname);
            gui->ReadClientManufMap(manuf_data);
            fclose(manuf_data);
        }

        free(client_manuf_name);
    }

    gui->AddClient(&kismet_serv);

    gui->InitDisplay(decay, serv_start);

    snprintf(status, STATUS_MAX, "Connected to Kismet server version %s.%s.%s build %s on %s:%d",
             kismet_serv.FetchMajor(), kismet_serv.FetchMinor(), kismet_serv.FetchTiny(),
             kismet_serv.FetchBuild(), guihost, guiport);
    gui->WriteStatus(status);
    gui->DrawDisplay();

    int num_networks = 0, num_packets = 0, num_noise = 0, num_dropped = 0;

    time_t last_click = time(0);

    fd_set read_set;
    FD_ZERO(&read_set);

    unsigned int max_fd = 0;
    FD_SET(fileno(stdin), &read_set);

    vector<TcpClient *> client_list;
    TcpClient *primary_client;
    wireless_network *lastspoken = NULL;

    while (1) {
        fd_set rset;
        FD_ZERO(&rset);
        rset = read_set;

        // Grab the list of clients and set them all in the readset
        gui->FetchClients(&client_list);
        primary_client = gui->FetchPrimaryClient();

        for (unsigned int cli = 0; cli < client_list.size(); cli++) {
            if (client_list[cli]->Valid()) {
                unsigned int client_descrip = client_list[cli]->FetchDescriptor();
                FD_SET(client_descrip, &rset);
                if (client_descrip > max_fd)
                    max_fd = client_descrip;
            }
        }

        struct timeval tim;
        tim.tv_sec = 0;
        tim.tv_usec = 500000;

        if (select(max_fd + 1, &rset, NULL, NULL, &tim) < 0) {
            if (errno != EINTR) {
                snprintf(status, STATUS_MAX,
                         "ERROR: select() error %d (%s)", errno, strerror(errno));
                gui->WriteStatus(status);
            }
        }

        // Handle user data
        if (FD_ISSET(fileno(stdin), &rset)) {
            int poll_ret;
            poll_ret = gui->Poll();

            if (poll_ret == FE_QUIT)
                CatchShutdown(-1);
        }

        // Reset our counters so we can add the different servers
        num_networks = 0;
        num_packets = 0;
        num_noise = 0;
        num_dropped = 0;

        for (unsigned int cli = 0; cli < client_list.size(); cli++) {
            // If we have incoming data...
            TcpClient *tcpcli = client_list[cli];
            unsigned int client_descrip = tcpcli->FetchDescriptor();
            if (tcpcli->Valid()) {
                if (FD_ISSET(client_descrip, &rset)) {
                    int pollret;
                    if ((pollret = tcpcli->Poll()) < 0) {
                        snprintf(status, STATUS_MAX, "%s:%d TCP error: %s",
                                 tcpcli->FetchHost(), tcpcli->FetchPort(), 
								 tcpcli->FetchError());
                        gui->WriteStatus(status);

                        // not any longer - clients are dynamically set in rset 
						// each time from the list of clients now
                        // Remove the client descriptor if we're not valid anymore
                        // FD_CLR(client_descrip, &read_set);

                        if (reconnect) {
                            snprintf(status, STATUS_MAX, 
									 "Will attempt to reconnect to %s:%d",
                                     tcpcli->FetchHost(), tcpcli->FetchPort());
                            gui->WriteStatus(status);
                        }
                    }

                    if (pollret != 0) {
                        if (pollret == CLIENT_ALERT)
                            if (sound == 1)
                                sound = PlaySound("alert");

                        if (strlen(tcpcli->FetchStatus()) != 0) {
                            gui->WriteStatus(tcpcli->FetchStatus());
                            // gui->DrawDisplay();
                        }

                        // The GPS only gets updated for the primary client
                        if (tcpcli == primary_client) {
                            if (tcpcli->FetchMode() == 0 && gpsmode != 0) {
                                if (sound == 1 && gpsmode != -1)
                                    sound = PlaySound("gpslost");
                                gpsmode = 0;
                            } else if (tcpcli->FetchMode() != 0 && gpsmode == 0) {
                                if (sound == 1 && gpsmode != -1)
                                    sound = PlaySound("gpslock");
                                gpsmode = 1;
                            }
                        }

                        if (tcpcli->FetchDeltaNumNetworks() > 0) {
                            wireless_network *newnet = tcpcli->FetchLastNewNetwork();

                            if (sound == 1 && newnet != lastspoken) {
                                if (newnet->crypt_set && 
                                    wav_map.find("new_wep") != wav_map.end())
                                    sound = PlaySound("new_wep");
                                else
                                    sound = PlaySound("new");
                            }

                            if (speech == 1 && newnet != lastspoken) {
                                string text;

                                if (newnet != NULL) {
                                    if (newnet->crypt_set)
                                        text = ExpandSpeechString(speech_sentence_encrypted, newnet, speech_encoding);
                                    else
                                        text = ExpandSpeechString(speech_sentence_unencrypted, newnet, speech_encoding);

                                    speech = SayText(text.c_str());
                                }
                            }

                            lastspoken = newnet;
                        }

                        num_networks += tcpcli->FetchNumNetworks();
                        num_packets += tcpcli->FetchNumPackets();
                        num_noise += tcpcli->FetchNumNoise();
                        num_dropped += tcpcli->FetchNumDropped();

                        if (tcpcli->FetchDeltaNumPackets() != 0) {
                            if (time(0) - last_click >= decay && sound == 1) {
                                if (tcpcli->FetchDeltaNumPackets() > tcpcli->FetchDeltaNumDropped()) {
                                    sound = PlaySound("traffic");
                                } else {
                                    sound = PlaySound("junktraffic");
                                }

                                last_click = time(0);
                            }
                        }
                    }
                }
            } else {
                // If we're supposed to try to reconnect and we're invalid, do so
                if (reconnect) {
                    if (tcpcli->Connect(guiport, guihost) >= 0) {
                        client_descrip = tcpcli->FetchDescriptor();
                        // FD_SET(client_descrip, &read_set);
                        if (client_descrip > max_fd)
                            max_fd = client_descrip;

                        snprintf(status, STATUS_MAX, "Reconnected to %s:%d.",
                                 tcpcli->FetchHost(), tcpcli->FetchPort());
                        gui->WriteStatus(status);
                    }
                }
            }
        }

        // Draw if it's time
        if (time(0) != last_draw) {
            // Let the UI do housekeeping
            gui->Tick();

            // Force a display event
            gui->DrawDisplay();
            last_draw = time(0);
        }
    }
}