Skip to content

Commit

Permalink
PSN connection made portable for Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
streetpea committed Apr 24, 2024
1 parent 6c01c7b commit 45f22a9
Show file tree
Hide file tree
Showing 13 changed files with 92 additions and 74 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/build-msys2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,10 @@ jobs:
cp /mingw64/bin/libjxl*.dll Chiaki4deck-Win/
cp /mingw64/bin/libzvbi-*.dll Chiaki4deck-Win/
cp /mingw64/bin/libhwy*.dll Chiaki4deck-Win/
cp /mingw64/bin/libminiupnpc.dll Chiaki4deck-Win/
cp /mingw64/bin/libpsl-*.dll Chiaki4deck-Win/
cp /mingw64/bin/libssh2-*.dll Chiaki4deck-Win/
cp /mingw64/bin/libjson-c-*.dll Chiaki4deck-Win/
/mingw64/bin/windeployqt6.exe --no-translations --qmldir=$PWD/gui/src/qml --release Chiaki4deck-Win/chiaki.exe
cp -R /mingw64/share/qt6/qml/QtQuick Chiaki4deck-Win/qml/
cp -R /mingw64/share/qt6/qml/QtQml/WorkerScript/ Chiaki4deck-Win/qml/QtQml/
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/build-windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ jobs:
-DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE `
-DCMAKE_BUILD_TYPE=Release `
-DCHIAKI_ENABLE_CLI=OFF `
-DCHIAKI_ENABLE_STEAM_SHORTCUT=OFF `
-DCHIAKI_GUI_ENABLE_SDL_GAMECONTROLLER=ON `
-DCHIAKI_ENABLE_STEAMDECK_NATIVE=OFF `
-DCHIAKI_USE_SYSTEM_CURL `
Expand All @@ -156,6 +155,7 @@ jobs:
run: |
mkdir Chiaki4deck-VC
cp build\gui\chiaki.exe Chiaki4deck-VC
cp build\third-party\cpp-steam-tools\libcpp-steam-tools.dll Chiaki4deck-VC
cp "${{ github.workspace }}\vcpkg_installed\${{ env.triplet }}\bin\libcrypto-*-x64.dll" Chiaki4deck-VC
cp "${{ github.workspace }}\vcpkg_installed\${{ env.triplet }}\bin\libssl-*-x64.dll" Chiaki4deck-VC
cp "${{ github.workspace }}\vcpkg_installed\${{ env.triplet }}\bin\SDL2.dll" Chiaki4deck-VC
Expand All @@ -164,6 +164,8 @@ jobs:
cp "${{ github.workspace }}\vcpkg_installed\${{ env.triplet }}\bin\opus.dll" Chiaki4deck-VC
cp "${{ github.workspace }}\vcpkg_installed\${{ env.triplet }}\bin\libspeexdsp.dll" Chiaki4deck-VC
cp "${{ github.workspace }}\vcpkg_installed\${{ env.triplet }}\bin\lcms2.dll" Chiaki4deck-VC
cp "${{ github.workspace }}\vcpkg_installed\${{ env.triplet }}\bin\libminiupnpc.dll Chiaki4deck-VC
cp "${{ github.workspace }}\vcpkg_installed\${{ env.triplet }}\bin\libjson-c-*.dll Chiaki4deck-VC
cp "${{ github.workspace }}\${{ env.dep_folder }}\bin\swresample-*.dll" Chiaki4deck-VC
cp "${{ github.workspace }}\${{ env.dep_folder }}\bin\avcodec-*.dll" Chiaki4deck-VC
cp "${{ github.workspace }}\${{ env.dep_folder }}\bin\avutil-*.dll" Chiaki4deck-VC
Expand Down
4 changes: 2 additions & 2 deletions lib/include/chiaki/sock.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ typedef SOCKET chiaki_socket_t;
#define CHIAKI_SOCKET_ERROR_FMT "%d"
#define CHIAKI_SOCKET_ERROR_VALUE (WSAGetLastError())
#define CHIAKI_SOCKET_EINPROGRESS (WSAGetLastError() == WSAEWOULDBLOCK)
#define SOCKET_BUF_TYPE (char*)
#define CHIAKI_SOCKET_BUF_TYPE char*
#else
#include <unistd.h>
#include <errno.h>
Expand All @@ -31,7 +31,7 @@ typedef int chiaki_socket_t;
#define CHIAKI_SOCKET_ERROR_FMT "%s"
#define CHIAKI_SOCKET_ERROR_VALUE (strerror(errno))
#define CHIAKI_SOCKET_EINPROGRESS (errno == EINPROGRESS)
#define SOCKET_BUF_TYPE (void *)
#define CHIAKI_SOCKET_BUF_TYPE void *
#endif

CHIAKI_EXPORT ChiakiErrorCode chiaki_socket_set_nonblock(chiaki_socket_t sock, bool nonblock);
Expand Down
15 changes: 8 additions & 7 deletions lib/src/ctrl.c
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ static void *ctrl_thread_func(void *user)
}
else
{
received = recv(ctrl->sock, ctrl->recv_buf + ctrl->recv_buf_size, sizeof(ctrl->recv_buf) - ctrl->recv_buf_size, 0);
received = recv(ctrl->sock, (CHIAKI_SOCKET_BUF_TYPE)ctrl->recv_buf + ctrl->recv_buf_size, sizeof(ctrl->recv_buf) - ctrl->recv_buf_size, 0);
if(received <= 0)
{
if(received < 0)
Expand Down Expand Up @@ -565,7 +565,7 @@ static ChiakiErrorCode ctrl_message_send(ChiakiCtrl *ctrl, uint16_t type, const
}
else
{
int sent = send(ctrl->sock, header, sizeof(header), 0);
int sent = send(ctrl->sock, (CHIAKI_SOCKET_BUF_TYPE)header, sizeof(header), 0);
if(sent < 0)
{
CHIAKI_LOGE(ctrl->session->log, "Failed to send Ctrl Message Header");
Expand All @@ -574,7 +574,7 @@ static ChiakiErrorCode ctrl_message_send(ChiakiCtrl *ctrl, uint16_t type, const

if(enc)
{
sent = send(ctrl->sock, enc, payload_size, 0);
sent = send(ctrl->sock, (CHIAKI_SOCKET_BUF_TYPE)enc, payload_size, 0);
free(enc);
if(sent < 0)
{
Expand Down Expand Up @@ -960,11 +960,12 @@ static ChiakiErrorCode ctrl_connect(ChiakiCtrl *ctrl)
ChiakiSession *session = ctrl->session;
uint16_t remote_counter = 0;
uint16_t local_counter = 0;
ChiakiErrorCode err = CHIAKI_ERR_SUCCESS;

if(session->rudp)
{
CHIAKI_LOGI(session->log, "Starting RUDP ctrl start session for %s", session->connect_info.ps5 ? "PS5" : "PS4");
ChiakiErrorCode err = chiaki_rudp_send_init_message(session->rudp, &local_counter);
err = chiaki_rudp_send_init_message(session->rudp, &local_counter);
if(err != CHIAKI_ERR_SUCCESS)
{
CHIAKI_LOGE(session->log, "Failed to send rudp ctrl start init message");
Expand Down Expand Up @@ -1064,7 +1065,7 @@ static ChiakiErrorCode ctrl_connect(ChiakiCtrl *ctrl)
return CHIAKI_ERR_NETWORK;
}

ChiakiErrorCode err = chiaki_socket_set_nonblock(sock, true);
err = chiaki_socket_set_nonblock(sock, true);
if(err != CHIAKI_ERR_SUCCESS)
CHIAKI_LOGE(session->log, "Failed to set ctrl socket to non-blocking: %s", chiaki_error_string(err));

Expand Down Expand Up @@ -1096,7 +1097,7 @@ static ChiakiErrorCode ctrl_connect(ChiakiCtrl *ctrl)
}

uint8_t auth_enc[CHIAKI_RPCRYPT_KEY_SIZE];
ChiakiErrorCode err = chiaki_rpcrypt_encrypt(&session->rpcrypt, ctrl->crypt_counter_local++, (const uint8_t *)session->connect_info.regist_key, auth_enc, CHIAKI_RPCRYPT_KEY_SIZE);
err = chiaki_rpcrypt_encrypt(&session->rpcrypt, ctrl->crypt_counter_local++, (const uint8_t *)session->connect_info.regist_key, auth_enc, CHIAKI_RPCRYPT_KEY_SIZE);
if(err != CHIAKI_ERR_SUCCESS)
goto error;
char auth_b64[CHIAKI_RPCRYPT_KEY_SIZE*2];
Expand Down Expand Up @@ -1227,7 +1228,7 @@ static ChiakiErrorCode ctrl_connect(ChiakiCtrl *ctrl)
}
else
{
int sent = send(ctrl->sock, buf, (size_t)request_len, 0);
int sent = send(ctrl->sock, (CHIAKI_SOCKET_BUF_TYPE)buf, (size_t)request_len, 0);
if(sent < 0)
{
CHIAKI_LOGE(session->log, "Failed to send ctrl request");
Expand Down
10 changes: 5 additions & 5 deletions lib/src/regist.c
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ static void *regist_thread_func(void *user)
}
else
{
int s = send(sock, request_header, request_header_size, 0);
int s = send(sock, (CHIAKI_SOCKET_BUF_TYPE)request_header, request_header_size, 0);
if(s < 0)
{
#ifdef _WIN32
Expand All @@ -442,7 +442,7 @@ static void *regist_thread_func(void *user)
}
else
{
int s = send(sock, payload, payload_size, 0);
int s = send(sock, (CHIAKI_SOCKET_BUF_TYPE)payload, payload_size, 0);
if(s < 0)
{
#ifdef _WIN32
Expand Down Expand Up @@ -543,7 +543,7 @@ static ChiakiErrorCode regist_search(ChiakiRegist *regist, struct addrinfo *addr
}

uint8_t buf[0x100];
int n = recvfrom(sock, buf, sizeof(buf) - 1, 0, recv_addr, recv_addr_size);
int n = recvfrom(sock, (CHIAKI_SOCKET_BUF_TYPE)buf, sizeof(buf) - 1, 0, recv_addr, recv_addr_size);
if(n <= 0)
{
if(n < 0)
Expand Down Expand Up @@ -599,7 +599,7 @@ static chiaki_socket_t regist_search_connect(ChiakiRegist *regist, struct addrin
if(regist->info.broadcast)
{
const int broadcast = 1;
int r = setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (const void *)&broadcast, sizeof(broadcast));
int r = setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (const CHIAKI_SOCKET_BUF_TYPE)&broadcast, sizeof(broadcast));
if(r < 0)
{
#ifdef _WIN32
Expand Down Expand Up @@ -792,7 +792,7 @@ static ChiakiErrorCode regist_recv_response(ChiakiRegist *regist, ChiakiRegister
return err;
}

int received = recv(sock, buf + buf_filled_size, (content_size + header_size) - buf_filled_size, 0);
int received = recv(sock, (CHIAKI_SOCKET_BUF_TYPE)buf + buf_filled_size, (content_size + header_size) - buf_filled_size, 0);
if(received <= 0)
{
CHIAKI_LOGE(regist->log, "Regist failed to receive response content");
Expand Down
24 changes: 14 additions & 10 deletions lib/src/remote/holepunch.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
*
* The hole punching process itself i
*/

// TODO: Make portable for Windows

// TODO: Make portable for Switch

#include <string.h>
Expand Down Expand Up @@ -1305,7 +1304,7 @@ static void* websocket_thread_func(void *user) {
FD_SET(sockfd, &fds);

// Need to send a ping every 5secs
struct timeval timeout = { .tv_sec = WEBSOCKET_PING_INTERVAL_SEC, .tv_usec = 0 };
uint32_t timeout = WEBSOCKET_PING_INTERVAL_SEC * 1000;
struct timespec ts;
uint64_t now = 0;
uint64_t last_ping_sent = 0;
Expand Down Expand Up @@ -1347,7 +1346,8 @@ static void* websocket_thread_func(void *user) {
{
if (res == CURLE_AGAIN)
{
if (select(sockfd + 1, &fds, NULL, NULL, &timeout) == -1)
ChiakiErrorCode err = chiaki_stop_pipe_select_single(&session->select_pipe, sockfd, false, timeout);
if(err != CHIAKI_ERR_SUCCESS && err != CHIAKI_ERR_TIMEOUT)
{
CHIAKI_LOGE(session->log, "websocket_thread_func: Select failed.");
goto cleanup_json;
Expand Down Expand Up @@ -2328,7 +2328,7 @@ static ChiakiErrorCode check_candidates(
continue;
}

if (send(sock, SOCKET_BUF_TYPE request_buf[0], sizeof(request_buf[0]), 0) < 0)
if (send(sock, (CHIAKI_SOCKET_BUF_TYPE) request_buf[0], sizeof(request_buf[0]), 0) < 0)
{
CHIAKI_LOGE(session->log, "check_candidate: Sending request failed for %s:%d with error: " CHIAKI_SOCKET_ERROR_FMT, candidate->addr, candidate->port, CHIAKI_SOCKET_ERROR_VALUE);
err = CHIAKI_ERR_NETWORK;
Expand Down Expand Up @@ -2363,7 +2363,11 @@ static ChiakiErrorCode check_candidates(
while (!selected_candidate)
{
int ret = select(maxfd, &fds, NULL, NULL, &tv);
if (ret < 0)
#ifdef _WIN32
if (ret < 0 && WSAGetLastError() != WSAEINTR)
#else
if (ret < 0 && errno != EINTR)
#endif
{
CHIAKI_LOGE(session->log, "check_candidate: Select failed");
err = CHIAKI_ERR_NETWORK;
Expand Down Expand Up @@ -2401,7 +2405,7 @@ static ChiakiErrorCode check_candidates(
}

CHIAKI_LOGD(session->log, "check_candidate: Receiving data from %s:%d", candidate->addr, candidate->port);
ssize_t response_len = recv(candidate_sock, SOCKET_BUF_TYPE response_buf, sizeof(response_buf), 0);
ssize_t response_len = recv(candidate_sock, (CHIAKI_SOCKET_BUF_TYPE) response_buf, sizeof(response_buf), 0);
if (response_len < 0)
{
CHIAKI_LOGE(session->log, "check_candidate: Receiving response from %s:%d failed with error: " CHIAKI_SOCKET_ERROR_FMT, candidate->addr, candidate->port, CHIAKI_SOCKET_ERROR_VALUE);
Expand Down Expand Up @@ -2452,7 +2456,7 @@ static ChiakiErrorCode check_candidates(
}
else
{
if (send(candidate_sock, SOCKET_BUF_TYPE request_buf[responses], sizeof(request_buf[responses]), 0) < 0)
if (send(candidate_sock, (CHIAKI_SOCKET_BUF_TYPE) request_buf[responses], sizeof(request_buf[responses]), 0) < 0)
{
CHIAKI_LOGE(session->log, "check_candidate: Sending request failed for %s:%d with error: " CHIAKI_SOCKET_ERROR_FMT, candidate->addr, candidate->port, CHIAKI_SOCKET_ERROR_VALUE);
err = CHIAKI_ERR_NETWORK;
Expand Down Expand Up @@ -2538,7 +2542,7 @@ static ChiakiErrorCode send_response_ps(Session *session, uint8_t *req, chiaki_s
CHIAKI_LOGI(session->log, "Our Response");
chiaki_log_hexdump(session->log, CHIAKI_LOG_INFO, confirm_buf, sizeof(confirm_buf));

if (send(*sock, SOCKET_BUF_TYPE confirm_buf, sizeof(confirm_buf), 0) < 0)
if (send(*sock, (CHIAKI_SOCKET_BUF_TYPE) confirm_buf, sizeof(confirm_buf), 0) < 0)
{
CHIAKI_LOGE(session->log, "check_candidate: Sending confirmation failed for %s:%d with error: %s", candidate->addr, candidate->port, CHIAKI_SOCKET_ERROR_VALUE);
return CHIAKI_ERR_NETWORK;
Expand Down Expand Up @@ -2571,7 +2575,7 @@ static ChiakiErrorCode receive_request_send_response_ps(Session *session, chiaki
if(err != CHIAKI_ERR_SUCCESS)
return err;

len = recv(*sock, SOCKET_BUF_TYPE req, sizeof(req), 0);
len = recv(*sock, (CHIAKI_SOCKET_BUF_TYPE) req, sizeof(req), 0);
if (len != sizeof(req))
{
CHIAKI_LOGE(session->log, "check_candidate: Received request of unexpected size %ld from %s:%d", len, candidate->addr, candidate->port);
Expand Down
4 changes: 2 additions & 2 deletions lib/src/remote/rudp.c
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_rudp_send_raw(RudpInstance *rudp, uint8_t *
{
return CHIAKI_ERR_DISCONNECTED;
}
int sent = send(rudp->sock, SOCKET_BUF_TYPE buf, buf_size, 0);
int sent = send(rudp->sock, (CHIAKI_SOCKET_BUF_TYPE) buf, buf_size, 0);
if(sent < 0)
{
#ifdef _WIN32
Expand All @@ -361,7 +361,7 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_rudp_recv(RudpInstance *rudp, size_t buf_si
return err;
}

int received_sz = recv(rudp->sock, SOCKET_BUF_TYPE buf, buf_size, 0);
int received_sz = recv(rudp->sock, (CHIAKI_SOCKET_BUF_TYPE) buf, buf_size, 0);
if(received_sz <= 8)
{
if(received_sz < 0)
Expand Down
10 changes: 4 additions & 6 deletions lib/src/remote/stun.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,30 +134,28 @@ static bool stun_get_external_address_from_server(ChiakiLog *log, StunServer *se

//uint8_t* transaction_id = &binding_req[8];

ssize_t sent = sendto(sock, SOCKET_BUF_TYPE binding_req, sizeof(binding_req), 0, (struct sockaddr*)server_addr, sizeof(struct sockaddr_in));
ssize_t sent = sendto(sock, (CHIAKI_SOCKET_BUF_TYPE)binding_req, sizeof(binding_req), 0, (struct sockaddr*)server_addr, sizeof(struct sockaddr_in));
if (sent != sizeof(binding_req)) {
CHIAKI_LOGE(log, "remote/stun.h: Failed to send STUN request, error was " CHIAKI_SOCKET_ERROR_FMT, CHIAKI_SOCKET_ERROR_VALUE);
CHIAKI_SOCKET_CLOSE(sock);
return false;
}

#ifdef _WIN32
char timeout[20] = {0};
sprintf(timeout, "%d", STUN_REPLY_TIMEOUT_SEC * 1000);
if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, timeout, sizeof(timeout)) < 0) {
int timeout = STUN_REPLY_TIMEOUT_SEC * 1000;
#else
struct timeval timeout;
timeout.tv_sec = STUN_REPLY_TIMEOUT_SEC;
timeout.tv_usec = 0;
if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)) < 0) {
#endif
if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (const CHIAKI_SOCKET_BUF_TYPE)&timeout, sizeof(timeout)) < 0) {
CHIAKI_LOGE(log, "remote/stun.h: Failed to set socket timeout, error was " CHIAKI_SOCKET_ERROR_FMT, CHIAKI_SOCKET_ERROR_VALUE);
CHIAKI_SOCKET_CLOSE(sock);
return false;
}

uint8_t binding_resp[256];
ssize_t received = recvfrom(sock, SOCKET_BUF_TYPE binding_resp, sizeof(binding_resp), 0, NULL, NULL);
ssize_t received = recvfrom(sock, (CHIAKI_SOCKET_BUF_TYPE)binding_resp, sizeof(binding_resp), 0, NULL, NULL);
CHIAKI_SOCKET_CLOSE(sock);
if (received < 0) {
CHIAKI_LOGE(log, "remote/stun.h: Failed to receive STUN response, error was " CHIAKI_SOCKET_ERROR_FMT, CHIAKI_SOCKET_ERROR_VALUE);
Expand Down
6 changes: 4 additions & 2 deletions lib/src/session.c
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,8 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_session_init(ChiakiSession *session, Chiaki
error_state_cond:
chiaki_cond_fini(&session->state_cond);
error:
chiaki_holepunch_session_fini(session->holepunch_session);
if(session->holepunch_session)
chiaki_holepunch_session_fini(session->holepunch_session);
return err;
}

Expand All @@ -269,7 +270,8 @@ CHIAKI_EXPORT void chiaki_session_fini(ChiakiSession *session)
chiaki_ctrl_fini(&session->ctrl);
if(session->rudp)
chiaki_rudp_fini(session->rudp);
chiaki_holepunch_session_fini(session->holepunch_session);
if(session->holepunch_session)
chiaki_holepunch_session_fini(session->holepunch_session);
chiaki_stop_pipe_fini(&session->stop_pipe);
chiaki_cond_fini(&session->state_cond);
chiaki_mutex_fini(&session->state_mutex);
Expand Down
4 changes: 4 additions & 0 deletions lib/src/stoppipe.c
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,11 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_stop_pipe_select_single(ChiakiStopPipe *sto
do
{
r = select(nfds, &rfds, write ? &wfds : NULL, NULL, timeout);
#ifdef _WIN32
} while(r < 0 && WSAGetLastError() == WSAEINTR)
#else
} while(r < 0 && errno == EINTR);
#endif

if(r < 0)
return CHIAKI_ERR_UNKNOWN;
Expand Down
Loading

0 comments on commit 45f22a9

Please sign in to comment.