working commit
This commit is contained in:
@@ -88,7 +88,7 @@ POST_INSTALL = :
|
||||
NORMAL_UNINSTALL = :
|
||||
PRE_UNINSTALL = :
|
||||
POST_UNINSTALL = :
|
||||
sbin_PROGRAMS = helmetd$(EXEEXT)
|
||||
sbin_PROGRAMS = helmetsrv$(EXEEXT) helmetcli$(EXEEXT)
|
||||
subdir = .
|
||||
ACLOCAL_M4 = $(top_srcdir)/aclocal.m4
|
||||
am__aclocal_m4_deps = $(top_srcdir)/configure.ac
|
||||
@@ -104,11 +104,16 @@ CONFIG_CLEAN_FILES =
|
||||
CONFIG_CLEAN_VPATH_FILES =
|
||||
am__installdirs = "$(DESTDIR)$(sbindir)"
|
||||
PROGRAMS = $(sbin_PROGRAMS)
|
||||
am__objects_1 = uxlogger.$(OBJEXT) tservice.$(OBJEXT) \
|
||||
sockhand.$(OBJEXT) msgheader.$(OBJEXT)
|
||||
am_helmetd_OBJECTS = helmetd.$(OBJEXT) $(am__objects_1)
|
||||
helmetd_OBJECTS = $(am_helmetd_OBJECTS)
|
||||
helmetd_LDADD = $(LDADD)
|
||||
am__objects_1 = uxlogger.$(OBJEXT) msgheader.$(OBJEXT) \
|
||||
interface.$(OBJEXT) uxcontrol.pb.$(OBJEXT)
|
||||
am_helmetcli_OBJECTS = helmetcli.$(OBJEXT) uxclient.$(OBJEXT) \
|
||||
$(am__objects_1)
|
||||
helmetcli_OBJECTS = $(am_helmetcli_OBJECTS)
|
||||
helmetcli_LDADD = $(LDADD)
|
||||
am_helmetsrv_OBJECTS = helmetsrv.$(OBJEXT) tservice.$(OBJEXT) \
|
||||
sockhand.$(OBJEXT) $(am__objects_1)
|
||||
helmetsrv_OBJECTS = $(am_helmetsrv_OBJECTS)
|
||||
helmetsrv_LDADD = $(LDADD)
|
||||
AM_V_P = $(am__v_P_$(V))
|
||||
am__v_P_ = $(am__v_P_$(AM_DEFAULT_VERBOSITY))
|
||||
am__v_P_0 = false
|
||||
@@ -149,8 +154,8 @@ AM_V_CCLD = $(am__v_CCLD_$(V))
|
||||
am__v_CCLD_ = $(am__v_CCLD_$(AM_DEFAULT_VERBOSITY))
|
||||
am__v_CCLD_0 = @echo " CCLD " $@;
|
||||
am__v_CCLD_1 =
|
||||
SOURCES = $(helmetd_SOURCES)
|
||||
DIST_SOURCES = $(helmetd_SOURCES)
|
||||
SOURCES = $(helmetcli_SOURCES) $(helmetsrv_SOURCES)
|
||||
DIST_SOURCES = $(helmetcli_SOURCES) $(helmetsrv_SOURCES)
|
||||
am__can_run_installinfo = \
|
||||
case $$AM_UPDATE_INFO_DIR in \
|
||||
n|no|NO) false;; \
|
||||
@@ -198,13 +203,13 @@ am__distuninstallcheck_listfiles = $(distuninstallcheck_listfiles) \
|
||||
distcleancheck_listfiles = \
|
||||
find . \( -type f -a \! \
|
||||
\( -name .nfs* -o -name .smb* -o -name .__afs* \) \) -print
|
||||
ACLOCAL = ${SHELL} '/home/ziggi/projects/stmsg/missing' aclocal-1.17
|
||||
ACLOCAL = ${SHELL} '/home/ziggi/Projects/stvpn/missing' aclocal-1.17
|
||||
AMTAR = $${TAR-tar}
|
||||
AM_DEFAULT_VERBOSITY = 1
|
||||
ASTYLE = /bin/astyle
|
||||
AUTOCONF = ${SHELL} '/home/ziggi/projects/stmsg/missing' autoconf
|
||||
AUTOHEADER = ${SHELL} '/home/ziggi/projects/stmsg/missing' autoheader
|
||||
AUTOMAKE = ${SHELL} '/home/ziggi/projects/stmsg/missing' automake-1.17
|
||||
AUTOCONF = ${SHELL} '/home/ziggi/Projects/stvpn/missing' autoconf
|
||||
AUTOHEADER = ${SHELL} '/home/ziggi/Projects/stvpn/missing' autoheader
|
||||
AUTOMAKE = ${SHELL} '/home/ziggi/Projects/stvpn/missing' automake-1.17
|
||||
AWK = mawk
|
||||
CC = gcc
|
||||
CCDEPMODE = depmode=none
|
||||
@@ -232,7 +237,7 @@ LDFLAGS = -pthread
|
||||
LIBOBJS =
|
||||
LIBS = -Wl,--as-need -lprotobuf
|
||||
LTLIBOBJS =
|
||||
MAKEINFO = ${SHELL} '/home/ziggi/projects/stmsg/missing' makeinfo
|
||||
MAKEINFO = ${SHELL} '/home/ziggi/Projects/stvpn/missing' makeinfo
|
||||
MKDIR_P = /bin/mkdir -p
|
||||
OBJEXT = o
|
||||
PACKAGE = helmet
|
||||
@@ -249,10 +254,10 @@ SET_MAKE =
|
||||
SHELL = /bin/bash
|
||||
STRIP =
|
||||
VERSION = 0.0.1
|
||||
abs_builddir = /home/ziggi/projects/stmsg
|
||||
abs_srcdir = /home/ziggi/projects/stmsg
|
||||
abs_top_builddir = /home/ziggi/projects/stmsg
|
||||
abs_top_srcdir = /home/ziggi/projects/stmsg
|
||||
abs_builddir = /home/ziggi/Projects/stvpn
|
||||
abs_srcdir = /home/ziggi/Projects/stvpn
|
||||
abs_top_builddir = /home/ziggi/Projects/stvpn
|
||||
abs_top_srcdir = /home/ziggi/Projects/stvpn
|
||||
ac_ct_CC = gcc
|
||||
ac_ct_CXX = g++
|
||||
am__include = include
|
||||
@@ -274,7 +279,7 @@ host_alias =
|
||||
htmldir = ${docdir}
|
||||
includedir = ${prefix}/include
|
||||
infodir = ${datarootdir}/info
|
||||
install_sh = ${SHELL} /home/ziggi/projects/stmsg/install-sh
|
||||
install_sh = ${SHELL} /home/ziggi/Projects/stvpn/install-sh
|
||||
libdir = ${exec_prefix}/lib
|
||||
libexecdir = ${exec_prefix}/libexec
|
||||
localedir = ${datarootdir}/locale
|
||||
@@ -296,19 +301,27 @@ top_build_prefix =
|
||||
top_builddir = .
|
||||
top_srcdir = .
|
||||
AUTOMAKE_OPTIONS = foreign no-dependencies no-installinfo subdir-objects
|
||||
helmetd_SOURCES = helmetd.cpp $(helmet_SOURCES)
|
||||
helmet_SOURCES = \
|
||||
uxlogger.cpp uxlogger.hpp \
|
||||
helmetsrv_SOURCES = helmetsrv.cpp \
|
||||
tservice.cpp tservice.hpp \
|
||||
sockhand.cpp dockhand.hpp \
|
||||
msgheader.cpp msgheader.hpp
|
||||
$(helmet_SOURCES)
|
||||
|
||||
helmetcli_SOURCES = helmetcli.cpp \
|
||||
uxclient.cpp uxclient.hpp \
|
||||
$(helmet_SOURCES)
|
||||
|
||||
helmet_SOURCES = \
|
||||
uxlogger.cpp uxlogger.hpp \
|
||||
msgheader.cpp msgheader.hpp \
|
||||
interface.cpp interface.hpp \
|
||||
uxcontrol.pb.cc uxcontrol.pb.h
|
||||
|
||||
ASTYLE_OPTS = --indent=spaces=8 --style=java
|
||||
all: defines.hpp
|
||||
$(MAKE) $(AM_MAKEFLAGS) all-am
|
||||
|
||||
.SUFFIXES:
|
||||
.SUFFIXES: .cpp .o .obj
|
||||
.SUFFIXES: .cc .cpp .o .obj
|
||||
am--refresh: Makefile
|
||||
@:
|
||||
$(srcdir)/Makefile.in: $(srcdir)/Makefile.am $(am__configure_deps)
|
||||
@@ -400,9 +413,13 @@ uninstall-sbinPROGRAMS:
|
||||
clean-sbinPROGRAMS:
|
||||
-$(am__rm_f) $(sbin_PROGRAMS)
|
||||
|
||||
helmetd$(EXEEXT): $(helmetd_OBJECTS) $(helmetd_DEPENDENCIES) $(EXTRA_helmetd_DEPENDENCIES)
|
||||
@rm -f helmetd$(EXEEXT)
|
||||
$(AM_V_CXXLD)$(CXXLINK) $(helmetd_OBJECTS) $(helmetd_LDADD) $(LIBS)
|
||||
helmetcli$(EXEEXT): $(helmetcli_OBJECTS) $(helmetcli_DEPENDENCIES) $(EXTRA_helmetcli_DEPENDENCIES)
|
||||
@rm -f helmetcli$(EXEEXT)
|
||||
$(AM_V_CXXLD)$(CXXLINK) $(helmetcli_OBJECTS) $(helmetcli_LDADD) $(LIBS)
|
||||
|
||||
helmetsrv$(EXEEXT): $(helmetsrv_OBJECTS) $(helmetsrv_DEPENDENCIES) $(EXTRA_helmetsrv_DEPENDENCIES)
|
||||
@rm -f helmetsrv$(EXEEXT)
|
||||
$(AM_V_CXXLD)$(CXXLINK) $(helmetsrv_OBJECTS) $(helmetsrv_LDADD) $(LIBS)
|
||||
|
||||
mostlyclean-compile:
|
||||
-rm -f *.$(OBJEXT)
|
||||
@@ -410,6 +427,12 @@ mostlyclean-compile:
|
||||
distclean-compile:
|
||||
-rm -f *.tab.c
|
||||
|
||||
.cc.o:
|
||||
$(AM_V_CXX)$(CXXCOMPILE) -c -o $@ $<
|
||||
|
||||
.cc.obj:
|
||||
$(AM_V_CXX)$(CXXCOMPILE) -c -o $@ `$(CYGPATH_W) '$<'`
|
||||
|
||||
.cpp.o:
|
||||
$(AM_V_CXX)$(CXXCOMPILE) -c -o $@ $<
|
||||
|
||||
|
||||
+12
-5
@@ -5,15 +5,22 @@ CXXFLAGS = -std=c++23 -Wall -I. -pthread -D_GNU_SOURCE=1 -MMD -MP
|
||||
LDFLAGS = -pthread
|
||||
LIBS = -Wl,--as-need -lprotobuf
|
||||
|
||||
sbin_PROGRAMS = helmetd
|
||||
sbin_PROGRAMS = helmetsrv helmetcli
|
||||
|
||||
helmetd_SOURCES = helmetd.cpp $(helmet_SOURCES)
|
||||
helmetsrv_SOURCES = helmetsrv.cpp \
|
||||
tservice.cpp tservice.hpp \
|
||||
sockhand.cpp dockhand.hpp \
|
||||
$(helmet_SOURCES)
|
||||
|
||||
helmetcli_SOURCES = helmetcli.cpp \
|
||||
uxclient.cpp uxclient.hpp \
|
||||
$(helmet_SOURCES)
|
||||
|
||||
helmet_SOURCES = \
|
||||
uxlogger.cpp uxlogger.hpp \
|
||||
tservice.cpp tservice.hpp \
|
||||
sockhand.cpp dockhand.hpp \
|
||||
msgheader.cpp msgheader.hpp
|
||||
msgheader.cpp msgheader.hpp \
|
||||
interface.cpp interface.hpp \
|
||||
uxcontrol.pb.cc uxcontrol.pb.h
|
||||
|
||||
ASTYLE_OPTS = --indent=spaces=8 --style=java
|
||||
format:
|
||||
|
||||
+39
-16
@@ -88,7 +88,7 @@ POST_INSTALL = :
|
||||
NORMAL_UNINSTALL = :
|
||||
PRE_UNINSTALL = :
|
||||
POST_UNINSTALL = :
|
||||
sbin_PROGRAMS = helmetd$(EXEEXT)
|
||||
sbin_PROGRAMS = helmetsrv$(EXEEXT) helmetcli$(EXEEXT)
|
||||
subdir = .
|
||||
ACLOCAL_M4 = $(top_srcdir)/aclocal.m4
|
||||
am__aclocal_m4_deps = $(top_srcdir)/configure.ac
|
||||
@@ -104,11 +104,16 @@ CONFIG_CLEAN_FILES =
|
||||
CONFIG_CLEAN_VPATH_FILES =
|
||||
am__installdirs = "$(DESTDIR)$(sbindir)"
|
||||
PROGRAMS = $(sbin_PROGRAMS)
|
||||
am__objects_1 = uxlogger.$(OBJEXT) tservice.$(OBJEXT) \
|
||||
sockhand.$(OBJEXT) msgheader.$(OBJEXT)
|
||||
am_helmetd_OBJECTS = helmetd.$(OBJEXT) $(am__objects_1)
|
||||
helmetd_OBJECTS = $(am_helmetd_OBJECTS)
|
||||
helmetd_LDADD = $(LDADD)
|
||||
am__objects_1 = uxlogger.$(OBJEXT) msgheader.$(OBJEXT) \
|
||||
interface.$(OBJEXT) uxcontrol.pb.$(OBJEXT)
|
||||
am_helmetcli_OBJECTS = helmetcli.$(OBJEXT) uxclient.$(OBJEXT) \
|
||||
$(am__objects_1)
|
||||
helmetcli_OBJECTS = $(am_helmetcli_OBJECTS)
|
||||
helmetcli_LDADD = $(LDADD)
|
||||
am_helmetsrv_OBJECTS = helmetsrv.$(OBJEXT) tservice.$(OBJEXT) \
|
||||
sockhand.$(OBJEXT) $(am__objects_1)
|
||||
helmetsrv_OBJECTS = $(am_helmetsrv_OBJECTS)
|
||||
helmetsrv_LDADD = $(LDADD)
|
||||
AM_V_P = $(am__v_P_@AM_V@)
|
||||
am__v_P_ = $(am__v_P_@AM_DEFAULT_V@)
|
||||
am__v_P_0 = false
|
||||
@@ -149,8 +154,8 @@ AM_V_CCLD = $(am__v_CCLD_@AM_V@)
|
||||
am__v_CCLD_ = $(am__v_CCLD_@AM_DEFAULT_V@)
|
||||
am__v_CCLD_0 = @echo " CCLD " $@;
|
||||
am__v_CCLD_1 =
|
||||
SOURCES = $(helmetd_SOURCES)
|
||||
DIST_SOURCES = $(helmetd_SOURCES)
|
||||
SOURCES = $(helmetcli_SOURCES) $(helmetsrv_SOURCES)
|
||||
DIST_SOURCES = $(helmetcli_SOURCES) $(helmetsrv_SOURCES)
|
||||
am__can_run_installinfo = \
|
||||
case $$AM_UPDATE_INFO_DIR in \
|
||||
n|no|NO) false;; \
|
||||
@@ -296,19 +301,27 @@ top_build_prefix = @top_build_prefix@
|
||||
top_builddir = @top_builddir@
|
||||
top_srcdir = @top_srcdir@
|
||||
AUTOMAKE_OPTIONS = foreign no-dependencies no-installinfo subdir-objects
|
||||
helmetd_SOURCES = helmetd.cpp $(helmet_SOURCES)
|
||||
helmet_SOURCES = \
|
||||
uxlogger.cpp uxlogger.hpp \
|
||||
helmetsrv_SOURCES = helmetsrv.cpp \
|
||||
tservice.cpp tservice.hpp \
|
||||
sockhand.cpp dockhand.hpp \
|
||||
msgheader.cpp msgheader.hpp
|
||||
$(helmet_SOURCES)
|
||||
|
||||
helmetcli_SOURCES = helmetcli.cpp \
|
||||
uxclient.cpp uxclient.hpp \
|
||||
$(helmet_SOURCES)
|
||||
|
||||
helmet_SOURCES = \
|
||||
uxlogger.cpp uxlogger.hpp \
|
||||
msgheader.cpp msgheader.hpp \
|
||||
interface.cpp interface.hpp \
|
||||
uxcontrol.pb.cc uxcontrol.pb.h
|
||||
|
||||
ASTYLE_OPTS = --indent=spaces=8 --style=java
|
||||
all: defines.hpp
|
||||
$(MAKE) $(AM_MAKEFLAGS) all-am
|
||||
|
||||
.SUFFIXES:
|
||||
.SUFFIXES: .cpp .o .obj
|
||||
.SUFFIXES: .cc .cpp .o .obj
|
||||
am--refresh: Makefile
|
||||
@:
|
||||
$(srcdir)/Makefile.in: $(srcdir)/Makefile.am $(am__configure_deps)
|
||||
@@ -400,9 +413,13 @@ uninstall-sbinPROGRAMS:
|
||||
clean-sbinPROGRAMS:
|
||||
-$(am__rm_f) $(sbin_PROGRAMS)
|
||||
|
||||
helmetd$(EXEEXT): $(helmetd_OBJECTS) $(helmetd_DEPENDENCIES) $(EXTRA_helmetd_DEPENDENCIES)
|
||||
@rm -f helmetd$(EXEEXT)
|
||||
$(AM_V_CXXLD)$(CXXLINK) $(helmetd_OBJECTS) $(helmetd_LDADD) $(LIBS)
|
||||
helmetcli$(EXEEXT): $(helmetcli_OBJECTS) $(helmetcli_DEPENDENCIES) $(EXTRA_helmetcli_DEPENDENCIES)
|
||||
@rm -f helmetcli$(EXEEXT)
|
||||
$(AM_V_CXXLD)$(CXXLINK) $(helmetcli_OBJECTS) $(helmetcli_LDADD) $(LIBS)
|
||||
|
||||
helmetsrv$(EXEEXT): $(helmetsrv_OBJECTS) $(helmetsrv_DEPENDENCIES) $(EXTRA_helmetsrv_DEPENDENCIES)
|
||||
@rm -f helmetsrv$(EXEEXT)
|
||||
$(AM_V_CXXLD)$(CXXLINK) $(helmetsrv_OBJECTS) $(helmetsrv_LDADD) $(LIBS)
|
||||
|
||||
mostlyclean-compile:
|
||||
-rm -f *.$(OBJEXT)
|
||||
@@ -410,6 +427,12 @@ mostlyclean-compile:
|
||||
distclean-compile:
|
||||
-rm -f *.tab.c
|
||||
|
||||
.cc.o:
|
||||
$(AM_V_CXX)$(CXXCOMPILE) -c -o $@ $<
|
||||
|
||||
.cc.obj:
|
||||
$(AM_V_CXX)$(CXXCOMPILE) -c -o $@ `$(CYGPATH_W) '$<'`
|
||||
|
||||
.cpp.o:
|
||||
$(AM_V_CXX)$(CXXCOMPILE) -c -o $@ $<
|
||||
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
|
||||
#include <expected>
|
||||
#include <string>
|
||||
#include <cstring>
|
||||
|
||||
#include <uxlogger.hpp>
|
||||
#include <uxclient.hpp>
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
UxClient client;
|
||||
auto connectRes = client.Connect("127.0.0.1", 1025);
|
||||
if (!connectRes) {
|
||||
uxlogger.Log(connectRes.error());
|
||||
return 1;
|
||||
}
|
||||
auto runRes = client.Run();
|
||||
if (!runRes) {
|
||||
uxlogger.Log(runRes.error());
|
||||
return 1;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -6,9 +6,6 @@
|
||||
#include <tservice.hpp>
|
||||
#include <uxlogger.hpp>
|
||||
|
||||
|
||||
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
TCPService service(1025);
|
||||
auto bindRes = service.Bind();
|
||||
+387
@@ -0,0 +1,387 @@
|
||||
|
||||
extern "C" {
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <net/if.h>
|
||||
#include <linux/if.h>
|
||||
#include <linux/if_tun.h>
|
||||
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
|
||||
#include <arpa/inet.h>
|
||||
#include <linux/netlink.h>
|
||||
#include <linux/rtnetlink.h>
|
||||
}
|
||||
|
||||
#include <cstring>
|
||||
#include <expected>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <span>
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
#include <chrono>
|
||||
|
||||
#include <interface.hpp>
|
||||
|
||||
std::expected<void, std::string> Interface::Create(const std::string name) {
|
||||
if ((tunfd = open("/dev/net/tun", O_RDWR | O_CLOEXEC)) < 0) {
|
||||
int errnocopy = errno;
|
||||
std::string error = std::strerror(errnocopy);
|
||||
return std::unexpected("Create interface error: " + error);
|
||||
}
|
||||
struct ifreq ifr;
|
||||
memset(&ifr, 0, sizeof(ifr));
|
||||
ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
|
||||
strncpy(ifr.ifr_name, name.c_str(), IFNAMSIZ - 1);
|
||||
if (ioctl(tunfd, TUNSETIFF, (void*)(&ifr)) < 0) {
|
||||
close(tunfd);
|
||||
int errnocopy = errno;
|
||||
std::string error = std::strerror(errnocopy);
|
||||
return std::unexpected("Create interface error: " + error);
|
||||
}
|
||||
ifname = ifr.ifr_name;
|
||||
#if 0
|
||||
if (ioctl(tunfd, TUNSETPERSIST, 0) < 0) {
|
||||
close(tunfd);
|
||||
int errnocopy = errno;
|
||||
std::string error = std::strerror(errnocopy);
|
||||
return std::unexpected("Create interface error: " + error);
|
||||
}
|
||||
#endif
|
||||
|
||||
//struct ifreq ifr;
|
||||
memset(&ifr, 0, sizeof(ifr));
|
||||
int sockfd = 0;
|
||||
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
|
||||
close(tunfd);
|
||||
int errnocopy = errno;
|
||||
std::string error = std::strerror(errnocopy);
|
||||
return std::unexpected("Get MTU error: " + error);
|
||||
}
|
||||
strncpy(ifr.ifr_name, ifname.c_str(), IFNAMSIZ - 1);
|
||||
if (ioctl(sockfd, SIOCGIFMTU, &ifr) < 0) {
|
||||
close(tunfd);
|
||||
close(sockfd);
|
||||
int errnocopy = errno;
|
||||
std::string error = std::strerror(errnocopy);
|
||||
return std::unexpected("Get MTU error: " + error);
|
||||
}
|
||||
close(sockfd);
|
||||
mtu = ifr.ifr_mtu;
|
||||
return {};
|
||||
}
|
||||
|
||||
std::expected<void, std::string> Interface::SetMTU(int ifmtu) {
|
||||
int sockfd = 0;
|
||||
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
|
||||
int errnocopy = errno;
|
||||
std::string error = std::strerror(errnocopy);
|
||||
return std::unexpected("Set MTU error: " + error);
|
||||
}
|
||||
struct ifreq ifr;
|
||||
memset(&ifr, 0, sizeof(ifr));
|
||||
strncpy(ifr.ifr_name, ifname.c_str(), IFNAMSIZ - 1);
|
||||
ifr.ifr_mtu = ifmtu;
|
||||
if (ioctl(sockfd, SIOCSIFMTU, &ifr) < 0) {
|
||||
close(sockfd);
|
||||
int errnocopy = errno;
|
||||
std::string error = std::strerror(errnocopy);
|
||||
return std::unexpected("Set MTU error: " + error);
|
||||
}
|
||||
close(sockfd);
|
||||
return {};
|
||||
}
|
||||
|
||||
std::expected<int, std::string> Interface::GetMTU(void) {
|
||||
struct ifreq ifr;
|
||||
memset(&ifr, 0, sizeof(ifr));
|
||||
int sockfd = 0;
|
||||
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
|
||||
int errnocopy = errno;
|
||||
std::string error = std::strerror(errnocopy);
|
||||
return std::unexpected("Get MTU error: " + error);
|
||||
}
|
||||
strncpy(ifr.ifr_name, ifname.c_str(), IFNAMSIZ - 1);
|
||||
if (ioctl(sockfd, SIOCGIFMTU, &ifr) < 0) {
|
||||
close(sockfd);
|
||||
int errnocopy = errno;
|
||||
std::string error = std::strerror(errnocopy);
|
||||
return std::unexpected("Get MTU error: " + error);
|
||||
}
|
||||
close(sockfd);
|
||||
return ifr.ifr_mtu;
|
||||
}
|
||||
|
||||
std::expected<std::string, std::string> Interface::GetIP4Address(void) {
|
||||
struct ifreq ifr;
|
||||
memset(&ifr, 0, sizeof(ifr));
|
||||
int sockfd = 0;
|
||||
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
|
||||
int errnocopy = errno;
|
||||
std::string error = std::strerror(errnocopy);
|
||||
return std::unexpected("Get MTU error: " + error);
|
||||
}
|
||||
ifr.ifr_addr.sa_family = AF_INET;
|
||||
strncpy(ifr.ifr_name, ifname.c_str(), IFNAMSIZ - 1);
|
||||
|
||||
if (ioctl(sockfd, SIOCGIFADDR, &ifr) < 0) {
|
||||
close(sockfd);
|
||||
int errnocopy = errno;
|
||||
std::string error = std::strerror(errnocopy);
|
||||
return std::unexpected("Get MTU error: " + error);
|
||||
}
|
||||
close(sockfd);
|
||||
|
||||
auto sinaddr = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr;
|
||||
std::string addr = inet_ntoa(sinaddr);
|
||||
return addr;
|
||||
}
|
||||
|
||||
std::expected<void, std::string> Interface::SetIP4Address(std::string ipaddr) {
|
||||
struct ifreq ifr;
|
||||
memset(&ifr, 0, sizeof(ifr));
|
||||
int sockfd = 0;
|
||||
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
|
||||
int errnocopy = errno;
|
||||
std::string error = std::strerror(errnocopy);
|
||||
return std::unexpected("Set address error: " + error);
|
||||
}
|
||||
ifr.ifr_addr.sa_family = AF_INET;
|
||||
strncpy(ifr.ifr_name, ifname.c_str(), IFNAMSIZ - 1);
|
||||
|
||||
struct sockaddr_in* addr = (struct sockaddr_in *)&ifr.ifr_addr;
|
||||
addr->sin_family = AF_INET;
|
||||
if (inet_pton(AF_INET, ipaddr.c_str(), &addr->sin_addr) <= 0) {
|
||||
close(sockfd);
|
||||
int errnocopy = errno;
|
||||
std::string error = std::strerror(errnocopy);
|
||||
return std::unexpected("Set address error: " + error);
|
||||
}
|
||||
if (ioctl(sockfd, SIOCSIFADDR, &ifr) < 0) {
|
||||
close(sockfd);
|
||||
int errnocopy = errno;
|
||||
std::string error = std::strerror(errnocopy);
|
||||
return std::unexpected("Set address error: " + error);
|
||||
}
|
||||
close(sockfd);
|
||||
return {};
|
||||
}
|
||||
|
||||
std::expected<void, std::string> Interface::SetIP4Netmask(int prefix) {
|
||||
if (prefix < 0 || prefix > 32) {
|
||||
return std::unexpected("Invalid prefix");
|
||||
}
|
||||
uint32_t mask = (prefix == 0) ? 0 : (~0U << (32 - prefix));
|
||||
|
||||
struct in_addr maskaddr;
|
||||
maskaddr.s_addr = htonl(mask);
|
||||
char buffer[INET_ADDRSTRLEN];
|
||||
if (inet_ntop(AF_INET, &maskaddr, buffer, sizeof(buffer)) == NULL) {
|
||||
int errnocopy = errno;
|
||||
std::string error = std::strerror(errnocopy);
|
||||
return std::unexpected("Set1 netmask error: " + error);
|
||||
}
|
||||
auto netmask = std::string(buffer);
|
||||
|
||||
struct ifreq ifr;
|
||||
memset(&ifr, 0, sizeof(ifr));
|
||||
int sockfd = 0;
|
||||
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
|
||||
int errnocopy = errno;
|
||||
std::string error = std::strerror(errnocopy);
|
||||
return std::unexpected("Set2 netmask error: " + error);
|
||||
}
|
||||
ifr.ifr_addr.sa_family = AF_INET;
|
||||
strncpy(ifr.ifr_name, ifname.c_str(), IFNAMSIZ - 1);
|
||||
|
||||
struct sockaddr_in* addr = (struct sockaddr_in *)&ifr.ifr_addr;
|
||||
addr->sin_family = AF_INET;
|
||||
if (inet_pton(AF_INET, netmask.c_str(), &addr->sin_addr) < 0) {
|
||||
close(sockfd);
|
||||
int errnocopy = errno;
|
||||
std::string error = std::strerror(errnocopy);
|
||||
return std::unexpected("Set3 netmask error: " + error);
|
||||
}
|
||||
if (ioctl(sockfd, SIOCSIFNETMASK, &ifr) < 0) {
|
||||
close(sockfd);
|
||||
int errnocopy = errno;
|
||||
std::string error = std::strerror(errnocopy);
|
||||
return std::unexpected("Set4 netmask error: " + error);
|
||||
}
|
||||
close(sockfd);
|
||||
return {};
|
||||
}
|
||||
|
||||
std::expected<void, std::string> Interface::Up(void) {
|
||||
int sockfd = 0;
|
||||
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
|
||||
int errnocopy = errno;
|
||||
std::string error = std::strerror(errnocopy);
|
||||
return std::unexpected("Up interface error: " + error);
|
||||
}
|
||||
|
||||
struct ifreq ifr;
|
||||
memset(&ifr, 0, sizeof(ifr));
|
||||
strncpy(ifr.ifr_name, ifname.c_str(), IFNAMSIZ - 1);
|
||||
if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) < 0) {
|
||||
close(sockfd);
|
||||
int errnocopy = errno;
|
||||
std::string error = std::strerror(errnocopy);
|
||||
return std::unexpected("Up interface error: " + error);
|
||||
}
|
||||
ifr.ifr_flags |= (IFF_UP | IFF_RUNNING);
|
||||
if (ioctl(sockfd, SIOCSIFFLAGS, &ifr) < 0) {
|
||||
close(sockfd);
|
||||
int errnocopy = errno;
|
||||
std::string error = std::strerror(errnocopy);
|
||||
return std::unexpected("Up interface error: " + error);
|
||||
}
|
||||
close(sockfd);
|
||||
return {};
|
||||
}
|
||||
|
||||
std::expected<void, std::string> Interface::Down(void) {
|
||||
int sockfd = 0;
|
||||
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
|
||||
int errnocopy = errno;
|
||||
std::string error = std::strerror(errnocopy);
|
||||
return std::unexpected("Up interface error: " + error);
|
||||
}
|
||||
struct ifreq ifr;
|
||||
memset(&ifr, 0, sizeof(ifr));
|
||||
strncpy(ifr.ifr_name, ifname.c_str(), IFNAMSIZ - 1);
|
||||
if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) < 0) {
|
||||
close(sockfd);
|
||||
int errnocopy = errno;
|
||||
std::string error = std::strerror(errnocopy);
|
||||
return std::unexpected("Up interface error: " + error);
|
||||
}
|
||||
ifr.ifr_flags &= ~IFF_UP;
|
||||
if (ioctl(sockfd, SIOCSIFFLAGS, &ifr) < 0) {
|
||||
close(sockfd);
|
||||
int errnocopy = errno;
|
||||
std::string error = std::strerror(errnocopy);
|
||||
return std::unexpected("Up interface error: " + error);
|
||||
}
|
||||
close(sockfd);
|
||||
return {};
|
||||
}
|
||||
|
||||
std::expected<std::string, std::string> Interface::Read() {
|
||||
char buffer[mtu];
|
||||
int rsize = 0;
|
||||
if ((rsize = read(tunfd, buffer, sizeof(buffer))) < 0) {
|
||||
int errnocopy = errno;
|
||||
std::string error = std::strerror(errnocopy);
|
||||
return std::unexpected("Read interface error: " + error);
|
||||
}
|
||||
std::string rdata;
|
||||
rdata.append(buffer, rsize);
|
||||
return rdata;
|
||||
}
|
||||
|
||||
std::string Interface::Name() {
|
||||
return ifname;
|
||||
}
|
||||
|
||||
int Interface::MTU() {
|
||||
return mtu;
|
||||
}
|
||||
|
||||
Interface::~Interface() {
|
||||
close(tunfd);
|
||||
}
|
||||
|
||||
|
||||
std::expected<void, std::string> Interface::UpN(void) {
|
||||
int netlinkfd = 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;
|
||||
|
||||
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) {
|
||||
close(netlinkfd);
|
||||
int errnocopy = errno;
|
||||
std::string error = std::strerror(errnocopy);
|
||||
return std::unexpected("Up interface error: " + error);
|
||||
}
|
||||
close(netlinkfd);
|
||||
return {};
|
||||
}
|
||||
|
||||
std::expected<void, std::string> Interface::SetIP4AddrMask(const std::string address, const int prefix) {
|
||||
int netlinkfd = 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);
|
||||
}
|
||||
|
||||
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) {
|
||||
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 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;
|
||||
|
||||
/* 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));
|
||||
|
||||
if (send(netlinkfd, &request, request.header.nlmsg_len, 0) < 0) {
|
||||
close(netlinkfd);
|
||||
int errnocopy = errno;
|
||||
std::string error = std::strerror(errnocopy);
|
||||
return std::unexpected("Set interface address error: " + error);
|
||||
}
|
||||
close(netlinkfd);
|
||||
return {};
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
|
||||
#ifndef INTERFACE_HPP
|
||||
#define INTERFACE_HPP
|
||||
|
||||
class Interface {
|
||||
private:
|
||||
int tunfd;
|
||||
std::string ifname;
|
||||
int mtu;
|
||||
public:
|
||||
std::expected<void, std::string> Create(const std::string name);
|
||||
std::string Name(void);
|
||||
//int TunDescr(void);
|
||||
|
||||
int MTU();
|
||||
std::expected<void, std::string> SetMTU(int mtu);
|
||||
std::expected<int, std::string> GetMTU(void);
|
||||
std::expected<void, std::string> SetIP4Address(std::string ipaddr);
|
||||
std::expected<void, std::string> SetIP4Netmask(int netmask);
|
||||
std::expected<std::string, std::string> GetIP4Address(void);
|
||||
|
||||
std::expected<void, std::string> Up(void);
|
||||
std::expected<void, std::string> Down(void);
|
||||
std::expected<void, std::string> UpN(void);
|
||||
std::expected<void, std::string> SetIP4AddrMask(const std::string address, const int prefix);
|
||||
|
||||
std::expected<std::string, std::string> Read(void);
|
||||
|
||||
~Interface();
|
||||
};
|
||||
|
||||
#endif
|
||||
+68
-45
@@ -1,4 +1,8 @@
|
||||
|
||||
extern "C" {
|
||||
#include <sys/socket.h>
|
||||
}
|
||||
|
||||
#include <expected>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
@@ -6,52 +10,43 @@
|
||||
#include <chrono>
|
||||
#include <cstring>
|
||||
#include <semaphore>
|
||||
#include <format>
|
||||
|
||||
#include <msgheader.hpp>
|
||||
#include <sockhand.hpp>
|
||||
#include <uxlogger.hpp>
|
||||
|
||||
//#include <uxcontrol.pb.h>
|
||||
|
||||
#include <uxcontrol.pb.h>
|
||||
|
||||
using namespace std::chrono_literals;
|
||||
|
||||
void SocketHandler::RecvMessages(void) {
|
||||
while (true) {
|
||||
uxlogger.Log("Handle socket");
|
||||
int rsize = 0;
|
||||
std::string rawHeader(msgHeaderSize, 0);
|
||||
{
|
||||
|
||||
if ((rsize = read(sock, rawHeader.data(), rawHeader.size())) < 0) {
|
||||
int errnoCopy = errno;
|
||||
std::string error = std::strerror(errnoCopy);
|
||||
uxlogger.Log(std::format("Read header error: {}", error));
|
||||
break;
|
||||
void SocketHandler::Handle(int newsock) {
|
||||
sock = newsock;
|
||||
|
||||
auto createRes = interface.Create(std::format("uxsrv{}", sock));
|
||||
if (!createRes) {
|
||||
uxlogger.Log(createRes.error());
|
||||
return;
|
||||
}
|
||||
auto upRes = interface.Up();
|
||||
if (!createRes) {
|
||||
uxlogger.Log(createRes.error());
|
||||
return;
|
||||
}
|
||||
if (rsize < msgHeaderSize) {
|
||||
uxlogger.Log(std::format("Read only {} from {} bytes", rsize, msgHeaderSize));
|
||||
break;
|
||||
}
|
||||
MessageHeader header;
|
||||
auto decodeRes = header.Decode(rawHeader);
|
||||
if (!decodeRes) {
|
||||
uxlogger.Log(std::format("Decode header error: {}", decodeRes.error()));
|
||||
}
|
||||
std::string rawMessage(header.PacketSize(), 0);
|
||||
{
|
||||
if ((rsize = read(sock, rawMessage.data(), rawMessage.size())) < 0) {
|
||||
int errnoCopy = errno;
|
||||
std::string error = std::strerror(errnoCopy);
|
||||
uxlogger.Log(std::format("Read message error: {}", error));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
done.release();
|
||||
uxlogger.Log("RecvMessages done");
|
||||
|
||||
std::thread sendThr(&SocketHandler::SendMessages, this);
|
||||
sendThr.detach();
|
||||
|
||||
std::thread recvThr(&SocketHandler::RecvMessages, this);
|
||||
recvThr.detach();
|
||||
|
||||
done.acquire();
|
||||
uxlogger.Log("Handler done");
|
||||
}
|
||||
|
||||
|
||||
void SocketHandler::SendMessages(void) {
|
||||
while (true) {
|
||||
MessageHeader header(0);
|
||||
@@ -68,20 +63,48 @@ void SocketHandler::SendMessages(void) {
|
||||
std::this_thread::sleep_for(std::chrono::seconds(1));
|
||||
}
|
||||
done.release();
|
||||
uxlogger.Log("SendMessages done");
|
||||
uxlogger.Log("Send messages done");
|
||||
}
|
||||
|
||||
void SocketHandler::Handle(int newsock) {
|
||||
sock = newsock;
|
||||
|
||||
std::thread sendThr(&SocketHandler::SendMessages, this);
|
||||
sendThr.detach();
|
||||
|
||||
std::thread recvThr(&SocketHandler::RecvMessages, this);
|
||||
recvThr.detach();
|
||||
|
||||
done.acquire();
|
||||
uxlogger.Log("Handler done");
|
||||
void SocketHandler::RecvMessages(void) {
|
||||
while (true) {
|
||||
int rsize = 0;
|
||||
std::string rawHeader(msgHeaderSize, 0);
|
||||
if ((rsize = recv(sock, rawHeader.data(), rawHeader.size(), MSG_WAITALL)) < 0) {
|
||||
int errnoCopy = errno;
|
||||
std::string error = std::strerror(errnoCopy);
|
||||
uxlogger.Log(std::format("Read header error: {}", error));
|
||||
break;
|
||||
}
|
||||
if (rsize < msgHeaderSize) {
|
||||
uxlogger.Log(std::format("Read only {} from {} bytes", rsize, msgHeaderSize));
|
||||
break;
|
||||
}
|
||||
uxlogger.Log(std::format("Receive header rsize {}", rsize));
|
||||
|
||||
MessageHeader header;
|
||||
auto decodeRes = header.Decode(rawHeader);
|
||||
if (!decodeRes) {
|
||||
uxlogger.Log(std::format("Decode header error: {}", decodeRes.error()));
|
||||
break;
|
||||
}
|
||||
auto pSize = header.PacketSize();
|
||||
uxlogger.Log(std::format("Message size {}", pSize));
|
||||
|
||||
if (pSize > 0) {
|
||||
std::string rawMessage(pSize, 0);
|
||||
if ((rsize = recv(sock, rawMessage.data(), rawMessage.size(), MSG_WAITALL)) < 0) {
|
||||
int errnoCopy = errno;
|
||||
std::string error = std::strerror(errnoCopy);
|
||||
uxlogger.Log(std::format("Read message error: {}", error));
|
||||
break;
|
||||
}
|
||||
uxlogger.Log(std::format("Receive message rsize {}", rsize));
|
||||
uxcontrol::Message msg;
|
||||
msg.ParseFromString(rawMessage);
|
||||
uxlogger.Log(std::format("Receive message, kind {}", msg.meta().kind()));
|
||||
}
|
||||
}
|
||||
done.release();
|
||||
uxlogger.Log("Recv messages done");
|
||||
}
|
||||
|
||||
+4
-1
@@ -7,11 +7,14 @@
|
||||
#include <string>
|
||||
#include <mutex>
|
||||
|
||||
#include <interface.hpp>
|
||||
|
||||
class SocketHandler {
|
||||
private:
|
||||
int sock;
|
||||
std::mutex sockMtx;
|
||||
std::mutex intMtx;
|
||||
std::binary_semaphore done{0};
|
||||
Interface interface;
|
||||
public:
|
||||
void Handle(int newsock);
|
||||
void RecvMessages(void);
|
||||
|
||||
@@ -1,96 +0,0 @@
|
||||
extern "C" {
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
}
|
||||
|
||||
#include <expected>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <span>
|
||||
#include <iostream>
|
||||
|
||||
#include <cstring>
|
||||
#include <expected>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
|
||||
|
||||
#include <tcpclient.hpp>
|
||||
|
||||
TCPClient::TCPClient() {
|
||||
sock = 0;
|
||||
family = 0;
|
||||
}
|
||||
|
||||
std::expected<void, std::string> TCPClient::Connect(const std::string naddress, const int port) {
|
||||
|
||||
struct sockaddr_in sa;
|
||||
if (inet_pton(AF_INET, naddress.c_str(), &(sa.sin_addr)) == 1) {
|
||||
family = AF_INET;
|
||||
} else if (inet_pton(AF_INET6, naddress.c_str(), &(sa.sin_addr)) == 1) {
|
||||
family = AF_INET6;
|
||||
} else {
|
||||
int errnocopy = errno;
|
||||
std::string error = std::strerror(errnocopy);
|
||||
return std::unexpected("Incorrect address " + naddress);
|
||||
}
|
||||
|
||||
sock = socket(family, SOCK_STREAM, 0);
|
||||
if (sock < 0) {
|
||||
return std::unexpected("Error opening socket");
|
||||
}
|
||||
struct sockaddr_in serv_addr;
|
||||
memset(&serv_addr, 0, sizeof(serv_addr));
|
||||
|
||||
serv_addr.sin_family = AF_INET;
|
||||
serv_addr.sin_port = htons(port);
|
||||
const char* addr = naddress.data();
|
||||
if (inet_pton(AF_INET, addr, &serv_addr.sin_addr) <= 0) {
|
||||
return std::unexpected("Invalid server IP address");
|
||||
}
|
||||
|
||||
struct timeval timeout;
|
||||
timeout.tv_sec = 5;
|
||||
timeout.tv_usec = 0;
|
||||
|
||||
if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof timeout) < 0) {
|
||||
return std::unexpected("Set timeout error");
|
||||
}
|
||||
if (setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof timeout) < 0) {
|
||||
return std::unexpected("Set timeout error");
|
||||
}
|
||||
if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
|
||||
return std::unexpected("Connecting error");
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
std::expected<int, std::string> TCPClient::Write(std::string payload) {
|
||||
int n = 0;
|
||||
if ((n = write(sock, payload.data(), payload.size())) < 0) {
|
||||
return std::unexpected("Write error");
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
std::expected<int, std::string> TCPClient::Read(std::string& res, int size) {
|
||||
char buffer[size + 1];
|
||||
memset(&buffer, 0, size + 1);
|
||||
int rsize = 0;
|
||||
if ((rsize = read(sock, &buffer, size)) < 0) {
|
||||
return std::unexpected("Read error");
|
||||
}
|
||||
res.append(buffer, rsize);
|
||||
return rsize;
|
||||
}
|
||||
|
||||
TCPClient::~TCPClient() {
|
||||
close(sock);
|
||||
}
|
||||
|
||||
@@ -1,20 +0,0 @@
|
||||
|
||||
#include <expected>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <span>
|
||||
#include <iostream>
|
||||
|
||||
#include <netclient.hpp>
|
||||
|
||||
class TCPClient : public NetClient {
|
||||
private:
|
||||
int sock;
|
||||
int family;
|
||||
public:
|
||||
TCPClient(void);
|
||||
std::expected<void, std::string> Connect(std::string address, const int port);
|
||||
std::expected<int, std::string> Write(std::string payload) override;
|
||||
std::expected<int, std::string> Read(std::string& buffer, int size) override;
|
||||
~TCPClient(void);
|
||||
};
|
||||
+200
@@ -0,0 +1,200 @@
|
||||
extern "C" {
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
}
|
||||
|
||||
#include <chrono>
|
||||
#include <cstring>
|
||||
#include <expected>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <format>
|
||||
|
||||
#include <uxclient.hpp>
|
||||
#include <msgheader.hpp>
|
||||
#include <uxlogger.hpp>
|
||||
|
||||
#include <uxcontrol.pb.h>
|
||||
|
||||
using namespace std::chrono_literals;
|
||||
|
||||
UxClient::UxClient() {
|
||||
sock = 0;
|
||||
family = 0;
|
||||
}
|
||||
|
||||
std::expected<void, std::string> UxClient::Connect(const std::string naddress, const int port) {
|
||||
|
||||
struct sockaddr_in sa;
|
||||
if (inet_pton(AF_INET, naddress.c_str(), &(sa.sin_addr)) == 1) {
|
||||
family = AF_INET;
|
||||
} else if (inet_pton(AF_INET6, naddress.c_str(), &(sa.sin_addr)) == 1) {
|
||||
family = AF_INET6;
|
||||
} else {
|
||||
int errnocopy = errno;
|
||||
std::string error = std::strerror(errnocopy);
|
||||
return std::unexpected("Incorrect address " + naddress);
|
||||
}
|
||||
|
||||
sock = socket(family, SOCK_STREAM, 0);
|
||||
if (sock < 0) {
|
||||
return std::unexpected("Error opening socket");
|
||||
}
|
||||
struct sockaddr_in serv_addr;
|
||||
memset(&serv_addr, 0, sizeof(serv_addr));
|
||||
|
||||
serv_addr.sin_family = AF_INET;
|
||||
serv_addr.sin_port = htons(port);
|
||||
const char* addr = naddress.data();
|
||||
if (inet_pton(AF_INET, addr, &serv_addr.sin_addr) <= 0) {
|
||||
return std::unexpected("Invalid server IP address");
|
||||
}
|
||||
|
||||
struct timeval timeout;
|
||||
timeout.tv_sec = 5;
|
||||
timeout.tv_usec = 0;
|
||||
|
||||
if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof timeout) < 0) {
|
||||
int errnoCopy = errno;
|
||||
std::string error = std::strerror(errnoCopy);
|
||||
return std::unexpected(std::format("Set timeout error: {}", error));
|
||||
}
|
||||
if (setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof timeout) < 0) {
|
||||
int errnoCopy = errno;
|
||||
std::string error = std::strerror(errnoCopy);
|
||||
return std::unexpected(std::format("Set send timeout error: {}", error));
|
||||
}
|
||||
if (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
|
||||
int errnoCopy = errno;
|
||||
std::string error = std::strerror(errnoCopy);
|
||||
return std::unexpected(std::format("Connect error: {}", error));
|
||||
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
void UxClient::RecvMessages(void) {
|
||||
while (true) {
|
||||
int rsize = 0;
|
||||
std::string rawHeader(msgHeaderSize, 0);
|
||||
if ((rsize = recv(sock, rawHeader.data(), rawHeader.size(), MSG_WAITALL)) < 0) {
|
||||
int errnoCopy = errno;
|
||||
std::string error = std::strerror(errnoCopy);
|
||||
uxlogger.Log(std::format("Read header error: {}", error));
|
||||
break;
|
||||
}
|
||||
if (rsize < msgHeaderSize) {
|
||||
uxlogger.Log(std::format("Read only {} from {} bytes", rsize, msgHeaderSize));
|
||||
break;
|
||||
}
|
||||
MessageHeader header;
|
||||
auto decodeRes = header.Decode(rawHeader);
|
||||
if (!decodeRes) {
|
||||
uxlogger.Log(std::format("Decode header error: {}", decodeRes.error()));
|
||||
}
|
||||
auto pSize = header.PacketSize();
|
||||
if (pSize > 0) {
|
||||
std::string rawMessage(pSize, 0);
|
||||
if ((rsize = recv(sock, rawMessage.data(), rawMessage.size(), MSG_WAITALL)) < 0) {
|
||||
int errnoCopy = errno;
|
||||
std::string error = std::strerror(errnoCopy);
|
||||
uxlogger.Log(std::format("Read message error: {}", error));
|
||||
break;
|
||||
}
|
||||
}
|
||||
uxlogger.Log(std::format("Receive message with size {}", pSize));
|
||||
}
|
||||
done.release();
|
||||
return;
|
||||
}
|
||||
|
||||
void UxClient::SendMessages(void) {
|
||||
while (true) {
|
||||
|
||||
auto readRes = interface.Read();
|
||||
if (!readRes) {
|
||||
uxlogger.Log(std::format("Read packet error: {}", readRes.error()));
|
||||
}
|
||||
auto packet = readRes.value();
|
||||
uxlogger.Log(std::format("Read packet size {}", packet.size()));
|
||||
|
||||
int wsize = 0;
|
||||
MessageHeader header(packet.size());
|
||||
auto rawHeader = header.Encode();
|
||||
if ((wsize = write(sock, rawHeader.data(), rawHeader.size())) < 0) {
|
||||
int errnoCopy = errno;
|
||||
std::string error = std::strerror(errnoCopy);
|
||||
uxlogger.Log(std::format("Write header error: {}", error));
|
||||
break;
|
||||
}
|
||||
uxlogger.Log(std::format("Write header wsize: {}", wsize));
|
||||
|
||||
uxcontrol::InternetPacket intPacket;
|
||||
auto meta = intPacket.mutable_meta();
|
||||
meta->set_kind("internetPacket");
|
||||
intPacket.set_payload(packet);
|
||||
|
||||
std::string rawMessage;
|
||||
intPacket.SerializeToString(&rawMessage);
|
||||
|
||||
if ((wsize = write(sock, rawMessage.data(), rawMessage.size())) < 0) {
|
||||
int errnoCopy = errno;
|
||||
std::string error = std::strerror(errnoCopy);
|
||||
uxlogger.Log(std::format("Write message error: {}", error));
|
||||
break;
|
||||
}
|
||||
uxlogger.Log("Send message");
|
||||
|
||||
//std::this_thread::sleep_for(std::chrono::seconds(1));
|
||||
}
|
||||
done.release();
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
std::expected<void, std::string> UxClient::Run(void) {
|
||||
|
||||
auto createRes = interface.Create("uxcli0");
|
||||
if (!createRes) {
|
||||
return std::unexpected(createRes.error());
|
||||
}
|
||||
auto setAddrRes = interface.SetIP4Address("10.1.1.2");
|
||||
if (!setAddrRes) {
|
||||
return std::unexpected(setAddrRes.error());
|
||||
}
|
||||
auto setMaskRes = interface.SetIP4Netmask(24);
|
||||
if (!setMaskRes) {
|
||||
return std::unexpected(setMaskRes.error());
|
||||
}
|
||||
auto setMTURes = interface.SetMTU(1280);
|
||||
if (!setMTURes) {
|
||||
return std::unexpected(setMTURes.error());
|
||||
}
|
||||
auto upRes = interface.Up();
|
||||
if (!createRes) {
|
||||
return std::unexpected(createRes.error());
|
||||
}
|
||||
std::thread sendThr(&UxClient::SendMessages, this);
|
||||
sendThr.detach();
|
||||
|
||||
std::thread recvThr(&UxClient::RecvMessages, this);
|
||||
recvThr.detach();
|
||||
|
||||
done.acquire();
|
||||
uxlogger.Log("Client done");
|
||||
return {};
|
||||
}
|
||||
|
||||
UxClient::~UxClient() {
|
||||
if (sock != 0) {
|
||||
close(sock);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
|
||||
#include <expected>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <span>
|
||||
#include <iostream>
|
||||
#include <semaphore>
|
||||
|
||||
#include <interface.hpp>
|
||||
|
||||
class UxClient {
|
||||
private:
|
||||
int family;
|
||||
int sock;
|
||||
std::binary_semaphore done{0};
|
||||
Interface interface;
|
||||
public:
|
||||
UxClient(void);
|
||||
std::expected<void, std::string> Connect(std::string address, const int port);
|
||||
void SendMessages(void);
|
||||
void RecvMessages(void);
|
||||
std::expected<void, std::string> Run(void);
|
||||
~UxClient(void);
|
||||
};
|
||||
+6
-11
@@ -269,7 +269,7 @@ const char descriptor_table_protodef_uxcontrol_2eproto[] PROTOBUF_SECTION_VARIAB
|
||||
"\014\n\004kind\030\001 \001(\t\"+\n\007Message\022 \n\004meta\030\001 \001(\0132\022"
|
||||
".uxcontrol.MsgMeta\"C\n\016InternetPacket\022 \n\004"
|
||||
"meta\030\001 \001(\0132\022.uxcontrol.MsgMeta\022\017\n\007payloa"
|
||||
"d\030\002 \001(\t\"=\n\010HelloMsg\022 \n\004meta\030\001 \001(\0132\022.uxco"
|
||||
"d\030\002 \001(\014\"=\n\010HelloMsg\022 \n\004meta\030\001 \001(\0132\022.uxco"
|
||||
"ntrol.MsgMeta\022\017\n\007message\030\002 \001(\t\"\027\n\007ReqMet"
|
||||
"a\022\014\n\004kind\030\001 \001(\t\")\n\007ResMeta\022\r\n\005error\030\001 \001("
|
||||
"\010\022\017\n\007message\030\002 \001(\t\"+\n\007Request\022 \n\004meta\030\001 "
|
||||
@@ -795,13 +795,12 @@ const char* InternetPacket::_InternalParse(const char* ptr, ::_pbi::ParseContext
|
||||
} else
|
||||
goto handle_unusual;
|
||||
continue;
|
||||
// string payload = 2;
|
||||
// bytes payload = 2;
|
||||
case 2:
|
||||
if (PROTOBUF_PREDICT_TRUE(static_cast<uint8_t>(tag) == 18)) {
|
||||
auto str = _internal_mutable_payload();
|
||||
ptr = ::_pbi::InlineGreedyStringParser(str, ptr, ctx);
|
||||
CHK_(ptr);
|
||||
CHK_(::_pbi::VerifyUTF8(str, "uxcontrol.InternetPacket.payload"));
|
||||
} else
|
||||
goto handle_unusual;
|
||||
continue;
|
||||
@@ -841,13 +840,9 @@ uint8_t* InternetPacket::_InternalSerialize(
|
||||
_Internal::meta(this).GetCachedSize(), target, stream);
|
||||
}
|
||||
|
||||
// string payload = 2;
|
||||
// bytes payload = 2;
|
||||
if (!this->_internal_payload().empty()) {
|
||||
::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::VerifyUtf8String(
|
||||
this->_internal_payload().data(), static_cast<int>(this->_internal_payload().length()),
|
||||
::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::SERIALIZE,
|
||||
"uxcontrol.InternetPacket.payload");
|
||||
target = stream->WriteStringMaybeAliased(
|
||||
target = stream->WriteBytesMaybeAliased(
|
||||
2, this->_internal_payload(), target);
|
||||
}
|
||||
|
||||
@@ -867,10 +862,10 @@ size_t InternetPacket::ByteSizeLong() const {
|
||||
// Prevent compiler warnings about cached_has_bits being unused
|
||||
(void) cached_has_bits;
|
||||
|
||||
// string payload = 2;
|
||||
// bytes payload = 2;
|
||||
if (!this->_internal_payload().empty()) {
|
||||
total_size += 1 +
|
||||
::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::StringSize(
|
||||
::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::BytesSize(
|
||||
this->_internal_payload());
|
||||
}
|
||||
|
||||
|
||||
+3
-3
@@ -526,7 +526,7 @@ class InternetPacket final :
|
||||
kPayloadFieldNumber = 2,
|
||||
kMetaFieldNumber = 1,
|
||||
};
|
||||
// string payload = 2;
|
||||
// bytes payload = 2;
|
||||
void clear_payload();
|
||||
const std::string& payload() const;
|
||||
template <typename ArgT0 = const std::string&, typename... ArgT>
|
||||
@@ -1972,7 +1972,7 @@ inline void InternetPacket::set_allocated_meta(::uxcontrol::MsgMeta* meta) {
|
||||
// @@protoc_insertion_point(field_set_allocated:uxcontrol.InternetPacket.meta)
|
||||
}
|
||||
|
||||
// string payload = 2;
|
||||
// bytes payload = 2;
|
||||
inline void InternetPacket::clear_payload() {
|
||||
_impl_.payload_.ClearToEmpty();
|
||||
}
|
||||
@@ -1984,7 +1984,7 @@ template <typename ArgT0, typename... ArgT>
|
||||
inline PROTOBUF_ALWAYS_INLINE
|
||||
void InternetPacket::set_payload(ArgT0&& arg0, ArgT... args) {
|
||||
|
||||
_impl_.payload_.Set(static_cast<ArgT0 &&>(arg0), args..., GetArenaForAllocation());
|
||||
_impl_.payload_.SetBytes(static_cast<ArgT0 &&>(arg0), args..., GetArenaForAllocation());
|
||||
// @@protoc_insertion_point(field_set:uxcontrol.InternetPacket.payload)
|
||||
}
|
||||
inline std::string* InternetPacket::mutable_payload() {
|
||||
|
||||
+1
-1
@@ -15,7 +15,7 @@ message Message {
|
||||
|
||||
message InternetPacket {
|
||||
MsgMeta meta = 1;
|
||||
string payload = 2;
|
||||
bytes payload = 2;
|
||||
}
|
||||
|
||||
message HelloMsg {
|
||||
|
||||
Reference in New Issue
Block a user