diff options
author | irungentoo <irungentoo@gmail.com> | 2013-06-26 09:56:15 -0400 |
---|---|---|
committer | irungentoo <irungentoo@gmail.com> | 2013-06-26 09:56:15 -0400 |
commit | c7f7e30c7521bcdafbddd9d21af288442c325a72 (patch) | |
tree | aba9e6ac90b0b418001bae57d22f7207c05dcce8 /core/network.c | |
parent | f7574c61fc935f514da4afd4994f86d8062efb38 (diff) |
Moved the network functions from the DHT into network.
Also made a nice function to init networking.
Diffstat (limited to 'core/network.c')
-rw-r--r-- | core/network.c | 74 |
1 files changed, 74 insertions, 0 deletions
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 @@ | |||
1 | |||
2 | |||
3 | #include "network.h" | ||
4 | |||
5 | //our UDP socket, a global variable. | ||
6 | static int sock; | ||
7 | |||
8 | //Basic network functions: | ||
9 | //TODO: put them somewhere else than here | ||
10 | |||
11 | //Function to send packet(data) of length length to ip_port | ||
12 | int sendpacket(IP_Port ip_port, char * data, uint32_t length) | ||
13 | { | ||
14 | ADDR addr = {AF_INET, ip_port.port, ip_port.ip}; | ||
15 | return sendto(sock, data, length, 0, (struct sockaddr *)&addr, sizeof(addr)); | ||
16 | |||
17 | } | ||
18 | |||
19 | //Function to recieve data, ip and port of sender is put into ip_port | ||
20 | //the packet data into data | ||
21 | //the packet length into length. | ||
22 | //dump all empty packets. | ||
23 | int recievepacket(IP_Port * ip_port, char * data, uint32_t * length) | ||
24 | { | ||
25 | ADDR addr; | ||
26 | uint32_t addrlen = sizeof(addr); | ||
27 | (*(int *)length) = recvfrom(sock, data, MAX_UDP_PACKET_SIZE, 0, (struct sockaddr *)&addr, &addrlen); | ||
28 | if(*(int *)length <= 0) | ||
29 | { | ||
30 | //nothing recieved | ||
31 | //or empty packet | ||
32 | return -1; | ||
33 | } | ||
34 | ip_port->ip = addr.ip; | ||
35 | ip_port->port = addr.port; | ||
36 | return 0; | ||
37 | |||
38 | } | ||
39 | |||
40 | //initialize networking | ||
41 | //bind to ip and port | ||
42 | //ip must be in network order EX: 127.0.0.1 = (7F000001) | ||
43 | //port is in host byte order (this means don't worry about it) | ||
44 | //returns 0 if no problems | ||
45 | //TODO: add something to check if there are errors | ||
46 | int init_networking(IP ip ,uint16_t port) | ||
47 | { | ||
48 | #ifdef WIN32 | ||
49 | WSADATA wsaData; | ||
50 | if(WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR) | ||
51 | { | ||
52 | return -1; | ||
53 | } | ||
54 | #endif | ||
55 | |||
56 | //initialize our socket | ||
57 | sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); | ||
58 | |||
59 | //Set socket nonblocking | ||
60 | #ifdef WIN32 | ||
61 | //I think this works for windows | ||
62 | u_long mode = 1; | ||
63 | //ioctl(sock, FIONBIO, &mode); | ||
64 | ioctlsocket(sock, FIONBIO, &mode); | ||
65 | #else | ||
66 | fcntl(sock, F_SETFL, O_NONBLOCK, 1); | ||
67 | #endif | ||
68 | |||
69 | //Bind our socket to port PORT and address 0.0.0.0 | ||
70 | ADDR addr = {AF_INET, htons(port), ip}; | ||
71 | bind(sock, (struct sockaddr*)&addr, sizeof(addr)); | ||
72 | return 0; | ||
73 | |||
74 | } \ No newline at end of file | ||