Skip to content

Commit

Permalink
Added custom packet functions to public tox api.
Browse files Browse the repository at this point in the history
This should make it easy for people to use toxcore to power many
types of networked applications.
  • Loading branch information
irungentoo committed Sep 6, 2014
1 parent 06eff4b commit df7a627
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 0 deletions.
9 changes: 9 additions & 0 deletions toxcore/Messenger.c
Original file line number Diff line number Diff line change
Expand Up @@ -1773,6 +1773,15 @@ int send_custom_lossless_packet(const Messenger *m, int32_t friendnumber, const
if (friend_not_valid(m, friendnumber))
return -1;

if (length == 0)
return -1;

if (data[0] < PACKET_ID_LOSSLESS_RANGE_START)
return -1;

if (data[0] >= (PACKET_ID_LOSSLESS_RANGE_START + PACKET_ID_LOSSLESS_RANGE_SIZE))
return -1;

if (m->friendlist[friendnumber].status != FRIEND_ONLINE)
return -1;

Expand Down
71 changes: 71 additions & 0 deletions toxcore/tox.c
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,77 @@ void tox_get_keys(Tox *tox, uint8_t *public_key, uint8_t *secret_key)
memcpy(secret_key, m->net_crypto->self_secret_key, crypto_box_SECRETKEYBYTES);
}

/* Set handlers for custom lossy packets.
* Set the function to be called when friend sends us a lossy packet starting with byte.
* byte must be in the 200-254 range.
*
* NOTE: lossy packets behave like UDP packets meaning they might never reach the other side
* or might arrive more than once (if someone is messing with the connection) or might arrive
* in the wrong order.
*
* Unless latency is an issue, it is recommended that you use lossless packets instead.
*
* return -1 on failure.
* return 0 on success.
*/
int tox_lossy_packet_registerhandler(Tox *tox, int32_t friendnumber, uint8_t byte,
int (*packet_handler_callback)(void *object, const uint8_t *data, uint32_t len), void *object)
{
Messenger *m = tox;

if (byte < (PACKET_ID_LOSSY_RANGE_START + 8)) /* First 8 reserved for A/V*/
return -1;

return custom_lossy_packet_registerhandler(m, friendnumber, byte, packet_handler_callback, object);
}

/* Function to send custom lossy packets.
* First byte of data must be in the range: 200-254.
*
* return -1 on failure.
* return 0 on success.
*/
int tox_send_lossy_packet(const Tox *tox, int32_t friendnumber, const uint8_t *data, uint32_t length)
{
const Messenger *m = tox;

if (length == 0)
return -1;

if (data[0] < (PACKET_ID_LOSSY_RANGE_START + 8)) /* First 8 reserved for A/V*/
return -1;

return send_custom_lossy_packet(m, friendnumber, data, length);
}

/* Set handlers for custom lossless packets.
* Set the function to be called when friend sends us a lossless packet starting with byte.
* byte must be in the 160-191 range.
*
* return -1 on failure.
* return 0 on success.
*/
int tox_lossless_packet_registerhandler(Tox *tox, int32_t friendnumber, uint8_t byte,
int (*packet_handler_callback)(void *object, const uint8_t *data, uint32_t len), void *object)
{
Messenger *m = tox;

return custom_lossless_packet_registerhandler(m, friendnumber, byte, packet_handler_callback, object);
}

/* Function to send custom lossless packets.
* First byte of data must be in the range: 160-191.
*
* return -1 on failure.
* return 0 on success.
*/
int tox_send_lossless_packet(const Tox *tox, int32_t friendnumber, const uint8_t *data, uint32_t length)
{
const Messenger *m = tox;

return send_custom_lossless_packet(m, friendnumber, data, length);
}

/**********GROUP CHAT FUNCTIONS: WARNING Group chats will be rewritten so this might change ************/

/* Set the callback for group invites.
Expand Down
47 changes: 47 additions & 0 deletions toxcore/tox.h
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,53 @@ void tox_set_nospam(Tox *tox, uint32_t nospam);
if the pointer is NULL, no data will be copied to it.*/
void tox_get_keys(Tox *tox, uint8_t *public_key, uint8_t *secret_key);

/* Maximum size of custom packets. */
#define TOX_MAX_CUSTOM_PACKET_SIZE 1373

/* Set handlers for custom lossy packets.
* Set the function to be called when friend sends us a lossy packet starting with byte.
* byte must be in the 200-254 range.
*
* NOTE: lossy packets behave like UDP packets meaning they might never reach the other side
* or might arrive more than once (if someone is messing with the connection) or might arrive
* in the wrong order.
*
* Unless latency is an issue, it is recommended that you use lossless packets instead.
*
* return -1 on failure.
* return 0 on success.
*/
int tox_lossy_packet_registerhandler(Tox *tox, int32_t friendnumber, uint8_t byte,
int (*packet_handler_callback)(void *object, const uint8_t *data, uint32_t len), void *object);

/* Function to send custom lossy packets.
* First byte of data must be in the range: 200-254.
*
* return -1 on failure.
* return 0 on success.
*/
int tox_send_lossy_packet(const Tox *tox, int32_t friendnumber, const uint8_t *data, uint32_t length);

/* Set handlers for custom lossless packets.
* Set the function to be called when friend sends us a lossless packet starting with byte.
* byte must be in the 160-191 range.
*
* Lossless packets behave kind of like TCP (reliability, arrive in order.) but with packets instead of a stream.
*
* return -1 on failure.
* return 0 on success.
*/
int tox_lossless_packet_registerhandler(Tox *tox, int32_t friendnumber, uint8_t byte,
int (*packet_handler_callback)(void *object, const uint8_t *data, uint32_t len), void *object);

/* Function to send custom lossless packets.
* First byte of data must be in the range: 160-191.
*
* return -1 on failure.
* return 0 on success.
*/
int tox_send_lossless_packet(const Tox *tox, int32_t friendnumber, const uint8_t *data, uint32_t length);

/**********GROUP CHAT FUNCTIONS: WARNING Group chats will be rewritten so this might change ************/

/* Set the callback for group invites.
Expand Down

0 comments on commit df7a627

Please sign in to comment.