diff options
author | Astonex <softukitu@gmail.com> | 2013-07-27 02:52:24 +0100 |
---|---|---|
committer | Astonex <softukitu@gmail.com> | 2013-07-27 02:52:24 +0100 |
commit | 3e1b96f333b7e51c8a714c2e701e7310239f1364 (patch) | |
tree | 838d8d55a7f2f69830ff59161820f21e86ddcca0 /core/network.c | |
parent | 37a300f9021cbf8c5e6e1134ae8eee9c33307eb7 (diff) | |
parent | 1b4ea2e1aeb874e872a2c767326633450de12d20 (diff) |
Merge remote-tracking branch 'ProjectTox/master'
Diffstat (limited to 'core/network.c')
-rw-r--r-- | core/network.c | 83 |
1 files changed, 37 insertions, 46 deletions
diff --git a/core/network.c b/core/network.c index d4e25c82..ec234593 100644 --- a/core/network.c +++ b/core/network.c | |||
@@ -1,30 +1,28 @@ | |||
1 | /* network.h | 1 | /* network.h |
2 | * | 2 | * |
3 | * Functions for the core networking. | 3 | * Functions for the core networking. |
4 | * | 4 | * |
5 | 5 | * Copyright (C) 2013 Tox project All Rights Reserved. | |
6 | Copyright (C) 2013 Tox project All Rights Reserved. | 6 | * |
7 | 7 | * This file is part of Tox. | |
8 | This file is part of Tox. | 8 | * |
9 | 9 | * Tox is free software: you can redistribute it and/or modify | |
10 | Tox is free software: you can redistribute it and/or modify | 10 | * it under the terms of the GNU General Public License as published by |
11 | it under the terms of the GNU General Public License as published by | 11 | * the Free Software Foundation, either version 3 of the License, or |
12 | the Free Software Foundation, either version 3 of the License, or | 12 | * (at your option) any later version. |
13 | (at your option) any later version. | 13 | * |
14 | 14 | * Tox is distributed in the hope that it will be useful, | |
15 | Tox is distributed in the hope that it will be useful, | 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 17 | * GNU General Public License for more details. |
18 | GNU General Public License for more details. | 18 | * |
19 | 19 | * You should have received a copy of the GNU General Public License | |
20 | You should have received a copy of the GNU General Public License | 20 | * along with Tox. If not, see <http://www.gnu.org/licenses/>. |
21 | along with Tox. If not, see <http://www.gnu.org/licenses/>. | 21 | * |
22 | 22 | */ | |
23 | */ | ||
24 | 23 | ||
25 | #include "network.h" | 24 | #include "network.h" |
26 | 25 | ||
27 | |||
28 | /* returns current UNIX time in microseconds (us). */ | 26 | /* returns current UNIX time in microseconds (us). */ |
29 | uint64_t current_time() | 27 | uint64_t current_time() |
30 | { | 28 | { |
@@ -44,8 +42,6 @@ uint64_t current_time() | |||
44 | time = 1000000UL*a.tv_sec + a.tv_usec; | 42 | time = 1000000UL*a.tv_sec + a.tv_usec; |
45 | return time; | 43 | return time; |
46 | #endif | 44 | #endif |
47 | |||
48 | |||
49 | } | 45 | } |
50 | 46 | ||
51 | /* return a random number | 47 | /* return a random number |
@@ -69,7 +65,6 @@ int sendpacket(IP_Port ip_port, uint8_t * data, uint32_t length) | |||
69 | { | 65 | { |
70 | ADDR addr = {AF_INET, ip_port.port, ip_port.ip}; | 66 | ADDR addr = {AF_INET, ip_port.port, ip_port.ip}; |
71 | return sendto(sock,(char *) data, length, 0, (struct sockaddr *)&addr, sizeof(addr)); | 67 | return sendto(sock,(char *) data, length, 0, (struct sockaddr *)&addr, sizeof(addr)); |
72 | |||
73 | } | 68 | } |
74 | 69 | ||
75 | /* Function to receive data, ip and port of sender is put into ip_port | 70 | /* Function to receive data, ip and port of sender is put into ip_port |
@@ -94,7 +89,6 @@ int receivepacket(IP_Port * ip_port, uint8_t * data, uint32_t * length) | |||
94 | ip_port->ip = addr.ip; | 89 | ip_port->ip = addr.ip; |
95 | ip_port->port = addr.port; | 90 | ip_port->port = addr.port; |
96 | return 0; | 91 | return 0; |
97 | |||
98 | } | 92 | } |
99 | 93 | ||
100 | /* initialize networking | 94 | /* initialize networking |
@@ -103,32 +97,28 @@ int receivepacket(IP_Port * ip_port, uint8_t * data, uint32_t * length) | |||
103 | port is in host byte order (this means don't worry about it) | 97 | port is in host byte order (this means don't worry about it) |
104 | returns 0 if no problems | 98 | returns 0 if no problems |
105 | returns -1 if there are problems */ | 99 | returns -1 if there are problems */ |
106 | int init_networking(IP ip ,uint16_t port) | 100 | int init_networking(IP ip, uint16_t port) |
107 | { | 101 | { |
108 | #ifdef WIN32 | 102 | #ifdef WIN32 |
109 | WSADATA wsaData; | 103 | WSADATA wsaData; |
110 | if(WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR) | 104 | if(WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR) |
111 | { | ||
112 | return -1; | 105 | return -1; |
113 | } | ||
114 | |||
115 | #else | 106 | #else |
116 | srandom((uint32_t)current_time()); | 107 | srandom((uint32_t)current_time()); |
117 | #endif | 108 | #endif |
118 | srand((uint32_t)current_time()); | 109 | srand((uint32_t)current_time()); |
119 | 110 | ||
120 | /* initialize our socket */ | 111 | /* initialize our socket */ |
121 | sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); | 112 | sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); |
122 | 113 | ||
123 | /* Check for socket error */ | 114 | /* Check for socket error */ |
124 | #ifdef WIN32 | 115 | #ifdef WIN32 |
125 | if (sock == INVALID_SOCKET) //MSDN recommends this | 116 | if (sock == INVALID_SOCKET) /* MSDN recommends this */ |
126 | return -1; | 117 | return -1; |
127 | #else | 118 | #else |
128 | if (sock < 0) | 119 | if (sock < 0) |
129 | return -1; | 120 | return -1; |
130 | #endif | 121 | #endif |
131 | |||
132 | 122 | ||
133 | /* Functions to increase the size of the send and receive UDP buffers | 123 | /* Functions to increase the size of the send and receive UDP buffers |
134 | NOTE: uncomment if necessary */ | 124 | NOTE: uncomment if necessary */ |
@@ -140,10 +130,13 @@ int init_networking(IP ip ,uint16_t port) | |||
140 | } | 130 | } |
141 | 131 | ||
142 | if(setsockopt(sock, SOL_SOCKET, SO_SNDBUF, (char*)&n, sizeof(n)) == -1) | 132 | if(setsockopt(sock, SOL_SOCKET, SO_SNDBUF, (char*)&n, sizeof(n)) == -1) |
143 | { | ||
144 | return -1; | 133 | return -1; |
145 | }*/ | 134 | */ |
146 | 135 | ||
136 | /* Enable broadcast on socket */ | ||
137 | int broadcast = 1; | ||
138 | setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (char*)&broadcast, sizeof(broadcast)); | ||
139 | |||
147 | /* Set socket nonblocking */ | 140 | /* Set socket nonblocking */ |
148 | #ifdef WIN32 | 141 | #ifdef WIN32 |
149 | /* I think this works for windows */ | 142 | /* I think this works for windows */ |
@@ -190,11 +183,9 @@ int resolve_addr(char *address) | |||
190 | 183 | ||
191 | int success = getaddrinfo(address, "7", &hints, &server); | 184 | int success = getaddrinfo(address, "7", &hints, &server); |
192 | if(success != 0) | 185 | if(success != 0) |
193 | { | ||
194 | return -1; | 186 | return -1; |
195 | } | ||
196 | 187 | ||
197 | int resolved = ((struct sockaddr_in*)server->ai_addr)->sin_addr.s_addr; | 188 | int resolved = ((struct sockaddr_in*)server->ai_addr)->sin_addr.s_addr; |
198 | freeaddrinfo(server); | 189 | freeaddrinfo(server); |
199 | return resolved; | 190 | return resolved; |
200 | } \ No newline at end of file | 191 | } |