Skip to content

Commit

Permalink
Use size_t instead of uint32_t
Browse files Browse the repository at this point in the history
Encountered some crossplatform errors with uint_t-like length type
definition.
  • Loading branch information
Waujito committed Jan 6, 2025
1 parent a29db60 commit c2fec4c
Show file tree
Hide file tree
Showing 14 changed files with 308 additions and 286 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ jobs:
tool: mips64el-unknown-linux-musl
# - arch: mips64
# tool: mips64-unknown-linux-musl
# - arch: mipsel
# tool: mipsel-unknown-linux-musl
- arch: mipsel
tool: mipsel-unknown-linux-musl
# - arch: mipselsf
# tool: mipsel-unknown-linux-muslsf
# - arch: mips
Expand Down
4 changes: 2 additions & 2 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@

#include "types.h"

typedef int (*raw_send_t)(const unsigned char *data, unsigned int data_len);
typedef int (*raw_send_t)(const unsigned char *data, size_t data_len);
/**
* Sends the packet after delay_ms. The function should schedule send and return immediately
* (for example, open daemon thread)
*/
typedef int (*delayed_send_t)(const unsigned char *data, unsigned int data_len, unsigned int delay_ms);
typedef int (*delayed_send_t)(const unsigned char *data, size_t data_len, unsigned int delay_ms);

struct instance_config_t {
raw_send_t send_raw_packet;
Expand Down
12 changes: 6 additions & 6 deletions src/kytunblock.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ static void close_raw_socket(void) {
sock_release(rawsocket);
}

static int send_raw_ipv4(const uint8_t *pkt, uint32_t pktlen) {
static int send_raw_ipv4(const uint8_t *pkt, size_t pktlen) {
int ret = 0;
if (pktlen > AVAILABLE_MTU) return -ENOMEM;

Expand Down Expand Up @@ -141,7 +141,7 @@ static void close_raw6_socket(void) {
sock_release(raw6socket);
}

static int send_raw_ipv6(const uint8_t *pkt, uint32_t pktlen) {
static int send_raw_ipv6(const uint8_t *pkt, size_t pktlen) {
int ret = 0;
if (pktlen > AVAILABLE_MTU) return -ENOMEM;

Expand Down Expand Up @@ -177,7 +177,7 @@ static int send_raw_ipv6(const uint8_t *pkt, uint32_t pktlen) {
return ret;
}

static int send_raw_socket(const uint8_t *pkt, uint32_t pktlen) {
static int send_raw_socket(const uint8_t *pkt, size_t pktlen) {
int ret;

if (pktlen > AVAILABLE_MTU) {
Expand All @@ -194,8 +194,8 @@ static int send_raw_socket(const uint8_t *pkt, uint32_t pktlen) {
NETBUF_FREE(buff2);
return -ENOMEM;
}
uint32_t buff1_size = MAX_PACKET_SIZE;
uint32_t buff2_size = MAX_PACKET_SIZE;
size_t buff1_size = MAX_PACKET_SIZE;
size_t buff2_size = MAX_PACKET_SIZE;

if ((ret = tcp_frag(pkt, pktlen, AVAILABLE_MTU-128,
buff1, &buff1_size, buff2, &buff2_size)) < 0) {
Expand Down Expand Up @@ -241,7 +241,7 @@ static int send_raw_socket(const uint8_t *pkt, uint32_t pktlen) {
return ret;
}

static int delay_packet_send(const unsigned char *data, unsigned int data_len, unsigned int delay_ms) {
static int delay_packet_send(const unsigned char *data, size_t data_len, unsigned int delay_ms) {
lginfo("delay_packet_send won't work on current youtubeUnblock version");
return send_raw_socket(data, data_len);
}
Expand Down
84 changes: 42 additions & 42 deletions src/mangle.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@
#include <stdlib.h>
#endif

int process_packet(const uint8_t *raw_payload, uint32_t raw_payload_len) {
int process_packet(const uint8_t *raw_payload, size_t raw_payload_len) {
if (raw_payload_len > MAX_PACKET_SIZE) {
return PKT_ACCEPT;
}

const struct iphdr *iph;
const struct ip6_hdr *ip6h;
uint32_t iph_len;
size_t iph_len;
const uint8_t *ip_payload;
uint32_t ip_payload_len;
size_t ip_payload_len;

int transport_proto = -1;
int ipver = netproto_version(raw_payload, raw_payload_len);
Expand Down Expand Up @@ -121,13 +121,13 @@ int process_packet(const uint8_t *raw_payload, uint32_t raw_payload_len) {
return verdict;
}

int process_tcp_packet(const struct section_config_t *section, const uint8_t *raw_payload, uint32_t raw_payload_len) {
int process_tcp_packet(const struct section_config_t *section, const uint8_t *raw_payload, size_t raw_payload_len) {
const void *ipxh;
uint32_t iph_len;
size_t iph_len;
const struct tcphdr *tcph;
uint32_t tcph_len;
size_t tcph_len;
const uint8_t *data;
uint32_t dlen;
size_t dlen;


int ipxv = netproto_version(raw_payload, raw_payload_len);
Expand All @@ -153,7 +153,7 @@ int process_tcp_packet(const struct section_config_t *section, const uint8_t *ra

memcpy(payload, ipxh, iph_len);
memcpy(payload + iph_len, tcph, tcph_len);
uint32_t fake_len = section->fake_sni_pkt_sz;
size_t fake_len = section->fake_sni_pkt_sz;

if (section->synfake_len)
fake_len = min(section->synfake_len, fake_len);
Expand Down Expand Up @@ -204,7 +204,7 @@ int process_tcp_packet(const struct section_config_t *section, const uint8_t *ra
size_t sni_offset = vrd.sni_ptr - data;
size_t target_sni_offset = vrd.target_sni_ptr - data;

uint32_t payload_len = raw_payload_len;
size_t payload_len = raw_payload_len;
NETBUF_ALLOC(payload, MAX_PACKET_SIZE);
if (!NETBUF_CHECK(payload)) {
lgerror(-ENOMEM, "Allocation error");
Expand All @@ -214,11 +214,11 @@ int process_tcp_packet(const struct section_config_t *section, const uint8_t *ra
memcpy(payload, raw_payload, raw_payload_len);

void *iph;
uint32_t iph_len;
size_t iph_len;
struct tcphdr *tcph;
uint32_t tcph_len;
size_t tcph_len;
uint8_t *data;
uint32_t dlen;
size_t dlen;

int ret = tcp_payload_split(payload, payload_len,
&iph, &iph_len, &tcph, &tcph_len,
Expand Down Expand Up @@ -262,7 +262,7 @@ int process_tcp_packet(const struct section_config_t *section, const uint8_t *ra
ipd_offset = target_sni_offset;
mid_offset = ipd_offset + vrd.target_sni_len / 2;

uint32_t poses[2];
size_t poses[2];
int cnt = 0;

if (section->frag_sni_pos && dlen > section->frag_sni_pos) {
Expand All @@ -274,7 +274,7 @@ int process_tcp_packet(const struct section_config_t *section, const uint8_t *ra
}

if (cnt > 1 && poses[0] > poses[1]) {
uint32_t tmp = poses[0];
size_t tmp = poses[0];
poses[0] = poses[1];
poses[1] = tmp;
}
Expand All @@ -294,7 +294,7 @@ int process_tcp_packet(const struct section_config_t *section, const uint8_t *ra
mid_offset = ipd_offset + vrd.target_sni_len / 2;
mid_offset += 8 - mid_offset % 8;

uint32_t poses[2];
size_t poses[2];
int cnt = 0;

if (section->frag_sni_pos && dlen > section->frag_sni_pos) {
Expand All @@ -308,7 +308,7 @@ int process_tcp_packet(const struct section_config_t *section, const uint8_t *ra
}

if (cnt > 1 && poses[0] > poses[1]) {
uint32_t tmp = poses[0];
size_t tmp = poses[0];
poses[0] = poses[1];
poses[1] = tmp;
}
Expand Down Expand Up @@ -356,12 +356,12 @@ int process_tcp_packet(const struct section_config_t *section, const uint8_t *ra
return PKT_DROP;
}

int process_udp_packet(const struct section_config_t *section, const uint8_t *pkt, uint32_t pktlen) {
int process_udp_packet(const struct section_config_t *section, const uint8_t *pkt, size_t pktlen) {
const void *iph;
uint32_t iph_len;
size_t iph_len;
const struct udphdr *udph;
const uint8_t *data;
uint32_t dlen;
size_t dlen;

int ret = udp_payload_split((uint8_t *)pkt, pktlen,
(void **)&iph, &iph_len,
Expand Down Expand Up @@ -398,7 +398,7 @@ int process_udp_packet(const struct section_config_t *section, const uint8_t *pk
lgerror(-ENOMEM, "Allocation error");
return -ENOMEM;
}
uint32_t fsn_len = MAX_PACKET_SIZE;
size_t fsn_len = MAX_PACKET_SIZE;

struct udp_fake_type fake_type = {
.fake_len = section->udp_fake_len,
Expand Down Expand Up @@ -441,20 +441,20 @@ int process_udp_packet(const struct section_config_t *section, const uint8_t *pk
return PKT_DROP;
}

int send_ip4_frags(const struct section_config_t *section, const uint8_t *packet, uint32_t pktlen, const uint32_t *poses, uint32_t poses_sz, uint32_t dvs) {
int send_ip4_frags(const struct section_config_t *section, const uint8_t *packet, size_t pktlen, const size_t *poses, size_t poses_sz, size_t dvs) {
if (poses_sz == 0) {
if (section->seg2_delay && ((dvs > 0) ^ section->frag_sni_reverse)) {
if (!instance_config.send_delayed_packet) {
return -EINVAL;
}

lgtrace_addp("Sent %d delayed for %d", pktlen, section->seg2_delay);
lgtrace_addp("Sent %zu delayed for %d", pktlen, section->seg2_delay);
instance_config.send_delayed_packet(
packet, pktlen, section->seg2_delay);

return 0;
} else {
lgtrace_addp("Sent %d bytes", pktlen);
lgtrace_addp("Sent %zu bytes", pktlen);
return instance_config.send_raw_packet(
packet, pktlen);
}
Expand Down Expand Up @@ -482,26 +482,26 @@ int send_ip4_frags(const struct section_config_t *section, const uint8_t *packet
}
*/

uint32_t f1len = MAX_PACKET_SIZE;
uint32_t f2len = MAX_PACKET_SIZE;
// uint32_t fake_pad_len = MAX_PACKET_SIZE;
size_t f1len = MAX_PACKET_SIZE;
size_t f2len = MAX_PACKET_SIZE;
// size_t fake_pad_len = MAX_PACKET_SIZE;

int ret;

if (dvs > poses[0]) {
lgerror(-EINVAL, "send_frags: Recursive dvs(%d) is more than poses0(%d)", dvs, poses[0]);
lgerror(-EINVAL, "send_frags: Recursive dvs(%zu) is more than poses0(%zu)", dvs, poses[0]);
ret = -EINVAL;
goto erret_lc;
}

uint32_t frag_pos = poses[0] - dvs;
size_t frag_pos = poses[0] - dvs;
frag_pos += 8 - frag_pos % 8;

ret = ip4_frag(packet, pktlen, frag_pos,
frag1, &f1len, frag2, &f2len);

if (ret < 0) {
lgerror(ret, "send_frags: frag: with context packet with size %d, position: %d, recursive dvs: %d", pktlen, poses[0], dvs);
lgerror(ret, "send_frags: frag: with context packet with size %zu, position: %zu, recursive dvs: %zu", pktlen, poses[0], dvs);
goto erret_lc;
}

Expand All @@ -522,7 +522,7 @@ int send_ip4_frags(const struct section_config_t *section, const uint8_t *packet
/*
if (section->frag_sni_faked) {
ITER_FAKE_STRAT(section->faking_strategy, strategy) {
uint32_t iphfl;
size_t iphfl;
fake_pad_len = f2len;
ret = ip4_payload_split(frag2, f2len, NULL, &iphfl, NULL, NULL);
if (ret < 0) {
Expand Down Expand Up @@ -573,7 +573,7 @@ int send_ip4_frags(const struct section_config_t *section, const uint8_t *packet
return 0;
}

int send_tcp_frags(const struct section_config_t *section, const uint8_t *packet, uint32_t pktlen, const uint32_t *poses, uint32_t poses_sz, uint32_t dvs) {
int send_tcp_frags(const struct section_config_t *section, const uint8_t *packet, size_t pktlen, const size_t *poses, size_t poses_sz, size_t dvs) {
if (poses_sz == 0) {
if (section->seg2_delay && ((dvs > 0) ^ section->frag_sni_reverse)) {
if (!instance_config.send_delayed_packet) {
Expand All @@ -585,7 +585,7 @@ int send_tcp_frags(const struct section_config_t *section, const uint8_t *packet

return 0;
} else {
lgtrace_addp("raw send packet of %d bytes with %d dvs", pktlen, dvs);
lgtrace_addp("raw send packet of %zu bytes with %zu dvs", pktlen, dvs);
return instance_config.send_raw_packet(
packet, pktlen);
}
Expand All @@ -603,13 +603,13 @@ int send_tcp_frags(const struct section_config_t *section, const uint8_t *packet
return -ENOMEM;
}

uint32_t f1len = MAX_PACKET_SIZE;
uint32_t f2len = MAX_PACKET_SIZE;
size_t f1len = MAX_PACKET_SIZE;
size_t f2len = MAX_PACKET_SIZE;

int ret;

if (dvs > poses[0]) {
lgerror(-EINVAL, "send_frags: Recursive dvs(%d) is more than poses0(%d)", dvs, poses[0]);
lgerror(-EINVAL, "send_frags: Recursive dvs(%zu) is more than poses0(%zu)", dvs, poses[0]);
ret = -EINVAL;
goto erret_lc;
}
Expand All @@ -619,10 +619,10 @@ int send_tcp_frags(const struct section_config_t *section, const uint8_t *packet
frag1, &f1len, frag2, &f2len);


lgtrace_addp("Packet split in %d bytes position of payload start, dvs: %d to two packets of %d and %d lengths", poses[0], dvs, f1len, f2len);
lgtrace_addp("Packet split in %zu bytes position of payload start, dvs: %zu to two packets of %zu and %zu lengths", poses[0], dvs, f1len, f2len);

if (ret < 0) {
lgerror(ret, "send_frags: tcp_frag: with context packet with size %d, position: %d", pktlen, poses[0]);
lgerror(ret, "send_frags: tcp_frag: with context packet with size %zu, position: %zu", pktlen, poses[0]);
goto erret_lc;
}

Expand All @@ -643,7 +643,7 @@ int send_tcp_frags(const struct section_config_t *section, const uint8_t *packet

send_fake:
if (section->frag_sni_faked) {
uint32_t iphfl, tcphfl;
size_t iphfl, tcphfl;
void *iph;
struct tcphdr *tcph;
ret = tcp_payload_split(frag2, f2len, &iph, &iphfl, &tcph, &tcphfl, NULL, NULL);
Expand Down Expand Up @@ -712,7 +712,7 @@ int post_fake_sni(struct fake_type f_type,
lgerror(-ENOMEM, "Allocation error");
return -ENOMEM;
}
uint32_t fsn_len = MAX_PACKET_SIZE;
size_t fsn_len = MAX_PACKET_SIZE;

ret = gen_fake_sni(
fake_seq_type,
Expand All @@ -734,9 +734,9 @@ int post_fake_sni(struct fake_type f_type,
lgerror(ret, "send fake sni");
goto erret_lc;
}
uint32_t iph_len;
uint32_t tcph_len;
uint32_t plen;
size_t iph_len;
size_t tcph_len;
size_t plen;
ret = tcp_payload_split(
fake_sni, fsn_len,
&fsiph, &iph_len,
Expand Down
14 changes: 7 additions & 7 deletions src/mangle.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,21 @@
* Processes the packet and returns verdict.
* This is the primary function that traverses the packet.
*/
int process_packet(const uint8_t *packet, uint32_t packet_len);
int process_packet(const uint8_t *packet, size_t packet_len);


/**
* Processe the TCP packet.
* Returns verdict.
*/
int process_tcp_packet(const struct section_config_t *section, const uint8_t *raw_payload, uint32_t raw_payload_len);
int process_tcp_packet(const struct section_config_t *section, const uint8_t *raw_payload, size_t raw_payload_len);


/**
* Processes the UDP packet.
* Returns verdict.
*/
int process_udp_packet(const struct section_config_t *section, const uint8_t *pkt, uint32_t pktlen);
int process_udp_packet(const struct section_config_t *section, const uint8_t *pkt, size_t pktlen);



Expand All @@ -63,15 +63,15 @@ int post_fake_sni(struct fake_type f_type,
* dvs used internally and should be zero.
*/
int send_tcp_frags(const struct section_config_t *section,
const uint8_t *packet, uint32_t pktlen,
const uint32_t *poses, uint32_t poses_len, uint32_t dvs);
const uint8_t *packet, size_t pktlen,
const size_t *poses, size_t poses_len, size_t dvs);

/**
* Splits packet by poses and posts.
* Poses are relative to start of TCP payload.
* dvs used internally and should be zero.
*/
int send_ip4_frags(const struct section_config_t *section,
const uint8_t *packet, uint32_t pktlen,
const uint32_t *poses, uint32_t poses_len, uint32_t dvs);
const uint8_t *packet, size_t pktlen,
const size_t *poses, size_t poses_len, size_t dvs);
#endif /* YU_MANGLE_H */
Loading

0 comments on commit c2fec4c

Please sign in to comment.