bool NetworkConnectorEngine::pruneConnectionProspectList(QList<Node *>* aListToPrune ) const {
    bool retval = false ;
    const Node* nodeOfConnection = NULL ;
    QList<QHostAddress> ipAddrList = QNetworkInterface::allAddresses() ;
    for ( int i = aListToPrune->size()-1 ; i >= 0 ; i-- ) {
        // if our own node got included, remove that from list
        bool prunedAlready = false ;
        if ( iController->getNode().nodeFingerPrint()
                == aListToPrune->value(i)->nodeFingerPrint()   ) {
            delete aListToPrune->value(i) ;
            aListToPrune->removeAt(i) ;
        } else {
            // and remove all nodes already connected
            for ( int j = iModel->getConnections().size()-1 ; j >= 0 ; j-- ) {
                // and don't connect to already-connected nodes
                nodeOfConnection = iModel->getConnections().value(j)->node() ;
                if ( nodeOfConnection &&
                        ( nodeOfConnection->nodeFingerPrint() == aListToPrune->value(i)->nodeFingerPrint() ) ) {
                    delete aListToPrune->value(i) ;
                    aListToPrune->removeAt(i) ;
                    retval = true ;
                    prunedAlready= true ;
                    break; // out from j-loop because i to compare against 
                           // is not any more
                }
            }
            // and check that no our own addresses are at list:
            for ( int k = 0 ; !prunedAlready && k < ipAddrList.size() ; k++ ) {
                QHostAddress a = ipAddrList[k] ;
                if ( ( a.protocol() == QAbstractSocket::IPv4Protocol &&
                        aListToPrune->value(i)->ipv4Addr() > 0 &&
                        QHostAddress(aListToPrune->value(i)->ipv4Addr()) == a ) || ( ! Connection::Ipv6AddressesEqual(KNullIpv6Addr,
                                aListToPrune->value(i)->ipv6Addr()) &&
                                a.protocol() == QAbstractSocket::IPv6Protocol &&
                                QHostAddress(aListToPrune->value(i)->ipv6Addr()) == a )  ) {
                    delete aListToPrune->value(i) ;
                    aListToPrune->removeAt(i) ;
                    retval = true ;
                    QLOG_STR("Pruned own network addr " + a.toString()) ;
                    break;
                }
            }
        }
    }
    // check blacklist too:
    bool prunedAlready = false ;
    for ( int i = aListToPrune->size()-1 ; i >= 0 ; i-- ) {
        prunedAlready = false ;
        if ( aListToPrune->value(i)->ipv4Addr() ) {
            QHostAddress ip4(aListToPrune->value(i)->ipv4Addr()) ;
            if ( iAddressBlacklist.contains(ip4) ) {
                delete aListToPrune->value(i) ;
                aListToPrune->removeAt(i) ;
                prunedAlready = true ;
                QLOG_STR("Pruned due to blacklist " + ip4.toString()) ;
            }
        }
        if ( prunedAlready == false &&
                ( ! Connection::Ipv6AddressesEqual(KNullIpv6Addr,
                        aListToPrune->value(i)->ipv6Addr()) ) ) {
            QHostAddress ip6(aListToPrune->value(i)->ipv6Addr()) ;
            if ( iAddressBlacklist.contains(ip6) ) {
                delete aListToPrune->value(i) ;
                aListToPrune->removeAt(i) ;
                prunedAlready = true ;
                QLOG_STR("Pruned due to blacklist " + ip6.toString()) ;
            }
        }
    }
    return retval ;
}
示例#2
0
static void __init
ip(void)
{
	ip4();
	ip6();
}
示例#3
0
void Service::start()
{
  auto& mac = Dev::eth(0).mac();
  net::IP4::addr ip4{{ mac.part[2],mac.part[3],mac.part[4],mac.part[5] }};
  net::IP6::addr ip6(0, 0, 0xFFFF0000, ip4.whole);
  
  // verify equality operators
  net::IP6::addr test1(9, 2, 3, 4, 5, 6, 7, 8);
  net::IP6::addr test2(9, 2, 3, 4, 5, 6, 7, 8);
  
  assert(ip6 == ip6);
  assert(ip6 != test1);
  assert(ip6 != test2);
  assert(test1 == test2);
  
  // verify that multicast addresses are multicast
  assert(net::IP6::addr::node_all_nodes.is_multicast());
  assert(net::IP6::addr::link_all_nodes.is_multicast());
  
  // basic UDP service
  net::Inet::ifconfig(
      net::ETH0, 
      ip4, {{255, 255, 255, 0}}, 
      ip6);
  
  net::Inet* inet = net::Inet::up();
  
  printf("Service IP4 address: %s\n", net::Inet::ip4(net::ETH0).str().c_str());
  printf("Service IP6 address: %s\n", net::Inet::ip6(net::ETH0).str().c_str());
  
  /// Using multicast we can see the packet from Linux:
  /// > nc -6u ff02::2%include0 64
  
  /// Multicast ping:
  /// > ping6 ff02::1 -I include0
  
  /// Regular ping:
  /// > 
  
  // IPv6 NDP autoconf testing
  inet->ip6_ndp_discovery();
  
  // basic UDP service
  static const int UDP_PORT = 64;
  inet->udp6_listen(UDP_PORT,
    [=] (std::shared_ptr<net::PacketUDP6>& pckt) -> int
    {
      printf("Received UDP6 packet from %s to my listener on port %d\n",
          pckt->src().str().c_str(), pckt->dst_port());
      
      std::string data((const char*) pckt->data(), pckt->data_length());
      
      printf("Contents (len=%d):\n%s\n", pckt->data_length(), data.c_str());
      
      // unfortunately,
      // copy the ether src field of the incoming packet
      net::Ethernet::addr ether_src = 
          ((net::Ethernet::header*) pckt->buffer())->src;
      
      // create a response packet with destination [ether_src] dst()
      std::shared_ptr<net::PacketUDP6> newpacket = 
          inet->udp6_create(ether_src, pckt->dst(), UDP_PORT);
      
      const char* text = "This is the response packet!";
      // copy text into UDP data section
      memcpy( newpacket->data(),  text,  strlen(text) );
      // set new length
      newpacket->set_length(strlen(text));
      
      // generate checksum for packet before sending
      newpacket->gen_checksum();
      
      // ship it to the ether
      inet->udp6_send(newpacket);
      return -1;
    }
  );
  
}