Skip to content

Commit

Permalink
feat: Endianness support
Browse files Browse the repository at this point in the history
  • Loading branch information
Chion82 committed Jun 24, 2017
1 parent 7d8a749 commit f494400
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 33 deletions.
14 changes: 7 additions & 7 deletions common.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 142,9 @@ void read_cb(struct ev_loop *loop, struct ev_io *w_, int revents) {

struct fragment_header* command_header = (struct fragment_header*)buffer;
bzero(command_header, sizeof(struct fragment_header));
command_header->conv = connection->conv;
command_header->conv = htonl(connection->conv);
command_header->command = CONNECTION_PUSH;
command_header->length = fragment_payload_length;
command_header->length = htonl(fragment_payload_length);

ikcp_send(kcp, buffer, sizeof(struct fragment_header) fragment_payload_length);

Expand Down Expand Up @@ -250,17 250,17 @@ void kcp_update_interval() {

if (pending_recv_stream_len >= sizeof(struct fragment_header)) {
struct fragment_header* command_header = (struct fragment_header*)pending_recv_stream;
if (pending_recv_stream_len >= sizeof(struct fragment_header) command_header->length) {
if (pending_recv_stream_len >= sizeof(struct fragment_header) ntohl(command_header->length)) {
handle_recv_stream();
}
}
}

void handle_recv_stream() {
struct fragment_header* command_header = (struct fragment_header*)pending_recv_stream;
int fragment_payload_length = command_header->length;
int fragment_payload_length = ntohl(command_header->length);
char* fragment_payload = (char*)command_header sizeof(struct fragment_header);
int conv = command_header->conv;
int conv = ntohl(command_header->conv);
struct connection_info* connection = &connection_queue[conv];

switch(command_header->command) {
Expand Down Expand Up @@ -363,7 363,7 @@ void handle_recv_stream() {
void notify_remote_connect(struct connection_info* connection) {
LOG("Notifying remote new connection. conv=%d", connection->conv);
struct fragment_header command_header;
command_header.conv = connection->conv;
command_header.conv = htonl(connection->conv);
command_header.command = CONNECTION_CONNECT;
command_header.length = 0;
ikcp_send(kcp, (char*)&command_header, sizeof(struct fragment_header));
Expand All @@ -372,7 372,7 @@ void notify_remote_connect(struct connection_info* connection) {
void notify_remote_close(struct connection_info* connection) {
LOG("Notifying remote closing. conv=%d", connection->conv);
struct fragment_header command_header;
command_header.conv = connection->conv;
command_header.conv = htonl(connection->conv);
command_header.command = CONNECTION_CLOSE;
command_header.length = 0;
ikcp_send(kcp, (char*)&command_header, sizeof(struct fragment_header));
Expand Down
4 changes: 2 additions & 2 deletions common.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 23,9 @@
#define is_valid_packet IS_VALID_PACKET

struct fragment_header {
int conv;
unsigned int conv;
char command;
int length;
unsigned int length;
};

struct io_wrap {
Expand Down
38 changes: 21 additions & 17 deletions ikcp.h
Original file line number Diff line number Diff line change
Expand Up @@ -228,23 228,27 @@ typedef struct IQUEUEHEAD iqueue_head;
// WORD ORDER
//---------------------------------------------------------------------
#ifndef IWORDS_BIG_ENDIAN
#ifdef _BIG_ENDIAN_
#if _BIG_ENDIAN_
#define IWORDS_BIG_ENDIAN 1
#endif
#endif
#ifndef IWORDS_BIG_ENDIAN
#if defined(__hppa__) || \
defined(__m68k__) || defined(mc68000) || defined(_M_M68K) || \
(defined(__MIPS__) && defined(__MISPEB__)) || \
defined(__ppc__) || defined(__POWERPC__) || defined(_M_PPC) || \
defined(__sparc__) || defined(__powerpc__) || \
defined(__mc68000__) || defined(__s390x__) || defined(__s390__)
#define IWORDS_BIG_ENDIAN 1
#endif
#endif
#ifndef IWORDS_BIG_ENDIAN
#define IWORDS_BIG_ENDIAN 0
#if defined(__BYTE_ORDER) && __BYTE_ORDER == __BIG_ENDIAN || \
defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ || \
defined(__BIG_ENDIAN__) || \
defined(__ARMEB__) || \
defined(__THUMBEB__) || \
defined(__AARCH64EB__) || \
defined(_MIPSEB) || defined(__MIPSEB) || defined(__MIPSEB__)
// It's a big-endian target architecture
#define IWORDS_BIG_ENDIAN 1
#elif defined(__BYTE_ORDER) && __BYTE_ORDER == __LITTLE_ENDIAN || \
defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ || \
defined(__LITTLE_ENDIAN__) || \
defined(__ARMEL__) || \
defined(__THUMBEL__) || \
defined(__AARCH64EL__) || \
defined(_MIPSEL) || defined(__MIPSEL) || defined(__MIPSEL__)
// It's a little-endian target architecture
#define IWORDS_BIG_ENDIAN 0
#else
// #error "Can not determining what architecture this is!"
#define IWORDS_BIG_ENDIAN 0
#endif
#endif

Expand Down
13 changes: 6 additions & 7 deletions trans_packet.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 10,6 @@
#include <netinet/if_ether.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <byteswap.h>
#include <ev.h>
#include <openssl/aes.h>

Expand Down Expand Up @@ -182,7 181,7 @@ void check_packet_recv(struct packet_info* packetinfo) {
}

if (!(packetinfo->disable_seq_update)) {
(packetinfo->state).ack = __bswap_32(tcph->seq) payloadlen;
(packetinfo->state).ack = ntohl(tcph->seq) payloadlen;
}

char* payload = buffer iphdrlen tcphdrlen;
Expand Down Expand Up @@ -267,8 266,8 @@ int send_packet(struct packet_info* packetinfo, char* source_payload, int source
//TCP Header
tcph->source = htons(packetinfo->source_port);
tcph->dest = sin.sin_port;
tcph->seq = __bswap_32((packetinfo->state).seq);
tcph->ack_seq = __bswap_32((packetinfo->state).ack);
tcph->seq = htonl((packetinfo->state).seq);
tcph->ack_seq = htonl((packetinfo->state).ack);
tcph->doff = 5; //tcp header size
tcph->fin=0;
tcph->syn=0;
Expand All @@ -291,16 290,16 @@ int send_packet(struct packet_info* packetinfo, char* source_payload, int source

if (flag == REPLY_SYN_ACK) {
tcph->seq = 0;
tcph->ack_seq = __bswap_32(1);
tcph->ack_seq = htonl(1);
tcph->syn = 1;
tcph->ack = 1;
tcph->psh=0;
LOG("[trans_packet]Server replying SYN ACK.");
}

if (flag == REPLY_ACK) {
tcph->seq = __bswap_32(1);
tcph->ack_seq = __bswap_32(1);
tcph->seq = htonl(1);
tcph->ack_seq = htonl(1);
tcph->syn = 0;
tcph->ack = 1;
tcph->psh=0;
Expand Down

0 comments on commit f494400

Please sign in to comment.