From a81b708eb57f53e5b051c5510cc2db03fdf16315 Mon Sep 17 00:00:00 2001 From: irungentoo Date: Sun, 13 Oct 2013 22:07:30 -0400 Subject: [PATCH] A/V integration in progress. Added functions to send/receive msi packets via secure messenger connections. Made function to get the ip/port of a friend in Messenger.c public --- toxcore/Messenger.c | 38 ++++++++++++++++++++++++++++++++++---- toxcore/Messenger.h | 27 ++++++++++++++++++++++++++- 2 files changed, 60 insertions(+), 5 deletions(-) diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index 7de14edf..44570742 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -682,7 +682,7 @@ int write_cryptpacket_id(Messenger *m, int friendnumber, uint8_t packet_id, uint /* returns valid ip port of connected friend on success * returns zeroed out IP_Port on failure */ -static IP_Port get_friend_ipport(Messenger *m, int friendnumber) +IP_Port get_friend_ipport(Messenger *m, int friendnumber) { IP_Port zero; memset(&zero, 0, sizeof(zero)); @@ -1266,6 +1266,26 @@ static int handle_filecontrol(Messenger *m, int friendnumber, uint8_t send_recei /**************************************/ +/* Set the callback for msi packets. + * + * Function(Messenger *m, int friendnumber, uint8_t *data, uint16_t length, void *userdata) + */ +void m_callback_msi_packet(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void *), + void *userdata) +{ + m->msi_packet = function; + m->msi_packet_userdata = userdata; +} + +/* Send an msi packet. + * + * return 1 on success + * return 0 on failure + */ +int m_msi_packet(Messenger *m, int friendnumber, uint8_t *data, uint16_t length) +{ + return write_cryptpacket_id(m, friendnumber, PACKET_ID_MSI, data, length); +} /* Send a LAN discovery packet every LAN_DISCOVERY_INTERVAL seconds. */ static void LANdiscovery(Messenger *m) @@ -1419,11 +1439,12 @@ void doFriends(Messenger *m) } len = read_cryptpacket(m->net_crypto, m->friendlist[i].crypt_connection_id, temp); - uint8_t packet_id = temp[0]; - uint8_t *data = temp + 1; - uint32_t data_length = len - 1; if (len > 0) { + uint8_t packet_id = temp[0]; + uint8_t *data = temp + 1; + uint32_t data_length = len - 1; + switch (packet_id) { case PACKET_ID_PING: { m->friendlist[i].ping_lastrecv = temp_time; @@ -1508,6 +1529,7 @@ void doFriends(Messenger *m) if (m->friendlist[i].receives_read_receipts) { write_cryptpacket_id(m, i, PACKET_ID_RECEIPT, message_id, message_id_length); } + if (m->friend_action) (*m->friend_action)(m, i, action, action_length, m->friend_action_userdata); @@ -1611,6 +1633,14 @@ void doFriends(Messenger *m) break; } + case PACKET_ID_MSI: { + if (data_length == 0) + break; + + if (m->msi_packet) + (*m->msi_packet)(m, i, data, data_length, m->msi_packet_userdata); + } + default: { break; } diff --git a/toxcore/Messenger.h b/toxcore/Messenger.h index 65fa19a5..f03a7478 100644 --- a/toxcore/Messenger.h +++ b/toxcore/Messenger.h @@ -44,6 +44,7 @@ #define PACKET_ID_RECEIPT 65 #define PACKET_ID_MESSAGE 64 #define PACKET_ID_ACTION 63 +#define PACKET_ID_MSI 69 #define PACKET_ID_FILE_SENDREQUEST 80 #define PACKET_ID_FILE_CONTROL 81 #define PACKET_ID_FILE_DATA 82 @@ -198,6 +199,9 @@ typedef struct Messenger { void (*file_filedata)(struct Messenger *m, int, uint8_t, uint8_t *, uint16_t length, void *); void *file_filedata_userdata; + void (*msi_packet)(struct Messenger *m, int, uint8_t *, uint16_t, void *); + void *msi_packet_userdata; + } Messenger; /* Format: [client_id (32 bytes)][nospam number (4 bytes)][checksum (2 bytes)] @@ -327,6 +331,11 @@ uint16_t getself_name(Messenger *m, uint8_t *name, uint16_t nlen); */ int getname(Messenger *m, int friendnumber, uint8_t *name); +/* returns valid ip port of connected friend on success + * returns zeroed out IP_Port on failure + */ +IP_Port get_friend_ipport(Messenger *m, int friendnumber); + /* Set our user status. * You are responsible for freeing status after. * @@ -547,7 +556,23 @@ int file_data(Messenger *m, int friendnumber, uint8_t filenumber, uint8_t *data, */ uint64_t file_dataremaining(Messenger *m, int friendnumber, uint8_t filenumber, uint8_t send_receive); -/*********************************/ +/*************** A/V related ******************/ + +/* Set the callback for msi packets. + * + * Function(Messenger *m, int friendnumber, uint8_t *data, uint16_t length, void *userdata) + */ +void m_callback_msi_packet(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void *), + void *userdata); + +/* Send an msi packet. + * + * return 1 on success + * return 0 on failure + */ +int m_msi_packet(Messenger *m, int friendnumber, uint8_t *data, uint16_t length); + +/**********************************************/ /* Run this at startup. * return allocated instance of Messenger on success.