The future of online communications.
Go to file
xhe 0becafd272
Split bit_rate_set(), one for audio, one for video.
Fixes #572.

As discussed in the issue, there's a risk that toxcore may not hold the
maximum bitrates libvpx supports, if toxcore insists on using integer
type. I initially proposed to have another flag in set(), so that we can
use unsigned type instead. iphydf came up with a better solution, that is
splitting the original functions, one for audio, one for video. Now, we
could safely replace int32_t with uint32_t.

Also: clean video_bit_rate_invalid()

Though this is not a part of issue #572, as it's used in the
toxav_bit_rate_set(), i cleaned the code. As mannol said, there should be
a check. Uint32_t is large enough to hold the maximum bitrates libvpx
supports, but user may pass a value larger than uint while smaller than
uint32_t. Thanks to the reminding from nurupo, it's no longer a stub
function.

Bitrate error enums are shared for both audio and video
https://github.com/TokTok/c-toxcore/pull/578#issuecomment-360095609, just
as iphydf said.
2018-01-25 14:29:01 +00:00
auto_tests Split bit_rate_set(), one for audio, one for video. 2018-01-25 14:29:01 +00:00
build Remove deprecated ToxDNS 2017-12-29 00:32:18 +00:00
cmake Use <stdlib.h> for alloca on FreeBSD. 2018-01-22 21:05:30 +00:00
dist-build Removed unnecessary parameters 2014-08-25 10:00:09 +04:00
docs Fix typos in docs 2017-09-20 21:31:56 +02:00
m4 Build system now automatically enables epoll support in TCP server 2014-07-17 20:44:49 -04:00
other Use <stdlib.h> for alloca on FreeBSD. 2018-01-22 21:05:30 +00:00
super_donators Add BUILD files for all the little tools in the repo. 2018-01-21 01:31:11 +00:00
testing Publish a single public BUILD target for c-toxcore. 2018-01-22 21:18:24 +00:00
toxav Split bit_rate_set(), one for audio, one for video. 2018-01-25 14:29:01 +00:00
toxcore Remove dead return statement. 2018-01-25 09:22:19 +00:00
toxencryptsave Publish a single public BUILD target for c-toxcore. 2018-01-22 21:18:24 +00:00
.editorconfig Add .editorconfig 2017-12-25 10:55:04 -06:00
.gitignore update rpm spec and use variables in cmake instead of hardcoded paths (#624) 2018-01-15 14:23:33 +03:00
.travis.yml Disable the autotools build in PR builds. 2018-01-25 02:52:13 +00:00
appveyor.yml Build tests on appveyor, the MSVC build. 2017-06-05 13:45:20 +00:00
autogen.sh Some configuration/build fixes, so building basicaly everything else than the library can be disabled 2013-10-07 02:01:16 +02:00
BUILD Publish a single public BUILD target for c-toxcore. 2018-01-22 21:18:24 +00:00
CHANGELOG.md Fix some typos in code and cmake comments 2018-01-12 21:22:44 +08:00
circle.yml update to astyle 2.04 on circleCI to get the correct result 2018-01-20 00:31:28 +01:00
CMakeLists.txt Use <stdlib.h> for alloca on FreeBSD. 2018-01-22 21:05:30 +00:00
configure.ac Remove nTox from the repo. 2018-01-20 19:05:53 +00:00
COPYING Removed the unused autotools files 2015-10-05 15:18:55 -07:00
DONATORS If we receive a packet from a node we are searching for, ping it. 2015-12-08 15:43:03 -05:00
INSTALL.md Remove nTox from the repo. 2018-01-20 19:05:53 +00:00
libtoxav.pc.in Build system fixes. 2014-12-18 10:04:31 -05:00
libtoxcore.pc.in Remove deprecated ToxDNS 2017-12-29 00:32:18 +00:00
Makefile.am Setup autotools to read .so version info from a separate file 2017-01-18 11:20:07 +01:00
README.md Add projects link to Readme. 2018-01-14 11:14:57 +00:00
so.version Bump toxcore version to 0.2.0. 2018-01-08 19:36:00 +00:00
tox.spec.in Remove nTox from the repo. 2018-01-20 19:05:53 +00:00

Project Tox

Current build status: Build Status Current Coverage: Coverage Status

Website | Wiki | Blog | FAQ | Binaries/Downloads | Clients | Compiling | Toxcore's Projects

IRC Channels: Users: #tox@freenode, Developers: #toktok@freenode

What is Tox

Tox is a peer to peer (serverless) instant messenger aimed at making security and privacy easy to obtain for regular users. It uses NaCl for its encryption and authentication.

IMPORTANT!

Danger: Experimental

This is an experimental cryptographic network library. It has not been formally audited by an independent third party that specializes in cryptography or cryptanalysis. Use this library at your own risk.

The underlying crypto library NaCl provides reliable encryption, but the security model has not yet been fully specified. See issue 210 for a discussion on developing a threat model. See other issues for known weaknesses (e.g. issue 426 describes what can happen if your secret key is stolen).

Toxcore Development Roadmap

The roadmap and changelog are generated from GitHub issues. You may view them on the website, where they are updated at least once every 24 hours:

Installing toxcore

Detailed installation instructions can be found in INSTALL.md.

In a nutshell, if you have libsodium or nacl installed, run:

autoreconf -fi
mkdir _build && cd _build
../configure
make
sudo make install

If you have libvpx and opus installed, the above will also build the A/V library for multimedia chats.

Using toxcore

The simplest "hello world" example could be an echo bot. Here we will walk through the implementation of a simple bot.

Creating the tox instance

All toxcore API functions work with error parameters. They are enums with one OK value and several error codes that describe the different situations in which the function might fail.

TOX_ERR_NEW err_new;
Tox *tox = tox_new(NULL, &err_new);
if (err_new != TOX_ERR_NEW_OK) {
  fprintf(stderr, "tox_new failed with error code %d\n", err_new);
  exit(1);
}

Here, we simply exit the program, but in a real client you will probably want to do some error handling and proper error reporting to the user. The NULL argument given to the first parameter of tox_new is the Tox_Options. It contains various write-once network settings and allows you to load a previously serialised instance. See toxcore/tox.h for details.

Setting up callbacks

Toxcore works with callbacks that you can register to listen for certain events. Examples of such events are "friend request received" or "friend sent a message". Search the API for tox_callback_* to find all of them.

Here, we will set up callbacks for receiving friend requests and receiving messages. We will always accept any friend request (because we're a bot), and when we receive a message, we send it back to the sender.

tox_callback_friend_request(tox, handle_friend_request);
tox_callback_friend_message(tox, handle_friend_message);

These two function calls set up the callbacks. Now we also need to implement these "handle" functions.

Handle friend requests

static void handle_friend_request(
  Tox *tox, const uint8_t *public_key, const uint8_t *message, size_t length,
  void *user_data) {
  // Accept the friend request:
  TOX_ERR_FRIEND_ADD err_friend_add;
  tox_friend_add_norequest(tox, public_key, &err_friend_add);
  if (err_friend_add != TOX_ERR_FRIEND_ADD_OK) {
    fprintf(stderr, "unable to add friend: %d\n", err_friend_add);
  }
}

The tox_friend_add_norequest function adds the friend without sending them a friend request. Since we already got a friend request, this is the right thing to do. If you wanted to send a friend request yourself, you would use tox_friend_add, which has an extra parameter for the message.

Handle messages

Now, when the friend sends us a message, we want to respond to them by sending them the same message back. This will be our "echo".

static void handle_friend_message(
  Tox *tox, uint32_t friend_number, TOX_MESSAGE_TYPE type,
  const uint8_t *message, size_t length,
  void *user_data) {
  TOX_ERR_FRIEND_SEND_MESSAGE err_send;
  tox_friend_send_message(tox, friend_number, type, message, length,
    &err_send);
  if (err_send != TOX_ERR_FRIEND_SEND_MESSAGE_OK) {
    fprintf(stderr, "unable to send message back to friend %d: %d\n",
      friend_number, err_send);
  }
}

That's it for the setup. Now we want to actually run the bot.

Main event loop

Toxcore works with a main event loop function tox_iterate that you need to call at a certain frequency dictated by tox_iteration_interval. This is a polling function that receives new network messages and processes them.

while (true) {
  usleep(1000 * tox_iteration_interval(tox));
  tox_iterate(tox, NULL);
}

That's it! Now you have a working echo bot. The only problem is that since Tox works with public keys, and you can't really guess your bot's public key, you can't add it as a friend in your client. For this, we need to call another API function: tox_self_get_address(tox, address). This will fill the 38 byte friend address into the address buffer. You can then display that binary string as hex and input it into your client. Writing a bin2hex function is left as exercise for the reader.

We glossed over a lot of details, such as the user data which we passed to tox_iterate (passing NULL), bootstrapping into an actual network (this bot will work in the LAN, but not on an internet server) and the fact that we now have no clean way of stopping the bot (while (true)). If you want to write a real bot, you will probably want to read up on all the API functions. Consult the API documentation in toxcore/tox.h for more information.

Other resources