void cleanup_handler(int sig) {
(void)sig; // Explicitly mark as unused to silence warning
running = false;
}
void print_banner(void) {
printf("----------------------------------------------------------------\n");
printf("Black Ice MTU v%s - Network Optimization Tool\n", VERSION);
printf("----------------------------------------------------------------\n");
printf("\"Break through network restrictions by optimizing MTU\"\n\n");
}
void print_usage(void) {
printf("Usage: bim [options]\n");
printf("Options:\n");
printf(" -custom <mtu> Set specific MTU value (%d-%d)\n", MIN_MTU, MAX_MTU);
printf(" -iface <name> Specify interface name\n");
printf(" -h Show this help message\n");
}
// Parse command line arguments
for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], "-h") == 0) {
print_usage();
return EXIT_SUCCESS;
}
else if (strcmp(argv[i], "-custom") == 0 && i + 1 < argc) {
custom_mtu = atoi(argv[++i]);
custom_mode = true;
}
else if (strcmp(argv[i], "-iface") == 0 && i + 1 < argc) {
strncpy(vpn_iface, argv[++i], MAX_IFACE_LEN - 1);
vpn_iface[MAX_IFACE_LEN - 1] = '\0';
}
else {
print_usage();
return EXIT_FAILURE;
}
}
// Validate custom MTU if specified
if (custom_mode) {
if (custom_mtu < MIN_MTU || custom_mtu > MAX_MTU) {
log_message("Invalid MTU value: %d (must be %d-%d)", custom_mtu, MIN_MTU, MAX_MTU);
return EXIT_FAILURE;
}
}
// Detect interface if not specified
if (vpn_iface[0] == '\0') {
printf("Scanning for VPN interface...\n");
if (!detect_vpn_interface(vpn_iface, sizeof(vpn_iface))) {
log_message("No VPN interface detected");
return EXIT_FAILURE;
}
}
printf("Using interface: %s\n", vpn_iface);
int current_mtu = get_current_mtu(vpn_iface);
if (current_mtu <= 0) {
log_message("Could not retrieve MTU for %s", vpn_iface);
return EXIT_FAILURE;
}
printf("Current MTU: %d\n", current_mtu);
if (custom_mode) {
printf("Setting custom MTU: %d\n", custom_mtu);
int retries = 0;
while (retries < MAX_RETRIES && running) {
if (set_mtu(vpn_iface, custom_mtu)) {
if (ping_test(custom_mtu)) {
printf("Successfully set and verified MTU %d\n", custom_mtu);
return EXIT_SUCCESS;
}
log_message("MTU %d set but ping test failed", custom_mtu);
}
retries++;
if (retries < MAX_RETRIES) {
sleep(1);
printf("Retrying (%d/%d)...\n", retries + 1, MAX_RETRIES);
}
}
log_message("Failed to set MTU %d after %d retries", custom_mtu, MAX_RETRIES);
return EXIT_FAILURE;
}
// Auto-optimization mode
printf("Optimizing MTU (testing from %d to %d)...\n", current_mtu, MIN_MTU);
for (int mtu = current_mtu; mtu >= MIN_MTU && running; mtu -= MTU_STEP) {
printf("Testing MTU %d: ", mtu);
fflush(stdout);
if (success) {
printf("Success\n");
retries = 0;
while (retries < MAX_RETRIES && running) {
if (set_mtu(vpn_iface, mtu)) {
printf("Successfully set MTU to %d\n", mtu);
return EXIT_SUCCESS;
}
retries++;
if (retries < MAX_RETRIES) {
sleep(1);
printf("Retrying set (%d/%d)...\n", retries + 1, MAX_RETRIES);
}
}
log_message("Failed to set MTU %d after %d retries", mtu, MAX_RETRIES);
return EXIT_FAILURE;
}
printf("Failed\n");
}
log_message("No working MTU found above %d", MIN_MTU);
return EXIT_FAILURE;
}