From c7f7e30c7521bcdafbddd9d21af288442c325a72 Mon Sep 17 00:00:00 2001 From: irungentoo Date: Wed, 26 Jun 2013 09:56:15 -0400 Subject: Moved the network functions from the DHT into network. Also made a nice function to init networking. --- CMakeLists.txt | 3 ++- core/DHT.c | 42 ++++++----------------------- core/DHT.h | 74 +++++++------------------------------------------- core/network.c | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++ core/network.h | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ testing/DHT_test.c | 28 +++++-------------- 6 files changed, 178 insertions(+), 122 deletions(-) create mode 100644 core/network.c create mode 100644 core/network.h diff --git a/CMakeLists.txt b/CMakeLists.txt index c1fa8cad..388e6d91 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/core/DHT.c b/core/DHT.c index 6ecae785..6b65e763 100644 --- a/core/DHT.c +++ b/core/DHT.c @@ -1,39 +1,13 @@ -#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)); - -} +/* DHT.c +* +* An implementation of the DHT as seen in docs/DHT.txt +* +*/ -//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; - -} +#include "DHT.h" +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; diff --git a/core/DHT.h b/core/DHT.h index c35b17d5..edd4236a 100644 --- a/core/DHT.h +++ b/core/DHT.h @@ -1,25 +1,14 @@ -#ifndef DHT_H -#define DHT_H - -#include -#include -#include -#include -#include +/* DHT.h +* +* An implementation of the DHT as seen in docs/DHT.txt +* +*/ -#ifdef WIN32 //Put win32 includes here -#include -#include - -#else //Linux includes - -#include -#include -#include -#include +#ifndef DHT_H +#define DHT_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 \ No newline at end of file diff --git a/core/network.c b/core/network.c new file mode 100644 index 00000000..10a37b80 --- /dev/null +++ b/core/network.c @@ -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; + +} \ No newline at end of file diff --git a/core/network.h b/core/network.h new file mode 100644 index 00000000..17b29f2c --- /dev/null +++ b/core/network.h @@ -0,0 +1,79 @@ +/* network.h +* +* Datatypes, functions and includes for the core networking. +* +*/ + + +#ifndef NETWORK_H +#define NETWORK_H + +#include +#include +#include +#include +#include + +#ifdef WIN32 //Put win32 includes here + +#include +#include + +#else //Linux includes + +#include +#include +#include +#include + +#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 \ No newline at end of file diff --git a/testing/DHT_test.c b/testing/DHT_test.c index 53712e6e..03a49a3e 100644 --- a/testing/DHT_test.c +++ b/testing/DHT_test.c @@ -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])); -- cgit v1.2.3