summaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
Diffstat (limited to 'core')
-rw-r--r--core/net_crypto.c10
-rw-r--r--core/network.c47
-rw-r--r--core/network.h16
3 files changed, 46 insertions, 27 deletions
diff --git a/core/net_crypto.c b/core/net_crypto.c
index 83cb20a2..34d481ca 100644
--- a/core/net_crypto.c
+++ b/core/net_crypto.c
@@ -75,10 +75,9 @@ int encrypt_data(uint8_t *public_key, uint8_t *secret_key, uint8_t *nonce,
75 uint32_t i; 75 uint32_t i;
76 uint32_t check = 0; 76 uint32_t check = 0;
77 for(i = 0; i < crypto_box_BOXZEROBYTES; ++i) { 77 for(i = 0; i < crypto_box_BOXZEROBYTES; ++i) {
78 if (temp_encrypted[i] != 0) 78 check |= temp_plain[i] ^ 0;
79 check = 1;
80 } 79 }
81 if(check == 1) 80 if(check != 0)
82 return -1; 81 return -1;
83 82
84 /* unpad the encrypted message */ 83 /* unpad the encrypted message */
@@ -110,10 +109,9 @@ int decrypt_data(uint8_t *public_key, uint8_t *secret_key, uint8_t *nonce,
110 uint32_t i; 109 uint32_t i;
111 uint32_t check = 0; 110 uint32_t check = 0;
112 for(i = 0; i < crypto_box_ZEROBYTES; ++i) { 111 for(i = 0; i < crypto_box_ZEROBYTES; ++i) {
113 if (temp_plain[i] != 0) 112 check |= temp_plain[i] ^ 0;
114 check = 1;
115 } 113 }
116 if(check == 1) 114 if(check != 0)
117 return -1; 115 return -1;
118 116
119 /* unpad the plain message */ 117 /* unpad the plain message */
diff --git a/core/network.c b/core/network.c
index a7a4efcd..c58549bf 100644
--- a/core/network.c
+++ b/core/network.c
@@ -164,25 +164,42 @@ void shutdown_networking()
164 return; 164 return;
165} 165}
166 166
167/* resolves provided address to a binary data in network byte order 167/*
168 address is ASCII null terminated string 168 resolve_addr():
169 address should represent IPv4, IPv6 or a hostname 169 address should represent IPv4 or a hostname with A record
170 on success returns a data in network byte order that can be used to set IP.i or IP_Port.ip.i 170
171 on failure returns -1 */ 171 returns a data in network byte order that can be used to set IP.i or IP_Port.ip.i
172int resolve_addr(const char *address) 172 returns 0 on failure
173
174 TODO: Fix ipv6 support
175*/
176uint32_t resolve_addr(const char *address)
173{ 177{
174 struct addrinfo hints; 178 struct addrinfo *server = NULL;
179 struct addrinfo hints;
180 int rc;
181 uint32_t addr;
182
175 memset(&hints, 0, sizeof(hints)); 183 memset(&hints, 0, sizeof(hints));
176 hints.ai_family = AF_UNSPEC; //support both IPv4 and IPv6 184 hints.ai_family = AF_INET; // IPv4 only right now.
177 hints.ai_socktype = SOCK_DGRAM; //type of socket Tox uses 185 hints.ai_socktype = SOCK_DGRAM; // type of socket Tox uses.
178 186
179 struct addrinfo *server = NULL; 187 rc = getaddrinfo(address, "echo", &hints, &server);
180 188
181 int success = getaddrinfo(address, "echo", &hints, &server); 189 // Lookup failed.
182 if(success != 0) 190 if(rc != 0) {
183 return -1; 191 return 0;
192 }
193
194 // IPv4 records only..
195 if(server->ai_family != AF_INET) {
196 freeaddrinfo(server);
197 return 0;
198 }
199
200
201 addr = ((struct sockaddr_in*)server->ai_addr)->sin_addr.s_addr;
184 202
185 int resolved = ((struct sockaddr_in*)server->ai_addr)->sin_addr.s_addr;
186 freeaddrinfo(server); 203 freeaddrinfo(server);
187 return resolved; 204 return addr;
188} 205}
diff --git a/core/network.h b/core/network.h
index 3277070c..a5f7899b 100644
--- a/core/network.h
+++ b/core/network.h
@@ -116,12 +116,16 @@ int init_networking(IP ip, uint16_t port);
116/* function to cleanup networking stuff(doesn't do much right now) */ 116/* function to cleanup networking stuff(doesn't do much right now) */
117void shutdown_networking(); 117void shutdown_networking();
118 118
119/* resolves provided address to a binary data in network byte order 119/*
120 address is ASCII null terminated string 120 resolve_addr():
121 address should represent IPv4, IPv6 or a hostname 121 address should represent IPv4 or a hostname with A record
122 on success returns a data in network byte order that can be used to set IP.i or IP_Port.ip.i 122
123 on failure returns -1 */ 123 returns a data in network byte order that can be used to set IP.i or IP_Port.ip.i
124int resolve_addr(const char *address); 124 returns 0 on failure
125
126 TODO: Fix ipv6 support
127*/
128uint32_t resolve_addr(const char *address);
125 129
126#ifdef __cplusplus 130#ifdef __cplusplus
127} 131}