summaryrefslogtreecommitdiff
path: root/toxcore
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2013-12-05 19:40:32 -0500
committerirungentoo <irungentoo@gmail.com>2013-12-05 19:40:32 -0500
commit45785ffed041d863a29ccffa6de8a1b13bafc439 (patch)
treebde590c66d390e6ac9fc8c87051bc4cba8fdcf79 /toxcore
parent2aae893b6d03e18faaf03830946b228d85c85994 (diff)
parent94df395fab0bf4bf7fd50b3283ad6ca5faccb734 (diff)
Merge branch 'master' of https://github.com/irungentoo/ProjectTox-Core
Diffstat (limited to 'toxcore')
-rw-r--r--toxcore/Messenger.c97
-rw-r--r--toxcore/tox.c2
-rw-r--r--toxcore/tox.h2
3 files changed, 68 insertions, 33 deletions
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c
index 10e36746..777e6a9e 100644
--- a/toxcore/Messenger.c
+++ b/toxcore/Messenger.c
@@ -1455,7 +1455,7 @@ int m_msi_packet(Messenger *m, int friendnumber, uint8_t *data, uint16_t length)
1455} 1455}
1456 1456
1457/* Function to filter out some friend requests*/ 1457/* Function to filter out some friend requests*/
1458static int friend_already_added(uint8_t * client_id, void * data) 1458static int friend_already_added(uint8_t *client_id, void *data)
1459{ 1459{
1460 Messenger *m = data; 1460 Messenger *m = data;
1461 1461
@@ -1538,6 +1538,24 @@ void kill_messenger(Messenger *m)
1538 free(m); 1538 free(m);
1539} 1539}
1540 1540
1541/* Check for and handle a timed-out friend request. If the request has
1542 * timed-out then the friend status is set back to FRIEND_ADDED.
1543 * i: friendlist index of the timed-out friend
1544 * t: time
1545 */
1546static void check_friend_request_timed_out(Messenger *m, uint32_t i, uint64_t t)
1547{
1548 Friend *f = &m->friendlist[i];
1549
1550 if (f->friendrequest_lastsent + f->friendrequest_timeout < t) {
1551 set_friend_status(m, i, FRIEND_ADDED);
1552 /* Double the default timeout everytime if friendrequest is assumed
1553 * to have been sent unsuccessfully.
1554 */
1555 f->friendrequest_timeout *= 2;
1556 }
1557}
1558
1541/* TODO: Make this function not suck. */ 1559/* TODO: Make this function not suck. */
1542void do_friends(Messenger *m) 1560void do_friends(Messenger *m)
1543{ 1561{
@@ -1565,13 +1583,7 @@ void do_friends(Messenger *m)
1565 /* If we didn't connect to friend after successfully sending him a friend request the request is deemed 1583 /* If we didn't connect to friend after successfully sending him a friend request the request is deemed
1566 * unsuccessful so we set the status back to FRIEND_ADDED and try again. 1584 * unsuccessful so we set the status back to FRIEND_ADDED and try again.
1567 */ 1585 */
1568 if (m->friendlist[i].friendrequest_lastsent + m->friendlist[i].friendrequest_timeout < temp_time) { 1586 check_friend_request_timed_out(m, i, temp_time);
1569 set_friend_status(m, i, FRIEND_ADDED);
1570 /* Double the default timeout everytime if friendrequest is assumed to have been
1571 * sent unsuccessfully.
1572 */
1573 m->friendlist[i].friendrequest_timeout *= 2;
1574 }
1575 } 1587 }
1576 1588
1577 IP_Port friendip; 1589 IP_Port friendip;
@@ -1948,7 +1960,30 @@ void do_messenger(Messenger *m)
1948 1960
1949 loglog(" = = = = = = = = \n"); 1961 loglog(" = = = = = = = = \n");
1950 1962
1951 uint32_t num_friends = MIN(m->numfriends, m->dht->num_friends); 1963 uint32_t friend, dhtfriend;
1964
1965 /* dht contains additional "friends" (requests) */
1966 uint32_t num_dhtfriends = m->dht->num_friends;
1967 int32_t m2dht[num_dhtfriends];
1968 int32_t dht2m[num_dhtfriends];
1969
1970 for (friend = 0; friend < num_dhtfriends; friend++) {
1971 m2dht[friend] = -1;
1972 dht2m[friend] = -1;
1973
1974 if (friend >= m->numfriends)
1975 continue;
1976
1977 for (dhtfriend = 0; dhtfriend < m->dht->num_friends; dhtfriend++)
1978 if (id_equal(m->friendlist[friend].client_id, m->dht->friends_list[dhtfriend].client_id)) {
1979 m2dht[friend] = dhtfriend;
1980 break;
1981 }
1982 }
1983
1984 for (friend = 0; friend < num_dhtfriends; friend++)
1985 if (m2dht[friend] >= 0)
1986 dht2m[m2dht[friend]] = friend;
1952 1987
1953 if (m->numfriends != m->dht->num_friends) { 1988 if (m->numfriends != m->dht->num_friends) {
1954 sprintf(logbuffer, "Friend num in DHT %u != friend num in msger %u\n", 1989 sprintf(logbuffer, "Friend num in DHT %u != friend num in msger %u\n",
@@ -1956,33 +1991,33 @@ void do_messenger(Messenger *m)
1956 loglog(logbuffer); 1991 loglog(logbuffer);
1957 } 1992 }
1958 1993
1959 uint32_t friend, ping_lastrecv; 1994 uint32_t ping_lastrecv;
1995 Friend *msgfptr;
1996 DHT_Friend *dhtfptr;
1960 1997
1961 for (friend = 0; friend < num_friends; friend++) { 1998 for (friend = 0; friend < num_dhtfriends; friend++) {
1962 Friend *msgfptr = &m->friendlist[friend]; 1999 if (dht2m[friend] >= 0)
1963 DHT_Friend *dhtfptr = &m->dht->friends_list[friend]; 2000 msgfptr = &m->friendlist[dht2m[friend]];
2001 else
2002 msgfptr = NULL;
1964 2003
1965 if (memcmp(msgfptr->client_id, dhtfptr->client_id, CLIENT_ID_SIZE)) { 2004 dhtfptr = &m->dht->friends_list[friend];
1966 if (sizeof(logbuffer) > 2 * CLIENT_ID_SIZE + 64) {
1967 sprintf(logbuffer, "F[%2u] ID(m) %s != ID(d) ", friend,
1968 ID2String(msgfptr->client_id));
1969 strcat(logbuffer + strlen(logbuffer), ID2String(dhtfptr->client_id));
1970 strcat(logbuffer + strlen(logbuffer), "\n");
1971 } else
1972 sprintf(logbuffer, "F[%2u] ID(m) != ID(d) ", friend);
1973
1974 loglog(logbuffer);
1975 }
1976 2005
1977 ping_lastrecv = lastdump - msgfptr->ping_lastrecv; 2006 if (msgfptr) {
2007 ping_lastrecv = lastdump - msgfptr->ping_lastrecv;
1978 2008
1979 if (ping_lastrecv > 999) 2009 if (ping_lastrecv > 999)
1980 ping_lastrecv = 999; 2010 ping_lastrecv = 999;
1981 2011
1982 snprintf(logbuffer, sizeof(logbuffer), "F[%2u] <%s> %02u [%03u] %s\n", 2012 snprintf(logbuffer, sizeof(logbuffer), "F[%2u:%2u] <%s> %02i [%03u] %s\n",
1983 friend, msgfptr->name, msgfptr->crypt_connection_id, 2013 dht2m[friend], friend, msgfptr->name, msgfptr->crypt_connection_id,
1984 ping_lastrecv, ID2String(msgfptr->client_id)); 2014 ping_lastrecv, ID2String(msgfptr->client_id));
1985 loglog(logbuffer); 2015 loglog(logbuffer);
2016 } else {
2017 snprintf(logbuffer, sizeof(logbuffer), "F[--:%2u] %s\n",
2018 friend, ID2String(dhtfptr->client_id));
2019 loglog(logbuffer);
2020 }
1986 2021
1987 for (client = 0; client < MAX_FRIEND_CLIENTS; client++) { 2022 for (client = 0; client < MAX_FRIEND_CLIENTS; client++) {
1988 Client_data *cptr = &dhtfptr->client_list[client]; 2023 Client_data *cptr = &dhtfptr->client_list[client];
diff --git a/toxcore/tox.c b/toxcore/tox.c
index bdc110e6..487f2517 100644
--- a/toxcore/tox.c
+++ b/toxcore/tox.c
@@ -222,7 +222,7 @@ int tox_set_status_message(Tox *tox, uint8_t *status, uint16_t length)
222 return m_set_statusmessage(m, status, length); 222 return m_set_statusmessage(m, status, length);
223} 223}
224 224
225int tox_set_userstatus(Tox *tox, TOX_USERSTATUS status) 225int tox_set_user_status(Tox *tox, TOX_USERSTATUS status)
226{ 226{
227 Messenger *m = tox; 227 Messenger *m = tox;
228 return m_set_userstatus(m, (USERSTATUS)status); 228 return m_set_userstatus(m, (USERSTATUS)status);
diff --git a/toxcore/tox.h b/toxcore/tox.h
index 407d7441..ccef8519 100644
--- a/toxcore/tox.h
+++ b/toxcore/tox.h
@@ -247,7 +247,7 @@ int tox_get_name(Tox *tox, int friendnumber, uint8_t *name);
247 * returns -1 on failure. 247 * returns -1 on failure.
248 */ 248 */
249int tox_set_status_message(Tox *tox, uint8_t *status, uint16_t length); 249int tox_set_status_message(Tox *tox, uint8_t *status, uint16_t length);
250int tox_set_userstatus(Tox *tox, TOX_USERSTATUS status); 250int tox_set_user_status(Tox *tox, TOX_USERSTATUS status);
251 251
252/* return the length of friendnumber's status message, including null. 252/* return the length of friendnumber's status message, including null.
253 * Pass it into malloc 253 * Pass it into malloc