| 258 | } |
| 259 | |
| 260 | bool SocketAdapter::send(NetPacket* pkt) |
| 261 | { |
| 262 | InspectSend(pkt); |
| 263 | if (NetAdapter::send(pkt)) |
| 264 | return true; |
| 265 | |
| 266 | pxAssert(std::this_thread::get_id() == sendThreadId); |
| 267 | ScopedGuard cleanup([&]() { |
| 268 | // Garbage collect closed connections |
| 269 | if (deleteQueueSendThread.size() != 0) |
| 270 | { |
| 271 | std::lock_guard deletelock(deleteSendSentry); |
| 272 | for (BaseSession* s : deleteQueueSendThread) |
| 273 | delete s; |
| 274 | deleteQueueSendThread.clear(); |
| 275 | } |
| 276 | }); |
| 277 | |
| 278 | EthernetFrame frame(pkt); |
| 279 | |
| 280 | switch (frame.protocol) |
| 281 | { |
| 282 | case static_cast<u16>(EtherType::null): |
| 283 | case 0x0C00: |
| 284 | //Packets with the above ethertypes get sent when the adapter is reset |
| 285 | //Catch them here instead of printing an error |
| 286 | return true; |
| 287 | case static_cast<int>(EtherType::IPv4): |
| 288 | { |
| 289 | PayloadPtr* payload = static_cast<PayloadPtr*>(frame.GetPayload()); |
| 290 | IP_Packet ippkt(payload->data, payload->GetLength()); |
| 291 | |
| 292 | return SendIP(&ippkt); |
| 293 | } |
| 294 | case static_cast<u16>(EtherType::ARP): |
| 295 | { |
| 296 | PayloadPtr* payload = static_cast<PayloadPtr*>(frame.GetPayload()); |
| 297 | ARP_Packet arpPkt(payload->data, payload->GetLength()); |
| 298 | |
| 299 | if (arpPkt.protocol == static_cast<u16>(EtherType::IPv4)) |
| 300 | { |
| 301 | if (arpPkt.op == 1) //ARP request |
| 302 | { |
| 303 | if (*(IP_Address*)arpPkt.targetProtocolAddress.get() != dhcpServer.ps2IP) |
| 304 | //it's trying to resolve the virtual gateway's mac addr |
| 305 | { |
| 306 | ARP_Packet* arpRet = new ARP_Packet(6, 4); |
| 307 | *(MAC_Address*)arpRet->targetHardwareAddress.get() = *(MAC_Address*)arpPkt.senderHardwareAddress.get(); |
| 308 | *(MAC_Address*)arpRet->senderHardwareAddress.get() = internalMAC; |
| 309 | *(IP_Address*)arpRet->targetProtocolAddress.get() = *(IP_Address*)arpPkt.senderProtocolAddress.get(); |
| 310 | *(IP_Address*)arpRet->senderProtocolAddress.get() = *(IP_Address*)arpPkt.targetProtocolAddress.get(); |
| 311 | arpRet->op = 2, |
| 312 | arpRet->protocol = arpPkt.protocol; |
| 313 | arpRet->hardwareType = arpPkt.hardwareType; |
| 314 | |
| 315 | EthernetFrame* retARP = new EthernetFrame(arpRet); |
| 316 | retARP->destinationMAC = ps2MAC; |
| 317 | retARP->sourceMAC = internalMAC; |