Add checksum calculation and verification
This commit is contained in:
parent
de3da7a6e4
commit
da750868b5
1 changed files with 23 additions and 0 deletions
|
@ -133,6 +133,12 @@ static struct timespec now;
|
|||
static ffvisd_announce_t *announcements = NULL;
|
||||
|
||||
|
||||
/* ones' complement add */
|
||||
static inline u_int16_t add16(u_int16_t a, u_int16_t b) {
|
||||
a += b;
|
||||
return a + (a < b);
|
||||
}
|
||||
|
||||
static inline bool is_eth_addr_unspec(const eth_addr_t *address) {
|
||||
const uint8_t *a = address->address;
|
||||
|
||||
|
@ -447,6 +453,16 @@ static void maintenance() {
|
|||
}
|
||||
}
|
||||
|
||||
static uint16_t do_checksum(void *data, unsigned n_words) {
|
||||
uint16_t ret = 0;
|
||||
uint16_t *words = data;
|
||||
|
||||
while(n_words)
|
||||
ret = add16(ret, words[--n_words]);
|
||||
|
||||
return ~ret;
|
||||
}
|
||||
|
||||
static void send_announcement(const char *ifname, unsigned ifindex, void *arg) {
|
||||
char *if_mesh = iface_get_mesh(ifname);
|
||||
|
||||
|
@ -488,6 +504,8 @@ static void send_announcements() {
|
|||
|
||||
packet->n_servers = htons(n_servers);
|
||||
|
||||
packet->csum = do_checksum(packet, (sizeof(ffvisd_packet_announce_t) + n_servers*sizeof(ffvisd_packet_server_announce_t))/2);
|
||||
|
||||
ffvisd_iface_foreach(send_announcement, packet);
|
||||
}
|
||||
|
||||
|
@ -513,6 +531,11 @@ static void receive_announcement() {
|
|||
return;
|
||||
}
|
||||
|
||||
if (do_checksum(packet, (sizeof(ffvisd_packet_announce_t) + n_servers*sizeof(ffvisd_packet_server_announce_t))/2)) {
|
||||
fprintf(stderr, "warning: invalid checksum");
|
||||
return;
|
||||
}
|
||||
|
||||
puts("Received announcements:");
|
||||
int i;
|
||||
for(i = 0; i < n_servers; i++)
|
||||
|
|
Reference in a new issue