Use net_pack/unpack instead of host_to_net.

The latter is doing pretty much the same thing but in a confusing way (it
doesn't change the type of the variable, but does change the semantics).
This commit is contained in:
iphydf 2019-01-12 21:28:21 +00:00
parent cbc26d9b16
commit e35d70af18
No known key found for this signature in database
GPG Key ID: 3855DBA2D74403C9
5 changed files with 16 additions and 48 deletions

View File

@ -13,5 +13,6 @@ cirrus-ci_task:
- git clone --branch=upgrade-bazel --depth=1 https://github.com/iphydf/toktok-stack cirrus-ci-build
- mv c-toxcore cirrus-ci-build
- cd -
- bazel version
test_all_script:
- bazel test --copt=-DUSE_IPV6=0 -c opt -k //c-toxcore/...

View File

@ -1122,8 +1122,7 @@ static int file_sendrequest(const Messenger *m, int32_t friendnumber, uint8_t fi
packet[0] = filenumber;
file_type = net_htonl(file_type);
memcpy(packet + 1, &file_type, sizeof(file_type));
host_to_net((uint8_t *)&filesize, sizeof(filesize));
memcpy(packet + 1 + sizeof(file_type), &filesize, sizeof(filesize));
net_pack_u64(packet + 1 + sizeof(file_type), filesize);
memcpy(packet + 1 + sizeof(file_type) + sizeof(filesize), file_id, FILE_ID_LENGTH);
if (filename_length) {
@ -1362,10 +1361,10 @@ int file_seek(const Messenger *m, int32_t friendnumber, uint32_t filenumber, uin
return -6;
}
uint64_t sending_pos = position;
host_to_net((uint8_t *)&sending_pos, sizeof(sending_pos));
uint8_t sending_pos[sizeof(uint64_t)];
net_pack_u64(sending_pos, position);
if (send_file_control_packet(m, friendnumber, 1, file_number, FILECONTROL_SEEK, (uint8_t *)&sending_pos,
if (send_file_control_packet(m, friendnumber, 1, file_number, FILECONTROL_SEEK, sending_pos,
sizeof(sending_pos))) {
ft->transferred = position;
} else {
@ -1760,8 +1759,7 @@ static int handle_filecontrol(Messenger *m, int32_t friendnumber, uint8_t receiv
return -1;
}
memcpy(&position, data, sizeof(position));
net_to_host((uint8_t *) &position, sizeof(position));
net_unpack_u64(data, &position);
if (position >= ft->size) {
LOGGER_DEBUG(m->log,
@ -2327,8 +2325,7 @@ static int m_handle_packet(void *object, int i, const uint8_t *temp, uint16_t le
memcpy(&file_type, data + 1, sizeof(file_type));
file_type = net_ntohl(file_type);
memcpy(&filesize, data + 1 + sizeof(uint32_t), sizeof(filesize));
net_to_host((uint8_t *) &filesize, sizeof(filesize));
net_unpack_u64(data + 1 + sizeof(uint32_t), &filesize);
struct File_Transfers *ft = &m->friendlist[i].file_receiving[filenumber];
if (ft->status != FILESTATUS_NONE) {
@ -2755,7 +2752,7 @@ struct Saved_Friend {
uint16_t statusmessage_length;
uint8_t userstatus;
uint32_t friendrequest_nospam;
uint64_t last_seen_time;
uint8_t last_seen_time[sizeof(uint64_t)];
};
static uint32_t friend_size(void)
@ -2780,7 +2777,7 @@ static uint32_t friend_size(void)
VALUE_MEMBER(userstatus);
data += 3; // padding
VALUE_MEMBER(friendrequest_nospam);
VALUE_MEMBER(last_seen_time);
ARRAY_MEMBER(last_seen_time);
#undef VALUE_MEMBER
#undef ARRAY_MEMBER
@ -2814,7 +2811,7 @@ static uint8_t *friend_save(const struct Saved_Friend *temp, uint8_t *data)
VALUE_MEMBER(userstatus);
data += 3; // padding
VALUE_MEMBER(friendrequest_nospam);
VALUE_MEMBER(last_seen_time);
ARRAY_MEMBER(last_seen_time);
#undef VALUE_MEMBER
#undef ARRAY_MEMBER
@ -2849,7 +2846,7 @@ static const uint8_t *friend_load(struct Saved_Friend *temp, const uint8_t *data
VALUE_MEMBER(userstatus);
data += 3; // padding
VALUE_MEMBER(friendrequest_nospam);
VALUE_MEMBER(last_seen_time);
ARRAY_MEMBER(last_seen_time);
#undef VALUE_MEMBER
#undef ARRAY_MEMBER
@ -3028,10 +3025,7 @@ static uint8_t *friends_list_save(const Messenger *m, uint8_t *data)
temp.statusmessage_length = net_htons(m->friendlist[i].statusmessage_length);
temp.userstatus = m->friendlist[i].userstatus;
uint8_t last_seen_time[sizeof(uint64_t)];
memcpy(last_seen_time, &m->friendlist[i].last_seen_time, sizeof(uint64_t));
host_to_net(last_seen_time, sizeof(uint64_t));
memcpy(&temp.last_seen_time, last_seen_time, sizeof(uint64_t));
net_pack_u64(temp.last_seen_time, m->friendlist[i].last_seen_time);
}
uint8_t *next_data = friend_save(&temp, cur_data);
@ -3079,10 +3073,7 @@ static State_Load_Status friends_list_load(Messenger *m, const uint8_t *data, ui
setfriendname(m, fnum, temp.name, net_ntohs(temp.name_length));
set_friend_statusmessage(m, fnum, temp.statusmessage, net_ntohs(temp.statusmessage_length));
set_friend_userstatus(m, fnum, temp.userstatus);
uint8_t last_seen_time[sizeof(uint64_t)];
memcpy(last_seen_time, &temp.last_seen_time, sizeof(uint64_t));
net_to_host(last_seen_time, sizeof(uint64_t));
memcpy(&m->friendlist[fnum].last_seen_time, last_seen_time, sizeof(uint64_t));
net_unpack_u64(temp.last_seen_time, &m->friendlist[fnum].last_seen_time);
} else if (temp.status != 0) {
/* TODO(irungentoo): This is not a good way to do this. */
uint8_t address[FRIEND_ADDRESS_SIZE];

View File

@ -971,8 +971,7 @@ static int handle_dhtpk_announce(void *object, const uint8_t *source_pubkey, con
}
uint64_t no_replay;
memcpy(&no_replay, data + 1, sizeof(uint64_t));
net_to_host((uint8_t *) &no_replay, sizeof(no_replay));
net_unpack_u64(data + 1, &no_replay);
if (no_replay <= onion_c->friends_list[friend_num].last_noreplay) {
return 1;
@ -1207,9 +1206,8 @@ static int send_dhtpk_announce(Onion_Client *onion_c, uint16_t friend_num, uint8
uint8_t data[DHTPK_DATA_MAX_LENGTH];
data[0] = ONION_DATA_DHTPK;
uint64_t no_replay = mono_time_get(onion_c->mono_time);
host_to_net((uint8_t *)&no_replay, sizeof(no_replay));
memcpy(data + 1, &no_replay, sizeof(no_replay));
const uint64_t no_replay = mono_time_get(onion_c->mono_time);
net_pack_u64(data + 1, no_replay);
memcpy(data + 1 + sizeof(uint64_t), dht_get_self_public_key(onion_c->dht), CRYPTO_PUBLIC_KEY_SIZE);
Node_format nodes[MAX_SENT_NODES];
uint16_t num_relays = copy_connected_tcp_relays(onion_c->c, nodes, (MAX_SENT_NODES / 2));

View File

@ -52,25 +52,6 @@ uint32_t id_copy(uint8_t *dest, const uint8_t *src)
return CRYPTO_PUBLIC_KEY_SIZE;
}
void host_to_net(uint8_t *num, uint16_t numbytes)
{
#ifndef WORDS_BIGENDIAN
uint32_t i;
VLA(uint8_t, buff, numbytes);
for (i = 0; i < numbytes; ++i) {
buff[i] = num[numbytes - i - 1];
}
memcpy(num, buff, numbytes);
#endif
}
void net_to_host(uint8_t *num, uint16_t numbytes)
{
host_to_net(num, numbytes);
}
int create_recursive_mutex(pthread_mutex_t *mutex)
{
pthread_mutexattr_t attr;

View File

@ -40,9 +40,6 @@ extern "C" {
bool id_equal(const uint8_t *dest, const uint8_t *src);
uint32_t id_copy(uint8_t *dest, const uint8_t *src); /* return value is CLIENT_ID_SIZE */
void host_to_net(uint8_t *num, uint16_t numbytes);
void net_to_host(uint8_t *num, uint16_t numbytes);
/* Returns -1 if failed or 0 if success */
int create_recursive_mutex(pthread_mutex_t *mutex);