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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
|
/*
* (C) Copyright 2011 Wojtek Kaniewski <wojtekka@irc.pl>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License Version
* 2.1 as published by the Free Software Foundation.
*
* This program 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser 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.
*/
#include "network.h"
#include <stdlib.h>
#include <string.h>
#ifdef _WIN32
/* Code losely based on sockerpair implementation by Nathan C. Meyrs.
* The original copyright notice follows: */
/* socketpair.c
* Copyright 2007, 2010 by Nathan C. Myers <ncm@cantrip.org>
* This code is Free Software. It may be copied freely, in original or
* modified form, subject only to the restrictions that (1) the author is
* relieved from all responsibilities for any use for any purpose, and (2)
* this copyright notice must be retained, unchanged, in its entirety. If
* for any reason the author might be held responsible for any consequences
* of copying or use, license is withheld.
*/
int gg_win32_socketpair(int sv[2])
{
struct sockaddr_in sin;
socklen_t sin_len = sizeof(sin);
int server = -1;
int tmp = 1;
int errno_copy;
server = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
sv[0] = -1;
sv[1] = -1;
if (server == -1)
goto fail;
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
sin.sin_port = 0;
if (setsockopt(server, SOL_SOCKET, SO_REUSEADDR, &tmp, sizeof(tmp)) == -1)
goto fail;
if (bind(server, (struct sockaddr*) &sin, sin_len) == -1)
goto fail;
if (listen(server, 1) == -1)
goto fail;
if (getsockname(server, (struct sockaddr*) &sin, &sin_len) == -1)
goto fail;
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
sv[0] = socket(AF_INET, SOCK_STREAM, 0);
if (sv[0] == -1)
goto fail;
if (connect(sv[0], (struct sockaddr*) &sin, sin_len) == -1)
goto fail;
sv[1] = accept(server, NULL, NULL);
if (sv[1] == -1)
goto fail;
close(server);
return 0;
fail:
errno_copy = errno;
close(server);
close(sv[0]);
close(sv[1]);
errno = errno_copy;
return -1;
}
static int gg_win32_map_wsa_error_to_errno(int wsaewouldblock_map)
{
int wsa_error;
wsa_error = WSAGetLastError();
/* Tutaj powinny być tłumaczone wszystkie typy błędów sprawdzane przez
* kod libgadu. Dla spójność są również tłumaczone typy błędów ustawiane
* przez libgadu.
* Ponadto gdyby okazało się, że jakaś aplikacja na Win32 chce móc
* polegać jeszcze na innych wartościach errno, można tutaj dodać
* ich tłumaczenie. Najpierw jednak zawsze trzeba porównać dokumentacje,
* aby upewnić się co do poprawności tłumaczenia (patrz WSAEWOULDBLOCK,
* które można tłumaczyć na EWOULDBLOCK lub EAGAIN, a nawet na
* EINPROGRESS w przypadku connect()).
*/
switch (wsa_error)
{
/* Typy błędów sprawdzane przez libgadu. */
case WSAEINTR:
return EINTR;
case WSAEWOULDBLOCK:
return wsaewouldblock_map;
/* Typy błędów ustawiane przez libgadu. */
case WSAECONNRESET:
return ECONNRESET;
case WSAEFAULT:
return EFAULT;
case WSAEINVAL:
return EINVAL;
case WSAENOTCONN:
return ENOTCONN;
case WSAETIMEDOUT:
return ETIMEDOUT;
default:
/* Najlepiej zwrócić oryginalny kod błędu. I tak będzie co najwyżej
* wyświetlony w komunikacie debugowym, a tym sposobem będzie łatwiej
* dojść przyczyny problemu. */
return wsa_error;
}
}
#undef accept
int gg_win32_accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen)
{
int res;
res = accept(sockfd, addr, addrlen);
if (res == -1)
errno = gg_win32_map_wsa_error_to_errno(EAGAIN);
return res;
}
#undef bind
int gg_win32_bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen)
{
int res;
res = bind(sockfd, addr, addrlen);
if (res == -1)
errno = gg_win32_map_wsa_error_to_errno(EAGAIN);
return res;
}
int gg_win32_close(int sockfd)
{
int res;
res = closesocket(sockfd);
if (res == -1)
errno = gg_win32_map_wsa_error_to_errno(EAGAIN);
return res;
}
#undef connect
int gg_win32_connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen)
{
int res;
res = connect(sockfd, addr, addrlen);
if (res == -1)
errno = gg_win32_map_wsa_error_to_errno(EINPROGRESS);
return res;
}
#undef gethostbyname
struct hostent *gg_win32_gethostbyname(const char *name)
{
struct hostent *res;
res = gethostbyname(name);
if (res == NULL)
errno = gg_win32_map_wsa_error_to_errno(EAGAIN);
return res;
}
#undef getsockname
int gg_win32_getsockname(int sockfd, struct sockaddr *addr, socklen_t *addrlen)
{
int res;
res = getsockname(sockfd, addr, addrlen);
if (res == -1)
errno = gg_win32_map_wsa_error_to_errno(EAGAIN);
return res;
}
#undef getsockopt
int gg_win32_getsockopt(int sockfd, int level, int optname, void *optval, socklen_t *optlen)
{
int res;
res = getsockopt(sockfd, level, optname, optval, optlen);
if (res == -1)
errno = gg_win32_map_wsa_error_to_errno(EAGAIN);
return res;
}
int gg_win32_ioctl(int d, int request, int *argp)
{
int res;
res = ioctlsocket(d, request, (u_long *)argp);
if (res == -1)
errno = gg_win32_map_wsa_error_to_errno(EAGAIN);
return res;
}
#undef listen
int gg_win32_listen(int sockfd, int backlog)
{
int res;
res = listen(sockfd, backlog);
if (res == -1)
errno = gg_win32_map_wsa_error_to_errno(EAGAIN);
return res;
}
#undef recv
int gg_win32_recv(int sockfd, void *buf, size_t len, int flags)
{
int res;
res = recv(sockfd, buf, len, flags);
if (res == -1)
errno = gg_win32_map_wsa_error_to_errno(EAGAIN);
return res;
}
#undef send
int gg_win32_send(int sockfd, const void *buf, size_t len, int flags)
{
int res;
res = send(sockfd, buf, len, flags);
if (res == -1)
errno = gg_win32_map_wsa_error_to_errno(EAGAIN);
return res;
}
#undef setsockopt
int gg_win32_setsockopt(int sockfd, int level, int optname, const void *optval, socklen_t optlen)
{
int res;
res = setsockopt(sockfd, level, optname, optval, optlen);
if (res == -1)
errno = gg_win32_map_wsa_error_to_errno(EAGAIN);
return res;
}
#undef socket
int gg_win32_socket(int domain, int type, int protocol)
{
int res;
res = socket(domain, type, protocol);
if (res == -1)
errno = gg_win32_map_wsa_error_to_errno(EAGAIN);
return res;
}
#endif /* _WIN32 */
|