bool isIPInRange(const string& aIP1, const string& aIP2, uint8_t mask, bool v6) {
#ifndef _WIN32
	// not implemented
	return true;
#else
	if (!v6) { 
		uint32_t mmask = (~0u) << (32-mask);
		uint32_t result1 = IPToUInt(aIP1) & mmask;
		uint32_t result2 = IPToUInt(aIP2) & mmask;

		return result1 == result2;
	} else {
		if (mask & 16)
			return false;

		in6_addr addr1, addr2;

		auto p = aIP1.find("%");
		inet_pton(AF_INET6, (p != string::npos ? aIP1.substr(0, p) : aIP1).c_str(), &addr1);

		p = aIP2.find("%");
		inet_pton(AF_INET6, (p != string::npos ? aIP2.substr(0, p) : aIP2).c_str(), &addr2);

		//reset the non-common bytes
		int resetPos = 16-((128-mask) / 16);
		for (int i = resetPos; i < 16; ++i) {
			addr1.u.Byte[i] = 0;
			addr2.u.Byte[i] = 0;
		}

		return memcmp(addr1.u.Byte, addr2.u.Byte, 16) == 0;
	}
#endif
}
Example #2
0
int IsIPInRange(char* ip, char* network, char* mask) 
{
    int ip_addr = IPToUInt(ip);
    int network_addr = IPToUInt(network);
    int mask_addr = IPToUInt(mask);
 
    int net_lower = (network_addr & mask_addr);
    int net_upper = (net_lower | (~mask_addr));
 
    if (ip_addr >= net_lower &&
        ip_addr <= net_upper)
        return true;
    return false;
}
bool IsIPInRange(char * ipaddr, char * network, char * mask)
{
    uint32_t ip_addr = IPToUInt(ipaddr);
    uint32_t network_addr = IPToUInt(network);
    uint32_t mask_addr = IPToUInt(mask);

    uint32_t net_lower = (network_addr & mask_addr);
    uint32_t net_upper = (net_lower | (~mask_addr));

    if (ip_addr >= net_lower && ip_addr <= net_upper)
    {
        return true;
    }
    return false;
}
Example #4
0
void BleBox::GetDevicesState()
{
	boost::lock_guard<boost::mutex> l(m_mutex);

	std::map<const std::string, const int>::const_iterator itt;
	for (itt = m_devices.begin(); itt != m_devices.end(); ++itt)
	{
		std::stringstream sstr;
		sstr << "/api/" << DevicesType[itt->second].api_state << "/state";
		std::string command = sstr.str();

		Json::Value root = SendCommand(itt->first, command);
		if (root == "")
			continue;


		int node = IPToUInt(itt->first);
		if (node != 0)
		{
			switch (itt->second)
			{
				case 0:
				{
					if (root["state"].empty() == true)
					{
						_log.Log(LOG_ERROR, "BleBox: node 'state' missing!");
						break;
					}
					const bool state = root["state"].asBool();

					SendSwitch(node, itt->second, 255, state, 0, DevicesType[itt->second].name);
					break;
				}
				case 1:
				{
					if (root["state"].empty() == true)
					{
						_log.Log(LOG_ERROR, "BleBox: node 'state' missing!");
						break;
					}
					const int state = root["state"].asInt();

					const int currentPos = root["currentPos"].asInt();
					//	const int desiredPos = root["desiredPos"].asInt();
					const int pos = currentPos;

					bool opened = true;
					if ((state == 2 && pos == 100) || (state == 3))
						opened = false;

					SendSwitch(node, itt->second, 255, opened, pos, DevicesType[itt->second].name);
					break;
				}
				case 2:
				{
					if (root["light"].empty() == true)
					{
						_log.Log(LOG_ERROR, "BleBox: node 'light' missing!");
						break;
					}
					if (root["light"]["currentColor"].empty() == true)
					{
						_log.Log(LOG_ERROR, "BleBox: node 'currentColor' missing!");
						break;
					}
					const std::string currentColor = root["light"]["currentColor"].asString();
					int hexNumber;
					sscanf(currentColor.c_str(), "%x", &hexNumber);
					int level = (int)(hexNumber / (255.0 / 100.0));

					SendSwitch(node, itt->second, 255, level > 0, level, DevicesType[itt->second].name);
					break;
				}
				case 3:
				{
					if (root["rgbw"].empty() == true)
					{
						_log.Log(LOG_ERROR, "BleBox: node 'rgbw' missing!");
						break;
					}
					if (root["rgbw"]["currentColor"].empty() == true)
					{
						_log.Log(LOG_ERROR, "BleBox: node 'currentColor' missing!");
						break;
					}
					const std::string currentColor = root["rgbw"]["currentColor"].asString();
					int hexNumber;
					sscanf(currentColor.c_str(), "%x", &hexNumber);

					SendRGBWSwitch(node, itt->second, 255, hexNumber, true, DevicesType[itt->second].name);
					break;
				}
				case 4:
				{
					if (root["currentPos"].empty() == true)
					{
						_log.Log(LOG_ERROR, "BleBox: node 'currentPos' missing!");
						break;
					}
					const int currentPos = root["currentPos"].asInt();
					int level = (int)(currentPos / (255.0 / 100.0));

					SendSwitch(node, itt->second, 255, level > 0, level, DevicesType[itt->second].name);
					break;
				}
			}
			SetHeartbeatReceived();
		}
	}
}