This commit is contained in:
rlt3 2013-08-01 16:01:29 -04:00
commit 36698f0824
7 changed files with 53 additions and 34 deletions

View File

@ -75,10 +75,9 @@ int encrypt_data(uint8_t *public_key, uint8_t *secret_key, uint8_t *nonce,
uint32_t i;
uint32_t check = 0;
for(i = 0; i < crypto_box_BOXZEROBYTES; ++i) {
if (temp_encrypted[i] != 0)
check = 1;
check |= temp_plain[i] ^ 0;
}
if(check == 1)
if(check != 0)
return -1;
/* unpad the encrypted message */
@ -110,10 +109,9 @@ int decrypt_data(uint8_t *public_key, uint8_t *secret_key, uint8_t *nonce,
uint32_t i;
uint32_t check = 0;
for(i = 0; i < crypto_box_ZEROBYTES; ++i) {
if (temp_plain[i] != 0)
check = 1;
check |= temp_plain[i] ^ 0;
}
if(check == 1)
if(check != 0)
return -1;
/* unpad the plain message */

View File

@ -164,25 +164,42 @@ void shutdown_networking()
return;
}
/* resolves provided address to a binary data in network byte order
address is ASCII null terminated string
address should represent IPv4, IPv6 or a hostname
on success returns a data in network byte order that can be used to set IP.i or IP_Port.ip.i
on failure returns -1 */
int resolve_addr(const char *address)
/*
resolve_addr():
address should represent IPv4 or a hostname with A record
returns a data in network byte order that can be used to set IP.i or IP_Port.ip.i
returns 0 on failure
TODO: Fix ipv6 support
*/
uint32_t resolve_addr(const char *address)
{
struct addrinfo hints;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC; //support both IPv4 and IPv6
hints.ai_socktype = SOCK_DGRAM; //type of socket Tox uses
struct addrinfo *server = NULL;
struct addrinfo hints;
int rc;
uint32_t addr;
int success = getaddrinfo(address, "echo", &hints, &server);
if(success != 0)
return -1;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET; // IPv4 only right now.
hints.ai_socktype = SOCK_DGRAM; // type of socket Tox uses.
rc = getaddrinfo(address, "echo", &hints, &server);
// Lookup failed.
if(rc != 0) {
return 0;
}
// IPv4 records only..
if(server->ai_family != AF_INET) {
freeaddrinfo(server);
return 0;
}
addr = ((struct sockaddr_in*)server->ai_addr)->sin_addr.s_addr;
int resolved = ((struct sockaddr_in*)server->ai_addr)->sin_addr.s_addr;
freeaddrinfo(server);
return resolved;
return addr;
}

View File

@ -116,12 +116,16 @@ int init_networking(IP ip, uint16_t port);
/* function to cleanup networking stuff(doesn't do much right now) */
void shutdown_networking();
/* resolves provided address to a binary data in network byte order
address is ASCII null terminated string
address should represent IPv4, IPv6 or a hostname
on success returns a data in network byte order that can be used to set IP.i or IP_Port.ip.i
on failure returns -1 */
int resolve_addr(const char *address);
/*
resolve_addr():
address should represent IPv4 or a hostname with A record
returns a data in network byte order that can be used to set IP.i or IP_Port.ip.i
returns 0 on failure
TODO: Fix ipv6 support
*/
uint32_t resolve_addr(const char *address);
#ifdef __cplusplus
}

View File

@ -245,7 +245,7 @@ struct server_conf_s configure_server(char *cfg_file)
it away in the server_info struct */
server_conf.info[i].valid = 1;
if(resolve_addr(strcpy(tmp_ip, bs_ip)) == -1) {
if(resolve_addr(strcpy(tmp_ip, bs_ip)) == 0) {
server_conf.info[i].valid = 0;
printf("bootstrap_server %d: Invalid IP\n", i);
}

View File

@ -423,7 +423,7 @@ int main(int argc, char *argv[])
IP_Port bootstrap_ip_port;
bootstrap_ip_port.port = htons(atoi(argv[2]));
int resolved_address = resolve_addr(argv[1]);
if (resolved_address != -1)
if (resolved_address != 0)
bootstrap_ip_port.ip.i = resolved_address;
else
exit(1);

View File

@ -346,7 +346,7 @@ int main(int argc, char *argv[])
IP_Port bootstrap_ip_port;
bootstrap_ip_port.port = htons(atoi(argv[2]));
int resolved_address = resolve_addr(argv[1]);
if (resolved_address != -1)
if (resolved_address != 0)
bootstrap_ip_port.ip.i = resolved_address;
else
exit(1);
@ -384,4 +384,4 @@ int main(int argc, char *argv[])
Sleep(1);
}
return 0;
}
}

View File

@ -83,8 +83,8 @@ static void execute(ToxWindow* self, char* cmd) {
dht.port = htons(atoi(port));
int resolved_address = resolve_addr(ip);
if (resolved_address == -1) {
uint32_t resolved_address = resolve_addr(ip);
if (resolved_address == 0) {
return;
}