diff options
Diffstat (limited to 'core')
-rw-r--r-- | core/Messenger.c | 28 | ||||
-rw-r--r-- | core/Messenger.h | 5 |
2 files changed, 31 insertions, 2 deletions
diff --git a/core/Messenger.c b/core/Messenger.c index 2cc5c7a3..72f59036 100644 --- a/core/Messenger.c +++ b/core/Messenger.c | |||
@@ -75,6 +75,21 @@ int getclient_id(Messenger *m, int friend_id, uint8_t *client_id) | |||
75 | 75 | ||
76 | return -1; | 76 | return -1; |
77 | } | 77 | } |
78 | /* | ||
79 | * returns a uint16_t that represents the checksum of address of length len | ||
80 | * | ||
81 | * TODO: Another checksum algorithm might be better. | ||
82 | */ | ||
83 | static uint16_t address_checksum(uint8_t *address, uint32_t len) | ||
84 | { | ||
85 | uint8_t checksum[2] = {0}; | ||
86 | uint16_t check; | ||
87 | uint32_t i; | ||
88 | for(i = 0; i < len; ++i) | ||
89 | checksum[i % 2] ^= address[i]; | ||
90 | memcpy(&check, checksum, sizeof(check)); | ||
91 | return check; | ||
92 | } | ||
78 | 93 | ||
79 | /* | 94 | /* |
80 | * returns a FRIEND_ADDRESS_SIZE byte address to give to others. | 95 | * returns a FRIEND_ADDRESS_SIZE byte address to give to others. |
@@ -88,6 +103,8 @@ void getaddress(Messenger *m, uint8_t *address) | |||
88 | memcpy(address, self_public_key, crypto_box_PUBLICKEYBYTES); | 103 | memcpy(address, self_public_key, crypto_box_PUBLICKEYBYTES); |
89 | uint32_t nospam = get_nospam(); | 104 | uint32_t nospam = get_nospam(); |
90 | memcpy(address + crypto_box_PUBLICKEYBYTES, &nospam, sizeof(nospam)); | 105 | memcpy(address + crypto_box_PUBLICKEYBYTES, &nospam, sizeof(nospam)); |
106 | uint16_t checksum = address_checksum(address, FRIEND_ADDRESS_SIZE - sizeof(checksum)); | ||
107 | memcpy(address + crypto_box_PUBLICKEYBYTES + sizeof(nospam), &checksum, sizeof(checksum)); | ||
91 | } | 108 | } |
92 | 109 | ||
93 | /* | 110 | /* |
@@ -101,6 +118,9 @@ void getaddress(Messenger *m, uint8_t *address) | |||
101 | * return FAERR_OWNKEY if user's own key | 118 | * return FAERR_OWNKEY if user's own key |
102 | * return FAERR_ALREADYSENT if friend request already sent or already a friend | 119 | * return FAERR_ALREADYSENT if friend request already sent or already a friend |
103 | * return FAERR_UNKNOWN for unknown error | 120 | * return FAERR_UNKNOWN for unknown error |
121 | * return FAERR_BADCHECKSUM if bad checksum in address | ||
122 | * return FAERR_SETNEWNOSPAM if the friend was already there but the nospam was different | ||
123 | * (the nospam for that friend was set to the new one) | ||
104 | */ | 124 | */ |
105 | int m_addfriend(Messenger *m, uint8_t *address, uint8_t *data, uint16_t length) | 125 | int m_addfriend(Messenger *m, uint8_t *address, uint8_t *data, uint16_t length) |
106 | { | 126 | { |
@@ -110,6 +130,10 @@ int m_addfriend(Messenger *m, uint8_t *address, uint8_t *data, uint16_t length) | |||
110 | return FAERR_TOOLONG; | 130 | return FAERR_TOOLONG; |
111 | uint8_t client_id[crypto_box_PUBLICKEYBYTES]; | 131 | uint8_t client_id[crypto_box_PUBLICKEYBYTES]; |
112 | memcpy(client_id, address, crypto_box_PUBLICKEYBYTES); | 132 | memcpy(client_id, address, crypto_box_PUBLICKEYBYTES); |
133 | uint16_t check, checksum = address_checksum(address, FRIEND_ADDRESS_SIZE - sizeof(checksum)); | ||
134 | memcpy(&check, address + crypto_box_PUBLICKEYBYTES + sizeof(uint32_t), sizeof(check)); | ||
135 | if (check != checksum) | ||
136 | return FAERR_BADCHECKSUM; | ||
113 | if (length < 1) | 137 | if (length < 1) |
114 | return FAERR_NOMESSAGE; | 138 | return FAERR_NOMESSAGE; |
115 | if (memcmp(client_id, self_public_key, crypto_box_PUBLICKEYBYTES) == 0) | 139 | if (memcmp(client_id, self_public_key, crypto_box_PUBLICKEYBYTES) == 0) |
@@ -117,11 +141,11 @@ int m_addfriend(Messenger *m, uint8_t *address, uint8_t *data, uint16_t length) | |||
117 | int friend_id = getfriend_id(m, client_id); | 141 | int friend_id = getfriend_id(m, client_id); |
118 | if (friend_id != -1) { | 142 | if (friend_id != -1) { |
119 | uint32_t nospam; | 143 | uint32_t nospam; |
120 | memcpy(&nospam, address + crypto_box_PUBLICKEYBYTES, sizeof(uint32_t)); | 144 | memcpy(&nospam, address + crypto_box_PUBLICKEYBYTES, sizeof(nospam)); |
121 | if(m->friendlist[friend_id].friendrequest_nospam == nospam) | 145 | if(m->friendlist[friend_id].friendrequest_nospam == nospam) |
122 | return FAERR_ALREADYSENT; | 146 | return FAERR_ALREADYSENT; |
123 | m->friendlist[friend_id].friendrequest_nospam = nospam; | 147 | m->friendlist[friend_id].friendrequest_nospam = nospam; |
124 | return FAERR_ALREADYSENT; /*TODO: decide what to return when updating the nospam of a friend*/ | 148 | return FAERR_SETNEWNOSPAM; |
125 | } | 149 | } |
126 | 150 | ||
127 | /* resize the friend list if necessary */ | 151 | /* resize the friend list if necessary */ |
diff --git a/core/Messenger.h b/core/Messenger.h index 4e1010f5..51f4eb14 100644 --- a/core/Messenger.h +++ b/core/Messenger.h | |||
@@ -61,6 +61,8 @@ extern "C" { | |||
61 | #define FAERR_OWNKEY -3 | 61 | #define FAERR_OWNKEY -3 |
62 | #define FAERR_ALREADYSENT -4 | 62 | #define FAERR_ALREADYSENT -4 |
63 | #define FAERR_UNKNOWN -5 | 63 | #define FAERR_UNKNOWN -5 |
64 | #define FAERR_BADCHECKSUM -6 | ||
65 | #define FAERR_SETNEWNOSPAM -7 | ||
64 | 66 | ||
65 | /* don't assume MAX_STATUSMESSAGE_LENGTH will stay at 128, it may be increased | 67 | /* don't assume MAX_STATUSMESSAGE_LENGTH will stay at 128, it may be increased |
66 | to an absurdly large number later */ | 68 | to an absurdly large number later */ |
@@ -155,6 +157,9 @@ void getaddress(Messenger *m, uint8_t *address); | |||
155 | * return -3 if user's own key | 157 | * return -3 if user's own key |
156 | * return -4 if friend request already sent or already a friend | 158 | * return -4 if friend request already sent or already a friend |
157 | * return -5 for unknown error | 159 | * return -5 for unknown error |
160 | * return -6 if bad checksum in address | ||
161 | * return -7 if the friend was already there but the nospam was different | ||
162 | * (the nospam for that friend was set to the new one) | ||
158 | */ | 163 | */ |
159 | int m_addfriend(Messenger *m, uint8_t *address, uint8_t *data, uint16_t length); | 164 | int m_addfriend(Messenger *m, uint8_t *address, uint8_t *data, uint16_t length); |
160 | 165 | ||