| 462 | } |
| 463 | |
| 464 | bool SocketAdapter::SendUDP(ConnectionKey Key, IP_Packet* ipPkt) |
| 465 | { |
| 466 | IP_PayloadPtr* ipPayload = static_cast<IP_PayloadPtr*>(ipPkt->GetPayload()); |
| 467 | UDP_Packet udp(ipPayload->data, ipPayload->GetLength()); |
| 468 | |
| 469 | Key.ps2Port = udp.sourcePort; |
| 470 | Key.srvPort = udp.destinationPort; |
| 471 | |
| 472 | const int res = SendFromConnection(Key, ipPkt); |
| 473 | if (res == 1) |
| 474 | return true; |
| 475 | else if (res == 0) |
| 476 | return false; |
| 477 | else |
| 478 | { |
| 479 | // Always bind the UDP source port |
| 480 | // PS2 software can run into issues if the source port is not preserved |
| 481 | UDP_FixedPort* fPort = nullptr; |
| 482 | BaseSession* fSession; |
| 483 | if (fixedUDPPorts.TryGetValue(udp.sourcePort, &fSession)) |
| 484 | { |
| 485 | fPort = static_cast<UDP_FixedPort*>(fSession); |
| 486 | } |
| 487 | else |
| 488 | { |
| 489 | ConnectionKey fKey{}; |
| 490 | fKey.protocol = static_cast<u8>(IP_Type::UDP); |
| 491 | fKey.ps2Port = udp.sourcePort; |
| 492 | fKey.srvPort = 0; |
| 493 | |
| 494 | Console.WriteLn("DEV9: Socket: Binding UDP fixed port %d", udp.sourcePort); |
| 495 | |
| 496 | fPort = new UDP_FixedPort(fKey, adapterIP, udp.sourcePort); |
| 497 | fPort->AddConnectionClosedHandler([&](BaseSession* session) { HandleFixedPortClosed(session); }); |
| 498 | |
| 499 | fPort->destIP = {}; |
| 500 | fPort->sourceIP = dhcpServer.ps2IP; |
| 501 | |
| 502 | connections.Add(fKey, fPort); |
| 503 | fixedUDPPorts.Add(udp.sourcePort, fPort); |
| 504 | |
| 505 | fPort->Init(); |
| 506 | } |
| 507 | |
| 508 | Console.WriteLn("DEV9: Socket: Creating New UDP Connection from fixed port %d to %d", udp.sourcePort, udp.destinationPort); |
| 509 | UDP_Session* s = fPort->NewClientSession(Key, |
| 510 | ipPkt->destinationIP == dhcpServer.broadcastIP || ipPkt->destinationIP == IP_Address{{{255, 255, 255, 255}}}, |
| 511 | (ipPkt->destinationIP.bytes[0] & 0xF0) == 0xE0); |
| 512 | |
| 513 | // If we are unable to bind to the port, fall back to a dynamic port |
| 514 | if (s == nullptr) |
| 515 | { |
| 516 | Console.Error("DEV9: Socket: Failed to Create New UDP Connection from fixed port"); |
| 517 | Console.WriteLn("DEV9: Socket: Retrying with dynamic port to %d", udp.destinationPort); |
| 518 | s = new UDP_Session(Key, adapterIP); |
| 519 | } |
| 520 | |
| 521 | s->AddConnectionClosedHandler([&](BaseSession* session) { HandleConnectionClosed(session); }); |
nothing calls this directly
no test coverage detected