Randomize message ID start
This commit is contained in:
parent
43540d51bc
commit
541dd28a64
36
ethermess.c
36
ethermess.c
|
@ -3,6 +3,7 @@
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <err.h>
|
#include <err.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
#include <fcntl.h>
|
||||||
#include <inttypes.h>
|
#include <inttypes.h>
|
||||||
#include <linux/if_packet.h>
|
#include <linux/if_packet.h>
|
||||||
#include <net/ethernet.h>
|
#include <net/ethernet.h>
|
||||||
|
@ -16,6 +17,7 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <sys/ioctl.h>
|
#include <sys/ioctl.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
|
@ -33,6 +35,7 @@
|
||||||
bool running = true;
|
bool running = true;
|
||||||
|
|
||||||
int packet_socket;
|
int packet_socket;
|
||||||
|
int urandom;
|
||||||
|
|
||||||
unsigned char own_mac[6];
|
unsigned char own_mac[6];
|
||||||
unsigned char broadcast_mac[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
|
unsigned char broadcast_mac[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
|
||||||
|
@ -75,8 +78,11 @@ ssize_t msgid_cache_add(const unsigned char mac[6]) {
|
||||||
|
|
||||||
// If we are adding into a new slot (instead of overwriting one),
|
// If we are adding into a new slot (instead of overwriting one),
|
||||||
// expand the fill pointer
|
// expand the fill pointer
|
||||||
if (msgid_cache_fill < index) {
|
//
|
||||||
msgid_cache_fill++;
|
// + 1 because msgid_cache_fill of N means that cache slots [0, N-1]
|
||||||
|
// are in use
|
||||||
|
if (msgid_cache_fill < index + 1) {
|
||||||
|
msgid_cache_fill = index + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
memcpy(msgid_cache[index].other_mac, mac, sizeof(msgid_cache[index].other_mac));
|
memcpy(msgid_cache[index].other_mac, mac, sizeof(msgid_cache[index].other_mac));
|
||||||
|
@ -86,6 +92,15 @@ ssize_t msgid_cache_add(const unsigned char mac[6]) {
|
||||||
return index;
|
return index;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
unsigned char random_byte(void) {
|
||||||
|
unsigned char randomness;
|
||||||
|
errno = 0;
|
||||||
|
if (read(urandom, &randomness, 1) != 1) {
|
||||||
|
err(1, "read");
|
||||||
|
}
|
||||||
|
return randomness;
|
||||||
|
}
|
||||||
|
|
||||||
char hexify(int nybble) {
|
char hexify(int nybble) {
|
||||||
assert(0 <= nybble && nybble <= 16);
|
assert(0 <= nybble && nybble <= 16);
|
||||||
return "0123456789abcdef"[nybble];
|
return "0123456789abcdef"[nybble];
|
||||||
|
@ -199,9 +214,8 @@ void send_msgid(const unsigned char destination[6]) {
|
||||||
|
|
||||||
if (!msgid_cache[cache_index].know_receive) {
|
if (!msgid_cache[cache_index].know_receive) {
|
||||||
// We don't have receive ID stored in the cache
|
// We don't have receive ID stored in the cache
|
||||||
// In that case, start from 0
|
// In that case, start from a random index
|
||||||
// TODO: Make it random instead
|
msgid_cache[cache_index].next_receive = (random_byte() << 8) | random_byte();
|
||||||
msgid_cache[cache_index].next_receive = 0;
|
|
||||||
msgid_cache[cache_index].know_receive = true;
|
msgid_cache[cache_index].know_receive = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -215,6 +229,7 @@ void send_msgid(const unsigned char destination[6]) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void read_command(void) {
|
void read_command(void) {
|
||||||
|
errno = 0;
|
||||||
int cmd = getchar();
|
int cmd = getchar();
|
||||||
if (cmd == EOF) {
|
if (cmd == EOF) {
|
||||||
err(1, "getchar");
|
err(1, "getchar");
|
||||||
|
@ -356,7 +371,7 @@ void process_frame(void) {
|
||||||
errno = 0;
|
errno = 0;
|
||||||
ssize_t res = recv(packet_socket, frame, sizeof(frame), 0);
|
ssize_t res = recv(packet_socket, frame, sizeof(frame), 0);
|
||||||
if (res == -1) {
|
if (res == -1) {
|
||||||
errx(1, "recv");
|
err(1, "recv");
|
||||||
} else if (res < 16) {
|
} else if (res < 16) {
|
||||||
// Frame too short to contain enough information
|
// Frame too short to contain enough information
|
||||||
return;
|
return;
|
||||||
|
@ -480,6 +495,9 @@ int main(int argc, char **argv) {
|
||||||
|
|
||||||
const char *interface_name = argv[1];
|
const char *interface_name = argv[1];
|
||||||
|
|
||||||
|
// Open /dev/urandom for getting randomness
|
||||||
|
urandom = open("/dev/urandom", O_RDONLY);
|
||||||
|
|
||||||
// Create a packet socket
|
// Create a packet socket
|
||||||
errno = 0;
|
errno = 0;
|
||||||
packet_socket = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
|
packet_socket = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
|
||||||
|
@ -545,5 +563,11 @@ int main(int argc, char **argv) {
|
||||||
err(1, "fflush");
|
err(1, "fflush");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Close urandom
|
||||||
|
errno = 0;
|
||||||
|
if (close(urandom) == -1) {
|
||||||
|
err(1, "close");
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue