diff --git a/Makefile b/Makefile index 2f2276f..cbbd47f 100644 --- a/Makefile +++ b/Makefile @@ -217,6 +217,7 @@ distcleancheck_listfiles = \ ACLOCAL = ${SHELL} '/home/ziggi/Projects/tcpserv09/missing' aclocal-1.17 AMTAR = $${TAR-tar} AM_DEFAULT_VERBOSITY = 1 +ASTYLE = /bin/astyle AUTOCONF = ${SHELL} '/home/ziggi/Projects/tcpserv09/missing' autoconf AUTOHEADER = ${SHELL} '/home/ziggi/Projects/tcpserv09/missing' autoheader AUTOMAKE = ${SHELL} '/home/ziggi/Projects/tcpserv09/missing' automake-1.17 @@ -334,6 +335,7 @@ udpclient_test_SOURCES = \ udpclient.cpp udpclient.hpp \ udpclient_test.cpp +ASTYLE_OPTS = --indent=spaces=8 --style=java all: defines.h $(MAKE) $(AM_MAKEFLAGS) all-am @@ -835,11 +837,14 @@ run: helmetd hello.pb.cc hello.pb.h: hello.proto $(PROTOC) --cpp_out=. hello.proto +format: + $(ASTYLE) $(ASTYLE_OPTS) *.hpp *.cpp clean-local: rm -rf autom4te.cache rm -f *~ rm -f *.o + rm -f *.orig # Tell versions [3.59,3.63) of GNU make to not export all variables. # Otherwise a system limit (for SysV at least) may be exceeded. diff --git a/Makefile.am b/Makefile.am index c98def7..f7b8976 100644 --- a/Makefile.am +++ b/Makefile.am @@ -45,8 +45,14 @@ run: helmetd hello.pb.cc hello.pb.h: hello.proto $(PROTOC) --cpp_out=. hello.proto +ASTYLE_OPTS = --indent=spaces=8 --style=java +format: + $(ASTYLE) $(ASTYLE_OPTS) *.hpp *.cpp + clean-local: rm -rf autom4te.cache rm -f *~ rm -f *.o + rm -f *.orig + diff --git a/Makefile.in b/Makefile.in index 6d9a17e..5cb0135 100644 --- a/Makefile.in +++ b/Makefile.in @@ -217,6 +217,7 @@ distcleancheck_listfiles = \ ACLOCAL = @ACLOCAL@ AMTAR = @AMTAR@ AM_DEFAULT_VERBOSITY = @AM_DEFAULT_VERBOSITY@ +ASTYLE = @ASTYLE@ AUTOCONF = @AUTOCONF@ AUTOHEADER = @AUTOHEADER@ AUTOMAKE = @AUTOMAKE@ @@ -334,6 +335,7 @@ udpclient_test_SOURCES = \ udpclient.cpp udpclient.hpp \ udpclient_test.cpp +ASTYLE_OPTS = --indent=spaces=8 --style=java all: defines.h $(MAKE) $(AM_MAKEFLAGS) all-am @@ -835,11 +837,14 @@ run: helmetd hello.pb.cc hello.pb.h: hello.proto $(PROTOC) --cpp_out=. hello.proto +format: + $(ASTYLE) $(ASTYLE_OPTS) *.hpp *.cpp clean-local: rm -rf autom4te.cache rm -f *~ rm -f *.o + rm -f *.orig # Tell versions [3.59,3.63) of GNU make to not export all variables. # Otherwise a system limit (for SysV at least) may be exceeded. diff --git a/configure b/configure index 6288408..8f7ef05 100755 --- a/configure +++ b/configure @@ -610,6 +610,7 @@ ac_subst_vars='am__EXEEXT_FALSE am__EXEEXT_TRUE LTLIBOBJS LIBOBJS +ASTYLE PROTOC am__fastdepCXX_FALSE am__fastdepCXX_TRUE @@ -4879,7 +4880,7 @@ fi -for ac_prog in protoc +for ac_prog in protoc true do # Extract the first word of "$ac_prog", so it can be a program name with args. set dummy $ac_prog; ac_word=$2 @@ -4930,6 +4931,58 @@ fi test -n "$PROTOC" && break done +for ac_prog in astyle true +do + # Extract the first word of "$ac_prog", so it can be a program name with args. +set dummy $ac_prog; ac_word=$2 +{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for $ac_word" >&5 +printf %s "checking for $ac_word... " >&6; } +if test ${ac_cv_path_ASTYLE+y} +then : + printf %s "(cached) " >&6 +else case e in #( + e) case $ASTYLE in + [\\/]* | ?:[\\/]*) + ac_cv_path_ASTYLE="$ASTYLE" # Let the user override the test with a path. + ;; + *) + as_save_IFS=$IFS; IFS=$PATH_SEPARATOR +for as_dir in $PATH +do + IFS=$as_save_IFS + case $as_dir in #((( + '') as_dir=./ ;; + */) ;; + *) as_dir=$as_dir/ ;; + esac + for ac_exec_ext in '' $ac_executable_extensions; do + if as_fn_executable_p "$as_dir$ac_word$ac_exec_ext"; then + ac_cv_path_ASTYLE="$as_dir$ac_word$ac_exec_ext" + printf "%s\n" "$as_me:${as_lineno-$LINENO}: found $as_dir$ac_word$ac_exec_ext" >&5 + break 2 + fi +done + done +IFS=$as_save_IFS + + ;; +esac ;; +esac +fi +ASTYLE=$ac_cv_path_ASTYLE +if test -n "$ASTYLE"; then + { printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $ASTYLE" >&5 +printf "%s\n" "$ASTYLE" >&6; } +else + { printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: no" >&5 +printf "%s\n" "no" >&6; } +fi + + + test -n "$ASTYLE" && break +done + + ac_config_files="$ac_config_files Makefile" diff --git a/configure.ac b/configure.ac index 6845a85..7508da6 100644 --- a/configure.ac +++ b/configure.ac @@ -7,7 +7,9 @@ AC_PROG_CC([gcc cc]) AC_PROG_RANLIB AC_PROG_CXX([g++ c++]) -AC_PATH_PROGS([PROTOC],[protoc]) +AC_PATH_PROGS([PROTOC],[protoc true]) +AC_PATH_PROGS([ASTYLE],[astyle true]) + AC_CONFIG_FILES([ Makefile diff --git a/hello.pb.cc b/hello.pb.cc index f52ba55..5af9655 100644 --- a/hello.pb.cc +++ b/hello.pb.cc @@ -19,17 +19,17 @@ namespace _pbi = _pb::internal; namespace control { PROTOBUF_CONSTEXPR Hello::Hello( - ::_pbi::ConstantInitialized): _impl_{ - /*decltype(_impl_.name_)*/{&::_pbi::fixed_address_empty_string, ::_pbi::ConstantInitialized{}} - , /*decltype(_impl_.id_)*/0 - , /*decltype(_impl_._cached_size_)*/{}} {} + ::_pbi::ConstantInitialized): _impl_{ + /*decltype(_impl_.name_)*/{&::_pbi::fixed_address_empty_string, ::_pbi::ConstantInitialized{}} + , /*decltype(_impl_.id_)*/0 + , /*decltype(_impl_._cached_size_)*/{}} {} struct HelloDefaultTypeInternal { - PROTOBUF_CONSTEXPR HelloDefaultTypeInternal() - : _instance(::_pbi::ConstantInitialized{}) {} - ~HelloDefaultTypeInternal() {} - union { - Hello _instance; - }; + PROTOBUF_CONSTEXPR HelloDefaultTypeInternal() + : _instance(::_pbi::ConstantInitialized{}) {} + ~HelloDefaultTypeInternal() {} + union { + Hello _instance; + }; }; PROTOBUF_ATTRIBUTE_NO_DESTROY PROTOBUF_CONSTINIT PROTOBUF_ATTRIBUTE_INIT_PRIORITY1 HelloDefaultTypeInternal _Hello_default_instance_; } // namespace control @@ -38,232 +38,235 @@ namespace control { // =================================================================== class Hello::_Internal { - public: +public: }; Hello::Hello(::PROTOBUF_NAMESPACE_ID::Arena* arena, - bool is_message_owned) - : ::PROTOBUF_NAMESPACE_ID::MessageLite(arena, is_message_owned) { - SharedCtor(arena, is_message_owned); - // @@protoc_insertion_point(arena_constructor:control.Hello) + bool is_message_owned) + : ::PROTOBUF_NAMESPACE_ID::MessageLite(arena, is_message_owned) { + SharedCtor(arena, is_message_owned); + // @@protoc_insertion_point(arena_constructor:control.Hello) } Hello::Hello(const Hello& from) - : ::PROTOBUF_NAMESPACE_ID::MessageLite() { - Hello* const _this = this; (void)_this; - new (&_impl_) Impl_{ - decltype(_impl_.name_){} - , decltype(_impl_.id_){} - , /*decltype(_impl_._cached_size_)*/{}}; + : ::PROTOBUF_NAMESPACE_ID::MessageLite() { + Hello* const _this = this; + (void)_this; + new (&_impl_) Impl_{ + decltype(_impl_.name_) {} + , decltype(_impl_.id_) {} + , /*decltype(_impl_._cached_size_)*/{}}; - _internal_metadata_.MergeFrom(from._internal_metadata_); - _impl_.name_.InitDefault(); - #ifdef PROTOBUF_FORCE_COPY_DEFAULT_STRING - _impl_.name_.Set("", GetArenaForAllocation()); - #endif // PROTOBUF_FORCE_COPY_DEFAULT_STRING - if (!from._internal_name().empty()) { - _this->_impl_.name_.Set(from._internal_name(), - _this->GetArenaForAllocation()); - } - _this->_impl_.id_ = from._impl_.id_; - // @@protoc_insertion_point(copy_constructor:control.Hello) + _internal_metadata_.MergeFrom(from._internal_metadata_); + _impl_.name_.InitDefault(); +#ifdef PROTOBUF_FORCE_COPY_DEFAULT_STRING + _impl_.name_.Set("", GetArenaForAllocation()); +#endif // PROTOBUF_FORCE_COPY_DEFAULT_STRING + if (!from._internal_name().empty()) { + _this->_impl_.name_.Set(from._internal_name(), + _this->GetArenaForAllocation()); + } + _this->_impl_.id_ = from._impl_.id_; + // @@protoc_insertion_point(copy_constructor:control.Hello) } inline void Hello::SharedCtor( - ::_pb::Arena* arena, bool is_message_owned) { - (void)arena; - (void)is_message_owned; - new (&_impl_) Impl_{ - decltype(_impl_.name_){} - , decltype(_impl_.id_){0} - , /*decltype(_impl_._cached_size_)*/{} - }; - _impl_.name_.InitDefault(); - #ifdef PROTOBUF_FORCE_COPY_DEFAULT_STRING - _impl_.name_.Set("", GetArenaForAllocation()); - #endif // PROTOBUF_FORCE_COPY_DEFAULT_STRING + ::_pb::Arena* arena, bool is_message_owned) { + (void)arena; + (void)is_message_owned; + new (&_impl_) Impl_{ + decltype(_impl_.name_) {} + , decltype(_impl_.id_) { + 0 + } + , /*decltype(_impl_._cached_size_)*/{} + }; + _impl_.name_.InitDefault(); +#ifdef PROTOBUF_FORCE_COPY_DEFAULT_STRING + _impl_.name_.Set("", GetArenaForAllocation()); +#endif // PROTOBUF_FORCE_COPY_DEFAULT_STRING } Hello::~Hello() { - // @@protoc_insertion_point(destructor:control.Hello) - if (auto *arena = _internal_metadata_.DeleteReturnArena()) { - (void)arena; - return; - } - SharedDtor(); + // @@protoc_insertion_point(destructor:control.Hello) + if (auto *arena = _internal_metadata_.DeleteReturnArena()) { + (void)arena; + return; + } + SharedDtor(); } inline void Hello::SharedDtor() { - GOOGLE_DCHECK(GetArenaForAllocation() == nullptr); - _impl_.name_.Destroy(); + GOOGLE_DCHECK(GetArenaForAllocation() == nullptr); + _impl_.name_.Destroy(); } void Hello::SetCachedSize(int size) const { - _impl_._cached_size_.Set(size); + _impl_._cached_size_.Set(size); } void Hello::Clear() { // @@protoc_insertion_point(message_clear_start:control.Hello) - uint32_t cached_has_bits = 0; - // Prevent compiler warnings about cached_has_bits being unused - (void) cached_has_bits; + uint32_t cached_has_bits = 0; + // Prevent compiler warnings about cached_has_bits being unused + (void) cached_has_bits; - _impl_.name_.ClearToEmpty(); - _impl_.id_ = 0; - _internal_metadata_.Clear(); + _impl_.name_.ClearToEmpty(); + _impl_.id_ = 0; + _internal_metadata_.Clear(); } const char* Hello::_InternalParse(const char* ptr, ::_pbi::ParseContext* ctx) { #define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure - while (!ctx->Done(&ptr)) { - uint32_t tag; - ptr = ::_pbi::ReadTag(ptr, &tag); - switch (tag >> 3) { - // int32 id = 1; - case 1: - if (PROTOBUF_PREDICT_TRUE(static_cast(tag) == 8)) { - _impl_.id_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint32(&ptr); - CHK_(ptr); - } else - goto handle_unusual; - continue; - // string name = 2; - case 2: - if (PROTOBUF_PREDICT_TRUE(static_cast(tag) == 18)) { - auto str = _internal_mutable_name(); - ptr = ::_pbi::InlineGreedyStringParser(str, ptr, ctx); - CHK_(ptr); - CHK_(::_pbi::VerifyUTF8(str, nullptr)); - } else - goto handle_unusual; - continue; - default: - goto handle_unusual; - } // switch - handle_unusual: - if ((tag == 0) || ((tag & 7) == 4)) { - CHK_(ptr); - ctx->SetLastTag(tag); - goto message_done; - } - ptr = UnknownFieldParse( - tag, - _internal_metadata_.mutable_unknown_fields(), - ptr, ctx); - CHK_(ptr != nullptr); - } // while + while (!ctx->Done(&ptr)) { + uint32_t tag; + ptr = ::_pbi::ReadTag(ptr, &tag); + switch (tag >> 3) { + // int32 id = 1; + case 1: + if (PROTOBUF_PREDICT_TRUE(static_cast(tag) == 8)) { + _impl_.id_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint32(&ptr); + CHK_(ptr); + } else + goto handle_unusual; + continue; + // string name = 2; + case 2: + if (PROTOBUF_PREDICT_TRUE(static_cast(tag) == 18)) { + auto str = _internal_mutable_name(); + ptr = ::_pbi::InlineGreedyStringParser(str, ptr, ctx); + CHK_(ptr); + CHK_(::_pbi::VerifyUTF8(str, nullptr)); + } else + goto handle_unusual; + continue; + default: + goto handle_unusual; + } // switch +handle_unusual: + if ((tag == 0) || ((tag & 7) == 4)) { + CHK_(ptr); + ctx->SetLastTag(tag); + goto message_done; + } + ptr = UnknownFieldParse( + tag, + _internal_metadata_.mutable_unknown_fields(), + ptr, ctx); + CHK_(ptr != nullptr); + } // while message_done: - return ptr; + return ptr; failure: - ptr = nullptr; - goto message_done; + ptr = nullptr; + goto message_done; #undef CHK_ } uint8_t* Hello::_InternalSerialize( - uint8_t* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const { - // @@protoc_insertion_point(serialize_to_array_start:control.Hello) - uint32_t cached_has_bits = 0; - (void) cached_has_bits; + uint8_t* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const { + // @@protoc_insertion_point(serialize_to_array_start:control.Hello) + uint32_t cached_has_bits = 0; + (void) cached_has_bits; - // int32 id = 1; - if (this->_internal_id() != 0) { - target = stream->EnsureSpace(target); - target = ::_pbi::WireFormatLite::WriteInt32ToArray(1, this->_internal_id(), target); - } + // int32 id = 1; + if (this->_internal_id() != 0) { + target = stream->EnsureSpace(target); + target = ::_pbi::WireFormatLite::WriteInt32ToArray(1, this->_internal_id(), target); + } - // string name = 2; - if (!this->_internal_name().empty()) { - ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::VerifyUtf8String( - this->_internal_name().data(), static_cast(this->_internal_name().length()), - ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::SERIALIZE, - "control.Hello.name"); - target = stream->WriteStringMaybeAliased( - 2, this->_internal_name(), target); - } + // string name = 2; + if (!this->_internal_name().empty()) { + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::VerifyUtf8String( + this->_internal_name().data(), static_cast(this->_internal_name().length()), + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::SERIALIZE, + "control.Hello.name"); + target = stream->WriteStringMaybeAliased( + 2, this->_internal_name(), target); + } - if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { - target = stream->WriteRaw(_internal_metadata_.unknown_fields(::PROTOBUF_NAMESPACE_ID::internal::GetEmptyString).data(), - static_cast(_internal_metadata_.unknown_fields(::PROTOBUF_NAMESPACE_ID::internal::GetEmptyString).size()), target); - } - // @@protoc_insertion_point(serialize_to_array_end:control.Hello) - return target; + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + target = stream->WriteRaw(_internal_metadata_.unknown_fields(::PROTOBUF_NAMESPACE_ID::internal::GetEmptyString).data(), + static_cast(_internal_metadata_.unknown_fields(::PROTOBUF_NAMESPACE_ID::internal::GetEmptyString).size()), target); + } + // @@protoc_insertion_point(serialize_to_array_end:control.Hello) + return target; } size_t Hello::ByteSizeLong() const { // @@protoc_insertion_point(message_byte_size_start:control.Hello) - size_t total_size = 0; + size_t total_size = 0; - uint32_t cached_has_bits = 0; - // Prevent compiler warnings about cached_has_bits being unused - (void) cached_has_bits; + uint32_t cached_has_bits = 0; + // Prevent compiler warnings about cached_has_bits being unused + (void) cached_has_bits; - // string name = 2; - if (!this->_internal_name().empty()) { - total_size += 1 + - ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::StringSize( - this->_internal_name()); - } + // string name = 2; + if (!this->_internal_name().empty()) { + total_size += 1 + + ::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::StringSize( + this->_internal_name()); + } - // int32 id = 1; - if (this->_internal_id() != 0) { - total_size += ::_pbi::WireFormatLite::Int32SizePlusOne(this->_internal_id()); - } + // int32 id = 1; + if (this->_internal_id() != 0) { + total_size += ::_pbi::WireFormatLite::Int32SizePlusOne(this->_internal_id()); + } - if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { - total_size += _internal_metadata_.unknown_fields(::PROTOBUF_NAMESPACE_ID::internal::GetEmptyString).size(); - } - int cached_size = ::_pbi::ToCachedSize(total_size); - SetCachedSize(cached_size); - return total_size; + if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) { + total_size += _internal_metadata_.unknown_fields(::PROTOBUF_NAMESPACE_ID::internal::GetEmptyString).size(); + } + int cached_size = ::_pbi::ToCachedSize(total_size); + SetCachedSize(cached_size); + return total_size; } void Hello::CheckTypeAndMergeFrom( - const ::PROTOBUF_NAMESPACE_ID::MessageLite& from) { - MergeFrom(*::_pbi::DownCast( - &from)); + const ::PROTOBUF_NAMESPACE_ID::MessageLite& from) { + MergeFrom(*::_pbi::DownCast( + &from)); } void Hello::MergeFrom(const Hello& from) { - Hello* const _this = this; - // @@protoc_insertion_point(class_specific_merge_from_start:control.Hello) - GOOGLE_DCHECK_NE(&from, _this); - uint32_t cached_has_bits = 0; - (void) cached_has_bits; + Hello* const _this = this; + // @@protoc_insertion_point(class_specific_merge_from_start:control.Hello) + GOOGLE_DCHECK_NE(&from, _this); + uint32_t cached_has_bits = 0; + (void) cached_has_bits; - if (!from._internal_name().empty()) { - _this->_internal_set_name(from._internal_name()); - } - if (from._internal_id() != 0) { - _this->_internal_set_id(from._internal_id()); - } - _this->_internal_metadata_.MergeFrom(from._internal_metadata_); + if (!from._internal_name().empty()) { + _this->_internal_set_name(from._internal_name()); + } + if (from._internal_id() != 0) { + _this->_internal_set_id(from._internal_id()); + } + _this->_internal_metadata_.MergeFrom(from._internal_metadata_); } void Hello::CopyFrom(const Hello& from) { // @@protoc_insertion_point(class_specific_copy_from_start:control.Hello) - if (&from == this) return; - Clear(); - MergeFrom(from); + if (&from == this) return; + Clear(); + MergeFrom(from); } bool Hello::IsInitialized() const { - return true; + return true; } void Hello::InternalSwap(Hello* other) { - using std::swap; - auto* lhs_arena = GetArenaForAllocation(); - auto* rhs_arena = other->GetArenaForAllocation(); - _internal_metadata_.InternalSwap(&other->_internal_metadata_); - ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::InternalSwap( - &_impl_.name_, lhs_arena, - &other->_impl_.name_, rhs_arena - ); - swap(_impl_.id_, other->_impl_.id_); + using std::swap; + auto* lhs_arena = GetArenaForAllocation(); + auto* rhs_arena = other->GetArenaForAllocation(); + _internal_metadata_.InternalSwap(&other->_internal_metadata_); + ::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::InternalSwap( + &_impl_.name_, lhs_arena, + &other->_impl_.name_, rhs_arena + ); + swap(_impl_.id_, other->_impl_.id_); } std::string Hello::GetTypeName() const { - return "control.Hello"; + return "control.Hello"; } @@ -272,7 +275,7 @@ std::string Hello::GetTypeName() const { PROTOBUF_NAMESPACE_OPEN template<> PROTOBUF_NOINLINE ::control::Hello* Arena::CreateMaybeMessage< ::control::Hello >(Arena* arena) { - return Arena::CreateMessageInternal< ::control::Hello >(arena); + return Arena::CreateMessageInternal< ::control::Hello >(arena); } PROTOBUF_NAMESPACE_CLOSE diff --git a/interface.cpp b/interface.cpp index cd48bfd..0c99a49 100644 --- a/interface.cpp +++ b/interface.cpp @@ -284,26 +284,26 @@ Interface::~Interface() { std::expected Interface::UpN(void) { int netlinkfd = 0; - if ((netlinkfd = socket(AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, NETLINK_ROUTE)) < 0) { + if ((netlinkfd = socket(AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, NETLINK_ROUTE)) < 0) { int errnocopy = errno; std::string error = std::strerror(errnocopy); return std::unexpected("Create interface error: " + error); } - struct { - struct nlmsghdr header; - struct ifinfomsg content; - } request; + struct { + struct nlmsghdr header; + struct ifinfomsg content; + } request; - memset(&request, 0, sizeof request); - request.header.nlmsg_len = NLMSG_LENGTH(sizeof request.content); - request.header.nlmsg_flags = NLM_F_REQUEST; - request.header.nlmsg_type = RTM_NEWLINK; - request.content.ifi_index = if_nametoindex(ifname.c_str()); - request.content.ifi_flags = IFF_UP; - request.content.ifi_change = 1; + memset(&request, 0, sizeof request); + request.header.nlmsg_len = NLMSG_LENGTH(sizeof request.content); + request.header.nlmsg_flags = NLM_F_REQUEST; + request.header.nlmsg_type = RTM_NEWLINK; + request.content.ifi_index = if_nametoindex(ifname.c_str()); + request.content.ifi_flags = IFF_UP; + request.content.ifi_change = 1; - if (send(netlinkfd, &request, request.header.nlmsg_len, 0) < 0) { + if (send(netlinkfd, &request, request.header.nlmsg_len, 0) < 0) { close(netlinkfd); int errnocopy = errno; std::string error = std::strerror(errnocopy); @@ -316,7 +316,7 @@ std::expected Interface::UpN(void) { std::expected Interface::SetIP4AddrMask(const std::string address, const int prefix) { int netlinkfd = 0; - if ((netlinkfd = socket(AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, NETLINK_ROUTE)) < 0) { + if ((netlinkfd = socket(AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, NETLINK_ROUTE)) < 0) { int errnocopy = errno; std::string error = std::strerror(errnocopy); return std::unexpected("Create interface error: " + error); @@ -324,46 +324,46 @@ std::expected Interface::SetIP4AddrMask(const std::string add int rc = 0; struct sockaddr_nl sockaddr; - memset(&sockaddr, 0, sizeof sockaddr); - sockaddr.nl_family = AF_NETLINK; - if ((rc = bind(netlinkfd, (struct sockaddr*) &sockaddr, sizeof sockaddr))< 0) { + memset(&sockaddr, 0, sizeof sockaddr); + sockaddr.nl_family = AF_NETLINK; + if ((rc = bind(netlinkfd, (struct sockaddr*) &sockaddr, sizeof sockaddr))< 0) { close(netlinkfd); int errnocopy = errno; std::string error = std::strerror(errnocopy); return std::unexpected("Set interface address error: " + error); } struct { - struct nlmsghdr header; - struct ifaddrmsg content; - char attributes_buf[64]; - } request; + struct nlmsghdr header; + struct ifaddrmsg content; + char attributes_buf[64]; + } request; - struct rtattr *request_attr; - size_t attributes_buf_avail = sizeof request.attributes_buf; + struct rtattr *request_attr; + size_t attributes_buf_avail = sizeof request.attributes_buf; - memset(&request, 0, sizeof request); - request.header.nlmsg_len = NLMSG_LENGTH(sizeof request.content); - request.header.nlmsg_flags = NLM_F_REQUEST | NLM_F_EXCL | NLM_F_CREATE; - request.header.nlmsg_type = RTM_NEWADDR; - request.content.ifa_index = if_nametoindex(ifname.c_str()); - request.content.ifa_family = AF_INET; - request.content.ifa_prefixlen = prefix; + memset(&request, 0, sizeof request); + request.header.nlmsg_len = NLMSG_LENGTH(sizeof request.content); + request.header.nlmsg_flags = NLM_F_REQUEST | NLM_F_EXCL | NLM_F_CREATE; + request.header.nlmsg_type = RTM_NEWADDR; + request.content.ifa_index = if_nametoindex(ifname.c_str()); + request.content.ifa_family = AF_INET; + request.content.ifa_prefixlen = prefix; - /* request.attributes[IFA_LOCAL] = address */ - request_attr = IFA_RTA(&request.content); - request_attr->rta_type = IFA_LOCAL; - request_attr->rta_len = RTA_LENGTH(sizeof (struct in_addr)); - request.header.nlmsg_len += request_attr->rta_len; - inet_pton(AF_INET, address.c_str(), RTA_DATA(request_attr)); + /* request.attributes[IFA_LOCAL] = address */ + request_attr = IFA_RTA(&request.content); + request_attr->rta_type = IFA_LOCAL; + request_attr->rta_len = RTA_LENGTH(sizeof (struct in_addr)); + request.header.nlmsg_len += request_attr->rta_len; + inet_pton(AF_INET, address.c_str(), RTA_DATA(request_attr)); - /* request.attributes[IFA_ADDRESS] = address */ - request_attr = RTA_NEXT(request_attr, attributes_buf_avail); - request_attr->rta_type = IFA_ADDRESS; - request_attr->rta_len = RTA_LENGTH(sizeof (struct in_addr)); - request.header.nlmsg_len += request_attr->rta_len; - inet_pton(AF_INET, address.c_str(), RTA_DATA(request_attr)); + /* request.attributes[IFA_ADDRESS] = address */ + request_attr = RTA_NEXT(request_attr, attributes_buf_avail); + request_attr->rta_type = IFA_ADDRESS; + request_attr->rta_len = RTA_LENGTH(sizeof (struct in_addr)); + request.header.nlmsg_len += request_attr->rta_len; + inet_pton(AF_INET, address.c_str(), RTA_DATA(request_attr)); - if (send(netlinkfd, &request, request.header.nlmsg_len, 0) < 0) { + if (send(netlinkfd, &request, request.header.nlmsg_len, 0) < 0) { close(netlinkfd); int errnocopy = errno; std::string error = std::strerror(errnocopy); diff --git a/interface.hpp b/interface.hpp index 8c7285c..e405d8a 100644 --- a/interface.hpp +++ b/interface.hpp @@ -3,11 +3,11 @@ #define INTERFACE_HPP class Interface { - private: +private: int tunfd; std::string ifname; int mtu; - public: +public: std::expected Create(const std::string name); std::string Name(); int MTU(); diff --git a/resolver.hpp b/resolver.hpp index 463c4fd..c24fb69 100644 --- a/resolver.hpp +++ b/resolver.hpp @@ -3,18 +3,18 @@ #include class Address { - private: +private: int family; std::string address; - public: +public: Address(const std::string iaddress, const int ifamily); int GetFamily(); std::string GetAddress(); }; class Resolver { - private: +private: int family; - public: +public: std::expected Resolve(const std::string hostname); }; diff --git a/server.hpp b/server.hpp index a68bebe..f5583fd 100644 --- a/server.hpp +++ b/server.hpp @@ -8,8 +8,8 @@ #include class Server { - private: +private: Interface iface; - public: +public: std::expected Create(const std::string name, const std::string addr, const int prefix); }; diff --git a/service.cpp b/service.cpp index ae1e00e..ea5c80b 100644 --- a/service.cpp +++ b/service.cpp @@ -31,7 +31,7 @@ Service::~Service() { close(port); } -std::expected Service::Bind(void){ +std::expected Service::Bind(void) { struct sockaddr_in address; int srvsock; if ((srvsock = socket(AF_INET, SOCK_STREAM, 0)) == 0) { @@ -62,7 +62,7 @@ std::expected Service::Bind(void){ return {}; } -std::expected Service::Listen(Handler *handler){ +std::expected Service::Listen(Handler *handler) { struct sockaddr_in address; int addrlen = sizeof(address); int newsock = 0; diff --git a/service.hpp b/service.hpp index 953ef01..30ddf54 100644 --- a/service.hpp +++ b/service.hpp @@ -6,15 +6,15 @@ #include class Handler { - public: +public: void Handle(int sock); }; class Service { - private: +private: int port; int sock; - public: +public: explicit Service(int port); std::expected Bind(void); std::expected Listen(Handler *handler); diff --git a/udpclient.cpp b/udpclient.cpp index 5760afd..f409cbf 100644 --- a/udpclient.cpp +++ b/udpclient.cpp @@ -18,7 +18,7 @@ extern "C" { #include -UDPClient::UDPClient(void){ +UDPClient::UDPClient(void) { sockfd = 0; rmax = 2024; }; @@ -53,7 +53,7 @@ std::expected UDPClient::Send(std::string message) { servaddr.sin_addr.s_addr = inet_addr(address.c_str()); if (sendto(sockfd, message.data(), message.size(), 0, - (const struct sockaddr*)&servaddr, sizeof(servaddr)) < 0) { + (const struct sockaddr*)&servaddr, sizeof(servaddr)) < 0) { int errnocopy = errno; std::string error = std::strerror(errnocopy); return std::unexpected("Send datagram error: " + error); @@ -72,7 +72,7 @@ std::expected UDPClient::Recv() { int rsize = 0; std::string buffer(rmax, 0); if((rsize = recvfrom(sockfd, buffer.data(), buffer.size(), - 0, (struct sockaddr *)&servaddr, &len)) < 0) { + 0, (struct sockaddr *)&servaddr, &len)) < 0) { int errnocopy = errno; std::string error = std::strerror(errnocopy); return std::unexpected("Receive datagram error: " + error); diff --git a/udpclient.hpp b/udpclient.hpp index 5456793..f0bf796 100644 --- a/udpclient.hpp +++ b/udpclient.hpp @@ -3,13 +3,13 @@ #define UPDCLIENT_HPP class UDPClient { - private: +private: int sockfd; std::string address; int port; int rmax; int family; - public: +public: UDPClient(void); std::expected Bind(std::string address, int port); std::expected Send(std::string buffer);