summaryrefslogtreecommitdiff
path: root/toxcore/Messenger.c
diff options
context:
space:
mode:
authormannol <eniz_vukovic@hotmail.com>2014-05-22 00:08:19 +0200
committermannol <eniz_vukovic@hotmail.com>2014-05-22 00:08:19 +0200
commit9db41e7582baf15d650376e8afc38dd6146492a8 (patch)
tree38c5d6b6066a1c3aba43d21ba78740ceb6b22323 /toxcore/Messenger.c
parent0620d1f0643d0f8a6a7d9361615b164242559768 (diff)
parentbe4559de73c17bcfa396098d6cc48b7a4b1157c6 (diff)
Merge remote-tracking branch 'upstream/master' into Multicalls-patch
Diffstat (limited to 'toxcore/Messenger.c')
-rw-r--r--toxcore/Messenger.c83
1 files changed, 54 insertions, 29 deletions
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c
index 4a9e5f7f..e0ef5a28 100644
--- a/toxcore/Messenger.c
+++ b/toxcore/Messenger.c
@@ -715,6 +715,25 @@ static int send_ping(Messenger *m, int32_t friendnumber)
715 return ret; 715 return ret;
716} 716}
717 717
718static int send_relays(Messenger *m, int32_t friendnumber)
719{
720 Node_format nodes[MAX_SHARED_RELAYS];
721 uint8_t data[1024];
722 int n, length;
723
724 n = copy_connected_tcp_relays(m->net_crypto, nodes, MAX_SHARED_RELAYS);
725 length = pack_nodes(data, sizeof(data), nodes, n);
726
727 int ret = write_cryptpacket_id(m, friendnumber, PACKET_ID_SHARE_RELAYS, data, length);
728
729 if (ret == 1)
730 m->friendlist[friendnumber].share_relays_lastsent = unix_time();
731
732 return ret;
733}
734
735
736
718static int set_friend_statusmessage(Messenger *m, int32_t friendnumber, uint8_t *status, uint16_t length) 737static int set_friend_statusmessage(Messenger *m, int32_t friendnumber, uint8_t *status, uint16_t length)
719{ 738{
720 if (friend_not_valid(m, friendnumber)) 739 if (friend_not_valid(m, friendnumber))
@@ -1681,46 +1700,35 @@ int m_msi_packet(Messenger *m, int32_t friendnumber, uint8_t *data, uint16_t len
1681 return write_cryptpacket_id(m, friendnumber, PACKET_ID_MSI, data, length); 1700 return write_cryptpacket_id(m, friendnumber, PACKET_ID_MSI, data, length);
1682} 1701}
1683 1702
1684static int32_t friendnum_from_ip_port(Messenger *m, IP_Port ip_port) 1703static int handle_custom_user_packet(void *object, int friend_num, uint8_t *packet, uint16_t length)
1685{
1686 uint32_t i;
1687
1688 for (i = 0; i < m->numonline_friends; ++i) {
1689 if (ipport_equal(&m->online_friendlist[i].ip_port, &ip_port))
1690 return m->online_friendlist[i].friend_num;
1691 }
1692
1693 return -1;
1694}
1695
1696static int handle_custom_user_packet(void *object, IP_Port source, uint8_t *packet, uint32_t length)
1697{ 1704{
1698 Messenger *m = object; 1705 Messenger *m = object;
1699 int32_t friend_num = friendnum_from_ip_port(m, source);
1700 1706
1701 if (friend_num == -1) 1707 if (friend_not_valid(m, friend_num))
1702 return 1; 1708 return 1;
1703 1709
1704 if (m->friendlist[friend_num].packethandlers[packet[0] % TOTAL_USERPACKETS].function) 1710 if (m->friendlist[friend_num].packethandlers[packet[0] % PACKET_ID_LOSSY_RANGE_SIZE].function)
1705 return m->friendlist[friend_num].packethandlers[packet[0] % TOTAL_USERPACKETS].function( 1711 return m->friendlist[friend_num].packethandlers[packet[0] % PACKET_ID_LOSSY_RANGE_SIZE].function(
1706 m->friendlist[friend_num].packethandlers[packet[0] % TOTAL_USERPACKETS].object, source, packet, length); 1712 m->friendlist[friend_num].packethandlers[packet[0] % PACKET_ID_LOSSY_RANGE_SIZE].object, packet, length);
1707 1713
1708 return 1; 1714 return 1;
1709} 1715}
1710 1716
1711 1717
1712int custom_user_packet_registerhandler(Messenger *m, int32_t friendnumber, uint8_t byte, packet_handler_callback cb, 1718int custom_user_packet_registerhandler(Messenger *m, int32_t friendnumber, uint8_t byte,
1713 void *object) 1719 int (*packet_handler_callback)(void *object, uint8_t *data, uint32_t len), void *object)
1714{ 1720{
1715 if (friend_not_valid(m, friendnumber)) 1721 if (friend_not_valid(m, friendnumber))
1716 return -1; 1722 return -1;
1717 1723
1718 if (byte < NET_PACKET_CUSTOM_RANGE_START || byte >= NET_PACKET_CUSTOM_RANGE_END) 1724 if (byte < PACKET_ID_LOSSY_RANGE_START)
1725 return -1;
1726
1727 if (byte >= (PACKET_ID_LOSSY_RANGE_START + PACKET_ID_LOSSY_RANGE_SIZE))
1719 return -1; 1728 return -1;
1720 1729
1721 m->friendlist[friendnumber].packethandlers[byte % TOTAL_USERPACKETS].function = cb; 1730 m->friendlist[friendnumber].packethandlers[byte % PACKET_ID_LOSSY_RANGE_SIZE].function = packet_handler_callback;
1722 m->friendlist[friendnumber].packethandlers[byte % TOTAL_USERPACKETS].object = object; 1731 m->friendlist[friendnumber].packethandlers[byte % PACKET_ID_LOSSY_RANGE_SIZE].object = object;
1723 networking_registerhandler(m->net, byte, handle_custom_user_packet, m);
1724 return 0; 1732 return 0;
1725} 1733}
1726 1734
@@ -1732,12 +1740,10 @@ int send_custom_user_packet(Messenger *m, int32_t friendnumber, uint8_t *data, u
1732 if (m->friendlist[friendnumber].status != FRIEND_ONLINE) 1740 if (m->friendlist[friendnumber].status != FRIEND_ONLINE)
1733 return -1; 1741 return -1;
1734 1742
1735 IP_Port ip_port = get_friend_ipport(m, friendnumber); 1743 if (m->friendlist[friendnumber].crypt_connection_id == -1)
1736
1737 if (ip_port.port == 0)
1738 return -1; 1744 return -1;
1739 1745
1740 return sendpacket(m->net, ip_port, data, length); 1746 return send_lossy_cryptpacket(m->net_crypto, m->friendlist[friendnumber].crypt_connection_id, data, length);
1741} 1747}
1742 1748
1743 1749
@@ -1776,6 +1782,7 @@ static int handle_new_connections(void *object, New_Connection *n_c)
1776 int id = accept_crypto_connection(m->net_crypto, n_c); 1782 int id = accept_crypto_connection(m->net_crypto, n_c);
1777 connection_status_handler(m->net_crypto, id, &handle_status, m, friend_id); 1783 connection_status_handler(m->net_crypto, id, &handle_status, m, friend_id);
1778 connection_data_handler(m->net_crypto, id, &handle_packet, m, friend_id); 1784 connection_data_handler(m->net_crypto, id, &handle_packet, m, friend_id);
1785 connection_lossy_data_handler(m->net_crypto, id, &handle_custom_user_packet, m, friend_id);
1779 m->friendlist[friend_id].crypt_connection_id = id; 1786 m->friendlist[friend_id].crypt_connection_id = id;
1780 set_friend_status(m, friend_id, FRIEND_CONFIRMED); 1787 set_friend_status(m, friend_id, FRIEND_CONFIRMED);
1781 return 0; 1788 return 0;
@@ -2162,6 +2169,20 @@ static int handle_packet(void *object, int i, uint8_t *temp, uint16_t len)
2162 (*m->msi_packet)(m, i, data, data_length, m->msi_packet_userdata); 2169 (*m->msi_packet)(m, i, data, data_length, m->msi_packet_userdata);
2163 } 2170 }
2164 2171
2172 case PACKET_ID_SHARE_RELAYS: {
2173 Node_format nodes[MAX_SHARED_RELAYS];
2174 int n;
2175
2176 if ((n = unpack_nodes(nodes, MAX_SHARED_RELAYS, NULL, data, data_length, 1)) == -1)
2177 break;
2178
2179 int i;
2180
2181 for (i = 0; i < n; i++) {
2182 add_tcp_relay(m->net_crypto, nodes[i].ip_port, nodes[i].client_id);
2183 }
2184 }
2185
2165 default: { 2186 default: {
2166 break; 2187 break;
2167 } 2188 }
@@ -2187,8 +2208,8 @@ static int friend_new_connection(Messenger *m, int32_t friendnumber, uint8_t *re
2187 m->friendlist[friendnumber].crypt_connection_id = id; 2208 m->friendlist[friendnumber].crypt_connection_id = id;
2188 connection_status_handler(m->net_crypto, id, &handle_status, m, friendnumber); 2209 connection_status_handler(m->net_crypto, id, &handle_status, m, friendnumber);
2189 connection_data_handler(m->net_crypto, id, &handle_packet, m, friendnumber); 2210 connection_data_handler(m->net_crypto, id, &handle_packet, m, friendnumber);
2211 connection_lossy_data_handler(m->net_crypto, id, &handle_custom_user_packet, m, friendnumber);
2190 return 0; 2212 return 0;
2191
2192} 2213}
2193 2214
2194/* TODO: Make this function not suck. */ 2215/* TODO: Make this function not suck. */
@@ -2276,6 +2297,10 @@ void do_friends(Messenger *m)
2276 m->friendlist[i].crypt_connection_id = -1; 2297 m->friendlist[i].crypt_connection_id = -1;
2277 set_friend_status(m, i, FRIEND_CONFIRMED); 2298 set_friend_status(m, i, FRIEND_CONFIRMED);
2278 } 2299 }
2300
2301 if (m->friendlist[i].share_relays_lastsent + FRIEND_SHARE_RELAYS_INTERVAL < temp_time) {
2302 send_relays(m, i);
2303 }
2279 } 2304 }
2280 } 2305 }
2281} 2306}