Ejemplo n.º 1
0
main(int argc, char **argv)
{
  int err=0, count, i;
  in_addr_t srv, srvs[4];
  char *qname;

  srv = sysdnsserv4(NULL, 0, NULL, &err);
  buprintv(1, "main server: %% %%\n", x(srv), ip4(srv));
  
  srv = sysdnsserv4(srvs, 4, &count, &err);
  for(i=0;i<count;i++)
    buprintv(1, "nameserver: %% %%\n", x(srvs[i]), ip4(srvs[i]));

  if(argc > 1) qname = argv[1];
  else qname = "mail1.slu.se";
  dnsfwd4(NULL, 0, qname, srv, 2000, NULL);

}
Ejemplo n.º 2
0
bool  Discovery::send(const std::string &msg, const NodeDevice &node)
{
	bool isMulticast = !node.exists();

	char data[500];
	char *dp = &data[0];

	auto devName = UP::getDeviceName();
	// create 0-seperated string list
	strcpy(dp, msg.c_str()); dp += msg.length() + 1;
	strcpy(dp, devName.c_str()); dp += devName.length() + 1;
	strcpy(dp, getHwAddress().c_str());  dp += getHwAddress().length() + 1;

	int dataLen = dp - &data[0];


	if (!isMulticast) {
		auto na = node.getAddr(m_broadcastPort);
		bool ipv6 = (na.getFamily() == AF_INET6);
		if (sendto(ipv6 ? m_socMulticast : m_socBroadcast4, data, dataLen, 0, (struct sockaddr*)&na, sizeof(na)) != dataLen) {
			LOG(logERROR) << "Could not send broadcast message to " << node << lastError();
			return false;
		}
	}
	else
	{
		// ipv4 udp broadcast
		if (m_socBroadcast4 != -1) {
			struct sockaddr_in addr4;
			memset(&addr4, 0, sizeof(addr4));
			addr4.sin_family = AF_INET;
			addr4.sin_port = htons(m_broadcastPort);
			addr4.sin_addr.s_addr = htonl(INADDR_BROADCAST);

			if (sendto(m_socBroadcast4, data, dataLen, 0, (struct sockaddr*)&addr4, sizeof(addr4)) != dataLen) {
				LOG(logERROR) << "Could not send broadcast message on INADDR_BROADCAST! " << lastError();
				return false;
			}

			// Windows broadcast fix (for ipv4): send broadcast on each host addr
			char ac[1000];
			if (gethostname(ac, sizeof(ac)) == -1)
				return false;

			// TODO bug gethostbyname causes seg fault on windows in release (maybe mongoose?)
			addrinfo *ai;
			if (getaddrinfo(ac, NULL, NULL, &ai) != 0) {
				return false;
			}
			
			for (auto cai = ai; cai != 0; cai = cai->ai_next) {
				struct in_addr daddr;
                memcpy(&daddr, cai->ai_addr, std::min(sizeof(daddr), (size_t)cai->ai_addrlen));
				addr4.sin_addr.s_addr = daddr.s_addr | (255) << (8 * 3);

				std::string ip4(inet_ntoa(addr4.sin_addr));
				//std::cout << "broadcast message to " << ip4 << ":" << ntohs(addr4.sin_port) << "" << std::endl;

				if (sendto(m_socBroadcast4, data, dataLen, 0, (struct sockaddr*)&addr4, sizeof(addr4)) != dataLen) {
					LOG(logERROR) << "Could not send broadcast message  to " << ip4 << "!" << std::endl;
				}
			}

			freeaddrinfo(ai);
		}

		// ipv6 multicast
		if (m_socMulticast != -1) {
			if (sendto(m_socMulticast, data, dataLen, 0,
				(struct sockaddr *)&m_multicastAddrSend, sizeof(m_multicastAddrSend))
				!= dataLen) {
				LOG(logERROR) << "sending ipv6 multicast message on " << m_multicastAddrSend << " failed! " << lastError();
				return false;
			}
		}

		// send to explicit nodes
		for (NodeDevice n : m_explicitNodes) {
			send(msg, n);
		}
	}

	return true;
}
Ejemplo n.º 3
0
static void __init
ip(void)
{
	ip4();
	ip6();
}
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 ;
}
Ejemplo n.º 5
0
void  Discovery::send(const std::string &msg, const NodeDevice &node)
{
	SOCKET soc = (SOCKET)m_soc;

	char data[500];

	char hostname[32];
	gethostname(hostname, 31);

	auto hwAddress = getHwAddress();

	char *dp = &data[0];



	// create 0-seperated string list
	strcpy(dp, msg.c_str()); dp += strlen(dp) + 1;
	strcpy(dp, hostname); dp += strlen(hostname) + 1;
	strcpy(dp, hwAddress.c_str());  dp += hwAddress.length() + 1;

	int dataLen = dp - &data[0];

	struct sockaddr_in s;
	memset(&s, 0, sizeof(struct sockaddr_in));
	s.sin_family = AF_INET;
	s.sin_port = htons(m_broadcastPort);
	s.sin_addr.s_addr = node.exists() ? node.addr.s_addr : htonl(INADDR_BROADCAST);

	int sent = sendto(soc, data, dataLen, 0, (struct sockaddr*)&s, sizeof(struct sockaddr_in));

	if (sent != dataLen) {
		LOG(logERROR) << "Could not send broadcast message!";
	}

	//#ifdef _WIN32
	// win32 broadcast fix
	// use this on linux too?
	char ac[200];
	if (gethostname(ac, sizeof(ac)) == -1)
		return;

	struct hostent *phe = gethostbyname(ac);
	if (phe == 0)
		return;

	for (int i = 0; phe->h_addr_list[i] != 0; ++i) {
		struct in_addr addr;
		memcpy(&addr, phe->h_addr_list[i], sizeof(struct in_addr));
		addr.s_addr = addr.s_addr | (255) << (8 * 3);
		s.sin_addr.s_addr = addr.s_addr | (255) << (8 * 3);

		std::string ip4(inet_ntoa(s.sin_addr));
		//std::cout << "broadcast message to " << ip4 << ":" << ntohs(s.sin_port) << "!" << std::endl;

		sent = sendto(soc, data, dataLen, 0, (struct sockaddr*)&s, sizeof(struct sockaddr_in));
		if (sent != dataLen) {
			LOG(logERROR) << "Could not send broadcast message  to " << ip4 << "!" << std::endl;
		}
	}
	//#else
	//	inet_aton("10.0.0.255", &s.sin_addr);
	//	sendto(soc, data, 16 + 32, 0, (struct sockaddr*)&s, sizeof(struct sockaddr_in));
	//#endif
}
Ejemplo n.º 6
0
std::ostream& operator<<(std::ostream &strm, const Discovery::NodeDevice &nd) {
	std::string ip4(inet_ntoa(nd.addr));
	return strm << nd.name << " (id " << nd.id << ", ip4 address " << ip4 << ")";
}