mirror of
https://github.com/irungentoo/toxcore.git
synced 2024-03-22 13:30:51 +08:00
util.*:
- added logging functions, default off tox.h: - added includes for sockaddr_in/6 network.c: - added logging functions, default off (#define in util.h) - IPv6: activating site-local all-nodes multicast address (i.e. IPv6 equivalent of broadcast)
This commit is contained in:
parent
f267266bf6
commit
3ae7460853
|
@ -965,19 +965,23 @@ void Messenger_save(Messenger *m, uint8_t *data)
|
||||||
{
|
{
|
||||||
save_keys(m->net_crypto, data);
|
save_keys(m->net_crypto, data);
|
||||||
data += crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES;
|
data += crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES;
|
||||||
|
|
||||||
uint32_t nospam = get_nospam(&(m->fr));
|
uint32_t nospam = get_nospam(&(m->fr));
|
||||||
memcpy(data, &nospam, sizeof(nospam));
|
memcpy(data, &nospam, sizeof(nospam));
|
||||||
data += sizeof(nospam);
|
data += sizeof(nospam);
|
||||||
|
|
||||||
uint32_t size = DHT_size(m->dht);
|
uint32_t size = DHT_size(m->dht);
|
||||||
memcpy(data, &size, sizeof(size));
|
memcpy(data, &size, sizeof(size));
|
||||||
data += sizeof(size);
|
data += sizeof(size);
|
||||||
DHT_save(m->dht, data);
|
DHT_save(m->dht, data);
|
||||||
data += size;
|
data += size;
|
||||||
|
|
||||||
size = sizeof(Friend) * m->numfriends;
|
size = sizeof(Friend) * m->numfriends;
|
||||||
memcpy(data, &size, sizeof(size));
|
memcpy(data, &size, sizeof(size));
|
||||||
data += sizeof(size);
|
data += sizeof(size);
|
||||||
memcpy(data, m->friendlist, sizeof(Friend) * m->numfriends);
|
memcpy(data, m->friendlist, sizeof(Friend) * m->numfriends);
|
||||||
data += size;
|
data += size;
|
||||||
|
|
||||||
uint16_t small_size = m->name_length;
|
uint16_t small_size = m->name_length;
|
||||||
memcpy(data, &small_size, sizeof(small_size));
|
memcpy(data, &small_size, sizeof(small_size));
|
||||||
data += sizeof(small_size);
|
data += sizeof(small_size);
|
||||||
|
@ -990,59 +994,64 @@ int Messenger_load(Messenger *m, uint8_t *data, uint32_t length)
|
||||||
if (length == ~((uint32_t)0))
|
if (length == ~((uint32_t)0))
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
if (length < crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizeof(uint32_t) * 3)
|
/* BLOCK1: PUBKEY, SECKEY, NOSPAM, SIZE */
|
||||||
|
if (length < crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizeof(uint32_t) * 2)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
length -= crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizeof(uint32_t) * 3;
|
|
||||||
load_keys(m->net_crypto, data);
|
load_keys(m->net_crypto, data);
|
||||||
data += crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES;
|
data += crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES;
|
||||||
|
length -= crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES;
|
||||||
|
|
||||||
uint32_t nospam;
|
uint32_t nospam;
|
||||||
memcpy(&nospam, data, sizeof(nospam));
|
memcpy(&nospam, data, sizeof(nospam));
|
||||||
set_nospam(&(m->fr), nospam);
|
set_nospam(&(m->fr), nospam);
|
||||||
data += sizeof(nospam);
|
data += sizeof(nospam);
|
||||||
|
length -= sizeof(nospam);
|
||||||
|
|
||||||
uint32_t size;
|
uint32_t size;
|
||||||
memcpy(&size, data, sizeof(size));
|
memcpy(&size, data, sizeof(size));
|
||||||
data += sizeof(size);
|
data += sizeof(size);
|
||||||
|
length -= sizeof(size);
|
||||||
|
|
||||||
if (length < size)
|
if (length < size)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
length -= size;
|
|
||||||
|
|
||||||
if (DHT_load(m->dht, data, size) == -1)
|
if (DHT_load(m->dht, data, size) == -1)
|
||||||
return -1;
|
fprintf(stderr, "Data file: Something wicked happened to the stored connections.\n");
|
||||||
|
|
||||||
|
/* go on, friends still might be intact */
|
||||||
|
|
||||||
data += size;
|
data += size;
|
||||||
|
length -= size;
|
||||||
|
|
||||||
memcpy(&size, data, sizeof(size));
|
memcpy(&size, data, sizeof(size));
|
||||||
data += sizeof(size);
|
data += sizeof(size);
|
||||||
|
if (length < size)
|
||||||
if (length < size || size % sizeof(Friend) != 0)
|
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
Friend *temp = malloc(size);
|
if (!(size % sizeof(Friend))) {
|
||||||
memcpy(temp, data, size);
|
uint16_t num = size / sizeof(Friend);
|
||||||
|
Friend temp[num];
|
||||||
|
memcpy(temp, data, size);
|
||||||
|
|
||||||
uint16_t num = size / sizeof(Friend);
|
uint32_t i;
|
||||||
|
for (i = 0; i < num; ++i) {
|
||||||
uint32_t i;
|
if (temp[i].status >= 3) {
|
||||||
|
int fnum = m_addfriend_norequest(m, temp[i].client_id);
|
||||||
for (i = 0; i < num; ++i) {
|
setfriendname(m, fnum, temp[i].name, temp[i].name_length);
|
||||||
if (temp[i].status >= 3) {
|
/* set_friend_statusmessage(fnum, temp[i].statusmessage, temp[i].statusmessage_length); */
|
||||||
int fnum = m_addfriend_norequest(m, temp[i].client_id);
|
} else if (temp[i].status != 0) {
|
||||||
setfriendname(m, fnum, temp[i].name, temp[i].name_length);
|
/* TODO: This is not a good way to do this. */
|
||||||
/* set_friend_statusmessage(fnum, temp[i].statusmessage, temp[i].statusmessage_length); */
|
uint8_t address[FRIEND_ADDRESS_SIZE];
|
||||||
} else if (temp[i].status != 0) {
|
memcpy(address, temp[i].client_id, crypto_box_PUBLICKEYBYTES);
|
||||||
/* TODO: This is not a good way to do this. */
|
memcpy(address + crypto_box_PUBLICKEYBYTES, &(temp[i].friendrequest_nospam), sizeof(uint32_t));
|
||||||
uint8_t address[FRIEND_ADDRESS_SIZE];
|
uint16_t checksum = address_checksum(address, FRIEND_ADDRESS_SIZE - sizeof(checksum));
|
||||||
memcpy(address, temp[i].client_id, crypto_box_PUBLICKEYBYTES);
|
memcpy(address + crypto_box_PUBLICKEYBYTES + sizeof(uint32_t), &checksum, sizeof(checksum));
|
||||||
memcpy(address + crypto_box_PUBLICKEYBYTES, &(temp[i].friendrequest_nospam), sizeof(uint32_t));
|
m_addfriend(m, address, temp[i].info, temp[i].info_size);
|
||||||
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;
|
data += size;
|
||||||
length -= size;
|
length -= size;
|
||||||
|
|
||||||
|
|
|
@ -26,6 +26,7 @@
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "network.h"
|
#include "network.h"
|
||||||
|
#include "util.h"
|
||||||
|
|
||||||
/* return current UNIX time in microseconds (us). */
|
/* return current UNIX time in microseconds (us). */
|
||||||
uint64_t current_time(void)
|
uint64_t current_time(void)
|
||||||
|
@ -61,6 +62,10 @@ uint32_t random_int(void)
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef LOGGING
|
||||||
|
static void loglogdata(char *message, uint8_t *buffer, size_t buflen, IP_Port *ip_port, ssize_t res);
|
||||||
|
#endif
|
||||||
|
|
||||||
/* Basic network functions:
|
/* Basic network functions:
|
||||||
* Function to send packet(data) of length length to ip_port.
|
* Function to send packet(data) of length length to ip_port.
|
||||||
*/
|
*/
|
||||||
|
@ -122,7 +127,11 @@ int sendpacket(Networking_Core *net, IP_Port ip_port, uint8_t *data, uint32_t le
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
return sendto(net->sock, (char *) data, length, 0, (struct sockaddr *)&addr, addrsize);
|
int res = sendto(net->sock, (char *) data, length, 0, (struct sockaddr *)&addr, addrsize);
|
||||||
|
#ifdef LOGGING
|
||||||
|
loglogdata("O=>", data, length, &ip_port, res);
|
||||||
|
#endif
|
||||||
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Function to receive data
|
/* Function to receive data
|
||||||
|
@ -131,11 +140,7 @@ int sendpacket(Networking_Core *net, IP_Port ip_port, uint8_t *data, uint32_t le
|
||||||
* Packet length is put into length.
|
* Packet length is put into length.
|
||||||
* Dump all empty packets.
|
* Dump all empty packets.
|
||||||
*/
|
*/
|
||||||
#ifdef WIN32
|
static int receivepacket(sock_t sock, IP_Port *ip_port, uint8_t *data, uint32_t *length)
|
||||||
static int receivepacket(unsigned int sock, IP_Port *ip_port, uint8_t *data, uint32_t *length)
|
|
||||||
#else
|
|
||||||
static int receivepacket(int sock, IP_Port *ip_port, uint8_t *data, uint32_t *length)
|
|
||||||
#endif
|
|
||||||
{
|
{
|
||||||
struct sockaddr_storage addr;
|
struct sockaddr_storage addr;
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
|
@ -145,8 +150,13 @@ static int receivepacket(int sock, IP_Port *ip_port, uint8_t *data, uint32_t *le
|
||||||
#endif
|
#endif
|
||||||
(*(int32_t *)length) = recvfrom(sock, (char *) data, MAX_UDP_PACKET_SIZE, 0, (struct sockaddr *)&addr, &addrlen);
|
(*(int32_t *)length) = recvfrom(sock, (char *) data, MAX_UDP_PACKET_SIZE, 0, (struct sockaddr *)&addr, &addrlen);
|
||||||
|
|
||||||
if (*(int32_t *)length <= 0)
|
if (*(int32_t *)length <= 0) {
|
||||||
|
#ifdef LOGGING
|
||||||
|
if ((length < 0) && (errno != EWOULDBLOCK))
|
||||||
|
sprintf(logbuffer, "Unexpected error reading from socket: %u, %s\n", errno, strerror(errno));
|
||||||
|
#endif
|
||||||
return -1; /* Nothing received or empty packet. */
|
return -1; /* Nothing received or empty packet. */
|
||||||
|
}
|
||||||
|
|
||||||
#ifdef TOX_ENABLE_IPV6
|
#ifdef TOX_ENABLE_IPV6
|
||||||
if (addr.ss_family == AF_INET) {
|
if (addr.ss_family == AF_INET) {
|
||||||
|
@ -173,6 +183,10 @@ static int receivepacket(int sock, IP_Port *ip_port, uint8_t *data, uint32_t *le
|
||||||
return -1;
|
return -1;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifdef LOGGING
|
||||||
|
loglogdata("=>O", data, *length, ip_port, 0);
|
||||||
|
#endif
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -307,6 +321,10 @@ Networking_Core *new_networking(IP ip, uint16_t port)
|
||||||
fcntl(temp->sock, F_SETFL, O_NONBLOCK, 1);
|
fcntl(temp->sock, F_SETFL, O_NONBLOCK, 1);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifdef LOGGING
|
||||||
|
loginit(ntohs(port));
|
||||||
|
#endif
|
||||||
|
|
||||||
/* Bind our socket to port PORT and the given IP address (usually 0.0.0.0 or ::) */
|
/* Bind our socket to port PORT and the given IP address (usually 0.0.0.0 or ::) */
|
||||||
uint16_t *portptr = NULL;
|
uint16_t *portptr = NULL;
|
||||||
struct sockaddr_storage addr;
|
struct sockaddr_storage addr;
|
||||||
|
@ -346,9 +364,33 @@ Networking_Core *new_networking(IP ip, uint16_t port)
|
||||||
if (ip.family == AF_INET6) {
|
if (ip.family == AF_INET6) {
|
||||||
char ipv6only = 0;
|
char ipv6only = 0;
|
||||||
int res = setsockopt(temp->sock, IPPROTO_IPV6, IPV6_V6ONLY, (char *)&ipv6only, sizeof(ipv6only));
|
int res = setsockopt(temp->sock, IPPROTO_IPV6, IPV6_V6ONLY, (char *)&ipv6only, sizeof(ipv6only));
|
||||||
|
#ifdef LOGGING
|
||||||
if (res < 0) {
|
if (res < 0) {
|
||||||
/* add log message*/
|
sprintf(logbuffer, "Failed to enable dual-stack on IPv6 socket, won't be able to receive from/send to IPv4 addresses. (%u, %s)\n",
|
||||||
|
errno, strerror(errno));
|
||||||
|
loglog(logbuffer);
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
loglog("Embedded IPv4 addresses enabled successfully.\n");
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* multicast local nodes */
|
||||||
|
struct ipv6_mreq mreq;
|
||||||
|
memset(&mreq, 0, sizeof(mreq));
|
||||||
|
mreq.ipv6mr_multiaddr.s6_addr[ 0] = 0xFF;
|
||||||
|
mreq.ipv6mr_multiaddr.s6_addr[ 1] = 0x02;
|
||||||
|
mreq.ipv6mr_multiaddr.s6_addr[15] = 0x01;
|
||||||
|
mreq.ipv6mr_interface = 0;
|
||||||
|
res = setsockopt(temp->sock, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq, sizeof(mreq));
|
||||||
|
#ifdef LOGGING
|
||||||
|
if (res < 0) {
|
||||||
|
sprintf(logbuffer, "Failed to activate local multicast membership. (%u, %s)\n",
|
||||||
|
errno, strerror(errno));
|
||||||
|
loglog(logbuffer);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
loglog("Local multicast group FF02::1 joined successfully.\n");
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -363,6 +405,10 @@ Networking_Core *new_networking(IP ip, uint16_t port)
|
||||||
if (!res)
|
if (!res)
|
||||||
{
|
{
|
||||||
temp->port = *portptr;
|
temp->port = *portptr;
|
||||||
|
#ifdef LOGGING
|
||||||
|
sprintf(logbuffer, "Bound successfully to %s:%u.\n", ip_ntoa(&ip), ntohs(temp->port));
|
||||||
|
loglog(logbuffer);
|
||||||
|
#endif
|
||||||
return temp;
|
return temp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -733,3 +779,17 @@ int addr_resolve_or_parse_ip(const char *address, IP *to)
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#ifdef LOGGING
|
||||||
|
static void loglogdata(char *message, uint8_t *buffer, size_t buflen, IP_Port *ip_port, ssize_t res)
|
||||||
|
{
|
||||||
|
snprintf(logbuffer, sizeof(logbuffer), "[%2u] %3u%c %s %s:%u (%u: %s) | %04x%04x\n",
|
||||||
|
buffer[0], res < 0 ? (buflen & 0xFF) : res,
|
||||||
|
res < 0 ? '-' : (res == buflen ? '=' : '+'),
|
||||||
|
message, ip_ntoa(&ip_port->ip), ntohs(ip_port->port), res < 0 ? errno : 0,
|
||||||
|
res < 0 ? strerror(errno) : "OK", buflen > 4 ? ntohl(*(uint32_t *)&buffer[1]) : 0,
|
||||||
|
buflen > 7 ? ntohl(*(uint32_t *)(&buffer[5])) : 0);
|
||||||
|
logbuffer[sizeof(logbuffer) - 1] = 0;
|
||||||
|
loglog(logbuffer);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
|
@ -119,6 +119,7 @@ typedef struct {
|
||||||
} IPAny_Port;
|
} IPAny_Port;
|
||||||
|
|
||||||
#undef TOX_ENABLE_IPV6
|
#undef TOX_ENABLE_IPV6
|
||||||
|
/* #define TOX_ENABLE_IPV6 */
|
||||||
#ifdef TOX_ENABLE_IPV6
|
#ifdef TOX_ENABLE_IPV6
|
||||||
#define TOX_ENABLE_IPV6_DEFAULT 1
|
#define TOX_ENABLE_IPV6_DEFAULT 1
|
||||||
typedef IPAny IP;
|
typedef IPAny IP;
|
||||||
|
|
|
@ -26,6 +26,22 @@
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#ifdef WIN32
|
||||||
|
#ifndef WINVER
|
||||||
|
//Windows XP
|
||||||
|
#define WINVER 0x0501
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <winsock2.h>
|
||||||
|
#include <windows.h>
|
||||||
|
#include <ws2tcpip.h>
|
||||||
|
|
||||||
|
#else
|
||||||
|
|
||||||
|
#include <netinet/ip.h>
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
@ -71,6 +87,7 @@ typedef struct {
|
||||||
} tox_IPAny_Port;
|
} tox_IPAny_Port;
|
||||||
|
|
||||||
#undef TOX_ENABLE_IPV6
|
#undef TOX_ENABLE_IPV6
|
||||||
|
/* #define TOX_ENABLE_IPV6 */
|
||||||
#ifdef TOX_ENABLE_IPV6
|
#ifdef TOX_ENABLE_IPV6
|
||||||
#define TOX_ENABLE_IPV6_DEFAULT 1
|
#define TOX_ENABLE_IPV6_DEFAULT 1
|
||||||
typedef tox_IPAny tox_IP;
|
typedef tox_IPAny tox_IP;
|
||||||
|
|
|
@ -10,11 +10,12 @@
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <stdint.h>
|
|
||||||
#include <stdbool.h>
|
|
||||||
|
|
||||||
|
/* for CLIENT_ID_SIZE */
|
||||||
#include "DHT.h"
|
#include "DHT.h"
|
||||||
|
|
||||||
|
#include "util.h"
|
||||||
|
|
||||||
uint64_t now()
|
uint64_t now()
|
||||||
{
|
{
|
||||||
return time(NULL);
|
return time(NULL);
|
||||||
|
@ -41,3 +42,30 @@ void id_cpy(uint8_t *dest, uint8_t *src)
|
||||||
{
|
{
|
||||||
memcpy(dest, src, CLIENT_ID_SIZE);
|
memcpy(dest, src, CLIENT_ID_SIZE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef LOGGING
|
||||||
|
char logbuffer[512];
|
||||||
|
static FILE *logfile = NULL;
|
||||||
|
void loginit(uint16_t port)
|
||||||
|
{
|
||||||
|
if (logfile)
|
||||||
|
fclose(logfile);
|
||||||
|
|
||||||
|
sprintf(logbuffer, "%u-%u.log", ntohs(port), now);
|
||||||
|
logfile = fopen(logbuffer, "w");
|
||||||
|
};
|
||||||
|
void loglog(char *text)
|
||||||
|
{
|
||||||
|
if (logfile) {
|
||||||
|
fprintf(logfile, text);
|
||||||
|
fflush(logfile);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
void logexit()
|
||||||
|
{
|
||||||
|
if (logfile) {
|
||||||
|
fclose(logfile);
|
||||||
|
logfile = NULL;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
#endif
|
||||||
|
|
|
@ -5,7 +5,24 @@
|
||||||
* Copyright 2013 plutooo
|
* Copyright 2013 plutooo
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#ifndef __UTIL_H__
|
||||||
|
#define __UTIL_H__
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
uint64_t now();
|
uint64_t now();
|
||||||
uint64_t random_64b();
|
uint64_t random_64b();
|
||||||
bool id_eq(uint8_t *dest, uint8_t *src);
|
bool id_eq(uint8_t *dest, uint8_t *src);
|
||||||
void id_cpy(uint8_t *dest, uint8_t *src);
|
void id_cpy(uint8_t *dest, uint8_t *src);
|
||||||
|
|
||||||
|
#undef LOGGING
|
||||||
|
// #define LOGGING
|
||||||
|
#ifdef LOGGING
|
||||||
|
extern char logbuffer[512];
|
||||||
|
void loginit(uint16_t port);
|
||||||
|
void loglog(char *text);
|
||||||
|
void logexit();
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif /* __UTIL_H__ */
|
||||||
|
|
Loading…
Reference in New Issue
Block a user