| 362 | } |
| 363 | |
| 364 | U32 KNetLinkObject::recvmsg(KThread* thread, const KFileDescriptorPtr& fd, U32 address, U32 flags) { |
| 365 | MsgHdr hdr = {}; |
| 366 | U32 result = 0; |
| 367 | KMemory* memory = thread->memory; |
| 368 | |
| 369 | BOXEDWINE_CRITICAL_SECTION_WITH_CONDITION(this->lockCond); |
| 370 | while (1) { |
| 371 | if (this->recvBuffer.size()) { |
| 372 | readMsgHdr(thread, address, &hdr); |
| 373 | for (U32 i = 0; i < hdr.msg_iovlen; i++) { |
| 374 | U32 p = memory->readd(hdr.msg_iov + 8 * i); |
| 375 | U32 len = memory->readd(hdr.msg_iov + 8 * i + 4); |
| 376 | |
| 377 | U32 count = std::min(len, (U32)this->recvBuffer.size()); |
| 378 | memory->performOnMemory(p, count, false, [this](U8* ram, U32 len) { |
| 379 | std::copy(this->recvBuffer.begin(), this->recvBuffer.begin() + len, ram); |
| 380 | this->recvBuffer.erase(this->recvBuffer.begin(), this->recvBuffer.begin() + len); |
| 381 | return true; |
| 382 | }); |
| 383 | result += count; |
| 384 | } |
| 385 | if (this->type == K_SOCK_STREAM) |
| 386 | memory->writed(address + 4, 0); // msg_namelen, set to 0 for connected sockets |
| 387 | memory->writed(address + 20, 0); // msg_controllen |
| 388 | U32 addr = memory->readd(address); |
| 389 | if (addr && afBound) { |
| 390 | memory->writew(addr, this->afBound->nl_family); |
| 391 | memory->writew(addr + 2, 0); |
| 392 | memory->writed(addr + 4, 0); // pid from kernel |
| 393 | memory->writed(addr + 8, this->afBound->nl_groups); |
| 394 | memory->writed(address + 4, 12); |
| 395 | } |
| 396 | return result; |
| 397 | } |
| 398 | if (this->inClosed) { |
| 399 | return 0; |
| 400 | } |
| 401 | if (!this->blocking) { |
| 402 | return -K_EWOULDBLOCK; |
| 403 | } |
| 404 | // :TODO: what about a time out |
| 405 | BOXEDWINE_CONDITION_WAIT(this->lockCond); |
| 406 | #ifdef BOXEDWINE_MULTI_THREADED |
| 407 | if (thread->terminating) { |
| 408 | return -K_EINTR; |
| 409 | } |
| 410 | if (thread->startSignal) { |
| 411 | thread->startSignal = false; |
| 412 | return -K_CONTINUE; |
| 413 | } |
| 414 | #endif |
| 415 | } |
| 416 | BOXEDWINE_CONDITION_SIGNAL_ALL(this->lockCond); |
| 417 | return result; |
| 418 | } |
| 419 | |
| 420 | struct boxed_nlmsghdr |
| 421 | { |