~ubuntu-branches/ubuntu/maverick/ntop/maverick

« back to all changes in this revision

Viewing changes to ntop/ntop_win32.c

  • Committer: Bazaar Package Importer
  • Author(s): Dennis Schoen
  • Date: 2002-04-12 11:38:47 UTC
  • Revision ID: james.westby@ubuntu.com-20020412113847-4k4yydw0pzybc6g8
Tags: upstream-2.0.0
ImportĀ upstreamĀ versionĀ 2.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  Copyright (C) 1998-2001 Luca Deri <deri@ntop.org>
 
3
 *                          Portions by Stefano Suin <stefano@ntop.org>
 
4
 *
 
5
 *                          http://www.ntop.org/
 
6
 *
 
7
 *  This program is free software; you can redistribute it and/or modify
 
8
 *  it under the terms of the GNU General Public License as published by
 
9
 *  the Free Software Foundation; either version 2 of the License, or
 
10
 *  (at your option) any later version.
 
11
 *
 
12
 *  This program is distributed in the hope that it will be useful,
 
13
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
 *  GNU General Public License for more details.
 
16
 *
 
17
 *  You should have received a copy of the GNU General Public License
 
18
 *  along with this program; if not, write to the Free Software
 
19
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
20
 */
 
21
 
 
22
#include <stdio.h>
 
23
#include <string.h>
 
24
#include <winsock2.h>
 
25
 
 
26
#include <winioctl.h>
 
27
#include "ntddndis.h"        // This defines the IOCTL constants.
 
28
 
 
29
#include "ntop.h"
 
30
 
 
31
extern char* intoa(struct in_addr addr);
 
32
 
 
33
extern char domainName[];
 
34
char *buildDate;
 
35
 
 
36
/*
 
37
extern char* device;
 
38
extern int datalink;
 
39
extern unsigned int localnet, netmask;
 
40
*/
 
41
 
 
42
char* getNwBoardMacAddress(char *deviceName); /* forward */
 
43
 
 
44
ULONG GetHostIPAddr(); /* forward declaration */
 
45
 
 
46
 
 
47
#ifdef ORIGINAL_NTOP
 
48
 
 
49
TCHAR          AdapterName[64];
 
50
FRAMEETH       ethernetFrame;
 
51
ULONG          NameLength=64, FrameLength;
 
52
LPADAPTER      Adapter;
 
53
LPPACKET       Packet;
 
54
 
 
55
/* ************************************************** */
 
56
 
 
57
typedef PVOID NDIS_HANDLE, *PNDIS_HANDLE;
 
58
 
 
59
#if 0
 
60
void initSniffer() {
 
61
  /* device = "eth"; */
 
62
  datalink = DLT_EN10MB;
 
63
 
 
64
        /* ****************** */
 
65
 
 
66
  PacketGetAdapterNames(AdapterName, &NameLength);
 
67
 
 
68
  Adapter = PacketOpenAdapter(AdapterName);
 
69
  if(Adapter == NULL) {
 
70
    traceEvent(TRACE_ERROR, "FATAL ERROR: please install MS NDIS 3.0 driver. Bye...");
 
71
    exit(-1);
 
72
  }
 
73
  PacketSetFilter(Adapter, NDIS_PACKET_TYPE_PROMISCUOUS);
 
74
  Packet = PacketAllocatePacket();
 
75
  PacketInitPacket(Packet, (PVOID)(&ethernetFrame), sizeof(FRAMEETH));
 
76
}
 
77
#endif
 
78
 
 
79
/* ******************************************** */
 
80
 
 
81
void terminateSniffer() {
 
82
  PacketCloseAdapter(Adapter);
 
83
}
 
84
 
 
85
/* ********************************** */
 
86
 
 
87
void sniffSinglePacket(void(*pbuf_process)(u_char *unused,
 
88
                                           const struct pcap_pkthdr *h,
 
89
                                           const u_char *p))
 
90
{
 
91
  struct pcap_pkthdr hdr;
 
92
  static int numPkts = 0;
 
93
 
 
94
  PacketReceivePacket(Adapter, Packet, TRUE, &FrameLength);
 
95
  hdr.caplen = (u_int32)FrameLength;
 
96
  hdr.len    = (u_int32)FrameLength;
 
97
 
 
98
#ifdef WIN32_DEMO
 
99
  if(numPkts < MAX_NUM_PACKETS)
 
100
#endif
 
101
    pbuf_process(NULL, &hdr, (u_char*)&ethernetFrame);
 
102
 
 
103
  numPkts++;
 
104
}
 
105
 
 
106
#endif /* ORIGINAL_NTOP */
 
107
 
 
108
/* ************************************************** */
 
109
 
 
110
short isWinNT() {
 
111
  DWORD dwVersion;
 
112
  DWORD dwWindowsMajorVersion;
 
113
 
 
114
  dwVersion=GetVersion();
 
115
  dwWindowsMajorVersion =  (DWORD)(LOBYTE(LOWORD(dwVersion)));
 
116
  if(!(dwVersion >= 0x80000000 && dwWindowsMajorVersion >= 4))
 
117
    return 1;
 
118
  else
 
119
    return 0;
 
120
}
 
121
 
 
122
/* ************************************************** */
 
123
 
 
124
void initWinsock32() {
 
125
  WORD wVersionRequested;
 
126
  WSADATA wsaData;
 
127
  int err;
 
128
 
 
129
  wVersionRequested = MAKEWORD(2, 0);
 
130
  err = WSAStartup( wVersionRequested, &wsaData );
 
131
  if( err != 0 ) {
 
132
    /* Tell the user that we could not find a usable */
 
133
    /* WinSock DLL.                                  */
 
134
    traceEvent(TRACE_ERROR, "FATAL ERROR: unable to initialise Winsock 2.x.");
 
135
    exit(-1);
 
136
  }
 
137
 
 
138
  version = "2.0";
 
139
  author  = "Luca Deri <deri@ntop.org>";
 
140
  buildDate = "27/12/2001";
 
141
 
 
142
  if(isWinNT())
 
143
    osName = "WinNT/2K/XP";
 
144
  else
 
145
    osName = "Win95/98/ME";
 
146
 
 
147
#ifdef WIN32_DEMO
 
148
  traceEvent(TRACE_INFO, "\n-----------------------------------------------------------");
 
149
  traceEvent(TRACE_INFO, "WARNING: this application is a limited ntop version able to");
 
150
  traceEvent(TRACE_INFO, "capture up to %d packets. If you are interested", MAX_NUM_PACKETS);
 
151
  traceEvent(TRACE_INFO, "in the full version please have a look at the ntop");
 
152
  traceEvent(TRACE_INFO, "home page http://www.ntop.org/.");
 
153
  traceEvent(TRACE_INFO, "-----------------------------------------------------------\n");
 
154
#endif
 
155
}
 
156
 
 
157
/* ************************************************** */
 
158
 
 
159
void termWinsock32() {
 
160
  WSACleanup( );
 
161
  //terminateSniffer();
 
162
}
 
163
/* ************************************************** */
 
164
 
 
165
 
 
166
ULONG GetHostIPAddr () {
 
167
  char szLclHost [64];
 
168
  LPHOSTENT lpstHostent;
 
169
  SOCKADDR_IN stLclAddr;
 
170
  SOCKADDR_IN stRmtAddr;
 
171
  int nAddrSize = sizeof(SOCKADDR);
 
172
  SOCKET hSock;
 
173
  int nRet;
 
174
 
 
175
  /* Init local address (to zero) */
 
176
  stLclAddr.sin_addr.s_addr = INADDR_ANY;
 
177
 
 
178
  /* Get the local hostname */
 
179
  nRet = gethostname(szLclHost, 64);
 
180
  if(nRet != SOCKET_ERROR) {
 
181
    /* Resolve hostname for local address */
 
182
    lpstHostent = gethostbyname((LPSTR)szLclHost);
 
183
    if(lpstHostent) {
 
184
      struct hostent *hp;
 
185
 
 
186
      stLclAddr.sin_addr.s_addr = *((u_long FAR*) (lpstHostent->h_addr));
 
187
 
 
188
      hp = (struct hostent*)gethostbyaddr((char*)&stLclAddr.sin_addr.s_addr, 4, AF_INET);
 
189
 
 
190
      if(hp && (hp->h_name)) {
 
191
        char *dotp = (char*)hp->h_name;
 
192
        int i;
 
193
 
 
194
        for(i=0; (dotp[i] != '\0') && (dotp[i] != '.'); i++)
 
195
          ;
 
196
 
 
197
        if(dotp[i] == '.') strncpy(domainName, &dotp[i+1], sizeof(domainName));
 
198
      }
 
199
    }
 
200
  }
 
201
 
 
202
  /* If still not resolved, then try second strategy */
 
203
  if(stLclAddr.sin_addr.s_addr == INADDR_ANY) {
 
204
    /* Get a UDP socket */
 
205
    hSock = socket(AF_INET, SOCK_DGRAM, 0);
 
206
    if(hSock != INVALID_SOCKET)  {
 
207
      /* Connect to arbitrary port and address (NOT loopback) */
 
208
      stRmtAddr.sin_family = AF_INET;
 
209
      stRmtAddr.sin_port   = htons(IPPORT_ECHO);
 
210
      stRmtAddr.sin_addr.s_addr = inet_addr("128.127.50.1");
 
211
      nRet = connect(hSock,
 
212
                     (LPSOCKADDR)&stRmtAddr,
 
213
                     sizeof(SOCKADDR));
 
214
      if(nRet != SOCKET_ERROR)
 
215
        /* Get local address */
 
216
        getsockname(hSock,
 
217
                    (LPSOCKADDR)&stLclAddr,
 
218
                    (int FAR*)&nAddrSize);
 
219
 
 
220
      closesocket(hSock);   /* we're done with the socket */
 
221
    }
 
222
  }
 
223
 
 
224
  /* Little/big endian crap... */
 
225
  stLclAddr.sin_addr.s_addr = ntohl(stLclAddr.sin_addr.s_addr);
 
226
 
 
227
  return (stLclAddr.sin_addr.s_addr);
 
228
}
 
229
 
 
230
/* **************************************
 
231
 
 
232
               WIN32 MULTITHREAD STUFF
 
233
 
 
234
   ************************************** */
 
235
 
 
236
int createThread(pthread_t *threadId,
 
237
                 void *(*__start_routine) (void *), char* userParm) {
 
238
  DWORD dwThreadId, dwThrdParam = 1;
 
239
 
 
240
  (*threadId) = CreateThread(NULL, /* no security attributes */
 
241
                             0,            /* use default stack size */
 
242
                             (LPTHREAD_START_ROUTINE)__start_routine, /* thread function */
 
243
                             userParm,     /* argument to thread function */
 
244
                             0,            /* use default creation flags */
 
245
                             &dwThreadId); /* returns the thread identifier */
 
246
 
 
247
  if(*threadId != NULL)
 
248
    return(1);
 
249
  else
 
250
    return(0);
 
251
}
 
252
 
 
253
/* ************************************ */
 
254
 
 
255
void killThread(pthread_t *threadId) {
 
256
  CloseHandle((HANDLE)*threadId);
 
257
}
 
258
 
 
259
/* ************************************ */
 
260
 
 
261
int _createMutex(PthreadMutex *mutexId, char* fileName, int fileLine) {
 
262
 
 
263
  memset(mutexId, 0, sizeof(PthreadMutex));
 
264
 
 
265
  mutexId->mutex = CreateMutex(NULL, FALSE, NULL);
 
266
  mutexId->isInitialized = 1;
 
267
 
 
268
#ifdef DEBUG
 
269
  if (fileName)
 
270
    traceEvent(TRACE_INFO,
 
271
               "INFO: createMutex() call with %x mutex [%s:%d]", mutexId,
 
272
               fileName, fileLine);
 
273
#endif
 
274
 
 
275
  return(1);
 
276
}
 
277
 
 
278
/* ************************************ */
 
279
 
 
280
void _deleteMutex(PthreadMutex *mutexId, char* fileName, int fileLine) {
 
281
 
 
282
#ifdef DEBUG
 
283
  if (fileName)
 
284
    traceEvent(TRACE_INFO,
 
285
               "INFO: deleteMutex() call with %x(%c,%x) mutex [%s:%d]",
 
286
               mutexId, (mutexId && mutexId->isInitialized) ? 'i' : '-',
 
287
               mutexId ? mutexId->mutex : 0, fileName, fileLine);
 
288
#endif
 
289
  
 
290
  if(!mutexId->isInitialized) {
 
291
    traceEvent(TRACE_ERROR, 
 
292
               "ERROR: deleteMutex() call with a NULL mutex [%s:%d]",
 
293
               fileName, fileLine);
 
294
    return;
 
295
  }
 
296
  
 
297
  ReleaseMutex(mutexId->mutex);
 
298
  CloseHandle(mutexId->mutex);
 
299
 
 
300
  memset(mutexId, 0, sizeof(PthreadMutex));
 
301
}
 
302
 
 
303
/* ************************************ */
 
304
 
 
305
int _accessMutex(PthreadMutex *mutexId, char* where,
 
306
                 char* fileName, int fileLine) {
 
307
#ifdef DEBUG
 
308
  traceEvent(TRACE_INFO, "Locking 0x%X @ %s [%s:%d]",
 
309
             mutexId->mutex, where, fileName, fileLine);
 
310
#endif
 
311
 
 
312
  WaitForSingleObject(mutexId->mutex, INFINITE);
 
313
 
 
314
  mutexId->numLocks++;
 
315
  mutexId->isLocked = 1;
 
316
  mutexId->lockTime = time(NULL);
 
317
 
 
318
  if(fileName != NULL) {
 
319
    strcpy(mutexId->lockFile, fileName);
 
320
    mutexId->lockLine = fileLine;
 
321
  }
 
322
 
 
323
  return(1);
 
324
}
 
325
 
 
326
/* ************************************ */
 
327
 
 
328
int _tryLockMutex(PthreadMutex *mutexId, char* where,
 
329
                  char* fileName, int fileLine) {
 
330
#ifdef DEBUG
 
331
  traceEvent(TRACE_INFO, "Try to Lock 0x%X @ %s [%s:%d]",
 
332
             mutexId->mutex, where, fileName, fileLine);
 
333
  fflush(stdout);
 
334
#endif
 
335
 
 
336
  if(WaitForSingleObject(mutexId->mutex, 0) == WAIT_FAILED)
 
337
    return(0);
 
338
  else {
 
339
    mutexId->numLocks++;
 
340
    mutexId->isLocked = 1;
 
341
    mutexId->lockTime = time(NULL);
 
342
 
 
343
    if(fileName != NULL) {
 
344
      strcpy(mutexId->lockFile, fileName);
 
345
      mutexId->lockLine = fileLine;
 
346
    }
 
347
 
 
348
    return(1);
 
349
  }
 
350
}
 
351
 
 
352
/* ************************************ */
 
353
 
 
354
int _releaseMutex(PthreadMutex *mutexId,
 
355
                  char* fileName, int fileLine) {
 
356
 
 
357
  time_t lockDuration;
 
358
  BOOL rc;
 
359
 
 
360
#ifdef DEBUG
 
361
  traceEvent(TRACE_INFO, "Unlocking 0x%X [%s:%d]",
 
362
             mutexId->mutex, fileName, fileLine);
 
363
#endif
 
364
  rc = ReleaseMutex(mutexId->mutex);
 
365
 
 
366
  if((rc == 0) && (fileName)) {
 
367
    traceEvent(TRACE_ERROR, "ERROR while unlocking 0x%X [%s:%d] (LastError=%d)",
 
368
               mutexId->mutex, fileName, fileLine, GetLastError());
 
369
  }
 
370
 
 
371
  lockDuration = time(NULL) - mutexId->lockTime;
 
372
 
 
373
  if((mutexId->maxLockedDuration < lockDuration)
 
374
     || (mutexId->maxLockedDurationUnlockLine == 0 /* Never set */)) {
 
375
    mutexId->maxLockedDuration = lockDuration;
 
376
 
 
377
    if(fileName != NULL) {
 
378
      strcpy(mutexId->maxLockedDurationUnlockFile, fileName);
 
379
      mutexId->maxLockedDurationUnlockLine = fileLine;
 
380
    }
 
381
 
 
382
#ifdef DEBUG
 
383
    traceEvent(TRACE_INFO, "INFO: semaphore 0x%X [%s:%d] locked for %d secs",
 
384
               &(mutexId->mutex), fileName, fileLine,
 
385
               mutexId->maxLockedDuration);
 
386
#endif
 
387
  }
 
388
 
 
389
  mutexId->isLocked = 0;
 
390
  mutexId->numReleases++;
 
391
  if(fileName != NULL) {
 
392
    strcpy(mutexId->unlockFile, fileName);
 
393
    mutexId->unlockLine = fileLine;
 
394
  }
 
395
 
 
396
  return(1);
 
397
}
 
398
 
 
399
/* ************************************ */
 
400
 
 
401
int createCondvar(ConditionalVariable *condvarId) {
 
402
  condvarId->condVar = CreateEvent(NULL,  /* no security */
 
403
                                   TRUE , /* auto-reset event (FALSE = single event, TRUE = broadcast) */
 
404
                                   FALSE, /* non-signaled initially */
 
405
                                   NULL); /* unnamed */
 
406
  InitializeCriticalSection(&condvarId->criticalSection);
 
407
  return(1);
 
408
}
 
409
 
 
410
/* ************************************ */
 
411
 
 
412
void deleteCondvar(ConditionalVariable *condvarId) {
 
413
  CloseHandle(condvarId->condVar);
 
414
  DeleteCriticalSection(&condvarId->criticalSection);
 
415
}
 
416
 
 
417
/* ************************************ */
 
418
 
 
419
int waitCondvar(ConditionalVariable *condvarId) {
 
420
  int rc;
 
421
#ifdef DEBUG
 
422
  traceEvent(TRACE_INFO, "Wait (%x)...", condvarId->condVar);
 
423
#endif
 
424
  EnterCriticalSection(&condvarId->criticalSection);
 
425
  rc = WaitForSingleObject(condvarId->condVar, INFINITE);
 
426
  LeaveCriticalSection(&condvarId->criticalSection);
 
427
 
 
428
#ifdef DEBUG
 
429
  traceEvent(TRACE_INFO, "Got signal (%d)...", rc);
 
430
#endif
 
431
 
 
432
  return(rc);
 
433
}
 
434
 
 
435
/* ************************************ */
 
436
 
 
437
int signalCondvar(ConditionalVariable *condvarId) {
 
438
#ifdef DEBUG
 
439
  traceEvent(TRACE_INFO, "Signaling (%x)...", condvarId->condVar);
 
440
#endif
 
441
  return((int)PulseEvent(condvarId->condVar));
 
442
}
 
443
 
 
444
/* ************************************ */
 
445
 
 
446
#if 0
 
447
void printAvailableInterfaces() {
 
448
  ULONG nameLength = 128;
 
449
  WCHAR adaptersName[128];
 
450
 
 
451
  PacketGetAdapterNames (adaptersName, &nameLength);
 
452
 
 
453
  if(isWinNT())
 
454
    {
 
455
      static char tmpString[128];
 
456
      int i, j;
 
457
 
 
458
      for(j=0, i=0; !((adaptersName[i] == 0) && (adaptersName[i+1] == 0)); i++) {
 
459
        if(adaptersName[i] != 0)
 
460
          tmpString[j++] = adaptersName[i];
 
461
      }
 
462
 
 
463
      tmpString[j++] = 0;
 
464
      memcpy(adaptersName, tmpString, 128);
 
465
    }
 
466
 
 
467
  traceEvent(TRACE_INFO, "Available interfaces:\n%s", (char*)adaptersName);
 
468
}
 
469
#endif
 
470
 
 
471
/* ************************************ */
 
472
 
 
473
#define _PATH_NETWORKS  "networks"
 
474
 
 
475
#define MAXALIASES      35
 
476
 
 
477
static char NETDB[] = _PATH_NETWORKS;
 
478
static FILE *netf = NULL;
 
479
static char line[BUFSIZ+1];
 
480
static struct netent net;
 
481
static char *net_aliases[MAXALIASES];
 
482
static char *any(char *, char *);
 
483
 
 
484
int _net_stayopen;
 
485
 
 
486
void
 
487
setnetent(f)
 
488
     int f;
 
489
{
 
490
  if(netf == NULL)
 
491
    netf = fopen(NETDB, "r" );
 
492
  else
 
493
    rewind(netf);
 
494
  _net_stayopen |= f;
 
495
}
 
496
 
 
497
void
 
498
endnetent()
 
499
{
 
500
  if(netf) {
 
501
    fclose(netf);
 
502
    netf = NULL;
 
503
  }
 
504
  _net_stayopen = 0;
 
505
}
 
506
 
 
507
static char *
 
508
any(cp, match)
 
509
     register char *cp;
 
510
     char *match;
 
511
{
 
512
  register char *mp, c;
 
513
 
 
514
  while (c = *cp) {
 
515
    for (mp = match; *mp; mp++)
 
516
      if(*mp == c)
 
517
        return (cp);
 
518
    cp++;
 
519
  }
 
520
  return ((char *)0);
 
521
}
 
522
 
 
523
u_int32_t
 
524
inet_network(const char *cp)
 
525
{
 
526
  register u_long val, base, n;
 
527
  register char c;
 
528
  u_long parts[4], *pp = parts;
 
529
  register int i;
 
530
 
 
531
 again:
 
532
  /*
 
533
   * Collect number up to ``.''.
 
534
   * Values are specified as for C:
 
535
   * 0x=hex, 0=octal, other=decimal.
 
536
   */
 
537
  val = 0; base = 10;
 
538
  /*
 
539
   * The 4.4BSD version of this file also accepts 'x__' as a hexa
 
540
   * number.  I don't think this is correct.  -- Uli
 
541
   */
 
542
  if(*cp == '0') {
 
543
    if(*++cp == 'x' || *cp == 'X')
 
544
      base = 16, cp++;
 
545
    else
 
546
      base = 8;
 
547
  }
 
548
  while ((c = *cp)) {
 
549
    if(isdigit(c)) {
 
550
      val = (val * base) + (c - '0');
 
551
      cp++;
 
552
      continue;
 
553
    }
 
554
    if(base == 16 && isxdigit(c)) {
 
555
      val = (val << 4) + (c + 10 - (islower(c) ? 'a' : 'A'));
 
556
      cp++;
 
557
      continue;
 
558
    }
 
559
    break;
 
560
  }
 
561
  if(*cp == '.') {
 
562
    if(pp >= parts + 4)
 
563
      return (INADDR_NONE);
 
564
    *pp++ = val, cp++;
 
565
    goto again;
 
566
  }
 
567
  if(*cp && !isspace(*cp))
 
568
    return (INADDR_NONE);
 
569
  *pp++ = val;
 
570
  n = pp - parts;
 
571
  if(n > 4)
 
572
    return (INADDR_NONE);
 
573
  for (val = 0, i = 0; i < (int)n; i++) {
 
574
    val <<= 8;
 
575
    val |= parts[i] & 0xff;
 
576
  }
 
577
  return (val);
 
578
}
 
579
 
 
580
struct netent *
 
581
getnetent()
 
582
{
 
583
  char *p;
 
584
  register char *cp, **q;
 
585
 
 
586
  if(netf == NULL && (netf = fopen(NETDB, "r" )) == NULL)
 
587
    return (NULL);
 
588
 again:
 
589
  p = fgets(line, BUFSIZ, netf);
 
590
  if(p == NULL)
 
591
    return (NULL);
 
592
  if(*p == '#')
 
593
    goto again;
 
594
  cp = any(p, "#\n");
 
595
  if(cp == NULL)
 
596
    goto again;
 
597
  *cp = '\0';
 
598
  net.n_name = p;
 
599
  cp = any(p, " \t");
 
600
  if(cp == NULL)
 
601
    goto again;
 
602
  *cp++ = '\0';
 
603
  while (*cp == ' ' || *cp == '\t')
 
604
    cp++;
 
605
  p = any(cp, " \t");
 
606
  if(p != NULL)
 
607
    *p++ = '\0';
 
608
  net.n_net = inet_network(cp);
 
609
  net.n_addrtype = AF_INET;
 
610
  q = net.n_aliases = net_aliases;
 
611
  if(p != NULL)
 
612
    cp = p;
 
613
  while (cp && *cp) {
 
614
    if(*cp == ' ' || *cp == '\t') {
 
615
      cp++;
 
616
      continue;
 
617
    }
 
618
    if(q < &net_aliases[MAXALIASES - 1])
 
619
      *q++ = cp;
 
620
    cp = any(cp, " \t");
 
621
    if(cp != NULL)
 
622
      *cp++ = '\0';
 
623
  }
 
624
  *q = NULL;
 
625
  return (&net);
 
626
}
 
627
 
 
628
struct netent *getnetbyname(const char *name)
 
629
{
 
630
  register struct netent *p;
 
631
  register char **cp;
 
632
 
 
633
  setnetent(_net_stayopen);
 
634
  while (p = getnetent()) {
 
635
    if(strcmp(p->n_name, name) == 0)
 
636
      break;
 
637
    for (cp = p->n_aliases; *cp != 0; cp++)
 
638
      if(strcmp(*cp, name) == 0)
 
639
        goto found;
 
640
  }
 
641
 found:
 
642
  if(!_net_stayopen)
 
643
    endnetent();
 
644
  return (p);
 
645
}
 
646
 
 
647
 
 
648
/* Find the first bit set in I.  */
 
649
int ffs (int i)
 
650
{
 
651
  static const unsigned char table[] =
 
652
  {
 
653
    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,
 
654
    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,
 
655
    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,
 
656
    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,
 
657
    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,
 
658
    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,
 
659
    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,
 
660
    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
 
661
  };
 
662
  unsigned long int a;
 
663
  unsigned long int x = i & -i;
 
664
 
 
665
  a = x <= 0xffff ? (x <= 0xff ? 0 : 8) : (x <= 0xffffff ?  16 : 24);
 
666
 
 
667
  return table[x >> a] + a;
 
668
}
 
669
 
 
670
/* ****************************************************** */
 
671
 
 
672
#if defined(WIN32) && defined(__GNUC__)
 
673
/* on mingw, struct timezone isn't defined so s/struct timezone/void/ - Scott Renfro <scott@renfro.org> */
 
674
int gettimeofday(struct timeval *tv, void *notUsed) {
 
675
#else
 
676
  int gettimeofday(struct timeval *tv, struct timezone *notUsed) {
 
677
#endif
 
678
    tv->tv_sec = time(NULL);
 
679
    tv->tv_usec = 0;
 
680
    return(0);
 
681
  }
 
682
 
 
683
  /* ****************************************************** */
 
684
 
 
685
/* Courtesy of Wies-Software <wies@wiessoft.de> */
 
686
  unsigned long waitForNextEvent(unsigned long ulDelay /* ms */) {
 
687
    unsigned long ulSlice = 1000L; // 1 Second
 
688
 
 
689
    while (capturePackets && (ulDelay > 0L)) {
 
690
      if (ulDelay < ulSlice)
 
691
        ulSlice = ulDelay;
 
692
      Sleep(ulSlice);
 
693
      ulDelay -= ulSlice;
 
694
    }
 
695
 
 
696
    return ulDelay;
 
697
  }