summaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
authorMaxim Biro <nurupo.contributions@gmail.com>2013-07-20 17:31:36 -0400
committerMaxim Biro <nurupo.contributions@gmail.com>2013-07-20 17:31:36 -0400
commite4469b5130ecf2b74f714bee93cab4755fc0543a (patch)
tree379f9cc75b79f8137e09ba8dec4a0afc3c72ca5d /core
parent292ccc54e091f660138bd1436539bb08ff8ce4e7 (diff)
Added address resolving function (Thank you stal).
Diffstat (limited to 'core')
-rw-r--r--core/network.c25
-rw-r--r--core/network.h11
2 files changed, 35 insertions, 1 deletions
diff --git a/core/network.c b/core/network.c
index dbe4574c..6337651a 100644
--- a/core/network.c
+++ b/core/network.c
@@ -159,3 +159,28 @@ void shutdown_networking()
159 #endif 159 #endif
160 return; 160 return;
161} 161}
162
163/* resolves provided address to a binary data in network byte order
164 address is ASCII null terminated string
165 address should represent IPv4, IPv6 or a hostname
166 on success returns a data in network byte order that can be used to set IP.i or IP_Port.ip.i
167 on failure returns -1 */
168int resolve_addr(char *address)
169{
170 struct addrinfo hints;
171 memset(&hints, 0, sizeof(hints));
172 hints.ai_family = AF_UNSPEC; //support both IPv4 and IPv6
173 hints.ai_socktype = SOCK_DGRAM; //type of socket Tox uses
174
175 struct addrinfo *server = NULL;
176
177 int success = getaddrinfo(address, "7", &hints, &server);
178 if(success != 0)
179 {
180 return -1;
181 }
182
183 int resolved = ((struct sockaddr_in*)server->ai_addr)->sin_addr.s_addr;
184 freeaddrinfo(server);
185 return resolved;
186} \ No newline at end of file
diff --git a/core/network.h b/core/network.h
index fe0c48eb..daab6e41 100644
--- a/core/network.h
+++ b/core/network.h
@@ -38,6 +38,7 @@
38 38
39#include <winsock2.h> 39#include <winsock2.h>
40#include <windows.h> 40#include <windows.h>
41#include <wspiapi.h>
41 42
42#undef VANILLA_NACL /* make sure on windows we use libsodium */ 43#undef VANILLA_NACL /* make sure on windows we use libsodium */
43 44
@@ -48,7 +49,8 @@
48#include <netinet/in.h> 49#include <netinet/in.h>
49#include <errno.h> 50#include <errno.h>
50#include <sys/time.h> 51#include <sys/time.h>
51 52#include <sys/types.h>
53#include <netdb.h>
52 54
53#endif 55#endif
54 56
@@ -121,4 +123,11 @@ int init_networking(IP ip ,uint16_t port);
121 123
122/* function to cleanup networking stuff(doesn't do much right now) */ 124/* function to cleanup networking stuff(doesn't do much right now) */
123void shutdown_networking(); 125void shutdown_networking();
126
127/* resolves provided address to a binary data in network byte order
128 address is ASCII null terminated string
129 address should represent IPv4, IPv6 or a hostname
130 on success returns a data in network byte order that can be used to set IP.i or IP_Port.ip.i
131 on failure returns -1 */
132int resolve_addr(char *address);
124#endif 133#endif