From cabc5add792a83b82aa5fe76011372f1f3677672 Mon Sep 17 00:00:00 2001 From: irungentoo Date: Thu, 22 Aug 2013 18:40:29 -0400 Subject: New api proposal. --- core/tox.h | 267 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 267 insertions(+) create mode 100644 core/tox.h (limited to 'core') diff --git a/core/tox.h b/core/tox.h new file mode 100644 index 00000000..2ad1ae2d --- /dev/null +++ b/core/tox.h @@ -0,0 +1,267 @@ +/* tox.h + * + * The Tox public API. + * + * Copyright (C) 2013 Tox project All Rights Reserved. + * + * This file is part of Tox. + * + * Tox is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Tox is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Tox. If not, see . + * + */ + +#ifndef TOX_H +#define TOX_H + + +#ifdef __cplusplus +extern "C" { +#endif + +#define MAX_NAME_LENGTH 128 +#define MAX_STATUSMESSAGE_LENGTH 128 + +#define FRIEND_ADDRESS_SIZE (crypto_box_PUBLICKEYBYTES + sizeof(uint32_t) + sizeof(uint16_t)) + +/* status definitions */ +#define FRIEND_ONLINE 4 +#define FRIEND_CONFIRMED 3 +#define FRIEND_REQUESTED 2 +#define FRIEND_ADDED 1 +#define NOFRIEND 0 + +/* errors for m_addfriend + * FAERR - Friend Add Error */ +#define FAERR_TOOLONG -1 +#define FAERR_NOMESSAGE -2 +#define FAERR_OWNKEY -3 +#define FAERR_ALREADYSENT -4 +#define FAERR_UNKNOWN -5 +#define FAERR_BADCHECKSUM -6 +#define FAERR_SETNEWNOSPAM -7 +#define FAERR_NOMEM -8 + +/* USERSTATUS + * Represents userstatuses someone can have. */ + +typedef enum { + USERSTATUS_NONE, + USERSTATUS_AWAY, + USERSTATUS_BUSY, + USERSTATUS_INVALID +} +USERSTATUS; + +/* + * returns a FRIEND_ADDRESS_SIZE byte address to give to others. + * format: [client_id (32 bytes)][nospam number (4 bytes)][checksum (2 bytes)] + * + */ +void tox_getaddress(void *tox, uint8_t *address); + +/* + * add a friend + * set the data that will be sent along with friend request + * address is the address of the friend (returned by getaddress of the friend you wish to add) it must be FRIEND_ADDRESS_SIZE bytes. TODO: add checksum. + * data is the data and length is the length + * returns the friend number if success + * return -1 if message length is too long + * return -2 if no message (message length must be >= 1 byte) + * return -3 if user's own key + * return -4 if friend request already sent or already a friend + * return -5 for unknown error + * return -6 if bad checksum in address + * return -7 if the friend was already there but the nospam was different + * (the nospam for that friend was set to the new one) + * return -8 if increasing the friend list size fails + */ +int tox_addfriend(void *tox, uint8_t *address, uint8_t *data, uint16_t length); + + +/* add a friend without sending a friendrequest. + returns the friend number if success + return -1 if failure. */ +int tox_addfriend_norequest(void *tox, uint8_t *client_id); + +/* return the friend id associated to that client id. + return -1 if no such friend */ +int tox_getfriend_id(void *tox, uint8_t *client_id); + +/* copies the public key associated to that friend id into client_id buffer. + make sure that client_id is of size CLIENT_ID_SIZE. + return 0 if success + return -1 if failure */ +int tox_getclient_id(void *tox, int friend_id, uint8_t *client_id); + +/* remove a friend */ +int tox_delfriend(void *tox, int friendnumber); + +/* return 4 if friend is online + return 3 if friend is confirmed + return 2 if the friend request was sent + return 1 if the friend was added + return 0 if there is no friend with that number */ +int tox_friendstatus(void *tox, int friendnumber); + +/* send a text chat message to an online friend + returns the message id if packet was successfully put into the send queue + return 0 if it was not + you will want to retain the return value, it will be passed to your read receipt callback + if one is received. + m_sendmessage_withid will send a message with the id of your choosing, + however we can generate an id for you by calling plain m_sendmessage. */ +uint32_t tox_sendmessage(void *tox, int friendnumber, uint8_t *message, uint32_t length); +uint32_t tox_sendmessage_withid(void *tox, int friendnumber, uint32_t theid, uint8_t *message, uint32_t length); + +/* send an action to an online friend + returns 1 if packet was successfully put into the send queue + return 0 if it was not */ +int tox_sendaction(void *tox, int friendnumber, uint8_t *action, uint32_t length); + +/* Set our nickname + name must be a string of maximum MAX_NAME_LENGTH length. + length must be at least 1 byte + length is the length of name with the NULL terminator + return 0 if success + return -1 if failure */ +int tox_setname(void *tox, uint8_t *name, uint16_t length); + +/* + Get your nickname. + m The messanger context to use. + name Pointer to a string for the name. + nlen The length of the string buffer. + returns Return the length of the name, 0 on error. +*/ +uint16_t tox_getselfname(void *tox, uint8_t *name, uint16_t nlen); + +/* get name of friendnumber + put it in name + name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH (128) bytes. + return 0 if success + return -1 if failure */ +int tox_getname(void *tox, int friendnumber, uint8_t *name); + +/* set our user status + you are responsible for freeing status after + returns 0 on success, -1 on failure */ +int tox_set_statusmessage(void *tox, uint8_t *status, uint16_t length); +int tox_set_userstatus(void *tox, USERSTATUS status); + +/* return the length of friendnumber's status message, + including null + pass it into malloc */ +int tox_get_statusmessage_size(void *tox, int friendnumber); + +/* copy friendnumber's status message into buf, truncating if size is over maxlen + get the size you need to allocate from m_get_statusmessage_size + The self variant will copy our own status message. */ +int tox_copy_statusmessage(void *tox, int friendnumber, uint8_t *buf, uint32_t maxlen); +int tox_copy_self_statusmessage(void *tox, uint8_t *buf, uint32_t maxlen); + +/* Return one of USERSTATUS values. + * Values unknown to your application should be represented as USERSTATUS_NONE. + * As above, the self variant will return our own USERSTATUS. + * If friendnumber is invalid, this shall return USERSTATUS_INVALID. */ +USERSTATUS tox_get_userstatus(void *tox, int friendnumber); +USERSTATUS tox_get_selfuserstatus(void *tox); + +/* Sets whether we send read receipts for friendnumber. + * This function is not lazy, and it will fail if yesno is not (0 or 1).*/ +void tox_set_sends_receipts(void *tox, int friendnumber, int yesno); + +/* set the function that will be executed when a friend request is received. + function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) */ +void tox_callback_friendrequest(void *tox, void (*function)(uint8_t *, uint8_t *, uint16_t, void *), void *userdata); + +/* set the function that will be executed when a message from a friend is received. + function format is: function(int friendnumber, uint8_t * message, uint32_t length) */ +void tox_callback_friendmessage(void *tox, void (*function)(void *tox, int, uint8_t *, uint16_t, void *), + void *userdata); + +/* set the function that will be executed when an action from a friend is received. + function format is: function(int friendnumber, uint8_t * action, uint32_t length) */ +void tox_callback_action(void *tox, void (*function)(void *tox, int, uint8_t *, uint16_t, void *), void *userdata); + +/* set the callback for name changes + function(int friendnumber, uint8_t *newname, uint16_t length) + you are not responsible for freeing newname */ +void tox_callback_namechange(void *tox, void (*function)(void *tox, int, uint8_t *, uint16_t, void *), + void *userdata); + +/* set the callback for status message changes + function(int friendnumber, uint8_t *newstatus, uint16_t length) + you are not responsible for freeing newstatus */ +void tox_callback_statusmessage(void *tox, void (*function)(void *tox, int, uint8_t *, uint16_t, void *), + void *userdata); + +/* set the callback for status type changes + function(int friendnumber, USERSTATUS kind) */ +void tox_callback_userstatus(void *tox, void (*function)(void *tox, int, USERSTATUS, void *), void *userdata); + +/* set the callback for read receipts + function(int friendnumber, uint32_t receipt) + if you are keeping a record of returns from m_sendmessage, + receipt might be one of those values, and that means the message + has been received on the other side. since core doesn't + track ids for you, receipt may not correspond to any message + in that case, you should discard it. */ +void tox_callback_read_receipt(void *tox, void (*function)(void *tox, int, uint32_t, void *), void *userdata); + +/* set the callback for connection status changes + function(int friendnumber, uint8_t status) + status: + 0 -- friend went offline after being previously online + 1 -- friend went online + note that this callback is not called when adding friends, thus the "after + being previously online" part. it's assumed that when adding friends, + their connection status is offline. */ +void tox_callback_connectionstatus(void *tox, void (*function)(void *tox, int, uint8_t, void *), void *userdata); + +/* Use this function to bootstrap the client + Sends a get nodes request to the given node with ip port and public_key */ +void tox_bootstrap(void *tox, IP_Port ip_port, uint8_t *public_key); + +/* returns 0 if we are not connected to the DHT + returns 1 if we are */ +int tox_isconnected(void *tox); + +/* run this at startup + * returns allocated instance of tox on success + * returns 0 if there are problems */ +void *tox_new(void); + +/* run this before closing shop + * free all datastructures */ +void tox_kill(void *tox); + +/* the main loop that needs to be run at least 20 times per second */ +void tox_do(void *tox); + +/* SAVING AND LOADING FUNCTIONS: */ + +/* returns the size of the messenger data (for saving) */ +uint32_t tox_size(void *tox); + +/* save the messenger in data (must be allocated memory of size Messenger_size()) */ +void tox_save(void *tox, uint8_t *data); + +/* load the messenger from data of size length */ +int tox_load(void *tox, uint8_t *data, uint32_t length); + +#ifdef __cplusplus +} +#endif + +#endif -- cgit v1.2.3 From 733066a57e8d5403c91475fb6b095f44a81bf270 Mon Sep 17 00:00:00 2001 From: irungentoo Date: Fri, 23 Aug 2013 06:31:19 -0400 Subject: Added TOX prefix to enum. --- core/tox.h | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'core') diff --git a/core/tox.h b/core/tox.h index 2ad1ae2d..3dc86855 100644 --- a/core/tox.h +++ b/core/tox.h @@ -56,12 +56,12 @@ extern "C" { * Represents userstatuses someone can have. */ typedef enum { - USERSTATUS_NONE, - USERSTATUS_AWAY, - USERSTATUS_BUSY, - USERSTATUS_INVALID + TOX_USERSTATUS_NONE, + TOX_USERSTATUS_AWAY, + TOX_USERSTATUS_BUSY, + TOX_USERSTATUS_INVALID } -USERSTATUS; +TOX_USERSTATUS; /* * returns a FRIEND_ADDRESS_SIZE byte address to give to others. @@ -174,8 +174,8 @@ int tox_copy_self_statusmessage(void *tox, uint8_t *buf, uint32_t maxlen); * Values unknown to your application should be represented as USERSTATUS_NONE. * As above, the self variant will return our own USERSTATUS. * If friendnumber is invalid, this shall return USERSTATUS_INVALID. */ -USERSTATUS tox_get_userstatus(void *tox, int friendnumber); -USERSTATUS tox_get_selfuserstatus(void *tox); +TOX_USERSTATUS tox_get_userstatus(void *tox, int friendnumber); +TOX_USERSTATUS tox_get_selfuserstatus(void *tox); /* Sets whether we send read receipts for friendnumber. * This function is not lazy, and it will fail if yesno is not (0 or 1).*/ -- cgit v1.2.3 From 9d3a8d94f21b584e199fbe04505cf0b259218ac8 Mon Sep 17 00:00:00 2001 From: irungentoo Date: Fri, 23 Aug 2013 10:26:52 -0400 Subject: New API done and tested. Some stuff needs to be cleaned a bit though. --- core/network.c | 43 +----- core/network.h | 10 -- core/tox.h | 146 ++++++++++++--------- .../bootstrap_serverdaemon/DHT_bootstrap_daemon.c | 41 ++++++ testing/nTox.c | 98 +++++++------- testing/nTox.h | 46 ++++++- 6 files changed, 219 insertions(+), 165 deletions(-) (limited to 'core') diff --git a/core/network.c b/core/network.c index 7880eae6..2bcf7d61 100644 --- a/core/network.c +++ b/core/network.c @@ -144,8 +144,9 @@ static void at_shutdown(void) returns NULL if there are problems */ Networking_Core *new_networking(IP ip, uint16_t port) { - if(at_startup() != 0) + if (at_startup() != 0) return NULL; + /* initialize our socket */ Networking_Core *temp = calloc(1, sizeof(Networking_Core)); @@ -215,43 +216,3 @@ void kill_networking(Networking_Core *net) free(net); return; } - -/* - resolve_addr(): - address should represent IPv4 or a hostname with A record - - returns a data in network byte order that can be used to set IP.i or IP_Port.ip.i - returns 0 on failure - - TODO: Fix ipv6 support -*/ -uint32_t resolve_addr(const char *address) -{ - struct addrinfo *server = NULL; - struct addrinfo hints; - int rc; - uint32_t addr; - - memset(&hints, 0, sizeof(hints)); - hints.ai_family = AF_INET; // IPv4 only right now. - hints.ai_socktype = SOCK_DGRAM; // type of socket Tox uses. - - rc = getaddrinfo(address, "echo", &hints, &server); - - // Lookup failed. - if (rc != 0) { - return 0; - } - - // IPv4 records only.. - if (server->ai_family != AF_INET) { - freeaddrinfo(server); - return 0; - } - - - addr = ((struct sockaddr_in *)server->ai_addr)->sin_addr.s_addr; - - freeaddrinfo(server); - return addr; -} diff --git a/core/network.h b/core/network.h index 088bbb3b..3547f79b 100644 --- a/core/network.h +++ b/core/network.h @@ -151,16 +151,6 @@ Networking_Core *new_networking(IP ip, uint16_t port); /* function to cleanup networking stuff(doesn't do much right now) */ void kill_networking(Networking_Core *net); -/* - resolve_addr(): - address should represent IPv4 or a hostname with A record - - returns a data in network byte order that can be used to set IP.i or IP_Port.ip.i - returns 0 on failure - - TODO: Fix ipv6 support -*/ -uint32_t resolve_addr(const char *address); #ifdef __cplusplus } diff --git a/core/tox.h b/core/tox.h index 3dc86855..d5e61240 100644 --- a/core/tox.h +++ b/core/tox.h @@ -24,34 +24,53 @@ #ifndef TOX_H #define TOX_H +#include #ifdef __cplusplus extern "C" { #endif -#define MAX_NAME_LENGTH 128 -#define MAX_STATUSMESSAGE_LENGTH 128 +#define TOX_MAX_NAME_LENGTH 128 +#define TOX_MAX_STATUSMESSAGE_LENGTH 128 +#define TOX_CLIENT_ID_SIZE 32 -#define FRIEND_ADDRESS_SIZE (crypto_box_PUBLICKEYBYTES + sizeof(uint32_t) + sizeof(uint16_t)) +#define TOX_FRIEND_ADDRESS_SIZE (TOX_CLIENT_ID_SIZE + sizeof(uint32_t) + sizeof(uint16_t)) + + +typedef union { + uint8_t c[4]; + uint16_t s[2]; + uint32_t i; +} IP; + +typedef struct { + IP ip; + uint16_t port; + /* not used for anything right now */ + uint16_t padding; +} IP_Port; /* status definitions */ -#define FRIEND_ONLINE 4 -#define FRIEND_CONFIRMED 3 -#define FRIEND_REQUESTED 2 -#define FRIEND_ADDED 1 -#define NOFRIEND 0 +enum { + TOX_NOFRIEND, + TOX_FRIEND_ADDED, + TOX_FRIEND_REQUESTED, + TOX_FRIEND_CONFIRMED, + TOX_FRIEND_ONLINE, +}; /* errors for m_addfriend * FAERR - Friend Add Error */ -#define FAERR_TOOLONG -1 -#define FAERR_NOMESSAGE -2 -#define FAERR_OWNKEY -3 -#define FAERR_ALREADYSENT -4 -#define FAERR_UNKNOWN -5 -#define FAERR_BADCHECKSUM -6 -#define FAERR_SETNEWNOSPAM -7 -#define FAERR_NOMEM -8 - +enum { + TOX_FAERR_TOOLONG = -1, + TOX_FAERR_NOMESSAGE = -2, + TOX_FAERR_OWNKEY = -3, + TOX_FAERR_ALREADYSENT = -4, + TOX_FAERR_UNKNOWN = -5, + TOX_FAERR_BADCHECKSUM = -6, + TOX_FAERR_SETNEWNOSPAM = -7, + TOX_FAERR_NOMEM = -8 +}; /* USERSTATUS * Represents userstatuses someone can have. */ @@ -63,12 +82,14 @@ typedef enum { } TOX_USERSTATUS; +typedef void Tox; + /* * returns a FRIEND_ADDRESS_SIZE byte address to give to others. * format: [client_id (32 bytes)][nospam number (4 bytes)][checksum (2 bytes)] * */ -void tox_getaddress(void *tox, uint8_t *address); +void tox_getaddress(Tox *tox, uint8_t *address); /* * add a friend @@ -76,43 +97,43 @@ void tox_getaddress(void *tox, uint8_t *address); * address is the address of the friend (returned by getaddress of the friend you wish to add) it must be FRIEND_ADDRESS_SIZE bytes. TODO: add checksum. * data is the data and length is the length * returns the friend number if success - * return -1 if message length is too long - * return -2 if no message (message length must be >= 1 byte) - * return -3 if user's own key - * return -4 if friend request already sent or already a friend - * return -5 for unknown error - * return -6 if bad checksum in address - * return -7 if the friend was already there but the nospam was different + * return FA_TOOLONG if message length is too long + * return FAERR_NOMESSAGE if no message (message length must be >= 1 byte) + * return FAERR_OWNKEY if user's own key + * return FAERR_ALREADYSENT if friend request already sent or already a friend + * return FAERR_UNKNOWN for unknown error + * return FAERR_BADCHECKSUM if bad checksum in address + * return FAERR_SETNEWNOSPAM if the friend was already there but the nospam was different * (the nospam for that friend was set to the new one) - * return -8 if increasing the friend list size fails + * return FAERR_NOMEM if increasing the friend list size fails */ -int tox_addfriend(void *tox, uint8_t *address, uint8_t *data, uint16_t length); +int tox_addfriend(Tox *tox, uint8_t *address, uint8_t *data, uint16_t length); /* add a friend without sending a friendrequest. returns the friend number if success return -1 if failure. */ -int tox_addfriend_norequest(void *tox, uint8_t *client_id); +int tox_addfriend_norequest(Tox *tox, uint8_t *client_id); /* return the friend id associated to that client id. return -1 if no such friend */ -int tox_getfriend_id(void *tox, uint8_t *client_id); +int tox_getfriend_id(Tox *tox, uint8_t *client_id); /* copies the public key associated to that friend id into client_id buffer. make sure that client_id is of size CLIENT_ID_SIZE. return 0 if success return -1 if failure */ -int tox_getclient_id(void *tox, int friend_id, uint8_t *client_id); +int tox_getclient_id(Tox *tox, int friend_id, uint8_t *client_id); /* remove a friend */ -int tox_delfriend(void *tox, int friendnumber); +int tox_delfriend(Tox *tox, int friendnumber); /* return 4 if friend is online return 3 if friend is confirmed return 2 if the friend request was sent return 1 if the friend was added return 0 if there is no friend with that number */ -int tox_friendstatus(void *tox, int friendnumber); +int tox_friendstatus(Tox *tox, int friendnumber); /* send a text chat message to an online friend returns the message id if packet was successfully put into the send queue @@ -121,13 +142,13 @@ int tox_friendstatus(void *tox, int friendnumber); if one is received. m_sendmessage_withid will send a message with the id of your choosing, however we can generate an id for you by calling plain m_sendmessage. */ -uint32_t tox_sendmessage(void *tox, int friendnumber, uint8_t *message, uint32_t length); -uint32_t tox_sendmessage_withid(void *tox, int friendnumber, uint32_t theid, uint8_t *message, uint32_t length); +uint32_t tox_sendmessage(Tox *tox, int friendnumber, uint8_t *message, uint32_t length); +uint32_t tox_sendmessage_withid(Tox *tox, int friendnumber, uint32_t theid, uint8_t *message, uint32_t length); /* send an action to an online friend returns 1 if packet was successfully put into the send queue return 0 if it was not */ -int tox_sendaction(void *tox, int friendnumber, uint8_t *action, uint32_t length); +int tox_sendaction(Tox *tox, int friendnumber, uint8_t *action, uint32_t length); /* Set our nickname name must be a string of maximum MAX_NAME_LENGTH length. @@ -135,7 +156,7 @@ int tox_sendaction(void *tox, int friendnumber, uint8_t *action, uint32_t length length is the length of name with the NULL terminator return 0 if success return -1 if failure */ -int tox_setname(void *tox, uint8_t *name, uint16_t length); +int tox_setname(Tox *tox, uint8_t *name, uint16_t length); /* Get your nickname. @@ -144,71 +165,71 @@ int tox_setname(void *tox, uint8_t *name, uint16_t length); nlen The length of the string buffer. returns Return the length of the name, 0 on error. */ -uint16_t tox_getselfname(void *tox, uint8_t *name, uint16_t nlen); +uint16_t tox_getselfname(Tox *tox, uint8_t *name, uint16_t nlen); /* get name of friendnumber put it in name name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH (128) bytes. return 0 if success return -1 if failure */ -int tox_getname(void *tox, int friendnumber, uint8_t *name); +int tox_getname(Tox *tox, int friendnumber, uint8_t *name); /* set our user status you are responsible for freeing status after returns 0 on success, -1 on failure */ -int tox_set_statusmessage(void *tox, uint8_t *status, uint16_t length); -int tox_set_userstatus(void *tox, USERSTATUS status); +int tox_set_statusmessage(Tox *tox, uint8_t *status, uint16_t length); +int tox_set_userstatus(Tox *tox, TOX_USERSTATUS status); /* return the length of friendnumber's status message, including null pass it into malloc */ -int tox_get_statusmessage_size(void *tox, int friendnumber); +int tox_get_statusmessage_size(Tox *tox, int friendnumber); /* copy friendnumber's status message into buf, truncating if size is over maxlen get the size you need to allocate from m_get_statusmessage_size The self variant will copy our own status message. */ -int tox_copy_statusmessage(void *tox, int friendnumber, uint8_t *buf, uint32_t maxlen); -int tox_copy_self_statusmessage(void *tox, uint8_t *buf, uint32_t maxlen); +int tox_copy_statusmessage(Tox *tox, int friendnumber, uint8_t *buf, uint32_t maxlen); +int tox_copy_self_statusmessage(Tox *tox, uint8_t *buf, uint32_t maxlen); /* Return one of USERSTATUS values. * Values unknown to your application should be represented as USERSTATUS_NONE. * As above, the self variant will return our own USERSTATUS. * If friendnumber is invalid, this shall return USERSTATUS_INVALID. */ -TOX_USERSTATUS tox_get_userstatus(void *tox, int friendnumber); -TOX_USERSTATUS tox_get_selfuserstatus(void *tox); +TOX_USERSTATUS tox_get_userstatus(Tox *tox, int friendnumber); +TOX_USERSTATUS tox_get_selfuserstatus(Tox *tox); /* Sets whether we send read receipts for friendnumber. * This function is not lazy, and it will fail if yesno is not (0 or 1).*/ -void tox_set_sends_receipts(void *tox, int friendnumber, int yesno); +void tox_set_sends_receipts(Tox *tox, int friendnumber, int yesno); /* set the function that will be executed when a friend request is received. function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) */ -void tox_callback_friendrequest(void *tox, void (*function)(uint8_t *, uint8_t *, uint16_t, void *), void *userdata); +void tox_callback_friendrequest(Tox *tox, void (*function)(uint8_t *, uint8_t *, uint16_t, void *), void *userdata); /* set the function that will be executed when a message from a friend is received. function format is: function(int friendnumber, uint8_t * message, uint32_t length) */ -void tox_callback_friendmessage(void *tox, void (*function)(void *tox, int, uint8_t *, uint16_t, void *), +void tox_callback_friendmessage(Tox *tox, void (*function)(Tox *tox, int, uint8_t *, uint16_t, void *), void *userdata); /* set the function that will be executed when an action from a friend is received. function format is: function(int friendnumber, uint8_t * action, uint32_t length) */ -void tox_callback_action(void *tox, void (*function)(void *tox, int, uint8_t *, uint16_t, void *), void *userdata); +void tox_callback_action(Tox *tox, void (*function)(Tox *tox, int, uint8_t *, uint16_t, void *), void *userdata); /* set the callback for name changes function(int friendnumber, uint8_t *newname, uint16_t length) you are not responsible for freeing newname */ -void tox_callback_namechange(void *tox, void (*function)(void *tox, int, uint8_t *, uint16_t, void *), +void tox_callback_namechange(Tox *tox, void (*function)(Tox *tox, int, uint8_t *, uint16_t, void *), void *userdata); /* set the callback for status message changes function(int friendnumber, uint8_t *newstatus, uint16_t length) you are not responsible for freeing newstatus */ -void tox_callback_statusmessage(void *tox, void (*function)(void *tox, int, uint8_t *, uint16_t, void *), +void tox_callback_statusmessage(Tox *tox, void (*function)(Tox *tox, int, uint8_t *, uint16_t, void *), void *userdata); /* set the callback for status type changes function(int friendnumber, USERSTATUS kind) */ -void tox_callback_userstatus(void *tox, void (*function)(void *tox, int, USERSTATUS, void *), void *userdata); +void tox_callback_userstatus(Tox *tox, void (*function)(Tox *tox, int, TOX_USERSTATUS, void *), void *userdata); /* set the callback for read receipts function(int friendnumber, uint32_t receipt) @@ -217,7 +238,7 @@ void tox_callback_userstatus(void *tox, void (*function)(void *tox, int, USERSTA has been received on the other side. since core doesn't track ids for you, receipt may not correspond to any message in that case, you should discard it. */ -void tox_callback_read_receipt(void *tox, void (*function)(void *tox, int, uint32_t, void *), void *userdata); +void tox_callback_read_receipt(Tox *tox, void (*function)(Tox *tox, int, uint32_t, void *), void *userdata); /* set the callback for connection status changes function(int friendnumber, uint8_t status) @@ -227,38 +248,39 @@ void tox_callback_read_receipt(void *tox, void (*function)(void *tox, int, uint3 note that this callback is not called when adding friends, thus the "after being previously online" part. it's assumed that when adding friends, their connection status is offline. */ -void tox_callback_connectionstatus(void *tox, void (*function)(void *tox, int, uint8_t, void *), void *userdata); +void tox_callback_connectionstatus(Tox *tox, void (*function)(Tox *tox, int, uint8_t, void *), void *userdata); /* Use this function to bootstrap the client Sends a get nodes request to the given node with ip port and public_key */ -void tox_bootstrap(void *tox, IP_Port ip_port, uint8_t *public_key); +void tox_bootstrap(Tox *tox, IP_Port ip_port, uint8_t *public_key); /* returns 0 if we are not connected to the DHT returns 1 if we are */ -int tox_isconnected(void *tox); +int tox_isconnected(Tox *tox); /* run this at startup * returns allocated instance of tox on success * returns 0 if there are problems */ -void *tox_new(void); +Tox *tox_new(void); /* run this before closing shop * free all datastructures */ -void tox_kill(void *tox); +void tox_kill(Tox *tox); /* the main loop that needs to be run at least 20 times per second */ -void tox_do(void *tox); +void tox_do(Tox *tox); /* SAVING AND LOADING FUNCTIONS: */ /* returns the size of the messenger data (for saving) */ -uint32_t tox_size(void *tox); +uint32_t tox_size(Tox *tox); /* save the messenger in data (must be allocated memory of size Messenger_size()) */ -void tox_save(void *tox, uint8_t *data); +void tox_save(Tox *tox, uint8_t *data); /* load the messenger from data of size length */ -int tox_load(void *tox, uint8_t *data, uint32_t length); +int tox_load(Tox *tox, uint8_t *data, uint32_t length); + #ifdef __cplusplus } diff --git a/other/bootstrap_serverdaemon/DHT_bootstrap_daemon.c b/other/bootstrap_serverdaemon/DHT_bootstrap_daemon.c index 7604b1e0..46409b76 100644 --- a/other/bootstrap_serverdaemon/DHT_bootstrap_daemon.c +++ b/other/bootstrap_serverdaemon/DHT_bootstrap_daemon.c @@ -72,6 +72,47 @@ int b16_to_key(char b16_string[], uint8_t *bs_pubkey) return 0; } +/* + resolve_addr(): + address should represent IPv4 or a hostname with A record + + returns a data in network byte order that can be used to set IP.i or IP_Port.ip.i + returns 0 on failure + + TODO: Fix ipv6 support +*/ + +uint32_t resolve_addr(const char *address) +{ + struct addrinfo *server = NULL; + struct addrinfo hints; + int rc; + uint32_t addr; + + memset(&hints, 0, sizeof(hints)); + hints.ai_family = AF_INET; // IPv4 only right now. + hints.ai_socktype = SOCK_DGRAM; // type of socket Tox uses. + + rc = getaddrinfo(address, "echo", &hints, &server); + + // Lookup failed. + if (rc != 0) { + return 0; + } + + // IPv4 records only.. + if (server->ai_family != AF_INET) { + freeaddrinfo(server); + return 0; + } + + + addr = ((struct sockaddr_in *)server->ai_addr)->sin_addr.s_addr; + + freeaddrinfo(server); + return addr; +} + /* This unction connects to all specified servers and connect to them. returns 1 if the connection to the DHT is up diff --git a/testing/nTox.c b/testing/nTox.c index a476cc19..87fec818 100644 --- a/testing/nTox.c +++ b/testing/nTox.c @@ -43,22 +43,22 @@ char *help = "[i] commands:\n/f ID (to add friend)\n/m friendnumber message " int x, y; typedef struct { - uint8_t id[CLIENT_ID_SIZE]; + uint8_t id[TOX_CLIENT_ID_SIZE]; uint8_t accepted; } Friend_request; Friend_request pending_requests[256]; uint8_t num_requests = 0; -void get_id(Messenger *m, char *data) +void get_id(Tox *m, char *data) { sprintf(data, "[i] ID: "); int offset = strlen(data); int i = 0; - uint8_t address[FRIEND_ADDRESS_SIZE]; - getaddress(m, address); + uint8_t address[TOX_FRIEND_ADDRESS_SIZE]; + tox_getaddress(m, address); - for (; i < FRIEND_ADDRESS_SIZE; i++) { + for (; i < TOX_FRIEND_ADDRESS_SIZE; i++) { sprintf(data + 2 * i + offset, "%02X ", address[i]); } } @@ -75,15 +75,15 @@ void new_lines(char *line) } -void print_friendlist(Messenger *m) +void print_friendlist(Tox *m) { - char name[MAX_NAME_LENGTH]; + char name[TOX_MAX_NAME_LENGTH]; int i = 0; new_lines("[i] Friend List:"); - while (getname(m, i, (uint8_t *)name) != -1) { + while (tox_getname(m, i, (uint8_t *)name) != -1) { /* account for the longest name and the longest "base" string */ - char fstring[MAX_NAME_LENGTH + strlen("[i] Friend: NULL\n\tid: ")]; + char fstring[TOX_MAX_NAME_LENGTH + strlen("[i] Friend: NULL\n\tid: ")]; if (strlen(name) <= 0) { sprintf(fstring, "[i] Friend: No Friend!\n\tid: %i", i); @@ -99,14 +99,14 @@ void print_friendlist(Messenger *m) new_lines("\tno friends! D:"); } -char *format_message(Messenger *m, char *message, int friendnum) +char *format_message(Tox *m, char *message, int friendnum) { - char name[MAX_NAME_LENGTH]; + char name[TOX_MAX_NAME_LENGTH]; if (friendnum != -1) { - getname(m, friendnum, (uint8_t *)name); + tox_getname(m, friendnum, (uint8_t *)name); } else { - getself_name(m, (uint8_t *)name, sizeof(name)); + tox_getselfname(m, (uint8_t *)name, sizeof(name)); } char *msg = malloc(100 + strlen(message) + strlen(name) + 1); @@ -129,7 +129,7 @@ char *format_message(Messenger *m, char *message, int friendnum) return msg; } -void line_eval(Messenger *m, char *line) +void line_eval(Tox *m, char *line) { if (line[0] == '/') { char inpt_command = line[1]; @@ -146,28 +146,28 @@ void line_eval(Messenger *m, char *line) temp_id[i] = line[i + prompt_offset]; unsigned char *bin_string = hex_string_to_bin(temp_id); - int num = m_addfriend(m, bin_string, (uint8_t *)"Install Gentoo", sizeof("Install Gentoo")); + int num = tox_addfriend(m, bin_string, (uint8_t *)"Install Gentoo", sizeof("Install Gentoo")); free(bin_string); char numstring[100]; switch (num) { - case FAERR_TOOLONG: + case TOX_FAERR_TOOLONG: sprintf(numstring, "[i] Message is too long."); break; - case FAERR_NOMESSAGE: + case TOX_FAERR_NOMESSAGE: sprintf(numstring, "[i] Please add a message to your request."); break; - case FAERR_OWNKEY: + case TOX_FAERR_OWNKEY: sprintf(numstring, "[i] That appears to be your own ID."); break; - case FAERR_ALREADYSENT: + case TOX_FAERR_ALREADYSENT: sprintf(numstring, "[i] Friend request already sent."); break; - case FAERR_UNKNOWN: + case TOX_FAERR_UNKNOWN: sprintf(numstring, "[i] Undefined error when adding friend."); break; @@ -179,7 +179,7 @@ void line_eval(Messenger *m, char *line) new_lines(numstring); do_refresh(); } else if (inpt_command == 'd') { - doMessenger(m); + tox_do(m); } else if (inpt_command == 'm') { //message command: /m friendnumber messsage size_t len = strlen(line); @@ -205,13 +205,13 @@ void line_eval(Messenger *m, char *line) int num = atoi(numstring); - if (m_sendmessage(m, num, (uint8_t *) message, strlen(message) + 1) != 1) { + if (tox_sendmessage(m, num, (uint8_t *) message, strlen(message) + 1) != 1) { new_lines("[i] could not send message"); } else { new_lines(format_message(m, message, -1)); } } else if (inpt_command == 'n') { - uint8_t name[MAX_NAME_LENGTH]; + uint8_t name[TOX_MAX_NAME_LENGTH]; int i = 0; size_t len = strlen(line); @@ -222,14 +222,14 @@ void line_eval(Messenger *m, char *line) } name[i - 3] = 0; - setname(m, name, i - 2); + tox_setname(m, name, i - 2); char numstring[100]; sprintf(numstring, "[i] changed nick to %s", (char *)name); new_lines(numstring); } else if (inpt_command == 'l') { print_friendlist(m); } else if (inpt_command == 's') { - uint8_t status[MAX_STATUSMESSAGE_LENGTH]; + uint8_t status[TOX_MAX_STATUSMESSAGE_LENGTH]; int i = 0; size_t len = strlen(line); @@ -240,7 +240,7 @@ void line_eval(Messenger *m, char *line) } status[i - 3] = 0; - m_set_statusmessage(m, status, strlen((char *)status) + 1); + tox_set_statusmessage(m, status, strlen((char *)status) + 1); char numstring[100]; sprintf(numstring, "[i] changed status to %s", (char *)status); new_lines(numstring); @@ -252,7 +252,7 @@ void line_eval(Messenger *m, char *line) sprintf(numchar, "[i] you either didn't receive that request or you already accepted it"); new_lines(numchar); } else { - int num = m_addfriend_norequest(m, pending_requests[numf].id); + int num = tox_addfriend_norequest(m, pending_requests[numf].id); if (num != -1) { pending_requests[numf].accepted = 1; @@ -364,40 +364,40 @@ void print_request(uint8_t *public_key, uint8_t *data, uint16_t length, void *us char numchar[100]; sprintf(numchar, "[i] accept request with /a %u", num_requests); new_lines(numchar); - memcpy(pending_requests[num_requests].id, public_key, CLIENT_ID_SIZE); + memcpy(pending_requests[num_requests].id, public_key, TOX_CLIENT_ID_SIZE); pending_requests[num_requests].accepted = 0; ++num_requests; do_refresh(); } -void print_message(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata) +void print_message(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata) { new_lines(format_message(m, (char *)string, friendnumber)); } -void print_nickchange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata) +void print_nickchange(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata) { - char name[MAX_NAME_LENGTH]; + char name[TOX_MAX_NAME_LENGTH]; - if (getname(m, friendnumber, (uint8_t *)name) != -1) { + if (tox_getname(m, friendnumber, (uint8_t *)name) != -1) { char msg[100 + length]; sprintf(msg, "[i] [%d] %s is now known as %s.", friendnumber, name, string); new_lines(msg); } } -void print_statuschange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata) +void print_statuschange(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata) { - char name[MAX_NAME_LENGTH]; + char name[TOX_MAX_NAME_LENGTH]; - if (getname(m, friendnumber, (uint8_t *)name) != -1) { + if (tox_getname(m, friendnumber, (uint8_t *)name) != -1) { char msg[100 + length + strlen(name) + 1]; sprintf(msg, "[i] [%d] %s's status changed to %s.", friendnumber, name, string); new_lines(msg); } } -void load_key(Messenger *m, char *path) +void load_key(Tox *m, char *path) { FILE *data_file = fopen(path, "r"); int size = 0; @@ -415,13 +415,13 @@ void load_key(Messenger *m, char *path) goto FILE_ERROR; } - Messenger_load(m, data, size); + tox_load(m, data, size); } else { //else save new keys - int size = Messenger_size(m); + int size = tox_size(m); uint8_t data[size]; - Messenger_save(m, data); + tox_save(m, data); data_file = fopen(path, "w"); if (!data_file) { @@ -463,7 +463,7 @@ int main(int argc, char *argv[]) int i = 0; char *filename = "data"; char idstring[200] = {0}; - Messenger *m; + Tox *m; if (argc < 4) { printf("[!] Usage: %s [IP] [port] [public_key] \n", argv[0]); @@ -487,7 +487,7 @@ int main(int argc, char *argv[]) } } - m = initMessenger(); + m = tox_new(); if ( !m ) { fputs("Failed to allocate Messenger datastructure", stderr); @@ -496,10 +496,10 @@ int main(int argc, char *argv[]) load_key(m, filename); - m_callback_friendrequest(m, print_request, NULL); - m_callback_friendmessage(m, print_message, NULL); - m_callback_namechange(m, print_nickchange, NULL); - m_callback_statusmessage(m, print_statuschange, NULL); + tox_callback_friendrequest(m, print_request, NULL); + tox_callback_friendmessage(m, print_message, NULL); + tox_callback_namechange(m, print_nickchange, NULL); + tox_callback_statusmessage(m, print_statuschange, NULL); initscr(); noecho(); @@ -521,17 +521,17 @@ int main(int argc, char *argv[]) exit(1); unsigned char *binary_string = hex_string_to_bin(argv[3]); - DHT_bootstrap(m->dht, bootstrap_ip_port, binary_string); + tox_bootstrap(m, bootstrap_ip_port, binary_string); free(binary_string); nodelay(stdscr, TRUE); while (true) { - if (on == 0 && DHT_isconnected(m->dht)) { + if (on == 0 && tox_isconnected(m)) { new_lines("[i] connected to DHT\n[i] define username with /n"); on = 1; } - doMessenger(m); + tox_do(m); c_sleep(1); do_refresh(); @@ -552,7 +552,7 @@ int main(int argc, char *argv[]) } } - cleanupMessenger(m); + tox_kill(m); endwin(); return 0; } diff --git a/testing/nTox.h b/testing/nTox.h index fdd88fb4..b27a956f 100644 --- a/testing/nTox.h +++ b/testing/nTox.h @@ -35,15 +35,55 @@ #include #include #include -#include "../core/Messenger.h" -#include "../core/network.h" +#include "../core/tox.h" #define STRING_LENGTH 256 #define HISTORY 50 #define PUB_KEY_BYTES 32 +/* + resolve_addr(): + address should represent IPv4 or a hostname with A record + + returns a data in network byte order that can be used to set IP.i or IP_Port.ip.i + returns 0 on failure + + TODO: Fix ipv6 support +*/ + +uint32_t resolve_addr(const char *address) +{ + struct addrinfo *server = NULL; + struct addrinfo hints; + int rc; + uint32_t addr; + + memset(&hints, 0, sizeof(hints)); + hints.ai_family = AF_INET; // IPv4 only right now. + hints.ai_socktype = SOCK_DGRAM; // type of socket Tox uses. + + rc = getaddrinfo(address, "echo", &hints, &server); + + // Lookup failed. + if (rc != 0) { + return 0; + } + + // IPv4 records only.. + if (server->ai_family != AF_INET) { + freeaddrinfo(server); + return 0; + } + + + addr = ((struct sockaddr_in *)server->ai_addr)->sin_addr.s_addr; + + freeaddrinfo(server); + return addr; +} + void new_lines(char *line); -void line_eval(Messenger *m, char *line); +void line_eval(Tox *m, char *line); void wrap(char output[STRING_LENGTH], char input[STRING_LENGTH], int line_width) ; int count_lines(char *string) ; char *appender(char *str, const char c); -- cgit v1.2.3 From 658bfab41ee4aaca7498830c8e6b62ba9923ebd7 Mon Sep 17 00:00:00 2001 From: irungentoo Date: Fri, 23 Aug 2013 10:29:13 -0400 Subject: Forgot to commit a file in last commit. --- core/CMakeLists.txt | 3 +- core/tox.c | 374 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 376 insertions(+), 1 deletion(-) create mode 100644 core/tox.c (limited to 'core') diff --git a/core/CMakeLists.txt b/core/CMakeLists.txt index 55a41912..abd15807 100644 --- a/core/CMakeLists.txt +++ b/core/CMakeLists.txt @@ -10,7 +10,8 @@ set(core_sources LAN_discovery.c Messenger.c util.c - ping.c) + ping.c + tox.c) if(SHARED_TOXCORE) add_library(toxcore SHARED ${core_sources}) diff --git a/core/tox.c b/core/tox.c new file mode 100644 index 00000000..a97e52bc --- /dev/null +++ b/core/tox.c @@ -0,0 +1,374 @@ +/* tox.c + * + * The Tox public API. + * + * Copyright (C) 2013 Tox project All Rights Reserved. + * + * This file is part of Tox. + * + * Tox is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Tox is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Tox. If not, see . + * + */ + +#include "Messenger.h" +/* + * returns a FRIEND_ADDRESS_SIZE byte address to give to others. + * format: [client_id (32 bytes)][nospam number (4 bytes)][checksum (2 bytes)] + * + */ +void tox_getaddress(void *tox, uint8_t *address) +{ + Messenger *m = tox; + getaddress(m, address); +} + +/* + * add a friend + * set the data that will be sent along with friend request + * address is the address of the friend (returned by getaddress of the friend you wish to add) it must be FRIEND_ADDRESS_SIZE bytes. TODO: add checksum. + * data is the data and length is the length + * returns the friend number if success + * return FA_TOOLONG if message length is too long + * return FAERR_NOMESSAGE if no message (message length must be >= 1 byte) + * return FAERR_OWNKEY if user's own key + * return FAERR_ALREADYSENT if friend request already sent or already a friend + * return FAERR_UNKNOWN for unknown error + * return FAERR_BADCHECKSUM if bad checksum in address + * return FAERR_SETNEWNOSPAM if the friend was already there but the nospam was different + * (the nospam for that friend was set to the new one) + * return FAERR_NOMEM if increasing the friend list size fails + */ +int tox_addfriend(void *tox, uint8_t *address, uint8_t *data, uint16_t length) +{ + Messenger *m = tox; + return m_addfriend(m, address, data, length); +} + +/* add a friend without sending a friendrequest. + returns the friend number if success + return -1 if failure. */ +int tox_addfriend_norequest(void *tox, uint8_t *client_id) +{ + Messenger *m = tox; + return m_addfriend_norequest(m, client_id); +} + +/* return the friend id associated to that client id. + return -1 if no such friend */ +int tox_getfriend_id(void *tox, uint8_t *client_id) +{ + Messenger *m = tox; + return getfriend_id(m, client_id); +} + +/* copies the public key associated to that friend id into client_id buffer. + make sure that client_id is of size CLIENT_ID_SIZE. + return 0 if success + return -1 if failure */ +int tox_getclient_id(void *tox, int friend_id, uint8_t *client_id) +{ + Messenger *m = tox; + return getclient_id(m, friend_id, client_id); +} + +/* remove a friend */ +int tox_delfriend(void *tox, int friendnumber) +{ + Messenger *m = tox; + return m_delfriend(m, friendnumber); +} + +/* return 4 if friend is online + return 3 if friend is confirmed + return 2 if the friend request was sent + return 1 if the friend was added + return 0 if there is no friend with that number */ +int tox_friendstatus(void *tox, int friendnumber) +{ + Messenger *m = tox; + return m_friendstatus(m, friendnumber); +} + +/* send a text chat message to an online friend + returns the message id if packet was successfully put into the send queue + return 0 if it was not + you will want to retain the return value, it will be passed to your read receipt callback + if one is received. + m_sendmessage_withid will send a message with the id of your choosing, + however we can generate an id for you by calling plain m_sendmessage. */ +uint32_t tox_sendmessage(void *tox, int friendnumber, uint8_t *message, uint32_t length) +{ + Messenger *m = tox; + return m_sendmessage(m, friendnumber, message, length); +} + +uint32_t tox_sendmessage_withid(void *tox, int friendnumber, uint32_t theid, uint8_t *message, uint32_t length) +{ + Messenger *m = tox; + return m_sendmessage_withid(m, friendnumber, theid, message, length); +} + +/* send an action to an online friend + returns 1 if packet was successfully put into the send queue + return 0 if it was not */ +int tox_sendaction(void *tox, int friendnumber, uint8_t *action, uint32_t length) +{ + Messenger *m = tox; + return m_sendaction(m, friendnumber, action, length); +} + +/* Set our nickname + name must be a string of maximum MAX_NAME_LENGTH length. + length must be at least 1 byte + length is the length of name with the NULL terminator + return 0 if success + return -1 if failure */ +int tox_setname(void *tox, uint8_t *name, uint16_t length) +{ + Messenger *m = tox; + return setname(m, name, length); +} + +/* + Get your nickname. + m The messanger context to use. + name Pointer to a string for the name. + nlen The length of the string buffer. + returns Return the length of the name, 0 on error. +*/ +uint16_t tox_getselfname(void *tox, uint8_t *name, uint16_t nlen) +{ + Messenger *m = tox; + return getself_name(m, name, nlen); +} + +/* get name of friendnumber + put it in name + name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH (128) bytes. + return 0 if success + return -1 if failure */ +int tox_getname(void *tox, int friendnumber, uint8_t *name) +{ + Messenger *m = tox; + return getname(m, friendnumber, name); +} + +/* set our user status + you are responsible for freeing status after + returns 0 on success, -1 on failure */ +int tox_set_statusmessage(void *tox, uint8_t *status, uint16_t length) +{ + Messenger *m = tox; + return m_set_statusmessage(m, status, length); +} + +int tox_set_userstatus(void *tox, USERSTATUS status) +{ + Messenger *m = tox; + return m_set_userstatus(m, status); +} + +/* return the length of friendnumber's status message, + including null + pass it into malloc */ +int tox_get_statusmessage_size(void *tox, int friendnumber) +{ + Messenger *m = tox; + return m_get_statusmessage_size(m, friendnumber); +} + +/* copy friendnumber's status message into buf, truncating if size is over maxlen + get the size you need to allocate from m_get_statusmessage_size + The self variant will copy our own status message. */ +int tox_copy_statusmessage(void *tox, int friendnumber, uint8_t *buf, uint32_t maxlen) +{ + Messenger *m = tox; + return m_copy_statusmessage(m, friendnumber, buf, maxlen); +} + +int tox_copy_self_statusmessage(void *tox, uint8_t *buf, uint32_t maxlen) +{ + Messenger *m = tox; + return m_copy_self_statusmessage(m, buf, maxlen); +} + +/* Return one of USERSTATUS values. + * Values unknown to your application should be represented as USERSTATUS_NONE. + * As above, the self variant will return our own USERSTATUS. + * If friendnumber is invalid, this shall return USERSTATUS_INVALID. */ +USERSTATUS tox_get_userstatus(void *tox, int friendnumber) +{ + Messenger *m = tox; + return m_get_userstatus(m, friendnumber); +} + +USERSTATUS tox_get_selfuserstatus(void *tox) +{ + Messenger *m = tox; + return m_get_self_userstatus(m); +} + + +/* Sets whether we send read receipts for friendnumber. + * This function is not lazy, and it will fail if yesno is not (0 or 1).*/ +void tox_set_sends_receipts(void *tox, int friendnumber, int yesno) +{ + Messenger *m = tox; + m_set_sends_receipts(m, friendnumber, yesno); +} + + +/* set the function that will be executed when a friend request is received. + function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) */ +void tox_callback_friendrequest(void *tox, void (*function)(uint8_t *, uint8_t *, uint16_t, void *), void *userdata) +{ + Messenger *m = tox; + m_callback_friendrequest(m, function, userdata); +} + + +/* set the function that will be executed when a message from a friend is received. + function format is: function(int friendnumber, uint8_t * message, uint32_t length) */ +void tox_callback_friendmessage(void *tox, void (*function)(Messenger *tox, int, uint8_t *, uint16_t, void *), + void *userdata) +{ + Messenger *m = tox; + m_callback_friendmessage(m, function, userdata); +} + +/* set the function that will be executed when an action from a friend is received. + function format is: function(int friendnumber, uint8_t * action, uint32_t length) */ +void tox_callback_action(void *tox, void (*function)(Messenger *tox, int, uint8_t *, uint16_t, void *), void *userdata) +{ + Messenger *m = tox; + m_callback_action(m, function, userdata); +} + +/* set the callback for name changes + function(int friendnumber, uint8_t *newname, uint16_t length) + you are not responsible for freeing newname */ +void tox_callback_namechange(void *tox, void (*function)(Messenger *tox, int, uint8_t *, uint16_t, void *), + void *userdata) +{ + Messenger *m = tox; + m_callback_namechange(m, function, userdata); +} + +/* set the callback for status message changes + function(int friendnumber, uint8_t *newstatus, uint16_t length) + you are not responsible for freeing newstatus */ +void tox_callback_statusmessage(void *tox, void (*function)(Messenger *tox, int, uint8_t *, uint16_t, void *), + void *userdata) +{ + Messenger *m = tox; + m_callback_statusmessage(m, function, userdata); +} + +/* set the callback for status type changes + function(int friendnumber, USERSTATUS kind) */ +void tox_callback_userstatus(void *tox, void (*function)(Messenger *tox, int, USERSTATUS, void *), void *userdata) +{ + Messenger *m = tox; + m_callback_userstatus(m, function, userdata); +} + +/* set the callback for read receipts + function(int friendnumber, uint32_t receipt) + if you are keeping a record of returns from m_sendmessage, + receipt might be one of those values, and that means the message + has been received on the other side. since core doesn't + track ids for you, receipt may not correspond to any message + in that case, you should discard it. */ +void tox_callback_read_receipt(void *tox, void (*function)(Messenger *tox, int, uint32_t, void *), void *userdata) +{ + Messenger *m = tox; + m_callback_read_receipt(m, function, userdata); +} + +/* set the callback for connection status changes + function(int friendnumber, uint8_t status) + status: + 0 -- friend went offline after being previously online + 1 -- friend went online + note that this callback is not called when adding friends, thus the "after + being previously online" part. it's assumed that when adding friends, + their connection status is offline. */ +void tox_callback_connectionstatus(void *tox, void (*function)(Messenger *tox, int, uint8_t, void *), void *userdata) +{ + Messenger *m = tox; + m_callback_connectionstatus(m, function, userdata); +} + +/* Use this function to bootstrap the client + Sends a get nodes request to the given node with ip port and public_key */ +void tox_bootstrap(void *tox, IP_Port ip_port, uint8_t *public_key) +{ + Messenger *m = tox; + DHT_bootstrap(m->dht, ip_port, public_key); +} + +/* returns 0 if we are not connected to the DHT + returns 1 if we are */ +int tox_isconnected(void *tox) +{ + Messenger *m = tox; + return DHT_isconnected(m->dht); +} + +/* run this at startup + * returns allocated instance of tox on success + * returns 0 if there are problems */ +void *tox_new(void) +{ + return initMessenger(); +} + +/* run this before closing shop + * free all datastructures */ +void tox_kill(void *tox) +{ + Messenger *m = tox; + cleanupMessenger(m); +} + +/* the main loop that needs to be run at least 20 times per second */ +void tox_do(void *tox) +{ + Messenger *m = tox; + doMessenger(m); +} + +/* SAVING AND LOADING FUNCTIONS: */ + +/* returns the size of the messenger data (for saving) */ +uint32_t tox_size(void *tox) +{ + Messenger *m = tox; + return Messenger_size(m); +} + +/* save the messenger in data (must be allocated memory of size Messenger_size()) */ +void tox_save(void *tox, uint8_t *data) +{ + Messenger *m = tox; + Messenger_save(m, data); +} + +/* load the messenger from data of size length */ +int tox_load(void *tox, uint8_t *data, uint32_t length) +{ + Messenger *m = tox; + return Messenger_load(m, data, length); +} + -- cgit v1.2.3 From 0f77a2d131ca0b96e9b6c6e2e516ed0d47884e27 Mon Sep 17 00:00:00 2001 From: irungentoo Date: Fri, 23 Aug 2013 10:34:10 -0400 Subject: Added tox_ to 2 typedefs. --- core/tox.h | 8 ++++---- testing/nTox.c | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) (limited to 'core') diff --git a/core/tox.h b/core/tox.h index d5e61240..099b53e1 100644 --- a/core/tox.h +++ b/core/tox.h @@ -41,14 +41,14 @@ typedef union { uint8_t c[4]; uint16_t s[2]; uint32_t i; -} IP; +} tox_IP; typedef struct { - IP ip; + tox_IP ip; uint16_t port; /* not used for anything right now */ uint16_t padding; -} IP_Port; +} tox_IP_Port; /* status definitions */ enum { @@ -252,7 +252,7 @@ void tox_callback_connectionstatus(Tox *tox, void (*function)(Tox *tox, int, uin /* Use this function to bootstrap the client Sends a get nodes request to the given node with ip port and public_key */ -void tox_bootstrap(Tox *tox, IP_Port ip_port, uint8_t *public_key); +void tox_bootstrap(Tox *tox, tox_IP_Port ip_port, uint8_t *public_key); /* returns 0 if we are not connected to the DHT returns 1 if we are */ diff --git a/testing/nTox.c b/testing/nTox.c index 87fec818..9df1e78b 100644 --- a/testing/nTox.c +++ b/testing/nTox.c @@ -511,7 +511,7 @@ int main(int argc, char *argv[]) new_lines(idstring); strcpy(line, ""); - IP_Port bootstrap_ip_port; + tox_IP_Port bootstrap_ip_port; bootstrap_ip_port.port = htons(atoi(argv[2])); int resolved_address = resolve_addr(argv[1]); -- cgit v1.2.3 From eb0bb66a204cf65929bf3d7e2176b98494a2663d Mon Sep 17 00:00:00 2001 From: irungentoo Date: Fri, 23 Aug 2013 11:02:01 -0400 Subject: Fixed some comments. --- core/tox.h | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) (limited to 'core') diff --git a/core/tox.h b/core/tox.h index 099b53e1..bdfac1d6 100644 --- a/core/tox.h +++ b/core/tox.h @@ -97,15 +97,15 @@ void tox_getaddress(Tox *tox, uint8_t *address); * address is the address of the friend (returned by getaddress of the friend you wish to add) it must be FRIEND_ADDRESS_SIZE bytes. TODO: add checksum. * data is the data and length is the length * returns the friend number if success - * return FA_TOOLONG if message length is too long - * return FAERR_NOMESSAGE if no message (message length must be >= 1 byte) - * return FAERR_OWNKEY if user's own key - * return FAERR_ALREADYSENT if friend request already sent or already a friend - * return FAERR_UNKNOWN for unknown error - * return FAERR_BADCHECKSUM if bad checksum in address - * return FAERR_SETNEWNOSPAM if the friend was already there but the nospam was different + * return TOX_FA_TOOLONG if message length is too long + * return TOX_FAERR_NOMESSAGE if no message (message length must be >= 1 byte) + * return TOX_FAERR_OWNKEY if user's own key + * return TOX_FAERR_ALREADYSENT if friend request already sent or already a friend + * return TOX_FAERR_UNKNOWN for unknown error + * return TOX_FAERR_BADCHECKSUM if bad checksum in address + * return TOX_FAERR_SETNEWNOSPAM if the friend was already there but the nospam was different * (the nospam for that friend was set to the new one) - * return FAERR_NOMEM if increasing the friend list size fails + * return TOX_FAERR_NOMEM if increasing the friend list size fails */ int tox_addfriend(Tox *tox, uint8_t *address, uint8_t *data, uint16_t length); @@ -128,11 +128,11 @@ int tox_getclient_id(Tox *tox, int friend_id, uint8_t *client_id); /* remove a friend */ int tox_delfriend(Tox *tox, int friendnumber); -/* return 4 if friend is online - return 3 if friend is confirmed - return 2 if the friend request was sent - return 1 if the friend was added - return 0 if there is no friend with that number */ +/* return TOX_FRIEND_ONLINE if friend is online + return TOX_FRIEND_CONFIRMED if friend is confirmed + return TOX_FRIEND_REQUESTED if the friend request was sent + return TOX_FRIEND_ADDED if the friend was added + return TOX_NOFRIEND if there is no friend with that number */ int tox_friendstatus(Tox *tox, int friendnumber); /* send a text chat message to an online friend -- cgit v1.2.3 From a71361d8905f10d0dc8325f060134d135b486f60 Mon Sep 17 00:00:00 2001 From: irungentoo Date: Fri, 23 Aug 2013 15:33:38 -0400 Subject: Replaced some defines with enums. --- core/Messenger.h | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) (limited to 'core') diff --git a/core/Messenger.h b/core/Messenger.h index 581c4ba9..e808529f 100644 --- a/core/Messenger.h +++ b/core/Messenger.h @@ -48,23 +48,28 @@ extern "C" { #define PACKET_ID_MESSAGE 64 #define PACKET_ID_ACTION 63 + /* status definitions */ -#define FRIEND_ONLINE 4 -#define FRIEND_CONFIRMED 3 -#define FRIEND_REQUESTED 2 -#define FRIEND_ADDED 1 -#define NOFRIEND 0 +enum { + NOFRIEND, + FRIEND_ADDED, + FRIEND_REQUESTED, + FRIEND_CONFIRMED, + FRIEND_ONLINE, +}; /* errors for m_addfriend * FAERR - Friend Add Error */ -#define FAERR_TOOLONG -1 -#define FAERR_NOMESSAGE -2 -#define FAERR_OWNKEY -3 -#define FAERR_ALREADYSENT -4 -#define FAERR_UNKNOWN -5 -#define FAERR_BADCHECKSUM -6 -#define FAERR_SETNEWNOSPAM -7 -#define FAERR_NOMEM -8 +enum { + FAERR_TOOLONG = -1, + FAERR_NOMESSAGE = -2, + FAERR_OWNKEY = -3, + FAERR_ALREADYSENT = -4, + FAERR_UNKNOWN = -5, + FAERR_BADCHECKSUM = -6, + FAERR_SETNEWNOSPAM = -7, + FAERR_NOMEM = -8 +}; /* don't assume MAX_STATUSMESSAGE_LENGTH will stay at 128, it may be increased to an absurdly large number later */ -- cgit v1.2.3