int ACE_Name_Request::encode (void *&buf) { ACE_TRACE ("ACE_Name_Request::encode"); // Compute the length *before* doing the marshaling. ACE_UINT32 len = this->length (); size_t nv_data_len = (this->transfer_.name_len_ + this->transfer_.value_len_) / sizeof (ACE_WCHAR_T); for (size_t i = 0; i < nv_data_len; i++) this->transfer_.data_[i] = ACE_HTONS (this->transfer_.data_[i]); buf = (void *) &this->transfer_; this->transfer_.block_forever_ = ACE_HTONL (this->transfer_.block_forever_); this->transfer_.usec_timeout_ = ACE_HTONL (this->transfer_.usec_timeout_); #if defined (ACE_LITTLE_ENDIAN) ACE_UINT64 secs = this->transfer_.sec_timeout_; ACE_CDR::swap_8 ((const char *)&secs, (char *)&this->transfer_.sec_timeout_); #endif this->transfer_.length_ = ACE_HTONL (this->transfer_.length_); this->transfer_.msg_type_ = ACE_HTONL (this->transfer_.msg_type_); this->transfer_.name_len_ = ACE_HTONL (this->transfer_.name_len_); this->transfer_.value_len_ = ACE_HTONL (this->transfer_.value_len_); this->transfer_.type_len_ = ACE_HTONL (this->transfer_.type_len_); return len; }
void ACE_INET_Addr::set_port_number (u_short port_number, int encode) { ACE_TRACE ("ACE_INET_Addr::set_port_number"); if (encode) port_number = ACE_HTONS (port_number); #if defined (ACE_HAS_IPV6) if (this->get_type () == AF_INET6) this->inet_addr_.in6_.sin6_port = port_number; else #endif /* ACE_HAS_IPV6 */ this->inet_addr_.in4_.sin_port = port_number; if (this->inet_addrs_.empty ()) return; for (std::vector<union ip46>::iterator i = this->inet_addrs_.begin (); i != this->inet_addrs_.end (); i++) { #if defined (ACE_HAS_IPV6) if (this->get_type () == AF_INET6) i->in6_.sin6_port = port_number; else #endif /* ACE_HAS_IPV6 */ i->in4_.sin_port = port_number; } }
int CRtpPacket::SetSeqNum(unsigned short usSeqNum) { if (NULL == m_pFixedHead) { //BP_RUN_LOG_ERR(UNDEF_ERRCODE, "CRtpPacket::SetSeqNum","Rtp packet set seqnum fail packet is null."); return NRU_FAIL; } m_pFixedHead->seq_no = ACE_HTONS(usSeqNum); return NRU_SUCCESS; }
// 生成扩展字段 y00196893 add int CRtpPacket::GenerateExtensionPacket(char* pRtpPacket, unsigned int ulLen) { if ((NULL == pRtpPacket) || (sizeof(RTP_EXTEND_HEADER) > ulLen)) { //BP_RUN_LOG_ERR(UNDEF_ERRCODE, "CRtpPacket::GeneratePacket","Rtp packet generate packet fail packet is null."); return NRU_FAIL; } memset(pRtpPacket, 0x0, ulLen); m_pRtpData = pRtpPacket; m_ulPacketLen += ulLen; // 所有项设置一个默认值,外层不想改变默认值的可以不用再设置 m_pExtHead = (RTP_EXTEND_HEADER*)(void*)m_pRtpData; m_pExtHead->usProfile = ACE_HTONS(0x4857);//RTP_WARTERMARK_TYPE; m_pExtHead->usLength = ACE_HTONS((ulLen - sizeof(RTP_EXTEND_HEADER)) / RTP_EXTEND_PROFILE_LEN); m_pExtData = (RTP_EXTENSION_DATA_S*)(void*)(pRtpPacket + sizeof(RTP_EXTEND_HEADER)); //此结构比m_pExtData可以多获取一个字段信息.旧摄像头仍使用m_pExtData扩展头结构,故保留 m_pExtDataHaveFillIn = (RTP_EXTENSION_DATA_S_EX*)(void*)(pRtpPacket + sizeof(RTP_EXTEND_HEADER)); m_pExtDataHaveFillIn->ulEncryptExLen = 0; //置默认值.默认补齐长度为0 //如果扩展长度正好与平台扩展长度一致,则指向扩展长度 if(sizeof(RTP_EXTEND_HEADER) + sizeof(RTP_EXTENSION_DATA_MU_S) == ulLen) { m_pMuExtData = (RTP_EXTENSION_DATA_MU_S*)(void*)(pRtpPacket + sizeof(RTP_EXTEND_HEADER)); m_pMuExtData->ulEncryptExLen = 0; //置默认值.默认补齐长度为0 } m_ulHeadLen += ulLen; return NRU_SUCCESS; }
void ACE_INET_Addr::set_port_number (u_short port_number, int encode) { ACE_TRACE ("ACE_INET_Addr::set_port_number"); if (encode) port_number = ACE_HTONS (port_number); #if defined (ACE_HAS_IPV6) if (this->get_type () == AF_INET6) this->inet_addr_.in6_.sin6_port = port_number; else #endif /* ACE_HAS_IPV6 */ this->inet_addr_.in4_.sin_port = port_number; }
static int get_port_number_from_name (const char port_name[], const char protocol[]) { // Maybe port_name is directly a port number? char *endp = 0; long port_number = ACE_OS::strtol (port_name, &endp, 10); if (*endp == '\0') { // port_name was really a number, and nothing else. // Check for overflow. if (port_number < 0 || port_number > ACE_MAX_DEFAULT_PORT) return -1; // Return the port number. NOTE: this number must // be returned in network byte order! u_short n = static_cast<u_short> (port_number); n = ACE_HTONS (n); return n; } // We try to resolve port number from its name. #if defined (ACE_LACKS_GETSERVBYNAME) port_number = 0; ACE_UNUSED_ARG (port_name); ACE_UNUSED_ARG (protocol); #else port_number = -1; servent sentry; ACE_SERVENT_DATA buf; servent *sp = ACE_OS::getservbyname_r (port_name, protocol, &sentry, buf); if (sp != 0) port_number = sp->s_port; #endif /* ACE_LACKS_GETSERVBYNAME */ return port_number; }
static int get_port_number_from_name (const char port_name[], const char protocol[]) { int port_number = 0; // Maybe port_name is directly a port number? char *endp = 0; port_number = static_cast<int> (ACE_OS::strtol (port_name, &endp, 10)); if (port_number >= 0 && *endp == '\0') { // Ok, port_name was really a number, and nothing else. We // store that value as the port number. NOTE: this number must // be returned in network byte order! u_short n = static_cast<u_short> (port_number); n = ACE_HTONS (n); return n; } // We try to resolve port number from its name. #if defined (ACE_LACKS_GETSERVBYNAME) port_number = 0; ACE_UNUSED_ARG (port_name); ACE_UNUSED_ARG (protocol); #else port_number = -1; servent sentry; ACE_SERVENT_DATA buf; servent *sp = ACE_OS::getservbyname_r (port_name, protocol, &sentry, buf); if (sp != 0) port_number = sp->s_port; #endif /* ACE_LACKS_GETSERVBYNAME */ return port_number; }
int run_main (int argc, ACE_TCHAR *argv[]) { ACE_UNUSED_ARG (argc); ACE_UNUSED_ARG (argv); ACE_START_TEST (ACE_TEXT ("INET_Addr_Test")); int status = 0; // Innocent until proven guilty const char *ipv4_addresses[] = { "127.0.0.1", "138.38.180.251", "64.219.54.121", "192.0.0.1", "10.0.0.1", 0 }; ACE_INET_Addr addr; status |= check_type_consistency (addr); char hostaddr[1024]; for (int i=0; ipv4_addresses[i] != 0; i++) { struct in_addr addrv4; ACE_UINT32 addr32; ACE_OS::inet_pton (AF_INET, ipv4_addresses[i], &addrv4); ACE_OS::memcpy (&addr32, &addrv4, sizeof (addr32)); addr.set (80, ipv4_addresses[i]); status |= check_type_consistency (addr); /* ** Now check to make sure get_ip_address matches and get_host_addr ** matches. */ if (addr.get_ip_address () != ACE_HTONL (addr32)) { ACE_ERROR ((LM_ERROR, ACE_TEXT ("Error: %s failed get_ip_address() check\n") ACE_TEXT ("0x%x != 0x%x\n"), ipv4_addresses[i], addr.get_ip_address (), addr32)); status = 1; } if (addr.get_host_addr () != 0 && ACE_OS::strcmp (addr.get_host_addr(), ipv4_addresses[i]) != 0) { ACE_ERROR ((LM_ERROR, ACE_TEXT ("%s failed get_host_addr() check\n") ACE_TEXT ("%s != %s\n"), ipv4_addresses[i], addr.get_host_addr (), ipv4_addresses[i])); status = 1; } // Now we check the operation of get_host_addr(char*,int) const char* haddr = addr.get_host_addr (&hostaddr[0], sizeof(hostaddr)); if (haddr != 0 && ACE_OS::strcmp (&hostaddr[0], haddr) != 0) { ACE_ERROR ((LM_ERROR, ACE_TEXT ("%s failed get_host_addr(char* buf,int) check\n") ACE_TEXT ("buf ['%s'] != return value ['%s']\n"), ipv4_addresses[i], &hostaddr[0], haddr)); status = 1; } if (ACE_OS::strcmp (&hostaddr[0], ipv4_addresses[i]) != 0) { ACE_ERROR ((LM_ERROR, ACE_TEXT ("%s failed get_host_addr(char*,int) check\n") ACE_TEXT ("buf ['%s'] != expected value ['%s']\n"), ipv4_addresses[i], &hostaddr[0], ipv4_addresses[i])); status = 1; } // Clear out the address by setting it to 1 and check addr.set (0, ACE_UINT32 (1), 1); status |= check_type_consistency (addr); if (addr.get_ip_address () != 1) { ACE_ERROR ((LM_ERROR, ACE_TEXT ("Failed to set address to 1\n"))); status = 1; } // Now set the address using a 32 bit number and check that we get // the right string out of get_host_addr(). addr.set (80, addr32, 0); // addr32 is already in network byte order status |= check_type_consistency(addr); if (addr.get_host_addr () != 0 && ACE_OS::strcmp (addr.get_host_addr (), ipv4_addresses[i]) != 0) { ACE_ERROR ((LM_ERROR, ACE_TEXT ("%s failed second get_host_addr() check\n") ACE_TEXT ("return value ['%s'] != expected value ['%s']\n"), ipv4_addresses[i], addr.get_host_addr (), ipv4_addresses[i])); status = 1; } // Test for ACE_INET_Addr::set_addr(). struct sockaddr_in sa4; sa4.sin_family = AF_INET; sa4.sin_addr = addrv4; sa4.sin_port = ACE_HTONS (8080); addr.set (0, ACE_UINT32 (1), 1); addr.set_addr (&sa4, sizeof(sa4)); status |= check_type_consistency (addr); if (addr.get_port_number () != 8080) { ACE_ERROR ((LM_ERROR, ACE_TEXT ("ACE_INET_Addr::set_addr() ") ACE_TEXT ("failed to update port number.\n"))); status = 1; } if (addr.get_ip_address () != ACE_HTONL (addr32)) { ACE_ERROR ((LM_ERROR, ACE_TEXT ("ACE_INET_Addr::set_addr() ") ACE_TEXT ("failed to update address.\n"))); status = 1; } } #if defined (ACE_HAS_IPV6) if (ACE::ipv6_enabled ()) { const char *ipv6_addresses[] = { "1080::8:800:200c:417a", // unicast address "ff01::101", // multicast address "::1", // loopback address "::", // unspecified addresses 0 }; for (int i=0; ipv6_addresses[i] != 0; i++) { ACE_INET_Addr addr (80, ipv6_addresses[i]); status |= check_type_consistency (addr); if (0 != ACE_OS::strcmp (addr.get_host_addr (), ipv6_addresses[i])) { ACE_ERROR ((LM_ERROR, ACE_TEXT ("IPv6 get_host_addr failed: %s != %s\n"), addr.get_host_addr (), ipv6_addresses[i])); status = 1; } } } #endif struct Address loopback_addresses[] = { {"127.0.0.1", true}, {"127.1.2.3", true} , {"127.0.0.0", true}, {"127.255.255.255", true} , {"126.255.255.255", false}, {"128.0.0.0", false}, {0, true} }; for (int i=0; loopback_addresses[i].name != 0; i++) { struct in_addr addrv4; ACE_UINT32 addr32 = 0; ACE_OS::inet_pton (AF_INET, loopback_addresses[i].name, &addrv4); ACE_OS::memcpy (&addr32, &addrv4, sizeof (addr32)); addr.set (80, loopback_addresses[i].name); if (addr.is_loopback() != loopback_addresses[i].loopback) { ACE_ERROR ((LM_ERROR, ACE_TEXT ("ACE_INET_Addr::is_loopback() ") ACE_TEXT ("failed to distinguish loopback address. %s\n") , loopback_addresses[i].name)); status = 1; } } ACE_END_TEST; return status; }
int ACE_TMAIN (int argc, ACE_TCHAR *argv[]) { // Initialize WinSock DLL on Win32... ACE_OS::socket_init (ACE_WSOCK_VERSION); struct sockaddr_in saddr; struct hostent *hp; const ACE_TCHAR *host = argc > 1 ? argv[1] : ACE_DEFAULT_SERVER_HOST; u_short port_num = ACE_HTONS (argc > 2 ? ACE_OS::atoi (argv[2]) : ACE_DEFAULT_SERVER_PORT); int sockbufsize = argc > 3 ? ACE_OS::atoi (argv[3]) : 0; char buf[BUFSIZ]; ACE_HANDLE s_handle; int w_bytes; int r_bytes; int n; // Create a local endpoint of communication. if ((s_handle = ACE_OS::socket (PF_INET, SOCK_STREAM, 0)) == ACE_INVALID_HANDLE) ACE_OS::perror (ACE_TEXT("socket")), ACE_OS::exit (1); // If a sockbufsize was specified, set it for both send and receive. if (sockbufsize > 0) { if (ACE_OS::setsockopt (s_handle, SOL_SOCKET, SO_SNDBUF, (const char *) &sockbufsize, sizeof (sockbufsize)) != 0) ACE_OS::perror (ACE_TEXT("SO_SNDBUF")), ACE_OS::exit (1); if (ACE_OS::setsockopt (s_handle, SOL_SOCKET, SO_RCVBUF, (const char *) &sockbufsize, sizeof (sockbufsize)) != 0) ACE_OS::perror (ACE_TEXT("SO_RCVBUF")), ACE_OS::exit (1); } // Determine IP address of the server. if ((hp = ACE_OS::gethostbyname (ACE_TEXT_ALWAYS_CHAR(host))) == 0) ACE_OS::perror (ACE_TEXT("gethostbyname")), ACE_OS::exit (1); // Set up the address information to contact the server. ACE_OS::memset ((void *) &saddr, 0, sizeof saddr); saddr.sin_family = AF_INET; saddr.sin_port = port_num; ACE_OS::memcpy (&saddr.sin_addr, hp->h_addr, hp->h_length); // Establish connection with remote server. if (ACE_OS::connect (s_handle, reinterpret_cast<sockaddr *> (&saddr), sizeof saddr) == -1) ACE_OS::perror (ACE_TEXT("connect")), ACE_OS::exit (1); // Send data to server (correctly handles "incomplete writes" due to // flow control). while ((r_bytes = ACE_OS::read (ACE_STDIN, buf, sizeof buf)) > 0) for (w_bytes = 0; w_bytes < r_bytes; w_bytes += n) if ((n = ACE_OS::send (s_handle, buf + w_bytes, r_bytes - w_bytes)) < 0) ACE_OS::perror (ACE_TEXT("write")), ACE_OS::exit (1); if (ACE_OS::recv (s_handle, buf, 1) == 1) ACE_OS::write (ACE_STDOUT, buf, 1); // Explicitly close the connection. if (ACE_OS::closesocket (s_handle) == -1) ACE_OS::perror (ACE_TEXT("close")), ACE_OS::exit (1); return 0; }
int InfoRepoMulticastResponder::handle_input(ACE_HANDLE) { if (OpenDDS::DCPS::DCPS_debug_level > 0) ACE_DEBUG((LM_DEBUG, "Entered InfoRepoMulticastResponder::handle_input\n")); // The length of the service name string that follows. CORBA::Short header; // Port to which to reply. ACE_UINT16 remote_port; // Name of the service for which the client is looking. char object_key[BUFSIZ]; ACE_INET_Addr remote_addr; // Take a peek at the header to find out how long is the service // name string we should receive. ssize_t n = this->mcast_dgram_.recv(&header, sizeof(header), remote_addr, MSG_PEEK); if (n <= 0) ACE_ERROR_RETURN((LM_ERROR, "InfoRepoMulticastResponder::handle_input - peek %d\n", n), 0); else if (ACE_NTOHS(header) <= 0) ACE_ERROR_RETURN((LM_ERROR, "InfoRepoMulticastResponder::handle_input() Header value < 1\n"), 0); // Receive full client multicast request. const int iovcnt = 3; iovec iov[iovcnt]; iov[0].iov_base = (char *) &header; iov[0].iov_len = sizeof(header); iov[1].iov_base = (char *) &remote_port; iov[1].iov_len = sizeof(ACE_UINT16); iov[2].iov_base = (char *) object_key; iov[2].iov_len = ACE_NTOHS(header); // Read the iovec. n = this->mcast_dgram_.recv(iov, iovcnt, remote_addr); if (n <= 0) ACE_ERROR_RETURN((LM_ERROR, "InfoRepoMulticastResponder::handle_input recv = %d\n", n), 0); if (OpenDDS::DCPS::DCPS_debug_level > 0) { ACE_TCHAR addr[64]; remote_addr.addr_to_string(addr, sizeof(addr)); ACE_DEBUG((LM_DEBUG, "(%P|%t) Received multicast from %s.\n" "Service Name received : %C\n" "Port received : %u\n", addr, object_key, ACE_NTOHS(remote_port))); } // Grab the IOR table. CORBA::Object_var table_object = orb_->resolve_initial_references("IORTable"); IORTable::Locator_var locator = IORTable::Locator::_narrow(table_object.in()); if (CORBA::is_nil(locator.in())) { ACE_ERROR((LM_ERROR, ACE_TEXT("Nil IORTable\n"))); } std::string ior; { CORBA::String_var ior_result; try { ior_result = locator->locate(object_key); } catch (const IORTable::NotFound&) { ACE_ERROR_RETURN((LM_ERROR, "InfoRepoMulticastResponder::handle_input() Object key not found\n"), 0); } ior = ior_result; } // Reply to the multicast message. ACE_SOCK_Connector connector; ACE_INET_Addr peer_addr; ACE_SOCK_Stream stream; peer_addr.set(remote_addr); peer_addr.set_port_number(ACE_NTOHS(remote_port)); #if defined (ACE_HAS_IPV6) if (peer_addr.is_linklocal()) { // If this is one of our local linklocal interfaces this is not going // to work. // Creating a connection using such interface to the client listening // at the IPv6 ANY address is not going to work (I'm not quite sure why // but it probably has to do with the rather restrictive routing rules // for linklocal interfaces). // So we see if this is one of our local interfaces and if so create the // connection using the IPv6 loopback address instead. ACE_INET_Addr peer_tmp(peer_addr); peer_tmp.set_port_number(static_cast<u_short>(0)); ACE_INET_Addr* tmp = 0; size_t cnt = 0; int err = ACE::get_ip_interfaces(cnt, tmp); if (err == 0) { for (size_t i = 0; i < cnt; ++i) { if (peer_tmp == tmp[i]) { peer_addr.set(ACE_NTOHS(remote_port), ACE_IPV6_LOCALHOST); break; } } delete[] tmp; } } #endif /* ACE_HAS_IPV6 */ if (OpenDDS::DCPS::DCPS_debug_level > 0) { ACE_TCHAR addr[64]; peer_addr.addr_to_string(addr, sizeof(addr)); ACE_DEBUG((LM_DEBUG, "(%P|%t) Replying to peer %s.\n", addr)); } // Connect. if (connector.connect(stream, peer_addr) == -1) ACE_ERROR_RETURN((LM_ERROR, "InfoRepoMulticastResponder::connect failed\n"), 0); // Send the IOR back to the client. (Send iovec, which contains ior // length as the first element, and ior itself as the second.) // Length of ior to be sent. CORBA::Short data_len = static_cast<CORBA::Short>(ACE_HTONS(ior.length() + 1)); // Vector to be sent. const int cnt = 2; iovec iovp[cnt]; // The length of ior to be sent. iovp[0].iov_base = (char *) &data_len; iovp[0].iov_len = sizeof(CORBA::Short); // The ior. iovp[1].iov_base = const_cast<char*>(ior.c_str()); iovp[1].iov_len = static_cast<u_long>(ior.length() + 1); ssize_t result = stream.sendv_n(iovp, cnt); // Close the stream. stream.close(); // Check for error. if (result == -1) ACE_ERROR_RETURN((LM_ERROR, "InfoRepoMulticastResponder::send failed\n"), 0); if (OpenDDS::DCPS::DCPS_debug_level > 0) ACE_DEBUG((LM_DEBUG, "(%P|%t) InfoRepoMulticastResponder::handle_input() ior: <%C>\n" "sent to %C:%u.\n" "result from send = %d\n", ior.c_str(), peer_addr.get_host_name(), peer_addr.get_port_number(), result)); return 0; }
int ACE_TMAIN (int argc, ACE_TCHAR *argv[]) { // Initialize WinSock DLL on Win32... ACE_OS::socket_init (ACE_WSOCK_VERSION); u_short port_num = ACE_HTONS (argc > 1 ? ACE_OS::atoi (argv[1]) : ACE_DEFAULT_SERVER_PORT); int sockbufsize = argc > 2 ? ACE_OS::atoi (argv[2]) : 0; struct sockaddr_in saddr; ACE_HANDLE s_handle, n_handle; /* Create a local endpoint of communication */ if ((s_handle = ACE_OS::socket (PF_INET, SOCK_STREAM, 0)) == ACE_INVALID_HANDLE) ACE_OS::perror (ACE_TEXT("socket")), ACE_OS::exit (1); // If a sockbufsize was specified, set it for both send and receive. if (sockbufsize > 0) { if (ACE_OS::setsockopt (s_handle, SOL_SOCKET, SO_SNDBUF, (const char *) &sockbufsize, sizeof (sockbufsize)) != 0) ACE_OS::perror (ACE_TEXT("SO_SNDBUF")), ACE_OS::exit (1); if (ACE_OS::setsockopt (s_handle, SOL_SOCKET, SO_RCVBUF, (const char *) &sockbufsize, sizeof (sockbufsize)) != 0) ACE_OS::perror (ACE_TEXT("SO_RCVBUF")), ACE_OS::exit (1); } /* Set up the address information to become a server */ ACE_OS::memset ((void *) &saddr, 0, sizeof saddr); saddr.sin_family = AF_INET; saddr.sin_port = port_num; saddr.sin_addr.s_addr = INADDR_ANY; /* Associate address with endpoint */ if (ACE_OS::bind (s_handle, reinterpret_cast<struct sockaddr *> (&saddr), sizeof saddr) == -1) ACE_OS::perror (ACE_TEXT("bind")), ACE_OS::exit (1); /* Make endpoint listen for service requests */ if (ACE_OS::listen (s_handle, 5) == -1) ACE_OS::perror (ACE_TEXT("listen")), ACE_OS::exit (1); /* Performs the iterative server activities */ for (;;) { char buf[BUFSIZ]; int r_bytes; struct sockaddr_in cli_addr; int cli_addr_len = sizeof cli_addr; struct hostent *hp; /* Create a new endpoint of communication */ do n_handle = ACE_OS::accept (s_handle, reinterpret_cast<struct sockaddr *> (&cli_addr), &cli_addr_len); while (n_handle == ACE_INVALID_HANDLE && errno == EINTR); if (n_handle == ACE_INVALID_HANDLE) { ACE_OS::perror (ACE_TEXT("accept")); continue; } #if !defined(_UNICOS) int addr_len = sizeof cli_addr.sin_addr.s_addr; #else /* ! _UNICOS */ // sizeof on bitfield fails int addr_len = sizeof cli_addr.sin_addr; // 32 bit biffield in UNICOS #endif /* ! _UNICOS */ hp = ACE_OS::gethostbyaddr ((char *) &cli_addr.sin_addr, addr_len, AF_INET); if (hp != 0) ACE_OS::printf ("client %s\n", hp->h_name), ACE_OS::fflush (stdout); else ACE_OS::perror (ACE_TEXT("gethostbyaddr")); /* Read data from client (terminate on error) */ while ((r_bytes = ACE_OS::recv (n_handle, buf, sizeof buf)) > 0) if (ACE_OS::write (ACE_STDOUT, buf, r_bytes) != r_bytes) ACE_OS::perror (ACE_TEXT("write")), ACE_OS::exit (1); if (ACE_OS::send (n_handle, "", 1) != 1) ACE_OS::perror ("write"), ACE_OS::exit (1); /* Close the new endpoint (listening endpoint remains open) */ if (ACE_OS::closesocket (n_handle) == -1) ACE_OS::perror (ACE_TEXT("close")), ACE_OS::exit (1); ACE_OS::exit (0); } ACE_NOTREACHED (return 0;) }
int CRtpPacket::ParsePacket ( char* pRtpData, unsigned int ulLen ) { if (NULL == pRtpData) { //BP_RUN_LOG_ERR(UNDEF_ERRCODE, "Rtp packet parse fail rtp data is null."," "); return NRU_FAIL; } // 不能小于固定头的长度, 且不能超过2000 (暂定) if (ulLen < sizeof(RTP_FIXED_HEADER) || RTP_VALID_LEN < ulLen) { //BP_RUN_LOG_ERR(UNDEF_ERRCODE, "Rtp packet parse fail rtp data.","len=%u is shorter than fixed head[%d] len.", ulLen, sizeof(RTP_FIXED_HEADER)); return NRU_FAIL; } m_pRtpData = (char*)pRtpData; m_ulPacketLen = ulLen; // 先指定固定头 m_pFixedHead = (RTP_FIXED_HEADER*)(void*)m_pRtpData; unsigned int ulHeadLen = sizeof(RTP_FIXED_HEADER) + m_pFixedHead->csrc_len * RTP_CSRC_LEN; if (ulLen < ulHeadLen) { //BP_RUN_LOG_ERR(UNDEF_ERRCODE, "Rtp packet parse fail rtp data len is shorter than fixed head len."," "); return NRU_FAIL; } // 检查版本号 if (NRU_SUCCESS != CheckVersion()) { //BP_RUN_LOG_ERR(UNDEF_ERRCODE, "Rtp packet parse fail check version fail."," "); return NRU_FAIL; } //检查是否有填充数据 if (1 == m_pFixedHead->padding) { if (m_ulPacketLen > (unsigned int)pRtpData[ulLen - 1]) //lint !e571 { m_ulPacketLen -= pRtpData[ulLen - 1];//lint !e737 } else { //BP_RUN_LOG_WAR("Rtp packet has padding data, but padding data len illegal.", "packet len=%u padding data len=%u", m_ulPacketLen, pRtpData[ulLen - 1]); } } m_ulFixedHeadLen = ulHeadLen; // 判断是否有扩展头 if (1 != m_pFixedHead->extension) { // 没有扩展头,直接返回 m_ulHeadLen = ulHeadLen; return NRU_SUCCESS; } //m_ulFixedHeadLen = ulHeadLen; if (ulLen < ulHeadLen + sizeof(RTP_EXTEND_HEADER)) { //BP_RUN_LOG_ERR(UNDEF_ERRCODE, "Rtp packet parse fail packet len is too short to contain extend head."," "); return NRU_FAIL; } // 指定扩展头 m_pExtHead = (RTP_EXTEND_HEADER*)(void*)(m_pRtpData + ulHeadLen); // 有扩展头,判断包的长度是否足够包含扩展头 ulHeadLen += sizeof(RTP_EXTEND_HEADER) + ACE_NTOHS(m_pExtHead->usLength) * RTP_EXTEND_PROFILE_LEN; if (ulLen < ulHeadLen) { //BP_RUN_LOG_ERR(UNDEF_ERRCODE, "Rtp packet parse fail packet len is too short."," "); return NRU_FAIL; } if (m_pExtHead->usProfile == ACE_HTONS(0x4857)) { m_pExtData = (RTP_EXTENSION_DATA_S*)(void*)(m_pRtpData + m_ulFixedHeadLen + sizeof(RTP_EXTEND_HEADER)); //根据扩展头长度判断是否前端传来的rtp扩展头,以及是否含有补齐信息字段.swx164794 add. 2013-03-09 if (sizeof(RTP_EXTENSION_DATA_S_EX) == ACE_NTOHS(m_pExtHead->usLength)* RTP_EXTEND_PROFILE_LEN) { m_pExtDataHaveFillIn = (RTP_EXTENSION_DATA_S_EX*)(void*)(m_pRtpData + m_ulFixedHeadLen + sizeof(RTP_EXTEND_HEADER)); } else if (sizeof(RTP_EXTENSION_DATA_MU_S) == ACE_NTOHS(m_pExtHead->usLength) * RTP_EXTEND_PROFILE_LEN) { m_pExtDataHaveFillIn = (RTP_EXTENSION_DATA_S_EX*)(void*)(m_pRtpData + m_ulFixedHeadLen + sizeof(RTP_EXTEND_HEADER)); m_pMuExtData = (RTP_EXTENSION_DATA_MU_S*)(void*)(m_pRtpData + m_ulFixedHeadLen + sizeof(RTP_EXTEND_HEADER)); } } // 至此,所有的头长度已经计算完毕了 m_ulHeadLen = ulHeadLen; return NRU_SUCCESS; }
int run_main (int, ACE_TCHAR *[]) { ACE_START_TEST (ACE_TEXT ("INET_Addr_Test")); int status = 0; // Innocent until proven guilty // Try to set up known IP and port. u_short port (80); ACE_UINT32 const ia_any = INADDR_ANY; ACE_INET_Addr local_addr(port, ia_any); status |= check_type_consistency (local_addr); if (local_addr.get_port_number () != 80) { ACE_ERROR ((LM_ERROR, ACE_TEXT ("Got port %d, expecting 80\n"), (int)(local_addr.get_port_number ()))); status = 1; } if (local_addr.get_ip_address () != ia_any) { ACE_ERROR ((LM_ERROR, ACE_TEXT ("Mismatch on local IP addr\n"))); status = 1; } // Assignment constructor ACE_INET_Addr local_addr2 (local_addr); status |= check_type_consistency (local_addr2); if (local_addr2.get_port_number () != 80) { ACE_ERROR ((LM_ERROR, ACE_TEXT ("Copy got port %d, expecting 80\n"), (int)(local_addr2.get_port_number ()))); status = 1; } if (local_addr2.get_ip_address () != ia_any) { ACE_ERROR ((LM_ERROR, ACE_TEXT ("Mismatch on copy local IP addr\n"))); status = 1; } if (local_addr != local_addr2) { ACE_ERROR ((LM_ERROR, ACE_TEXT ("Copy local addr mismatch\n"))); status = 1; } // Try to parse out a simple address:port string. Intentionally reuse // the ACE_INET_Addr to ensure resetting an address works. const char *addr_ports[] = { "127.0.0.1:80", "www.dre.vanderbilt.edu:80", 0 }; ACE_INET_Addr addr_port; for (int i = 0; addr_ports[i] != 0; ++i) { if (addr_port.set (addr_ports[i]) == 0) { status |= check_type_consistency (addr_port); if (addr_port.get_port_number () != 80) { ACE_ERROR ((LM_ERROR, ACE_TEXT ("Got port %d from %s\n"), (int)(addr_port.get_port_number ()), addr_ports[i])); status = 1; } ACE_INET_Addr check (addr_ports[i]); if (addr_port != check) { ACE_ERROR ((LM_ERROR, ACE_TEXT ("Reset on iter %d failed\n"), i)); status = 1; } } else { // Sometimes this fails because the run-time host lacks the capability to // resolve a name. But it shouldn't fail on the first one, 127.0.0.1. if (i == 0) { ACE_ERROR ((LM_ERROR, ACE_TEXT ("%C: %p\n"), addr_ports[i], ACE_TEXT ("lookup"))); status = 1; } else { ACE_ERROR ((LM_WARNING, ACE_TEXT ("%C: %p\n"), addr_ports[i], ACE_TEXT ("lookup"))); } } } const char *ipv4_addresses[] = { "127.0.0.1", "138.38.180.251", "64.219.54.121", "192.0.0.1", "10.0.0.1", 0 }; ACE_INET_Addr addr; status |= check_type_consistency (addr); char hostaddr[1024]; for (int i=0; ipv4_addresses[i] != 0; i++) { struct in_addr addrv4; ACE_OS::memset ((void *) &addrv4, 0, sizeof addrv4); ACE_UINT32 addr32; ACE_OS::inet_pton (AF_INET, ipv4_addresses[i], &addrv4); ACE_OS::memcpy (&addr32, &addrv4, sizeof (addr32)); status |= !(addr.set (80, ipv4_addresses[i]) == 0); status |= check_type_consistency (addr); /* ** Now check to make sure get_ip_address matches and get_host_addr ** matches. */ if (addr.get_ip_address () != ACE_HTONL (addr32)) { ACE_ERROR ((LM_ERROR, ACE_TEXT ("Error: %C failed get_ip_address() check\n") ACE_TEXT ("0x%x != 0x%x\n"), ipv4_addresses[i], addr.get_ip_address (), ACE_HTONL (addr32))); status = 1; } if (addr.get_host_addr () != 0 && ACE_OS::strcmp (addr.get_host_addr(), ipv4_addresses[i]) != 0) { ACE_ERROR ((LM_ERROR, ACE_TEXT ("%C failed get_host_addr() check\n") ACE_TEXT ("%C != %C\n"), ipv4_addresses[i], addr.get_host_addr (), ipv4_addresses[i])); status = 1; } // Now we check the operation of get_host_addr(char*,int) const char* haddr = addr.get_host_addr (&hostaddr[0], sizeof(hostaddr)); if (haddr != 0 && ACE_OS::strcmp (&hostaddr[0], haddr) != 0) { ACE_ERROR ((LM_ERROR, ACE_TEXT ("%C failed get_host_addr(char* buf,int) check\n") ACE_TEXT ("buf ['%C'] != return value ['%C']\n"), ipv4_addresses[i], &hostaddr[0], haddr)); status = 1; } if (ACE_OS::strcmp (&hostaddr[0], ipv4_addresses[i]) != 0) { ACE_ERROR ((LM_ERROR, ACE_TEXT ("%C failed get_host_addr(char*,int) check\n") ACE_TEXT ("buf ['%C'] != expected value ['%C']\n"), ipv4_addresses[i], &hostaddr[0], ipv4_addresses[i])); status = 1; } // Clear out the address by setting it to 1 and check addr.set (0, ACE_UINT32 (1), 1); status |= check_type_consistency (addr); if (addr.get_ip_address () != 1) { ACE_ERROR ((LM_ERROR, ACE_TEXT ("Failed to set address to 1\n"))); status = 1; } // Now set the address using a 32 bit number and check that we get // the right string out of get_host_addr(). addr.set (80, addr32, 0); // addr32 is already in network byte order status |= check_type_consistency(addr); if (addr.get_host_addr () != 0 && ACE_OS::strcmp (addr.get_host_addr (), ipv4_addresses[i]) != 0) { ACE_ERROR ((LM_ERROR, ACE_TEXT ("%C failed second get_host_addr() check\n") ACE_TEXT ("return value ['%C'] != expected value ['%C']\n"), ipv4_addresses[i], addr.get_host_addr (), ipv4_addresses[i])); status = 1; } // Test for ACE_INET_Addr::set_addr(). struct sockaddr_in sa4; sa4.sin_family = AF_INET; sa4.sin_addr = addrv4; sa4.sin_port = ACE_HTONS (8080); addr.set (0, ACE_UINT32 (1), 1); addr.set_addr (&sa4, sizeof(sa4)); status |= check_type_consistency (addr); if (addr.get_port_number () != 8080) { ACE_ERROR ((LM_ERROR, ACE_TEXT ("ACE_INET_Addr::set_addr() ") ACE_TEXT ("failed to update port number.\n"))); status = 1; } if (addr.get_ip_address () != ACE_HTONL (addr32)) { ACE_ERROR ((LM_ERROR, ACE_TEXT ("ACE_INET_Addr::set_addr() ") ACE_TEXT ("failed to update address.\n"))); status = 1; } } #if defined (ACE_HAS_IPV6) if (ACE::ipv6_enabled ()) { const char *ipv6_addresses[] = { "1080::8:800:200c:417a", // unicast address "ff01::101", // multicast address "::1", // loopback address "::", // unspecified addresses 0 }; for (int i=0; ipv6_addresses[i] != 0; i++) { ACE_INET_Addr addr (80, ipv6_addresses[i]); status |= check_type_consistency (addr); if (0 != ACE_OS::strcmp (addr.get_host_addr (), ipv6_addresses[i])) { ACE_ERROR ((LM_ERROR, ACE_TEXT ("IPv6 get_host_addr failed: %C != %C\n"), addr.get_host_addr (), ipv6_addresses[i])); status = 1; } } const char *ipv6_names[] = { "naboo.dre.vanderbilt.edu", "v6.ipv6-test.com", 0 }; for (int i=0; ipv6_names[i] != 0; i++) { ACE_INET_Addr addr (80, ipv6_names[i]); status |= check_type_consistency (addr); if (0 != ACE_OS::strcmp (addr.get_host_name (), ipv6_names[i])) { // Alias? Check lookup on the reverse. ACE_INET_Addr alias_check; if (alias_check.set (80, addr.get_host_name ()) == 0) { if (addr != alias_check) ACE_ERROR ((LM_WARNING, ACE_TEXT ("IPv6 name mismatch: %s (%s) != %s\n"), addr.get_host_name (), addr.get_host_addr (), ipv6_names[i])); } else { ACE_ERROR ((LM_WARNING, ACE_TEXT ("IPv6 reverse lookup mismatch: %s (%s) != %s\n"), addr.get_host_name (), addr.get_host_addr (), ipv6_names[i])); } } } } ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("IPv6 tests done\n"))); #else ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("ACE_HAS_IPV6 not set; no IPv6 tests run\n"))); #endif struct Address loopback_addresses[] = { {"127.0.0.1", true}, {"127.1.2.3", true} , {"127.0.0.0", true}, {"127.255.255.255", true} , {"126.255.255.255", false}, {"128.0.0.0", false}, {0, true} }; for (int i=0; loopback_addresses[i].name != 0; i++) { struct in_addr addrv4; ACE_UINT32 addr32 = 0; ACE_OS::inet_pton (AF_INET, loopback_addresses[i].name, &addrv4); ACE_OS::memcpy (&addr32, &addrv4, sizeof (addr32)); addr.set (80, loopback_addresses[i].name); if (addr.is_loopback() != loopback_addresses[i].loopback) { ACE_ERROR ((LM_ERROR, ACE_TEXT ("ACE_INET_Addr::is_loopback() ") ACE_TEXT ("failed to distinguish loopback address. %C\n") , loopback_addresses[i].name)); status = 1; } } if (addr.string_to_addr ("127.0.0.1:72000", AF_INET) != -1) { ACE_ERROR ((LM_ERROR, ACE_TEXT ("ACE_INET_Addr::string_to_addr() ") ACE_TEXT ("failed to detect port number overflow\n"))); status = 1; } if (!test_tao_use ()) status = 1; if (!test_multiple ()) status = 1; if (!test_port_assignment ()) status = 1; ACE_INET_Addr a1 (80, "127.0.0.1"); ACE_INET_Addr a2 = a1; if (a1 != a2) { ACE_ERROR ((LM_ERROR, ACE_TEXT ("Address equality check failed after assignment\n"))); status = 1; } ACE_END_TEST; return status; }