From 35c320d53d6f4a05345b00aaa34bd53e2303a76b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20M=C3=A4rkl?= Date: Sat, 17 Nov 2018 11:18:23 +0100 Subject: [PATCH] Prepare Ctrl --- lib/CMakeLists.txt | 6 ++- lib/include/chiaki/ctrl.h | 41 +++++++++++++++ lib/include/chiaki/session.h | 9 +++- lib/src/ctrl.c | 97 ++++++++++++++++++++++++++++++++++++ lib/src/session.c | 9 ++-- 5 files changed, 153 insertions(+), 9 deletions(-) create mode 100644 lib/include/chiaki/ctrl.h create mode 100644 lib/src/ctrl.c diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index a1d611f..8037f67 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -5,7 +5,8 @@ set(HEADER_FILES include/chiaki/thread.h include/chiaki/base64.h include/chiaki/http.h - include/chiaki/log.h) + include/chiaki/log.h + include/chiaki/ctrl.h) set(SOURCE_FILES src/common.c @@ -13,7 +14,8 @@ set(SOURCE_FILES src/thread.c src/base64.c src/http.c - src/log.c) + src/log.c + src/ctrl.c) add_library(chiaki-lib ${HEADER_FILES} ${SOURCE_FILES}) set_target_properties(chiaki-lib PROPERTIES OUTPUT_NAME chiaki) diff --git a/lib/include/chiaki/ctrl.h b/lib/include/chiaki/ctrl.h new file mode 100644 index 0000000..feacfb7 --- /dev/null +++ b/lib/include/chiaki/ctrl.h @@ -0,0 +1,41 @@ +/* + * This file is part of Chiaki. + * + * Chiaki is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Chiaki is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Chiaki. If not, see . + */ + +#ifndef CHIAKI_CTRL_H +#define CHIAKI_CTRL_H + +#include "common.h" +#include "thread.h" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct chiaki_ctrl_t +{ + struct chiaki_session_t *session; + ChiakiThread thread; +} ChiakiCtrl; + +CHIAKI_EXPORT ChiakiErrorCode chiaki_ctrl_start(ChiakiCtrl *ctrl, struct chiaki_session_t *session); +CHIAKI_EXPORT ChiakiErrorCode chiaki_ctrl_join(ChiakiCtrl *ctrl); + +#ifdef __cplusplus +} +#endif + +#endif // CHIAKI_CTRL_H diff --git a/lib/include/chiaki/session.h b/lib/include/chiaki/session.h index 5f69181..804cc41 100644 --- a/lib/include/chiaki/session.h +++ b/lib/include/chiaki/session.h @@ -21,6 +21,7 @@ #include "common.h" #include "thread.h" #include "log.h" +#include "ctrl.h" #include #include @@ -41,10 +42,12 @@ typedef struct chiaki_connect_info_t typedef enum { CHIAKI_QUIT_REASON_NONE, - CHIAKI_QUIT_REASON_SESSION_REQUEST_CONNECTION_REFUSED, CHIAKI_QUIT_REASON_SESSION_REQUEST_UNKNOWN, + CHIAKI_QUIT_REASON_SESSION_REQUEST_CONNECTION_REFUSED, CHIAKI_QUIT_REASON_SESSION_REQUEST_RP_IN_USE, - CHIAKI_QUIT_REASON_SESSION_REQUEST_RP_CRASH + CHIAKI_QUIT_REASON_SESSION_REQUEST_RP_CRASH, + CHIAKI_QUIT_REASON_CTRL_UNKNOWN, + CHIAKI_QUIT_REASON_CTRL_CONNECTION_REFUSED } ChiakiQuitReason; typedef struct chiaki_quit_event_t @@ -75,6 +78,7 @@ typedef struct chiaki_session_t { struct addrinfo *host_addrinfos; struct addrinfo *host_addrinfo_selected; + char hostname[128]; char *regist_key; char *ostype; char auth[CHIAKI_KEY_BYTES]; @@ -89,6 +93,7 @@ typedef struct chiaki_session_t void *event_cb_user; ChiakiThread session_thread; + ChiakiCtrl ctrl; ChiakiLog log; } ChiakiSession; diff --git a/lib/src/ctrl.c b/lib/src/ctrl.c new file mode 100644 index 0000000..443b978 --- /dev/null +++ b/lib/src/ctrl.c @@ -0,0 +1,97 @@ +/* + * This file is part of Chiaki. + * + * Chiaki is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Chiaki is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Chiaki. If not, see . + */ + +#include +#include + +#include +#include +#include + + +#define SESSION_CTRL_PORT 9295 + + +static void *ctrl_thread_func(void *user); + +CHIAKI_EXPORT ChiakiErrorCode chiaki_ctrl_start(ChiakiCtrl *ctrl, ChiakiSession *session) +{ + ctrl->session = session; + return chiaki_thread_create(&ctrl->thread, ctrl_thread_func, ctrl); +} + +CHIAKI_EXPORT ChiakiErrorCode chiaki_ctrl_join(ChiakiCtrl *ctrl) +{ + return chiaki_thread_join(&ctrl->thread, NULL); +} + + +static ChiakiErrorCode ctrl_thread_connect(ChiakiCtrl *ctrl); + +static void *ctrl_thread_func(void *user) +{ + ChiakiCtrl *ctrl = user; + + + + return NULL; +} + +static ChiakiErrorCode ctrl_thread_connect(ChiakiCtrl *ctrl) +{ + ChiakiSession *session = ctrl->session; + struct addrinfo *addr = session->connect_info.host_addrinfo_selected; + struct sockaddr *sa = malloc(addr->ai_addrlen); + if(!sa) + return CHIAKI_ERR_MEMORY; + memcpy(sa, addr->ai_addr, addr->ai_addrlen); + + if(sa->sa_family == AF_INET) + ((struct sockaddr_in *)sa)->sin_port = htons(SESSION_CTRL_PORT); + else if(sa->sa_family == AF_INET6) + ((struct sockaddr_in6 *)sa)->sin6_port = htons(SESSION_CTRL_PORT); + else + return CHIAKI_ERR_INVALID_DATA; + + int sock = socket(sa->sa_family, SOCK_STREAM, IPPROTO_TCP); + if(sock < 0) + { + CHIAKI_LOGE(&session->log, "Session ctrl socket creation failed.\n"); + if(session->quit_reason == CHIAKI_QUIT_REASON_NONE) + session->quit_reason = CHIAKI_QUIT_REASON_CTRL_UNKNOWN; + return CHIAKI_ERR_NETWORK; + } + + CHIAKI_LOGI(&session->log, "Connected to %s:%d\n", session->connect_info.hostname, SESSION_CTRL_PORT); + + static const char request_fmt[] = + "GET /sce/rp/session/ctrl HTTP/1.1\r\n" + "Host: %s:%d\r\n" + "User-Agent: remoteplay Windows\r\n" + "Connection: keep-alive\r\n" + "Content-Length: 0\r\n" + "RP-Auth: %s\r\n" + "RP-Version: 8.0\r\n" + "RP-Did: %s\r\n" + "RP-ControllerType: 3\r\n" + "RP-ClientType: 11\r\n" + "RP-OSType: %s\r\n" + "RP-ConPath: 1\r\n\r\n"; + + close(sock); + return CHIAKI_ERR_SUCCESS; +} \ No newline at end of file diff --git a/lib/src/session.c b/lib/src/session.c index 7845221..ddc62be 100644 --- a/lib/src/session.c +++ b/lib/src/session.c @@ -162,7 +162,6 @@ static void parse_session_response(SessionResponse *response, ChiakiHttpResponse static bool session_thread_request_session(ChiakiSession *session) { int session_sock = -1; - char host_buf[128]; for(struct addrinfo *ai=session->connect_info.host_addrinfos; ai; ai=ai->ai_next) { if(ai->ai_protocol != IPPROTO_TCP) @@ -183,14 +182,14 @@ static bool session_thread_request_session(ChiakiSession *session) continue; } - int r = getnameinfo(sa, ai->ai_addrlen, host_buf, sizeof(host_buf), NULL, 0, 0); + int r = getnameinfo(sa, ai->ai_addrlen, session->connect_info.hostname, sizeof(session->connect_info.hostname), NULL, 0, 0); if(r != 0) { free(sa); continue; } - CHIAKI_LOGI(&session->log, "Trying to request session from %s:%d\n", host_buf, SESSION_PORT); + CHIAKI_LOGI(&session->log, "Trying to request session from %s:%d\n", session->connect_info.hostname, SESSION_PORT); session_sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); if(session_sock < 0) @@ -224,7 +223,7 @@ static bool session_thread_request_session(ChiakiSession *session) return false; } - CHIAKI_LOGI(&session->log, "Connected to %s:%d\n", host_buf, SESSION_PORT); + CHIAKI_LOGI(&session->log, "Connected to %s:%d\n", session->connect_info.hostname, SESSION_PORT); static const char session_request_fmt[] = "GET /sce/rp/session HTTP/1.1\r\n" @@ -238,7 +237,7 @@ static bool session_thread_request_session(ChiakiSession *session) char buf[512]; int request_len = snprintf(buf, sizeof(buf), session_request_fmt, - host_buf, SESSION_PORT, session->connect_info.regist_key); + session->connect_info.hostname, SESSION_PORT, session->connect_info.regist_key); if(request_len < 0 || request_len >= sizeof(buf)) { close(session_sock);