receiver(boost::asio::io_service& io_service, const boost::asio::ip::address& listen_address, const boost::asio::ip::address& multicast_address) : socket_(io_service) { // Create the socket so that multiple may be bound to the same address. boost::asio::ip::udp::endpoint listen_endpoint( listen_address, multicast_port); socket_.open(listen_endpoint.protocol()); socket_.set_option(boost::asio::ip::udp::socket::reuse_address(true)); socket_.bind(listen_endpoint); // Join the multicast group. socket_.set_option( boost::asio::ip::multicast::join_group(multicast_address)); socket_.async_receive_from( boost::asio::buffer(data_, max_length), sender_endpoint_, boost::bind(&receiver::handle_receive_from, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)); }
mDNS::mDNS(boost::asio::io_service& io_service, const boost::asio::ip::address& listen_address, const boost::asio::ip::address& multicast_address, int interval, bool DNS_SD ) : socket_(io_service), endpoint_(multicast_address, 5353), timer_(io_service), message_count_(0), interval_(interval), DNS_SD(DNS_SD) { // Create the socket so that multiple may be bound to the same address. boost::asio::ip::udp::endpoint listen_endpoint( listen_address, MULTICAST_PORT); socket_.open(listen_endpoint.protocol()); socket_.set_option(boost::asio::ip::udp::socket::reuse_address(true)); socket_.bind(listen_endpoint); // Join the multicast group. socket_.set_option( boost::asio::ip::multicast::join_group(multicast_address)); char hostname[64]; gethostname(hostname, 64); //unsigned char tmp[strlen(hostname)+strlen((char*) SERVICE_NAME)+1]; unsigned int i = 0; for (uint i = 0; i < 128; i++) { my_name[i] = '\0'; ssh_service_name[i] = '\0'; } for (; i < strlen(hostname); i++) { my_name[i] = hostname[i]; ssh_service_name[i] = hostname[i]; } uint j = i; my_name[i++] = '.'; ssh_service_name[j++] = '.'; for (; i <= strlen(hostname) + strlen((char*) SERVICE_NAME); i++) { my_name[i] = SERVICE_NAME[i-strlen(hostname)-1]; } for (; j <= strlen(hostname) + strlen((char*) SSH_SERVICE); j++) { ssh_service_name[j] = SSH_SERVICE[j-strlen(hostname)-1]; } //std::cout << "MY_NAME = " << my_name << std::endl; //std::cout << "SSH_SERVICE_NAME = " << ssh_service_name << std::endl; my_ip = getIP(); srand ( time(NULL) ); prepare_PTR_query(); send_PTR_query(); if (DNS_SD) { broadcast_SSH(); } receive(); }