Senkusha send big

This commit is contained in:
Florian Märkl 2018-11-23 21:45:17 +01:00
commit 99242c726b
No known key found for this signature in database
GPG key ID: 125BC8A5A6A1E857
5 changed files with 102 additions and 15 deletions

View file

@ -22,7 +22,8 @@ set(SOURCE_FILES
src/rpcrypt.c
src/takion.c
src/senkusha.c
src/utils.h)
src/utils.h
src/pb_utils.h)
add_subdirectory(protobuf)
include_directories("${NANOPB_SOURCE_DIR}")
@ -40,3 +41,5 @@ target_link_libraries(chiaki-lib Threads::Threads)
find_package(OpenSSL REQUIRED)
target_link_libraries(chiaki-lib OpenSSL::Crypto)
target_link_libraries(chiaki-lib protobuf-nanopb-static)

View file

@ -62,7 +62,7 @@ typedef struct chiaki_takion_t
CHIAKI_EXPORT ChiakiErrorCode chiaki_takion_connect(ChiakiTakion *takion, ChiakiTakionConnectInfo *info);
CHIAKI_EXPORT void chiaki_takion_close(ChiakiTakion *takion);
CHIAKI_EXPORT ChiakiErrorCode chiaki_takion_send_raw(ChiakiTakion *takion, uint8_t *buf, size_t buf_size);
CHIAKI_EXPORT ChiakiErrorCode chiaki_send_message_data(ChiakiTakion *takion, uint8_t *buf, size_t buf_size);
CHIAKI_EXPORT ChiakiErrorCode chiaki_takion_send_message_data(ChiakiTakion *takion, uint32_t key_pos, uint8_t type_b, uint16_t channel, uint8_t *buf, size_t buf_size);
#ifdef __cplusplus
}

33
lib/src/pb_utils.h Normal file
View file

@ -0,0 +1,33 @@
/*
* 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_PB_UTILS_H
#define CHIAKI_PB_UTILS_H
#include <pb_encode.h>
bool chiaki_pb_encode_string(pb_ostream_t *stream, const pb_field_t *field, void *const *arg)
{
char *str = *arg;
if (!pb_encode_tag_for_field(stream, field))
return false;
return pb_encode_string(stream, (uint8_t*)str, strlen(str));
}
#endif // CHIAKI_PB_UTILS_H

View file

@ -25,6 +25,11 @@
#include <zconf.h>
#include "utils.h"
#include "pb_utils.h"
#include <takion.pb.h>
#include <pb_encode.h>
#include <pb.h>
#define SENKUSHA_SOCKET 9297
@ -37,7 +42,8 @@ typedef struct senkusha_t
} Senkusha;
void senkusha_takion_data(uint8_t *buf, size_t buf_size, void *user);
static void senkusha_takion_data(uint8_t *buf, size_t buf_size, void *user);
static ChiakiErrorCode senkusha_send_big(Senkusha *senkusha);
CHIAKI_EXPORT ChiakiErrorCode chiaki_senkusha_run(ChiakiSession *session)
{
@ -65,16 +71,61 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_senkusha_run(ChiakiSession *session)
return err;
}
CHIAKI_LOGI(&session->log, "Senkusha sending big\n");
err = senkusha_send_big(&senkusha);
if(err != CHIAKI_ERR_SUCCESS)
{
CHIAKI_LOGE(&session->log, "Senkusha failed to send big\n");
return err;
// TODO: close takion
}
while(true)
sleep(1);
return CHIAKI_ERR_SUCCESS;
}
void senkusha_takion_data(uint8_t *buf, size_t buf_size, void *user)
static void senkusha_takion_data(uint8_t *buf, size_t buf_size, void *user)
{
Senkusha *senkusha = user;
CHIAKI_LOGD(senkusha->log, "Senkusha received data:\n");
chiaki_log_hexdump(senkusha->log, CHIAKI_LOG_DEBUG, buf, buf_size);
}
static ChiakiErrorCode senkusha_send_big(Senkusha *senkusha)
{
tkproto_TakionMessage msg;
memset(&msg, 0, sizeof(msg));
CHIAKI_LOGD(senkusha->log, "sizeof(tkproto_TakionMessage) = %lu\n", sizeof(tkproto_TakionMessage));
msg.type = tkproto_TakionMessage_PayloadType_BIG;
msg.has_big_payload = true;
msg.big_payload.client_version = 7;
msg.big_payload.session_key.arg = "";
msg.big_payload.session_key.funcs.encode = chiaki_pb_encode_string;
msg.big_payload.launch_spec.arg = "";
msg.big_payload.launch_spec.funcs.encode = chiaki_pb_encode_string;
msg.big_payload.encrypted_key.arg = "";
msg.big_payload.encrypted_key.funcs.encode = chiaki_pb_encode_string;
uint8_t buf[512];
size_t buf_size;
pb_ostream_t stream = pb_ostream_from_buffer(buf, sizeof(buf));
bool pbr = pb_encode(&stream, tkproto_TakionMessage_fields, &msg);
if(!pbr)
{
CHIAKI_LOGE(senkusha->log, "Senkusha big protobuf encoding failed\n");
return CHIAKI_ERR_UNKNOWN;
}
buf_size = stream.bytes_written;
ChiakiErrorCode err = chiaki_takion_send_message_data(&senkusha->takion, 0, 1, 1, buf, buf_size);
return err;
}

View file

@ -140,7 +140,7 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_takion_connect(ChiakiTakion *takion, Chiaki
init_payload.something = TAKION_LOCAL_SOMETHING;
init_payload.min = TAKION_LOCAL_MIN;
init_payload.max = TAKION_LOCAL_MIN;
init_payload.tag1 = takion->tag_remote;
init_payload.tag1 = takion->tag_local;
ChiakiErrorCode err = takion_send_message_init(takion, &init_payload);
if(err != CHIAKI_ERR_SUCCESS)
{
@ -395,11 +395,11 @@ static ChiakiErrorCode takion_parse_message(ChiakiTakion *takion, uint8_t *buf,
return CHIAKI_ERR_INVALID_DATA;
}
msg->tag = htonl(*((uint32_t *)buf));
msg->key_pos = htonl(*((uint32_t *)(buf + 0x8)));
msg->tag = ntohl(*((uint32_t *)buf));
msg->key_pos = ntohl(*((uint32_t *)(buf + 0x8)));
msg->type_a = buf[0xc];
msg->type_b = buf[0xd];
msg->payload_size = htons(*((uint16_t *)(buf + 0xe)));
msg->payload_size = ntohs(*((uint16_t *)(buf + 0xe)));
if(msg->tag != takion->tag_local)
{
@ -490,11 +490,11 @@ static ChiakiErrorCode takion_recv_message_init_ack(ChiakiTakion *takion, Takion
assert(msg.payload_size == 0x10 + TAKION_COOKIE_SIZE);
uint8_t *pl = msg.payload;
payload->tag = htonl(*((uint32_t *)(pl + 0)));
payload->unknown0 = htonl(*((uint32_t *)(pl + 4)));
payload->min = htons(*((uint16_t *)(pl + 8)));
payload->max = htons(*((uint16_t *)(pl + 0xa)));
payload->unknown1 = htonl(*((uint16_t *)(pl + 0xc)));
payload->tag = ntohl(*((uint32_t *)(pl + 0)));
payload->unknown0 = ntohl(*((uint32_t *)(pl + 4)));
payload->min = ntohs(*((uint16_t *)(pl + 8)));
payload->max = ntohs(*((uint16_t *)(pl + 0xa)));
payload->unknown1 = ntohl(*((uint16_t *)(pl + 0xc)));
memcpy(payload->cookie, pl + 0x10, TAKION_COOKIE_SIZE);
return CHIAKI_ERR_SUCCESS;