Skip to content

Instantly share code, notes, and snippets.

#!/usr/bin/env python3
from sys import argv
"""
Converts a string representation of a sorted array of integers `s` into a bitfield.
e.g. indicesToBin("1 2 4 9 10 13 15 17 18 20 21 25 26 28 29 33 34 36 37 38 39")
-> "01101000011001010110110001101100011011110"
@JFreegman
JFreegman / Tox NGC handshake protocol.txt
Last active November 22, 2021 16:16
NGC handshake protocol
1. Peer A receives Peer B's announcement from DHT:
A -> B send handshake request GH_REQUEST & HS_INVITE_REQUEST
B -> A send handshake response GH_RESPONSE & HS_INVITE_REQUEST
A -> B send invite request GP_INVITE_REQUEST
B -> A send invite response GP_INVITE_RESPONSE
A -> B send sync request GP_SYNC_REQUEST
B -> A send sync response GP_SYNC_RESPONSE
B -> A send all other peer announcements [goto: 2]
A -> Connection State = CS_CONNECTED
/* Basic rust callback function */
pub fn func(data: &[u8], src_addr: &SocketAddr, obj: &Rc<RefCell<Any>>) {
let mut temp = obj.borrow_mut();
let mut dht = match temp.downcast_mut::<DHT>() {
Some(dht) => dht,
None => return,
};
...
let port = match get_available_port(DEAFULT_PORT_FROM, DEFAULT_PORT_TO) {
Some(port) => port,
None => panic!("failed to find available port"),
};
let sockaddr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), port);
let new_sock = match UdpSocket::bind(&sockaddr) {
Ok(sock) => sock,
Err(e) => panic!("failed to bind socket to port {}; err={:?}", port, e),
/**
* Converts a string representation of a key into a byte array. The supplied
* string must contain 64 hex-characters.
*
* Returns a byte array of length 32 on success.
* Returns None on failure.
*/
pub fn key_string_to_bytes(s: &str) -> Option<[u8; 32]> {
if s.len() < 64 {
return None;
#!/usr/bin/python
key = 'kw6PZq3Zd;ekR[_1'
ret = ''
for i, ch in enumerate(key):
ecx = i + 1
edx = 0x66666667
eax = ecx
eax = edx * eax
#0 0x00007ff53fdda39b in recursive_DHT_bucket_get_nodes (bucket=0x16f57c0, nodes=0x0, number=number@entry=8,
public_key=public_key@entry=0x7ffd247512f0 "", friend_ok=friend_ok@entry=true, ret_ips=ret_ips@entry=true)
at ../toxcore/DHT.c:772
i = 7
counter = 0
#1 0x00007ff53fdda480 in recursive_DHT_bucket_get_nodes (bucket=bucket@entry=0x1a144d0, nodes=0x0,
number=number@entry=8, public_key=public_key@entry=0x7ffd247512f0 "", friend_ok=friend_ok@entry=true,
ret_ips=ret_ips@entry=true) at ../toxcore/DHT.c:746
ret = <optimized out>
#2 0x00007ff53fdda5c4 in dealloc_buckets (bucket=0x1a144d0) at ../toxcore/DHT.c:816
@JFreegman
JFreegman / thing.c
Last active December 10, 2015 07:10
thing.c
static void utox_group_peer_join_cb(Tox *tox, uint32_t groupnumber, uint32_t peer_id, void *UNUSED(user_data)) {
debug("newGC:\tpeer join group %u, peer %u\n", groupnumber, peer_id);
/* Get and store name for this group, used by other functions */
uint32_t name_length = tox_group_peer_get_name_size(tox, groupnumber, peer_id, NULL);
uint8_t *name = malloc(name_length + 1);
TOX_ERR_GROUP_PEER_QUERY err = 0;
tox_group_peer_get_name(tox, groupnumber, peer_id, name, &err);
if (err) {
debug("newGC:\tError getting peer %u name!\n", peer_id);
/* Max size of a packet payload */
#define FUZZ_PACKET_LEN_RANGE 512
/* Sends packet of size length to peernumber for every relevant group message type */
static void fuzz_send_group_packet(GC_Chat *chat, uint32_t peernumber, const uint8_t *packet, size_t length)
{
size_t i;
/* All lossy packet types */
for (i = GP_PING; i <= GP_TCP_RELAYS; ++i)