forked from lucab/ntop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathntop_win32.c
2526 lines (2041 loc) · 60.7 KB
/
ntop_win32.c
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
/*
* Copyright (C) 1998-2012 Luca Deri <[email protected]>
*
* http://www.ntop.org/
*
* 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <stdio.h>
#include <string.h>
#include <winsock2.h>
#include <process.h>
#include <tchar.h>
#include <winioctl.h>
#include "ntddndis.h" // This defines the IOCTL constants.
#include "ntop.h"
extern char* intoa(struct in_addr addr);
extern char domainName[];
char *buildDate;
char _wdir[256];
u_char isNtopAservice;
/*
extern char* myGlobals.device;
extern int datalink;
extern unsigned int localnet, netmask;
*/
char* getNwBoardMacAddress(char *deviceName); /* forward */
ULONG GetHostIPAddr(); /* forward declaration */
#define NTOP_SERVICE_STOPPED 1
#define NTOP_SHUTDOWN 2
#define NTOP_CLOSE 3
#define NTOP_LOGOFF 4
/* ************************************************** */
short isWinNT() {
DWORD dwVersion;
DWORD dwWindowsMajorVersion;
dwVersion=GetVersion();
dwWindowsMajorVersion = (DWORD)(LOBYTE(LOWORD(dwVersion)));
if(!(dwVersion >= 0x80000000 && dwWindowsMajorVersion >= 4))
return 1;
else
return 0;
}
/* ************************************************** */
void initWinsock32() {
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD(2, 0);
err = WSAStartup( wVersionRequested, &wsaData );
if( err != 0 ) {
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
traceEvent(CONST_TRACE_FATALERROR, "Unable to initialise Winsock 2.x.");
exit(-1);
}
ntop_author = "Luca Deri <[email protected]>";
if(!isWinNT()) {
osName = "Win95/98/ME";
strcpy(_wdir, ".");
} else {
osName = "WinNT/2K/XP/Vista/Win7";
// Get the full path and filename of this program
if(GetModuleFileName( NULL, _wdir, sizeof(_wdir) ) == 0 ) {
_wdir[0] = '\0';
} else {
int i;
for(i=strlen(_wdir)-1; i>0; i--)
if(_wdir[i] == '\\') {
_wdir[i] = '\0';
break;
}
}
/* traceEvent(CONST_TRACE_ERROR, "Wdir=%s", _wdir); */
}
#ifdef WIN32
if(myGlobals.runningPref.pcapLogBasePath) free(myGlobals.runningPref.pcapLogBasePath); myGlobals.runningPref.pcapLogBasePath = strdup(_wdir);
if(myGlobals.dbPath) free(myGlobals.dbPath); myGlobals.dbPath = strdup(_wdir);
if(myGlobals.spoolPath) free(myGlobals.spoolPath); myGlobals.spoolPath = strdup(_wdir);
#endif
#ifdef WIN32_DEMO
traceEvent(CONST_TRACE_ALWAYSDISPLAY, "");
traceEvent(CONST_TRACE_ALWAYSDISPLAY, "-----------------------------------------------------------");
traceEvent(CONST_TRACE_ALWAYSDISPLAY, "WARNING: this application is a limited ntop version able to");
traceEvent(CONST_TRACE_ALWAYSDISPLAY, "capture up to %d packets. If you are interested", MAX_NUM_PACKETS);
traceEvent(CONST_TRACE_ALWAYSDISPLAY, "in the full version please have a look at the ntop");
traceEvent(CONST_TRACE_ALWAYSDISPLAY, "home page http://www.ntop.org/.");
traceEvent(CONST_TRACE_ALWAYSDISPLAY, "-----------------------------------------------------------");
traceEvent(CONST_TRACE_ALWAYSDISPLAY, "");
#endif
}
/* ************************************************** */
void termWinsock32() {
WSACleanup( );
//terminateSniffer();
}
/* ************************************************** */
ULONG GetHostIPAddr () {
char szLclHost [64];
LPHOSTENT lpstHostent;
SOCKADDR_IN stLclAddr;
SOCKADDR_IN stRmtAddr;
int nAddrSize = sizeof(SOCKADDR);
SOCKET hSock;
int nRet;
/* Init local address (to zero) */
stLclAddr.sin_addr.s_addr = INADDR_ANY;
/* Get the local hostname */
nRet = gethostname(szLclHost, sizeof(szLclHost));
if(nRet != SOCKET_ERROR) {
/* Resolve hostname for local address */
lpstHostent = gethostbyname((LPSTR)szLclHost);
if(lpstHostent) {
struct hostent *hp;
stLclAddr.sin_addr.s_addr = *((u_long FAR*) (lpstHostent->h_addr));
hp = (struct hostent*)gethostbyaddr((char*)&stLclAddr.sin_addr.s_addr, 4, AF_INET);
if(hp && (hp->h_name)) {
char *dotp = (char*)hp->h_name;
int i;
for(i=0; (dotp[i] != '\0') && (dotp[i] != '.'); i++)
;
if(dotp[i] == '.') strncpy(myGlobals.runningPref.domainName, &dotp[i+1], sizeof(myGlobals.runningPref.domainName));
}
}
}
/* If still not resolved, then try second strategy */
if(stLclAddr.sin_addr.s_addr == INADDR_ANY) {
/* Get a UDP socket */
hSock = socket(AF_INET, SOCK_DGRAM, 0);
if(hSock != INVALID_SOCKET) {
/* Connect to arbitrary port and address (NOT loopback) */
stRmtAddr.sin_family = AF_INET;
stRmtAddr.sin_port = htons(IPPORT_ECHO);
stRmtAddr.sin_addr.s_addr = inet_addr("128.127.50.1");
nRet = connect(hSock,
(LPSOCKADDR)&stRmtAddr,
sizeof(SOCKADDR));
if(nRet != SOCKET_ERROR)
/* Get local address */
getsockname(hSock,
(LPSOCKADDR)&stLclAddr,
(int FAR*)&nAddrSize);
closesocket(hSock); /* we're done with the socket */
}
}
/* Little/big endian crap... */
stLclAddr.sin_addr.s_addr = ntohl(stLclAddr.sin_addr.s_addr);
return (stLclAddr.sin_addr.s_addr);
}
/* **************************************
WIN32 MULTITHREAD STUFF
http://www-128.ibm.com/developerworks/eserver/articles/es-MigratingWin32toLinux.html
************************************** */
int createThread(pthread_t *threadId,
void *(*__start_routine) (void *), char* userParm) {
DWORD dwThreadId, dwThrdParam = 1;
(*threadId) = CreateThread(NULL, /* no security attributes */
0, /* use default stack size */
(LPTHREAD_START_ROUTINE)__start_routine, /* thread function */
userParm, /* argument to thread function */
0, /* use default creation flags */
&dwThreadId); /* returns the thread identifier */
if(*threadId != NULL)
return(1);
else
return(0);
}
/* ************************************ */
int _killThread(pthread_t *threadId) {
CloseHandle((HANDLE)*threadId);
return(0);
}
/* ************************************ */
int _joinThread(pthread_t *threadId) {
WaitForSingleObject((HANDLE)*threadId, INFINITE);
return(0);
}
/* ************************************ */
int _createMutex(PthreadMutex *mutexId, char* fileName, int fileLine) {
memset(mutexId, 0, sizeof(PthreadMutex));
mutexId->mutex = CreateMutex(NULL, FALSE, NULL);
mutexId->isInitialized = 1;
#ifdef DEBUG
if (fileName)
traceEvent(CONST_TRACE_INFO,
"DEBUG: createMutex() call with %x mutex [%s:%d]", mutexId,
fileName, fileLine);
#endif
return(1);
}
/* ************************************ */
void _deleteMutex(PthreadMutex *mutexId, char* fileName, int fileLine) {
#ifdef DEBUG
if (fileName)
traceEvent(CONST_TRACE_INFO,
"DEBUG: deleteMutex() call with %x(%c,%x) mutex [%s:%d]",
mutexId, (mutexId && mutexId->isInitialized) ? 'i' : '-',
mutexId ? mutexId->mutex : 0, fileName, fileLine);
#endif
if(!mutexId->isInitialized) {
traceEvent(CONST_TRACE_WARNING,
"deleteMutex() call with a NULL mutex [%s:%d]",
fileName, fileLine);
return;
}
ReleaseMutex(mutexId->mutex);
CloseHandle(mutexId->mutex);
memset(mutexId, 0, sizeof(PthreadMutex));
}
/* ************************************ */
int _accessMutex(PthreadMutex *mutexId, char* where,
char* fileName, int fileLine) {
#ifdef DEBUG
traceEvent(CONST_TRACE_INFO, "Locking 0x%X @ %s [%s:%d]",
mutexId->mutex, where, fileName, fileLine);
#endif
WaitForSingleObject(mutexId->mutex, INFINITE);
#ifdef MUTEX_DEBUG
mutexId->numLocks++;
mutexId->isLocked = 1;
if(!myGlobals.runningPref.disableMutexExtraInfo) {
memcpy(&(mutexId->lock), &(mutexId->attempt), sizeof(Holder));
memset(&(mutexId->attempt), 0, sizeof(Holder));
}
#endif
return(0);
}
/* ************************************ */
int _tryLockMutex(PthreadMutex *mutexId, char* where,
char* fileName, int fileLine) {
int rc;
#ifdef DEBUG
traceEvent(CONST_TRACE_INFO, "Try to Lock 0x%X @ %s [%s:%d]",
mutexId->mutex, where, fileName, fileLine);
fflush(stdout);
#endif
rc = WaitForSingleObject(mutexId->mutex, 0);
/* traceEvent(CONST_TRACE_INFO, "_tryLockMutex=%d", rc); */
if(rc != WAIT_OBJECT_0 /* OK */)
return(1);
else {
#ifdef MUTEX_DEBUG
mutexId->numLocks++;
mutexId->isLocked = 1;
if(!myGlobals.runningPref.disableMutexExtraInfo) {
memcpy(&(mutexId->lock), &(mutexId->attempt), sizeof(Holder));
memset(&(mutexId->attempt), 0, sizeof(Holder));
}
#endif
return(0);
}
}
/* ************************************ */
int _releaseMutex(PthreadMutex *mutexId, char* fileName, int fileLine) {
time_t lockDuration;
BOOL rc;
#ifdef DEBUG
traceEvent(CONST_TRACE_INFO, "Unlocking 0x%X [%s:%d]",
mutexId->mutex, fileName, fileLine);
#endif
rc = ReleaseMutex(mutexId->mutex);
#ifdef MUTEX_DEBUG
if((rc == 0) && (fileName)) {
traceEvent(CONST_TRACE_WARNING, "Unlock failed for 0x%X [%s:%d] (LastError=%d)",
mutexId->mutex, fileName, fileLine, GetLastError());
}
mutexId->isLocked = 0;
mutexId->numReleases++;
if(!myGlobals.runningPref.disableMutexExtraInfo) {
setHolder(mutexId->unlock);
lockDuration = timeval_subtract(mutexId->unlock.time, mutexId->lock.time);
if((mutexId->maxLockedDuration < lockDuration)
|| (mutexId->max.line == 0 /* Never set */)) {
memcpy(&(mutexId->max), &(mutexId->lock), sizeof(Holder));
mutexId->maxLockedDuration = lockDuration;
}
}
#endif
#ifdef DEBUG
traceEvent(CONST_TRACE_INFO, "DEBUG: semaphore 0x%X [%s:%d] locked for %d secs",
&(mutexId->mutex), fileName, fileLine,
mutexId->maxLockedDuration);
#endif
return(0);
}
/* ************************************ */
int createCondvar(ConditionalVariable *condvarId) {
condvarId->condVar = CreateEvent(NULL, /* no security */
TRUE , /* auto-reset event (FALSE = single event, TRUE = broadcast) */
FALSE, /* non-signaled initially */
NULL); /* unnamed */
InitializeCriticalSection(&condvarId->criticalSection);
return(1);
}
/* ************************************ */
void deleteCondvar(ConditionalVariable *condvarId) {
CloseHandle(condvarId->condVar);
DeleteCriticalSection(&condvarId->criticalSection);
}
/* ************************************ */
int waitCondvar(ConditionalVariable *condvarId) {
int rc;
#ifdef DEBUG
traceEvent(CONST_TRACE_INFO, "Wait (%x)...", condvarId->condVar);
#endif
EnterCriticalSection(&condvarId->criticalSection);
rc = WaitForSingleObject(condvarId->condVar, INFINITE);
LeaveCriticalSection(&condvarId->criticalSection);
#ifdef DEBUG
traceEvent(CONST_TRACE_INFO, "Got signal (%d)...", rc);
#endif
return(rc);
}
/* ************************************ */
int signalCondvar(ConditionalVariable *condvarId, u_int8_t broadcast) {
#ifdef DEBUG
traceEvent(CONST_TRACE_INFO, "Signaling (%x)...", condvarId->condVar);
#endif
/* FIX: handle "u_int8_t broadcast" */
return((int)PulseEvent(condvarId->condVar));
}
/* ************************************ */
void printAvailableInterfaces() {
char ebuf[CONST_SIZE_PCAP_ERR_BUF];
int i, numInterfaces = 0;
pcap_if_t *devpointer;
ebuf[0] = '\0';
printf("\nAvailable interfaces (-i <interface index>):\n");
if(pcap_findalldevs(&devpointer, ebuf) < 0) {
;
} else {
for (i = 0; devpointer != 0; i++) {
if(validInterface(devpointer->description)) {
printf(" [index=%d] %s\n (%s)\n",
numInterfaces++, devpointer->description, devpointer->name);
}
devpointer = devpointer->next;
} /* for */
} /* else */
if(numInterfaces == 0) {
traceEvent(CONST_TRACE_WARNING, "No interfaces available! This application cannot work");
traceEvent(CONST_TRACE_WARNING, " Make sure that winpcap is installed properly");
traceEvent(CONST_TRACE_WARNING, " and that you have network interfaces installed.");
}
}
/* ************************************ */
#define CONST_WIN32_PATH_NETWORKS "networks"
#define MAX_WIN32_NET_ALIASES 35
static FILE *netf = NULL;
static char line[BUFSIZ+1];
static struct netent net;
static char *net_aliases[MAX_WIN32_NET_ALIASES];
static char *any(char *, char *);
int _net_stayopen;
/* ************************************ */
static char *any(char *cp, char *match) {
register char *mp, c;
while (c = *cp) {
for (mp = match; *mp; mp++)
if(*mp == c)
return (cp);
cp++;
}
return ((char *)0);
}
/* ************************************ */
u_int32_t inet_network(const char *cp) {
register u_long val, base, n;
register char c;
u_long parts[4], *pp = parts;
register int i;
again:
/*
* Collect number up to ``.''.
* Values are specified as for C:
* 0x=hex, 0=octal, other=decimal.
*/
val = 0; base = 10;
/*
* The 4.4BSD myGlobals.version of this file also accepts 'x__' as a hexa
* number. I don't think this is correct. -- Uli
*/
if(*cp == '0') {
if(*++cp == 'x' || *cp == 'X')
base = 16, cp++;
else
base = 8;
}
while ((c = *cp)) {
if(isdigit(c)) {
val = (val * base) + (c - '0');
cp++;
continue;
}
if(base == 16 && isxdigit(c)) {
val = (val << 4) + (c + 10 - (islower(c) ? 'a' : 'A'));
cp++;
continue;
}
break;
}
if(*cp == '.') {
if(pp >= parts + 4)
return (INADDR_NONE);
*pp++ = val, cp++;
goto again;
}
if(*cp && !isspace(*cp))
return (INADDR_NONE);
*pp++ = val;
n = pp - parts;
if(n > 4)
return (INADDR_NONE);
for (val = 0, i = 0; i < (int)n; i++) {
val <<= 8;
val |= parts[i] & 0xff;
}
return (val);
}
/* ************************************ */
#if 0
struct netent* getnetent() {
char *p;
register char *cp, **q;
if(netf == NULL && (netf = fopen(NETDB, "r" )) == NULL)
return (NULL);
again:
p = fgets(line, BUFSIZ, netf);
if(p == NULL)
return (NULL);
if(*p == '#')
goto again;
cp = any(p, "#\n");
if(cp == NULL)
goto again;
*cp = '\0';
net.n_name = p;
cp = any(p, " \t");
if(cp == NULL)
goto again;
*cp++ = '\0';
while (*cp == ' ' || *cp == '\t')
cp++;
p = any(cp, " \t");
if(p != NULL)
*p++ = '\0';
net.n_net = inet_network(cp);
net.n_addrtype = AF_INET;
q = net.n_aliases = net_aliases;
if(p != NULL)
cp = p;
while (cp && *cp) {
if(*cp == ' ' || *cp == '\t') {
cp++;
continue;
}
if(q < &net_aliases[MAX_WIN32_NET_ALIASES - 1])
*q++ = cp;
cp = any(cp, " \t");
if(cp != NULL)
*cp++ = '\0';
}
*q = NULL;
return (&net);
}
/* ************************************ */
struct netent *getnetbyname(const char *name) {
register struct netent *p;
register char **cp;
setnetent(_net_stayopen);
while (p = getnetent()) {
if(strcmp(p->n_name, name) == 0)
break;
for (cp = p->n_aliases; *cp != 0; cp++)
if(strcmp(*cp, name) == 0)
goto found;
}
found:
if(!_net_stayopen)
endnetent();
return (p);
}
#endif
/* ************************************ */
/* Find the first bit set in I. */
int ffs (int i) {
static const unsigned char table[] =
{
0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8
};
unsigned long int a;
unsigned long int x = i & -i;
a = x <= 0xffff ? (x <= 0xff ? 0 : 8) : (x <= 0xffffff ? 16 : 24);
return table[x >> a] + a;
}
/* ****************************************************** */
int gettimeofday(struct timeval *tv,
#if defined(WIN32) && defined(__GNUC__)
/*
on mingw, struct timezone isn't defined so s/struct timezone/void/
Scott Renfro <[email protected]>
*/
void *notUsed
#else
struct timezone *notUsed
#endif
) {
tv->tv_sec = time(NULL);
tv->tv_usec = 0;
return(0);
}
/* ****************************************************** */
/* Courtesy of Wies-Software <[email protected]> */
unsigned long waitForNextEvent(unsigned long ulDelay /* ms */) {
unsigned long ulSlice = 1000L; /* 1 Second */
while ((myGlobals.ntopRunState < FLAG_NTOPSTATE_SHUTDOWN) && (ulDelay > 0L)) {
if (ulDelay < ulSlice)
ulSlice = ulDelay;
Sleep(ulSlice);
ulDelay -= ulSlice;
}
return ulDelay;
}
/* ************************************************************* */
/* Code borrowed from http://www.cvsnt.org/ */
#define DEF_INPMODE (ENABLE_LINE_INPUT|ENABLE_ECHO_INPUT|ENABLE_PROCESSED_INPUT)
#define HID_INPMODE (ENABLE_LINE_INPUT|ENABLE_PROCESSED_INPUT)
char* getpass(const char *prompt) {
static char pwd_buf[128];
size_t i;
DWORD br;
HANDLE hInput;
DWORD dwMode;
if(isWinNT()) {
return("admin"); // Default password
}
hInput=GetStdHandle(STD_INPUT_HANDLE);
fputs(prompt, stderr);
fflush(stderr);
fflush(stdout);
FlushConsoleInputBuffer(hInput);
GetConsoleMode(hInput,&dwMode);
SetConsoleMode(hInput, ENABLE_PROCESSED_INPUT);
for(i = 0; i < sizeof (pwd_buf) - 1; ++i) {
ReadFile(GetStdHandle(STD_INPUT_HANDLE),pwd_buf+i,1,&br,NULL);
if (pwd_buf[i] == '\r')
break;
fputc('*',stdout);
fflush (stderr);
fflush (stdout);
}
SetConsoleMode(hInput,dwMode);
pwd_buf[i] = '\0';
fputs ("\n", stderr);
return pwd_buf;
}
/* *************************************************************
Windown NT/2K Service Registration Routines
Copyright 2001 by Bill Giel/KC Multimedia and Design Group, Inc.
************************************************************* */
#ifdef __cplusplus
extern "C" {
#endif
//
// FUNCTION: convertArgStringToArgList()
//
// PURPOSE: Return an array of strings containing all arguments that
// are parsed from a tab-delimited argument string.
//
// PARAMETERS:
// args - The string array address to be allocated and receive the data
// len - pointer to an int that will contain the returned array length
// argstring - string containing arguments to be parsed.
//
// RETURN VALUE:
// String array address containing the filtered arguments
// NULL on failure
//
LPTSTR* convertArgStringToArgList(LPTSTR *args, PDWORD pdwLen, LPTSTR lpszArgstring);
//
// FUNCTION: convertArgListToArgString()
//
// PURPOSE: Create a single tab-delimited string of arguments from
// an argument list
//
// PARAMETERS:
// target - pointer to the string to be allocated and created
// start - zero-based offest into the list to the first arg value used to
// build the list.
// argc - length of the argument list
// argv - array of strings, the argument list.
//
// RETURN VALUE:
// Character pointer to the target string.
// NULL on failure
//
LPTSTR convertArgListToArgString(LPTSTR lpszTarget, DWORD dwStart, DWORD dwArgc, LPTSTR *lpszArgv);
#ifdef __cplusplus
}
#endif
#ifdef __cplusplus
extern "C" {
#endif
//
// FUNCTION: getStringValue()
//
// PURPOSE: Fetches a REG_SZ or REG_EXPAND_SZ string value
// from a specified registry key
//
// PARAMETERS:
// lpVal - a string buffer for the desired value
// lpcbLen - pointer to LONG value with buffer length
// hkRoot - the primary root key, e.g. HKEY_LOCAL_MACHINE
// lpszPath - the registry path to the subkey containing th desired value
// lpszValue - the name of the desired value
//
// RETURN VALUE:
// 0 on success, 1 on failure
//
int getStringValue(LPBYTE lpVal, LPDWORD lpcbLen, HKEY hkRoot, LPCTSTR lpszPath, LPTSTR lpszValue);
//
// FUNCTION: setStringValue()
//
// PURPOSE: Assigns a REG_SZ value to a
// specified registry key
//
// PARAMETERS:
// lpVal - Constant byte array containing the value
// cbLen - data length
// hkRoot - the primary root key, e.g. HKEY_LOCAL_MACHINE
// lpszPath - the registry path to the subkey containing th desired value
// lpszValue - the name of the desired value
//
// RETURN VALUE:
// 0 on success, 1 on failure
//
int setStringValue(CONST BYTE *lpVal, DWORD cbLen, HKEY hkRoot, LPCTSTR lpszPath, LPCTSTR lpszValue);
//
// FUNCTION: makeNewKey()
//
// PURPOSE: Creates a new key at the specified path
//
// PARAMETERS:
// hkRoot - the primary root key, e.g. HKEY_LOCAL_MACHINE
// lpszPath - the registry path to the new subkey
//
// RETURN VALUE:
// 0 on success, 1 on failure
//
int makeNewKey(HKEY hkRoot, LPCTSTR lpszPath);
int setDwordValue(DWORD data, HKEY hkRoot, LPCTSTR lpszPath, LPCTSTR lpszValue);
#ifdef __cplusplus
}
#endif
#ifdef __cplusplus
extern "C" {
#endif
// =========================================================
// TO DO: change as needed for specific Java app and service
// =========================================================
// internal name of the service
#define SZSERVICENAME "ntop"
// displayed name of the service
#define SZSERVICEDISPLAYNAME "ntop for Win32"
// Service TYPE Permissable values:
// SERVICE_AUTO_START
// SERVICE_DEMAND_START
// SERVICE_DISABLED
#define SERVICESTARTTYPE SERVICE_AUTO_START
// =========================================================
// You should not need any changes below this line
// =========================================================
// Value name for app parameters
#define SZAPPPARAMS "AppParameters"
// list of service dependencies - "dep1\0dep2\0\0"
// If none, use ""
#define SZDEPENDENCIES ""
//
// FUNCTION: getConsoleMode()
//
// PURPOSE: Is the app running as a service or a console app.
//
// RETURN VALUE:
// TRUE - if running as a console application
// FALSE - if running as a service
//
BOOL getConsoleMode();
//
// FUNCTION: ReportStatusToSCMgr()
//
// PURPOSE: Sets the current status of the service and
// reports it to the Service Control Manager
//
// PARAMETERS:
// dwCurrentState - the state of the service
// dwWin32ExitCode - error code to report
// dwWaitHint - worst case estimate to next checkpoint
//
// RETURN VALUE:
// TRUE - success
// FALSE - failure
//
BOOL ReportStatus(DWORD dwCurrentState, DWORD dwWin32ExitCode, DWORD dwWaitHint);
//
// FUNCTION: AddToMessageLog(LPTSTR lpszMsg)
//
// PURPOSE: Allows any thread to log an error message
//
// PARAMETERS:
// lpszMsg - text for message
//
// RETURN VALUE:
// none
//
void AddToMessageLog(LPTSTR lpszMsg);
VOID ServiceStart(DWORD dwArgc, LPTSTR *lpszArgv);
VOID ServiceStop();
#ifdef __cplusplus
}
#endif
//
// Values are 32 bit values layed out as follows:
//
// 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1
// 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
// +---+-+-+-----------------------+-------------------------------+
// |Sev|C|R| Facility | Code |
// +---+-+-+-----------------------+-------------------------------+
//
// where
//
// Sev - is the severity code
//
// 00 - Success
// 01 - Informational
// 10 - Warning
// 11 - Error
//
// C - is the Customer code flag
//
// R - is a reserved bit
//
// Facility - is the facility code
//
// Code - is the facility's status code
//
//
// Define the facility codes
//
//
// Define the severity codes
//
//
// MessageId: EVENT_GENERIC_INFORMATION
//
// MessageText:
//
// %1
//
#define EVENT_GENERIC_INFORMATION 0x40000001L
//global variables
SERVICE_STATUS ssStatus;
SERVICE_STATUS_HANDLE sshStatusHandle;
DWORD dwErr = 0;
BOOL bConsole = FALSE;
TCHAR szErr[256];
#define SZFAILURE "StartServiceControlDispatcher failed!"
#define SZSCMGRFAILURE "OpenSCManager failed - %s\n"
int getStringValue(LPBYTE lpVal, LPDWORD lpcbLen, HKEY hkRoot, LPCTSTR lpszPath, LPTSTR lpszValue)
{
LONG result;
HKEY hKey;
DWORD dwType;
result = RegOpenKeyEx(
hkRoot,
lpszPath,
(DWORD)0,
KEY_EXECUTE | KEY_QUERY_VALUE,
(PHKEY)&hKey);
if(result != ERROR_SUCCESS){
return 1;
}
result = RegQueryValueEx(
hKey,
lpszValue,
NULL,
(LPDWORD)&dwType,
lpVal,
lpcbLen);