Basic Session

This commit is contained in:
Florian Märkl 2018-11-15 19:12:21 +01:00
commit a81de8f7b5
No known key found for this signature in database
GPG key ID: 125BC8A5A6A1E857
11 changed files with 445 additions and 3 deletions

View file

@ -3,4 +3,5 @@ cmake_minimum_required(VERSION 3.2)
project(chiaki)
add_subdirectory(lib)
add_subdirectory(test)

19
lib/CMakeLists.txt Normal file
View file

@ -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)

View file

@ -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 <https://www.gnu.org/licenses/>.
*/
#ifndef CHIAKI_BASE64_H
#define CHIAKI_BASE64_H
#include "common.h"
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
#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

View file

@ -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 <https://www.gnu.org/licenses/>.
*/
#ifndef CHIAKI_COMMON_H
#define CHIAKI_COMMON_H
#ifdef __cplusplus
extern "C" {
#endif
#define CHIAKI_EXPORT
#ifdef __cplusplus
}
#endif
#endif // CHIAKI_COMMON_H

View file

@ -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 <https://www.gnu.org/licenses/>.
*/
#ifndef CHIAKI_SESSION_H
#define CHIAKI_SESSION_H
#include "common.h"
#include "thread.h"
#include <stdint.h>
#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

View file

@ -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 <https://www.gnu.org/licenses/>.
*/
#ifndef CHIAKI_THREAD_H
#define CHIAKI_THREAD_H
#include "common.h"
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
#endif
#include <pthread.h>
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

95
lib/src/base64.c Normal file
View file

@ -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 <https://www.gnu.org/licenses/>.
*/
#include <chiaki/base64.h>
#include <stdint.h>
// 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;
}

73
lib/src/session.c Normal file
View file

@ -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 <https://www.gnu.org/licenses/>.
*/
#include <chiaki/session.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
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;
}

43
lib/src/thread.c Normal file
View file

@ -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 <https://www.gnu.org/licenses/>.
*/
#include <chiaki/thread.h>
#include <stdio.h>
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;
}

View file

@ -1,3 +1,5 @@
add_executable(chiaki-test
main.c)
target_link_libraries(chiaki-test chiaki-lib)

View file

@ -1,8 +1,42 @@
#include <chiaki/session.h>
#include <chiaki/base64.h>
#include <stdio.h>
#include <string.h>
int main()
int main(int argc, const char *argv[])
{
printf("Sleepy...\n");
if(argc != 6)
{
printf("Usage: %s <host> <registkey> <ostype> <auth> <morning (base64)>\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;
}