Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use bool in place of 0/1 int values. #1132

Merged
merged 1 commit into from
Aug 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions toxcore/DHT.c
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ void get_shared_key(const Mono_Time *mono_time, Shared_Keys *shared_keys, uint8_

if (num != UINT32_MAX) {
Shared_Key *const key = &shared_keys->keys[curr];
key->stored = 1;
key->stored = true;
key->times_requested = 1;
memcpy(key->public_key, public_key, CRYPTO_PUBLIC_KEY_SIZE);
memcpy(key->shared_key, shared_key, CRYPTO_SHARED_KEY_SIZE);
Expand Down Expand Up @@ -491,7 +491,7 @@ static int dht_create_packet(const uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE],
* Return size of unpacked ip_port on success.
* Return -1 on failure.
*/
int unpack_ip_port(IP_Port *ip_port, const uint8_t *data, uint16_t length, uint8_t tcp_enabled)
int unpack_ip_port(IP_Port *ip_port, const uint8_t *data, uint16_t length, bool tcp_enabled)
{
if (data == nullptr) {
return -1;
Expand Down Expand Up @@ -589,7 +589,7 @@ int pack_nodes(uint8_t *data, uint16_t length, const Node_format *nodes, uint16_
* return -1 on failure.
*/
int unpack_nodes(Node_format *nodes, uint16_t max_num_nodes, uint16_t *processed_data_len, const uint8_t *data,
uint16_t length, uint8_t tcp_enabled)
uint16_t length, bool tcp_enabled)
{
uint32_t num = 0, len_processed = 0;

Expand Down
6 changes: 3 additions & 3 deletions toxcore/DHT.h
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ int pack_ip_port(uint8_t *data, uint16_t length, const IP_Port *ip_port);
* Return size of unpacked ip_port on success.
* Return -1 on failure.
*/
int unpack_ip_port(IP_Port *ip_port, const uint8_t *data, uint16_t length, uint8_t tcp_enabled);
int unpack_ip_port(IP_Port *ip_port, const uint8_t *data, uint16_t length, bool tcp_enabled);

/* Pack number of nodes into data of maxlength length.
*
Expand All @@ -191,7 +191,7 @@ int pack_nodes(uint8_t *data, uint16_t length, const Node_format *nodes, uint16_
* return -1 on failure.
*/
int unpack_nodes(Node_format *nodes, uint16_t max_num_nodes, uint16_t *processed_data_len, const uint8_t *data,
uint16_t length, uint8_t tcp_enabled);
uint16_t length, bool tcp_enabled);


/*----------------------------------------------------------------------------------*/
Expand All @@ -203,7 +203,7 @@ typedef struct Shared_Key {
uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE];
uint8_t shared_key[CRYPTO_SHARED_KEY_SIZE];
uint32_t times_requested;
uint8_t stored; /* 0 if not, 1 if is */
bool stored;
uint64_t time_last_requested;
} Shared_Key;

Expand Down
158 changes: 29 additions & 129 deletions toxcore/network.c
Original file line number Diff line number Diff line change
Expand Up @@ -346,12 +346,7 @@ bool net_family_is_tox_tcp_ipv6(Family family)
return family.value == net_family_tox_tcp_ipv6.value;
}

/* Check if socket is valid.
*
* return 1 if valid
* return 0 if not valid
*/
int sock_valid(Socket sock)
bool sock_valid(Socket sock)
{
return sock.socket != net_invalid_socket.socket;
}
Expand All @@ -367,60 +362,40 @@ void kill_sock(Socket sock)
#endif
}

/* Set socket as nonblocking
*
* return 1 on success
* return 0 on failure
*/
int set_socket_nonblock(Socket sock)
bool set_socket_nonblock(Socket sock)
{
#ifdef OS_WIN32
u_long mode = 1;
return (ioctlsocket(sock.socket, FIONBIO, &mode) == 0);
return ioctlsocket(sock.socket, FIONBIO, &mode) == 0;
#else
return (fcntl(sock.socket, F_SETFL, O_NONBLOCK, 1) == 0);
return fcntl(sock.socket, F_SETFL, O_NONBLOCK, 1) == 0;
#endif
}

/* Set socket to not emit SIGPIPE
*
* return 1 on success
* return 0 on failure
*/
int set_socket_nosigpipe(Socket sock)
bool set_socket_nosigpipe(Socket sock)
{
#if defined(__APPLE__)
int set = 1;
return setsockopt(sock.socket, SOL_SOCKET, SO_NOSIGPIPE, (const char *)&set, sizeof(int)) == 0;
#else
return 1;
return true;
#endif
}

/* Enable SO_REUSEADDR on socket.
*
* return 1 on success
* return 0 on failure
*/
int set_socket_reuseaddr(Socket sock)
bool set_socket_reuseaddr(Socket sock)
{
int set = 1;
return setsockopt(sock.socket, SOL_SOCKET, SO_REUSEADDR, (const char *)&set, sizeof(set)) == 0;
}

/* Set socket to dual (IPv4 + IPv6 socket)
*
* return 1 on success
* return 0 on failure
*/
int set_socket_dualstack(Socket sock)
bool set_socket_dualstack(Socket sock)
{
int ipv6only = 0;
socklen_t optsize = sizeof(ipv6only);
int res = getsockopt(sock.socket, IPPROTO_IPV6, IPV6_V6ONLY, (char *)&ipv6only, &optsize);

if ((res == 0) && (ipv6only == 0)) {
return 1;
return true;
}

ipv6only = 0;
Expand Down Expand Up @@ -973,16 +948,10 @@ void kill_networking(Networking_Core *net)
}


/* ip_equal
* compares two IPAny structures
* unset means unequal
*
* returns 0 when not equal or when uninitialized
*/
int ip_equal(const IP *a, const IP *b)
bool ip_equal(const IP *a, const IP *b)
{
if (!a || !b) {
return 0;
return false;
}

/* same family */
Expand All @@ -1000,7 +969,7 @@ int ip_equal(const IP *a, const IP *b)
a->ip.v6.uint64[1] == b->ip.v6.uint64[1];
}

return 0;
return false;
}

/* different family: check on the IPv6 one if it is the IPv4 one embedded */
Expand All @@ -1018,23 +987,17 @@ int ip_equal(const IP *a, const IP *b)
}
}

return 0;
return false;
}

/* ipport_equal
* compares two IPAny_Port structures
* unset means unequal
*
* returns 0 when not equal or when uninitialized
*/
int ipport_equal(const IP_Port *a, const IP_Port *b)
bool ipport_equal(const IP_Port *a, const IP_Port *b)
{
if (!a || !b) {
return 0;
return false;
}

if (!a->port || (a->port != b->port)) {
return 0;
return false;
}

return ip_equal(&a->ip, &b->ip);
Expand Down Expand Up @@ -1153,25 +1116,10 @@ const char *ip_ntoa(const IP *ip, char *ip_str, size_t length)
return ip_str;
}

/*
* ip_parse_addr
* parses IP structure into an address string
*
* input
* ip: ip of TOX_AF_INET or TOX_AF_INET6 families
* length: length of the address buffer
* Must be at least INET_ADDRSTRLEN for TOX_AF_INET
* and INET6_ADDRSTRLEN for TOX_AF_INET6
*
* output
* address: dotted notation (IPv4: quad, IPv6: 16) or colon notation (IPv6)
*
* returns 1 on success, 0 on failure
*/
int ip_parse_addr(const IP *ip, char *address, size_t length)
bool ip_parse_addr(const IP *ip, char *address, size_t length)
{
if (!address || !ip) {
return 0;
return false;
}

if (net_family_is_ipv4(ip->family)) {
Expand All @@ -1184,64 +1132,34 @@ int ip_parse_addr(const IP *ip, char *address, size_t length)
return inet_ntop(make_family(ip->family), addr, address, length) != nullptr;
}

return 0;
return false;
}

/*
* addr_parse_ip
* directly parses the input into an IP structure
* tries IPv4 first, then IPv6
*
* input
* address: dotted notation (IPv4: quad, IPv6: 16) or colon notation (IPv6)
*
* output
* IP: family and the value is set on success
*
* returns 1 on success, 0 on failure
*/
int addr_parse_ip(const char *address, IP *to)
bool addr_parse_ip(const char *address, IP *to)
{
if (!address || !to) {
return 0;
return false;
}

struct in_addr addr4;

if (inet_pton(AF_INET, address, &addr4) == 1) {
to->family = net_family_ipv4;
get_ip4(&to->ip.v4, &addr4);
return 1;
return true;
}

struct in6_addr addr6;

if (inet_pton(AF_INET6, address, &addr6) == 1) {
to->family = net_family_ipv6;
get_ip6(&to->ip.v6, &addr6);
return 1;
return true;
}

return 0;
return false;
}

/*
* addr_resolve():
* uses getaddrinfo to resolve an address into an IP address
* uses the first IPv4/IPv6 addresses returned by getaddrinfo
*
* input
* address: a hostname (or something parseable to an IP address)
* to: to.family MUST be initialized, either set to a specific IP version
* (TOX_AF_INET/TOX_AF_INET6) or to the unspecified AF_UNSPEC (= 0), if both
* IP versions are acceptable
* extra can be NULL and is only set in special circumstances, see returns
*
* returns in *to a valid IPAny (v4/v6),
* prefers v6 if ip.family was AF_UNSPEC and both available
* returns in *extra an IPv4 address, if family was AF_UNSPEC and *to is TOX_AF_INET6
* returns 0 on failure, TOX_ADDR_RESOLVE_* on success.
*/
int addr_resolve(const char *address, IP *to, IP *extra)
{
if (!address || !to) {
Expand Down Expand Up @@ -1332,30 +1250,15 @@ int addr_resolve(const char *address, IP *to, IP *extra)
return result;
}

/*
* addr_resolve_or_parse_ip
* resolves string into an IP address
*
* address: a hostname (or something parseable to an IP address)
* to: to.family MUST be initialized, either set to a specific IP version
* (TOX_AF_INET/TOX_AF_INET6) or to the unspecified AF_UNSPEC (= 0), if both
* IP versions are acceptable
* extra can be NULL and is only set in special circumstances, see returns
*
* returns in *tro a matching address (IPv6 or IPv4)
* returns in *extra, if not NULL, an IPv4 address, if to->family was AF_UNSPEC
* returns 1 on success
* returns 0 on failure
*/
int addr_resolve_or_parse_ip(const char *address, IP *to, IP *extra)
bool addr_resolve_or_parse_ip(const char *address, IP *to, IP *extra)
{
if (!addr_resolve(address, to, extra)) {
if (!addr_parse_ip(address, to)) {
return 0;
return false;
}
}

return 1;
return true;
}

int net_connect(Socket sock, IP_Port ip_port)
Expand Down Expand Up @@ -1466,10 +1369,7 @@ void net_freeipport(IP_Port *ip_ports)
free(ip_ports);
}

/* return 1 on success
* return 0 on failure
*/
int bind_to_port(Socket sock, Family family, uint16_t port)
bool bind_to_port(Socket sock, Family family, uint16_t port)
{
struct sockaddr_storage addr = {0};
size_t addrsize;
Expand All @@ -1487,7 +1387,7 @@ int bind_to_port(Socket sock, Family family, uint16_t port)
addr6->sin6_family = AF_INET6;
addr6->sin6_port = net_htons(port);
} else {
return 0;
return false;
}

return bind(sock.socket, (struct sockaddr *)&addr, addrsize) == 0;
Expand Down
Loading