| 1 |
#include "UnapiNet.hh" |
| 2 |
|
| 3 |
#include "MSXException.hh" |
| 4 |
#include "one_of.hh" |
| 5 |
#include "serialize.hh" |
| 6 |
|
| 7 |
#ifdef _WIN32 |
| 8 |
#include <ws2tcpip.h> |
| 9 |
#include <iphlpapi.h> |
| 10 |
#include <icmpapi.h> |
| 11 |
#ifdef interface |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
#undef interface |
| 16 |
#endif |
| 17 |
#else |
| 18 |
#include <arpa/inet.h> |
| 19 |
#include <netdb.h> |
| 20 |
#include <netinet/tcp.h> |
| 21 |
#include <errno.h> |
| 22 |
#endif |
| 23 |
|
| 24 |
#include <algorithm> |
| 25 |
#include <cassert> |
| 26 |
#include <chrono> |
| 27 |
#include <cstring> |
| 28 |
#include <expected> |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
namespace openmsx { |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
static constexpr uint8_t ERR_OK = 0; |
| 46 |
static constexpr uint8_t ERR_NOT_IMP = 1; |
| 47 |
static constexpr uint8_t ERR_NO_NETWORK = 2; |
| 48 |
static constexpr uint8_t ERR_NO_DATA = 3; |
| 49 |
static constexpr uint8_t ERR_INV_PARAM = 4; |
| 50 |
static constexpr uint8_t ERR_QUERY_EXISTS = 5; |
| 51 |
static constexpr uint8_t ERR_NO_FREE_CONN = 9; |
| 52 |
static constexpr uint8_t ERR_NO_CONN = 11; |
| 53 |
static constexpr uint8_t ERR_CONN_STATE = 12; |
| 54 |
static constexpr uint8_t ERR_BUFFER = 13; |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
static constexpr uint8_t CAPS_BASE = 0x0F; |
| 60 |
static constexpr uint8_t CAP_ICMP = 0x10; |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
static constexpr size_t MAX_TRANSFER = 4096; |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
static constexpr size_t MAX_RECV_BUF = 65536; |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
static constexpr size_t MAX_PARAM_BUF = sizeof(UdpSendParamHeader) + 0xFFFF + 1; |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
static constexpr size_t MAX_HOSTNAME = 253; |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
static constexpr size_t MAX_SEND_BUF = 64 * 1024; |
| 86 |
|
| 87 |
|
| 88 |
static constexpr size_t MAX_UDP_QUEUE = 16; |
| 89 |
static constexpr size_t MAX_ICMP_QUEUE = 16; |
| 90 |
static constexpr size_t MAX_UDP_DGRAM = 2048; |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
static constexpr uint8_t CMD_DETECT = 0x00; |
| 96 |
static constexpr uint8_t CMD_DNS_QUERY = 0x01; |
| 97 |
static constexpr uint8_t CMD_DNS_STATUS = 0x02; |
| 98 |
static constexpr uint8_t CMD_TCP_OPEN = 0x03; |
| 99 |
static constexpr uint8_t CMD_TCP_SEND = 0x04; |
| 100 |
static constexpr uint8_t CMD_TCP_RECV = 0x05; |
| 101 |
static constexpr uint8_t CMD_TCP_CLOSE = 0x06; |
| 102 |
static constexpr uint8_t CMD_TCP_STATE = 0x07; |
| 103 |
static constexpr uint8_t CMD_TCP_ABORT = 0x08; |
| 104 |
static constexpr uint8_t CMD_UDP_OPEN = 0x09; |
| 105 |
static constexpr uint8_t CMD_UDP_CLOSE = 0x0A; |
| 106 |
static constexpr uint8_t CMD_UDP_STATE = 0x0B; |
| 107 |
static constexpr uint8_t CMD_UDP_SEND = 0x0C; |
| 108 |
static constexpr uint8_t CMD_GET_LOCALIP = 0x0D; |
| 109 |
static constexpr uint8_t CMD_NET_STATE = 0x0E; |
| 110 |
static constexpr uint8_t CMD_UDP_RECV = 0x0F; |
| 111 |
static constexpr uint8_t CMD_ICMP_SEND = 0x11; |
| 112 |
static constexpr uint8_t CMD_ICMP_RECV = 0x12; |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
static constexpr auto CLOSE_TIMEOUT = std::chrono::seconds(30); |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
enum class IoError { WouldBlock, Closed, Failed }; |
| 123 |
using IoResult = std::expected<size_t, IoError>; |
| 124 |
|
| 125 |
|
| 126 |
[[nodiscard]] static bool ioWouldBlock() |
| 127 |
{ |
| 128 |
#ifdef _WIN32 |
| 129 |
int err = WSAGetLastError(); |
| 130 |
return (err == WSAEWOULDBLOCK) || (err == WSAEINTR); |
| 131 |
#else |
| 132 |
return (errno == EWOULDBLOCK) || (errno == EAGAIN) || (errno == EINTR); |
| 133 |
#endif |
| 134 |
} |
| 135 |
|
| 136 |
[[nodiscard]] static IoResult netRecv(SOCKET sd, char* buf, size_t count) |
| 137 |
{ |
| 138 |
auto n = recv(sd, buf, static_cast<int>(count), 0); |
| 139 |
if (n > 0) return static_cast<size_t>(n); |
| 140 |
if (n == 0) return std::unexpected(IoError::Closed); |
| 141 |
return std::unexpected(ioWouldBlock() ? IoError::WouldBlock : IoError::Failed); |
| 142 |
} |
| 143 |
|
| 144 |
[[nodiscard]] static IoResult netSend(SOCKET sd, const uint8_t* buf, size_t count) |
| 145 |
{ |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
#ifdef MSG_NOSIGNAL |
| 150 |
constexpr int SEND_FLAGS = MSG_NOSIGNAL; |
| 151 |
#else |
| 152 |
constexpr int SEND_FLAGS = 0; |
| 153 |
#endif |
| 154 |
auto n = send(sd, reinterpret_cast<const char*>(buf), |
| 155 |
static_cast<int>(count), SEND_FLAGS); |
| 156 |
if (n >= 0) return static_cast<size_t>(n); |
| 157 |
return std::unexpected(ioWouldBlock() ? IoError::WouldBlock : IoError::Failed); |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
static void shutdownSend(SOCKET sd) |
| 162 |
{ |
| 163 |
#ifdef _WIN32 |
| 164 |
shutdown(sd, SD_SEND); |
| 165 |
#else |
| 166 |
shutdown(sd, SHUT_WR); |
| 167 |
#endif |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
static constexpr uint8_t DNS_ERR_NO_SUCH_HOST = 3; |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
UnapiNet::UnapiNet(const DeviceConfig& config) |
| 176 |
: MSXDevice(config) |
| 177 |
{ |
| 178 |
paramBuf.reserve(MAX_TRANSFER + 16); |
| 179 |
resultBuf.reserve(MAX_TRANSFER + 16); |
| 180 |
|
| 181 |
reset(EmuTime::dummy()); |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
SOCKET r = socket(AF_INET, SOCK_DGRAM, 0); |
| 192 |
if (r == OPENMSX_INVALID_SOCKET) { |
| 193 |
throw MSXException("UnapiNet: cannot create the wake socket pair"); |
| 194 |
} |
| 195 |
sockaddr_in addr = sock_makeIPv4(INADDR_LOOPBACK, 0); |
| 196 |
::socklen_t alen = sizeof(addr); |
| 197 |
if (bind(r, reinterpret_cast<sockaddr*>(&addr), sizeof(addr)) != 0 || |
| 198 |
getsockname(r, reinterpret_cast<sockaddr*>(&addr), &alen) != 0) { |
| 199 |
sock_close(r); |
| 200 |
throw MSXException("UnapiNet: cannot bind the wake socket pair"); |
| 201 |
} |
| 202 |
SOCKET s = socket(AF_INET, SOCK_DGRAM, 0); |
| 203 |
if (s == OPENMSX_INVALID_SOCKET || |
| 204 |
connect(s, reinterpret_cast<sockaddr*>(&addr), sizeof(addr)) != 0) { |
| 205 |
if (s != OPENMSX_INVALID_SOCKET) sock_close(s); |
| 206 |
sock_close(r); |
| 207 |
throw MSXException("UnapiNet: cannot connect the wake socket pair"); |
| 208 |
} |
| 209 |
sock_setNonBlocking(r); |
| 210 |
sock_setNonBlocking(s); |
| 211 |
wakeRecv = r; |
| 212 |
wakeSend = s; |
| 213 |
|
| 214 |
#ifdef _WIN32 |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
if (HANDLE h = IcmpCreateFile(); h != INVALID_HANDLE_VALUE) { |
| 219 |
icmpChannel = h; |
| 220 |
icmpAvailable = true; |
| 221 |
} |
| 222 |
#endif |
| 223 |
|
| 224 |
|
| 225 |
running = true; |
| 226 |
sockThread = std::thread([this]() { socketLoop(); }); |
| 227 |
dnsThread = std::thread([this]() { dnsWorkerLoop(); }); |
| 228 |
if (icmpAvailable) { |
| 229 |
icmpWorker = std::thread([this]() { icmpWorkerLoop(); }); |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
UnapiNet::~UnapiNet() |
| 234 |
{ |
| 235 |
running = false; |
| 236 |
poke(); |
| 237 |
if (sockThread.joinable()) sockThread.join(); |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
{ |
| 242 |
std::scoped_lock lock(icmpMutex); |
| 243 |
} |
| 244 |
icmpCv.notify_all(); |
| 245 |
if (icmpWorker.joinable()) icmpWorker.join(); |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
{ |
| 259 |
std::scoped_lock lock(dns.mutex); |
| 260 |
} |
| 261 |
dns.cv.notify_all(); |
| 262 |
if (dnsThread.joinable()) dnsThread.join(); |
| 263 |
closeAllConnections(); |
| 264 |
|
| 265 |
|
| 266 |
{ |
| 267 |
std::scoped_lock lock(closeMutex); |
| 268 |
for (SOCKET sd : socksToClose) sock_close(sd); |
| 269 |
socksToClose.clear(); |
| 270 |
} |
| 271 |
sock_close(wakeSend); |
| 272 |
sock_close(wakeRecv); |
| 273 |
|
| 274 |
#ifdef _WIN32 |
| 275 |
if (icmpChannel) IcmpCloseHandle(icmpChannel); |
| 276 |
#endif |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
void UnapiNet::reset(EmuTime ) |
| 282 |
{ |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
paramBuf.clear(); |
| 287 |
resultBuf.clear(); |
| 288 |
resultPos = 0; |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
for (auto& c : tcp) requestClose(c, CloseReason::NeverUsed, true); |
| 294 |
for (auto& u : udp) requestClose(u); |
| 295 |
|
| 296 |
{ |
| 297 |
std::scoped_lock lock(dns.mutex); |
| 298 |
dns.status = DnsStatus::Idle; |
| 299 |
dns.resolvedIp = 0; |
| 300 |
dns.request.reset(); |
| 301 |
|
| 302 |
|
| 303 |
} |
| 304 |
{ |
| 305 |
std::scoped_lock lock(icmpMutex); |
| 306 |
icmpReplies.clear(); |
| 307 |
++icmpGeneration; |
| 308 |
|
| 309 |
icmpPending = false; |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
byte UnapiNet::peekIO(uint16_t , EmuTime ) const |
| 316 |
{ |
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
if (resultPos < resultBuf.size()) { |
| 324 |
return resultBuf[resultPos]; |
| 325 |
} |
| 326 |
return 0xFF; |
| 327 |
} |
| 328 |
|
| 329 |
byte UnapiNet::readIO(uint16_t port, EmuTime time) |
| 330 |
{ |
| 331 |
byte b = peekIO(port, time); |
| 332 |
|
| 333 |
if (resultPos < resultBuf.size()) { |
| 334 |
++resultPos; |
| 335 |
} |
| 336 |
return b; |
| 337 |
} |
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
void UnapiNet::writeIO(uint16_t port, byte value, EmuTime ) |
| 342 |
{ |
| 343 |
if (port & 1) { |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
resultBuf.clear(); |
| 348 |
resultPos = 0; |
| 349 |
if (paramBuf.size() < MAX_PARAM_BUF) { |
| 350 |
paramBuf.push_back(value); |
| 351 |
} |
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
} else { |
| 356 |
|
| 357 |
processCmd(value); |
| 358 |
} |
| 359 |
} |
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
void UnapiNet::setResult(std::span<const uint8_t> data) |
| 364 |
{ |
| 365 |
assert(!data.empty()); |
| 366 |
resultBuf.assign(data.begin(), data.end()); |
| 367 |
resultPos = 0; |
| 368 |
} |
| 369 |
|
| 370 |
void UnapiNet::replyStatus(uint8_t status) |
| 371 |
{ |
| 372 |
setResult(std::span<const uint8_t>(&status, 1)); |
| 373 |
} |
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
int UnapiNet::allocTcpHandle() |
| 378 |
{ |
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
for (int i = 0; i < MAX_TCP; i++) { |
| 383 |
if (tcp[i].sock == OPENMSX_INVALID_SOCKET && |
| 384 |
tcp[i].tcpState == TcpState::Closed) { |
| 385 |
return i; |
| 386 |
} |
| 387 |
} |
| 388 |
return INVALID_HANDLE; |
| 389 |
} |
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
UnapiNet::TcpConnection* UnapiNet::tcpForHandle(int wireHandle) |
| 394 |
{ |
| 395 |
return (wireHandle >= 1 && wireHandle <= MAX_TCP) ? &tcp[wireHandle - 1] : nullptr; |
| 396 |
} |
| 397 |
|
| 398 |
UnapiNet::UdpConnection* UnapiNet::udpForHandle(int wireHandle) |
| 399 |
{ |
| 400 |
return (wireHandle >= 1 && wireHandle <= MAX_UDP) ? &udp[wireHandle - 1] : nullptr; |
| 401 |
} |
| 402 |
|
| 403 |
|
| 404 |
void UnapiNet::closeTcp(TcpConnection& c) |
| 405 |
{ |
| 406 |
std::scoped_lock lock(c.mutex); |
| 407 |
if (SOCKET sd = c.sock; sd != OPENMSX_INVALID_SOCKET) { |
| 408 |
sock_close(sd); |
| 409 |
c.sock = OPENMSX_INVALID_SOCKET; |
| 410 |
} |
| 411 |
c.finSent = false; |
| 412 |
c.tcpState = TcpState::Closed; |
| 413 |
c.remoteIp = 0; |
| 414 |
c.remotePort = 0; |
| 415 |
c.localPort = 0; |
| 416 |
c.resident = false; |
| 417 |
c.recvBuf.clear(); |
| 418 |
c.sendBuf.clear(); |
| 419 |
} |
| 420 |
|
| 421 |
void UnapiNet::closeAllConnections() |
| 422 |
{ |
| 423 |
for (auto& c : tcp) { |
| 424 |
closeTcp(c); |
| 425 |
c.closeReason = CloseReason::NeverUsed; |
| 426 |
} |
| 427 |
for (auto& u : udp) { |
| 428 |
closeUdp(u); |
| 429 |
} |
| 430 |
} |
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
void UnapiNet::poke() |
| 448 |
{ |
| 449 |
uint8_t b = 0; |
| 450 |
[[maybe_unused]] auto result = netSend(wakeSend, &b, 1); |
| 451 |
} |
| 452 |
|
| 453 |
void UnapiNet::deferSockClose(SOCKET sd) |
| 454 |
{ |
| 455 |
std::scoped_lock lock(closeMutex); |
| 456 |
socksToClose.push_back(sd); |
| 457 |
poke(); |
| 458 |
} |
| 459 |
|
| 460 |
void UnapiNet::requestClose(TcpConnection& c, CloseReason reason, |
| 461 |
bool clearMetadata) |
| 462 |
{ |
| 463 |
SOCKET sd; |
| 464 |
{ |
| 465 |
std::scoped_lock lock(c.mutex); |
| 466 |
sd = c.sock; |
| 467 |
c.sock = OPENMSX_INVALID_SOCKET; |
| 468 |
c.closeReason = reason; |
| 469 |
c.tcpState = TcpState::Closed; |
| 470 |
c.finSent = false; |
| 471 |
c.sendBuf.clear(); |
| 472 |
if (clearMetadata) { |
| 473 |
c.remoteIp = 0; |
| 474 |
c.remotePort = 0; |
| 475 |
c.localPort = 0; |
| 476 |
c.resident = false; |
| 477 |
c.recvBuf.clear(); |
| 478 |
} |
| 479 |
} |
| 480 |
if (sd != OPENMSX_INVALID_SOCKET) deferSockClose(sd); |
| 481 |
} |
| 482 |
|
| 483 |
void UnapiNet::requestClose(UdpConnection& u) |
| 484 |
{ |
| 485 |
|
| 486 |
|
| 487 |
SOCKET sd; |
| 488 |
{ |
| 489 |
std::scoped_lock lock(u.mutex); |
| 490 |
sd = u.sock; |
| 491 |
u.sock = OPENMSX_INVALID_SOCKET; |
| 492 |
u.localPort = 0; |
| 493 |
u.resident = false; |
| 494 |
u.recvQueue.clear(); |
| 495 |
} |
| 496 |
if (sd != OPENMSX_INVALID_SOCKET) deferSockClose(sd); |
| 497 |
} |
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
void UnapiNet::gracefulClose(TcpConnection& c) |
| 506 |
{ |
| 507 |
{ |
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
std::scoped_lock lock(c.mutex); |
| 512 |
SOCKET sd = c.sock; |
| 513 |
if (sd != OPENMSX_INVALID_SOCKET) { |
| 514 |
if (c.tcpState == TcpState::FinWait1) { |
| 515 |
return; |
| 516 |
} |
| 517 |
if (c.tcpState == one_of(TcpState::Established, TcpState::CloseWait)) { |
| 518 |
c.closeReason = CloseReason::ClosedByUser; |
| 519 |
c.tcpState = TcpState::FinWait1; |
| 520 |
c.closeDeadline = std::chrono::steady_clock::now() + CLOSE_TIMEOUT; |
| 521 |
c.finSent = false; |
| 522 |
if (c.sendBuf.empty()) { |
| 523 |
shutdownSend(sd); |
| 524 |
c.finSent = true; |
| 525 |
} |
| 526 |
poke(); |
| 527 |
|
| 528 |
return; |
| 529 |
} |
| 530 |
} |
| 531 |
} |
| 532 |
|
| 533 |
requestClose(c, CloseReason::ClosedByUser); |
| 534 |
} |
| 535 |
|
| 536 |
void UnapiNet::socketLoop() |
| 537 |
{ |
| 538 |
while (running) { |
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
{ |
| 543 |
std::vector<SOCKET> toClose; |
| 544 |
{ |
| 545 |
std::scoped_lock lock(closeMutex); |
| 546 |
toClose.swap(socksToClose); |
| 547 |
} |
| 548 |
for (SOCKET sd : toClose) sock_close(sd); |
| 549 |
} |
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
fd_set rfds; |
| 557 |
fd_set wfds; |
| 558 |
fd_set efds; |
| 559 |
FD_ZERO(&rfds); |
| 560 |
FD_ZERO(&wfds); |
| 561 |
FD_ZERO(&efds); |
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
FD_SET(wakeRecv, &rfds); |
| 566 |
SOCKET maxSock = wakeRecv; |
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
std::array<SOCKET, MAX_TCP> watchedTcp; |
| 571 |
std::array<SOCKET, MAX_UDP> watchedUdp; |
| 572 |
watchedTcp.fill(OPENMSX_INVALID_SOCKET); |
| 573 |
watchedUdp.fill(OPENMSX_INVALID_SOCKET); |
| 574 |
|
| 575 |
for (int i = 0; i < MAX_TCP; ++i) { |
| 576 |
auto& c = tcp[i]; |
| 577 |
SOCKET sd = c.sock; |
| 578 |
if (sd == OPENMSX_INVALID_SOCKET) continue; |
| 579 |
|
| 580 |
|
| 581 |
if (c.tcpState == TcpState::FinWait1) { |
| 582 |
bool giveUp = false; |
| 583 |
{ |
| 584 |
std::scoped_lock lock(c.mutex); |
| 585 |
if (!c.finSent && c.sendBuf.empty()) { |
| 586 |
|
| 587 |
shutdownSend(sd); |
| 588 |
c.finSent = true; |
| 589 |
} |
| 590 |
giveUp = std::chrono::steady_clock::now() > c.closeDeadline; |
| 591 |
} |
| 592 |
if (giveUp) { |
| 593 |
requestClose(c, CloseReason::ClosedByUser); |
| 594 |
continue; |
| 595 |
} |
| 596 |
} |
| 597 |
|
| 598 |
watchedTcp[i] = sd; |
| 599 |
if (c.tcpState == TcpState::SynSent) { |
| 600 |
|
| 601 |
|
| 602 |
FD_SET(sd, &wfds); |
| 603 |
FD_SET(sd, &efds); |
| 604 |
} else { |
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
bool pendingSend; |
| 615 |
bool hasRoom; |
| 616 |
{ |
| 617 |
std::scoped_lock lock(c.mutex); |
| 618 |
pendingSend = !c.sendBuf.empty(); |
| 619 |
hasRoom = c.recvBuf.size() < MAX_RECV_BUF; |
| 620 |
} |
| 621 |
if (hasRoom) FD_SET(sd, &rfds); |
| 622 |
if (pendingSend) FD_SET(sd, &wfds); |
| 623 |
} |
| 624 |
maxSock = std::max(maxSock, sd); |
| 625 |
} |
| 626 |
for (int i = 0; i < MAX_UDP; ++i) { |
| 627 |
auto& u = udp[i]; |
| 628 |
SOCKET sd = u.sock; |
| 629 |
if (sd == OPENMSX_INVALID_SOCKET) continue; |
| 630 |
watchedUdp[i] = sd; |
| 631 |
FD_SET(sd, &rfds); |
| 632 |
maxSock = std::max(maxSock, sd); |
| 633 |
} |
| 634 |
struct timeval tv = {0, 100000}; |
| 635 |
if (select(static_cast<int>(maxSock) + 1, &rfds, &wfds, &efds, &tv) <= 0) { |
| 636 |
continue; |
| 637 |
} |
| 638 |
if (FD_ISSET(wakeRecv, &rfds)) { |
| 639 |
|
| 640 |
|
| 641 |
std::array<char, 64> pokes; |
| 642 |
while (netRecv(wakeRecv, pokes.data(), pokes.size())) {} |
| 643 |
} |
| 644 |
|
| 645 |
for (int i = 0; i < MAX_TCP; ++i) { |
| 646 |
auto& c = tcp[i]; |
| 647 |
SOCKET sd = watchedTcp[i]; |
| 648 |
|
| 649 |
|
| 650 |
if (sd == OPENMSX_INVALID_SOCKET || c.sock != sd) continue; |
| 651 |
|
| 652 |
|
| 653 |
if (c.tcpState == TcpState::SynSent) { |
| 654 |
if (FD_ISSET(sd, &efds)) { |
| 655 |
requestClose(c, CloseReason::ConnectFailed); |
| 656 |
} else if (FD_ISSET(sd, &wfds)) { |
| 657 |
int err = sock_getIntOption(sd, SOL_SOCKET, SO_ERROR); |
| 658 |
if (err != 0) { |
| 659 |
requestClose(c, CloseReason::ConnectFailed); |
| 660 |
} else { |
| 661 |
std::scoped_lock lock(c.mutex); |
| 662 |
if (c.sock == sd) c.tcpState = TcpState::Established; |
| 663 |
} |
| 664 |
} |
| 665 |
continue; |
| 666 |
} |
| 667 |
|
| 668 |
|
| 669 |
if (c.tcpState == TcpState::Listen) { |
| 670 |
if (!FD_ISSET(sd, &rfds)) continue; |
| 671 |
struct sockaddr_in peer; |
| 672 |
::socklen_t plen = sizeof(peer); |
| 673 |
SOCKET a = accept(sd, reinterpret_cast<struct sockaddr*>(&peer), &plen); |
| 674 |
if (a == OPENMSX_INVALID_SOCKET) continue; |
| 675 |
uint32_t peerIp = ntohl(peer.sin_addr.s_addr); |
| 676 |
uint16_t peerPort = ntohs(peer.sin_port); |
| 677 |
sock_setNonBlocking(a); |
| 678 |
sock_setIntOption(a, IPPROTO_TCP, TCP_NODELAY); |
| 679 |
|
| 680 |
bool taken = false; |
| 681 |
{ |
| 682 |
std::scoped_lock lock(c.mutex); |
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
if (c.sock == sd && c.tcpState == TcpState::Listen && |
| 688 |
(c.remoteIp == 0 || peerIp == c.remoteIp)) { |
| 689 |
|
| 690 |
|
| 691 |
c.sock = a; |
| 692 |
c.tcpState = TcpState::Established; |
| 693 |
c.remoteIp = peerIp; |
| 694 |
c.remotePort = peerPort; |
| 695 |
taken = true; |
| 696 |
} |
| 697 |
} |
| 698 |
if (taken) { |
| 699 |
deferSockClose(sd); |
| 700 |
} else { |
| 701 |
sock_close(a); |
| 702 |
} |
| 703 |
continue; |
| 704 |
} |
| 705 |
|
| 706 |
if (c.tcpState != one_of(TcpState::Established, TcpState::CloseWait, |
| 707 |
TcpState::FinWait1)) { |
| 708 |
continue; |
| 709 |
} |
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
if (FD_ISSET(sd, &wfds)) { |
| 715 |
bool sendFailed = false; |
| 716 |
{ |
| 717 |
std::scoped_lock lock(c.mutex); |
| 718 |
while (c.sock == sd && !c.sendBuf.empty()) { |
| 719 |
auto r = netSend(sd, c.sendBuf.data(), c.sendBuf.size()); |
| 720 |
if (!r) { |
| 721 |
sendFailed = (r.error() == IoError::Failed); |
| 722 |
break; |
| 723 |
} |
| 724 |
if (*r == 0) break; |
| 725 |
c.sendBuf.erase(c.sendBuf.begin(), |
| 726 |
c.sendBuf.begin() + *r); |
| 727 |
} |
| 728 |
} |
| 729 |
if (sendFailed) { |
| 730 |
requestClose(c, CloseReason::ConnectionReset); |
| 731 |
continue; |
| 732 |
} |
| 733 |
} |
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
if (!FD_ISSET(sd, &rfds)) continue; |
| 741 |
size_t room; |
| 742 |
{ |
| 743 |
std::scoped_lock lock(c.mutex); |
| 744 |
if (c.sock != sd) continue; |
| 745 |
room = MAX_RECV_BUF - std::min(MAX_RECV_BUF, c.recvBuf.size()); |
| 746 |
} |
| 747 |
if (room == 0) continue; |
| 748 |
std::array<char, 512> buf; |
| 749 |
auto r = netRecv(sd, buf.data(), std::min(buf.size(), room)); |
| 750 |
if (r) { |
| 751 |
std::scoped_lock lock(c.mutex); |
| 752 |
if (c.sock != sd) continue; |
| 753 |
const auto* d = reinterpret_cast<const uint8_t*>(buf.data()); |
| 754 |
c.recvBuf.insert(c.recvBuf.end(), d, d + *r); |
| 755 |
} else if (r.error() == IoError::Closed) { |
| 756 |
|
| 757 |
|
| 758 |
if (c.tcpState == TcpState::FinWait1) { |
| 759 |
requestClose(c, CloseReason::ClosedByUser); |
| 760 |
} else { |
| 761 |
std::scoped_lock lock(c.mutex); |
| 762 |
if (c.sock == sd) c.tcpState = TcpState::CloseWait; |
| 763 |
} |
| 764 |
} else if (r.error() == IoError::Failed) { |
| 765 |
requestClose(c, CloseReason::ConnectionReset); |
| 766 |
} |
| 767 |
|
| 768 |
} |
| 769 |
|
| 770 |
for (int i = 0; i < MAX_UDP; ++i) { |
| 771 |
auto& u = udp[i]; |
| 772 |
SOCKET sd = watchedUdp[i]; |
| 773 |
if (sd == OPENMSX_INVALID_SOCKET || u.sock != sd) continue; |
| 774 |
if (!FD_ISSET(sd, &rfds)) continue; |
| 775 |
std::array<char, MAX_UDP_DGRAM> buf; |
| 776 |
struct sockaddr_in src; |
| 777 |
::socklen_t slen = sizeof(src); |
| 778 |
int n = recvfrom(sd, buf.data(), buf.size(), 0, |
| 779 |
reinterpret_cast<struct sockaddr*>(&src), &slen); |
| 780 |
#ifdef _WIN32 |
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
if (n < 0 && WSAGetLastError() == WSAEMSGSIZE) { |
| 788 |
n = static_cast<int>(buf.size()); |
| 789 |
} |
| 790 |
#endif |
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
if (n < 0) continue; |
| 795 |
UdpDatagram dg; |
| 796 |
dg.srcIp = ntohl(src.sin_addr.s_addr); |
| 797 |
dg.srcPort = ntohs(src.sin_port); |
| 798 |
dg.data.assign(buf.data(), buf.data() + n); |
| 799 |
std::scoped_lock lock(u.mutex); |
| 800 |
if (u.sock != sd) continue; |
| 801 |
if (u.recvQueue.size() < MAX_UDP_QUEUE) { |
| 802 |
u.recvQueue.push_back(std::move(dg)); |
| 803 |
} |
| 804 |
} |
| 805 |
} |
| 806 |
} |
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
void UnapiNet::processCmd(uint8_t cmd) |
| 816 |
{ |
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
switch (cmd) { |
| 821 |
case CMD_DETECT: cmdDetect(); break; |
| 822 |
case CMD_DNS_QUERY: cmdDnsQuery(); break; |
| 823 |
case CMD_DNS_STATUS: cmdDnsStatus(); break; |
| 824 |
case CMD_TCP_OPEN: cmdTcpOpen(); break; |
| 825 |
case CMD_TCP_SEND: cmdTcpSend(); break; |
| 826 |
case CMD_TCP_RECV: cmdTcpRecv(); break; |
| 827 |
case CMD_TCP_CLOSE: cmdTcpClose(); break; |
| 828 |
case CMD_TCP_STATE: cmdTcpState(); break; |
| 829 |
case CMD_TCP_ABORT: cmdTcpAbort(); break; |
| 830 |
case CMD_GET_LOCALIP: cmdGetLocalIP(); break; |
| 831 |
case CMD_NET_STATE: cmdNetState(); break; |
| 832 |
case CMD_UDP_OPEN: cmdUdpOpen(); break; |
| 833 |
case CMD_UDP_CLOSE: cmdUdpClose(); break; |
| 834 |
case CMD_UDP_STATE: cmdUdpState(); break; |
| 835 |
case CMD_UDP_SEND: cmdUdpSend(); break; |
| 836 |
case CMD_UDP_RECV: cmdUdpRecv(); break; |
| 837 |
case CMD_ICMP_SEND: cmdIcmpSend(); break; |
| 838 |
case CMD_ICMP_RECV: cmdIcmpRecv(); break; |
| 839 |
default: |
| 840 |
replyStatus(ERR_NOT_IMP); |
| 841 |
break; |
| 842 |
} |
| 843 |
paramBuf.clear(); |
| 844 |
} |
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
|
| 850 |
void UnapiNet::cmdDetect() |
| 851 |
{ |
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
if (!paramBuf.empty()) { |
| 857 |
replyStatus(ERR_INV_PARAM); |
| 858 |
return; |
| 859 |
} |
| 860 |
setResult(DetectResult{.caps = |
| 861 |
static_cast<uint8_t>(CAPS_BASE | (icmpAvailable ? CAP_ICMP : 0))}); |
| 862 |
} |
| 863 |
|
| 864 |
|
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
void UnapiNet::cmdDnsQuery() |
| 869 |
{ |
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
|
| 874 |
|
| 875 |
|
| 876 |
|
| 877 |
|
| 878 |
if (paramBuf.empty() || paramBuf.size() > MAX_HOSTNAME || |
| 879 |
std::ranges::find(paramBuf, uint8_t(0)) != paramBuf.end()) { |
| 880 |
replyStatus(ERR_INV_PARAM); |
| 881 |
return; |
| 882 |
} |
| 883 |
{ |
| 884 |
std::scoped_lock lock(dns.mutex); |
| 885 |
if (dns.status == DnsStatus::InProgress) { |
| 886 |
|
| 887 |
replyStatus(ERR_QUERY_EXISTS); |
| 888 |
return; |
| 889 |
} |
| 890 |
} |
| 891 |
|
| 892 |
std::string hostname(paramBuf.begin(), paramBuf.end()); |
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
struct in_addr addr; |
| 898 |
if (inet_pton(AF_INET, hostname.c_str(), &addr) == 1) { |
| 899 |
uint32_t ip = ntohl(addr.s_addr); |
| 900 |
{ |
| 901 |
std::scoped_lock lock(dns.mutex); |
| 902 |
dns.resolvedIp = ip; |
| 903 |
dns.status = DnsStatus::Complete; |
| 904 |
} |
| 905 |
setResult(DnsQueryResult{.ip = ip}); |
| 906 |
return; |
| 907 |
} |
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
|
| 913 |
{ |
| 914 |
std::scoped_lock lock(dns.mutex); |
| 915 |
dns.status = DnsStatus::InProgress; |
| 916 |
dns.resolvedIp = 0; |
| 917 |
dns.request = std::move(hostname); |
| 918 |
} |
| 919 |
dns.cv.notify_one(); |
| 920 |
|
| 921 |
const std::array<uint8_t, 2> started{ERR_OK, 0}; |
| 922 |
setResult(started); |
| 923 |
} |
| 924 |
|
| 925 |
|
| 926 |
|
| 927 |
|
| 928 |
|
| 929 |
|
| 930 |
|
| 931 |
|
| 932 |
|
| 933 |
|
| 934 |
void UnapiNet::dnsWorkerLoop() |
| 935 |
{ |
| 936 |
std::unique_lock lock(dns.mutex); |
| 937 |
while (true) { |
| 938 |
dns.cv.wait(lock, [&] { return !running || dns.request.has_value(); }); |
| 939 |
if (!running) return; |
| 940 |
std::string hostname = *dns.request; |
| 941 |
|
| 942 |
lock.unlock(); |
| 943 |
|
| 944 |
struct addrinfo hints; |
| 945 |
memset(&hints, 0, sizeof(hints)); |
| 946 |
hints.ai_family = AF_INET; |
| 947 |
hints.ai_socktype = SOCK_STREAM; |
| 948 |
|
| 949 |
struct addrinfo* res = nullptr; |
| 950 |
int err = getaddrinfo(hostname.c_str(), nullptr, &hints, &res); |
| 951 |
std::optional<uint32_t> ip; |
| 952 |
if (err == 0 && res != nullptr) { |
| 953 |
auto* addr4 = reinterpret_cast<struct sockaddr_in*>(res->ai_addr); |
| 954 |
|
| 955 |
|
| 956 |
ip = ntohl(addr4->sin_addr.s_addr); |
| 957 |
} |
| 958 |
if (res) freeaddrinfo(res); |
| 959 |
|
| 960 |
lock.lock(); |
| 961 |
if (dns.request == hostname) { |
| 962 |
|
| 963 |
dns.request.reset(); |
| 964 |
if (ip) { |
| 965 |
dns.resolvedIp = *ip; |
| 966 |
dns.status = DnsStatus::Complete; |
| 967 |
} else { |
| 968 |
dns.status = DnsStatus::Error; |
| 969 |
} |
| 970 |
} |
| 971 |
} |
| 972 |
} |
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
void UnapiNet::cmdDnsStatus() |
| 981 |
{ |
| 982 |
if (!paramBuf.empty()) { |
| 983 |
replyStatus(ERR_INV_PARAM); |
| 984 |
return; |
| 985 |
} |
| 986 |
std::scoped_lock lock(dns.mutex); |
| 987 |
switch (dns.status) { |
| 988 |
case DnsStatus::Complete: |
| 989 |
setResult(DnsStatusResult{.ip = dns.resolvedIp}); |
| 990 |
break; |
| 991 |
case DnsStatus::Error: |
| 992 |
|
| 993 |
|
| 994 |
|
| 995 |
setResult(DnsStatusFailed{.sub = DNS_ERR_NO_SUCH_HOST}); |
| 996 |
break; |
| 997 |
default: { |
| 998 |
const std::array<uint8_t, 2> r{ERR_OK, static_cast<uint8_t>(dns.status)}; |
| 999 |
setResult(r); |
| 1000 |
break; |
| 1001 |
} |
| 1002 |
} |
| 1003 |
} |
| 1004 |
|
| 1005 |
|
| 1006 |
|
| 1007 |
|
| 1008 |
|
| 1009 |
|
| 1010 |
void UnapiNet::cmdTcpOpen() |
| 1011 |
{ |
| 1012 |
auto p = exactParams<TcpOpenParams>(); |
| 1013 |
if (!p) { |
| 1014 |
replyStatus(ERR_INV_PARAM); |
| 1015 |
return; |
| 1016 |
} |
| 1017 |
if (p->flags & ~0x03) { |
| 1018 |
replyStatus(ERR_INV_PARAM); |
| 1019 |
return; |
| 1020 |
} |
| 1021 |
bool passive = (p->flags & 0x01) != 0; |
| 1022 |
bool resident = (p->flags & 0x02) != 0; |
| 1023 |
uint32_t ip = p->remoteIp; |
| 1024 |
uint16_t remotePort = p->remotePort; |
| 1025 |
uint16_t localPortReq = p->localPort; |
| 1026 |
|
| 1027 |
|
| 1028 |
|
| 1029 |
|
| 1030 |
|
| 1031 |
int h = allocTcpHandle(); |
| 1032 |
if (h == INVALID_HANDLE) { |
| 1033 |
replyStatus(ERR_NO_FREE_CONN); |
| 1034 |
return; |
| 1035 |
} |
| 1036 |
auto& c = tcp[h]; |
| 1037 |
|
| 1038 |
SOCKET s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
| 1039 |
if (s == OPENMSX_INVALID_SOCKET) { |
| 1040 |
replyStatus(ERR_NO_NETWORK); |
| 1041 |
return; |
| 1042 |
} |
| 1043 |
|
| 1044 |
sock_setIntOption(s, IPPROTO_TCP, TCP_NODELAY); |
| 1045 |
sock_setNonBlocking(s); |
| 1046 |
|
| 1047 |
TcpState newState = TcpState::Closed; |
| 1048 |
uint16_t localPort = 0; |
| 1049 |
|
| 1050 |
if (passive) { |
| 1051 |
|
| 1052 |
if (localPortReq == 0xFFFF) localPortReq = 0; |
| 1053 |
|
| 1054 |
sock_setIntOption(s, SOL_SOCKET, SO_REUSEADDR); |
| 1055 |
sockaddr_in addr = sock_makeIPv4(INADDR_ANY, localPortReq); |
| 1056 |
if (bind(s, reinterpret_cast<struct sockaddr*>(&addr), sizeof(addr)) < 0) { |
| 1057 |
sock_close(s); |
| 1058 |
replyStatus(ERR_NO_NETWORK); |
| 1059 |
return; |
| 1060 |
} |
| 1061 |
if (listen(s, 1) < 0) { |
| 1062 |
sock_close(s); |
| 1063 |
replyStatus(ERR_NO_NETWORK); |
| 1064 |
return; |
| 1065 |
} |
| 1066 |
newState = TcpState::Listen; |
| 1067 |
|
| 1068 |
|
| 1069 |
::socklen_t alen = sizeof(addr); |
| 1070 |
if (getsockname(s, reinterpret_cast<struct sockaddr*>(&addr), &alen) == 0) { |
| 1071 |
localPort = ntohs(addr.sin_port); |
| 1072 |
} else { |
| 1073 |
localPort = localPortReq; |
| 1074 |
} |
| 1075 |
} else { |
| 1076 |
|
| 1077 |
|
| 1078 |
sockaddr_in dest = sock_makeIPv4(ip, remotePort); |
| 1079 |
int ret = connect(s, reinterpret_cast<struct sockaddr*>(&dest), sizeof(dest)); |
| 1080 |
if (ret == 0) { |
| 1081 |
newState = TcpState::Established; |
| 1082 |
} else { |
| 1083 |
#ifdef _WIN32 |
| 1084 |
int err = WSAGetLastError(); |
| 1085 |
if (err == WSAEWOULDBLOCK) { |
| 1086 |
#else |
| 1087 |
if (errno == EINPROGRESS) { |
| 1088 |
#endif |
| 1089 |
newState = TcpState::SynSent; |
| 1090 |
} else { |
| 1091 |
sock_close(s); |
| 1092 |
replyStatus(ERR_NO_NETWORK); |
| 1093 |
return; |
| 1094 |
} |
| 1095 |
} |
| 1096 |
|
| 1097 |
struct sockaddr_in local; |
| 1098 |
::socklen_t len = sizeof(local); |
| 1099 |
if (getsockname(s, reinterpret_cast<struct sockaddr*>(&local), &len) == 0) { |
| 1100 |
localPort = ntohs(local.sin_port); |
| 1101 |
} |
| 1102 |
} |
| 1103 |
|
| 1104 |
{ |
| 1105 |
|
| 1106 |
|
| 1107 |
|
| 1108 |
|
| 1109 |
std::scoped_lock lock(c.mutex); |
| 1110 |
c.closeReason = CloseReason::None; |
| 1111 |
c.remoteIp = ip; |
| 1112 |
c.remotePort = remotePort; |
| 1113 |
c.localPort = localPort; |
| 1114 |
c.resident = resident; |
| 1115 |
c.recvBuf.clear(); |
| 1116 |
c.sendBuf.clear(); |
| 1117 |
c.finSent = false; |
| 1118 |
c.tcpState = newState; |
| 1119 |
c.sock = s; |
| 1120 |
} |
| 1121 |
poke(); |
| 1122 |
|
| 1123 |
setResult(OpenResult{.handle = static_cast<uint8_t>(h + 1)}); |
| 1124 |
} |
| 1125 |
|
| 1126 |
|
| 1127 |
|
| 1128 |
|
| 1129 |
|
| 1130 |
|
| 1131 |
void UnapiNet::cmdTcpSend() |
| 1132 |
{ |
| 1133 |
if (paramBuf.size() < sizeof(TcpSendParamHeader)) { |
| 1134 |
replyStatus(ERR_INV_PARAM); |
| 1135 |
return; |
| 1136 |
} |
| 1137 |
auto ph = fromBytes<TcpSendParamHeader>(paramBuf); |
| 1138 |
size_t len = ph.len; |
| 1139 |
if (paramBuf.size() != sizeof(TcpSendParamHeader) + len || |
| 1140 |
len > MAX_TRANSFER) { |
| 1141 |
replyStatus(ERR_INV_PARAM); |
| 1142 |
return; |
| 1143 |
} |
| 1144 |
|
| 1145 |
auto* cp = tcpForHandle(ph.handle); |
| 1146 |
if (!cp) { |
| 1147 |
replyStatus(ERR_NO_CONN); |
| 1148 |
return; |
| 1149 |
} |
| 1150 |
auto& c = *cp; |
| 1151 |
|
| 1152 |
|
| 1153 |
|
| 1154 |
const auto* data = paramBuf.data() + sizeof(TcpSendParamHeader); |
| 1155 |
bool failed = false; |
| 1156 |
bool queued = false; |
| 1157 |
uint8_t status = ERR_OK; |
| 1158 |
{ |
| 1159 |
std::scoped_lock lock(c.mutex); |
| 1160 |
SOCKET sd = c.sock; |
| 1161 |
if (sd == OPENMSX_INVALID_SOCKET) { |
| 1162 |
replyStatus(ERR_NO_CONN); |
| 1163 |
return; |
| 1164 |
} |
| 1165 |
|
| 1166 |
if (c.tcpState != one_of(TcpState::Established, TcpState::CloseWait)) { |
| 1167 |
replyStatus(ERR_CONN_STATE); |
| 1168 |
return; |
| 1169 |
} |
| 1170 |
|
| 1171 |
if (!c.sendBuf.empty()) { |
| 1172 |
|
| 1173 |
|
| 1174 |
|
| 1175 |
if (c.sendBuf.size() + len > MAX_SEND_BUF) { |
| 1176 |
status = ERR_BUFFER; |
| 1177 |
} else { |
| 1178 |
c.sendBuf.insert(c.sendBuf.end(), data, data + len); |
| 1179 |
queued = true; |
| 1180 |
} |
| 1181 |
} else { |
| 1182 |
|
| 1183 |
|
| 1184 |
|
| 1185 |
|
| 1186 |
|
| 1187 |
auto r = netSend(sd, data, len); |
| 1188 |
if (r || r.error() == IoError::WouldBlock) { |
| 1189 |
size_t sent = r.value_or(0); |
| 1190 |
c.sendBuf.insert(c.sendBuf.end(), data + sent, data + len); |
| 1191 |
queued = sent < len; |
| 1192 |
} else { |
| 1193 |
failed = true; |
| 1194 |
} |
| 1195 |
} |
| 1196 |
} |
| 1197 |
if (failed) { |
| 1198 |
|
| 1199 |
|
| 1200 |
requestClose(c, CloseReason::ConnectionReset); |
| 1201 |
replyStatus(ERR_CONN_STATE); |
| 1202 |
return; |
| 1203 |
} |
| 1204 |
if (queued) poke(); |
| 1205 |
|
| 1206 |
replyStatus(status); |
| 1207 |
} |
| 1208 |
|
| 1209 |
|
| 1210 |
|
| 1211 |
|
| 1212 |
|
| 1213 |
|
| 1214 |
|
| 1215 |
void UnapiNet::cmdTcpRecv() |
| 1216 |
{ |
| 1217 |
auto p = exactParams<TcpRecvParams>(); |
| 1218 |
if (!p) { |
| 1219 |
replyStatus(ERR_INV_PARAM); |
| 1220 |
return; |
| 1221 |
} |
| 1222 |
auto* cp = tcpForHandle(p->handle); |
| 1223 |
if (!cp) { |
| 1224 |
replyStatus(ERR_NO_CONN); |
| 1225 |
return; |
| 1226 |
} |
| 1227 |
auto& c = *cp; |
| 1228 |
|
| 1229 |
|
| 1230 |
|
| 1231 |
size_t maxlen = std::min(static_cast<size_t>(p->maxlen), MAX_TRANSFER); |
| 1232 |
|
| 1233 |
|
| 1234 |
|
| 1235 |
|
| 1236 |
|
| 1237 |
|
| 1238 |
|
| 1239 |
std::scoped_lock lock(c.mutex); |
| 1240 |
auto avail = static_cast<uint16_t>(std::min(maxlen, c.recvBuf.size())); |
| 1241 |
setResult(TcpRecvResultHeader{.actualLen = avail}); |
| 1242 |
resultBuf.insert(resultBuf.end(), c.recvBuf.begin(), |
| 1243 |
c.recvBuf.begin() + avail); |
| 1244 |
c.recvBuf.erase(c.recvBuf.begin(), c.recvBuf.begin() + avail); |
| 1245 |
} |
| 1246 |
|
| 1247 |
|
| 1248 |
|
| 1249 |
|
| 1250 |
|
| 1251 |
void UnapiNet::cmdTcpClose() |
| 1252 |
{ |
| 1253 |
if (paramBuf.size() != 1) { |
| 1254 |
replyStatus(ERR_INV_PARAM); |
| 1255 |
return; |
| 1256 |
} |
| 1257 |
int h = paramBuf[0]; |
| 1258 |
|
| 1259 |
if (h == 0) { |
| 1260 |
|
| 1261 |
|
| 1262 |
for (auto& c : tcp) { |
| 1263 |
if (!c.resident && c.sock != OPENMSX_INVALID_SOCKET) { |
| 1264 |
gracefulClose(c); |
| 1265 |
} |
| 1266 |
} |
| 1267 |
replyStatus(ERR_OK); |
| 1268 |
return; |
| 1269 |
} |
| 1270 |
|
| 1271 |
auto* cp = tcpForHandle(h); |
| 1272 |
if (!cp || cp->sock == OPENMSX_INVALID_SOCKET) { |
| 1273 |
replyStatus(ERR_NO_CONN); |
| 1274 |
return; |
| 1275 |
} |
| 1276 |
gracefulClose(*cp); |
| 1277 |
replyStatus(ERR_OK); |
| 1278 |
} |
| 1279 |
|
| 1280 |
|
| 1281 |
|
| 1282 |
|
| 1283 |
|
| 1284 |
|
| 1285 |
|
| 1286 |
|
| 1287 |
void UnapiNet::cmdTcpState() |
| 1288 |
{ |
| 1289 |
if (paramBuf.size() != 1) { |
| 1290 |
replyStatus(ERR_INV_PARAM); |
| 1291 |
return; |
| 1292 |
} |
| 1293 |
auto* cp = tcpForHandle(paramBuf[0]); |
| 1294 |
if (!cp) { |
| 1295 |
replyStatus(ERR_NO_CONN); |
| 1296 |
return; |
| 1297 |
} |
| 1298 |
auto& c = *cp; |
| 1299 |
|
| 1300 |
TcpStateResult r{}; |
| 1301 |
{ |
| 1302 |
|
| 1303 |
|
| 1304 |
std::scoped_lock lock(c.mutex); |
| 1305 |
r.state = static_cast<uint8_t>(c.tcpState.load()); |
| 1306 |
r.avail = static_cast<uint16_t>( |
| 1307 |
std::min(c.recvBuf.size(), static_cast<size_t>(0xFFFF))); |
| 1308 |
r.closeReason = static_cast<uint8_t>(c.closeReason); |
| 1309 |
r.remoteIp = c.remoteIp; |
| 1310 |
r.remotePort = c.remotePort; |
| 1311 |
r.localPort = c.localPort; |
| 1312 |
} |
| 1313 |
setResult(r); |
| 1314 |
} |
| 1315 |
|
| 1316 |
|
| 1317 |
|
| 1318 |
|
| 1319 |
|
| 1320 |
void UnapiNet::cmdTcpAbort() |
| 1321 |
{ |
| 1322 |
if (paramBuf.size() != 1) { |
| 1323 |
replyStatus(ERR_INV_PARAM); |
| 1324 |
return; |
| 1325 |
} |
| 1326 |
int h = paramBuf[0]; |
| 1327 |
|
| 1328 |
if (h == 0) { |
| 1329 |
|
| 1330 |
for (auto& c : tcp) { |
| 1331 |
if (!c.resident && c.sock != OPENMSX_INVALID_SOCKET) { |
| 1332 |
requestClose(c, CloseReason::Aborted); |
| 1333 |
} |
| 1334 |
} |
| 1335 |
replyStatus(ERR_OK); |
| 1336 |
return; |
| 1337 |
} |
| 1338 |
|
| 1339 |
auto* cp = tcpForHandle(h); |
| 1340 |
if (!cp || cp->sock == OPENMSX_INVALID_SOCKET) { |
| 1341 |
replyStatus(ERR_NO_CONN); |
| 1342 |
return; |
| 1343 |
} |
| 1344 |
requestClose(*cp, CloseReason::Aborted); |
| 1345 |
replyStatus(ERR_OK); |
| 1346 |
} |
| 1347 |
|
| 1348 |
|
| 1349 |
|
| 1350 |
|
| 1351 |
|
| 1352 |
void UnapiNet::cmdGetLocalIP() |
| 1353 |
{ |
| 1354 |
if (!paramBuf.empty()) { |
| 1355 |
replyStatus(ERR_INV_PARAM); |
| 1356 |
return; |
| 1357 |
} |
| 1358 |
setResult(GetLocalIpResult{.ip = sock_localIPv4()}); |
| 1359 |
} |
| 1360 |
|
| 1361 |
|
| 1362 |
|
| 1363 |
|
| 1364 |
|
| 1365 |
void UnapiNet::cmdNetState() |
| 1366 |
{ |
| 1367 |
if (!paramBuf.empty()) { |
| 1368 |
replyStatus(ERR_INV_PARAM); |
| 1369 |
return; |
| 1370 |
} |
| 1371 |
const std::array<uint8_t, 2> r{ERR_OK, 2}; |
| 1372 |
setResult(r); |
| 1373 |
} |
| 1374 |
|
| 1375 |
|
| 1376 |
|
| 1377 |
int UnapiNet::allocUdpHandle() |
| 1378 |
{ |
| 1379 |
for (int i = 0; i < MAX_UDP; i++) { |
| 1380 |
if (udp[i].sock == OPENMSX_INVALID_SOCKET) { |
| 1381 |
return i; |
| 1382 |
} |
| 1383 |
} |
| 1384 |
return INVALID_HANDLE; |
| 1385 |
} |
| 1386 |
|
| 1387 |
|
| 1388 |
void UnapiNet::closeUdp(UdpConnection& u) |
| 1389 |
{ |
| 1390 |
std::scoped_lock lock(u.mutex); |
| 1391 |
if (SOCKET sd = u.sock; sd != OPENMSX_INVALID_SOCKET) { |
| 1392 |
sock_close(sd); |
| 1393 |
u.sock = OPENMSX_INVALID_SOCKET; |
| 1394 |
} |
| 1395 |
u.localPort = 0; |
| 1396 |
u.resident = false; |
| 1397 |
u.recvQueue.clear(); |
| 1398 |
} |
| 1399 |
|
| 1400 |
|
| 1401 |
|
| 1402 |
|
| 1403 |
|
| 1404 |
|
| 1405 |
void UnapiNet::cmdUdpOpen() |
| 1406 |
{ |
| 1407 |
auto p = exactParams<UdpOpenParams>(); |
| 1408 |
if (!p) { |
| 1409 |
replyStatus(ERR_INV_PARAM); |
| 1410 |
return; |
| 1411 |
} |
| 1412 |
uint16_t localPort = p->localPort; |
| 1413 |
|
| 1414 |
|
| 1415 |
int h = allocUdpHandle(); |
| 1416 |
if (h == INVALID_HANDLE) { |
| 1417 |
replyStatus(ERR_NO_FREE_CONN); |
| 1418 |
return; |
| 1419 |
} |
| 1420 |
auto& u = udp[h]; |
| 1421 |
|
| 1422 |
SOCKET s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); |
| 1423 |
if (s == OPENMSX_INVALID_SOCKET) { |
| 1424 |
replyStatus(ERR_NO_NETWORK); |
| 1425 |
return; |
| 1426 |
} |
| 1427 |
|
| 1428 |
sock_setNonBlocking(s); |
| 1429 |
|
| 1430 |
|
| 1431 |
|
| 1432 |
|
| 1433 |
|
| 1434 |
sock_setIntOption(s, SOL_SOCKET, SO_BROADCAST); |
| 1435 |
|
| 1436 |
sockaddr_in addr = sock_makeIPv4(INADDR_ANY, localPort == 0xFFFF ? 0 : localPort); |
| 1437 |
if (bind(s, reinterpret_cast<struct sockaddr*>(&addr), sizeof(addr)) < 0) { |
| 1438 |
|
| 1439 |
|
| 1440 |
|
| 1441 |
|
| 1442 |
|
| 1443 |
|
| 1444 |
|
| 1445 |
addr.sin_port = 0; |
| 1446 |
if (bind(s, reinterpret_cast<struct sockaddr*>(&addr), sizeof(addr)) < 0) { |
| 1447 |
sock_close(s); |
| 1448 |
replyStatus(ERR_NO_NETWORK); |
| 1449 |
return; |
| 1450 |
} |
| 1451 |
} |
| 1452 |
|
| 1453 |
|
| 1454 |
const auto boundPort = [&]() -> uint16_t { |
| 1455 |
::socklen_t alen = sizeof(addr); |
| 1456 |
return (getsockname(s, reinterpret_cast<struct sockaddr*>(&addr), &alen) == 0) |
| 1457 |
? ntohs(addr.sin_port) |
| 1458 |
: localPort; |
| 1459 |
}(); |
| 1460 |
|
| 1461 |
{ |
| 1462 |
|
| 1463 |
std::scoped_lock lock(u.mutex); |
| 1464 |
u.localPort = boundPort; |
| 1465 |
u.resident = false; |
| 1466 |
u.recvQueue.clear(); |
| 1467 |
u.sock = s; |
| 1468 |
} |
| 1469 |
poke(); |
| 1470 |
|
| 1471 |
setResult(OpenResult{.handle = static_cast<uint8_t>(h + 1)}); |
| 1472 |
} |
| 1473 |
|
| 1474 |
|
| 1475 |
|
| 1476 |
|
| 1477 |
|
| 1478 |
void UnapiNet::cmdUdpClose() |
| 1479 |
{ |
| 1480 |
if (paramBuf.size() != 1) { |
| 1481 |
replyStatus(ERR_INV_PARAM); |
| 1482 |
return; |
| 1483 |
} |
| 1484 |
int h = paramBuf[0]; |
| 1485 |
|
| 1486 |
if (h == 0) { |
| 1487 |
for (auto& u : udp) { |
| 1488 |
if (!u.resident && u.sock != OPENMSX_INVALID_SOCKET) { |
| 1489 |
requestClose(u); |
| 1490 |
} |
| 1491 |
} |
| 1492 |
replyStatus(ERR_OK); |
| 1493 |
return; |
| 1494 |
} |
| 1495 |
|
| 1496 |
auto* up = udpForHandle(h); |
| 1497 |
if (!up || up->sock == OPENMSX_INVALID_SOCKET) { |
| 1498 |
replyStatus(ERR_NO_CONN); |
| 1499 |
return; |
| 1500 |
} |
| 1501 |
requestClose(*up); |
| 1502 |
replyStatus(ERR_OK); |
| 1503 |
} |
| 1504 |
|
| 1505 |
|
| 1506 |
|
| 1507 |
|
| 1508 |
|
| 1509 |
|
| 1510 |
|
| 1511 |
void UnapiNet::cmdUdpState() |
| 1512 |
{ |
| 1513 |
if (paramBuf.size() != 1) { |
| 1514 |
replyStatus(ERR_INV_PARAM); |
| 1515 |
return; |
| 1516 |
} |
| 1517 |
auto* up = udpForHandle(paramBuf[0]); |
| 1518 |
if (!up || up->sock == OPENMSX_INVALID_SOCKET) { |
| 1519 |
replyStatus(ERR_NO_CONN); |
| 1520 |
return; |
| 1521 |
} |
| 1522 |
auto& u = *up; |
| 1523 |
|
| 1524 |
const auto size = [&]() -> uint16_t { |
| 1525 |
std::scoped_lock lock(u.mutex); |
| 1526 |
if (u.recvQueue.empty()) return 0; |
| 1527 |
return static_cast<uint16_t>( |
| 1528 |
std::min(u.recvQueue.front().data.size(), |
| 1529 |
static_cast<size_t>(0xFFFF))); |
| 1530 |
}(); |
| 1531 |
setResult(UdpStateResult{.firstDgramSize = size}); |
| 1532 |
} |
| 1533 |
|
| 1534 |
|
| 1535 |
|
| 1536 |
|
| 1537 |
|
| 1538 |
|
| 1539 |
void UnapiNet::cmdUdpSend() |
| 1540 |
{ |
| 1541 |
if (paramBuf.size() < sizeof(UdpSendParamHeader)) { |
| 1542 |
replyStatus(ERR_INV_PARAM); |
| 1543 |
return; |
| 1544 |
} |
| 1545 |
auto ph = fromBytes<UdpSendParamHeader>(paramBuf); |
| 1546 |
size_t len = ph.len; |
| 1547 |
if (paramBuf.size() != sizeof(UdpSendParamHeader) + len) { |
| 1548 |
replyStatus(ERR_INV_PARAM); |
| 1549 |
return; |
| 1550 |
} |
| 1551 |
|
| 1552 |
auto* up = udpForHandle(ph.handle); |
| 1553 |
if (!up || up->sock == OPENMSX_INVALID_SOCKET) { |
| 1554 |
replyStatus(ERR_NO_CONN); |
| 1555 |
return; |
| 1556 |
} |
| 1557 |
auto& u = *up; |
| 1558 |
|
| 1559 |
sockaddr_in dest = sock_makeIPv4(ph.destIp, ph.destPort); |
| 1560 |
const char* data = reinterpret_cast<const char*>( |
| 1561 |
paramBuf.data() + sizeof(UdpSendParamHeader)); |
| 1562 |
int n; |
| 1563 |
{ |
| 1564 |
|
| 1565 |
std::scoped_lock lock(u.mutex); |
| 1566 |
SOCKET sd = u.sock; |
| 1567 |
if (sd == OPENMSX_INVALID_SOCKET) { |
| 1568 |
replyStatus(ERR_NO_CONN); |
| 1569 |
return; |
| 1570 |
} |
| 1571 |
n = sendto(sd, data, static_cast<int>(len), 0, |
| 1572 |
reinterpret_cast<struct sockaddr*>(&dest), sizeof(dest)); |
| 1573 |
} |
| 1574 |
|
| 1575 |
replyStatus(n == static_cast<int>(len) ? ERR_OK : ERR_NO_NETWORK); |
| 1576 |
} |
| 1577 |
|
| 1578 |
|
| 1579 |
|
| 1580 |
|
| 1581 |
|
| 1582 |
|
| 1583 |
|
| 1584 |
|
| 1585 |
void UnapiNet::cmdUdpRecv() |
| 1586 |
{ |
| 1587 |
auto p = exactParams<UdpRecvParams>(); |
| 1588 |
if (!p) { |
| 1589 |
replyStatus(ERR_INV_PARAM); |
| 1590 |
return; |
| 1591 |
} |
| 1592 |
uint16_t maxlen = p->maxlen; |
| 1593 |
auto* up = udpForHandle(p->handle); |
| 1594 |
if (!up || up->sock == OPENMSX_INVALID_SOCKET) { |
| 1595 |
replyStatus(ERR_NO_CONN); |
| 1596 |
return; |
| 1597 |
} |
| 1598 |
auto& u = *up; |
| 1599 |
|
| 1600 |
std::optional<UdpDatagram> dg; |
| 1601 |
{ |
| 1602 |
std::scoped_lock lock(u.mutex); |
| 1603 |
if (!u.recvQueue.empty()) { |
| 1604 |
dg = std::move(u.recvQueue.front()); |
| 1605 |
u.recvQueue.pop_front(); |
| 1606 |
} |
| 1607 |
} |
| 1608 |
if (!dg) { |
| 1609 |
replyStatus(ERR_NO_DATA); |
| 1610 |
return; |
| 1611 |
} |
| 1612 |
|
| 1613 |
|
| 1614 |
|
| 1615 |
|
| 1616 |
|
| 1617 |
|
| 1618 |
|
| 1619 |
auto copied = std::min(static_cast<size_t>(maxlen), dg->data.size()); |
| 1620 |
setResult(UdpRecvResultHeader{ |
| 1621 |
.srcIp = dg->srcIp, |
| 1622 |
.srcPort = dg->srcPort, |
| 1623 |
.actualLen = static_cast<uint16_t>(dg->data.size())}, |
| 1624 |
std::span<const uint8_t>(dg->data.data(), copied)); |
| 1625 |
} |
| 1626 |
|
| 1627 |
|
| 1628 |
|
| 1629 |
|
| 1630 |
|
| 1631 |
|
| 1632 |
|
| 1633 |
|
| 1634 |
void UnapiNet::icmpWorkerLoop() |
| 1635 |
{ |
| 1636 |
#ifdef _WIN32 |
| 1637 |
HANDLE hIcmp = icmpChannel; |
| 1638 |
|
| 1639 |
std::unique_lock lock(icmpMutex); |
| 1640 |
while (true) { |
| 1641 |
icmpCv.wait(lock, [&] { return !running || icmpPending; }); |
| 1642 |
if (!running) return; |
| 1643 |
icmpPending = false; |
| 1644 |
IcmpRequest req = icmpRequest; |
| 1645 |
|
| 1646 |
uint32_t gen = icmpGeneration; |
| 1647 |
lock.unlock(); |
| 1648 |
|
| 1649 |
std::vector<uint8_t> payload(req.dataLen); |
| 1650 |
for (size_t i = 0; i < payload.size(); i++) |
| 1651 |
payload[i] = static_cast<uint8_t>(i); |
| 1652 |
|
| 1653 |
unsigned long replySize = sizeof(ICMP_ECHO_REPLY) + req.dataLen + 8; |
| 1654 |
std::vector<uint8_t> replyBuf(replySize); |
| 1655 |
|
| 1656 |
IP_OPTION_INFORMATION opt = {}; |
| 1657 |
opt.Ttl = req.ttl ? req.ttl : 255; |
| 1658 |
|
| 1659 |
unsigned long ret = IcmpSendEcho(hIcmp, |
| 1660 |
htonl(req.dstIp), |
| 1661 |
payload.empty() ? nullptr : payload.data(), |
| 1662 |
static_cast<unsigned short>(payload.size()), |
| 1663 |
&opt, |
| 1664 |
replyBuf.data(), |
| 1665 |
replySize, |
| 1666 |
2000); |
| 1667 |
|
| 1668 |
std::optional<IcmpReply> r; |
| 1669 |
if (ret > 0) { |
| 1670 |
auto* reply = reinterpret_cast<ICMP_ECHO_REPLY*>(replyBuf.data()); |
| 1671 |
if (reply->Status == IP_SUCCESS) { |
| 1672 |
r = IcmpReply{ |
| 1673 |
.srcIp = ntohl(reply->Address), |
| 1674 |
.ttl = reply->Options.Ttl, |
| 1675 |
.identifier = req.identifier, |
| 1676 |
.sequence = req.sequence, |
| 1677 |
.dataLen = reply->DataSize}; |
| 1678 |
} |
| 1679 |
} |
| 1680 |
|
| 1681 |
lock.lock(); |
| 1682 |
if (r && icmpGeneration == gen) { |
| 1683 |
|
| 1684 |
if (icmpReplies.size() >= MAX_ICMP_QUEUE) { |
| 1685 |
|
| 1686 |
|
| 1687 |
icmpReplies.pop_front(); |
| 1688 |
} |
| 1689 |
icmpReplies.push_back(*r); |
| 1690 |
} |
| 1691 |
} |
| 1692 |
#endif |
| 1693 |
} |
| 1694 |
|
| 1695 |
|
| 1696 |
|
| 1697 |
|
| 1698 |
|
| 1699 |
|
| 1700 |
void UnapiNet::cmdIcmpSend() |
| 1701 |
{ |
| 1702 |
auto p = exactParams<IcmpSendParams>(); |
| 1703 |
if (!p) { |
| 1704 |
replyStatus(ERR_INV_PARAM); |
| 1705 |
return; |
| 1706 |
} |
| 1707 |
if (!icmpAvailable) { |
| 1708 |
|
| 1709 |
replyStatus(ERR_NOT_IMP); |
| 1710 |
return; |
| 1711 |
} |
| 1712 |
{ |
| 1713 |
|
| 1714 |
|
| 1715 |
std::scoped_lock lock(icmpMutex); |
| 1716 |
icmpRequest.dstIp = p->dstIp; |
| 1717 |
icmpRequest.ttl = p->ttl; |
| 1718 |
icmpRequest.identifier = p->identifier; |
| 1719 |
icmpRequest.sequence = p->sequence; |
| 1720 |
icmpRequest.dataLen = std::min<uint16_t>(p->len, 512); |
| 1721 |
icmpPending = true; |
| 1722 |
} |
| 1723 |
icmpCv.notify_one(); |
| 1724 |
replyStatus(ERR_OK); |
| 1725 |
} |
| 1726 |
|
| 1727 |
|
| 1728 |
|
| 1729 |
|
| 1730 |
|
| 1731 |
|
| 1732 |
void UnapiNet::cmdIcmpRecv() |
| 1733 |
{ |
| 1734 |
if (!paramBuf.empty()) { |
| 1735 |
replyStatus(ERR_INV_PARAM); |
| 1736 |
return; |
| 1737 |
} |
| 1738 |
if (!icmpAvailable) { |
| 1739 |
replyStatus(ERR_NOT_IMP); |
| 1740 |
return; |
| 1741 |
} |
| 1742 |
|
| 1743 |
std::optional<IcmpReply> r; |
| 1744 |
{ |
| 1745 |
std::scoped_lock lock(icmpMutex); |
| 1746 |
if (!icmpReplies.empty()) { |
| 1747 |
r = icmpReplies.front(); |
| 1748 |
icmpReplies.pop_front(); |
| 1749 |
} |
| 1750 |
} |
| 1751 |
if (!r) { |
| 1752 |
replyStatus(ERR_NO_DATA); |
| 1753 |
return; |
| 1754 |
} |
| 1755 |
|
| 1756 |
setResult(IcmpRecvResult{ |
| 1757 |
.srcIp = r->srcIp, |
| 1758 |
.ttl = r->ttl, |
| 1759 |
.identifier = r->identifier, |
| 1760 |
.sequence = r->sequence, |
| 1761 |
.dataLen = r->dataLen}); |
| 1762 |
} |
| 1763 |
|
| 1764 |
|
| 1765 |
|
| 1766 |
|
| 1767 |
|
| 1768 |
template<typename Archive> |
| 1769 |
void UnapiNet::serialize(Archive& ar, unsigned ) |
| 1770 |
{ |
| 1771 |
ar.template serializeBase<MSXDevice>(*this); |
| 1772 |
|
| 1773 |
} |
| 1774 |
|
| 1775 |
INSTANTIATE_SERIALIZE_METHODS(UnapiNet); |
| 1776 |
REGISTER_MSXDEVICE(UnapiNet, "UnapiNet"); |
| 1777 |
|
| 1778 |
} |