summaryrefslogtreecommitdiff
path: root/toxav
diff options
context:
space:
mode:
authoriphydf <iphydf@users.noreply.github.com>2020-03-05 16:20:17 +0000
committeriphydf <iphydf@users.noreply.github.com>2020-03-06 11:05:52 +0000
commit7b3d2eda3f8b4bc221215875a3d813654e044a55 (patch)
treed105366207b29b926c6b526d9cb434d7246b3a37 /toxav
parent7923a619597cc9a4e62db65b0f8400176a78dfa3 (diff)
Use `net_pack` instead of casting bytes to ints.
The current code violates the C standard and causes crashes on strict alignment architectures.
Diffstat (limited to 'toxav')
-rw-r--r--toxav/bwcontroller.c26
1 files changed, 18 insertions, 8 deletions
diff --git a/toxav/bwcontroller.c b/toxav/bwcontroller.c
index 238a44c8..113a228e 100644
--- a/toxav/bwcontroller.c
+++ b/toxav/bwcontroller.c
@@ -146,12 +146,16 @@ void send_update(BWController *bwc)
146 (void *)bwc, bwc->cycle.recv, bwc->cycle.lost, 146 (void *)bwc, bwc->cycle.recv, bwc->cycle.lost,
147 (((double) bwc->cycle.lost / (bwc->cycle.recv + bwc->cycle.lost)) * 100.0)); 147 (((double) bwc->cycle.lost / (bwc->cycle.recv + bwc->cycle.lost)) * 100.0));
148 uint8_t bwc_packet[sizeof(struct BWCMessage) + 1]; 148 uint8_t bwc_packet[sizeof(struct BWCMessage) + 1];
149 struct BWCMessage *msg = (struct BWCMessage *)(bwc_packet + 1); 149 size_t offset = 0;
150 bwc_packet[0] = BWC_PACKET_ID; // set packet ID
151 msg->lost = net_htonl(bwc->cycle.lost);
152 msg->recv = net_htonl(bwc->cycle.recv);
153 150
154 if (-1 == m_send_custom_lossy_packet(bwc->m, bwc->friend_number, bwc_packet, sizeof(bwc_packet))) { 151 bwc_packet[offset] = BWC_PACKET_ID; // set packet ID
152 ++offset;
153
154 offset += net_pack_u32(bwc_packet + offset, bwc->cycle.lost);
155 offset += net_pack_u32(bwc_packet + offset, bwc->cycle.recv);
156 assert(offset == sizeof(bwc_packet));
157
158 if (m_send_custom_lossy_packet(bwc->m, bwc->friend_number, bwc_packet, sizeof(bwc_packet)) == -1) {
155 const char *netstrerror = net_new_strerror(net_error()); 159 const char *netstrerror = net_new_strerror(net_error());
156 LOGGER_WARNING(bwc->m->log, "BWC send failed (len: %u)! std error: %s, net error %s", 160 LOGGER_WARNING(bwc->m->log, "BWC send failed (len: %u)! std error: %s, net error %s",
157 (unsigned)sizeof(bwc_packet), strerror(errno), netstrerror); 161 (unsigned)sizeof(bwc_packet), strerror(errno), netstrerror);
@@ -177,8 +181,8 @@ static int on_update(BWController *bwc, const struct BWCMessage *msg)
177 181
178 bwc->cycle.last_recv_timestamp = current_time_monotonic(bwc->m->mono_time); 182 bwc->cycle.last_recv_timestamp = current_time_monotonic(bwc->m->mono_time);
179 183
180 uint32_t recv = net_ntohl(msg->recv); 184 const uint32_t recv = msg->recv;
181 uint32_t lost = net_ntohl(msg->lost); 185 const uint32_t lost = msg->lost;
182 186
183 if (lost && bwc->mcb) { 187 if (lost && bwc->mcb) {
184 LOGGER_DEBUG(bwc->m->log, "recved: %u lost: %u percentage: %f %%", recv, lost, 188 LOGGER_DEBUG(bwc->m->log, "recved: %u lost: %u percentage: %f %%", recv, lost,
@@ -197,5 +201,11 @@ int bwc_handle_data(Messenger *m, uint32_t friendnumber, const uint8_t *data, ui
197 return -1; 201 return -1;
198 } 202 }
199 203
200 return on_update((BWController *)object, (const struct BWCMessage *)(data + 1)); 204 size_t offset = 1; // Ignore packet id.
205 struct BWCMessage msg;
206 offset += net_unpack_u32(data + offset, &msg.lost);
207 offset += net_unpack_u32(data + offset, &msg.recv);
208 assert(offset == length);
209
210 return on_update((BWController *)object, &msg);
201} 211}