int SocketBase::RecvFrom(void *pBuffer, int iBufLength, unsigned int uiFlags, IPAddress &cSourceIP, int &iSourcePortNumber) { SocketAddress cSource; socklen_t slDummy = cSource.SizeOf(); int iBytesRead; WaitMutex(); if ((iBytesRead = recvfrom(iSockDesc, pBuffer, iBufLength, uiFlags, (sockaddr *)cSource, &slDummy)) < 0) { ClearMutex(); switch (errno) { case EBADF: case ENOTSOCK: throw SocketException(SocketException::errBadDescriptor); case ENOTCONN: throw SocketException(SocketException::errNotConnected); case EAGAIN: throw SocketException(SocketException::errWouldBlock); case EINTR: throw SocketException(SocketException::errInterrupted); case EFAULT: throw SocketException(SocketException::errIllegalPointer); case EINVAL: throw SocketException(SocketException::errInvalidArgument); default: throw SocketException(SocketException::errUnknown, errno); } } ClearMutex(); if (iBytesRead) { cSource.GetIPAddress(cSourceIP); cSource.GetPortNumber(iSourcePortNumber); return iBytesRead; } throw SocketException(SocketException::errNotConnected); }
void SocketBase::GetPeerName(IPAddress &cRemoteAddr, int &iRemotePort) { SocketAddress cRemote; socklen_t slDummy = cRemote.SizeOf(); WaitMutex(); if (getpeername(iSockDesc, (sockaddr *)cRemote, &slDummy) < 0) { ClearMutex(); switch (errno) { case EBADF: case ENOTSOCK: throw SocketException(SocketException::errBadDescriptor); case ENOBUFS: throw SocketException(SocketException::errKernelMemory); case EFAULT: throw SocketException(SocketException::errIllegalPointer); default: throw SocketException(SocketException::errUnknown, errno); } } ClearMutex(); cRemote.GetIPAddress(cRemoteAddr); cRemote.GetPortNumber(iRemotePort); }
int SocketBase::protAccept(IPAddress &cRemoteAddr, int &iRemotePortNo) { int iNewSockDesc; SocketAddress cRemote; socklen_t slDummy = cRemote.SizeOf(); WaitMutex(); if ((iNewSockDesc = accept(iSockDesc, (sockaddr *) cRemote, &slDummy)) < 0) { ClearMutex(); switch (errno) { case EBADF: case ENOTSOCK: throw SocketException(SocketException::errBadDescriptor); case EAFNOSUPPORT: throw SocketException(SocketException::errNotStreamSock); case EFAULT: throw SocketException(SocketException::errIllegalPointer); case EAGAIN: throw SocketException(SocketException::errNoPendingConnections); case EPERM: throw SocketException(SocketException::errFirewall); case ENOMEM: throw SocketException(SocketException::errMemory); default: throw SocketException(SocketException::errUnknown, errno); } } ClearMutex(); cRemote.GetIPAddress(cRemoteAddr); cRemote.GetPortNumber(iRemotePortNo); return iNewSockDesc; }