mirror of
https://github.com/irungentoo/toxcore.git
synced 2024-03-22 13:30:51 +08:00
Moved the network functions from the DHT into network.
Also made a nice function to init networking.
This commit is contained in:
parent
f7574c61fc
commit
c7f7e30c75
|
@ -2,7 +2,8 @@ cmake_minimum_required(VERSION 2.6.0)
|
|||
project(TOX C)
|
||||
|
||||
set(core_sources
|
||||
core/DHT.c)
|
||||
core/DHT.c
|
||||
core/network.c)
|
||||
|
||||
set(test_sources
|
||||
testing/DHT_test.c)
|
||||
|
|
44
core/DHT.c
44
core/DHT.c
|
@ -1,39 +1,13 @@
|
|||
/* DHT.c
|
||||
*
|
||||
* An implementation of the DHT as seen in docs/DHT.txt
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#include "DHT.h"
|
||||
|
||||
|
||||
//Basic network functions:
|
||||
//TODO: put them somewhere else than here
|
||||
|
||||
//Function to send packet(data) of length length to ip_port
|
||||
int sendpacket(IP_Port ip_port, char * data, uint32_t length)
|
||||
{
|
||||
ADDR addr = {AF_INET, ip_port.port, ip_port.ip};
|
||||
return sendto(sock, data, length, 0, (struct sockaddr *)&addr, sizeof(addr));
|
||||
|
||||
}
|
||||
|
||||
//Function to recieve 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 recievepacket(IP_Port * ip_port, char * data, uint32_t * length)
|
||||
{
|
||||
ADDR addr;
|
||||
uint32_t addrlen = sizeof(addr);
|
||||
(*(int *)length) = recvfrom(sock, data, MAX_UDP_PACKET_SIZE, 0, (struct sockaddr *)&addr, &addrlen);
|
||||
if(*(int *)length <= 0)
|
||||
{
|
||||
//nothing recieved
|
||||
//or empty packet
|
||||
return -1;
|
||||
}
|
||||
ip_port->ip = addr.ip;
|
||||
ip_port->port = addr.port;
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
|
||||
char self_client_id[CLIENT_ID_SIZE];
|
||||
|
||||
//Compares client_id1 and client_id2 with client_id
|
||||
//return 0 if both are same distance
|
||||
|
@ -744,7 +718,7 @@ void doFriends()
|
|||
static uint32_t close_lastgetnodes;
|
||||
|
||||
//Ping each client in the close nodes list every 60 seconds.
|
||||
//Send a get nodes request every 20 seconds to a random good node int the list.
|
||||
//Send a get nodes request every 20 seconds to a random good node in the list.
|
||||
void doClose()//tested
|
||||
{
|
||||
uint32_t i;
|
||||
|
|
74
core/DHT.h
74
core/DHT.h
|
@ -1,25 +1,14 @@
|
|||
/* DHT.h
|
||||
*
|
||||
* An implementation of the DHT as seen in docs/DHT.txt
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#ifndef DHT_H
|
||||
#define DHT_H
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
#ifdef WIN32 //Put win32 includes here
|
||||
|
||||
#include <winsock2.h>
|
||||
#include <windows.h>
|
||||
|
||||
#else //Linux includes
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <errno.h>
|
||||
|
||||
#endif
|
||||
#include "network.h"
|
||||
|
||||
//Current time, unix format
|
||||
#define unix_time() ((uint32_t)time(NULL))
|
||||
|
@ -27,24 +16,6 @@
|
|||
//size of the client_id in bytes
|
||||
#define CLIENT_ID_SIZE 32
|
||||
|
||||
#define MAX_UDP_PACKET_SIZE 65507
|
||||
|
||||
typedef union
|
||||
{
|
||||
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
|
||||
{
|
||||
char client_id[CLIENT_ID_SIZE];
|
||||
|
@ -77,19 +48,6 @@ typedef struct
|
|||
}Pinged;
|
||||
|
||||
|
||||
typedef struct
|
||||
{
|
||||
int16_t family;
|
||||
uint16_t port;
|
||||
IP ip;
|
||||
uint8_t zeroes[8];
|
||||
#ifdef ENABLE_IPV6
|
||||
uint8_t zeroes2[12];
|
||||
#endif
|
||||
}ADDR;
|
||||
|
||||
|
||||
|
||||
//Add a new friend to the friends list
|
||||
//client_id must be CLIENT_ID_SIZE bytes long.
|
||||
//returns 0 if success
|
||||
|
@ -133,11 +91,8 @@ void bootstrap(IP_Port ip_port);
|
|||
//Global variables
|
||||
|
||||
//Our client id
|
||||
char self_client_id[CLIENT_ID_SIZE];
|
||||
extern char self_client_id[CLIENT_ID_SIZE];
|
||||
|
||||
//Our UDP socket.
|
||||
//We only use one so it's much easier to have it as a global variable
|
||||
int sock;
|
||||
|
||||
//TODO: Move these out of here and put them into the .c file.
|
||||
//A list of the clients mathematically closest to ours.
|
||||
|
@ -162,15 +117,4 @@ Pinged pings[LPING_ARRAY];
|
|||
Pinged send_nodes[LSEND_NODES_ARRAY];
|
||||
|
||||
|
||||
//Basic network functions:
|
||||
//TODO: put them somewhere else than here
|
||||
|
||||
//Function to send packet(data) of length length to ip_port
|
||||
int sendpacket(IP_Port ip_port, char * data, uint32_t length);
|
||||
|
||||
//Function to recieve data, ip and port of sender is put into ip_port
|
||||
//the packet data into data
|
||||
//the packet length into length.
|
||||
int recievepacket(IP_Port * ip_port, char * data, uint32_t * length);
|
||||
|
||||
#endif
|
74
core/network.c
Normal file
74
core/network.c
Normal file
|
@ -0,0 +1,74 @@
|
|||
|
||||
|
||||
#include "network.h"
|
||||
|
||||
//our UDP socket, a global variable.
|
||||
static int sock;
|
||||
|
||||
//Basic network functions:
|
||||
//TODO: put them somewhere else than here
|
||||
|
||||
//Function to send packet(data) of length length to ip_port
|
||||
int sendpacket(IP_Port ip_port, char * data, uint32_t length)
|
||||
{
|
||||
ADDR addr = {AF_INET, ip_port.port, ip_port.ip};
|
||||
return sendto(sock, data, length, 0, (struct sockaddr *)&addr, sizeof(addr));
|
||||
|
||||
}
|
||||
|
||||
//Function to recieve 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 recievepacket(IP_Port * ip_port, char * data, uint32_t * length)
|
||||
{
|
||||
ADDR addr;
|
||||
uint32_t addrlen = sizeof(addr);
|
||||
(*(int *)length) = recvfrom(sock, data, MAX_UDP_PACKET_SIZE, 0, (struct sockaddr *)&addr, &addrlen);
|
||||
if(*(int *)length <= 0)
|
||||
{
|
||||
//nothing recieved
|
||||
//or empty packet
|
||||
return -1;
|
||||
}
|
||||
ip_port->ip = addr.ip;
|
||||
ip_port->port = addr.port;
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
//initialize networking
|
||||
//bind to ip and port
|
||||
//ip must be in network order EX: 127.0.0.1 = (7F000001)
|
||||
//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;
|
||||
}
|
||||
#endif
|
||||
|
||||
//initialize our socket
|
||||
sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
||||
|
||||
//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;
|
||||
|
||||
}
|
79
core/network.h
Normal file
79
core/network.h
Normal file
|
@ -0,0 +1,79 @@
|
|||
/* network.h
|
||||
*
|
||||
* Datatypes, functions and includes for the core networking.
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#ifndef NETWORK_H
|
||||
#define NETWORK_H
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
#ifdef WIN32 //Put win32 includes here
|
||||
|
||||
#include <winsock2.h>
|
||||
#include <windows.h>
|
||||
|
||||
#else //Linux includes
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <errno.h>
|
||||
|
||||
#endif
|
||||
|
||||
#define MAX_UDP_PACKET_SIZE 65507
|
||||
|
||||
typedef union
|
||||
{
|
||||
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;
|
||||
|
||||
|
||||
|
||||
|
||||
//Basic network functions:
|
||||
|
||||
//Function to send packet(data) of length length to ip_port
|
||||
int sendpacket(IP_Port ip_port, char * data, uint32_t length);
|
||||
|
||||
//Function to recieve data, ip and port of sender is put into ip_port
|
||||
//the packet data into data
|
||||
//the packet length into length.
|
||||
int recievepacket(IP_Port * ip_port, char * data, uint32_t * length);
|
||||
|
||||
//initialize networking
|
||||
//bind to ip and port
|
||||
//ip must be in network order EX: 127.0.0.1 = (7F000001)
|
||||
//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);
|
||||
#endif
|
|
@ -87,35 +87,19 @@ int main(int argc, char *argv[])
|
|||
memcpy(self_client_id, &randdomnum, 4);
|
||||
//memcpy(self_client_id, "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq", 32);
|
||||
|
||||
#ifdef WIN32
|
||||
WSADATA wsaData;
|
||||
if(WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
#endif
|
||||
|
||||
if (argc < 4) {
|
||||
printf("usage %s ip port client_id(of friend to find ip_port of)\n", argv[0]);
|
||||
exit(0);
|
||||
}
|
||||
addfriend(argv[3]);
|
||||
|
||||
//initialize our socket
|
||||
sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
||||
//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), {{0}}};
|
||||
bind(sock, (struct sockaddr*)&addr, sizeof(addr));
|
||||
//initialize networking
|
||||
//bind to ip 0.0.0.0:PORT
|
||||
IP ip;
|
||||
ip.i = 0;
|
||||
init_networking(ip, PORT);
|
||||
|
||||
perror("Initialization");
|
||||
IP_Port bootstrap_ip_port;
|
||||
bootstrap_ip_port.port = htons(atoi(argv[2]));
|
||||
|
|
Loading…
Reference in New Issue
Block a user