diff --git a/.github/workflows/build-msys2.yml b/.github/workflows/build-msys2.yml index 643291c51..764861033 100644 --- a/.github/workflows/build-msys2.yml +++ b/.github/workflows/build-msys2.yml @@ -33,9 +33,11 @@ jobs: ffmpeg:p fftw:p hidapi:p + json-c:p lcms2:p libdovi:p meson:p + miniupnpc:p nasm:p ninja:p openssl:p diff --git a/.github/workflows/build-windows.yml b/.github/workflows/build-windows.yml index 675114404..917cd7f1b 100644 --- a/.github/workflows/build-windows.yml +++ b/.github/workflows/build-windows.yml @@ -144,6 +144,7 @@ jobs: -DCHIAKI_ENABLE_STEAM_SHORTCUT=OFF ` -DCHIAKI_GUI_ENABLE_SDL_GAMECONTROLLER=ON ` -DCHIAKI_ENABLE_STEAMDECK_NATIVE=OFF ` + -DCHIAKI_USE_SYSTEM_CURL ` -DPYTHON_EXECUTABLE="${{ env.pythonLocation }}\python.exe" ` -DCMAKE_PREFIX_PATH="${{ github.workspace }}\${{ env.dep_folder }};${{ env.VULKAN_SDK }}" diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 1eaf23e70..f5c79efdd 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -122,7 +122,7 @@ add_dependencies(chiaki-lib chiaki-pb) set_target_properties(chiaki-lib PROPERTIES OUTPUT_NAME chiaki) if(WIN32) - target_link_libraries(chiaki-lib wsock32 ws2_32 bcrypt) + target_link_libraries(chiaki-lib wsock32 ws2_32 bcrypt iphlpapi) endif() target_include_directories(chiaki-lib PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include") diff --git a/lib/include/chiaki/sock.h b/lib/include/chiaki/sock.h index 9ddae9c09..d20f3ecbb 100644 --- a/lib/include/chiaki/sock.h +++ b/lib/include/chiaki/sock.h @@ -20,6 +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*) #else #include #include @@ -30,6 +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 *) #endif CHIAKI_EXPORT ChiakiErrorCode chiaki_socket_set_nonblock(chiaki_socket_t sock, bool nonblock); diff --git a/lib/src/remote/holepunch.c b/lib/src/remote/holepunch.c index 087e8391d..1281ebc5e 100644 --- a/lib/src/remote/holepunch.c +++ b/lib/src/remote/holepunch.c @@ -19,16 +19,21 @@ #include #include -#include #include +#include + +#ifdef _WIN32 +#include +#include +#else +#include +#include +#include #include #include -#include +#include #include #include -#if defined __linux__ -#include -#elif defined _WIN32 #endif #include @@ -315,7 +320,7 @@ static bool get_client_addr_remote_upnp(ChiakiLog *log, UPNPGatewayInfo *gw_info static bool upnp_add_udp_port_mapping(ChiakiLog *log, UPNPGatewayInfo *gw_info, uint16_t port_internal, uint16_t port_external); static bool upnp_delete_udp_port_mapping(ChiakiLog *log, UPNPGatewayInfo *gw_info, uint16_t port_external); static bool get_client_addr_remote_stun(ChiakiLog *log, char *out); -static bool get_mac_addr(ChiakiLog *log, uint8_t *mac_addr); +// static bool get_mac_addr(ChiakiLog *log, uint8_t *mac_addr); static void log_session_state(Session *session); static ChiakiErrorCode decode_customdata1(const char *customdata1, uint8_t *out, size_t out_len); static ChiakiErrorCode check_candidates( @@ -384,6 +389,7 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_holepunch_list_devices( headers = curl_slist_append(headers, oauth_header); curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1L); + curl_easy_setopt(curl, CURLOPT_TIMEOUT, 5L); curl_easy_setopt(curl, CURLOPT_URL, url); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_write_cb); @@ -1316,13 +1322,13 @@ static void* websocket_thread_func(void *user) { clock_gettime(CLOCK_MONOTONIC, &ts); now = ts.tv_sec * SECOND_NS + ts.tv_nsec; - if (expecting_pong && now - last_ping_sent > 5L * SECOND_NS) + if (expecting_pong && now - last_ping_sent > 5LL * SECOND_NS) { CHIAKI_LOGE(session->log, "websocket_thread_func: Did not receive PONG in time."); goto cleanup_json; } - if (now - last_ping_sent > 5L * SECOND_NS) + if (now - last_ping_sent > 5LL * SECOND_NS) { res = curl_ws_send(curl, buf, 0, &wlen, 0, CURLWS_PING); if (res != CURLE_OK) @@ -1508,17 +1514,17 @@ static ChiakiErrorCode send_offer(Session *session, int req_id, Candidate *local socklen_t client_addr_len = sizeof(client_addr); const int enable = 1; #if defined(SO_REUSEPORT) - if (setsockopt(session->client_sock, SOL_SOCKET, SO_REUSEPORT, &enable, sizeof(int)) < 0) - CHIAKI_LOGE(session->log, "setsockopt(SO_REUSEPORT) failed with error %s", strerror(errno)); + if (setsockopt(session->client_sock, SOL_SOCKET, SO_REUSEPORT, (const void *)&enable, sizeof(int)) < 0) + CHIAKI_LOGE(session->log, "setsockopt(SO_REUSEPORT) failed with error " CHIAKI_SOCKET_ERROR_FMT, CHIAKI_SOCKET_ERROR_VALUE); #else - if (setsockopt(session->client_sock, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)) < 0) - CHIAKI_LOGE(session->log, "setsockopt(SO_REUSEADDR) failed with error %s", strerror(errno)); + if (setsockopt(session->client_sock, SOL_SOCKET, SO_REUSEADDR, (const void *)&enable, sizeof(int)) < 0) + CHIAKI_LOGE(session->log, "setsockopt(SO_REUSEADDR) failed with error" CHIAKI_SOCKET_ERROR_FMT, CHIAKI_SOCKET_ERROR_VALUE); #endif bind(session->client_sock, (struct sockaddr*)&client_addr, client_addr_len); if (getsockname(session->client_sock, (struct sockaddr*)&client_addr, &client_addr_len) < 0) { CHIAKI_LOGE(session->log, "send_offer: Getting socket port failed"); - close(session->client_sock); + CHIAKI_SOCKET_CLOSE(session->client_sock); return CHIAKI_ERR_UNKNOWN; } @@ -1574,7 +1580,7 @@ static ChiakiErrorCode send_offer(Session *session, int req_id, Candidate *local if (!have_addr) { CHIAKI_LOGE(session->log, "send_offer: Could not get remote address"); err = CHIAKI_ERR_UNKNOWN; - close(session->client_sock); + CHIAKI_SOCKET_CLOSE(session->client_sock); goto cleanup; } memcpy(candidate_remote->addr_mapped, "0.0.0.0", 8); @@ -1585,7 +1591,7 @@ static ChiakiErrorCode send_offer(Session *session, int req_id, Candidate *local if (err != CHIAKI_ERR_SUCCESS) { CHIAKI_LOGE(session->log, "send_offer: Sending session message failed"); - close(session->client_sock); + CHIAKI_SOCKET_CLOSE(session->client_sock); } memcpy(&local_candidates[0], candidate_local, sizeof(Candidate)); memcpy(&local_candidates[1], candidate_remote, sizeof(Candidate)); @@ -1941,6 +1947,61 @@ static ChiakiErrorCode deleteSession(Session *session) static ChiakiErrorCode get_client_addr_local(Session *session, Candidate *local_console_candidate, char *out, size_t out_len) { ChiakiErrorCode err = CHIAKI_ERR_SUCCESS; + bool status = false; +#ifdef _WIN32 + PIP_ADAPTER_INFO pAdapterInfo; + PIP_ADAPTER_INFO pAdapter = NULL; +#define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x)) +#define FREE(x) HeapFree(GetProcessHeap(), 0, (x)) + DWORD dwRetVal = 0; + ULONG ulOutBufLen = sizeof (IP_ADAPTER_INFO); + pAdapterInfo = (IP_ADAPTER_INFO *) MALLOC(sizeof (IP_ADAPTER_INFO)); + if (pAdapterInfo == NULL) { + CHIAKI_LOGE(session->log, "Error allocating memory needed to call GetAdaptersinfo\n"); + return CHIAKI_ERR_MEMORY; + } + if (GetAdaptersInfo(pAdapterInfo, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW) { + FREE(pAdapterInfo); + pAdapterInfo = (IP_ADAPTER_INFO *) MALLOC(ulOutBufLen); + if (pAdapterInfo == NULL) { + CHIAKI_LOGE(session->log, "Error allocating memory needed to call GetAdaptersinfo\n"); + return CHIAKI_ERR_MEMORY; + } + } + + if ((dwRetVal = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen)) == NO_ERROR) { + pAdapter = pAdapterInfo; + while (pAdapter) { + if(pAdapter->Type == MIB_IF_TYPE_LOOPBACK) + { + pAdapter = pAdapter->Next; + continue; + } + if(strcmp(pAdapter->IpAddressList.IpAddress.String, "") == 0) + { + pAdapter = pAdapter->Next; + continue; + } + memcpy(local_console_candidate->addr, pAdapter->IpAddressList.IpAddress.String, strlen(pAdapter->IpAddressList.IpAddress.String)); + status = true; + break; + } + } else { + CHIAKI_LOGE(session->log, "GetAdaptersInfo failed with error: %d\n", dwRetVal); + + } + if (pAdapterInfo) + FREE(pAdapterInfo); + + if(!status) + { + CHIAKI_LOGE(session->log, "Couldn't find a valid external address!"); + return CHIAKI_ERR_NETWORK; + } + return err; +#undef MALLOC +#undef FREE +#else struct ifaddrs *local_addrs, *current_addr; void *in_addr; struct sockaddr_in *res4 = NULL; @@ -1951,8 +2012,6 @@ static ChiakiErrorCode get_client_addr_local(Session *session, Candidate *local_ hints.ai_family = AF_INET; hints.ai_socktype = SOCK_DGRAM; - bool status; - if(getifaddrs(&local_addrs) != 0) { CHIAKI_LOGE(session->log, "Couldn't get local address"); @@ -1983,7 +2042,7 @@ static ChiakiErrorCode get_client_addr_local(Session *session, Candidate *local_ } if (!inet_ntop(current_addr->ifa_addr->sa_family, in_addr, local_console_candidate->addr, sizeof(local_console_candidate->addr))) { - CHIAKI_LOGE(session->log, "%s: inet_ntop failed with error: %s\n", current_addr->ifa_name, strerror(errno)); + CHIAKI_LOGE(session->log, "%s: inet_ntop failed with error: " CHIAKI_SOCKET_ERROR_FMT "\n", current_addr->ifa_name, CHIAKI_SOCKET_ERROR_VALUE); continue; } status = true; @@ -1996,6 +2055,7 @@ static ChiakiErrorCode get_client_addr_local(Session *session, Candidate *local_ } freeifaddrs(local_addrs); return err; +#endif } /** @@ -2131,7 +2191,7 @@ static bool get_client_addr_remote_stun(ChiakiLog *log, char *out) // conf.ifc_buf = buf; // if(ioctl(sock, SIOCGIFCONF, &conf) < 0) // { -// CHIAKI_LOGE(log, "Error getting local IFConfig: %s", strerror(errno)); +// CHIAKI_LOGE(log, "Error getting local IFConfig: " CHIAKI_SOCKET_ERROR_FMT, CHIAKI_SOCKET_ERROR_VALUE); // return false; // } @@ -2150,7 +2210,7 @@ static bool get_client_addr_remote_stun(ChiakiLog *log, char *out) // } // } // else -// CHIAKI_LOGE(log, "Error getting IFFlags for local device: %s", strerror(errno)); +// CHIAKI_LOGE(log, "Error getting IFFlags for local device: " CHIAKI_SOCKET_ERROR_FMT, CHIAKI_SOCKET_ERROR_VALUE); // } // if(!success) // return false; @@ -2213,7 +2273,7 @@ static ChiakiErrorCode check_candidates( if (getaddrinfo(local_candidate->addr, service_local, &hints, &addr_local) != 0) { - CHIAKI_LOGE(session->log, "check_candidate: getaddrinfo failed for %s:%d with error %s", local_candidate->addr, local_candidate->port, strerror(errno)); + CHIAKI_LOGE(session->log, "check_candidate: getaddrinfo failed for %s:%d with error " CHIAKI_SOCKET_ERROR_FMT, local_candidate->addr, local_candidate->port, CHIAKI_SOCKET_ERROR_VALUE); return CHIAKI_ERR_UNKNOWN; } @@ -2238,15 +2298,15 @@ static ChiakiErrorCode check_candidates( const int enable = 1; #if defined(SO_REUSEPORT) - if (setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, &enable, sizeof(int)) < 0) - CHIAKI_LOGE(session->log, "setsockopt(SO_REUSEPORT) failed with error %s", strerror(errno)); + if (setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, (const void *)&enable, sizeof(int)) < 0) + CHIAKI_LOGE(session->log, "setsockopt(SO_REUSEPORT) failed with error " CHIAKI_SOCKET_ERROR_FMT, CHIAKI_SOCKET_ERROR_VALUE); #else - if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)) < 0) - CHIAKI_LOGE(session->log, "setsockopt(SO_REUSEADDR) failed with error %s", strerror(errno)); + if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (const void *)&enable, sizeof(int)) < 0) + CHIAKI_LOGE(session->log, "setsockopt(SO_REUSEADDR) failed with error " CHIAKI_SOCKET_ERROR_FMT, CHIAKI_SOCKET_ERROR_VALUE); #endif if (bind(sock, addr_local->ai_addr, addr_local->ai_addrlen) < 0) { - CHIAKI_LOGE(session->log, "check_candidate: Binding socket failed for local address: %s and port %d: with error %s", local_candidate->addr, local_candidate->port, strerror(errno)); + CHIAKI_LOGE(session->log, "check_candidate: Binding socket failed for local address: %s and port %d: with error " CHIAKI_SOCKET_ERROR_FMT, local_candidate->addr, local_candidate->port, CHIAKI_SOCKET_ERROR_VALUE); err = CHIAKI_ERR_NETWORK; freeaddrinfo(addr_remote); continue; @@ -2254,7 +2314,7 @@ static ChiakiErrorCode check_candidates( if (getaddrinfo(candidate->addr, service_remote, &hints, &addr_remote) != 0) { - CHIAKI_LOGE(session->log, "check_candidate: getaddrinfo failed for %s:%d with error %s", candidate->addr, candidate->port, strerror(errno)); + CHIAKI_LOGE(session->log, "check_candidate: getaddrinfo failed for %s:%d with error " CHIAKI_SOCKET_ERROR_FMT, candidate->addr, candidate->port, CHIAKI_SOCKET_ERROR_VALUE); err = CHIAKI_ERR_UNKNOWN; if(i < num_candidates - 1) continue; @@ -2262,15 +2322,15 @@ static ChiakiErrorCode check_candidates( if (connect(sock, addr_remote->ai_addr, addr_remote->ai_addrlen) < 0) { - CHIAKI_LOGE(session->log, "check_candidate: Connecting socket failed for %s:%d with error %s", candidate->addr, candidate->port, strerror(errno)); + CHIAKI_LOGE(session->log, "check_candidate: Connecting socket failed for %s:%d with error " CHIAKI_SOCKET_ERROR_FMT, candidate->addr, candidate->port, CHIAKI_SOCKET_ERROR_VALUE); err = CHIAKI_ERR_NETWORK; freeaddrinfo(addr_remote); continue; } - if (send(sock, request_buf[0], sizeof(request_buf[0]), 0) < 0) + if (send(sock, 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: %s", candidate->addr, candidate->port, strerror(errno)); + 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; freeaddrinfo(addr_remote); continue; @@ -2341,10 +2401,10 @@ 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, response_buf, sizeof(response_buf), 0); + ssize_t response_len = recv(candidate_sock, 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: %s", candidate->addr, candidate->port, strerror(errno)); + 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); continue; } if (response_len != sizeof(response_buf)) @@ -2392,9 +2452,9 @@ static ChiakiErrorCode check_candidates( } else { - if (send(candidate_sock, request_buf[responses], sizeof(request_buf[responses]), 0) < 0) + if (send(candidate_sock, 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: %s", candidate->addr, candidate->port, strerror(errno)); + 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; freeaddrinfo(addr_remote); continue; @@ -2406,10 +2466,10 @@ static ChiakiErrorCode check_candidates( for (int i=0; i < num_candidates; i++) { if (sockets[i] != *out && sockets[i] >= 0) - close(sockets[i]); + CHIAKI_SOCKET_CLOSE(sockets[i]); } if (session->client_sock >= 0) - close(session->client_sock); + CHIAKI_SOCKET_CLOSE(session->client_sock); err = receive_request_send_response_ps(session, out, selected_candidate, WAIT_RESPONSE_TIMEOUT_SEC); if(err != CHIAKI_ERR_SUCCESS) @@ -2434,10 +2494,10 @@ static ChiakiErrorCode check_candidates( for (int i=0; i < num_candidates; i++) { if (sockets[i] != *out && sockets[i] >= 0) - close(sockets[i]); + CHIAKI_SOCKET_CLOSE(sockets[i]); } if (session->client_sock >= 0) - close(session->client_sock); + CHIAKI_SOCKET_CLOSE(session->client_sock); return err; } @@ -2478,9 +2538,9 @@ 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, confirm_buf, sizeof(confirm_buf), 0) < 0) + if (send(*sock, 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, strerror(errno)); + 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; } CHIAKI_LOGI(session->log, "Sent response to %s:%d", candidate->addr, candidate->port); @@ -2511,7 +2571,7 @@ static ChiakiErrorCode receive_request_send_response_ps(Session *session, chiaki if(err != CHIAKI_ERR_SUCCESS) return err; - len = recv(*sock, req, sizeof(req), 0); + len = recv(*sock, 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); @@ -2519,7 +2579,7 @@ static ChiakiErrorCode receive_request_send_response_ps(Session *session, chiaki } if (len < 0) { - CHIAKI_LOGE(session->log, "check_candidate: Receiving response from %s:%d failed with error: %s", candidate->addr, candidate->port, strerror(errno)); + 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); continue; } if (len != sizeof(req)) diff --git a/lib/src/remote/rudp.c b/lib/src/remote/rudp.c index 9ffb7a646..ceb419d3e 100644 --- a/lib/src/remote/rudp.c +++ b/lib/src/remote/rudp.c @@ -5,7 +5,6 @@ #include #include -#include #include #include @@ -337,7 +336,7 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_rudp_send_raw(RudpInstance *rudp, uint8_t * { return CHIAKI_ERR_DISCONNECTED; } - int sent = send(rudp->sock, buf, buf_size, 0); + int sent = send(rudp->sock, SOCKET_BUF_TYPE buf, buf_size, 0); if(sent < 0) { #ifdef _WIN32 @@ -362,7 +361,7 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_rudp_recv(RudpInstance *rudp, size_t buf_si return err; } - int received_sz = recv(rudp->sock, buf, buf_size, 0); + int received_sz = recv(rudp->sock, SOCKET_BUF_TYPE buf, buf_size, 0); if(received_sz <= 8) { if(received_sz < 0) diff --git a/lib/src/remote/rudpsendbuffer.c b/lib/src/remote/rudpsendbuffer.c index 4bb406727..1be08ffc7 100644 --- a/lib/src/remote/rudpsendbuffer.c +++ b/lib/src/remote/rudpsendbuffer.c @@ -8,7 +8,6 @@ #include #include #include -#include #define RUDP_DATA_RESEND_TIMEOUT_MS 200 #define RUDP_DATA_RESEND_WAKEUP_TIMEOUT_MS (RUDP_DATA_RESEND_TIMEOUT_MS/2) diff --git a/lib/src/remote/stun.h b/lib/src/remote/stun.h index d0cd0b7d3..59e0db8cd 100644 --- a/lib/src/remote/stun.h +++ b/lib/src/remote/stun.h @@ -2,9 +2,15 @@ #include #include #include + +#ifdef _WIN32 +#include +#else +#include #include #include #include +#endif #include #include @@ -88,7 +94,7 @@ static bool stun_get_external_address_from_server(ChiakiLog *log, StunServer *se { chiaki_socket_t sock = socket(AF_INET, SOCK_DGRAM, 0); if (sock < 0) { - CHIAKI_LOGE(log, "remote/stun.h: Failed to create socket, error was '%s'", strerror(errno)); + CHIAKI_LOGE(log, "remote/stun.h: Failed to create socket, error was " CHIAKI_SOCKET_ERROR_FMT, CHIAKI_SOCKET_ERROR_VALUE); return false; } @@ -99,8 +105,8 @@ static bool stun_get_external_address_from_server(ChiakiLog *log, StunServer *se local_addr.sin_addr.s_addr = htonl(INADDR_ANY); if (bind(sock, (struct sockaddr *)&local_addr, sizeof(local_addr)) < 0) { - CHIAKI_LOGE(log, "remote/stun.h: Failed to bind socket to local address, error was '%s'", strerror(errno)); - close(sock); + CHIAKI_LOGE(log, "remote/stun.h: Failed to bind socket to local address, error was " CHIAKI_SOCKET_ERROR_FMT, CHIAKI_SOCKET_ERROR_VALUE); + CHIAKI_SOCKET_CLOSE(sock); return false; } @@ -111,8 +117,8 @@ static bool stun_get_external_address_from_server(ChiakiLog *log, StunServer *se hints.ai_family = AF_INET; hints.ai_socktype = SOCK_DGRAM; if (getaddrinfo(server->host, NULL, &hints, &resolved) != 0) { - CHIAKI_LOGE(log, "remote/stun.h: Failed to resolve STUN server '%s', error was '%s'", server->host, strerror(errno)); - close(sock); + CHIAKI_LOGE(log, "remote/stun.h: Failed to resolve STUN server '%s', error was " CHIAKI_SOCKET_ERROR_FMT, server->host, CHIAKI_SOCKET_ERROR_VALUE); + CHIAKI_SOCKET_CLOSE(sock); return false; } @@ -128,27 +134,33 @@ static bool stun_get_external_address_from_server(ChiakiLog *log, StunServer *se //uint8_t* transaction_id = &binding_req[8]; - ssize_t sent = sendto(sock, binding_req, sizeof(binding_req), 0, (struct sockaddr*)server_addr, sizeof(struct sockaddr_in)); + ssize_t sent = sendto(sock, 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 '%s'", strerror(errno)); - close(sock); + 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) { +#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) { - CHIAKI_LOGE(log, "remote/stun.h: Failed to set socket timeout, error was '%s'", strerror(errno)); - close(sock); +#endif + 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, binding_resp, sizeof(binding_resp), 0, NULL, NULL); - close(sock); + ssize_t received = recvfrom(sock, 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 '%s'", strerror(errno)); + CHIAKI_LOGE(log, "remote/stun.h: Failed to receive STUN response, error was " CHIAKI_SOCKET_ERROR_FMT, CHIAKI_SOCKET_ERROR_VALUE); return false; } diff --git a/lib/src/utils.h b/lib/src/utils.h index 9f96d7321..282858a5b 100644 --- a/lib/src/utils.h +++ b/lib/src/utils.h @@ -90,7 +90,7 @@ static inline int sendto_broadcast(ChiakiLog *log, chiaki_socket_t s, const void return r; } #endif - return sendto(s, msg, len, flags, to, tolen); + return sendto(s, SOCKET_BUF_TYPE msg, len, flags, to, tolen); } static inline void xor_bytes(uint8_t *dst, uint8_t *src, size_t sz) diff --git a/vcpkg.json b/vcpkg.json index 093b8d1af..568b4dc10 100644 --- a/vcpkg.json +++ b/vcpkg.json @@ -1,12 +1,21 @@ { "name": "chiaki4deck", - "version": "1.6.3", + "version": "1.7.0", "dependencies": [ "pkgconf", + { + "name": "curl", + "features": [ + "ssl", + "websockets" + ] + }, "sdl2", "protobuf", "openssl", "hidapi", + "json-c", + "miniupnpc", "opus", "fftw3", "lcms",