commit 731da3c76930d3889938213256077940ca3e0930 Author: Ian Gulliver Date: Fri Apr 26 05:34:19 2019 +0000 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4b4c904 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +mirall diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..c9dbbe7 --- /dev/null +++ b/Makefile @@ -0,0 +1,5 @@ +mirall: Makefile mirall.cpp + g++ -std=gnu++2a -o mirall mirall.cpp -lgflags -lglog + +clean: + rm --force mirall diff --git a/mirall.cpp b/mirall.cpp new file mode 100644 index 0000000..a47a33a --- /dev/null +++ b/mirall.cpp @@ -0,0 +1,39 @@ +#include +#include +#include +#include +#include + +DEFINE_int32(port, 9000, "TCP port to bind"); + +int main(int argc, char *argv[]) { + google::InitGoogleLogging(argv[0]); + gflags::ParseCommandLineFlags(&argc, &argv, true); + + auto listen_sock = socket(AF_INET6, SOCK_STREAM, 0); + PCHECK(listen_sock >= 0) << "socket()"; + + sockaddr_in6 bind_addr = { + .sin6_family = AF_INET6, + .sin6_port = htons(FLAGS_port), + .sin6_addr = IN6ADDR_LOOPBACK_INIT, + }; + PCHECK(bind(listen_sock, (sockaddr*) &bind_addr, sizeof(bind_addr)) == 0); + + PCHECK(listen(listen_sock, 128) == 0); + + while (true) { + sockaddr_in6 client_addr; + socklen_t client_addr_len = sizeof(client_addr); + + auto sock = accept(listen_sock, (sockaddr*) &client_addr, &client_addr_len); + PCHECK(sock >= 0) << "accept()"; + + CHECK_EQ(client_addr.sin6_family, AF_INET6); + + char client_addr_str[INET6_ADDRSTRLEN]; + PCHECK(inet_ntop(AF_INET6, &client_addr.sin6_addr, client_addr_str, sizeof(client_addr_str))); + + LOG(INFO) << "new connection: [" << client_addr_str << "]:" << ntohs(client_addr.sin6_port); + } +}