summaryrefslogtreecommitdiff
path: root/core/network.c
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/network.c
parent292ccc54e091f660138bd1436539bb08ff8ce4e7 (diff)
Added address resolving function (Thank you stal).
Diffstat (limited to 'core/network.c')
-rw-r--r--core/network.c25
1 files changed, 25 insertions, 0 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