Commit 057e78a1 authored by nanamicat's avatar nanamicat

make buffer unsigned

parent d8d45793
#include <iostream> #include <iostream>
#include <thread> #include <thread>
#include <sys/socket.h> #include <sys/socket.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <fcntl.h> #include <fcntl.h>
...@@ -10,37 +9,35 @@ ...@@ -10,37 +9,35 @@
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <unistd.h> #include <unistd.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <boost/program_options.hpp> #include <boost/program_options.hpp>
namespace po = boost::program_options; namespace po = boost::program_options;
in_addr_t remote; in_addr_t remote;
auto cipher = (unsigned char *) "Mathematics is the art of giving the same name to different things";
auto cipher = "Mathematics is the art of giving the same name to different things"; void encrypt(unsigned char *buffer, size_t length) {
void encrypt(char *buffer, size_t length) {
for (auto i = 0; i < length; i++) { for (auto i = 0; i < length; i++) {
buffer[i] ^= cipher[i]; buffer[i] ^= cipher[i];
} }
} }
void decrypt(char *buffer, size_t length) { void decrypt(unsigned char *buffer, size_t length) {
// xor decrypt is same as encrypt
encrypt(buffer, length); encrypt(buffer, length);
} }
// internet -> tun // internet -> tun
void inbound(int raw, int tun) { void inbound(int raw, int tun) {
char buf[ETH_DATA_LEN]; unsigned char buffer[ETH_DATA_LEN];
sockaddr_in address{.sin_family = AF_INET}; sockaddr_in address{.sin_family = AF_INET};
socklen_t address_length; socklen_t address_length;
size_t packet_length; size_t packet_length;
while ((packet_length = recvfrom(raw, buf, sizeof(buf), 0, (sockaddr *) &address, &address_length)) >= 0) { while ((packet_length = recvfrom(raw, buffer, sizeof(buffer), 0, (sockaddr *) &address, &address_length)) >= 0) {
// std::cout << "received " << packet_length << " bytes from " << inet_ntoa(address.sin_addr) << std::endl; // std::cout << "received " << packet_length << " bytes from " << inet_ntoa(address.sin_addr) << std::endl;
auto *packet = (iphdr *) buf; auto *packet = (iphdr *) buffer;
auto overhead = packet->ihl * 4; auto overhead = packet->ihl * 4;
auto payload = buf + overhead; auto payload = buffer + overhead;
auto payload_length = packet_length - overhead; auto payload_length = packet_length - overhead;
decrypt(payload, payload_length); decrypt(payload, payload_length);
if (write(tun, payload, payload_length) < 0) { if (write(tun, payload, payload_length) < 0) {
...@@ -52,13 +49,13 @@ void inbound(int raw, int tun) { ...@@ -52,13 +49,13 @@ void inbound(int raw, int tun) {
// tun -> internet // tun -> internet
void outbound(int raw, int tun) { void outbound(int raw, int tun) {
char buf[ETH_DATA_LEN]; unsigned char buffer[ETH_DATA_LEN];
sockaddr_in address{.sin_family = AF_INET}; sockaddr_in address{.sin_family = AF_INET};
address.sin_addr.s_addr = remote; address.sin_addr.s_addr = remote;
size_t packet_length; size_t packet_length;
while ((packet_length = read(tun, buf, sizeof(buf))) >= 0) { while ((packet_length = read(tun, buffer, sizeof(buffer))) >= 0) {
encrypt(buf, packet_length); encrypt(buffer, packet_length);
if (sendto(raw, buf, packet_length, 0, (sockaddr *) &address, sizeof(address)) < 0) { if (sendto(raw, buffer, packet_length, 0, (sockaddr *) &address, sizeof(address)) < 0) {
perror("outbound write"); 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