~ubuntu-branches/ubuntu/trusty/ntop/trusty

« back to all changes in this revision

Viewing changes to address.c

  • Committer: Bazaar Package Importer
  • Author(s): Ola Lundqvist
  • Date: 2008-06-15 14:38:28 UTC
  • mfrom: (2.1.11 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080615143828-oalh84nda2hje4do
Tags: 3:3.3-11
Correction of Polish translation encoding, closes: #479490. Thanks
to Christian Perrier <bubulle@debian.org> for the help.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 *  Copyright (C) 1998-2005 Luca Deri <deri@ntop.org>
 
2
 *  Copyright (C) 1998-2007 Luca Deri <deri@ntop.org>
3
3
 *
4
4
 *                          http://www.ntop.org/
5
5
 *
62
62
          symbolic[MAX_LEN_SYM_HOST_NAME-2] = '\0';
63
63
 
64
64
        for(i=0; i<strlen(symbolic); i++)
65
 
          if(isupper(symbolic[i])) tolower(symbolic[i]);
 
65
          if(isupper(symbolic[i])) symbolic[i] = tolower(symbolic[i]);
66
66
 
67
67
        setResolvedName(el, symbolic, type);
68
68
      }
153
153
  /* First check whether the address we search for is cached... */
154
154
  if((data_data.dptr != NULL) &&
155
155
     (data_data.dsize == (sizeof(StoredAddress))) &&
156
 
     (myGlobals.actTime - ((StoredAddress*)data_data.dptr)->recordCreationTime < CONST_DNSCACHE_LIFETIME) ) {
 
156
     (myGlobals.actTime - ((StoredAddress*)data_data.dptr)->recordCreationTime < CONST_DNSCACHE_LIFETIME)) {
157
157
    StoredAddress *retrievedAddress;
158
158
 
159
159
    retrievedAddress = (StoredAddress*)data_data.dptr;
389
389
             * Treat as NOT FOUND, but use reportedFreaky to put out one message per run.
390
390
             * Known to happen under Linux, other OSes uncertain...
391
391
             */
392
 
        if (reportedFreaky == FALSE) {
393
 
                reportedFreaky = TRUE;
394
 
                traceEvent(CONST_TRACE_NOISY, "gethost... call returned NULL/NETDB_SUCCESS - "
395
 
                           "this is odd, but apparently normal");
 
392
        if((reportedFreaky == FALSE) && (hp != NULL)) {
 
393
          reportedFreaky = TRUE;
 
394
          traceEvent(CONST_TRACE_NOISY, "gethost... call returned NULL/NETDB_SUCCESS - "
 
395
                     "this is odd, but apparently normal");
396
396
        }
397
397
        myGlobals.numDNSErrorHostNotFound++;
398
398
        break;
519
519
 
520
520
  if((!forceResolution)
521
521
     && myGlobals.runningPref.trackOnlyLocalHosts
522
 
     && (!_pseudoLocalAddress(&elem)))
 
522
     && (!_pseudoLocalAddress(&elem, NULL, NULL)))
523
523
    return;
524
524
 
525
525
  /*
526
526
    The address queue is far too long. This is usefult for
527
527
    avoiding problems due to DOS applications
528
528
  */
529
 
  if(myGlobals.addressQueuedCurrent > MAX_NUM_QUEUED_ADDRESSES) {
 
529
  if(myGlobals.addressQueuedCurrent > (MAX_NUM_QUEUED_ADDRESSES*myGlobals.numDevices)) {
530
530
    static char shownMsg = 0;
531
531
 
532
532
    if(!shownMsg) {
554
554
    key_data.dsize = 16;
555
555
  }
556
556
#endif
 
557
  else {
 
558
        traceEvent(CONST_TRACE_WARNING, "Invalid address family (%d) found!",
 
559
                        elem.hostFamily);
 
560
        return;
 
561
  }
557
562
 
558
563
  safe_snprintf(__FILE__, __LINE__, dataBuf, sizeof(dataBuf), "%s", addrtostr(&elem));
559
564
  data_data.dptr = dataBuf;
562
567
  rc = gdbm_store(myGlobals.addressQueueFile, key_data, data_data, GDBM_INSERT);
563
568
 
564
569
  if (rc == 0) {
 
570
    accessMutex(&myGlobals.queueAddressMutex, "dequeueAddress");
565
571
    myGlobals.addressQueuedCurrent++, myGlobals.addressQueuedCount++;
566
572
    if (myGlobals.addressQueuedCurrent > myGlobals.addressQueuedMax)
567
573
      myGlobals.addressQueuedMax = myGlobals.addressQueuedCurrent;
 
574
    releaseMutex(&myGlobals.queueAddressMutex);
568
575
 
569
576
#ifdef DNS_DEBUG
570
577
    traceEvent(CONST_TRACE_INFO, "DNS_DEBUG: Queued address '%s' [addr queue=%d/max=%d]",
573
580
  } else {
574
581
    /* rc = 1 is duplicate key, which is fine.  Other codes are problems... */
575
582
    if (rc != 1) {
576
 
      traceEvent(CONST_TRACE_ERROR, "Queue of address '%s' failed, code %d [addr queue=%d/max=%d]",
577
 
                 dataBuf, rc, myGlobals.addressQueuedCurrent, myGlobals.addressQueuedMax);
 
583
      traceEvent(CONST_TRACE_ERROR, "Queue of address '%s' failed (%s) [addr queue=%d/max=%d]",
 
584
                 dataBuf, 
 
585
#if defined(WIN32) && defined(__GNUC__)
 
586
                 "no additional information available",
 
587
#else
 
588
                 gdbm_strerror(gdbm_errno),
 
589
#endif
 
590
                 myGlobals.addressQueuedCurrent,
 
591
                 myGlobals.addressQueuedMax);
578
592
      traceEvent(CONST_TRACE_INFO, "ntop processing continues, address will not be resolved");
579
593
    } else {
 
594
      accessMutex(&myGlobals.queueAddressMutex, "dequeueAddress");
580
595
      myGlobals.addressQueuedDup++;
 
596
      releaseMutex(&myGlobals.queueAddressMutex);
581
597
#ifdef DNS_DEBUG
582
598
      traceEvent(CONST_TRACE_INFO, "DNS_DEBUG: Duplicate queue of address '%s' ignored",
583
599
                 dataBuf);
596
612
 
597
613
/* ************************************ */
598
614
 
 
615
/*
 
616
  Remove from queue an address that was waiting to be
 
617
  resolved
 
618
*/
 
619
void purgeQueuedV4HostAddress(u_int32_t addr) {
 
620
  datum key_data;
 
621
  struct in_addr addrv4;
 
622
 
 
623
  addrv4.s_addr = addr;
 
624
 
 
625
  /*
 
626
    traceEvent(CONST_TRACE_INFO, "purgeQueuedV4HostAddress(%s)", 
 
627
                                 _intoa(addrv4, buf, sizeof(buf))); 
 
628
  */
 
629
 
 
630
  key_data.dptr = (void*)&addr;
 
631
  key_data.dsize = 4;
 
632
  if(gdbm_delete(myGlobals.addressQueueFile, key_data)) {
 
633
    accessMutex(&myGlobals.queueAddressMutex, "purgeQueuedV4HostAddress");
 
634
    if(myGlobals.addressQueuedCurrent > 0) myGlobals.addressQueuedCurrent--;
 
635
    releaseMutex(&myGlobals.queueAddressMutex);
 
636
  }
 
637
}
 
638
 
 
639
/* ************************************ */
 
640
 
599
641
void* dequeueAddress(void *_i) {
600
 
  int dqaIndex = (int)_i;
 
642
  int dqaIndex = (int)((long)_i);
601
643
 
602
644
  HostAddr addr;
603
645
  datum key_data, data_data;
604
646
 
605
 
  traceEvent(CONST_TRACE_INFO, "THREADMGMT[t%lu]: DNSAR(%d): Address resolution thread running [p%d]",
606
 
             pthread_self(), dqaIndex+1, getpid());
607
 
 
 
647
  traceEvent(CONST_TRACE_INFO, 
 
648
             "THREADMGMT[t%lu]: DNSAR(%d): Address resolution thread running",
 
649
             pthread_self(), dqaIndex+1);
 
650
  
608
651
  while(myGlobals.ntopRunState <= FLAG_NTOPSTATE_RUN) {
609
652
#ifdef DEBUG
610
653
    traceEvent(CONST_TRACE_INFO, "DEBUG: Waiting for address to resolve...");
650
693
      traceEvent(CONST_TRACE_INFO, "DNS_DEBUG: Resolved address %s", addrtostr(&addr));
651
694
#endif
652
695
 
653
 
      myGlobals.addressQueuedCurrent--;
 
696
      accessMutex(&myGlobals.queueAddressMutex, "dequeueAddress");
 
697
      if(myGlobals.addressQueuedCurrent > 0) myGlobals.addressQueuedCurrent--;
 
698
      releaseMutex(&myGlobals.queueAddressMutex);
 
699
 
654
700
      gdbm_delete(myGlobals.addressQueueFile, data_data);
655
701
      key_data = data_data;
656
702
      data_data = gdbm_nextkey(myGlobals.addressQueueFile, key_data);
1270
1316
        if(id == 128)
1271
1317
          msg[j++] = '\'';
1272
1318
        else if(id < 128) {
1273
 
          snprintf(tmpStr2, sizeof(tmpStr2), "%c", id);
 
1319
          safe_snprintf(__FILE__, __LINE__, tmpStr2, sizeof(tmpStr2), "%c", id);
1274
1320
          msg[j++] = tmpStr2[0];
1275
1321
        }
1276
1322
 
1364
1410
 
1365
1411
/* ************************************ */
1366
1412
 
 
1413
//#define MDNS_DEBUG
 
1414
 
1367
1415
static void handleMdnsName(HostTraffic *srcHost, u_short sport, u_char *mdns_name) {
1368
1416
  char *mdnsStrtokState, *name = NULL, *appl = NULL, *proto = NULL, *domain = NULL;
1369
1417
  char *tmpStr = strdup((char*)mdns_name);
1415
1463
        /* Digital Audio Access Protocol (daap.sourceforge.net) */
1416
1464
        updateHostUsers(name, BITFLAG_DAAP_USER, srcHost);
1417
1465
      }
 
1466
    } else if(name && appl && (!strcmp(appl, "local"))) {
 
1467
      setHostName(srcHost, name);
1418
1468
    }
1419
1469
 
1420
1470
     free(tmpStr);
1658
1708
    {
1659
1709
      u_int32_t     padding;
1660
1710
 
1661
 
      padding=((u_int32_t)bp) % sizeof(u_int32_t);
 
1711
      padding=((u_int32_t)((long)bp)) % sizeof(u_int32_t);
1662
1712
      bp += padding;
1663
1713
      buflen -= padding;
1664
1714
    }
1755
1805
  return(transactionId);
1756
1806
}
1757
1807
 
1758
 
 
1759
1808
/* **************************************** */
1760
1809
 
1761
1810
void checkSpoofing(HostTraffic *hostToCheck, int actualDeviceId) {
1783
1832
 
1784
1833
}
1785
1834
 
 
1835
/* **************************************** */
 
1836
 
 
1837
char* host2networkName(HostTraffic *el, char *buf, u_short buf_len) {
 
1838
  struct in_addr addr = el->hostIpAddress.Ip4Address;
 
1839
  char buf1[64];
 
1840
      
 
1841
  addr.s_addr = addr.s_addr & (0xFFFFFFFF << (32-el->network_mask));
 
1842
 
 
1843
  safe_snprintf(__FILE__, __LINE__, buf, buf_len, "%s/%d",
 
1844
                _intoa(addr, buf1, sizeof(buf1)), 
 
1845
                el->network_mask);
 
1846
  
 
1847
  return(buf);
 
1848
}