2013-06-27 03:02:42 +08:00
|
|
|
/* network.h
|
2013-07-27 20:43:36 +08:00
|
|
|
*
|
2013-07-26 09:45:56 +08:00
|
|
|
* Functions for the core networking.
|
2013-07-27 20:43:36 +08:00
|
|
|
*
|
2013-07-26 09:45:56 +08:00
|
|
|
* Copyright (C) 2013 Tox project All Rights Reserved.
|
|
|
|
*
|
|
|
|
* This file is part of Tox.
|
|
|
|
*
|
|
|
|
* Tox is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Tox is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Tox. If not, see <http://www.gnu.org/licenses/>.
|
2013-07-27 20:43:36 +08:00
|
|
|
*
|
2013-07-26 09:45:56 +08:00
|
|
|
*/
|
2013-06-26 21:56:15 +08:00
|
|
|
|
|
|
|
#include "network.h"
|
|
|
|
|
2013-07-20 22:39:05 +08:00
|
|
|
/* returns current UNIX time in microseconds (us). */
|
2013-08-05 21:57:17 +08:00
|
|
|
uint64_t current_time(void)
|
2013-06-27 19:37:06 +08:00
|
|
|
{
|
|
|
|
uint64_t time;
|
2013-07-27 20:43:36 +08:00
|
|
|
#ifdef WIN32
|
2013-07-20 22:39:05 +08:00
|
|
|
/* This probably works fine */
|
2013-07-04 04:35:44 +08:00
|
|
|
FILETIME ft;
|
|
|
|
GetSystemTimeAsFileTime(&ft);
|
|
|
|
time = ft.dwHighDateTime;
|
2013-08-17 01:11:09 +08:00
|
|
|
time <<= 32;
|
2013-07-04 04:35:44 +08:00
|
|
|
time |= ft.dwLowDateTime;
|
|
|
|
time -= 116444736000000000UL;
|
2013-08-17 01:11:09 +08:00
|
|
|
return time / 10;
|
2013-07-27 20:43:36 +08:00
|
|
|
#else
|
2013-06-27 19:37:06 +08:00
|
|
|
struct timeval a;
|
|
|
|
gettimeofday(&a, NULL);
|
2013-08-17 01:11:09 +08:00
|
|
|
time = 1000000UL * a.tv_sec + a.tv_usec;
|
2013-06-27 19:37:06 +08:00
|
|
|
return time;
|
2013-07-27 20:43:36 +08:00
|
|
|
#endif
|
2013-06-27 19:37:06 +08:00
|
|
|
}
|
|
|
|
|
2013-07-20 22:39:05 +08:00
|
|
|
/* return a random number
|
|
|
|
NOTE: this function should probably not be used where cryptographic randomness is absolutely necessary */
|
2013-08-05 21:57:17 +08:00
|
|
|
uint32_t random_int(void)
|
2013-06-28 05:19:09 +08:00
|
|
|
{
|
2013-07-27 20:43:36 +08:00
|
|
|
#ifndef VANILLA_NACL
|
2013-07-04 04:35:44 +08:00
|
|
|
//NOTE: this function comes from libsodium
|
|
|
|
return randombytes_random();
|
2013-07-27 20:43:36 +08:00
|
|
|
#else
|
2013-06-28 05:19:09 +08:00
|
|
|
return random();
|
2013-07-27 20:43:36 +08:00
|
|
|
#endif
|
2013-06-28 05:19:09 +08:00
|
|
|
}
|
|
|
|
|
2013-07-20 22:39:05 +08:00
|
|
|
/* Basic network functions:
|
|
|
|
Function to send packet(data) of length length to ip_port */
|
2013-08-20 11:54:28 +08:00
|
|
|
int sendpacket(int sock, IP_Port ip_port, uint8_t *data, uint32_t length)
|
2013-06-26 21:56:15 +08:00
|
|
|
{
|
2013-07-27 20:43:36 +08:00
|
|
|
ADDR addr = {AF_INET, ip_port.port, ip_port.ip};
|
2013-08-17 01:11:09 +08:00
|
|
|
return sendto(sock, (char *) data, length, 0, (struct sockaddr *)&addr, sizeof(addr));
|
2013-06-26 21:56:15 +08:00
|
|
|
}
|
|
|
|
|
2013-07-20 22:39:05 +08:00
|
|
|
/* 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. */
|
2013-08-20 11:54:28 +08:00
|
|
|
static int receivepacket(int sock, IP_Port *ip_port, uint8_t *data, uint32_t *length)
|
2013-06-26 21:56:15 +08:00
|
|
|
{
|
|
|
|
ADDR addr;
|
2013-07-27 20:43:36 +08:00
|
|
|
#ifdef WIN32
|
2013-07-04 04:35:44 +08:00
|
|
|
int addrlen = sizeof(addr);
|
2013-07-27 20:43:36 +08:00
|
|
|
#else
|
2013-06-26 21:56:15 +08:00
|
|
|
uint32_t addrlen = sizeof(addr);
|
2013-07-27 20:43:36 +08:00
|
|
|
#endif
|
2013-08-17 01:11:09 +08:00
|
|
|
(*(int32_t *)length) = recvfrom(sock, (char *) data, MAX_UDP_PACKET_SIZE, 0, (struct sockaddr *)&addr, &addrlen);
|
|
|
|
|
|
|
|
if (*(int32_t *)length <= 0)
|
2013-07-27 11:07:25 +08:00
|
|
|
return -1; /* nothing received or empty packet */
|
2013-07-27 20:43:36 +08:00
|
|
|
|
2013-06-26 21:56:15 +08:00
|
|
|
ip_port->ip = addr.ip;
|
|
|
|
ip_port->port = addr.port;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-08-21 07:37:05 +08:00
|
|
|
void networking_registerhandler(Networking_Core *net, uint8_t byte, packet_handler_callback cb, void *object)
|
2013-08-10 07:30:18 +08:00
|
|
|
{
|
2013-08-20 11:54:28 +08:00
|
|
|
net->packethandlers[byte].function = cb;
|
|
|
|
net->packethandlers[byte].object = object;
|
2013-08-10 07:30:18 +08:00
|
|
|
}
|
|
|
|
|
2013-08-21 07:37:05 +08:00
|
|
|
void networking_poll(Networking_Core *net)
|
2013-08-10 07:30:18 +08:00
|
|
|
{
|
2013-08-10 18:18:25 +08:00
|
|
|
IP_Port ip_port;
|
|
|
|
uint8_t data[MAX_UDP_PACKET_SIZE];
|
|
|
|
uint32_t length;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-20 11:54:28 +08:00
|
|
|
while (receivepacket(net->sock, &ip_port, data, &length) != -1) {
|
2013-08-10 18:18:25 +08:00
|
|
|
if (length < 1) continue;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-20 11:54:28 +08:00
|
|
|
if (!(net->packethandlers[data[0]].function)) continue;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-20 11:54:28 +08:00
|
|
|
net->packethandlers[data[0]].function(net->packethandlers[data[0]].object, ip_port, data, length);
|
2013-08-10 18:18:25 +08:00
|
|
|
}
|
2013-08-10 07:30:18 +08:00
|
|
|
}
|
|
|
|
|
2013-08-20 11:54:28 +08:00
|
|
|
uint8_t at_startup_ran;
|
|
|
|
static void at_startup(void)
|
2013-06-26 21:56:15 +08:00
|
|
|
{
|
2013-08-20 11:54:28 +08:00
|
|
|
if (at_startup_ran != 0)
|
|
|
|
return;
|
2013-08-21 07:37:05 +08:00
|
|
|
|
2013-07-27 20:43:36 +08:00
|
|
|
#ifdef WIN32
|
2013-06-26 21:56:15 +08:00
|
|
|
WSADATA wsaData;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
|
|
|
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != NO_ERROR)
|
2013-06-26 21:56:15 +08:00
|
|
|
return -1;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-07-27 20:43:36 +08:00
|
|
|
#else
|
2013-06-28 05:19:09 +08:00
|
|
|
srandom((uint32_t)current_time());
|
2013-07-27 20:43:36 +08:00
|
|
|
#endif
|
2013-08-21 07:37:05 +08:00
|
|
|
srand((uint32_t)current_time());
|
2013-08-20 11:54:28 +08:00
|
|
|
at_startup_ran = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* TODO: put this somewhere
|
|
|
|
static void at_shutdown(void)
|
|
|
|
{
|
|
|
|
#ifdef WIN32
|
|
|
|
WSACleanup();
|
2013-08-21 07:37:05 +08:00
|
|
|
#endif
|
2013-08-20 11:54:28 +08:00
|
|
|
}
|
|
|
|
*/
|
2013-07-27 06:10:22 +08:00
|
|
|
|
2013-08-20 11:54:28 +08:00
|
|
|
/* 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 Networking_Core object if no problems
|
|
|
|
returns NULL if there are problems */
|
2013-08-21 07:37:05 +08:00
|
|
|
Networking_Core *new_networking(IP ip, uint16_t port)
|
2013-08-20 11:54:28 +08:00
|
|
|
{
|
|
|
|
at_startup();
|
2013-07-20 22:39:05 +08:00
|
|
|
/* initialize our socket */
|
2013-08-21 07:37:05 +08:00
|
|
|
Networking_Core *temp = calloc(1, sizeof(Networking_Core));
|
|
|
|
|
2013-08-20 11:54:28 +08:00
|
|
|
if (temp == NULL)
|
2013-08-21 07:37:05 +08:00
|
|
|
return NULL;
|
|
|
|
|
2013-08-20 11:54:28 +08:00
|
|
|
temp->sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
2013-07-24 08:22:45 +08:00
|
|
|
|
2013-07-26 09:00:31 +08:00
|
|
|
/* Check for socket error */
|
2013-07-27 20:43:36 +08:00
|
|
|
#ifdef WIN32
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-20 11:54:28 +08:00
|
|
|
if (temp->sock == INVALID_SOCKET) { /* MSDN recommends this */
|
|
|
|
free(temp);
|
|
|
|
return NULL;
|
|
|
|
}
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-07-27 20:43:36 +08:00
|
|
|
#else
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-20 11:54:28 +08:00
|
|
|
if (temp->sock < 0) {
|
|
|
|
free(temp);
|
|
|
|
return NULL;
|
|
|
|
}
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-07-27 20:43:36 +08:00
|
|
|
#endif
|
2013-07-24 08:33:08 +08:00
|
|
|
|
2013-07-20 22:39:05 +08:00
|
|
|
/* Functions to increase the size of the send and receive UDP buffers
|
|
|
|
NOTE: uncomment if necessary */
|
2013-06-27 02:07:01 +08:00
|
|
|
/*
|
|
|
|
int n = 1024 * 1024 * 2;
|
|
|
|
if(setsockopt(sock, SOL_SOCKET, SO_RCVBUF, (char*)&n, sizeof(n)) == -1)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(setsockopt(sock, SOL_SOCKET, SO_SNDBUF, (char*)&n, sizeof(n)) == -1)
|
|
|
|
return -1;
|
2013-07-27 06:10:22 +08:00
|
|
|
*/
|
2013-07-27 20:43:36 +08:00
|
|
|
|
2013-07-27 06:10:22 +08:00
|
|
|
/* Enable broadcast on socket */
|
2013-07-26 09:00:31 +08:00
|
|
|
int broadcast = 1;
|
2013-08-20 11:54:28 +08:00
|
|
|
setsockopt(temp->sock, SOL_SOCKET, SO_BROADCAST, (char *)&broadcast, sizeof(broadcast));
|
2013-07-26 22:24:56 +08:00
|
|
|
|
2013-07-20 22:39:05 +08:00
|
|
|
/* Set socket nonblocking */
|
2013-07-27 20:43:36 +08:00
|
|
|
#ifdef WIN32
|
2013-07-20 22:39:05 +08:00
|
|
|
/* I think this works for windows */
|
2013-06-26 21:56:15 +08:00
|
|
|
u_long mode = 1;
|
2013-07-20 22:39:05 +08:00
|
|
|
/* ioctl(sock, FIONBIO, &mode); */
|
2013-08-20 11:54:28 +08:00
|
|
|
ioctlsocket(temp->sock, FIONBIO, &mode);
|
2013-07-27 20:43:36 +08:00
|
|
|
#else
|
2013-08-20 11:54:28 +08:00
|
|
|
fcntl(temp->sock, F_SETFL, O_NONBLOCK, 1);
|
2013-07-27 20:43:36 +08:00
|
|
|
#endif
|
|
|
|
|
2013-07-20 22:39:05 +08:00
|
|
|
/* Bind our socket to port PORT and address 0.0.0.0 */
|
2013-07-27 20:43:36 +08:00
|
|
|
ADDR addr = {AF_INET, htons(port), ip};
|
2013-08-20 11:54:28 +08:00
|
|
|
bind(temp->sock, (struct sockaddr *)&addr, sizeof(addr));
|
|
|
|
return temp;
|
2013-06-27 22:01:31 +08:00
|
|
|
}
|
|
|
|
|
2013-07-20 22:39:05 +08:00
|
|
|
/* function to cleanup networking stuff */
|
2013-08-21 07:37:05 +08:00
|
|
|
void kill_networking(Networking_Core *net)
|
2013-06-27 22:01:31 +08:00
|
|
|
{
|
2013-07-27 20:43:36 +08:00
|
|
|
#ifdef WIN32
|
2013-08-20 11:54:28 +08:00
|
|
|
closesocket(net->sock);
|
2013-07-27 20:43:36 +08:00
|
|
|
#else
|
2013-08-20 11:54:28 +08:00
|
|
|
close(net->sock);
|
2013-07-27 20:43:36 +08:00
|
|
|
#endif
|
2013-08-20 11:54:28 +08:00
|
|
|
free(net);
|
2013-06-27 22:01:31 +08:00
|
|
|
return;
|
2013-07-20 22:39:05 +08:00
|
|
|
}
|
2013-07-21 05:31:36 +08:00
|
|
|
|
2013-08-02 02:52:13 +08:00
|
|
|
/*
|
|
|
|
resolve_addr():
|
|
|
|
address should represent IPv4 or a hostname with A record
|
|
|
|
|
|
|
|
returns a data in network byte order that can be used to set IP.i or IP_Port.ip.i
|
|
|
|
returns 0 on failure
|
|
|
|
|
|
|
|
TODO: Fix ipv6 support
|
|
|
|
*/
|
|
|
|
uint32_t resolve_addr(const char *address)
|
2013-07-21 05:31:36 +08:00
|
|
|
{
|
2013-08-02 02:52:13 +08:00
|
|
|
struct addrinfo *server = NULL;
|
|
|
|
struct addrinfo hints;
|
|
|
|
int rc;
|
|
|
|
uint32_t addr;
|
|
|
|
|
2013-07-21 05:31:36 +08:00
|
|
|
memset(&hints, 0, sizeof(hints));
|
2013-08-02 02:52:13 +08:00
|
|
|
hints.ai_family = AF_INET; // IPv4 only right now.
|
|
|
|
hints.ai_socktype = SOCK_DGRAM; // type of socket Tox uses.
|
2013-07-21 05:31:36 +08:00
|
|
|
|
2013-08-02 02:52:13 +08:00
|
|
|
rc = getaddrinfo(address, "echo", &hints, &server);
|
2013-07-21 05:31:36 +08:00
|
|
|
|
2013-08-02 02:52:13 +08:00
|
|
|
// Lookup failed.
|
2013-08-17 01:11:09 +08:00
|
|
|
if (rc != 0) {
|
2013-08-02 02:52:13 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// IPv4 records only..
|
2013-08-17 01:11:09 +08:00
|
|
|
if (server->ai_family != AF_INET) {
|
2013-08-02 02:52:13 +08:00
|
|
|
freeaddrinfo(server);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-08-17 01:11:09 +08:00
|
|
|
|
|
|
|
addr = ((struct sockaddr_in *)server->ai_addr)->sin_addr.s_addr;
|
2013-07-21 05:31:36 +08:00
|
|
|
|
|
|
|
freeaddrinfo(server);
|
2013-08-02 02:52:13 +08:00
|
|
|
return addr;
|
2013-07-27 06:10:22 +08:00
|
|
|
}
|