예제 #1
0
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);
}
예제 #2
0
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);
}
예제 #3
0
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;
}
예제 #4
0
void
SocketBase::Bind(int iLocalPort)
{
    SocketAddress cLocal;

    cLocal.SetIPAddressWildCard(bIPv6Socket);
    cLocal.SetPortNumber(iLocalPort);

    WaitMutex();

    if (bind(iSockDesc, (sockaddr *)cLocal, cLocal.SizeOf()) < 0)
    {
        ClearMutex();
        switch (errno)
        {
            case EBADF:
            case ENOTSOCK:      throw SocketException(SocketException::errBadDescriptor);
            case EINVAL:        throw SocketException(SocketException::errAlreadyBound);
            case EACCES:        throw SocketException(SocketException::errAddressProtected);
            case EADDRINUSE:    throw SocketException(SocketException::errAddrInUse);
            default:            throw SocketException(SocketException::errUnknown, errno);
        }
    }
    ClearMutex();
}
예제 #5
0
void
SocketBase::Connect(IPAddress &cServAddr, int iServPortNo)
{
    SocketAddress cServer;

    cServer.SetIPAddress(cServAddr);
    cServer.SetPortNumber(iServPortNo);

    WaitMutex();

    if (connect(iSockDesc, (sockaddr *) cServer, cServer.SizeOf()) < 0)
    {
        ClearMutex();
        switch (errno)
        {
            case EBADF:
            case ENOTSOCK:      throw SocketException(SocketException::errBadDescriptor);
            case EFAULT:        throw SocketException(SocketException::errIllegalPointer);
            case EISCONN:       throw SocketException(SocketException::errAlreadyConnected);
            case ECONNREFUSED:  throw SocketException(SocketException::errConnectRefused);
            case ETIMEDOUT:     throw SocketException(SocketException::errConnectTimeOut);
            case ENETUNREACH:   throw SocketException(SocketException::errNetUnreachable);
            case EHOSTUNREACH:  throw SocketException(SocketException::errHostUnreachable);
            case EADDRINUSE:    throw SocketException(SocketException::errAddrInUse);
            case EINPROGRESS:   throw SocketException(SocketException::errInProgress);
            case EALREADY:      throw SocketException(SocketException::errAlreadyConnecting);
            case EAFNOSUPPORT:  throw SocketException(SocketException::errIncorrectAddrFamily);
            case EACCES:        throw SocketException(SocketException::errBrdCastNotEnabled);
            default:            throw SocketException(SocketException::errUnknown, errno);
        }
    }
    ClearMutex();
}
예제 #6
0
int
SocketBase::SendTo(const void *pPayload, int iPayloadLength, unsigned int uiFlags,
                   IPAddress &cDestinationIP, int iDestinationPortNumber)
{
    SocketAddress   cDestination;
    int             iBytesRead;

    cDestination.SetIPAddress(cDestinationIP);
    cDestination.SetPortNumber(iDestinationPortNumber);

    WaitMutex();

    if ((iBytesRead = sendto(iSockDesc, pPayload, iPayloadLength, uiFlags, (sockaddr *)cDestination, cDestination.SizeOf())) < 0)
    {
        ClearMutex();
        switch (errno)
        {
            case EBADF:
            case ENOTSOCK:     throw SocketException(SocketException::errBadDescriptor);
            case EAGAIN:       throw SocketException(SocketException::errWouldBlock);
            case EINTR:        throw SocketException(SocketException::errInterrupted);
            case EFAULT:       throw SocketException(SocketException::errIllegalPointer);
            case EINVAL:       throw SocketException(SocketException::errInvalidArgument);
            case EMSGSIZE:     throw SocketException(SocketException::errMessageSizeTooBig);
            case ENOBUFS:
            case ENOMEM:       throw SocketException(SocketException::errKernelMemory);
            case EHOSTUNREACH: throw SocketException(SocketException::errHostUnreachable);
            case EPIPE:        throw SocketException(SocketException::errNotConnected);
            default:           throw SocketException(SocketException::errUnknown, errno);
        }
    }
    ClearMutex();

    return iBytesRead;
}