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. --- core/network.c | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 core/network.c (limited to 'core/network.c') 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 -- cgit v1.2.3