码迷,mamicode.com
首页 > 其他好文 > 详细

live555 出现Unable to determine our source address: This computer has an invalid IP address: 0.0.0.0 的解决方案。

时间:2014-11-29 15:57:56      阅读:2116      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   ar   color   os   使用   sp   

网上的方案我没有使用。对于只有一个网卡的主机来说,它的ip只有一个。可用shell命令获取到这个ip。

 

官方的live555的live/groupsock/GroupsockHelper.cpp里面的函数ourIPAddress如下:

  1 netAddressBits ourIPAddress(UsageEnvironment& env) {
  2   static netAddressBits ourAddress = 0;
  3   int sock = -1;
  4   struct in_addr testAddr;
  5 
  6   if (ReceivingInterfaceAddr != INADDR_ANY) {
  7     // Hack: If we were told to receive on a specific interface address, then 
  8     // define this to be our ip address:
  9     ourAddress = ReceivingInterfaceAddr;
 10   }
 11 
 12   if (ourAddress == 0) {
 13     // We need to find our source address
 14     struct sockaddr_in fromAddr;
 15     fromAddr.sin_addr.s_addr = 0;
 16 
 17     // Get our address by sending a (0-TTL) multicast packet,
 18     // receiving it, and looking at the source address used.
 19     // (This is kinda bogus, but it provides the best guarantee
 20     // that other nodes will think our address is the same as we do.)
 21     do {
 22       loopbackWorks = 0; // until we learn otherwise
 23 
 24       testAddr.s_addr = our_inet_addr("228.67.43.91"); // arbitrary
 25       Port testPort(15947); // ditto
 26 
 27       sock = setupDatagramSocket(env, testPort);
 28       if (sock < 0) break;
 29 
 30       if (!socketJoinGroup(env, sock, testAddr.s_addr)) break;
 31 
 32       unsigned char testString[] = "hostIdTest";
 33       unsigned testStringLength = sizeof testString;
 34 
 35       if (!writeSocket(env, sock, testAddr, testPort, 0,
 36                testString, testStringLength)) break;
 37 
 38       // Block until the socket is readable (with a 5-second timeout):
 39       fd_set rd_set;
 40       FD_ZERO(&rd_set);
 41       FD_SET((unsigned)sock, &rd_set);
 42       const unsigned numFds = sock+1;
 43       struct timeval timeout;
 44       timeout.tv_sec = 5;
 45       timeout.tv_usec = 0;
 46       int result = select(numFds, &rd_set, NULL, NULL, &timeout);
 47       if (result <= 0) break;
 48 
 49       unsigned char readBuffer[20];
 50       int bytesRead = readSocket(env, sock,
 51                  readBuffer, sizeof readBuffer,
 52                  fromAddr);
 53       if (bytesRead != (int)testStringLength
 54       || strncmp((char*)readBuffer, (char*)testString, testStringLength) != 0) {
 55     break;
 56       }
 57 
 58       // We use this packet‘s source address, if it‘s good:
 59       loopbackWorks = !badAddressForUs(fromAddr.sin_addr.s_addr);
 60     } while (0);
 61 
 62     if (sock >= 0) {
 63       socketLeaveGroup(env, sock, testAddr.s_addr);
 64       closeSocket(sock);
 65     }
 66 
 67     if (!loopbackWorks) do {
 68       // We couldn‘t find our address using multicast loopback,
 69       // so try instead to look it up directly - by first getting our host name, and then resolving this host name
 70       char hostname[100];
 71       hostname[0] = \0;
 72       int result = gethostname(hostname, sizeof hostname);
 73       if (result != 0 || hostname[0] == \0) {
 74     env.setResultErrMsg("initial gethostname() failed");
 75     break;
 76       }
 77 
 78       // Try to resolve "hostname" to an IP address:
 79       NetAddressList addresses(hostname);
 80       NetAddressList::Iterator iter(addresses);
 81       NetAddress const* address;
 82 
 83       // Take the first address that‘s not bad:
 84       netAddressBits addr = 0;
 85       while ((address = iter.nextAddress()) != NULL) {
 86     netAddressBits a = *(netAddressBits*)(address->data());
 87     if (!badAddressForUs(a)) {
 88       addr = a;
 89       break;
 90     }
 91       }
 92 
 93       // Assign the address that we found to "fromAddr" (as if the ‘loopback‘ method had worked), to simplify the code below: 
 94       fromAddr.sin_addr.s_addr = addr;
 95     } while (0);
 96 
 97     // Make sure we have a good address:
 98     netAddressBits from = fromAddr.sin_addr.s_addr;
 99     if (badAddressForUs(from)) {
100       char tmp[100];
101       sprintf(tmp, "This computer has an invalid IP address: %s", AddressString(from).val());
102       env.setResultMsg(tmp);
103       from = 0;
104     }
105 
106     ourAddress = from;
107 
108     // Use our newly-discovered IP address, and the current time,
109     // to initialize the random number generator‘s seed:
110     struct timeval timeNow;
111     gettimeofday(&timeNow, NULL);
112     unsigned seed = ourAddress^timeNow.tv_sec^timeNow.tv_usec;
113     our_srandom(seed);
114   }
115   return ourAddress;
116 }

这里通过发送一个udp包,来获取到主机ip地址;如果获取ip地址失败,则用gethostname的方法获取ip地址。

但是gethostname的方法也有可能失败,我在虚拟机上测试,发现gethostname的方法获取到ip地址都是"0.0.0.0"。所以我们要添加多一个方法来获取到正确的ip地址。

 

我们知道,如下的shell命令是可以获取到主机ip地址的:

ifconfig eth0|grep inet addr|awk -F ":" {print $2}|awk {print $1}

 bubuko.com,布布扣

 

在如下代码位置的上边,添加一个获取ip地址的代码。

    // Make sure we have a good address:
    netAddressBits from = fromAddr.sin_addr.s_addr;


新的获取ip地址的代码:

 1     if (badAddressForUs(fromAddr.sin_addr.s_addr))
 2     {
 3         #define MY_IP_BUF_LEN 32
 4         char MyIpBuf[ MY_IP_BUF_LEN ]={0};
 5         FILE *fpRead;
 6         //eth0:网口名称,实际主机的网口名称可能不是这个,请使用ifconfig命令查看。
 7         char* command=(char*)"ifconfig eth0|grep ‘inet addr‘|awk -F \":\" ‘{print $2}‘|awk ‘{print $1}‘";
 8         char* renewCh;
 9         
10         fpRead = popen(command, "r");
11         fgets(MyIpBuf, MY_IP_BUF_LEN, fpRead);
12         if(fpRead != NULL)
13             pclose(fpRead);
14 
15         renewCh=strstr(MyIpBuf,"\r");
16         if(renewCh)
17             *renewCh=\0;
18         renewCh=strstr(MyIpBuf,"\n");
19         if(renewCh)
20             *renewCh=\0;
21             
22         fromAddr.sin_addr.s_addr=our_inet_addr(MyIpBuf);
23         printf("----------fix for geting local ip fails, local ip=%s.\n", MyIpBuf);
24     }

 

重新编译,centos64位,运行:

bubuko.com,布布扣

bubuko.com,布布扣 

 

完。

 

live555 出现Unable to determine our source address: This computer has an invalid IP address: 0.0.0.0 的解决方案。

标签:des   style   blog   http   ar   color   os   使用   sp   

原文地址:http://www.cnblogs.com/liyou-blog/p/4130980.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!