summaryrefslogtreecommitdiff
path: root/toxcore/Messenger.c
diff options
context:
space:
mode:
Diffstat (limited to 'toxcore/Messenger.c')
-rw-r--r--toxcore/Messenger.c46
1 files changed, 17 insertions, 29 deletions
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c
index 1e6e6c06..aefa3a68 100644
--- a/toxcore/Messenger.c
+++ b/toxcore/Messenger.c
@@ -1699,46 +1699,35 @@ int m_msi_packet(Messenger *m, int32_t friendnumber, uint8_t *data, uint16_t len
1699 return write_cryptpacket_id(m, friendnumber, PACKET_ID_MSI, data, length); 1699 return write_cryptpacket_id(m, friendnumber, PACKET_ID_MSI, data, length);
1700} 1700}
1701 1701
1702static int32_t friendnum_from_ip_port(Messenger *m, IP_Port ip_port) 1702static int handle_custom_user_packet(void *object, int friend_num, uint8_t *packet, uint16_t length)
1703{
1704 uint32_t i;
1705
1706 for (i = 0; i < m->numonline_friends; ++i) {
1707 if (ipport_equal(&m->online_friendlist[i].ip_port, &ip_port))
1708 return m->online_friendlist[i].friend_num;
1709 }
1710
1711 return -1;
1712}
1713
1714static int handle_custom_user_packet(void *object, IP_Port source, uint8_t *packet, uint32_t length)
1715{ 1703{
1716 Messenger *m = object; 1704 Messenger *m = object;
1717 int32_t friend_num = friendnum_from_ip_port(m, source);
1718 1705
1719 if (friend_num == -1) 1706 if (friend_not_valid(m, friend_num))
1720 return 1; 1707 return 1;
1721 1708
1722 if (m->friendlist[friend_num].packethandlers[packet[0] % TOTAL_USERPACKETS].function) 1709 if (m->friendlist[friend_num].packethandlers[packet[0] % PACKET_ID_LOSSY_RANGE_SIZE].function)
1723 return m->friendlist[friend_num].packethandlers[packet[0] % TOTAL_USERPACKETS].function( 1710 return m->friendlist[friend_num].packethandlers[packet[0] % PACKET_ID_LOSSY_RANGE_SIZE].function(
1724 m->friendlist[friend_num].packethandlers[packet[0] % TOTAL_USERPACKETS].object, source, packet, length); 1711 m->friendlist[friend_num].packethandlers[packet[0] % PACKET_ID_LOSSY_RANGE_SIZE].object, packet, length);
1725 1712
1726 return 1; 1713 return 1;
1727} 1714}
1728 1715
1729 1716
1730int custom_user_packet_registerhandler(Messenger *m, int32_t friendnumber, uint8_t byte, packet_handler_callback cb, 1717int custom_user_packet_registerhandler(Messenger *m, int32_t friendnumber, uint8_t byte,
1731 void *object) 1718 int (*packet_handler_callback)(void *object, uint8_t *data, uint32_t len), void *object)
1732{ 1719{
1733 if (friend_not_valid(m, friendnumber)) 1720 if (friend_not_valid(m, friendnumber))
1734 return -1; 1721 return -1;
1735 1722
1736 if (byte < NET_PACKET_CUSTOM_RANGE_START || byte >= NET_PACKET_CUSTOM_RANGE_END) 1723 if (byte < PACKET_ID_LOSSY_RANGE_START)
1724 return -1;
1725
1726 if (byte >= (PACKET_ID_LOSSY_RANGE_START + PACKET_ID_LOSSY_RANGE_SIZE))
1737 return -1; 1727 return -1;
1738 1728
1739 m->friendlist[friendnumber].packethandlers[byte % TOTAL_USERPACKETS].function = cb; 1729 m->friendlist[friendnumber].packethandlers[byte % PACKET_ID_LOSSY_RANGE_SIZE].function = packet_handler_callback;
1740 m->friendlist[friendnumber].packethandlers[byte % TOTAL_USERPACKETS].object = object; 1730 m->friendlist[friendnumber].packethandlers[byte % PACKET_ID_LOSSY_RANGE_SIZE].object = object;
1741 networking_registerhandler(m->net, byte, handle_custom_user_packet, m);
1742 return 0; 1731 return 0;
1743} 1732}
1744 1733
@@ -1750,12 +1739,10 @@ int send_custom_user_packet(Messenger *m, int32_t friendnumber, uint8_t *data, u
1750 if (m->friendlist[friendnumber].status != FRIEND_ONLINE) 1739 if (m->friendlist[friendnumber].status != FRIEND_ONLINE)
1751 return -1; 1740 return -1;
1752 1741
1753 IP_Port ip_port = get_friend_ipport(m, friendnumber); 1742 if (m->friendlist[friendnumber].crypt_connection_id == -1)
1754
1755 if (ip_port.port == 0)
1756 return -1; 1743 return -1;
1757 1744
1758 return sendpacket(m->net, ip_port, data, length); 1745 return send_lossy_cryptpacket(m->net_crypto, m->friendlist[friendnumber].crypt_connection_id, data, length);
1759} 1746}
1760 1747
1761 1748
@@ -1794,6 +1781,7 @@ static int handle_new_connections(void *object, New_Connection *n_c)
1794 int id = accept_crypto_connection(m->net_crypto, n_c); 1781 int id = accept_crypto_connection(m->net_crypto, n_c);
1795 connection_status_handler(m->net_crypto, id, &handle_status, m, friend_id); 1782 connection_status_handler(m->net_crypto, id, &handle_status, m, friend_id);
1796 connection_data_handler(m->net_crypto, id, &handle_packet, m, friend_id); 1783 connection_data_handler(m->net_crypto, id, &handle_packet, m, friend_id);
1784 connection_lossy_data_handler(m->net_crypto, id, &handle_custom_user_packet, m, friend_id);
1797 m->friendlist[friend_id].crypt_connection_id = id; 1785 m->friendlist[friend_id].crypt_connection_id = id;
1798 set_friend_status(m, friend_id, FRIEND_CONFIRMED); 1786 set_friend_status(m, friend_id, FRIEND_CONFIRMED);
1799 return 0; 1787 return 0;
@@ -2218,8 +2206,8 @@ static int friend_new_connection(Messenger *m, int32_t friendnumber, uint8_t *re
2218 m->friendlist[friendnumber].crypt_connection_id = id; 2206 m->friendlist[friendnumber].crypt_connection_id = id;
2219 connection_status_handler(m->net_crypto, id, &handle_status, m, friendnumber); 2207 connection_status_handler(m->net_crypto, id, &handle_status, m, friendnumber);
2220 connection_data_handler(m->net_crypto, id, &handle_packet, m, friendnumber); 2208 connection_data_handler(m->net_crypto, id, &handle_packet, m, friendnumber);
2209 connection_lossy_data_handler(m->net_crypto, id, &handle_custom_user_packet, m, friendnumber);
2221 return 0; 2210 return 0;
2222
2223} 2211}
2224 2212
2225/* TODO: Make this function not suck. */ 2213/* TODO: Make this function not suck. */