Prepare Ctrl

This commit is contained in:
Florian Märkl 2018-11-17 11:18:23 +01:00
commit 35c320d53d
No known key found for this signature in database
GPG key ID: 125BC8A5A6A1E857
5 changed files with 153 additions and 9 deletions

View file

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

41
lib/include/chiaki/ctrl.h Normal file
View file

@ -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 <https://www.gnu.org/licenses/>.
*/
#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

View file

@ -21,6 +21,7 @@
#include "common.h"
#include "thread.h"
#include "log.h"
#include "ctrl.h"
#include <stdint.h>
#include <netdb.h>
@ -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;

97
lib/src/ctrl.c Normal file
View file

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

View file

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