// Send data to remote party // Put a debug message on failure // Return true if all bytes were sent static bool sendData(Socket& sock, const SocketAddr& to, const void* data, int len, const char* what, bool& flag) { if (!sock.valid()) return false; if (!to.valid()) { if (flag) { flag = false; SocketAddr local; sock.getSockName(local); Debug(DebugNote,"%s send failed (local=%s): invalid remote address", what,local.addr().c_str()); } return false; } int wr = sock.sendTo(data,len,to); if (wr == Socket::socketError() && flag && !sock.canRetry()) { flag = false; // Retrieve the error before calling getSockName() to avoid reset String s; int e = sock.error(); Thread::errorString(s,e); SocketAddr local; sock.getSockName(local); Debug(DebugNote,"%s send failed (local=%s remote=%s): %d %s", what,local.addr().c_str(),to.addr().c_str(),e,s.c_str()); } return wr == len; }
bool SocketAddr::local(const SocketAddr& remote) { if (!remote.valid()) return false; SocketAddr tmp(remote); if (!tmp.port()) tmp.port(16384); Socket sock(tmp.family(),SOCK_DGRAM); if (sock.valid() && sock.connect(tmp) && sock.getSockName(*this)) { port(0); return true; } return false; }
bool RTPTransport::remoteAddr(SocketAddr& addr, bool sniff) { Lock lock(group()); m_autoRemote = sniff; int p = addr.port(); // make sure we have a valid address and a port // we do not check that it's even numbered as many NAPTs will break that if (p && addr.valid()) { m_warnSendErrorRtp = true; m_warnSendErrorRtcp = true; m_remoteAddr = addr; m_remoteRTCP = addr; m_remoteRTCP.port(addr.port()+1); // if sniffing packets from other sources remember preferred address if (sniff) m_remotePref = addr; setScopeId(m_localAddr,m_remoteAddr,m_remoteRTCP,sniff ? &m_remotePref : 0); return true; } return false; }