Skip to content
This repository has been archived by the owner on Mar 4, 2024. It is now read-only.

Commit

Permalink
Applied clang-format and fixed typos in comments
Browse files Browse the repository at this point in the history
  • Loading branch information
NorbertHeusser committed Sep 14, 2022
1 parent 0c370f0 commit 8d3b93b
Show file tree
Hide file tree
Showing 8 changed files with 148 additions and 130 deletions.
18 changes: 9 additions & 9 deletions src/uv_ip.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ int uvIpAddrSplit(const char *address,
{
char colon = ':';
const char *service_ptr = NULL;

if (host) {
service_ptr = strCpyUntil(host, address, host_size, colon);
if (!service_ptr) {
Expand Down Expand Up @@ -64,22 +64,22 @@ int uvIpResolveBindAddresses(const char *address, struct addrinfo **ai_result)
char hostname[NI_MAXHOST];
char service[NI_MAXSERV];
int rv;

rv = uvIpAddrSplit( address, hostname, sizeof(hostname), service, sizeof(service));

rv = uvIpAddrSplit(address, hostname, sizeof(hostname), service,
sizeof(service));
if (rv != 0) {
return rv;
}

if (hostname[0]) {
rv = getaddrinfo( hostname, service, &hints, ai_result);
rv = getaddrinfo(hostname, service, &hints, ai_result);
} else {
rv = getaddrinfo( NULL, service, &hints, ai_result);
rv = getaddrinfo(NULL, service, &hints, ai_result);
}

if (rv != 0) {
return RAFT_IOERR;
}

return 0;
}

2 changes: 1 addition & 1 deletion src/uv_tcp.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "uv_ip.h"
#include "uv_tcp.h"
#include "uv_ip.h"

#include <string.h>

Expand Down
23 changes: 11 additions & 12 deletions src/uv_tcp_connect.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
/* The happy path of a connection request is:
*
* - Create a TCP handle and submit a TCP connect request.
* - Initiate a asynchronous dns resolve request
* - Initiate an asynchronous dns resolve request
* - Once the name lookup was successfull connect to the first given IP
* - Once connected over TCP, submit a write request for the handshake.
* - Once the write completes, fire the connection request callback.
Expand Down Expand Up @@ -159,7 +159,6 @@ static void uvTcpTryNextConnectCb(struct uv_handle_s *handle)
uvTcpAsyncConnect(connect);
}


/* The TCP connection is established. Write the handshake data. */
static void uvTcpConnectUvConnectCb(struct uv_connect_s *req, int status)
{
Expand All @@ -175,9 +174,10 @@ static void uvTcpConnectUvConnectCb(struct uv_connect_s *req, int status)
if (status != 0) {
assert(status != UV_ECANCELED); /* t->closing would have been true */
connect->ai_current = connect->ai_current->ai_next;
if (connect->ai_current){
if (connect->ai_current) {
/* For the next connect attempt we need to close the tcp handle. */
/* To avoid interference with aborting we set a flag to indicate the connect attempt */
/* To avoid interference with aborting we set a flag to indicate the
* connect attempt */
connect->retry = true;
uv_close((struct uv_handle_s *)connect->tcp, uvTcpTryNextConnectCb);
return;
Expand Down Expand Up @@ -205,9 +205,9 @@ static void uvTcpConnectUvConnectCb(struct uv_connect_s *req, int status)
/* Helper function to connect to the remote node */
static void uvTcpAsyncConnect(struct uvTcpConnect *connect)
{
int rv = uv_tcp_connect(&connect->connect, connect->tcp,
connect->ai_current->ai_addr,
uvTcpConnectUvConnectCb);
int rv; =
rv = uv_tcp_connect(&connect->connect, connect->tcp,
connect->ai_current->ai_addr, uvTcpConnectUvConnectCb);
if (rv != 0) {
/* UNTESTED: since parsing succeed, this should fail only because of
* lack of system resources */
Expand Down Expand Up @@ -250,11 +250,10 @@ static void uvGetAddrInfoCb(uv_getaddrinfo_t *req,
/* Create a new TCP handle and submit a connection request to the event loop. */
static int uvTcpConnectStart(struct uvTcpConnect *r, const char *address)
{
static struct addrinfo hints = {
.ai_flags = AI_V4MAPPED | AI_ADDRCONFIG,
.ai_family = AF_INET,
.ai_socktype = SOCK_STREAM,
.ai_protocol = 0};
static struct addrinfo hints = {.ai_flags = AI_V4MAPPED | AI_ADDRCONFIG,
.ai_family = AF_INET,
.ai_socktype = SOCK_STREAM,
.ai_protocol = 0};
struct UvTcp *t = r->t;
char hostname[NI_MAXHOST];
char service[NI_MAXSERV];
Expand Down
41 changes: 23 additions & 18 deletions src/uv_tcp_listen.c
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,10 @@ static int uvTcpIncomingStart(struct uvTcpIncoming *incoming)
return rv;
}

#define IS_IN_ARRAY(elem,array,array_size) \
(const char*)(elem) >= (const char*)(array) && (const char*)(elem) < (const char*)(array)+array_size*sizeof(*array)
#define IS_IN_ARRAY(elem, array, array_size) \
(const char *)(elem) >= (const char *)(array) && \
(const char *)(elem) < \
(const char *)(array) + array_size * sizeof(*array)

/* Called when there's a new incoming connection: create a new tcp_accept object
* and start receiving handshake data. */
Expand All @@ -252,7 +254,7 @@ static void uvTcpListenCb(struct uv_stream_s *stream, int status)
int rv;

if (t->listeners) {
assert(IS_IN_ARRAY(stream,t->listeners,t->num_listeners));
assert(IS_IN_ARRAY(stream, t->listeners, t->num_listeners));
} else {
assert(stream == (struct uv_stream_s *)&t->default_listener);
}
Expand All @@ -268,7 +270,7 @@ static void uvTcpListenCb(struct uv_stream_s *stream, int status)
goto err;
}
incoming->t = t;
incoming->listener = (struct uv_tcp_s*)stream;
incoming->listener = (struct uv_tcp_s *)stream;
incoming->tcp = NULL;

QUEUE_PUSH(&t->accepting, &incoming->queue);
Expand All @@ -288,7 +290,7 @@ static void uvTcpListenCb(struct uv_stream_s *stream, int status)
}

/* Do bind/listen call on the tcp handle */
static int UvTcpBindListen(struct uv_tcp_s* listener, struct sockaddr *addr)
static int UvTcpBindListen(struct uv_tcp_s *listener, struct sockaddr *addr)
{
if (uv_tcp_bind(listener, addr, 0) ||
uv_listen((uv_stream_t *)listener, 1, uvTcpListenCb)) {
Expand All @@ -298,7 +300,8 @@ static int UvTcpBindListen(struct uv_tcp_s* listener, struct sockaddr *addr)
}

/* Create a tcp handle and do bind/listen for each IP */
static int UvTcpListenOnMultipleIP(struct raft_uv_transport *transport, struct addrinfo *addr_infos)
static int UvTcpListenOnMultipleIP(struct raft_uv_transport *transport,
struct addrinfo *addr_infos)
{
struct UvTcp *t;
struct addrinfo *current;
Expand All @@ -307,24 +310,24 @@ static int UvTcpListenOnMultipleIP(struct raft_uv_transport *transport, struct a

t = transport->impl;

num_listeners=0;
num_listeners = 0;
for (current = addr_infos; current; current = current->ai_next) {
++num_listeners;
}

current = addr_infos;
t->listeners = raft_malloc(num_listeners*sizeof(*t->listeners));
if (!t->listeners){
t->listeners = raft_malloc(num_listeners * sizeof(*t->listeners));
if (!t->listeners) {
return RAFT_NOMEM;
goto err;
}

t->num_listeners = num_listeners;
for (num_listeners = 0; num_listeners < t->num_listeners; ++num_listeners) {
struct uv_tcp_s* listener = &t->listeners[num_listeners];
struct uv_tcp_s *listener = &t->listeners[num_listeners];
listener->data = t;
if (uv_tcp_init(t->loop, listener) ||
UvTcpBindListen(listener,current->ai_addr)) {
UvTcpBindListen(listener, current->ai_addr)) {
rv = RAFT_IOERR;
goto err;
}
Expand All @@ -335,7 +338,7 @@ static int UvTcpListenOnMultipleIP(struct raft_uv_transport *transport, struct a
err:
if (t->listeners) {
for (size_t i = 0; i <= num_listeners; ++i) {
uv_close((struct uv_handle_s*)&t->listeners[i], NULL);
uv_close((struct uv_handle_s *)&t->listeners[i], NULL);
}
raft_free(t->listeners);
t->listeners = NULL;
Expand All @@ -344,18 +347,19 @@ static int UvTcpListenOnMultipleIP(struct raft_uv_transport *transport, struct a
return rv;
}

/* Ignore duplicate entries from glibc getaddrinfo due to
/* Ignore duplicate entries from glibc getaddrinfo due to
* https://bugzilla.redhat.com/show_bug.cgi?id=496300
* in case of resolving localhost */
static bool UvIsAddressDuplication(struct addrinfo *addr_info)
{
struct addrinfo *next = addr_info->ai_next;

/* Check, if we have a list of length 2 */
if (!next || next->ai_next) {
return false;
}
if (addr_info->ai_addrlen != next->ai_addrlen || bcmp(addr_info->ai_addr, next->ai_addr, addr_info->ai_addrlen)) {
if (addr_info->ai_addrlen != next->ai_addrlen ||
bcmp(addr_info->ai_addr, next->ai_addr, addr_info->ai_addrlen)) {
return false;
}
return true;
Expand Down Expand Up @@ -387,7 +391,6 @@ int UvTcpListen(struct raft_uv_transport *transport, raft_uv_accept_cb cb)
return rv;
}


/* Close callback for uvTcp->listener. */
static void uvTcpListenCloseCbListener(struct uv_handle_s *handle)
{
Expand Down Expand Up @@ -420,8 +423,10 @@ void UvTcpListenClose(struct UvTcp *t)

if (t->listeners) {
for (size_t i = 0; i < t->num_listeners; ++i) {
uv_close((struct uv_handle_s *)&t->listeners[i], uvTcpListenCloseCbListener);
uv_close((struct uv_handle_s *)&t->listeners[i],
uvTcpListenCloseCbListener);
}
}
uv_close((struct uv_handle_s *)&t->default_listener, uvTcpListenCloseCbListener);
uv_close((struct uv_handle_s *)&t->default_listener,
uvTcpListenCloseCbListener);
}
28 changes: 15 additions & 13 deletions test/integration/test_uv_tcp_connect.c
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,11 @@ TEST(tcp_connect, connectByName, setUp, tearDown, 0, NULL)

/* Successfully connect to the peer by first IP */
TEST(tcp_connect, firstIP, setUp, tearDown, 0, NULL)
{
{
struct fixture *f = data;
const struct AddrinfoResult results[] = { { "127.0.0.1", TCP_SERVER_PORT}, { "192.0.2.0", 6666} };
AddrinfoInjectSetResponse( 0, 2, results);
const struct AddrinfoResult results[] = {{"127.0.0.1", TCP_SERVER_PORT},
{"192.0.2.0", 6666}};
AddrinfoInjectSetResponse(0, 2, results);
CONNECT(2, "any-host");
return MUNIT_OK;
}
Expand All @@ -204,9 +205,10 @@ TEST(tcp_connect, firstIP, setUp, tearDown, 0, NULL)
TEST(tcp_connect, secondIP, setUp, tearDown, 0, NULL)
{
struct fixture *f = data;
const struct AddrinfoResult results[] = { { "127.0.0.1", .6666}, { "127.0.0.1", TCP_SERVER_PORT} };

AddrinfoInjectSetResponse( 0, 2, results);
const struct AddrinfoResult results[] = {{"127.0.0.1", .6666},
{"127.0.0.1", TCP_SERVER_PORT}};

AddrinfoInjectSetResponse(0, 2, results);
CONNECT(2, "any-host");
return MUNIT_OK;
}
Expand Down Expand Up @@ -267,7 +269,7 @@ TEST(tcp_connect, closeDuringHandshake, setUp, tearDownDeps, 0, NULL)
* iteration, not leaving us a chance to close without going through a lot
* of hoops.
* https://github.com/libuv/libuv/pull/3598 */
unsigned incompatible_uv = (1 << 16) | (44 << 8) | 2;
unsigned incompatible_uv = (1 << 16) | (44 << 8) | 2;
if (uv_version() >= incompatible_uv) {
CLOSE;
return MUNIT_SKIP;
Expand Down Expand Up @@ -327,16 +329,17 @@ TEST(tcp_connect, closeDuringConnectAbort, setUp, tearDownDeps, 0, NULL)
return MUNIT_OK;
}

/* The transport gets closed right after the first connection attempt failed, while
* doing a second connection attempt. */
/* The transport gets closed right after the first connection attempt failed,
* while doing a second connection attempt. */
TEST(tcp_connect, closeDuringSecondConnect, setUp, tearDownDeps, 0, NULL)
{
struct fixture *f = data;
struct uv_check_s check;
int rv;
const struct AddrinfoResult results[] = { { "127.0.0.1", .6666}, { "127.0.0.1", TCP_SERVER_PORT} };

AddrinfoInjectSetResponse( 0, 2, results);
const struct AddrinfoResult results[] = {{"127.0.0.1", .6666},
{"127.0.0.1", TCP_SERVER_PORT}};

AddrinfoInjectSetResponse(0, 2, results);

/* Use a check handle in order to close the transport in the same loop
* iteration where the second connection attempt occurs. */
Expand All @@ -352,4 +355,3 @@ TEST(tcp_connect, closeDuringSecondConnect, setUp, tearDownDeps, 0, NULL)
CLOSE_WAIT;
return MUNIT_OK;
}

Loading

0 comments on commit 8d3b93b

Please sign in to comment.