~ubuntu-branches/ubuntu/precise/mysql-5.1/precise

« back to all changes in this revision

Viewing changes to extra/yassl/src/socket_wrapper.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Tretkowski
  • Date: 2010-03-17 14:56:02 UTC
  • Revision ID: james.westby@ubuntu.com-20100317145602-x7e30l1b2sb5s6w6
Tags: upstream-5.1.45
ImportĀ upstreamĀ versionĀ 5.1.45

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
   Copyright (C) 2000-2007 MySQL AB
 
3
 
 
4
   This program is free software; you can redistribute it and/or modify
 
5
   it under the terms of the GNU General Public License as published by
 
6
   the Free Software Foundation; version 2 of the License.
 
7
 
 
8
   This program is distributed in the hope that it will be useful,
 
9
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
   GNU General Public License for more details.
 
12
 
 
13
   You should have received a copy of the GNU General Public License
 
14
   along with this program; see the file COPYING. If not, write to the
 
15
   Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
 
16
   MA  02110-1301  USA.
 
17
*/
 
18
 
 
19
 
 
20
/* The socket wrapper source implements a Socket class that hides the 
 
21
 * differences between Berkely style sockets and Windows sockets, allowing 
 
22
 * transparent TCP access.
 
23
 */
 
24
 
 
25
 
 
26
#include "runtime.hpp"
 
27
#include "socket_wrapper.hpp"
 
28
 
 
29
#ifndef _WIN32
 
30
    #include <errno.h>
 
31
    #include <netdb.h>
 
32
    #include <unistd.h>
 
33
    #include <arpa/inet.h>
 
34
    #include <netinet/in.h>
 
35
    #include <sys/ioctl.h>
 
36
    #include <string.h>
 
37
    #include <fcntl.h>
 
38
#endif // _WIN32
 
39
 
 
40
#if defined(__sun) || defined(__SCO_VERSION__) || defined(__NETWARE__)
 
41
    #include <sys/filio.h>
 
42
#endif
 
43
 
 
44
#ifdef _WIN32
 
45
    const int SOCKET_EINVAL = WSAEINVAL;
 
46
    const int SOCKET_EWOULDBLOCK = WSAEWOULDBLOCK;
 
47
    const int SOCKET_EAGAIN = WSAEWOULDBLOCK;
 
48
#else
 
49
    const int SOCKET_EINVAL = EINVAL;
 
50
    const int SOCKET_EWOULDBLOCK = EWOULDBLOCK;
 
51
    const int SOCKET_EAGAIN = EAGAIN;
 
52
#endif // _WIN32
 
53
 
 
54
 
 
55
namespace yaSSL {
 
56
 
 
57
 
 
58
Socket::Socket(socket_t s) 
 
59
    : socket_(s), wouldBlock_(false), nonBlocking_(false)
 
60
{}
 
61
 
 
62
 
 
63
void Socket::set_fd(socket_t s)
 
64
{
 
65
    socket_ = s;
 
66
}
 
67
 
 
68
 
 
69
socket_t Socket::get_fd() const
 
70
{
 
71
    return socket_;
 
72
}
 
73
 
 
74
 
 
75
Socket::~Socket()
 
76
{
 
77
    // don't close automatically now
 
78
}
 
79
 
 
80
 
 
81
void Socket::closeSocket()
 
82
{
 
83
    if (socket_ != INVALID_SOCKET) {
 
84
#ifdef _WIN32
 
85
        closesocket(socket_);
 
86
#else
 
87
        close(socket_);
 
88
#endif
 
89
        socket_ = INVALID_SOCKET;
 
90
    }
 
91
}
 
92
 
 
93
 
 
94
uint Socket::get_ready() const
 
95
{
 
96
#ifdef _WIN32
 
97
    unsigned long ready = 0;
 
98
    ioctlsocket(socket_, FIONREAD, &ready);
 
99
#else
 
100
    /*
 
101
       64-bit Solaris requires the variable passed to
 
102
       FIONREAD be a 32-bit value.
 
103
    */
 
104
    unsigned int ready = 0;
 
105
    ioctl(socket_, FIONREAD, &ready);
 
106
#endif
 
107
 
 
108
    return ready;
 
109
}
 
110
 
 
111
 
 
112
uint Socket::send(const byte* buf, unsigned int sz, int flags) const
 
113
{
 
114
    const byte* pos = buf;
 
115
    const byte* end = pos + sz;
 
116
 
 
117
    while (pos != end) {
 
118
        int sent = ::send(socket_, reinterpret_cast<const char *>(pos),
 
119
                          static_cast<int>(end - pos), flags);
 
120
 
 
121
    if (sent == -1)
 
122
        return 0;
 
123
 
 
124
        pos += sent;
 
125
    }
 
126
 
 
127
    return sz;
 
128
}
 
129
 
 
130
 
 
131
uint Socket::receive(byte* buf, unsigned int sz, int flags)
 
132
{
 
133
    wouldBlock_ = false;
 
134
 
 
135
    int recvd = ::recv(socket_, reinterpret_cast<char *>(buf), sz, flags);
 
136
 
 
137
    // idea to seperate error from would block by arnetheduck@gmail.com
 
138
    if (recvd == -1) {
 
139
        if (get_lastError() == SOCKET_EWOULDBLOCK || 
 
140
            get_lastError() == SOCKET_EAGAIN) {
 
141
            wouldBlock_  = true; // would have blocked this time only
 
142
            nonBlocking_ = true; // socket nonblocking, win32 only way to tell
 
143
        return 0;
 
144
    }
 
145
    }
 
146
    else if (recvd == 0)
 
147
        return static_cast<uint>(-1);
 
148
 
 
149
    return recvd;
 
150
}
 
151
 
 
152
 
 
153
// wait if blocking for input, return false for error
 
154
bool Socket::wait()
 
155
{
 
156
    byte b;
 
157
    return receive(&b, 1, MSG_PEEK) != static_cast<uint>(-1);
 
158
}
 
159
 
 
160
 
 
161
void Socket::shutDown(int how)
 
162
{
 
163
    shutdown(socket_, how);
 
164
}
 
165
 
 
166
 
 
167
int Socket::get_lastError()
 
168
{
 
169
#ifdef _WIN32
 
170
    return WSAGetLastError();
 
171
#else
 
172
    return errno;
 
173
#endif
 
174
}
 
175
 
 
176
 
 
177
bool Socket::WouldBlock() const
 
178
{
 
179
    return wouldBlock_;
 
180
}
 
181
 
 
182
 
 
183
bool Socket::IsNonBlocking() const
 
184
{
 
185
    return nonBlocking_;
 
186
}
 
187
 
 
188
 
 
189
void Socket::set_lastError(int errorCode)
 
190
{
 
191
#ifdef _WIN32
 
192
    WSASetLastError(errorCode);
 
193
#else
 
194
    errno = errorCode;
 
195
#endif
 
196
}
 
197
 
 
198
 
 
199
} // namespace