Fixed spam problem. (I broke the API so this will not build)

The friend address is what the byte string that you give away for people
to add you will be called.

1. Every friend address now contains a number set by the friend. This is
to prevent someone from randomly spamming people in the DHT with friend
requests and makes it so you need the person to actually give you the
address in some way to send the friend request. This number is expected
to be encrypted with the friend request. All requests that do not
contain this number will be rejected. This means the spammer can no
longer use the DHT to collect lists of valid addresses to spam. It also
enables users to quickly change the number in case a spammer gets hold
of the address and starts spamming it.

2. A 2 byte checksum will be added (not implemented yet) to prevent
people from accidentally adding random strings as friends. (NOTE that
this has nothing to do with the spam problem I just decided to add a
placeholder for it now.)
This commit is contained in:
irungentoo 2013-08-13 09:32:31 -04:00
parent 8fe1dec5d6
commit 97f449a2f1
6 changed files with 82 additions and 20 deletions

View File

@ -76,10 +76,24 @@ int getclient_id(Messenger *m, int friend_id, uint8_t *client_id)
return -1;
}
/*
* returns a FRIEND_ADDRESS_SIZE byte address to give to others.
* format: [client_id (32 bytes)][nospam number (4 bytes)][checksum (2 bytes)]
*
* TODO: add checksum.
*/
void getaddress(Messenger *m, uint8_t *address)
{
//memcpy(address, m->public_key, crypto_box_PUBLICKEYBYTES); //TODO
memcpy(address, self_public_key, crypto_box_PUBLICKEYBYTES);
uint32_t nospam = get_nospam();
memcpy(address + crypto_box_PUBLICKEYBYTES, &nospam, sizeof(nospam));
}
/*
* add a friend
* set the data that will be sent along with friend request
* client_id is the client id of the friend
* address is the address of the friend (returned by getaddress) it must be FRIEND_ADDRESS_SIZE bytes. TODO: add checksum.
* data is the data and length is the length
* returns the friend number if success
* return FA_TOOLONG if message length is too long
@ -88,12 +102,14 @@ int getclient_id(Messenger *m, int friend_id, uint8_t *client_id)
* return FAERR_ALREADYSENT if friend request already sent or already a friend
* return FAERR_UNKNOWN for unknown error
*/
int m_addfriend(Messenger *m, uint8_t *client_id, uint8_t *data, uint16_t length)
int m_addfriend(Messenger *m, uint8_t *address, uint8_t *data, uint16_t length)
{
if (length >= (MAX_DATA_SIZE - crypto_box_PUBLICKEYBYTES
- crypto_box_NONCEBYTES - crypto_box_BOXZEROBYTES
+ crypto_box_ZEROBYTES))
return FAERR_TOOLONG;
uint8_t client_id[crypto_box_PUBLICKEYBYTES];
memcpy(client_id, address, crypto_box_PUBLICKEYBYTES);
if (length < 1)
return FAERR_NOMESSAGE;
if (memcmp(client_id, self_public_key, crypto_box_PUBLICKEYBYTES) == 0)
@ -119,6 +135,7 @@ int m_addfriend(Messenger *m, uint8_t *client_id, uint8_t *data, uint16_t length
m->friendlist[i].info_size = length;
m->friendlist[i].message_id = 0;
m->friendlist[i].receives_read_receipts = 1; /* default: YES */
memcpy(&(m->friendlist[i].friendrequest_nospam), address + crypto_box_PUBLICKEYBYTES, sizeof(uint32_t));
++ m->numfriends;
return i;
@ -524,6 +541,7 @@ Messenger * initMessenger(void)
LosslessUDP_init();
friendreq_init();
LANdiscovery_init();
set_nospam(random_int());
timer_single(&LANdiscovery, 0, LAN_DISCOVERY_INTERVAL);
@ -545,7 +563,7 @@ void doFriends(Messenger *m)
uint8_t temp[MAX_DATA_SIZE];
for (i = 0; i < m->numfriends; ++i) {
if (m->friendlist[i].status == FRIEND_ADDED) {
int fr = send_friendrequest(m->friendlist[i].client_id, m->friendlist[i].info, m->friendlist[i].info_size);
int fr = send_friendrequest(m->friendlist[i].client_id, m->friendlist[i].friendrequest_nospam, m->friendlist[i].info, m->friendlist[i].info_size);
if (fr == 0) /* TODO: This needs to be fixed so that it sends the friend requests a couple of times in case of packet loss */
set_friend_status(m, i, FRIEND_REQUESTED);
else if (fr > 0)
@ -554,7 +572,7 @@ void doFriends(Messenger *m)
if (m->friendlist[i].status == FRIEND_REQUESTED || m->friendlist[i].status == FRIEND_CONFIRMED) { /* friend is not online */
if (m->friendlist[i].status == FRIEND_REQUESTED) {
if (m->friendlist[i].friend_request_id + 10 < unix_time()) { /*I know this is hackish but it should work.*/
send_friendrequest(m->friendlist[i].client_id, m->friendlist[i].info, m->friendlist[i].info_size);
send_friendrequest(m->friendlist[i].client_id, m->friendlist[i].friendrequest_nospam, m->friendlist[i].info, m->friendlist[i].info_size);
m->friendlist[i].friend_request_id = unix_time();
}
}

View File

@ -38,6 +38,8 @@ extern "C" {
#define MAX_NAME_LENGTH 128
#define MAX_STATUSMESSAGE_LENGTH 128
#define FRIEND_ADDRESS_SIZE (crypto_box_PUBLICKEYBYTES + sizeof(uint32_t) + sizeof(uint16_t))
#define PACKET_ID_NICKNAME 48
#define PACKET_ID_STATUSMESSAGE 49
#define PACKET_ID_USERSTATUS 50
@ -89,6 +91,7 @@ typedef struct {
uint16_t info_size; /* length of the info */
uint32_t message_id; /* a semi-unique id used in read receipts */
uint8_t receives_read_receipts; /* shall we send read receipts to this person? */
uint32_t friendrequest_nospam; /*The nospam number used in the friend request*/
} Friend;
typedef struct Messenger {
@ -133,10 +136,18 @@ typedef struct Messenger {
} Messenger;
/*
* returns a FRIEND_ADDRESS_SIZE byte address to give to others.
* format: [client_id (32 bytes)][nospam number (4 bytes)][checksum (2 bytes)]
*
* TODO: add checksum.
*/
void getaddress(Messenger *m, uint8_t *address);
/*
* add a friend
* set the data that will be sent along with friend request
* client_id is the client id of the friend
* address is the address of the friend (returned by getaddress) it must be FRIEND_ADDRESS_SIZE bytes. TODO: add checksum.
* data is the data and length is the length
* returns the friend number if success
* return -1 if message length is too long
@ -145,7 +156,7 @@ typedef struct Messenger {
* return -4 if friend request already sent or already a friend
* return -5 for unknown error
*/
int m_addfriend(Messenger *m, uint8_t *client_id, uint8_t *data, uint16_t length);
int m_addfriend(Messenger *m, uint8_t *address, uint8_t *data, uint16_t length);
/* add a friend without sending a friendrequest.

View File

@ -25,15 +25,22 @@
uint8_t self_public_key[crypto_box_PUBLICKEYBYTES];
/* Try to send a friendrequest to peer with public_key
data is the data in the request and length is the length.
return -1 if failure.
return 0 if it sent the friend request directly to the friend.
return the number of peers it was routed through if it did not send it directly.*/
int send_friendrequest(uint8_t * public_key, uint8_t * data, uint32_t length)
int send_friendrequest(uint8_t * public_key, uint32_t nospam_num, uint8_t * data, uint32_t length)
{
if(length - sizeof(nospam_num) > MAX_DATA_SIZE)
return -1;
uint8_t temp[MAX_DATA_SIZE];
memcpy(temp, &nospam_num, sizeof(nospam_num));
memcpy(temp + sizeof(nospam_num), data, length);
uint8_t packet[MAX_DATA_SIZE];
int len = create_request(packet, public_key, data, length, 32); /* 32 is friend request packet id */
int len = create_request(packet, public_key, temp, length + sizeof(nospam_num), 32); /* 32 is friend request packet id */
if (len == -1)
return -1;
@ -57,6 +64,20 @@ int send_friendrequest(uint8_t * public_key, uint8_t * data, uint32_t length)
return num;
}
static uint32_t nospam;
/*
* Set and get the nospam variable used to prevent one type of friend request spam
*/
void set_nospam(uint32_t num)
{
nospam = num;
}
uint32_t get_nospam()
{
return nospam;
}
static void (*handle_friendrequest)(uint8_t *, uint8_t *, uint16_t, void*);
static uint8_t handle_friendrequest_isset = 0;
static void* handle_friendrequest_userdata;
@ -115,14 +136,17 @@ static int friendreq_handlepacket(IP_Port source, uint8_t * packet, uint32_t len
uint8_t public_key[crypto_box_PUBLICKEYBYTES];
uint8_t data[MAX_DATA_SIZE];
int len = handle_request(public_key, data, packet, length);
if (len == -1)
return 1;
if (len <= sizeof(nospam))
return 1;
if (request_received(public_key))
return 1;
if (memcmp(data, &nospam, sizeof(nospam)) != 0)
return 1;
addto_receivedlist(public_key);
(*handle_friendrequest)(public_key, data, len, handle_friendrequest_userdata);
(*handle_friendrequest)(public_key, data + 4, len - 4, handle_friendrequest_userdata);
} else { /* if request is not for us, try routing it. */
if(route_packet(packet + 1, packet, length) == length)
return 0;

View File

@ -33,7 +33,12 @@ extern "C" {
/* Try to send a friendrequest to peer with public_key
data is the data in the request and length is the length. */
int send_friendrequest(uint8_t *public_key, uint8_t *data, uint32_t length);
int send_friendrequest(uint8_t * public_key, uint32_t nospam_num, uint8_t * data, uint32_t length);
/*
* Set and get the nospam variable used to prevent one type of friend request spam
*/
void set_nospam(uint32_t num);
uint32_t get_nospam();
/* set the function that will be executed when a friend request for us is received.
function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) */

View File

@ -307,11 +307,13 @@ void execute(ToxWindow *self, ChatContext *ctx, Messenger *m, char *cmd)
}
else if (!strcmp(cmd, "/myid")) {
char id[KEY_SIZE_BYTES*2+1] = {0};
char id[FRIEND_ADDRESS_SIZE*2+1] = {0};
int i;
for (i = 0; i < KEY_SIZE_BYTES; i++) {
uint8_t address[FRIEND_ADDRESS_SIZE];
getaddress(m, address);
for (i = 0; i < FRIEND_ADDRESS_SIZE; i++) {
char xx[3];
snprintf(xx, sizeof(xx), "%02x", self_public_key[i] & 0xff);
snprintf(xx, sizeof(xx), "%02X", address[i] & 0xff);
strcat(id, xx);
}
wprintw(ctx->history, "Your ID: %s\n", id);

View File

@ -93,7 +93,7 @@ void cmd_accept(ToxWindow *self, Messenger *m, char **args)
void cmd_add(ToxWindow *self, Messenger *m, char **args)
{
uint8_t id_bin[KEY_SIZE_BYTES];
uint8_t id_bin[FRIEND_ADDRESS_SIZE];
char xx[3];
uint32_t x;
char *id = args[1];
@ -106,12 +106,12 @@ void cmd_add(ToxWindow *self, Messenger *m, char **args)
if (!msg)
msg = "";
if (strlen(id) != 2*KEY_SIZE_BYTES) {
if (strlen(id) != 2*FRIEND_ADDRESS_SIZE) {
wprintw(self->window, "Invalid ID length.\n");
return;
}
int i;
for (i = 0; i < KEY_SIZE_BYTES; ++i) {
for (i = 0; i < FRIEND_ADDRESS_SIZE; ++i) {
xx[0] = id[2*i];
xx[1] = id[2*i+1];
xx[2] = '\0';
@ -217,11 +217,13 @@ void cmd_msg(ToxWindow *self, Messenger *m, char **args)
void cmd_myid(ToxWindow *self, Messenger *m, char **args)
{
char id[KEY_SIZE_BYTES*2 + 1] = {0};
char id[FRIEND_ADDRESS_SIZE*2 + 1] = {0};
size_t i;
for (i = 0; i < KEY_SIZE_BYTES; ++i) {
uint8_t address[FRIEND_ADDRESS_SIZE];
getaddress(m, address);
for (i = 0; i < FRIEND_ADDRESS_SIZE; ++i) {
char xx[3];
snprintf(xx, sizeof(xx), "%02x", self_public_key[i] & 0xff);
snprintf(xx, sizeof(xx), "%02X", address[i] & 0xff);
strcat(id, xx);
}
wprintw(self->window, "Your ID: %s\n", id);