Commit febc13c2 authored by nanamicat's avatar nanamicat

float ip

parent e57f20c7
......@@ -5,11 +5,11 @@ set(CMAKE_CXX_STANDARD 23)
add_executable(tun main.cpp)
target_link_libraries(tun -static)
target_link_libraries(tun)
find_package(Threads REQUIRED)
target_link_libraries(tun Threads::Threads)
set(Boost_USE_STATIC_LIBS ON)
find_package(Boost COMPONENTS program_options REQUIRED)
target_link_libraries(tun Boost::program_options)
#set(Boost_USE_STATIC_LIBS ON)
#find_package(Boost COMPONENTS program_options REQUIRED)
#target_link_libraries(tun Boost::program_options)
......@@ -3,10 +3,11 @@ RUN apk --no-cache add build-base cmake linux-headers boost-dev boost-static
WORKDIR /usr/src/app
COPY . .
RUN cmake -DCMAKE_BUILD_TYPE=Release .
RUN cmake -DCMAKE_BUILD_TYPE=Debug .
RUN make
FROM alpine
RUN apk --no-cache add libgcc libstdc++
COPY --from=builder /usr/src/app/tun .
ENTRYPOINT ["./tun"]
......@@ -60,7 +60,7 @@ void encrypt_package(unsigned char *buffer, size_t length) {
void inbound(int raw, int tun) {
unsigned char buffer[ETH_DATA_LEN];
sockaddr_in address{.sin_family = AF_INET};
socklen_t address_length;
socklen_t address_length = sizeof(address);
size_t packet_length;
while ((packet_length = recvfrom(raw, buffer, sizeof(buffer), 0, (sockaddr *) &address, &address_length)) >= 0) {
auto *packet = (iphdr *) buffer;
......@@ -77,6 +77,9 @@ void inbound(int raw, int tun) {
// << " tot_len " << ntohs(packet->tot_len)
// << " inner->tot_len " << ntohs(inner->tot_len)
// << " from " << inet_ntoa(address.sin_addr) << std::endl;
if (remote_addr.sin_addr.s_addr != address.sin_addr.s_addr) {
std::cout << "float ip: " << inet_ntoa(address.sin_addr) << std::endl;
}
remote_addr = address;
if (write(tun, inner, payload_length) < 0) {
......@@ -95,7 +98,8 @@ void outbound(int raw, int tun) {
meta->reversed = 0;
auto inner = buffer + sizeof(Meta);
size_t packet_length;
while ((packet_length = read(tun, inner, sizeof(buffer) - sizeof(Meta))) >= 0) {
while ((packet_length = read(tun, inner, sizeof(buffer) + sizeof(Meta))) >= 0) {
// std::cout << "sendto: " << inet_ntoa(remote_addr.sin_addr) << std::endl;
if (!remote_addr.sin_addr.s_addr) continue;
encrypt_package(inner, packet_length);
if (sendto(raw, buffer, packet_length + sizeof(Meta), 0, (sockaddr *) &remote_addr, sizeof(remote_addr)) < 0) {
......@@ -116,7 +120,15 @@ int main(int argc, char *argv[]) {
auto up = getenv("up");
if (endpoint != nullptr) {
remote_addr.sin_addr.s_addr = ((in_addr *) gethostbyname(endpoint)->h_name)->s_addr;
auto he = gethostbyname(endpoint);
if (he == nullptr) {
perror("gethostbyname");
return -1;
}
if (he->h_addr_list[0] == nullptr) {
perror("gethostbyname 2");
}
remote_addr.sin_addr.s_addr = *(in_addr_t *) he->h_addr_list[0];
}
secret_length = strlen(secret);
......@@ -135,6 +147,7 @@ int main(int argc, char *argv[]) {
perror("tun init error");
return -1;
}
std::cout << dev << std::endl;
if (ioctl(tun, TUNSETIFF, &ifr) < 0) {
perror("ioctl error");
......
Markdown is supported
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