📄 rtpfaketransmitter.cpp
字号:
datacopy = new uint8_t[data_len]; if (datacopy == 0) { delete addr; return ERR_RTP_OUTOFMEM; } memcpy(datacopy, data, data_len); // got data, process it if (receivemode == RTPTransmitter::AcceptAll) acceptdata = true; else acceptdata = ShouldAcceptData(addr->GetIP(),addr->GetPort()); if (acceptdata) { // adding packet to queue RTPRawPacket *pack; pack = new RTPRawPacket(datacopy,data_len,addr,curtime,rtp); if (pack == 0) { delete addr; return ERR_RTP_OUTOFMEM; } rawpacketlist.push_back(pack); } return 0;}int RTPFakeTransmitter::ProcessAddAcceptIgnoreEntry(uint32_t ip,uint16_t port){ acceptignoreinfo.GotoElement(ip); if (acceptignoreinfo.HasCurrentElement()) // An entry for this IP address already exists { PortInfo *portinf = acceptignoreinfo.GetCurrentElement(); if (port == 0) // select all ports { portinf->all = true; portinf->portlist.clear(); } else if (!portinf->all) { std::list<uint16_t>::const_iterator it,begin,end; begin = portinf->portlist.begin(); end = portinf->portlist.end(); for (it = begin ; it != end ; it++) { if (*it == port) // already in list return 0; } portinf->portlist.push_front(port); } } else // got to create an entry for this IP address { PortInfo *portinf; int status; portinf = new PortInfo(); if (port == 0) // select all ports portinf->all = true; else portinf->portlist.push_front(port); status = acceptignoreinfo.AddElement(ip,portinf); if (status < 0) { delete portinf; return status; } } return 0;}void RTPFakeTransmitter::ClearAcceptIgnoreInfo(){ acceptignoreinfo.GotoFirstElement(); while (acceptignoreinfo.HasCurrentElement()) { PortInfo *inf; inf = acceptignoreinfo.GetCurrentElement(); delete inf; acceptignoreinfo.GotoNextElement(); } acceptignoreinfo.Clear();} int RTPFakeTransmitter::ProcessDeleteAcceptIgnoreEntry(uint32_t ip,uint16_t port){ acceptignoreinfo.GotoElement(ip); if (!acceptignoreinfo.HasCurrentElement()) return ERR_RTP_FAKETRANS_NOSUCHENTRY; PortInfo *inf; inf = acceptignoreinfo.GetCurrentElement(); if (port == 0) // delete all entries { inf->all = false; inf->portlist.clear(); } else // a specific port was selected { if (inf->all) // currently, all ports are selected. Add the one to remove to the list { // we have to check if the list doesn't contain the port already std::list<uint16_t>::const_iterator it,begin,end; begin = inf->portlist.begin(); end = inf->portlist.end(); for (it = begin ; it != end ; it++) { if (*it == port) // already in list: this means we already deleted the entry return ERR_RTP_FAKETRANS_NOSUCHENTRY; } inf->portlist.push_front(port); } else // check if we can find the port in the list { std::list<uint16_t>::iterator it,begin,end; begin = inf->portlist.begin(); end = inf->portlist.end(); for (it = begin ; it != end ; ++it) { if (*it == port) // found it! { inf->portlist.erase(it); return 0; } } // didn't find it return ERR_RTP_FAKETRANS_NOSUCHENTRY; } } return 0;}bool RTPFakeTransmitter::ShouldAcceptData(uint32_t srcip,uint16_t srcport){ if (receivemode == RTPTransmitter::AcceptSome) { PortInfo *inf; acceptignoreinfo.GotoElement(srcip); if (!acceptignoreinfo.HasCurrentElement()) return false; inf = acceptignoreinfo.GetCurrentElement(); if (!inf->all) // only accept the ones in the list { std::list<uint16_t>::const_iterator it,begin,end; begin = inf->portlist.begin(); end = inf->portlist.end(); for (it = begin ; it != end ; it++) { if (*it == srcport) return true; } return false; } else // accept all, except the ones in the list { std::list<uint16_t>::const_iterator it,begin,end; begin = inf->portlist.begin(); end = inf->portlist.end(); for (it = begin ; it != end ; it++) { if (*it == srcport) return false; } return true; } } else // IgnoreSome { PortInfo *inf; acceptignoreinfo.GotoElement(srcip); if (!acceptignoreinfo.HasCurrentElement()) return true; inf = acceptignoreinfo.GetCurrentElement(); if (!inf->all) // ignore the ports in the list { std::list<uint16_t>::const_iterator it,begin,end; begin = inf->portlist.begin(); end = inf->portlist.end(); for (it = begin ; it != end ; it++) { if (*it == srcport) return false; } return true; } else // ignore all, except the ones in the list { std::list<uint16_t>::const_iterator it,begin,end; begin = inf->portlist.begin(); end = inf->portlist.end(); for (it = begin ; it != end ; it++) { if (*it == srcport) return true; } return false; } } return true;}#ifdef WIN32int RTPFakeTransmitter::CreateAbortDescriptors(){ // no need for these no more/* SOCKET listensock; int size; struct sockaddr_in addr; listensock = socket(PF_INET,SOCK_STREAM,0); if (listensock == RTPSOCKERR) return ERR_RTP_FAKETRANS_CANTCREATEABORTDESCRIPTORS; memset(&addr,0,sizeof(struct sockaddr_in)); addr.sin_family = AF_INET; if (bind(listensock,(struct sockaddr *)&addr,sizeof(struct sockaddr_in)) != 0) { RTPCLOSE(listensock); return ERR_RTP_FAKETRANS_CANTCREATEABORTDESCRIPTORS; } memset(&addr,0,sizeof(struct sockaddr_in)); size = sizeof(struct sockaddr_in); if (getsockname(listensock,(struct sockaddr*)&addr,&size) != 0) { RTPCLOSE(listensock); return ERR_RTP_FAKETRANS_CANTCREATEABORTDESCRIPTORS; } unsigned short connectport = ntohs(addr.sin_port); abortdesc[0] = socket(PF_INET,SOCK_STREAM,0); if (abortdesc[0] == RTPSOCKERR) { RTPCLOSE(listensock); return ERR_RTP_FAKETRANS_CANTCREATEABORTDESCRIPTORS; } memset(&addr,0,sizeof(struct sockaddr_in)); addr.sin_family = AF_INET; if (bind(abortdesc[0],(struct sockaddr *)&addr,sizeof(struct sockaddr_in)) != 0) { RTPCLOSE(listensock); RTPCLOSE(abortdesc[0]); return ERR_RTP_FAKETRANS_CANTCREATEABORTDESCRIPTORS; } if (listen(listensock,1) != 0) { RTPCLOSE(listensock); RTPCLOSE(abortdesc[0]); return ERR_RTP_FAKETRANS_CANTCREATEABORTDESCRIPTORS; } memset(&addr,0,sizeof(struct sockaddr_in)); addr.sin_family = AF_INET; addr.sin_addr.s_addr = inet_addr("127.0.0.1"); addr.sin_port = htons(connectport); if (connect(abortdesc[0],(struct sockaddr *)&addr,sizeof(struct sockaddr_in)) != 0) { RTPCLOSE(listensock); RTPCLOSE(abortdesc[0]); return ERR_RTP_FAKETRANS_CANTCREATEABORTDESCRIPTORS; } memset(&addr,0,sizeof(struct sockaddr_in)); size = sizeof(struct sockaddr_in); abortdesc[1] = accept(listensock,(struct sockaddr *)&addr,&size); if (abortdesc[1] == RTPSOCKERR) { RTPCLOSE(listensock); RTPCLOSE(abortdesc[0]); return ERR_RTP_FAKETRANS_CANTCREATEABORTDESCRIPTORS; } // okay, got the connection, close the listening socket RTPCLOSE(listensock); return 0;*/}void RTPFakeTransmitter::DestroyAbortDescriptors(){// RTPCLOSE(abortdesc[0]);// RTPCLOSE(abortdesc[1]);}#else // in a non winsock environment we can use pipesint RTPFakeTransmitter::CreateAbortDescriptors(){// if (pipe(abortdesc) < 0)// return ERR_RTP_FAKETRANS_CANTCREATEPIPE;// return 0;}void RTPFakeTransmitter::DestroyAbortDescriptors(){// close(abortdesc[0]);// close(abortdesc[1]);}#endif // WIN32int RTPFakeTransmitter::CreateLocalIPList(){ // first try to obtain the list from the network interface info if (!GetLocalIPList_Interfaces()) { // If this fails, we'll have to depend on DNS info GetLocalIPList_DNS(); } AddLoopbackAddress(); return 0;}//#ifdef WIN32bool RTPFakeTransmitter::GetLocalIPList_Interfaces(){ // REMINDER: got to find out how to do this return false;}/*#else // use ioctlbool RTPFakeTransmitter::GetLocalIPList_Interfaces(){ int status; char buffer[RTPFakeTRANS_IFREQBUFSIZE]; struct ifconf ifc; struct ifreq *ifr; struct sockaddr *sa; char *startptr,*endptr; int remlen; ifc.ifc_len = RTPFakeTRANS_IFREQBUFSIZE; ifc.ifc_buf = buffer; status = ioctl(rtpsock,SIOCGIFCONF,&ifc); if (status < 0) return false; startptr = (char *)ifc.ifc_req; endptr = startptr + ifc.ifc_len; remlen = ifc.ifc_len; while((startptr < endptr) && remlen >= (int)sizeof(struct ifreq)) { ifr = (struct ifreq *)startptr; sa = &(ifr->ifr_addr);#ifdef RTP_HAVE_SOCKADDR_LEN if (sa->sa_len <= sizeof(struct sockaddr)) { if (sa->sa_len == sizeof(struct sockaddr_in) && sa->sa_family == PF_INET) { uint32_t ip; struct sockaddr_in *addr = (struct sockaddr_in *)sa; ip = ntohl(addr->sin_addr.s_addr); localIPs.push_back(ip); } remlen -= sizeof(struct ifreq); startptr += sizeof(struct ifreq); } else { int l = sa->sa_len-sizeof(struct sockaddr)+sizeof(struct ifreq); remlen -= l; startptr += l; }#else // don't have sa_len in struct sockaddr if (sa->sa_family == PF_INET) { uint32_t ip; struct sockaddr_in *addr = (struct sockaddr_in *)sa; ip = ntohl(addr->sin_addr.s_addr); localIPs.push_back(ip); } remlen -= sizeof(struct ifreq); startptr += sizeof(struct ifreq); #endif // RTP_HAVE_SOCKADDR_LEN } if (localIPs.empty()) return false; return true;}#endif // WIN32*/void RTPFakeTransmitter::GetLocalIPList_DNS(){ struct hostent *he; char name[1024]; uint32_t ip; bool done; int i,j; gethostname(name,1023); name[1023] = 0; he = gethostbyname(name); if (he == 0) return; ip = 0; i = 0; done = false; while (!done) { if (he->h_addr_list[i] == NULL) done = true; else { ip = 0; for (j = 0 ; j < 4 ; j++) ip |= ((uint32_t)((unsigned char)he->h_addr_list[i][j])<<((3-j)*8)); localIPs.push_back(ip); i++; } }}void RTPFakeTransmitter::AbortWaitInternal(){/*#ifdef WIN32 send(abortdesc[1],"*",1,0);#else write(abortdesc[1],"*",1);#endif // WIN32*/}void RTPFakeTransmitter::AddLoopbackAddress(){ uint32_t loopbackaddr = (((uint32_t)127)<<24)|((uint32_t)1); std::list<uint32_t>::const_iterator it; bool found = false; for (it = localIPs.begin() ; !found && it != localIPs.end() ; it++) { if (*it == loopbackaddr) found = true; } if (!found) localIPs.push_back(loopbackaddr);}#ifdef RTPDEBUGvoid RTPFakeTransmitter::Dump(){ if (!init) std::cout << "Not initialized" << std::endl; else { MAINMUTEX_LOCK if (!created) std::cout << "Not created" << std::endl; else { char str[16]; uint32_t ip; std::list<uint32_t>::const_iterator it; std::cout << "Portbase: " << params->GetPortbase() << std::endl; std::cout << "Local IP addresses:" << std::endl; for (it = localIPs.begin() ; it != localIPs.end() ; it++) { ip = (*it); snprintf(str,16,"%d.%d.%d.%d",(int)((ip>>24)&0xFF),(int)((ip>>16)&0xFF),(int)((ip>>8)&0xFF),(int)(ip&0xFF)); std::cout << " " << str << std::endl; }// std::cout << "Multicast TTL: " << (int)multicastTTL << std::endl; std::cout << "Receive mode: "; switch (receivemode) { case RTPTransmitter::AcceptAll: std::cout << "Accept all"; break; case RTPTransmitter::AcceptSome: std::cout << "Accept some"; break; case RTPTransmitter::IgnoreSome: std::cout << "Ignore some"; } std::cout << std::endl; if (receivemode != RTPTransmitter::AcceptAll) { acceptignoreinfo.GotoFirstElement(); while(acceptignoreinfo.HasCurrentElement()) { ip = acceptignoreinfo.GetCurrentKey(); snprintf(str,16,"%d.%d.%d.%d",(int)((ip>>24)&0xFF),(int)((ip>>16)&0xFF),(int)((ip>>8)&0xFF),(int)(ip&0xFF)); PortInfo *pinfo = acceptignoreinfo.GetCurrentElement(); std::cout << " " << str << ": "; if (pinfo->all) { std::cout << "All ports"; if (!pinfo->portlist.empty()) std::cout << ", except "; } std::list<uint16_t>::const_iterator it; for (it = pinfo->portlist.begin() ; it != pinfo->portlist.end() ; ) { std::cout << (*it); it++; if (it != pinfo->portlist.end()) std::cout << ", "; } std::cout << std::endl; } } std::cout << "Local host name: "; if (localhostname == 0) std::cout << "Not set"; else std::cout << localhostname; std::cout << std::endl; std::cout << "List of destinations: "; destinations.GotoFirstElement(); if (destinations.HasCurrentElement()) { std::cout << std::endl; do { std::cout << " " << destinations.GetCurrentElement().GetDestinationString() << std::endl; destinations.GotoNextElement(); } while (destinations.HasCurrentElement()); } else std::cout << "Empty" << std::endl; std::cout << "Supports multicasting: " << ((supportsmulticasting)?"Yes":"No") << std::endl;#ifdef RTP_SUPPORT_IPV4MULTICAST/* std::cout << "List of multicast groups: "; multicastgroups.GotoFirstElement(); if (multicastgroups.HasCurrentElement()) { std::cout << std::endl; do { ip = multicastgroups.GetCurrentElement(); snprintf(str,16,"%d.%d.%d.%d",(int)((ip>>24)&0xFF),(int)((ip>>16)&0xFF),(int)((ip>>8)&0xFF),(int)(ip&0xFF)); std::cout << " " << str << std::endl; multicastgroups.GotoNextElement(); } while (multicastgroups.HasCurrentElement()); } else std::cout << "Empty" << std::endl;*/#endif // RTP_SUPPORT_IPV4MULTICAST std::cout << "Number of raw packets in queue: " << rawpacketlist.size() << std::endl; std::cout << "Maximum allowed packet size: " << maxpacksize << std::endl; std::cout << "RTP packet count: " << rtppackcount << std::endl; std::cout << "RTCP packet count: " << rtcppackcount << std::endl; } MAINMUTEX_UNLOCK }}#endif // RTPDEBUG
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -