Skip to content

Commit

Permalink
Some code cleanups.
Browse files Browse the repository at this point in the history
  • Loading branch information
irungentoo committed Apr 2, 2014
1 parent ce412b2 commit c8ab296
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 118 deletions.
2 changes: 2 additions & 0 deletions toxcore/Messenger.c
Original file line number Diff line number Diff line change
Expand Up @@ -2246,7 +2246,9 @@ void do_messenger(Messenger *m)

if (unix_time() > lastdump + DUMPING_CLIENTS_FRIENDS_EVERY_N_SECONDS) {
loglog(" = = = = = = = = \n");
#ifdef ENABLE_ASSOC_DHT
Assoc_status(m->dht->assoc);
#endif

if (m->numchats > 0) {
size_t c;
Expand Down
64 changes: 4 additions & 60 deletions toxcore/TCP_server.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* TCP_server.c -- Implementation of the TCP relay server part of Tox.
*
* Copyright (C) 2013 Tox project All Rights Reserved.
* Copyright (C) 2014 Tox project All Rights Reserved.
*
* This file is part of Tox.
*
Expand All @@ -28,62 +28,6 @@

#include "util.h"

/* return 1 if valid
* return 0 if not valid
*/
static int sock_valid(sock_t sock)
{
#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32)

if (sock == INVALID_SOCKET) {
#else

if (sock < 0) {
#endif
return 0;
}

return 1;
}

static void kill_sock(sock_t sock)
{
#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32)
closesocket(sock);
#else
close(sock);
#endif
}

/* return 1 on success
* return 0 on failure
*/
static int set_nonblock(sock_t sock)
{
#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32)
u_long mode = 1;
return (ioctlsocket(sock, FIONBIO, &mode) == 0);
#else
return (fcntl(sock, F_SETFL, O_NONBLOCK, 1) == 0);
#endif
}

/* return 1 on success
* return 0 on failure
*/
static int set_dualstack(sock_t sock)
{
char ipv6only = 0;
socklen_t optsize = sizeof(ipv6only);
int res = getsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, &ipv6only, &optsize);

if ((res == 0) && (ipv6only == 0))
return 1;

ipv6only = 0;
return (setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, &ipv6only, sizeof(ipv6only)) == 0);
}

/* return 1 on success
* return 0 on failure
*/
Expand Down Expand Up @@ -747,7 +691,7 @@ static int accept_connection(TCP_Server *TCP_server, sock_t sock)
if (!sock_valid(sock))
return 0;

if (!set_nonblock(sock)) {
if (!set_socket_nonblock(sock)) {
kill_sock(sock);
return 0;
}
Expand All @@ -774,10 +718,10 @@ static sock_t new_listening_TCP_socket(int family, uint16_t port)
return ~0;
}

int ok = set_nonblock(sock);
int ok = set_socket_nonblock(sock);

if (ok && family == AF_INET6) {
ok = set_dualstack(sock);
ok = set_socket_dualstack(sock);
}

ok = ok && bind_to_port(sock, family, port) && (listen(sock, TCP_MAX_BACKLOG) == 0);
Expand Down
2 changes: 1 addition & 1 deletion toxcore/TCP_server.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* TCP_server.h -- Implementation of the TCP relay server part of Tox.
*
* Copyright (C) 2013 Tox project All Rights Reserved.
* Copyright (C) 2014 Tox project All Rights Reserved.
*
* This file is part of Tox.
*
Expand Down
135 changes: 78 additions & 57 deletions toxcore/network.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,70 @@ static int inet_pton(sa_family_t family, const char *addrString, void *addrbuf)

#endif

/* Check if socket is valid.
*
* return 1 if valid
* return 0 if not valid
*/
int sock_valid(sock_t sock)
{
#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32)

if (sock == INVALID_SOCKET) {
#else

if (sock < 0) {
#endif
return 0;
}

return 1;
}

/* Close the socket.
*/
void kill_sock(sock_t sock)
{
#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32)
closesocket(sock);
#else
close(sock);
#endif
}

/* Set socket as nonblocking
*
* return 1 on success
* return 0 on failure
*/
int set_socket_nonblock(sock_t sock)
{
#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32)
u_long mode = 1;
return (ioctlsocket(sock, FIONBIO, &mode) == 0);
#else
return (fcntl(sock, F_SETFL, O_NONBLOCK, 1) == 0);
#endif
}

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

if ((res == 0) && (ipv6only == 0))
return 1;

ipv6only = 0;
return (setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, &ipv6only, sizeof(ipv6only)) == 0);
}

/* return current UNIX time in microseconds (us). */
uint64_t current_time(void)
{
Expand Down Expand Up @@ -508,25 +572,14 @@ Networking_Core *new_networking(IP ip, uint16_t port)
temp->sock = socket(temp->family, SOCK_DGRAM, IPPROTO_UDP);

/* Check for socket error. */
#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32)

if (temp->sock == INVALID_SOCKET) { /* MSDN recommends this. */
free(temp);
return NULL;
}

#else /* !WIN32 */

if (temp->sock < 0) {
if (!sock_valid(temp->sock)) {
#ifdef DEBUG
fprintf(stderr, "Failed to get a socket?! %u, %s\n", errno, strerror(errno));
#endif
free(temp);
return NULL;
}

#endif /* !WIN32 */

/* Functions to increase the size of the send and receive UDP buffers.
*/
int n = 1024 * 1024 * 2;
Expand All @@ -538,14 +591,10 @@ Networking_Core *new_networking(IP ip, uint16_t port)
setsockopt(temp->sock, SOL_SOCKET, SO_BROADCAST, (char *)&broadcast, sizeof(broadcast));

/* Set socket nonblocking. */
#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32)
/* I think this works for Windows. */
u_long mode = 1;
/* ioctl(sock, FIONBIO, &mode); */
ioctlsocket(temp->sock, FIONBIO, &mode);
#else /* !WIN32 */
fcntl(temp->sock, F_SETFL, O_NONBLOCK, 1);
#endif /* !WIN32 */
if (!set_socket_nonblock(temp->sock)) {
kill_networking(temp);
return NULL;
}

/* Bind our socket to port PORT and the given IP address (usually 0.0.0.0 or ::) */
uint16_t *portptr = NULL;
Expand Down Expand Up @@ -579,43 +628,19 @@ Networking_Core *new_networking(IP ip, uint16_t port)
}

if (ip.family == AF_INET6) {
char ipv6only = 0;
socklen_t optsize = sizeof(ipv6only);
#ifdef LOGGING
errno = 0;
#endif
int res = getsockopt(temp->sock, IPPROTO_IPV6, IPV6_V6ONLY, &ipv6only, &optsize);

if ((res == 0) && (ipv6only == 0)) {
#ifdef LOGGING
loglog("Dual-stack socket: enabled per default.\n");
#endif
} else {
ipv6only = 0;
#ifdef LOGGING

if (res < 0) {
sprintf(logbuffer, "Dual-stack socket: Failed to query default. (%d, %s)\n",
errno, strerror(errno));
loglog(logbuffer);
}

errno = 0;
res =
int is_dualstack =
#endif
setsockopt(temp->sock, IPPROTO_IPV6, IPV6_V6ONLY, (char *)&ipv6only, sizeof(ipv6only));
set_socket_dualstack(temp->sock);
#ifdef LOGGING

if (res < 0) {
sprintf(logbuffer,
"Dual-stack socket: Failed to enable, won't be able to receive from/send to IPv4 addresses. (%u, %s)\n",
errno, strerror(errno));
loglog(logbuffer);
} else
loglog("Dual-stack socket: Enabled successfully.\n");
if (is_dualstack) {
loglog("Dual-stack socket: enabled.\n");
} else {
loglog("Dual-stack socket: Failed to enable, won't be able to receive from/send to IPv4 addresses.\n");
}

#endif
}

/* multicast local nodes */
struct ipv6_mreq mreq;
Expand All @@ -626,7 +651,7 @@ Networking_Core *new_networking(IP ip, uint16_t port)
mreq.ipv6mr_interface = 0;
#ifdef LOGGING
errno = 0;
res =
int res =
#endif
setsockopt(temp->sock, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, (char *)&mreq, sizeof(mreq));
#ifdef LOGGING
Expand Down Expand Up @@ -701,11 +726,7 @@ Networking_Core *new_networking(IP ip, uint16_t port)
/* Function to cleanup networking stuff. */
void kill_networking(Networking_Core *net)
{
#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32)
closesocket(net->sock);
#else
close(net->sock);
#endif
kill_sock(net->sock);
free(net);
return;
}
Expand Down
25 changes: 25 additions & 0 deletions toxcore/network.h
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,31 @@ typedef struct {
uint64_t send_fail_eagain;
} Networking_Core;

/* Check if socket is valid.
*
* return 1 if valid
* return 0 if not valid
*/
int sock_valid(sock_t sock);

/* Close the socket.
*/
void kill_sock(sock_t sock);

/* Set socket as nonblocking
*
* return 1 on success
* return 0 on failure
*/
int set_socket_nonblock(sock_t sock);

/* Set socket to dual (IPv4 + IPv6 socket)
*
* return 1 on success
* return 0 on failure
*/
int set_socket_dualstack(sock_t sock);

/* return current time in milleseconds since the epoch. */
uint64_t current_time(void);

Expand Down

0 comments on commit c8ab296

Please sign in to comment.