/*!
	Scans an interface and enumerates all baos devices.
	It sends a search request as outlined in the BAOS 1.2 protocol
	documentation and waits for the responses. There are lots of
	magic numbers here and hard-coded offsets... See the spec
	for more information on what is happening here...

	We implement a receive timeout, and keep receiving until this
	timeout elapses. If this timeout is too fast, increase it to 500
	or 1000 for example.
*/
void BaosIpEnumerator::scanInterface(const NetworkInterface& networkInterface)
{
	poco_information(LOGGER(),
	                 format("Search devices on interface: %s (%s)",
	                        networkInterface.displayName(),
	                        networkInterface.address().toString()));

	try
	{
		// initialize socket
		MulticastSocket socket;
		socket.bind(SocketAddress(networkInterface.address(), 0));
		socket.setTimeToLive(DefaultMulticastTTL);

		// builds and sends a SEARCH_REQUEST to the socket
		sendSearchRequestFrame(socket);

		// wait for SEARCH_RESPONSES and collect it
		waitForSearchResponseFrames(socket);
	}
	catch (Poco::Exception& e)
	{
		poco_warning(LOGGER(), format("... search failed with error: %s", e.displayText()));
	}
}
bool HTTPMUSocket::send(const std::string &msg, const std::string &bindAddr, int bindPort) {
    MulticastSocket msock;
    if ((0 < bindAddr.length()) && (0 < bindPort))
        msock.bind(bindPort, bindAddr);
    DatagramPacket dgmPacket(msg, &m_ssdpMultiGroup);
    // Thnaks for Tho Beisch (11/09/04)
    msock.setTimeToLive(4);
    msock.send(&dgmPacket);
    return true;
}
Exemple #3
0
void MulticastSocketTest::testMulticast()
{
	MulticastEchoServer echoServer;
	MulticastSocket ms;
	int n = ms.sendTo("hello", 5, echoServer.group());
	assert (n == 5);
	char buffer[256];
	n = ms.receiveBytes(buffer, sizeof(buffer));
	assert (n == 5);
	assert (std::string(buffer, n) == "hello");
	ms.close();
}
bool BaosIpEnumerator::waitForRx(MulticastSocket& socket, std::vector<unsigned char>& buffer)
{
	const Timespan timeout = SearchResponseTimeout * 1000;
	if (socket.poll(timeout, Poco::Net::Socket::SELECT_READ))
	{
		buffer.resize(RxBufferLength);
		SocketAddress deviceAddress;
		std::size_t size = socket.receiveFrom(&buffer.at(0), buffer.size(), deviceAddress);
		buffer.resize(size);
		return true;
	}

	return false;
}
void BaosIpEnumerator::waitForSearchResponseFrames(MulticastSocket& socket)
{
	std::vector<unsigned char> buffer;
	while (waitForRx(socket, buffer))
	{
		poco_trace(LOGGER(), format("Received search response: %s", LoggerFormatter::toHex(buffer)));
		addDevice(buffer, socket.address().host());
	}
}
Exemple #6
0
int run_mcast_client(InetSocketAddress &addr)
{
    try
    {
        InetSocketAddress from;
        SRL::byte buf[256];
        DateTime dt;
        MulticastSocket client;
        while (1)
        {
            dt.update();
            client.sendTo(addr, dt.asString());
            Console::formatLine("sent: '%s'", dt.asString().text());
            System::Sleep(1000);
        }
        client.close();
    }
    catch (SRL::Errors::Exception &e)
    {
        Console::writeLine(e.message());
    }
    return 0;
}
void BaosIpEnumerator::sendSearchRequestFrame(MulticastSocket& socket)
{
	const IPAddress& address = socket.address().host();
	StringTokenizer stringTokenizer(address.toString(), ".");
	BOOST_ASSERT(stringTokenizer.count() == 4 && "Invalid IP Address");

	const unsigned short port = socket.address().port();

	// build SEARCH_REQUEST frame
	const std::vector<unsigned char> request =
	{
		0x06, 0x10, 0x02, 0x01, 0x00, 0x0E, 0x08, 0x01,
		static_cast<unsigned char>(NumberParser::parse(stringTokenizer[0])),
		static_cast<unsigned char>(NumberParser::parse(stringTokenizer[1])),
		static_cast<unsigned char>(NumberParser::parse(stringTokenizer[2])),
		static_cast<unsigned char>(NumberParser::parse(stringTokenizer[3])),
		static_cast<unsigned char>((port >> 8) & 0xFF),
		static_cast<unsigned char>(port & 0xFF)
	};

	// send to socket
	const SocketAddress destAddress(MulticastProtocolConstants::Address, MulticastProtocolConstants::Port);
	socket.sendTo(&request.at(0), static_cast<int>(request.size()), destAddress);
}