Commit b0d21f92 authored by JINMEI Tatuya's avatar JINMEI Tatuya
Browse files

[1593] some trivial style fixes. mostly to be aligned with the guideline.

also constify some variables, and remove unnecessary cast.
parent e40cec7b
......@@ -105,9 +105,9 @@ handle_request(const int input_fd, const int output_fd,
addr_len = sizeof(addr_in);
memset(&addr_in, 0, sizeof(addr_in));
addr_in.sin_family = AF_INET;
read_message(input_fd, static_cast<void*>(&addr_in.sin_port),
read_message(input_fd, &addr_in.sin_port,
sizeof(addr_in.sin_port));
read_message(input_fd, static_cast<void*>(&addr_in.sin_addr.s_addr),
read_message(input_fd, &addr_in.sin_addr.s_addr,
sizeof(addr_in.sin_addr.s_addr));
break;
......@@ -116,9 +116,9 @@ handle_request(const int input_fd, const int output_fd,
addr_len = sizeof addr_in6;
memset(&addr_in6, 0, sizeof(addr_in6));
addr_in6.sin6_family = AF_INET6;
read_message(input_fd, static_cast<void*>(&addr_in6.sin6_port),
read_message(input_fd, &addr_in6.sin6_port,
sizeof(addr_in6.sin6_port));
read_message(input_fd, static_cast<void*>(&addr_in6.sin6_addr.s6_addr),
read_message(input_fd, &addr_in6.sin6_addr.s6_addr,
sizeof(addr_in6.sin6_addr.s6_addr));
break;
......@@ -127,7 +127,7 @@ handle_request(const int input_fd, const int output_fd,
}
// Obtain the socket
int result = get_sock(sock_type, addr, addr_len);
const int result = get_sock(sock_type, addr, addr_len);
if (result >= 0) {
// Got the socket, send it to the client.
write_message(output_fd, "S", 1);
......@@ -160,8 +160,8 @@ handle_request(const int input_fd, const int output_fd,
}
// ...and append the reason code to the error message
int error = errno;
write_message(output_fd, static_cast<void*>(&error), sizeof error);
const int error = errno;
write_message(output_fd, &error, sizeof(error));
}
}
......@@ -172,24 +172,26 @@ namespace socket_creator {
// Get the socket and bind to it.
int
get_sock(const int type, struct sockaddr *bind_addr, const socklen_t addr_len)
get_sock(const int type, struct sockaddr* bind_addr, const socklen_t addr_len)
{
int sock = socket(bind_addr->sa_family, type, 0);
const int sock = socket(bind_addr->sa_family, type, 0);
if (sock == -1) {
return -1;
return (-1);
}
const int on = 1;
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1) {
return -2; // This is part of the binding process, so it's a bind error
// This is part of the binding process, so it's a bind error
return (-2);
}
if (bind_addr->sa_family == AF_INET6 &&
setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on)) == -1) {
return -2; // This is part of the binding process, so it's a bind error
// This is part of the binding process, so it's a bind error
return (-2);
}
if (bind(sock, bind_addr, addr_len) == -1) {
return -2;
return (-2);
}
return sock;
return (sock);
}
// Main run loop.
......
......@@ -12,8 +12,6 @@
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
#include <iostream>
#include "../sockcreator.h"
#include <util/unittests/fork.h>
......@@ -25,6 +23,8 @@
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <iostream>
#include <cstring>
#include <cerrno>
......@@ -106,7 +106,7 @@ typedef void (*socket_check_t)(const int);
// IPv4 check
void addressFamilySpecificCheck(const sockaddr_in*, const int) {
};
}
// IPv6 check
void addressFamilySpecificCheck(const sockaddr_in6*, const int socknum) {
......@@ -114,7 +114,7 @@ void addressFamilySpecificCheck(const sockaddr_in6*, const int socknum) {
socklen_t len = sizeof(options);
EXPECT_EQ(0, getsockopt(socknum, IPPROTO_IPV6, IPV6_V6ONLY, &options, &len));
EXPECT_NE(0, options);
};
}
// Generic version of the socket test. It creates the socket and checks that
......@@ -133,7 +133,7 @@ void testAnyCreate(int socket_type, socket_check_t socket_check) {
memset(&addr, 0, sizeof(addr));
setAddressFamilyFields(&addr);
sockaddr* addr_ptr = reinterpret_cast<sockaddr*>(&addr);
int socket = get_sock(socket_type, addr_ptr, sizeof(addr));
const int socket = get_sock(socket_type, addr_ptr, sizeof(addr));
ASSERT_GE(socket, 0) << "Couldn't create socket: failed with " <<
"return code " << socket << " and error " << strerror(errno);
......@@ -198,8 +198,7 @@ TEST(get_sock, fail_with_nonsense) {
// -1: The simulated bind() call has failed
// -2: The simulated socket() call has failed
int
get_sock_dummy(const int type, struct sockaddr *addr, const socklen_t)
{
get_sock_dummy(const int type, struct sockaddr* addr, const socklen_t) {
int result = 0;
int port = 0;
......@@ -245,8 +244,7 @@ get_sock_dummy(const int type, struct sockaddr *addr, const socklen_t)
// Dummy send function - return data (the result of get_sock()) to the destination.
int
send_fd_dummy(const int destination, const int what)
{
send_fd_dummy(const int destination, const int what) {
// Make sure it is 1 byte so we know the length. We do not use more during
// the test anyway. And even with the LS bute, we can distinguish between
// the different results.
......@@ -275,11 +273,11 @@ void run_test(const char* input_data, const size_t input_size,
// process sends data from the client to run() and the checker process
// reads the response and checks it against the expected response.
int input_fd = 0;
pid_t input = provide_input(&input_fd, input_data, input_size);
const pid_t input = provide_input(&input_fd, input_data, input_size);
ASSERT_NE(-1, input) << "Couldn't start input feeder";
int output_fd = 0;
pid_t output = check_output(&output_fd, output_data, output_size);
const pid_t output = check_output(&output_fd, output_data, output_size);
ASSERT_NE(-1, output) << "Couldn't start output checker";
// Run the body
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment