summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoriphydf <iphydf@users.noreply.github.com>2019-01-12 21:28:21 +0000
committeriphydf <iphydf@users.noreply.github.com>2020-03-05 22:56:26 +0000
commite35d70af1822095b7b273f1cec16878645b90078 (patch)
tree143241b035976125cfb7fee241e3fde9ad02195b
parentcbc26d9b165277e3c450806e1da1e6904a9dcedb (diff)
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).
-rw-r--r--.cirrus.yml1
-rw-r--r--toxcore/Messenger.c33
-rw-r--r--toxcore/onion_client.c8
-rw-r--r--toxcore/util.c19
-rw-r--r--toxcore/util.h3
5 files changed, 16 insertions, 48 deletions
diff --git a/.cirrus.yml b/.cirrus.yml
index 7a289e53..54fedfb1 100644
--- a/.cirrus.yml
+++ b/.cirrus.yml
@@ -13,5 +13,6 @@ cirrus-ci_task:
13 - git clone --branch=upgrade-bazel --depth=1 https://github.com/iphydf/toktok-stack cirrus-ci-build 13 - git clone --branch=upgrade-bazel --depth=1 https://github.com/iphydf/toktok-stack cirrus-ci-build
14 - mv c-toxcore cirrus-ci-build 14 - mv c-toxcore cirrus-ci-build
15 - cd - 15 - cd -
16 - bazel version
16 test_all_script: 17 test_all_script:
17 - bazel test --copt=-DUSE_IPV6=0 -c opt -k //c-toxcore/... 18 - bazel test --copt=-DUSE_IPV6=0 -c opt -k //c-toxcore/...
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c
index 2dccd4a8..b745d99b 100644
--- a/toxcore/Messenger.c
+++ b/toxcore/Messenger.c
@@ -1122,8 +1122,7 @@ static int file_sendrequest(const Messenger *m, int32_t friendnumber, uint8_t fi
1122 packet[0] = filenumber; 1122 packet[0] = filenumber;
1123 file_type = net_htonl(file_type); 1123 file_type = net_htonl(file_type);
1124 memcpy(packet + 1, &file_type, sizeof(file_type)); 1124 memcpy(packet + 1, &file_type, sizeof(file_type));
1125 host_to_net((uint8_t *)&filesize, sizeof(filesize)); 1125 net_pack_u64(packet + 1 + sizeof(file_type), filesize);
1126 memcpy(packet + 1 + sizeof(file_type), &filesize, sizeof(filesize));
1127 memcpy(packet + 1 + sizeof(file_type) + sizeof(filesize), file_id, FILE_ID_LENGTH); 1126 memcpy(packet + 1 + sizeof(file_type) + sizeof(filesize), file_id, FILE_ID_LENGTH);
1128 1127
1129 if (filename_length) { 1128 if (filename_length) {
@@ -1362,10 +1361,10 @@ int file_seek(const Messenger *m, int32_t friendnumber, uint32_t filenumber, uin
1362 return -6; 1361 return -6;
1363 } 1362 }
1364 1363
1365 uint64_t sending_pos = position; 1364 uint8_t sending_pos[sizeof(uint64_t)];
1366 host_to_net((uint8_t *)&sending_pos, sizeof(sending_pos)); 1365 net_pack_u64(sending_pos, position);
1367 1366
1368 if (send_file_control_packet(m, friendnumber, 1, file_number, FILECONTROL_SEEK, (uint8_t *)&sending_pos, 1367 if (send_file_control_packet(m, friendnumber, 1, file_number, FILECONTROL_SEEK, sending_pos,
1369 sizeof(sending_pos))) { 1368 sizeof(sending_pos))) {
1370 ft->transferred = position; 1369 ft->transferred = position;
1371 } else { 1370 } else {
@@ -1760,8 +1759,7 @@ static int handle_filecontrol(Messenger *m, int32_t friendnumber, uint8_t receiv
1760 return -1; 1759 return -1;
1761 } 1760 }
1762 1761
1763 memcpy(&position, data, sizeof(position)); 1762 net_unpack_u64(data, &position);
1764 net_to_host((uint8_t *) &position, sizeof(position));
1765 1763
1766 if (position >= ft->size) { 1764 if (position >= ft->size) {
1767 LOGGER_DEBUG(m->log, 1765 LOGGER_DEBUG(m->log,
@@ -2327,8 +2325,7 @@ static int m_handle_packet(void *object, int i, const uint8_t *temp, uint16_t le
2327 memcpy(&file_type, data + 1, sizeof(file_type)); 2325 memcpy(&file_type, data + 1, sizeof(file_type));
2328 file_type = net_ntohl(file_type); 2326 file_type = net_ntohl(file_type);
2329 2327
2330 memcpy(&filesize, data + 1 + sizeof(uint32_t), sizeof(filesize)); 2328 net_unpack_u64(data + 1 + sizeof(uint32_t), &filesize);
2331 net_to_host((uint8_t *) &filesize, sizeof(filesize));
2332 struct File_Transfers *ft = &m->friendlist[i].file_receiving[filenumber]; 2329 struct File_Transfers *ft = &m->friendlist[i].file_receiving[filenumber];
2333 2330
2334 if (ft->status != FILESTATUS_NONE) { 2331 if (ft->status != FILESTATUS_NONE) {
@@ -2755,7 +2752,7 @@ struct Saved_Friend {
2755 uint16_t statusmessage_length; 2752 uint16_t statusmessage_length;
2756 uint8_t userstatus; 2753 uint8_t userstatus;
2757 uint32_t friendrequest_nospam; 2754 uint32_t friendrequest_nospam;
2758 uint64_t last_seen_time; 2755 uint8_t last_seen_time[sizeof(uint64_t)];
2759}; 2756};
2760 2757
2761static uint32_t friend_size(void) 2758static uint32_t friend_size(void)
@@ -2780,7 +2777,7 @@ static uint32_t friend_size(void)
2780 VALUE_MEMBER(userstatus); 2777 VALUE_MEMBER(userstatus);
2781 data += 3; // padding 2778 data += 3; // padding
2782 VALUE_MEMBER(friendrequest_nospam); 2779 VALUE_MEMBER(friendrequest_nospam);
2783 VALUE_MEMBER(last_seen_time); 2780 ARRAY_MEMBER(last_seen_time);
2784 2781
2785#undef VALUE_MEMBER 2782#undef VALUE_MEMBER
2786#undef ARRAY_MEMBER 2783#undef ARRAY_MEMBER
@@ -2814,7 +2811,7 @@ static uint8_t *friend_save(const struct Saved_Friend *temp, uint8_t *data)
2814 VALUE_MEMBER(userstatus); 2811 VALUE_MEMBER(userstatus);
2815 data += 3; // padding 2812 data += 3; // padding
2816 VALUE_MEMBER(friendrequest_nospam); 2813 VALUE_MEMBER(friendrequest_nospam);
2817 VALUE_MEMBER(last_seen_time); 2814 ARRAY_MEMBER(last_seen_time);
2818 2815
2819#undef VALUE_MEMBER 2816#undef VALUE_MEMBER
2820#undef ARRAY_MEMBER 2817#undef ARRAY_MEMBER
@@ -2849,7 +2846,7 @@ static const uint8_t *friend_load(struct Saved_Friend *temp, const uint8_t *data
2849 VALUE_MEMBER(userstatus); 2846 VALUE_MEMBER(userstatus);
2850 data += 3; // padding 2847 data += 3; // padding
2851 VALUE_MEMBER(friendrequest_nospam); 2848 VALUE_MEMBER(friendrequest_nospam);
2852 VALUE_MEMBER(last_seen_time); 2849 ARRAY_MEMBER(last_seen_time);
2853 2850
2854#undef VALUE_MEMBER 2851#undef VALUE_MEMBER
2855#undef ARRAY_MEMBER 2852#undef ARRAY_MEMBER
@@ -3028,10 +3025,7 @@ static uint8_t *friends_list_save(const Messenger *m, uint8_t *data)
3028 temp.statusmessage_length = net_htons(m->friendlist[i].statusmessage_length); 3025 temp.statusmessage_length = net_htons(m->friendlist[i].statusmessage_length);
3029 temp.userstatus = m->friendlist[i].userstatus; 3026 temp.userstatus = m->friendlist[i].userstatus;
3030 3027
3031 uint8_t last_seen_time[sizeof(uint64_t)]; 3028 net_pack_u64(temp.last_seen_time, m->friendlist[i].last_seen_time);
3032 memcpy(last_seen_time, &m->friendlist[i].last_seen_time, sizeof(uint64_t));
3033 host_to_net(last_seen_time, sizeof(uint64_t));
3034 memcpy(&temp.last_seen_time, last_seen_time, sizeof(uint64_t));
3035 } 3029 }
3036 3030
3037 uint8_t *next_data = friend_save(&temp, cur_data); 3031 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
3079 setfriendname(m, fnum, temp.name, net_ntohs(temp.name_length)); 3073 setfriendname(m, fnum, temp.name, net_ntohs(temp.name_length));
3080 set_friend_statusmessage(m, fnum, temp.statusmessage, net_ntohs(temp.statusmessage_length)); 3074 set_friend_statusmessage(m, fnum, temp.statusmessage, net_ntohs(temp.statusmessage_length));
3081 set_friend_userstatus(m, fnum, temp.userstatus); 3075 set_friend_userstatus(m, fnum, temp.userstatus);
3082 uint8_t last_seen_time[sizeof(uint64_t)]; 3076 net_unpack_u64(temp.last_seen_time, &m->friendlist[fnum].last_seen_time);
3083 memcpy(last_seen_time, &temp.last_seen_time, sizeof(uint64_t));
3084 net_to_host(last_seen_time, sizeof(uint64_t));
3085 memcpy(&m->friendlist[fnum].last_seen_time, last_seen_time, sizeof(uint64_t));
3086 } else if (temp.status != 0) { 3077 } else if (temp.status != 0) {
3087 /* TODO(irungentoo): This is not a good way to do this. */ 3078 /* TODO(irungentoo): This is not a good way to do this. */
3088 uint8_t address[FRIEND_ADDRESS_SIZE]; 3079 uint8_t address[FRIEND_ADDRESS_SIZE];
diff --git a/toxcore/onion_client.c b/toxcore/onion_client.c
index d11a91e8..6dd945b0 100644
--- a/toxcore/onion_client.c
+++ b/toxcore/onion_client.c
@@ -971,8 +971,7 @@ static int handle_dhtpk_announce(void *object, const uint8_t *source_pubkey, con
971 } 971 }
972 972
973 uint64_t no_replay; 973 uint64_t no_replay;
974 memcpy(&no_replay, data + 1, sizeof(uint64_t)); 974 net_unpack_u64(data + 1, &no_replay);
975 net_to_host((uint8_t *) &no_replay, sizeof(no_replay));
976 975
977 if (no_replay <= onion_c->friends_list[friend_num].last_noreplay) { 976 if (no_replay <= onion_c->friends_list[friend_num].last_noreplay) {
978 return 1; 977 return 1;
@@ -1207,9 +1206,8 @@ static int send_dhtpk_announce(Onion_Client *onion_c, uint16_t friend_num, uint8
1207 1206
1208 uint8_t data[DHTPK_DATA_MAX_LENGTH]; 1207 uint8_t data[DHTPK_DATA_MAX_LENGTH];
1209 data[0] = ONION_DATA_DHTPK; 1208 data[0] = ONION_DATA_DHTPK;
1210 uint64_t no_replay = mono_time_get(onion_c->mono_time); 1209 const uint64_t no_replay = mono_time_get(onion_c->mono_time);
1211 host_to_net((uint8_t *)&no_replay, sizeof(no_replay)); 1210 net_pack_u64(data + 1, no_replay);
1212 memcpy(data + 1, &no_replay, sizeof(no_replay));
1213 memcpy(data + 1 + sizeof(uint64_t), dht_get_self_public_key(onion_c->dht), CRYPTO_PUBLIC_KEY_SIZE); 1211 memcpy(data + 1 + sizeof(uint64_t), dht_get_self_public_key(onion_c->dht), CRYPTO_PUBLIC_KEY_SIZE);
1214 Node_format nodes[MAX_SENT_NODES]; 1212 Node_format nodes[MAX_SENT_NODES];
1215 uint16_t num_relays = copy_connected_tcp_relays(onion_c->c, nodes, (MAX_SENT_NODES / 2)); 1213 uint16_t num_relays = copy_connected_tcp_relays(onion_c->c, nodes, (MAX_SENT_NODES / 2));
diff --git a/toxcore/util.c b/toxcore/util.c
index 73e16c45..3625f3d1 100644
--- a/toxcore/util.c
+++ b/toxcore/util.c
@@ -52,25 +52,6 @@ uint32_t id_copy(uint8_t *dest, const uint8_t *src)
52 return CRYPTO_PUBLIC_KEY_SIZE; 52 return CRYPTO_PUBLIC_KEY_SIZE;
53} 53}
54 54
55void host_to_net(uint8_t *num, uint16_t numbytes)
56{
57#ifndef WORDS_BIGENDIAN
58 uint32_t i;
59 VLA(uint8_t, buff, numbytes);
60
61 for (i = 0; i < numbytes; ++i) {
62 buff[i] = num[numbytes - i - 1];
63 }
64
65 memcpy(num, buff, numbytes);
66#endif
67}
68
69void net_to_host(uint8_t *num, uint16_t numbytes)
70{
71 host_to_net(num, numbytes);
72}
73
74int create_recursive_mutex(pthread_mutex_t *mutex) 55int create_recursive_mutex(pthread_mutex_t *mutex)
75{ 56{
76 pthread_mutexattr_t attr; 57 pthread_mutexattr_t attr;
diff --git a/toxcore/util.h b/toxcore/util.h
index 79f5deb5..2a0503e1 100644
--- a/toxcore/util.h
+++ b/toxcore/util.h
@@ -40,9 +40,6 @@ extern "C" {
40bool id_equal(const uint8_t *dest, const uint8_t *src); 40bool id_equal(const uint8_t *dest, const uint8_t *src);
41uint32_t id_copy(uint8_t *dest, const uint8_t *src); /* return value is CLIENT_ID_SIZE */ 41uint32_t id_copy(uint8_t *dest, const uint8_t *src); /* return value is CLIENT_ID_SIZE */
42 42
43void host_to_net(uint8_t *num, uint16_t numbytes);
44void net_to_host(uint8_t *num, uint16_t numbytes);
45
46/* Returns -1 if failed or 0 if success */ 43/* Returns -1 if failed or 0 if success */
47int create_recursive_mutex(pthread_mutex_t *mutex); 44int create_recursive_mutex(pthread_mutex_t *mutex);
48 45