0

How can I overpass the messages that comes through broadcast UDP from self? I need to overpass the udp requests that come from the same server(when sending broadcast). How can I obtain the curent IP address? Here is what I tried, but I get 127.0.1.1 which is not the IP address from the proper interface:

 tcp::resolver resolver(mIoService);
 tcp::resolver::query query(boost::asio::ip::host_name(), "");
 tcp::resolver::iterator iter = resolver.resolve(query);
 tcp::resolver::iterator end; // End marker.

Thanks!

yonutix
  • 1,964
  • 1
  • 22
  • 51
  • possible duplicate of [Get Local IP-Address using Boost.Asio](http://stackoverflow.com/questions/2674314/get-local-ip-address-using-boost-asio) – m.s. May 06 '15 at 08:34
  • It is not, in those answer there is a solution using the internet, but I don' t want to use this method, I want to use the program in a local area network. – yonutix May 06 '15 at 08:35

1 Answers1

0

As far as I know, there is no portable solution. What platforms are you targeting ? On most recent Unixes you should probably use ifaddrs, here is a (very) dummy example:

#include <ifaddrs.h>
#include <iostream>
#include <netinet/in.h>
#include <sstream>
#include <sys/types.h>

int
main()
{
  ifaddrs* ifap = 0;
  if (getifaddrs(&ifap) != 0)
    return 1;
  for (ifaddrs* iter = ifap; iter != nullptr; iter = iter->ifa_next)
    {
      std::ostringstream oss;
      uint8_t* tab;
      sockaddr_in* inet_addr;
      if (iter->ifa_addr->sa_family == AF_INET)
        {
          inet_addr = reinterpret_cast<sockaddr_in*>(iter->ifa_addr);
          tab = reinterpret_cast<uint8_t*>(&inet_addr->sin_addr.s_addr);
          for (size_t i = 0; i < sizeof(inet_addr->sin_addr.s_addr); ++i)
            {
              if (i != 0)
                oss << '.';
              oss << static_cast<unsigned int>(tab[i]);
            }
          std::cerr << oss.str() << std::endl;
        }
    }
  freeifaddrs(ifap);
}

This assumes ipv4 and all, you probably need to adapt it.

mefyl
  • 264
  • 1
  • 6