Skip to content

Commit

Permalink
添加 MTU 配置项
Browse files Browse the repository at this point in the history
  • Loading branch information
lanthora committed Aug 7, 2024
1 parent 047139e commit ec5993b
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.18.4)

project(candy LANGUAGES C CXX VERSION 5.10.2)
project(candy LANGUAGES C CXX VERSION 5.10.3)

option(CANDY_NOEXE "Don't build executable")
option(CANDY_DEVEL "Build development library")
Expand Down
3 changes: 3 additions & 0 deletions candy.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,6 @@ route = 5
# [Optional] Number of worker threads
# Default is 0 means no independent worker threads are enabled
#workers=0

# [Optional] Maximum Transmission Unit
#mtu=1400
5 changes: 5 additions & 0 deletions src/cffi/candy.cc
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ int candy_client_set_route_cost(void *candy, int cost) {
return c->setRouteCost(cost);
}

int candy_client_set_mtu(void *candy, int mtu) {
Candy::Client *c = static_cast<Candy::Client *>(candy);
return c->setMtu(mtu);
}

int candy_client_set_address_update_callback(void *candy, void (*callback)(const char *, const char *)) {
Candy::Client *c = static_cast<Candy::Client *>(candy);
return c->setAddressUpdateCallback([=](const std::string &address) {
Expand Down
1 change: 1 addition & 0 deletions src/cffi/candy.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ int candy_client_set_virtual_mac(void *candy, const char *vmac);
int candy_client_set_stun(void *candy, const char *stun);
int candy_client_set_discovery_interval(void *candy, int interval);
int candy_client_set_route_cost(void *candy, int cost);
int candy_client_set_mtu(void *candy, int mtu);
int candy_client_set_udp_bind_port(void *candy, int port);
int candy_client_set_localhost(void *candy, const char *ip);
int candy_client_set_address_update_callback(void *candy, void (*callback)(const char *, const char *));
Expand Down
7 changes: 6 additions & 1 deletion src/core/client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ int Client::setLocalhost(std::string ip) {
return 0;
}

int Client::setMtu(int mtu) {
this->mtu = mtu;
return 0;
}

int Client::run() {
std::lock_guard lock(this->runningMutex);
this->running = true;
Expand Down Expand Up @@ -778,7 +783,7 @@ int Client::startTunThread() {
if (this->tun.setAddress(this->realAddress)) {
return -1;
}
if (this->tun.setMTU(1400)) {
if (this->tun.setMTU(this->mtu)) {
return -1;
}
if (this->tun.setTimeout(1)) {
Expand Down
4 changes: 4 additions & 0 deletions src/core/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,17 @@ class Client {
// 用于局域网连接的地址
int setLocalhost(std::string ip);

// 设置最大传输单元
int setMtu(int mtu);

// 启停客户端用于处理任务的线程
int run();
int shutdown();

private:
// Common
int workers = 0;
int mtu = 1400;
bool running = false;
std::string password;
std::mutex runningMutex;
Expand Down
5 changes: 5 additions & 0 deletions src/main/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ struct arguments {
int udpPort = 0;
int discovery = 0;
int routeCost = 0;
int mtu = 1400;
};

int disableLogTimestamp() {
Expand Down Expand Up @@ -92,6 +93,7 @@ void parseConfig(std::string cfgFile, arguments &args) {
{"discovery", [&](const std::string &value) { args.discovery = std::stoi(value); }},
{"route", [&](const std::string &value) { args.routeCost = std::stoi(value); }},
{"port", [&](const std::string &value) { args.udpPort = std::stoi(value); }},
{"mtu", [&](const std::string &value) { args.mtu = std::stoi(value); }},
{"localhost", [&](const std::string &value) { args.localhost = value; }},
};
auto trim = [](std::string str) {
Expand Down Expand Up @@ -276,6 +278,7 @@ int serve(const arguments &args) {
client.setTunAddress(args.tun);
client.setExpectedAddress(getLastestAddress(args.name));
client.setVirtualMac(virtualMac(args.name));
client.setMtu(args.mtu);
client.setWorkers(args.workers);
client.setName(args.name);
client.run();
Expand Down Expand Up @@ -311,6 +314,7 @@ int parseConfig(int argc, char *argv[], arguments &args) {
program.add_argument("-t", "--tun").help("static address").metavar("CIDR");
program.add_argument("-s", "--stun").help("stun address").metavar("URI");
program.add_argument("-s", "--port").help("udp port").scan<'i', int>().metavar("NUMBER");
program.add_argument("--mtu").help("maximum transmission unit").scan<'i', int>().metavar("NUMBER");
program.add_argument("-r", "--route").help("routing cost").scan<'i', int>().metavar("COST");
program.add_argument("--discovery").help("discovery interval").scan<'i', int>().metavar("SECONDS");
program.add_argument("--localhost").help("local ip").metavar("IP");
Expand Down Expand Up @@ -338,6 +342,7 @@ int parseConfig(int argc, char *argv[], arguments &args) {
args.stun = program.is_used("--stun") ? program.get<std::string>("--stun") : args.stun;
args.localhost = program.is_used("--localhost") ? program.get<std::string>("--localhost") : args.localhost;
args.udpPort = program.is_used("--port") ? program.get<int>("--port") : args.udpPort;
args.mtu = program.is_used("--mtu") ? program.get<int>("--mtu") : args.mtu;
args.discovery = program.is_used("--discovery") ? program.get<int>("--discovery") : args.discovery;
args.routeCost = program.is_used("--route") ? program.get<int>("--route") : args.routeCost;

Expand Down

0 comments on commit ec5993b

Please sign in to comment.