mirror of
https://github.com/irungentoo/toxcore.git
synced 2024-03-22 13:30:51 +08:00
Use net_pack
instead of casting bytes to ints.
The current code violates the C standard and causes crashes on strict alignment architectures.
This commit is contained in:
parent
7923a61959
commit
7b3d2eda3f
|
@ -146,12 +146,16 @@ void send_update(BWController *bwc)
|
|||
(void *)bwc, bwc->cycle.recv, bwc->cycle.lost,
|
||||
(((double) bwc->cycle.lost / (bwc->cycle.recv + bwc->cycle.lost)) * 100.0));
|
||||
uint8_t bwc_packet[sizeof(struct BWCMessage) + 1];
|
||||
struct BWCMessage *msg = (struct BWCMessage *)(bwc_packet + 1);
|
||||
bwc_packet[0] = BWC_PACKET_ID; // set packet ID
|
||||
msg->lost = net_htonl(bwc->cycle.lost);
|
||||
msg->recv = net_htonl(bwc->cycle.recv);
|
||||
size_t offset = 0;
|
||||
|
||||
if (-1 == m_send_custom_lossy_packet(bwc->m, bwc->friend_number, bwc_packet, sizeof(bwc_packet))) {
|
||||
bwc_packet[offset] = BWC_PACKET_ID; // set packet ID
|
||||
++offset;
|
||||
|
||||
offset += net_pack_u32(bwc_packet + offset, bwc->cycle.lost);
|
||||
offset += net_pack_u32(bwc_packet + offset, bwc->cycle.recv);
|
||||
assert(offset == sizeof(bwc_packet));
|
||||
|
||||
if (m_send_custom_lossy_packet(bwc->m, bwc->friend_number, bwc_packet, sizeof(bwc_packet)) == -1) {
|
||||
const char *netstrerror = net_new_strerror(net_error());
|
||||
LOGGER_WARNING(bwc->m->log, "BWC send failed (len: %u)! std error: %s, net error %s",
|
||||
(unsigned)sizeof(bwc_packet), strerror(errno), netstrerror);
|
||||
|
@ -177,8 +181,8 @@ static int on_update(BWController *bwc, const struct BWCMessage *msg)
|
|||
|
||||
bwc->cycle.last_recv_timestamp = current_time_monotonic(bwc->m->mono_time);
|
||||
|
||||
uint32_t recv = net_ntohl(msg->recv);
|
||||
uint32_t lost = net_ntohl(msg->lost);
|
||||
const uint32_t recv = msg->recv;
|
||||
const uint32_t lost = msg->lost;
|
||||
|
||||
if (lost && bwc->mcb) {
|
||||
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
|
|||
return -1;
|
||||
}
|
||||
|
||||
return on_update((BWController *)object, (const struct BWCMessage *)(data + 1));
|
||||
size_t offset = 1; // Ignore packet id.
|
||||
struct BWCMessage msg;
|
||||
offset += net_unpack_u32(data + offset, &msg.lost);
|
||||
offset += net_unpack_u32(data + offset, &msg.recv);
|
||||
assert(offset == length);
|
||||
|
||||
return on_update((BWController *)object, &msg);
|
||||
}
|
||||
|
|
|
@ -186,7 +186,7 @@ static void fetch_broadcast_info(uint16_t port)
|
|||
continue;
|
||||
}
|
||||
|
||||
struct sockaddr_in *sock4 = (struct sockaddr_in *)&i_faces[i].ifr_broadaddr;
|
||||
struct sockaddr_in *sock4 = (struct sockaddr_in *)(void *)&i_faces[i].ifr_broadaddr;
|
||||
|
||||
if (count >= MAX_INTERFACES) {
|
||||
break;
|
||||
|
|
|
@ -1207,12 +1207,12 @@ int addr_resolve(const char *address, IP *to, IP *extra)
|
|||
switch (walker->ai_family) {
|
||||
case AF_INET:
|
||||
if (walker->ai_family == family) { /* AF_INET requested, done */
|
||||
struct sockaddr_in *addr = (struct sockaddr_in *)walker->ai_addr;
|
||||
struct sockaddr_in *addr = (struct sockaddr_in *)(void *)walker->ai_addr;
|
||||
get_ip4(&to->ip.v4, &addr->sin_addr);
|
||||
result = TOX_ADDR_RESOLVE_INET;
|
||||
done = 1;
|
||||
} else if (!(result & TOX_ADDR_RESOLVE_INET)) { /* AF_UNSPEC requested, store away */
|
||||
struct sockaddr_in *addr = (struct sockaddr_in *)walker->ai_addr;
|
||||
struct sockaddr_in *addr = (struct sockaddr_in *)(void *)walker->ai_addr;
|
||||
get_ip4(&ip4.ip.v4, &addr->sin_addr);
|
||||
result |= TOX_ADDR_RESOLVE_INET;
|
||||
}
|
||||
|
@ -1222,14 +1222,14 @@ int addr_resolve(const char *address, IP *to, IP *extra)
|
|||
case AF_INET6:
|
||||
if (walker->ai_family == family) { /* AF_INET6 requested, done */
|
||||
if (walker->ai_addrlen == sizeof(struct sockaddr_in6)) {
|
||||
struct sockaddr_in6 *addr = (struct sockaddr_in6 *)walker->ai_addr;
|
||||
struct sockaddr_in6 *addr = (struct sockaddr_in6 *)(void *)walker->ai_addr;
|
||||
get_ip6(&to->ip.v6, &addr->sin6_addr);
|
||||
result = TOX_ADDR_RESOLVE_INET6;
|
||||
done = 1;
|
||||
}
|
||||
} else if (!(result & TOX_ADDR_RESOLVE_INET6)) { /* AF_UNSPEC requested, store away */
|
||||
if (walker->ai_addrlen == sizeof(struct sockaddr_in6)) {
|
||||
struct sockaddr_in6 *addr = (struct sockaddr_in6 *)walker->ai_addr;
|
||||
struct sockaddr_in6 *addr = (struct sockaddr_in6 *)(void *)walker->ai_addr;
|
||||
get_ip6(&ip6.ip.v6, &addr->sin6_addr);
|
||||
result |= TOX_ADDR_RESOLVE_INET6;
|
||||
}
|
||||
|
@ -1344,10 +1344,10 @@ int32_t net_getipport(const char *node, IP_Port **res, int tox_type)
|
|||
}
|
||||
|
||||
if (cur->ai_family == AF_INET) {
|
||||
struct sockaddr_in *addr = (struct sockaddr_in *)cur->ai_addr;
|
||||
struct sockaddr_in *addr = (struct sockaddr_in *)(void *)cur->ai_addr;
|
||||
memcpy(&ip_port->ip.ip.v4, &addr->sin_addr, sizeof(IP4));
|
||||
} else if (cur->ai_family == AF_INET6) {
|
||||
struct sockaddr_in6 *addr = (struct sockaddr_in6 *)cur->ai_addr;
|
||||
struct sockaddr_in6 *addr = (struct sockaddr_in6 *)(void *)cur->ai_addr;
|
||||
memcpy(&ip_port->ip.ip.v6, &addr->sin6_addr, sizeof(IP6));
|
||||
} else {
|
||||
continue;
|
||||
|
|
Loading…
Reference in New Issue
Block a user