您当前的位置: 首页 > 

qianbo_insist

暂无认证

  • 0浏览

    0关注

    399博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

去除防火墙和虚拟机对live555启动IP地址的影响

qianbo_insist 发布时间:2022-06-22 17:22:30 ,浏览量:0

live555 启动IP获取方式

live555 的ip获取方式和其他软件都不太一样,主要是在于他是用组播方式获取的。代码如下所示:

Boolean loopbackWorks = 1;

netAddressBits ourIPAddress(UsageEnvironment& env) {
  static netAddressBits ourAddress = 0;
  int sock = -1;
  struct in_addr testAddr;

  if (ReceivingInterfaceAddr != INADDR_ANY) {
    // Hack: If we were told to receive on a specific interface address, then 
    // define this to be our ip address:
    ourAddress = ReceivingInterfaceAddr;
  }

  if (ourAddress == 0) {
    // We need to find our source address
    struct sockaddr_in fromAddr;
    fromAddr.sin_addr.s_addr = 0;

    // Get our address by sending a (0-TTL) multicast packet,
    // receiving it, and looking at the source address used.
    // (This is kinda bogus, but it provides the best guarantee
    // that other nodes will think our address is the same as we do.)
    do {
      loopbackWorks = 0; // until we learn otherwise

      testAddr.s_addr = our_inet_addr("228.67.43.91"); // arbitrary
      Port testPort(15947); // ditto

      sock = setupDatagramSocket(env, testPort);
      if (sock data());
	if (!badAddressForUs(a)) {
	  addr = a;
	  break;
	}
      }

      // Assign the address that we found to "fromAddr" (as if the 'loopback' method had worked), to simplify the code below: 
      fromAddr.sin_addr.s_addr = addr;
    } while (0);

    // Make sure we have a good address:
    netAddressBits from = fromAddr.sin_addr.s_addr;
    if (badAddressForUs(from)) {
      char tmp[100];
      sprintf(tmp, "This computer has an invalid IP address: %s", AddressString(from).val());
      env.setResultMsg(tmp);
      from = 0;
    }

    ourAddress = from;

    // Use our newly-discovered IP address, and the current time,
    // to initialize the random number generator's seed:
    struct timeval timeNow;
    gettimeofday(&timeNow, NULL);
    unsigned seed = ourAddress^timeNow.tv_sec^timeNow.tv_usec;
    our_srandom(seed);
  }
  return ourAddress;
}
两个问题

     1、这代码本身问题不大,一是防火墙问题,如果防火墙不能放行某些端口,是获取不到IP的,这导致直接最后的IP地址是初始值"0.0.0.0",还有一个问题,就是虚拟机引起的多IP地址地址问题。

     2、当我们安装虚拟机时,会多出很多IP地址,这样导致一个问题,就是他会取到一个无用的IP地址,需要解决这两个问题。

     直接修改live555 的获取IP的地方,增加配置文件是最合适的。我们自己来写一个读取文件的config文件的东西

#pragma once
#include 
#include 
#include 
#include 
#include 
#include 
#include  //transform
#include 
#pragma comment(lib,"ws2_32")
#define GET_lower(x) transform(x.begin(), x.end(), x.begin(), ::tolower)
//to translate the string to upper 
#define Get_upper(x) transform(s.begin(), s.end(), s.begin(), ::toupper)

static void trim(std::string &s)
{
	if (s.empty())
		return;
	s.erase(s.find_last_not_of('\r') + 1); //the linux system getline must remove the '\r'
	s.erase(s.find_last_not_of(' ') + 1);
	//s.erase(s.find_last_not_of('\t') + 1);
	s.erase(0, s.find_first_not_of(' '));
	//s.erase(0, s.find_first_not_of('\t'));

}
static bool ifnote(const std::string & tmp)
{
	if (tmp.empty())
		return false;
	int l = (int)tmp.length();
	char a = tmp[0];
	char b;
	if (l >= 2)
		b = tmp[1];
	if (l == 1)
		return a == '#';
	if (l >= 2)
		return a == '#' || (a == '/'&& b == '/');
	return false;
}


//judge is node
//[] is the node,   
//len the line's length  
//if tmp is a node, the variable node return the real node name 
//the tmp string has been trimed trim
static bool ifnode(const std::string & tmp, std::string &node)
{
	int len = -1;
	node.clear();
	if (tmp.empty())
		return false;
	//trim(tmp);
	int l = (int)tmp.size();
	if (l > 2)//bigger than []
	{
		//cout            
关注
打赏
1663161521
查看更多评论
0.0412s