Commit 9941bd10 authored by nanamicat's avatar nanamicat

beautify

parent d43161dd
# Project exclude paths
cmake-build-debug
.idea
Dockerfile
\ No newline at end of file
# Project exclude paths
/cmake-build-debug/
/.idea/
\ No newline at end of file
cmake-build-debug
.idea
\ No newline at end of file
......@@ -8,5 +8,5 @@ add_executable(tun main.cpp)
find_package(Threads REQUIRED)
target_link_libraries(tun Threads::Threads)
#target_link_libraries(tun fmt)
target_link_libraries(tun -static-libgcc -static-libstdc++)
FROM alpine AS builder
RUN apk --no-cache add build-base cmake linux-headers
WORKDIR /usr/src/app
COPY . .
RUN cmake -DCMAKE_BUILD_TYPE=Release .
RUN make
FROM alpine
COPY --from=builder /usr/src/app/tun .
RUN ["./tun"]
\ No newline at end of file
./tun $1
\ No newline at end of file
......@@ -16,14 +16,14 @@ in_addr_t remote;
// internet -> tun
void inbound(int raw, int tun) {
char buf[ETH_DATA_LEN];
sockaddr_in addr{AF_INET};
socklen_t addrlen;
size_t nread;
while ((nread = recvfrom(raw, buf, sizeof(buf), 0, (sockaddr *) &addr, &addrlen)) >= 0) {
std::cout << "recv " << nread << " bytes from" << inet_ntoa(addr.sin_addr) << std::endl;
sockaddr_in address{.sin_family = AF_INET};
socklen_t address_length;
size_t packet_length;
while ((packet_length = recvfrom(raw, buf, sizeof(buf), 0, (sockaddr *) &address, &address_length)) >= 0) {
// std::cout << "received " << packet_length << " bytes from " << inet_ntoa(address.sin_addr) << std::endl;
auto *packet = (iphdr *) buf;
auto overhead = packet->ihl * 4;
if (write(tun, buf + overhead, nread - overhead) < 0) {
if (write(tun, buf + overhead, packet_length - overhead) < 0) {
perror("inbound write");
}
}
......@@ -33,11 +33,11 @@ void inbound(int raw, int tun) {
// tun -> internet
void outbound(int raw, int tun) {
char buf[ETH_DATA_LEN];
sockaddr_in addr{AF_INET};
addr.sin_addr.s_addr = remote;
size_t nread;
while ((nread = read(tun, buf, sizeof(buf))) >= 0) {
if (sendto(raw, buf, nread, 0, (sockaddr *) &addr, sizeof(addr)) < 0) {
sockaddr_in address{.sin_family = AF_INET};
address.sin_addr.s_addr = remote;
size_t packet_length;
while ((packet_length = read(tun, buf, sizeof(buf))) >= 0) {
if (sendto(raw, buf, packet_length, 0, (sockaddr *) &address, sizeof(address)) < 0) {
perror("outbound write");
}
}
......
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