Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use net_pack instead of casting bytes to ints. #1381

Merged
merged 1 commit into from
Mar 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions toxav/bwcontroller.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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,
Expand All @@ -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);
}
2 changes: 1 addition & 1 deletion toxcore/LAN_discovery.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
iphydf marked this conversation as resolved.
Show resolved Hide resolved

if (count >= MAX_INTERFACES) {
break;
Expand Down
12 changes: 6 additions & 6 deletions toxcore/network.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down