mirror of
https://github.com/irungentoo/toxcore.git
synced 2024-03-22 13:30:51 +08:00
More changes made to comments, as requested by jvrv
This commit is contained in:
parent
5101ef756a
commit
9616cc6a89
|
@ -39,11 +39,11 @@ typedef struct
|
|||
uint8_t recv_nonce[crypto_box_NONCEBYTES]; /* nonce of received packets */
|
||||
uint8_t sent_nonce[crypto_box_NONCEBYTES]; /* nonce of sent packets. */
|
||||
uint8_t sessionpublic_key[crypto_box_PUBLICKEYBYTES]; /* our public key for this session. */
|
||||
uint8_t sessionsecret_key[crypto_box_SECRETKEYBYTES];our private key for this session.
|
||||
uint8_t sessionsecret_key[crypto_box_SECRETKEYBYTES]; /* our private key for this session. */
|
||||
uint8_t peersessionpublic_key[crypto_box_PUBLICKEYBYTES]; /* The public key of the peer. */
|
||||
uint8_t status;/* 0 if no connection, 1 we have sent a handshake, 2 if connexion is not confirmed yet
|
||||
(we have received a handshake but no empty data packet), 3 if the connection is established.
|
||||
4 if the connection is timed out. */
|
||||
uint8_t status; /* 0 if no connection, 1 we have sent a handshake, 2 if connexion is not confirmed yet
|
||||
(we have received a handshake but no empty data packet), 3 if the connection is established.
|
||||
4 if the connection is timed out. */
|
||||
uint16_t number; /* Lossless_UDP connection number corresponding to this connection. */
|
||||
|
||||
}Crypto_Connection;
|
||||
|
@ -65,7 +65,7 @@ static int incoming_connections[MAX_INCOMING];
|
|||
/* encrypts plain of length length to encrypted of length + 16 using the
|
||||
public key(32 bytes) of the receiver and the secret key of the sender and a 24 byte nonce
|
||||
return -1 if there was a problem.
|
||||
return length of encrypted data if everything was fine. */
|
||||
return length of encrypted data if everything was fine. */
|
||||
int encrypt_data(uint8_t * public_key, uint8_t * secret_key, uint8_t * nonce,
|
||||
uint8_t * plain, uint32_t length, uint8_t * encrypted)
|
||||
{
|
||||
|
@ -128,9 +128,9 @@ int decrypt_data(uint8_t * public_key, uint8_t * secret_key, uint8_t * nonce,
|
|||
void increment_nonce(uint8_t * nonce)
|
||||
{
|
||||
uint32_t i;
|
||||
for(i = 0; i < crypto_box_NONCEBYTES; ++i)
|
||||
for(i = 0; i < crypto_box_NONCEBYTES; i++)
|
||||
{
|
||||
++nonce[i];
|
||||
nonce[i]++;
|
||||
if(nonce[i] != 0)
|
||||
{
|
||||
break;
|
||||
|
@ -143,7 +143,7 @@ void increment_nonce(uint8_t * nonce)
|
|||
void random_nonce(uint8_t * nonce)
|
||||
{
|
||||
uint32_t i;
|
||||
for(i = 0; i < crypto_box_NONCEBYTES; ++i)
|
||||
for(i = 0; i < crypto_box_NONCEBYTES; i++)
|
||||
{
|
||||
nonce[i] = random_int() % 256;
|
||||
}
|
||||
|
@ -228,7 +228,7 @@ int send_friendrequest(uint8_t * public_key, IP_Port ip_port, uint8_t * data, ui
|
|||
return -1;
|
||||
}
|
||||
uint32_t i;
|
||||
for(i = 0; i < MAX_FRIEND_REQUESTS; ++i)
|
||||
for(i = 0; i < MAX_FRIEND_REQUESTS; i++)
|
||||
{
|
||||
if(outbound_friendrequests[i] == -1)
|
||||
{
|
||||
|
@ -365,7 +365,7 @@ int handle_cryptohandshake(uint8_t * public_key, uint8_t * secret_nonce,
|
|||
int handle_friendrequest(uint8_t * public_key, uint8_t * data)
|
||||
{
|
||||
uint32_t i;
|
||||
for(i = 0; i < MAX_INCOMING; ++i)
|
||||
for(i = 0; i < MAX_INCOMING; i++)
|
||||
{
|
||||
if(incoming_connections[i] != -1)
|
||||
{
|
||||
|
@ -403,7 +403,7 @@ int handle_friendrequest(uint8_t * public_key, uint8_t * data)
|
|||
int getcryptconnection_id(uint8_t * public_key)
|
||||
{
|
||||
uint32_t i;
|
||||
for(i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i)
|
||||
for(i = 0; i < MAX_CRYPTO_CONNECTIONS; i++)
|
||||
{
|
||||
if(crypto_connections[i].status > 0)
|
||||
{
|
||||
|
@ -432,7 +432,7 @@ int crypto_connect(uint8_t * public_key, IP_Port ip_port)
|
|||
return -1;
|
||||
}
|
||||
}
|
||||
for(i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i)
|
||||
for(i = 0; i < MAX_CRYPTO_CONNECTIONS; i++)
|
||||
{
|
||||
if(crypto_connections[i].status == 0)
|
||||
{
|
||||
|
@ -469,7 +469,7 @@ int crypto_connect(uint8_t * public_key, IP_Port ip_port)
|
|||
int crypto_inbound(uint8_t * public_key, uint8_t * secret_nonce, uint8_t * session_key)
|
||||
{
|
||||
uint32_t i;
|
||||
for(i = 0; i < MAX_INCOMING; ++i)
|
||||
for(i = 0; i < MAX_INCOMING; i++)
|
||||
{
|
||||
if(incoming_connections[i] != -1)
|
||||
{
|
||||
|
@ -530,7 +530,7 @@ int accept_crypto_inbound(int connection_id, uint8_t * public_key, uint8_t * sec
|
|||
{
|
||||
return -1;
|
||||
}*/
|
||||
for(i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i)
|
||||
for(i = 0; i < MAX_CRYPTO_CONNECTIONS; i++)
|
||||
{
|
||||
if(crypto_connections[i].status == 0)
|
||||
{
|
||||
|
@ -603,7 +603,7 @@ void load_keys(uint8_t * keys)
|
|||
int new_incoming(int id)
|
||||
{
|
||||
uint32_t i;
|
||||
for(i = 0; i < MAX_INCOMING; ++i)
|
||||
for(i = 0; i < MAX_INCOMING; i++)
|
||||
{
|
||||
if(incoming_connections[i] == -1)
|
||||
{
|
||||
|
@ -633,7 +633,7 @@ static void handle_incomings()
|
|||
static void receive_crypto()
|
||||
{
|
||||
uint32_t i;
|
||||
for(i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i)
|
||||
for(i = 0; i < MAX_CRYPTO_CONNECTIONS; i++)
|
||||
{
|
||||
if(crypto_connections[i].status == 1)
|
||||
{
|
||||
|
@ -717,7 +717,7 @@ void initNetCrypto()
|
|||
memset(outbound_friendrequests, -1 ,sizeof(outbound_friendrequests));
|
||||
memset(incoming_connections, -1 ,sizeof(incoming_connections));
|
||||
uint32_t i;
|
||||
for(i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i)
|
||||
for(i = 0; i < MAX_CRYPTO_CONNECTIONS; i++)
|
||||
{
|
||||
crypto_connections[i].number = ~0;
|
||||
}
|
||||
|
|
197
core/network.h
197
core/network.h
|
@ -1,6 +1,6 @@
|
|||
/* network.h
|
||||
*
|
||||
* Functions for the core networking.
|
||||
* Datatypes, functions and includes for the core networking.
|
||||
*
|
||||
|
||||
Copyright (C) 2013 Tox project All Rights Reserved.
|
||||
|
@ -21,81 +21,94 @@
|
|||
along with Tox. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
*/
|
||||
|
||||
|
||||
#include "network.h"
|
||||
#ifndef NETWORK_H
|
||||
#define NETWORK_H
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
|
||||
/* returns current UNIX time in microseconds (us). */
|
||||
uint64_t current_time()
|
||||
|
||||
#ifdef WIN32 /* Put win32 includes here */
|
||||
|
||||
#include <winsock2.h>
|
||||
#include <windows.h>
|
||||
|
||||
#undef VANILLA_NACL /* make sure on windows we use libsodium */
|
||||
|
||||
#else //Linux includes
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <errno.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
#ifndef VANILLA_NACL
|
||||
/* we use libsodium by default */
|
||||
#include <sodium.h>
|
||||
#else
|
||||
|
||||
/* TODO: Including stuff like this is bad. This needs fixing.
|
||||
We keep support for the original NaCl for now. */
|
||||
#include "../nacl/build/Linux/include/amd64/crypto_box.h"
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
#define MAX_UDP_PACKET_SIZE 65507
|
||||
|
||||
typedef union
|
||||
{
|
||||
uint64_t time;
|
||||
#ifdef WIN32
|
||||
/* This probably works fine */
|
||||
FILETIME ft;
|
||||
GetSystemTimeAsFileTime(&ft);
|
||||
time = ft.dwHighDateTime;
|
||||
time <<=32;
|
||||
time |= ft.dwLowDateTime;
|
||||
time -= 116444736000000000UL;
|
||||
return time/10;
|
||||
#else
|
||||
struct timeval a;
|
||||
gettimeofday(&a, NULL);
|
||||
time = 1000000UL*a.tv_sec + a.tv_usec;
|
||||
return time;
|
||||
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;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
int16_t family;
|
||||
uint16_t port;
|
||||
IP ip;
|
||||
uint8_t zeroes[8];
|
||||
#ifdef ENABLE_IPV6
|
||||
uint8_t zeroes2[12];
|
||||
#endif
|
||||
|
||||
|
||||
}
|
||||
}ADDR;
|
||||
|
||||
|
||||
/* returns current time in milleseconds since the epoch. */
|
||||
uint64_t current_time();
|
||||
|
||||
/* return a random number
|
||||
NOTE: this function should probably not be used where cryptographic randomness is absolutely necessary */
|
||||
uint32_t random_int()
|
||||
{
|
||||
#ifndef VANILLA_NACL
|
||||
/* NOTE: this function comes from libsodium */
|
||||
return randombytes_random();
|
||||
#else
|
||||
return random();
|
||||
#endif
|
||||
}
|
||||
uint32_t random_int();
|
||||
|
||||
/* our UDP socket, a global variable. */
|
||||
static int sock;
|
||||
/* Basic network functions: */
|
||||
|
||||
/* Basic network functions:
|
||||
Function to send packet(data) of length length to ip_port */
|
||||
int sendpacket(IP_Port ip_port, uint8_t * data, uint32_t length)
|
||||
{
|
||||
ADDR addr = {AF_INET, ip_port.port, ip_port.ip};
|
||||
return sendto(sock,(char *) data, length, 0, (struct sockaddr *)&addr, sizeof(addr));
|
||||
|
||||
}
|
||||
/* Function to send packet(data) of length length to ip_port */
|
||||
int sendpacket(IP_Port ip_port, uint8_t * data, uint32_t length);
|
||||
|
||||
/* Function to receive data, ip and port of sender is put into ip_port
|
||||
the packet data into data
|
||||
the packet length into length.
|
||||
dump all empty packets. */
|
||||
int receivepacket(IP_Port * ip_port, uint8_t * data, uint32_t * length)
|
||||
{
|
||||
ADDR addr;
|
||||
#ifdef WIN32
|
||||
int addrlen = sizeof(addr);
|
||||
#else
|
||||
uint32_t addrlen = sizeof(addr);
|
||||
#endif
|
||||
(*(int32_t *)length) = recvfrom(sock,(char *) data, MAX_UDP_PACKET_SIZE, 0, (struct sockaddr *)&addr, &addrlen);
|
||||
if(*(int32_t *)length <= 0)
|
||||
{
|
||||
/* nothing received
|
||||
or empty packet */
|
||||
return -1;
|
||||
}
|
||||
ip_port->ip = addr.ip;
|
||||
ip_port->port = addr.port;
|
||||
return 0;
|
||||
|
||||
}
|
||||
the packet length into length. */
|
||||
int receivepacket(IP_Port * ip_port, uint8_t * data, uint32_t * length);
|
||||
|
||||
/* initialize networking
|
||||
bind to ip and port
|
||||
|
@ -103,59 +116,9 @@ int receivepacket(IP_Port * ip_port, uint8_t * data, uint32_t * length)
|
|||
port is in host byte order (this means don't worry about it)
|
||||
returns 0 if no problems
|
||||
TODO: add something to check if there are errors */
|
||||
int init_networking(IP ip ,uint16_t port)
|
||||
{
|
||||
#ifdef WIN32
|
||||
WSADATA wsaData;
|
||||
if(WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
#else
|
||||
srandom((uint32_t)current_time());
|
||||
#endif
|
||||
srand((uint32_t)current_time());
|
||||
|
||||
/* initialize our socket */
|
||||
sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
||||
|
||||
/* Functions to increase the size of the send and receive UDP buffers
|
||||
NOTE: uncomment if necessary
|
||||
|
||||
int n = 1024 * 1024 * 2;
|
||||
if(setsockopt(sock, SOL_SOCKET, SO_RCVBUF, (char*)&n, sizeof(n)) == -1)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
int init_networking(IP ip ,uint16_t port);
|
||||
|
||||
if(setsockopt(sock, SOL_SOCKET, SO_SNDBUF, (char*)&n, sizeof(n)) == -1)
|
||||
{
|
||||
return -1;
|
||||
}*/
|
||||
|
||||
/*Set socket nonblocking */
|
||||
#ifdef WIN32
|
||||
/* I think this works for windows */
|
||||
u_long mode = 1;
|
||||
/* ioctl(sock, FIONBIO, &mode); */
|
||||
ioctlsocket(sock, FIONBIO, &mode);
|
||||
#else
|
||||
fcntl(sock, F_SETFL, O_NONBLOCK, 1);
|
||||
#endif
|
||||
|
||||
/* Bind our socket to port PORT and address 0.0.0.0 */
|
||||
ADDR addr = {AF_INET, htons(port), ip};
|
||||
bind(sock, (struct sockaddr*)&addr, sizeof(addr));
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
/* function to cleanup networking stuff */
|
||||
void shutdown_networking()
|
||||
{
|
||||
#ifdef WIN32
|
||||
WSACleanup();
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
/* function to cleanup networking stuff(doesn't do much right now) */
|
||||
void shutdown_networking();
|
||||
#endif
|
||||
|
|
Loading…
Reference in New Issue
Block a user