diff options
author | irungentoo <irungentoo@gmail.com> | 2013-10-28 16:01:29 -0400 |
---|---|---|
committer | irungentoo <irungentoo@gmail.com> | 2013-10-28 16:01:29 -0400 |
commit | 415835ce3d2daa9ecd4100e76d58d93b3afa2db0 (patch) | |
tree | 755d041ea06a9b1c05fc72687c9dd5c6e98bbe73 /toxcore/Messenger.c | |
parent | 988922bdeb94dd83e61ed387b29187ac4c9936e0 (diff) | |
parent | 2bdce934373d238778bc77a04198bd3f1481542f (diff) |
Merge branch 'master' into harden
Also removed some old, useless loading code.
Diffstat (limited to 'toxcore/Messenger.c')
-rw-r--r-- | toxcore/Messenger.c | 166 |
1 files changed, 15 insertions, 151 deletions
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index ff64a072..be3d82e0 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c | |||
@@ -92,7 +92,7 @@ int getfriend_id(Messenger *m, uint8_t *client_id) | |||
92 | 92 | ||
93 | for (i = 0; i < m->numfriends; ++i) { | 93 | for (i = 0; i < m->numfriends; ++i) { |
94 | if (m->friendlist[i].status > 0) | 94 | if (m->friendlist[i].status > 0) |
95 | if (memcmp(client_id, m->friendlist[i].client_id, crypto_box_PUBLICKEYBYTES) == 0) | 95 | if (id_equal(client_id, m->friendlist[i].client_id)) |
96 | return i; | 96 | return i; |
97 | } | 97 | } |
98 | 98 | ||
@@ -140,7 +140,7 @@ static uint16_t address_checksum(uint8_t *address, uint32_t len) | |||
140 | */ | 140 | */ |
141 | void getaddress(Messenger *m, uint8_t *address) | 141 | void getaddress(Messenger *m, uint8_t *address) |
142 | { | 142 | { |
143 | memcpy(address, m->net_crypto->self_public_key, crypto_box_PUBLICKEYBYTES); | 143 | id_copy(address, m->net_crypto->self_public_key); |
144 | uint32_t nospam = get_nospam(&(m->fr)); | 144 | uint32_t nospam = get_nospam(&(m->fr)); |
145 | memcpy(address + crypto_box_PUBLICKEYBYTES, &nospam, sizeof(nospam)); | 145 | memcpy(address + crypto_box_PUBLICKEYBYTES, &nospam, sizeof(nospam)); |
146 | uint16_t checksum = address_checksum(address, FRIEND_ADDRESS_SIZE - sizeof(checksum)); | 146 | uint16_t checksum = address_checksum(address, FRIEND_ADDRESS_SIZE - sizeof(checksum)); |
@@ -173,7 +173,7 @@ int m_addfriend(Messenger *m, uint8_t *address, uint8_t *data, uint16_t length) | |||
173 | return FAERR_TOOLONG; | 173 | return FAERR_TOOLONG; |
174 | 174 | ||
175 | uint8_t client_id[crypto_box_PUBLICKEYBYTES]; | 175 | uint8_t client_id[crypto_box_PUBLICKEYBYTES]; |
176 | memcpy(client_id, address, crypto_box_PUBLICKEYBYTES); | 176 | id_copy(client_id, address); |
177 | uint16_t check, checksum = address_checksum(address, FRIEND_ADDRESS_SIZE - sizeof(checksum)); | 177 | uint16_t check, checksum = address_checksum(address, FRIEND_ADDRESS_SIZE - sizeof(checksum)); |
178 | memcpy(&check, address + crypto_box_PUBLICKEYBYTES + sizeof(uint32_t), sizeof(check)); | 178 | memcpy(&check, address + crypto_box_PUBLICKEYBYTES + sizeof(uint32_t), sizeof(check)); |
179 | 179 | ||
@@ -183,7 +183,7 @@ int m_addfriend(Messenger *m, uint8_t *address, uint8_t *data, uint16_t length) | |||
183 | if (length < 1) | 183 | if (length < 1) |
184 | return FAERR_NOMESSAGE; | 184 | return FAERR_NOMESSAGE; |
185 | 185 | ||
186 | if (memcmp(client_id, m->net_crypto->self_public_key, crypto_box_PUBLICKEYBYTES) == 0) | 186 | if (id_equal(client_id, m->net_crypto->self_public_key)) |
187 | return FAERR_OWNKEY; | 187 | return FAERR_OWNKEY; |
188 | 188 | ||
189 | int friend_id = getfriend_id(m, client_id); | 189 | int friend_id = getfriend_id(m, client_id); |
@@ -214,7 +214,7 @@ int m_addfriend(Messenger *m, uint8_t *address, uint8_t *data, uint16_t length) | |||
214 | m->friendlist[i].crypt_connection_id = -1; | 214 | m->friendlist[i].crypt_connection_id = -1; |
215 | m->friendlist[i].friendrequest_lastsent = 0; | 215 | m->friendlist[i].friendrequest_lastsent = 0; |
216 | m->friendlist[i].friendrequest_timeout = FRIENDREQUEST_TIMEOUT; | 216 | m->friendlist[i].friendrequest_timeout = FRIENDREQUEST_TIMEOUT; |
217 | memcpy(m->friendlist[i].client_id, client_id, CLIENT_ID_SIZE); | 217 | id_copy(m->friendlist[i].client_id, client_id); |
218 | m->friendlist[i].statusmessage = calloc(1, 1); | 218 | m->friendlist[i].statusmessage = calloc(1, 1); |
219 | m->friendlist[i].statusmessage_length = 1; | 219 | m->friendlist[i].statusmessage_length = 1; |
220 | m->friendlist[i].userstatus = USERSTATUS_NONE; | 220 | m->friendlist[i].userstatus = USERSTATUS_NONE; |
@@ -243,7 +243,7 @@ int m_addfriend_norequest(Messenger *m, uint8_t *client_id) | |||
243 | if (realloc_friendlist(m, m->numfriends + 1) != 0) | 243 | if (realloc_friendlist(m, m->numfriends + 1) != 0) |
244 | return FAERR_NOMEM; | 244 | return FAERR_NOMEM; |
245 | 245 | ||
246 | if (memcmp(client_id, m->net_crypto->self_public_key, crypto_box_PUBLICKEYBYTES) == 0) | 246 | if (id_equal(client_id, m->net_crypto->self_public_key)) |
247 | return FAERR_OWNKEY; | 247 | return FAERR_OWNKEY; |
248 | 248 | ||
249 | memset(&(m->friendlist[m->numfriends]), 0, sizeof(Friend)); | 249 | memset(&(m->friendlist[m->numfriends]), 0, sizeof(Friend)); |
@@ -256,7 +256,7 @@ int m_addfriend_norequest(Messenger *m, uint8_t *client_id) | |||
256 | m->friendlist[i].status = FRIEND_CONFIRMED; | 256 | m->friendlist[i].status = FRIEND_CONFIRMED; |
257 | m->friendlist[i].crypt_connection_id = -1; | 257 | m->friendlist[i].crypt_connection_id = -1; |
258 | m->friendlist[i].friendrequest_lastsent = 0; | 258 | m->friendlist[i].friendrequest_lastsent = 0; |
259 | memcpy(m->friendlist[i].client_id, client_id, CLIENT_ID_SIZE); | 259 | id_copy(m->friendlist[i].client_id, client_id); |
260 | m->friendlist[i].statusmessage = calloc(1, 1); | 260 | m->friendlist[i].statusmessage = calloc(1, 1); |
261 | m->friendlist[i].statusmessage_length = 1; | 261 | m->friendlist[i].statusmessage_length = 1; |
262 | m->friendlist[i].userstatus = USERSTATUS_NONE; | 262 | m->friendlist[i].userstatus = USERSTATUS_NONE; |
@@ -728,7 +728,7 @@ static int group_num(Messenger *m, uint8_t *group_public_key) | |||
728 | uint32_t i; | 728 | uint32_t i; |
729 | 729 | ||
730 | for (i = 0; i < m->numchats; ++i) { | 730 | for (i = 0; i < m->numchats; ++i) { |
731 | if (memcmp(m->chats[i]->self_public_key, group_public_key, crypto_box_PUBLICKEYBYTES) == 0) | 731 | if (id_equal(m->chats[i]->self_public_key, group_public_key)) |
732 | return i; | 732 | return i; |
733 | } | 733 | } |
734 | 734 | ||
@@ -917,8 +917,8 @@ int join_groupchat(Messenger *m, int friendnumber, uint8_t *friend_group_public_ | |||
917 | if (groupnum == -1) | 917 | if (groupnum == -1) |
918 | return -1; | 918 | return -1; |
919 | 919 | ||
920 | memcpy(data, friend_group_public_key, crypto_box_PUBLICKEYBYTES); | 920 | id_copy(data, friend_group_public_key); |
921 | memcpy(data + crypto_box_PUBLICKEYBYTES, m->chats[groupnum]->self_public_key, crypto_box_PUBLICKEYBYTES); | 921 | id_copy(data + crypto_box_PUBLICKEYBYTES, m->chats[groupnum]->self_public_key); |
922 | 922 | ||
923 | if (write_cryptpacket_id(m, friendnumber, PACKET_ID_JOIN_GROUPCHAT, data, sizeof(data))) { | 923 | if (write_cryptpacket_id(m, friendnumber, PACKET_ID_JOIN_GROUPCHAT, data, sizeof(data))) { |
924 | chat_bootstrap_nonlazy(m->chats[groupnum], get_friend_ipport(m, friendnumber), | 924 | chat_bootstrap_nonlazy(m->chats[groupnum], get_friend_ipport(m, friendnumber), |
@@ -965,7 +965,7 @@ static int handle_group(void *object, IP_Port source, uint8_t *packet, uint32_t | |||
965 | if (m->chats[i] == NULL) | 965 | if (m->chats[i] == NULL) |
966 | continue; | 966 | continue; |
967 | 967 | ||
968 | if (memcmp(packet + 1, m->chats[i]->self_public_key, crypto_box_PUBLICKEYBYTES) == 0) | 968 | if (id_equal(packet + 1, m->chats[i]->self_public_key)) |
969 | return handle_groupchatpacket(m->chats[i], source, packet, length); | 969 | return handle_groupchatpacket(m->chats[i], source, packet, length); |
970 | } | 970 | } |
971 | 971 | ||
@@ -1769,6 +1769,8 @@ static char *ID2String(uint8_t *client_id) | |||
1769 | /* The main loop that needs to be run at least 20 times per second. */ | 1769 | /* The main loop that needs to be run at least 20 times per second. */ |
1770 | void do_messenger(Messenger *m) | 1770 | void do_messenger(Messenger *m) |
1771 | { | 1771 | { |
1772 | unix_time_update(); | ||
1773 | |||
1772 | networking_poll(m->net); | 1774 | networking_poll(m->net); |
1773 | 1775 | ||
1774 | do_DHT(m->dht); | 1776 | do_DHT(m->dht); |
@@ -1888,144 +1890,6 @@ void wait_cleanup_messenger(Messenger *m, uint8_t *data, uint16_t len) | |||
1888 | networking_wait_cleanup(m->net, data, len); | 1890 | networking_wait_cleanup(m->net, data, len); |
1889 | } | 1891 | } |
1890 | 1892 | ||
1891 | /* return size of the messenger data (for saving) */ | ||
1892 | uint32_t Messenger_size_old(Messenger *m) | ||
1893 | { | ||
1894 | return crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES | ||
1895 | + sizeof(uint32_t) // nospam. | ||
1896 | + sizeof(uint32_t) // DHT size. | ||
1897 | + DHT_size(m->dht) // DHT itself. | ||
1898 | + sizeof(uint32_t) // Friendlist size. | ||
1899 | + sizeof(Friend) * m->numfriends // Friendlist itself. | ||
1900 | + sizeof(uint16_t) // Own nickname length. | ||
1901 | + m->name_length // Own nickname. | ||
1902 | ; | ||
1903 | } | ||
1904 | |||
1905 | /* Save the messenger in data of size Messenger_size(). Old version without cookies. */ | ||
1906 | static void Messenger_save_old(Messenger *m, uint8_t *data) | ||
1907 | { | ||
1908 | save_keys(m->net_crypto, data); | ||
1909 | data += crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES; | ||
1910 | |||
1911 | uint32_t nospam = get_nospam(&(m->fr)); | ||
1912 | memcpy(data, &nospam, sizeof(nospam)); | ||
1913 | data += sizeof(nospam); | ||
1914 | |||
1915 | uint32_t size = DHT_size(m->dht); | ||
1916 | memcpy(data, &size, sizeof(size)); | ||
1917 | data += sizeof(size); | ||
1918 | DHT_save(m->dht, data); | ||
1919 | data += size; | ||
1920 | |||
1921 | size = sizeof(Friend) * m->numfriends; | ||
1922 | memcpy(data, &size, sizeof(size)); | ||
1923 | data += sizeof(size); | ||
1924 | memcpy(data, m->friendlist, sizeof(Friend) * m->numfriends); | ||
1925 | data += size; | ||
1926 | |||
1927 | uint16_t small_size = m->name_length; | ||
1928 | memcpy(data, &small_size, sizeof(small_size)); | ||
1929 | data += sizeof(small_size); | ||
1930 | memcpy(data, m->name, small_size); | ||
1931 | } | ||
1932 | |||
1933 | /* Load the messenger from data of size length. Old version without cookies. */ | ||
1934 | static int Messenger_load_old(Messenger *m, uint8_t *data, uint32_t length) | ||
1935 | { | ||
1936 | if (length == ~((uint32_t)0)) | ||
1937 | return -1; | ||
1938 | |||
1939 | /* BLOCK1: PUBKEY, SECKEY, NOSPAM, SIZE */ | ||
1940 | if (length < crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizeof(uint32_t) * 2) | ||
1941 | return -1; | ||
1942 | |||
1943 | load_keys(m->net_crypto, data); | ||
1944 | data += crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES; | ||
1945 | length -= crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES; | ||
1946 | |||
1947 | uint32_t nospam; | ||
1948 | memcpy(&nospam, data, sizeof(nospam)); | ||
1949 | set_nospam(&(m->fr), nospam); | ||
1950 | data += sizeof(nospam); | ||
1951 | length -= sizeof(nospam); | ||
1952 | |||
1953 | uint32_t size; | ||
1954 | |||
1955 | if (length < sizeof(size)) | ||
1956 | return -1; | ||
1957 | |||
1958 | memcpy(&size, data, sizeof(size)); | ||
1959 | data += sizeof(size); | ||
1960 | length -= sizeof(size); | ||
1961 | |||
1962 | if (length < size) | ||
1963 | return -1; | ||
1964 | |||
1965 | if (DHT_load_old(m->dht, data, size) == -1) { | ||
1966 | #ifdef DEBUG | ||
1967 | fprintf(stderr, "Data file: Something wicked happened to the stored connections...\n"); | ||
1968 | /* DO go on, friends/name still might be intact */ | ||
1969 | #endif | ||
1970 | } | ||
1971 | |||
1972 | data += size; | ||
1973 | length -= size; | ||
1974 | |||
1975 | if (length < sizeof(size)) | ||
1976 | return -1; | ||
1977 | |||
1978 | memcpy(&size, data, sizeof(size)); | ||
1979 | data += sizeof(size); | ||
1980 | length -= sizeof(size); | ||
1981 | |||
1982 | if (length < size) | ||
1983 | return -1; | ||
1984 | |||
1985 | if (!(size % sizeof(Friend))) { | ||
1986 | uint16_t num = size / sizeof(Friend); | ||
1987 | Friend *friend_list = (Friend *)data; | ||
1988 | |||
1989 | uint32_t i; | ||
1990 | |||
1991 | for (i = 0; i < num; ++i) { | ||
1992 | if (friend_list[i].status >= 3) { | ||
1993 | int fnum = m_addfriend_norequest(m, friend_list[i].client_id); | ||
1994 | setfriendname(m, fnum, friend_list[i].name, friend_list[i].name_length); | ||
1995 | /* set_friend_statusmessage(fnum, temp[i].statusmessage, temp[i].statusmessage_length); */ | ||
1996 | } else if (friend_list[i].status != 0) { | ||
1997 | /* TODO: This is not a good way to do this. */ | ||
1998 | uint8_t address[FRIEND_ADDRESS_SIZE]; | ||
1999 | memcpy(address, friend_list[i].client_id, crypto_box_PUBLICKEYBYTES); | ||
2000 | memcpy(address + crypto_box_PUBLICKEYBYTES, &(friend_list[i].friendrequest_nospam), sizeof(uint32_t)); | ||
2001 | uint16_t checksum = address_checksum(address, FRIEND_ADDRESS_SIZE - sizeof(checksum)); | ||
2002 | memcpy(address + crypto_box_PUBLICKEYBYTES + sizeof(uint32_t), &checksum, sizeof(checksum)); | ||
2003 | m_addfriend(m, address, friend_list[i].info, friend_list[i].info_size); | ||
2004 | } | ||
2005 | } | ||
2006 | } | ||
2007 | |||
2008 | data += size; | ||
2009 | length -= size; | ||
2010 | |||
2011 | uint16_t small_size; | ||
2012 | |||
2013 | if (length < sizeof(small_size)) | ||
2014 | return -1; | ||
2015 | |||
2016 | memcpy(&small_size, data, sizeof(small_size)); | ||
2017 | data += sizeof(small_size); | ||
2018 | length -= sizeof(small_size); | ||
2019 | |||
2020 | if (length < small_size) | ||
2021 | return -1; | ||
2022 | |||
2023 | setname(m, data, small_size); | ||
2024 | |||
2025 | return 0; | ||
2026 | } | ||
2027 | |||
2028 | |||
2029 | /* new messenger format for load/save, more robust and forward compatible */ | 1893 | /* new messenger format for load/save, more robust and forward compatible */ |
2030 | 1894 | ||
2031 | #define MESSENGER_STATE_COOKIE_GLOBAL 0x15ed1b1e | 1895 | #define MESSENGER_STATE_COOKIE_GLOBAL 0x15ed1b1e |
@@ -2130,7 +1994,7 @@ static int messenger_load_state_callback(void *outer, uint8_t *data, uint32_t le | |||
2130 | } else if (friends[i].status != 0) { | 1994 | } else if (friends[i].status != 0) { |
2131 | /* TODO: This is not a good way to do this. */ | 1995 | /* TODO: This is not a good way to do this. */ |
2132 | uint8_t address[FRIEND_ADDRESS_SIZE]; | 1996 | uint8_t address[FRIEND_ADDRESS_SIZE]; |
2133 | memcpy(address, friends[i].client_id, crypto_box_PUBLICKEYBYTES); | 1997 | id_copy(address, friends[i].client_id); |
2134 | memcpy(address + crypto_box_PUBLICKEYBYTES, &(friends[i].friendrequest_nospam), sizeof(uint32_t)); | 1998 | memcpy(address + crypto_box_PUBLICKEYBYTES, &(friends[i].friendrequest_nospam), sizeof(uint32_t)); |
2135 | uint16_t checksum = address_checksum(address, FRIEND_ADDRESS_SIZE - sizeof(checksum)); | 1999 | uint16_t checksum = address_checksum(address, FRIEND_ADDRESS_SIZE - sizeof(checksum)); |
2136 | memcpy(address + crypto_box_PUBLICKEYBYTES + sizeof(uint32_t), &checksum, sizeof(checksum)); | 2000 | memcpy(address + crypto_box_PUBLICKEYBYTES + sizeof(uint32_t), &checksum, sizeof(checksum)); |
@@ -2174,7 +2038,7 @@ int messenger_load(Messenger *m, uint8_t *data, uint32_t length) | |||
2174 | return load_state(messenger_load_state_callback, m, data + cookie_len, | 2038 | return load_state(messenger_load_state_callback, m, data + cookie_len, |
2175 | length - cookie_len, MESSENGER_STATE_COOKIE_TYPE); | 2039 | length - cookie_len, MESSENGER_STATE_COOKIE_TYPE); |
2176 | else /* old state file */ | 2040 | else /* old state file */ |
2177 | return Messenger_load_old(m, data, length); | 2041 | return -1; |
2178 | } | 2042 | } |
2179 | 2043 | ||
2180 | /* Return the number of friends in the instance m. | 2044 | /* Return the number of friends in the instance m. |