socket_wrapper.cpp 3.58 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/* socket_wrapper.cpp                           
 *
 * Copyright (C) 2003 Sawtooth Consulting Ltd.
 *
 * This file is part of yaSSL.
 *
 * yaSSL is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * yaSSL is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 */


/* The socket wrapper source implements a Socket class that hides the 
 * differences between Berkely style sockets and Windows sockets, allowing 
 * transparent TCP access.
 */


29
#include "runtime.hpp"
30 31
#include "socket_wrapper.hpp"

32
#ifndef _WIN32
33 34 35 36 37 38 39
    #include <errno.h>
    #include <netdb.h>
    #include <unistd.h>
    #include <arpa/inet.h>
    #include <netinet/in.h>
    #include <sys/ioctl.h>
    #include <string.h>
40
#endif // _WIN32
41

42
#if defined(__sun) || defined(__SCO_VERSION__)
43 44 45
    #include <sys/filio.h>
#endif

46
#ifdef _WIN32
47 48
    const int SOCKET_EINVAL = WSAEINVAL;
    const int SOCKET_EWOULDBLOCK = WSAEWOULDBLOCK;
49
    const int SOCKET_EAGAIN = WSAEWOULDBLOCK;
50 51 52
#else
    const int SOCKET_EINVAL = EINVAL;
    const int SOCKET_EWOULDBLOCK = EWOULDBLOCK;
53
    const int SOCKET_EAGAIN = EAGAIN;
54
#endif // _WIN32
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85


namespace yaSSL {


Socket::Socket(socket_t s) 
    : socket_(s) 
{}


void Socket::set_fd(socket_t s)
{
    socket_ = s;
}


socket_t Socket::get_fd() const
{
    return socket_;
}


Socket::~Socket()
{
    closeSocket();
}


void Socket::closeSocket()
{
    if (socket_ != INVALID_SOCKET) {
86
#ifdef _WIN32
87 88 89 90 91 92 93 94 95 96 97
        closesocket(socket_);
#else
        close(socket_);
#endif
        socket_ = INVALID_SOCKET;
    }
}


uint Socket::get_ready() const
{
98
#ifdef _WIN32
99
    unsigned long ready = 0;
100 101
    ioctlsocket(socket_, FIONREAD, &ready);
#else
102 103 104 105 106
    /*
       64-bit Solaris requires the variable passed to
       FIONREAD be a 32-bit value.
    */
    unsigned int ready = 0;
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
    ioctl(socket_, FIONREAD, &ready);
#endif

    return ready;
}


uint Socket::send(const byte* buf, unsigned int sz, int flags) const
{
    assert(socket_ != INVALID_SOCKET);
    int sent = ::send(socket_, reinterpret_cast<const char *>(buf), sz, flags);

    if (sent == -1)
        return 0;

    return sent;
}


uint Socket::receive(byte* buf, unsigned int sz, int flags) const
{
    assert(socket_ != INVALID_SOCKET);
    int recvd = ::recv(socket_, reinterpret_cast<char *>(buf), sz, flags);

131 132 133 134
    // idea to seperate error from would block by arnetheduck@gmail.com
    if (recvd == -1) {
        if (get_lastError() == SOCKET_EWOULDBLOCK || 
            get_lastError() == SOCKET_EAGAIN)
135
        return 0;
136 137 138
    }
    else if (recvd == 0)
        return static_cast<uint>(-1);
139 140 141 142 143

    return recvd;
}


144 145
// wait if blocking for input, return false for error
bool Socket::wait() const
146 147
{
    byte b;
148
    return receive(&b, 1, MSG_PEEK) != static_cast<uint>(-1);
149 150 151 152 153 154 155 156 157 158 159 160
}


void Socket::shutDown(int how)
{
    assert(socket_ != INVALID_SOCKET);
    shutdown(socket_, how);
}


int Socket::get_lastError()
{
161
#ifdef _WIN32
162 163 164 165 166 167 168 169 170
    return WSAGetLastError();
#else
    return errno;
#endif
}


void Socket::set_lastError(int errorCode)
{
171
#ifdef _WIN32
172 173 174 175 176 177 178 179
    WSASetLastError(errorCode);
#else
    errno = errorCode;
#endif
}


} // namespace