/* run this at startup */ Messenger *initMessenger(void) { Messenger *m = calloc(1, sizeof(Messenger)); if ( ! m ) return 0; new_keys(); m_set_statusmessage(m, (uint8_t *)"Online", sizeof("Online")); initNetCrypto(); IP ip; ip.i = 0; if (init_networking(ip, PORT) == -1) return 0; DHT_init(); LosslessUDP_init(); friendreq_init(); LANdiscovery_init(); set_nospam(random_int()); send_LANdiscovery(htons(PORT)); timer_single(&LANdiscovery, 0, LAN_DISCOVERY_INTERVAL); return m; }
/* run this at startup */ Messenger *initMessenger(void) { Messenger *m = calloc(1, sizeof(Messenger)); if ( ! m ) return NULL; IP ip; ip.i = 0; m->net = new_networking(ip, PORT); if (m->net == NULL) { free(m); return NULL; } m->net_crypto = new_net_crypto(m->net); if (m->net_crypto == NULL) { kill_networking(m->net); free(m); return NULL; } m->dht = new_DHT(m->net_crypto); if (m->dht == NULL) { kill_net_crypto(m->net_crypto); kill_networking(m->net); free(m); return NULL; } new_keys(m->net_crypto); m_set_statusmessage(m, (uint8_t *)"Online", sizeof("Online")); friendreq_init(&(m->fr), m->net_crypto); LANdiscovery_init(m->dht); set_nospam(&(m->fr), random_int()); init_cryptopackets(m->dht); return m; }
void tox_set_nospam(Tox *tox, uint32_t nospam) { Messenger *m = tox; set_nospam(&(m->fr), nospam); }
/* load the messenger from data of size length. */ int Messenger_load(Messenger *m, uint8_t *data, uint32_t length) { if (length == ~0) return -1; if (length < crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizeof(uint32_t) * 3) return -1; length -= crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizeof(uint32_t) * 3; load_keys(data); data += crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES; uint32_t nospam; memcpy(&nospam, data, sizeof(nospam)); set_nospam(nospam); data += sizeof(nospam); uint32_t size; memcpy(&size, data, sizeof(size)); data += sizeof(size); if (length < size) return -1; length -= size; if (DHT_load(data, size) == -1) return -1; data += size; memcpy(&size, data, sizeof(size)); data += sizeof(size); if (length < size || size % sizeof(Friend) != 0) return -1; Friend *temp = malloc(size); memcpy(temp, data, size); uint16_t num = size / sizeof(Friend); uint32_t i; for (i = 0; i < num; ++i) { if (temp[i].status >= 3) { int fnum = m_addfriend_norequest(m, temp[i].client_id); setfriendname(m, fnum, temp[i].name); /* set_friend_statusmessage(fnum, temp[i].statusmessage, temp[i].statusmessage_length); */ } else if (temp[i].status != 0) { /* TODO: this is not a good way to do this. */ uint8_t address[FRIEND_ADDRESS_SIZE]; memcpy(address, temp[i].client_id, crypto_box_PUBLICKEYBYTES); memcpy(address + crypto_box_PUBLICKEYBYTES, &(temp[i].friendrequest_nospam), sizeof(uint32_t)); uint16_t checksum = address_checksum(address, FRIEND_ADDRESS_SIZE - sizeof(checksum)); memcpy(address + crypto_box_PUBLICKEYBYTES + sizeof(uint32_t), &checksum, sizeof(checksum)); m_addfriend(m, address, temp[i].info, temp[i].info_size); } } free(temp); data += size; length -= size; uint16_t small_size; if (length < sizeof(small_size)) return -1; memcpy(&small_size, data, sizeof(small_size)); data += sizeof(small_size); length -= sizeof(small_size); if (length != small_size) return -1; setname(m, data, small_size); return 0; }