diff --git a/CMakeLists.txt b/CMakeLists.txt index a59f5b6..9dcf447 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,4 +3,5 @@ cmake_minimum_required(VERSION 3.2) project(chiaki) +add_subdirectory(lib) add_subdirectory(test) \ No newline at end of file diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt new file mode 100644 index 0000000..805508a --- /dev/null +++ b/lib/CMakeLists.txt @@ -0,0 +1,19 @@ + +set(HEADER_FILES + include/chiaki/session.h + include/chiaki/common.h + include/chiaki/thread.h + include/chiaki/base64.h) + +set(SOURCE_FILES + src/session.c + src/thread.c + src/base64.c) + +add_library(chiaki-lib ${HEADER_FILES} ${SOURCE_FILES}) +set_target_properties(chiaki-lib PROPERTIES OUTPUT_NAME chiaki) + +target_include_directories(chiaki-lib PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include") + +find_package(Threads) +target_link_libraries(chiaki-lib Threads::Threads) diff --git a/lib/include/chiaki/base64.h b/lib/include/chiaki/base64.h new file mode 100644 index 0000000..8b0933e --- /dev/null +++ b/lib/include/chiaki/base64.h @@ -0,0 +1,37 @@ +/* + * 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_BASE64_H +#define CHIAKI_BASE64_H + +#include "common.h" + +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +CHIAKI_EXPORT bool chiaki_base64_decode(const char *in, size_t in_size, uint8_t *out, size_t *out_size); + +#ifdef __cplusplus +} +#endif + +#endif // CHIAKI_BASE64_H diff --git a/lib/include/chiaki/common.h b/lib/include/chiaki/common.h new file mode 100644 index 0000000..78e9a0c --- /dev/null +++ b/lib/include/chiaki/common.h @@ -0,0 +1,31 @@ +/* + * 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_COMMON_H +#define CHIAKI_COMMON_H + +#ifdef __cplusplus +extern "C" { +#endif + +#define CHIAKI_EXPORT + +#ifdef __cplusplus +} +#endif + +#endif // CHIAKI_COMMON_H diff --git a/lib/include/chiaki/session.h b/lib/include/chiaki/session.h new file mode 100644 index 0000000..f80edc3 --- /dev/null +++ b/lib/include/chiaki/session.h @@ -0,0 +1,62 @@ +/* + * 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_SESSION_H +#define CHIAKI_SESSION_H + +#include "common.h" +#include "thread.h" + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct chiaki_connect_info_t +{ + const char *host; + const char *regist_key; + const char *ostype; + char auth[0x10]; + uint8_t morning[0x10]; +} ChiakiConnectInfo; + +typedef struct chiaki_session_t +{ + struct + { + char *host; + char *regist_key; + char *ostype; + char auth[0x10]; + uint8_t morning[0x10]; + } connect_info; + + ChiakiThread session_thread; +} ChiakiSession; + +CHIAKI_EXPORT void chiaki_session_init(ChiakiSession *session, ChiakiConnectInfo *connect_info); +CHIAKI_EXPORT void chiaki_session_fini(ChiakiSession *session); +CHIAKI_EXPORT bool chiaki_session_start(ChiakiSession *session); +CHIAKI_EXPORT void chiaki_session_join(ChiakiSession *session); + +#ifdef __cplusplus +} +#endif + +#endif // CHIAKI_SESSION_H diff --git a/lib/include/chiaki/thread.h b/lib/include/chiaki/thread.h new file mode 100644 index 0000000..910973e --- /dev/null +++ b/lib/include/chiaki/thread.h @@ -0,0 +1,45 @@ +/* + * 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_THREAD_H +#define CHIAKI_THREAD_H + +#include "common.h" + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#include + +typedef struct chiaki_thread_t +{ + pthread_t thread; +} ChiakiThread; + +typedef void *(*ChiakiThreadFunc)(void *); + +CHIAKI_EXPORT bool chiaki_thread_create(ChiakiThread *thread, ChiakiThreadFunc func, void *arg); +CHIAKI_EXPORT bool chiaki_thread_join(ChiakiThread *thread, void **retval); + +#ifdef __cplusplus +} +#endif + +#endif // CHIAKI_THREAD_H diff --git a/lib/src/base64.c b/lib/src/base64.c new file mode 100644 index 0000000..26187a7 --- /dev/null +++ b/lib/src/base64.c @@ -0,0 +1,95 @@ +/* + * 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 + +// Implementation taken from +// https://en.wikibooks.org/wiki/Algorithm_Implementation/Miscellaneous/Base64 + +#define WHITESPACE 64 +#define EQUALS 65 +#define INVALID 66 + +static const unsigned char d[] = { + 66,66,66,66,66,66,66,66,66,66,64,66,66,66,66,66,66,66,66,66,66,66,66,66,66, + 66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,62,66,66,66,63,52,53, + 54,55,56,57,58,59,60,61,66,66,66,65,66,66,66, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, + 10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,66,66,66,66,66,66,26,27,28, + 29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,66,66, + 66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66, + 66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66, + 66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66, + 66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66, + 66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66, + 66,66,66,66,66,66 +}; + +CHIAKI_EXPORT bool chiaki_base64_decode(const char *in, size_t in_size, uint8_t *out, size_t *out_size) +{ + const char *end = in + in_size; + char iter = 0; + uint32_t buf = 0; + size_t len = 0; + + while (in < end) + { + unsigned char c = d[*in++]; + + switch(c) + { + case WHITESPACE: + continue; // skip whitespace + case INVALID: + return false; // invalid input + case EQUALS: // pad character, end of data + in = end; + continue; + default: + buf = buf << 6 | c; + iter++; // increment the number of iteration + // If the buffer is full, split it into bytes + if(iter == 4) + { + if((len += 3) > *out_size) + return false; // buffer overflow + *(out++) = (unsigned char)((buf >> 16) & 0xff); + *(out++) = (unsigned char)((buf >> 8) & 0xff); + *(out++) = (unsigned char)(buf & 0xff); + buf = 0; iter = 0; + } + } + } + + if(iter == 3) + { + if((len += 2) > *out_size) + return false; // buffer overflow + *(out++) = (unsigned char)((buf >> 10) & 0xff); + *(out++) = (unsigned char)((buf >> 2) & 0xff); + } + else if(iter == 2) + { + if(++len > *out_size) + return 1; // buffer overflow + *(out++) = (unsigned char)((buf >> 4) & 0xff); + } + + *out_size = len; + return true; +} \ No newline at end of file diff --git a/lib/src/session.c b/lib/src/session.c new file mode 100644 index 0000000..57662e7 --- /dev/null +++ b/lib/src/session.c @@ -0,0 +1,73 @@ +/* + * 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 + + +static const char session_request[] = + "GET /sce/rp/session HTTP/1.1\r\n" + "Host: 192.168. 1. 8:9295\r\n" + "User-Agent: remoteplay Windows\r\n" + "Connection: close\r\n" + "Content-Length: 0\r\n" + "RP-Registkey: 3131633065363864\r\n" + "Rp-Version: 8.0\r\n" + "\r\n"; + + +static void *session_thread_func(void *arg); + + +CHIAKI_EXPORT void chiaki_session_init(ChiakiSession *session, ChiakiConnectInfo *connect_info) +{ + session->connect_info.host = strdup(connect_info->host); + session->connect_info.regist_key = strdup(connect_info->regist_key); + session->connect_info.ostype = strdup(connect_info->ostype); + memcpy(session->connect_info.auth, connect_info->auth, sizeof(session->connect_info.auth)); + memcpy(session->connect_info.morning, connect_info->morning, sizeof(session->connect_info.morning)); +} + +CHIAKI_EXPORT void chiaki_session_fini(ChiakiSession *session) +{ + free(session->connect_info.host); + free(session->connect_info.regist_key); + free(session->connect_info.ostype); +} + +CHIAKI_EXPORT bool chiaki_session_start(ChiakiSession *session) +{ + bool r = chiaki_thread_create(&session->session_thread, session_thread_func, session); + if(!r) + return false; + return true; +} + +CHIAKI_EXPORT void chiaki_session_join(ChiakiSession *session) +{ + chiaki_thread_join(&session->session_thread, NULL); +} + +static void *session_thread_func(void *arg) +{ + ChiakiSession *session = arg; + printf("Sleepy...\n"); + return NULL; +} \ No newline at end of file diff --git a/lib/src/thread.c b/lib/src/thread.c new file mode 100644 index 0000000..256be43 --- /dev/null +++ b/lib/src/thread.c @@ -0,0 +1,43 @@ +/* + * 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 + + +CHIAKI_EXPORT bool chiaki_thread_create(ChiakiThread *thread, ChiakiThreadFunc func, void *arg) +{ + int r = pthread_create(&thread->thread, NULL, func, arg); + if(r != 0) + { + perror("pthread_create"); + return false; + } + return true; +} + +CHIAKI_EXPORT bool chiaki_thread_join(ChiakiThread *thread, void **retval) +{ + int r = pthread_join(thread->thread, retval); + if(r != 0) + { + perror("pthread_join"); + return false; + } + return true; +} \ No newline at end of file diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 12ce2c8..6c42fb5 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,3 +1,5 @@ add_executable(chiaki-test - main.c) \ No newline at end of file + main.c) + +target_link_libraries(chiaki-test chiaki-lib) \ No newline at end of file diff --git a/test/main.c b/test/main.c index 8a2f829..cee38eb 100644 --- a/test/main.c +++ b/test/main.c @@ -1,8 +1,42 @@ +#include +#include + #include +#include -int main() +int main(int argc, const char *argv[]) { - printf("Sleepy...\n"); + if(argc != 6) + { + printf("Usage: %s \n", argv[0]); + return 1; + } + + ChiakiConnectInfo connect_info; + connect_info.host = argv[1]; + connect_info.regist_key = argv[2]; + connect_info.ostype = argv[3]; + + size_t auth_len = strlen(argv[4]); + if(auth_len > sizeof(connect_info.auth)) + auth_len = sizeof(connect_info.auth); + memcpy(connect_info.auth, argv[4], auth_len); + if(auth_len < sizeof(connect_info.auth)) + memset(connect_info.auth + auth_len, 0, sizeof(connect_info.auth) - auth_len); + + size_t morning_size = sizeof(connect_info.morning); + bool r = chiaki_base64_decode(argv[5], strlen(argv[5]), connect_info.morning, &morning_size); + if(!r || morning_size != sizeof(connect_info.morning)) + { + printf("morning invalid.\n"); + return 1; + } + + ChiakiSession session; + chiaki_session_init(&session, &connect_info); + chiaki_session_start(&session); + chiaki_session_join(&session); + chiaki_session_fini(&session); return 0; } \ No newline at end of file