| 1 |
/* |
| 2 |
* |
| 3 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 4 |
* or more contributor license agreements. See the NOTICE file |
| 5 |
* distributed with this work for additional information |
| 6 |
* regarding copyright ownership. The ASF licenses this file |
| 7 |
* to you under the Apache License, Version 2.0 (the |
| 8 |
* "License"); you may not use this file except in compliance |
| 9 |
* with the License. You may obtain a copy of the License at |
| 10 |
* |
| 11 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 12 |
* |
| 13 |
* Unless required by applicable law or agreed to in writing, |
| 14 |
* software distributed under the License is distributed on an |
| 15 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 |
* KIND, either express or implied. See the License for the |
| 17 |
* specific language governing permissions and limitations |
| 18 |
* under the License. |
| 19 |
* |
| 20 |
*/ |
| 21 |
|
| 22 |
#define FD_SETSIZE 2048 |
| 23 |
#ifndef _WIN32_WINNT |
| 24 |
#define _WIN32_WINNT 0x0501 |
| 25 |
#endif |
| 26 |
#if _WIN32_WINNT < 0x0501 |
| 27 |
#error "Proton requires Windows API support for XP or later." |
| 28 |
#endif |
| 29 |
#include <winsock2.h> |
| 30 |
#include <mswsock.h> |
| 31 |
#include <Ws2tcpip.h> |
| 32 |
|
| 33 |
#include "reactor/io.h" |
| 34 |
#include "reactor/selector.h" |
| 35 |
|
| 36 |
#include "platform/platform.h" |
| 37 |
#include "iocp.h" |
| 38 |
#include "core/util.h" |
| 39 |
|
| 40 |
#include <proton/object.h> |
| 41 |
|
| 42 |
#include <ctype.h> |
| 43 |
#include <errno.h> |
| 44 |
#include <stdio.h> |
| 45 |
#include <assert.h> |
| 46 |
|
| 47 |
int pni_win32_error(pn_error_t *error, const char *msg, HRESULT code) |
| 48 |
{ |
| 49 |
// Error code can be from GetLastError or WSAGetLastError, |
| 50 |
char err[1024] = {0}; |
| 51 |
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS | |
| 52 |
FORMAT_MESSAGE_MAX_WIDTH_MASK, NULL, code, 0, (LPSTR)&err, sizeof(err), NULL); |
| 53 |
return pn_error_format(error, PN_ERR, "%s: %s", msg, err); |
| 54 |
} |
| 55 |
|
| 56 |
static void io_log(const char *fmt, ...) |
| 57 |
{ |
| 58 |
va_list ap; |
| 59 |
va_start(ap, fmt); |
| 60 |
vfprintf(stderr, fmt, ap); |
| 61 |
va_end(ap); |
| 62 |
fflush(stderr); |
| 63 |
} |
| 64 |
|
| 65 |
struct pn_io_t { |
| 66 |
char host[NI_MAXHOST]; |
| 67 |
char serv[NI_MAXSERV]; |
| 68 |
pn_error_t *error; |
| 69 |
bool trace; |
| 70 |
bool wouldblock; |
| 71 |
iocp_t *iocp; |
| 72 |
}; |
| 73 |
|
| 74 |
void pn_io_initialize(void *obj) |
| 75 |
{ |
| 76 |
pn_io_t *io = (pn_io_t *) obj; |
| 77 |
io->error = pn_error(); |
| 78 |
io->wouldblock = false; |
| 79 |
io->trace = pn_env_bool("PN_TRACE_DRV"); |
| 80 |
|
| 81 |
/* Request WinSock 2.2 */ |
| 82 |
WORD wsa_ver = MAKEWORD(2, 2); |
| 83 |
WSADATA unused; |
| 84 |
int err = WSAStartup(wsa_ver, &unused); |
| 85 |
if (err) { |
| 86 |
pni_win32_error(io->error, "WSAStartup", WSAGetLastError()); |
| 87 |
fprintf(stderr, "Can't load WinSock: %s\n", pn_error_text(io->error)); |
| 88 |
} |
| 89 |
io->iocp = pni_iocp(); |
| 90 |
} |
| 91 |
|
| 92 |
void pn_io_finalize(void *obj) |
| 93 |
{ |
| 94 |
pn_io_t *io = (pn_io_t *) obj; |
| 95 |
pn_selector_t *sel = io->iocp->selector; |
| 96 |
pn_free(io->iocp); |
| 97 |
if (sel) |
| 98 |
pn_decref(sel); |
| 99 |
pn_error_free(io->error); |
| 100 |
WSACleanup(); |
| 101 |
} |
| 102 |
|
| 103 |
#define pn_io_hashcode NULL |
| 104 |
#define pn_io_compare NULL |
| 105 |
#define pn_io_inspect |
| 106 |
|
| 107 |
pn_io_t *pn_io(void) |
| 108 |
{ |
| 109 |
static const pn_class_t clazz = PN_CLASS(pn_io); |
| 110 |
pn_io_t *io = (pn_io_t *) pn_class_new(&clazz, sizeof(pn_io_t)); |
| 111 |
return io; |
| 112 |
} |
| 113 |
|
| 114 |
void pn_io_free(pn_io_t *io) |
| 115 |
{ |
| 116 |
pn_free(io); |
| 117 |
} |
| 118 |
|
| 119 |
pn_error_t *pn_io_error(pn_io_t *io) |
| 120 |
{ |
| 121 |
assert(io); |
| 122 |
return io->error; |
| 123 |
} |
| 124 |
|
| 125 |
static void ensure_unique(pn_io_t *io, pn_socket_t new_socket) |
| 126 |
{ |
| 127 |
// A brand new socket can have the same HANDLE value as a previous |
| 128 |
// one after a socketclose. If the application closes one itself |
| 129 |
// (i.e. not using pn_close), we don't find out about it until here. |
| 130 |
iocpdesc_t *iocpd = pni_iocpdesc_map_get(io->iocp, new_socket); |
| 131 |
if (iocpd) { |
| 132 |
if (io->trace) |
| 133 |
io_log("Stale external socket reference discarded\n"); |
| 134 |
// Re-use means former socket instance was closed |
| 135 |
assert(iocpd->ops_in_progress == 0); |
| 136 |
assert(iocpd->external); |
| 137 |
// Clean up the straggler as best we can |
| 138 |
pn_socket_t sock = iocpd->socket; |
| 139 |
iocpd->socket = INVALID_SOCKET; |
| 140 |
pni_iocpdesc_map_del(io->iocp, sock); // may free the iocpdesc_t depending on refcount |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
/* |
| 146 |
* This heavyweight surrogate pipe could be replaced with a normal Windows pipe |
| 147 |
* now that select() is no longer used. If interrupt semantics are all that is |
| 148 |
* needed, a simple user space counter and reserved completion status would |
| 149 |
* probably suffice. |
| 150 |
*/ |
| 151 |
static int pni_socket_pair(pn_io_t *io, SOCKET sv[2]); |
| 152 |
|
| 153 |
int pn_pipe(pn_io_t *io, pn_socket_t *dest) |
| 154 |
{ |
| 155 |
int n = pni_socket_pair(io, dest); |
| 156 |
if (n) { |
| 157 |
pni_win32_error(io->error, "pipe", WSAGetLastError()); |
| 158 |
} |
| 159 |
return n; |
| 160 |
} |
| 161 |
|
| 162 |
static void pn_configure_sock(pn_io_t *io, pn_socket_t sock) { |
| 163 |
// |
| 164 |
// Disable the Nagle algorithm on TCP connections. |
| 165 |
// |
| 166 |
int flag = 1; |
| 167 |
if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (char *)&flag, sizeof(flag)) != 0) { |
| 168 |
perror("setsockopt"); |
| 169 |
} |
| 170 |
|
| 171 |
u_long nonblock = 1; |
| 172 |
if (ioctlsocket(sock, FIONBIO, &nonblock)) { |
| 173 |
perror("ioctlsocket"); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
static inline pn_socket_t pni_create_socket(int domain, int protocol); |
| 178 |
|
| 179 |
static const char *amqp_service(const char *port) { |
| 180 |
// Help older Windows to know about amqp[s] ports |
| 181 |
if (port) { |
| 182 |
if (!strcmp("amqp", port)) return "5672"; |
| 183 |
if (!strcmp("amqps", port)) return "5671"; |
| 184 |
} |
| 185 |
return port; |
| 186 |
} |
| 187 |
|
| 188 |
pn_socket_t pn_listen(pn_io_t *io, const char *host, const char *port) |
| 189 |
{ |
| 190 |
struct addrinfo *addr; |
| 191 |
int code = getaddrinfo(host, amqp_service(port), NULL, &addr); |
| 192 |
if (code) { |
| 193 |
pn_error_format(io->error, PN_ERR, "getaddrinfo(%s, %s): %s\n", host, port, gai_strerror(code)); |
| 194 |
return INVALID_SOCKET; |
| 195 |
} |
| 196 |
|
| 197 |
pn_socket_t sock = pni_create_socket(addr->ai_family, addr->ai_protocol); |
| 198 |
if (sock == INVALID_SOCKET) { |
| 199 |
pni_win32_error(io->error, "pni_create_socket", WSAGetLastError()); |
| 200 |
return INVALID_SOCKET; |
| 201 |
} |
| 202 |
ensure_unique(io, sock); |
| 203 |
|
| 204 |
bool optval = 1; |
| 205 |
if (setsockopt(sock, SOL_SOCKET, SO_EXCLUSIVEADDRUSE, (const char *) &optval, |
| 206 |
sizeof(optval)) == -1) { |
| 207 |
pni_win32_error(io->error, "setsockopt", WSAGetLastError()); |
| 208 |
closesocket(sock); |
| 209 |
return INVALID_SOCKET; |
| 210 |
} |
| 211 |
|
| 212 |
if (bind(sock, addr->ai_addr, addr->ai_addrlen) == -1) { |
| 213 |
pni_win32_error(io->error, "bind", WSAGetLastError()); |
| 214 |
freeaddrinfo(addr); |
| 215 |
closesocket(sock); |
| 216 |
return INVALID_SOCKET; |
| 217 |
} |
| 218 |
freeaddrinfo(addr); |
| 219 |
|
| 220 |
if (listen(sock, 50) == -1) { |
| 221 |
pni_win32_error(io->error, "listen", WSAGetLastError()); |
| 222 |
closesocket(sock); |
| 223 |
return INVALID_SOCKET; |
| 224 |
} |
| 225 |
|
| 226 |
if (io->iocp->selector) { |
| 227 |
iocpdesc_t *iocpd = pni_iocpdesc_create(io->iocp, sock, false); |
| 228 |
if (!iocpd) { |
| 229 |
pn_i_error_from_errno(io->error, "register"); |
| 230 |
closesocket(sock); |
| 231 |
return INVALID_SOCKET; |
| 232 |
} |
| 233 |
pni_iocpdesc_start(iocpd); |
| 234 |
} |
| 235 |
|
| 236 |
return sock; |
| 237 |
} |
| 238 |
|
| 239 |
pn_socket_t pn_connect(pn_io_t *io, const char *hostarg, const char *port) |
| 240 |
{ |
| 241 |
// convert "0.0.0.0" to "127.0.0.1" on Windows for outgoing sockets |
| 242 |
const char *host = strcmp("0.0.0.0", hostarg) ? hostarg : "127.0.0.1"; |
| 243 |
|
| 244 |
struct addrinfo *addr; |
| 245 |
int code = getaddrinfo(host, amqp_service(port), NULL, &addr); |
| 246 |
if (code) { |
| 247 |
pn_error_format(io->error, PN_ERR, "getaddrinfo(%s, %s): %s", host, port, gai_strerror(code)); |
| 248 |
return INVALID_SOCKET; |
| 249 |
} |
| 250 |
|
| 251 |
pn_socket_t sock = pni_create_socket(addr->ai_family, addr->ai_protocol); |
| 252 |
if (sock == INVALID_SOCKET) { |
| 253 |
pni_win32_error(io->error, "proton pni_create_socket", WSAGetLastError()); |
| 254 |
freeaddrinfo(addr); |
| 255 |
return INVALID_SOCKET; |
| 256 |
} |
| 257 |
|
| 258 |
ensure_unique(io, sock); |
| 259 |
pn_configure_sock(io, sock); |
| 260 |
|
| 261 |
if (io->iocp->selector) { |
| 262 |
return pni_iocp_begin_connect(io->iocp, sock, addr, io->error); |
| 263 |
} else { |
| 264 |
if (connect(sock, addr->ai_addr, addr->ai_addrlen) != 0) { |
| 265 |
if (WSAGetLastError() != WSAEWOULDBLOCK) { |
| 266 |
pni_win32_error(io->error, "connect", WSAGetLastError()); |
| 267 |
freeaddrinfo(addr); |
| 268 |
closesocket(sock); |
| 269 |
return INVALID_SOCKET; |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
freeaddrinfo(addr); |
| 274 |
return sock; |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
pn_socket_t pn_accept(pn_io_t *io, pn_socket_t listen_sock, char *name, size_t size) |
| 279 |
{ |
| 280 |
struct sockaddr_storage addr; |
| 281 |
socklen_t addrlen = sizeof(addr); |
| 282 |
iocpdesc_t *listend = pni_iocpdesc_map_get(io->iocp, listen_sock); |
| 283 |
pn_socket_t accept_sock; |
| 284 |
|
| 285 |
*name = '\0'; |
| 286 |
if (listend) |
| 287 |
accept_sock = pni_iocp_end_accept(listend, (struct sockaddr *) &addr, &addrlen, &io->wouldblock, io->error); |
| 288 |
else { |
| 289 |
// User supplied socket |
| 290 |
accept_sock = accept(listen_sock, (struct sockaddr *) &addr, &addrlen); |
| 291 |
if (accept_sock == INVALID_SOCKET) |
| 292 |
pni_win32_error(io->error, "sync accept", WSAGetLastError()); |
| 293 |
} |
| 294 |
|
| 295 |
if (accept_sock == INVALID_SOCKET) |
| 296 |
return accept_sock; |
| 297 |
|
| 298 |
int code = getnameinfo((struct sockaddr *) &addr, addrlen, io->host, NI_MAXHOST, |
| 299 |
io->serv, NI_MAXSERV, 0); |
| 300 |
if (code) |
| 301 |
code = getnameinfo((struct sockaddr *) &addr, addrlen, io->host, NI_MAXHOST, |
| 302 |
io->serv, NI_MAXSERV, NI_NUMERICHOST | NI_NUMERICSERV); |
| 303 |
if (code) { |
| 304 |
pn_error_format(io->error, PN_ERR, "getnameinfo: %s\n", gai_strerror(code)); |
| 305 |
pn_close(io, accept_sock); |
| 306 |
return INVALID_SOCKET; |
| 307 |
} else { |
| 308 |
pn_configure_sock(io, accept_sock); |
| 309 |
snprintf(name, size, "%s:%s", io->host, io->serv); |
| 310 |
if (listend) { |
| 311 |
pni_iocpdesc_start(pni_iocpdesc_map_get(io->iocp, accept_sock)); |
| 312 |
} |
| 313 |
return accept_sock; |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
static inline pn_socket_t pni_create_socket(int domain, int protocol) { |
| 318 |
return socket(domain, SOCK_STREAM, protocol); |
| 319 |
} |
| 320 |
|
| 321 |
ssize_t pn_send(pn_io_t *io, pn_socket_t sockfd, const void *buf, size_t len) { |
| 322 |
ssize_t count; |
| 323 |
iocpdesc_t *iocpd = pni_iocpdesc_map_get(io->iocp, sockfd); |
| 324 |
if (iocpd) { |
| 325 |
count = pni_iocp_begin_write(iocpd, buf, len, &io->wouldblock, io->error); |
| 326 |
} else { |
| 327 |
count = send(sockfd, (const char *) buf, len, 0); |
| 328 |
io->wouldblock = count < 0 && WSAGetLastError() == WSAEWOULDBLOCK; |
| 329 |
} |
| 330 |
return count; |
| 331 |
} |
| 332 |
|
| 333 |
ssize_t pn_recv(pn_io_t *io, pn_socket_t socket, void *buf, size_t size) |
| 334 |
{ |
| 335 |
ssize_t count; |
| 336 |
iocpdesc_t *iocpd = pni_iocpdesc_map_get(io->iocp, socket); |
| 337 |
if (iocpd) { |
| 338 |
count = pni_iocp_recv(iocpd, buf, size, &io->wouldblock, io->error); |
| 339 |
} else { |
| 340 |
count = recv(socket, (char *) buf, size, 0); |
| 341 |
io->wouldblock = count < 0 && WSAGetLastError() == WSAEWOULDBLOCK; |
| 342 |
} |
| 343 |
return count; |
| 344 |
} |
| 345 |
|
| 346 |
ssize_t pn_write(pn_io_t *io, pn_socket_t socket, const void *buf, size_t size) |
| 347 |
{ |
| 348 |
// non-socket io is mapped to socket io for now. See pn_pipe() |
| 349 |
return pn_send(io, socket, buf, size); |
| 350 |
} |
| 351 |
|
| 352 |
ssize_t pn_read(pn_io_t *io, pn_socket_t socket, void *buf, size_t size) |
| 353 |
{ |
| 354 |
return pn_recv(io, socket, buf, size); |
| 355 |
} |
| 356 |
|
| 357 |
void pn_close(pn_io_t *io, pn_socket_t socket) |
| 358 |
{ |
| 359 |
iocpdesc_t *iocpd = pni_iocpdesc_map_get(io->iocp, socket); |
| 360 |
if (iocpd) |
| 361 |
pni_iocp_begin_close(iocpd); |
| 362 |
else { |
| 363 |
closesocket(socket); |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
bool pn_wouldblock(pn_io_t *io) |
| 368 |
{ |
| 369 |
return io->wouldblock; |
| 370 |
} |
| 371 |
|
| 372 |
pn_selector_t *pn_io_selector(pn_io_t *io) |
| 373 |
{ |
| 374 |
if (io->iocp->selector == NULL) { |
| 375 |
io->iocp->selector = pni_selector_create(io->iocp); |
| 376 |
pn_incref(io->iocp->selector); |
| 377 |
} |
| 378 |
return io->iocp->selector; |
| 379 |
} |
| 380 |
|
| 381 |
static void configure_pipe_socket(pn_io_t *io, pn_socket_t sock) |
| 382 |
{ |
| 383 |
u_long v = 1; |
| 384 |
ioctlsocket (sock, FIONBIO, &v); |
| 385 |
ensure_unique(io, sock); |
| 386 |
iocpdesc_t *iocpd = pni_iocpdesc_create(io->iocp, sock, false); |
| 387 |
pni_iocpdesc_start(iocpd); |
| 388 |
} |
| 389 |
|
| 390 |
|
| 391 |
static int pni_socket_pair (pn_io_t *io, SOCKET sv[2]) { |
| 392 |
// no socketpair on windows. provide pipe() semantics using sockets |
| 393 |
struct protoent * pe_tcp = getprotobyname("tcp"); |
| 394 |
if (pe_tcp == NULL) { |
| 395 |
perror("getprotobyname"); |
| 396 |
return -1; |
| 397 |
} |
| 398 |
|
| 399 |
SOCKET sock = socket(AF_INET, SOCK_STREAM, pe_tcp->p_proto); |
| 400 |
if (sock == INVALID_SOCKET) { |
| 401 |
perror("socket"); |
| 402 |
return -1; |
| 403 |
} |
| 404 |
|
| 405 |
BOOL b = 1; |
| 406 |
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (const char *) &b, sizeof(b)) == -1) { |
| 407 |
perror("setsockopt"); |
| 408 |
closesocket(sock); |
| 409 |
return -1; |
| 410 |
} |
| 411 |
else { |
| 412 |
struct sockaddr_in addr = {0}; |
| 413 |
addr.sin_family = AF_INET; |
| 414 |
addr.sin_port = 0; |
| 415 |
addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK); |
| 416 |
|
| 417 |
if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) == -1) { |
| 418 |
perror("bind"); |
| 419 |
closesocket(sock); |
| 420 |
return -1; |
| 421 |
} |
| 422 |
} |
| 423 |
|
| 424 |
if (listen(sock, 50) == -1) { |
| 425 |
perror("listen"); |
| 426 |
closesocket(sock); |
| 427 |
return -1; |
| 428 |
} |
| 429 |
|
| 430 |
if ((sv[1] = socket(AF_INET, SOCK_STREAM, pe_tcp->p_proto)) == INVALID_SOCKET) { |
| 431 |
perror("sock1"); |
| 432 |
closesocket(sock); |
| 433 |
return -1; |
| 434 |
} |
| 435 |
else { |
| 436 |
struct sockaddr addr = {0}; |
| 437 |
int l = sizeof(addr); |
| 438 |
if (getsockname(sock, &addr, &l) == -1) { |
| 439 |
perror("getsockname"); |
| 440 |
closesocket(sock); |
| 441 |
return -1; |
| 442 |
} |
| 443 |
|
| 444 |
if (connect(sv[1], &addr, sizeof(addr)) == -1) { |
| 445 |
int err = WSAGetLastError(); |
| 446 |
fprintf(stderr, "connect wsaerrr %d\n", err); |
| 447 |
closesocket(sock); |
| 448 |
closesocket(sv[1]); |
| 449 |
return -1; |
| 450 |
} |
| 451 |
|
| 452 |
if ((sv[0] = accept(sock, &addr, &l)) == INVALID_SOCKET) { |
| 453 |
perror("accept"); |
| 454 |
closesocket(sock); |
| 455 |
closesocket(sv[1]); |
| 456 |
return -1; |
| 457 |
} |
| 458 |
} |
| 459 |
|
| 460 |
configure_pipe_socket(io, sv[0]); |
| 461 |
configure_pipe_socket(io, sv[1]); |
| 462 |
closesocket(sock); |
| 463 |
return 0; |
| 464 |
} |