add mertric option

This commit is contained in:
fujr 2024-10-24 17:59:20 +08:00
parent 179e912fff
commit eb4ee6de59
3 changed files with 34 additions and 21 deletions

View File

@ -194,6 +194,7 @@ typedef struct __PROFILE {
const char *password; const char *password;
int auth; int auth;
int iptype; int iptype;
const char *metric;
const char *pincode; const char *pincode;
char proxy[32]; char proxy[32];
int pdp;//pdp_context int pdp;//pdp_context
@ -247,7 +248,6 @@ typedef struct __PROFILE {
char old_password[64]; char old_password[64];
int old_auth; int old_auth;
int old_iptype; int old_iptype;
int metric;
const struct qmi_device_ops *qmi_ops; const struct qmi_device_ops *qmi_ops;
const struct request_ops *request_ops; const struct request_ops *request_ops;
@ -372,8 +372,8 @@ extern int debug_qmi;
extern int qmidevice_control_fd[2]; extern int qmidevice_control_fd[2];
extern int g_donot_exit_when_modem_hangup; extern int g_donot_exit_when_modem_hangup;
extern void update_resolv_conf(int iptype, const char *ifname, const char *dns1, const char *dns2); extern void update_resolv_conf(int iptype, const char *ifname, const char *dns1, const char *dns2);
void update_ipv4_address(const char *ifname, const char *ip, const char *gw, unsigned prefix); void update_ipv4_address(const char *ifname, const char *ip, const char *gw, unsigned prefix, char *metric);
void update_ipv6_address(const char *ifname, const char *ip, const char *gw, unsigned prefix); void update_ipv6_address(const char *ifname, const char *ip, const char *gw, unsigned prefix, char *metric);
int reattach_driver(PROFILE_T *profile); int reattach_driver(PROFILE_T *profile);
extern void no_trunc_strncpy(char *dest, const char *src, size_t dest_size); extern void no_trunc_strncpy(char *dest, const char *src, size_t dest_size);

View File

@ -251,6 +251,7 @@ static int usage(const char *progname) {
dbg_time("-b Enable network interface bridge function (default 0)"); dbg_time("-b Enable network interface bridge function (default 0)");
dbg_time("-v Verbose log mode, for debug purpose."); dbg_time("-v Verbose log mode, for debug purpose.");
dbg_time("-d Obtain the IP address and dns through qmi"); dbg_time("-d Obtain the IP address and dns through qmi");
dbg_time("-M metric Specify the metric of the default route");
dbg_time("[Examples]"); dbg_time("[Examples]");
dbg_time("Example 1: %s ", progname); dbg_time("Example 1: %s ", progname);
dbg_time("Example 2: %s -s 3gnet ", progname); dbg_time("Example 2: %s -s 3gnet ", progname);
@ -902,18 +903,30 @@ static int parse_user_input(int argc, char **argv, PROFILE_T *profile) {
profile->kill_pdp = argv[opt++][0] - '0'; profile->kill_pdp = argv[opt++][0] - '0';
} }
break; break;
case 't':
case 'M':
if (has_more_argv()) { if (has_more_argv()) {
profile->metric = atoi(argv[opt++]); const char *arg = argv[opt++];
if (atoi(arg) > 0) {
profile->metric = arg;
}
else {
dbg_time("unknow metric '%s'", arg);
return usage(argv[0]);
}
} }
break; break;
default: default:
return usage(argv[0]); return usage(argv[0]);
break; break;
} }
} }
if (!profile->metric){
profile->metric = "0";
}
if (profile->enable_ipv4 != 1 && profile->enable_ipv6 != 1) { // default enable IPv4 if (profile->enable_ipv4 != 1 && profile->enable_ipv6 != 1) { // default enable IPv4
profile->enable_ipv4 = 1; profile->enable_ipv4 = 1;
} }

View File

@ -248,7 +248,7 @@ static const char *ipv6Str(const UCHAR Address[16]) {
return str; return str;
} }
void update_ipv4_address(const char *ifname, const char *ip, const char *gw, unsigned prefix) void update_ipv4_address(const char *ifname, const char *ip, const char *gw, unsigned prefix, char *metric)
{ {
char shell_cmd[128]; char shell_cmd[128];
@ -263,7 +263,7 @@ void update_ipv4_address(const char *ifname, const char *ip, const char *gw, uns
ql_system(shell_cmd); ql_system(shell_cmd);
//ping6 www.qq.com //ping6 www.qq.com
snprintf(shell_cmd, sizeof(shell_cmd), "ip -%d route add default via %s dev %s", 4, gw, ifname); snprintf(shell_cmd, sizeof(shell_cmd), "ip -%d route add default via %s dev %s metric %s", 4, gw, ifname, metric);
ql_system(shell_cmd); ql_system(shell_cmd);
} else { } else {
unsigned n = (0xFFFFFFFF >> (32 - prefix)) << (32 - prefix); unsigned n = (0xFFFFFFFF >> (32 - prefix)) << (32 - prefix);
@ -273,15 +273,16 @@ void update_ipv4_address(const char *ifname, const char *ip, const char *gw, uns
ql_system(shell_cmd); ql_system(shell_cmd);
//Resetting default routes //Resetting default routes
snprintf(shell_cmd, sizeof(shell_cmd), "route del default dev %s", ifname); dbg_time("Resetting default routes");
//snprintf(shell_cmd, sizeof(shell_cmd), "route del default dev %s", ifname);
while(!system(shell_cmd)); while(!system(shell_cmd));
snprintf(shell_cmd, sizeof(shell_cmd), "route add default gw %s dev %s", gw, ifname); snprintf(shell_cmd, sizeof(shell_cmd), "route add default gw %s dev %s metric %s ", gw, ifname, metric);
ql_system(shell_cmd); ql_system(shell_cmd);
} }
} }
void update_ipv6_address(const char *ifname, const char *ip, const char *gw, unsigned prefix) { void update_ipv6_address(const char *ifname, const char *ip, const char *gw, unsigned prefix, char* metric) {
char shell_cmd[128]; char shell_cmd[128];
(void)gw; (void)gw;
@ -293,18 +294,18 @@ void update_ipv6_address(const char *ifname, const char *ip, const char *gw, uns
ql_system(shell_cmd); ql_system(shell_cmd);
//ping6 www.qq.com //ping6 www.qq.com
snprintf(shell_cmd, sizeof(shell_cmd), "ip -%d route add default dev %s", 6, ifname); snprintf(shell_cmd, sizeof(shell_cmd), "ip -%d route add default dev %s metric %s ", 6, ifname, metric);
ql_system(shell_cmd); ql_system(shell_cmd);
} else { } else {
snprintf(shell_cmd, sizeof(shell_cmd), "ifconfig %s %s/%d", ifname, ip, prefix); snprintf(shell_cmd, sizeof(shell_cmd), "ifconfig %s %s/%d", ifname, ip, prefix);
ql_system(shell_cmd); ql_system(shell_cmd);
snprintf(shell_cmd, sizeof(shell_cmd), "route -A inet6 add default dev %s", ifname); snprintf(shell_cmd, sizeof(shell_cmd), "route -A inet6 add default dev %s metric %s ", ifname ,metric);
ql_system(shell_cmd); ql_system(shell_cmd);
} }
} }
static void update_ip_address_by_qmi(const char *ifname, const IPV4_T *ipv4, const IPV6_T *ipv6) { static void update_ip_address_by_qmi(const char *ifname, const IPV4_T *ipv4, const IPV6_T *ipv6, char *m) {
char *d1, *d2; char *d1, *d2;
if (ipv4 && ipv4->Address) { if (ipv4 && ipv4->Address) {
@ -319,7 +320,7 @@ static void update_ip_address_by_qmi(const char *ifname, const IPV4_T *ipv4, con
} }
} }
update_ipv4_address(ifname, d1, d2, prefix); update_ipv4_address(ifname, d1, d2, prefix,m);
free(d1); free(d2); free(d1); free(d2);
//Adding DNS //Adding DNS
@ -335,7 +336,7 @@ static void update_ip_address_by_qmi(const char *ifname, const IPV4_T *ipv4, con
d1 = strdup(ipv6Str(ipv6->Address)); d1 = strdup(ipv6Str(ipv6->Address));
d2 = strdup(ipv6Str(ipv6->Gateway)); d2 = strdup(ipv6Str(ipv6->Gateway));
update_ipv6_address(ifname, d1, d2, ipv6->PrefixLengthIPAddr); update_ipv6_address(ifname, d1, d2, ipv6->PrefixLengthIPAddr,m);
free(d1); free(d2); free(d1); free(d2);
//Adding DNS //Adding DNS
@ -522,7 +523,7 @@ void udhcpc_start(PROFILE_T *profile) {
#if 0 #if 0
if (profile->rawIP != 0) //mdm9x07/ec25,ec20 R2.0 if (profile->rawIP != 0) //mdm9x07/ec25,ec20 R2.0
{ {
update_ip_address_by_qmi(ifname, &profile->ipv4, &profile->ipv6); update_ip_address_by_qmi(ifname, &profile->ipv4, profile->ipv6, &profile->metric);
return; return;
} }
#endif #endif
@ -531,7 +532,7 @@ void udhcpc_start(PROFILE_T *profile) {
goto set_ipv6; goto set_ipv6;
if (profile->no_dhcp || profile->request_ops == &mbim_request_ops) { //lots of mbim modem do not support DHCP if (profile->no_dhcp || profile->request_ops == &mbim_request_ops) { //lots of mbim modem do not support DHCP
update_ip_address_by_qmi(ifname, &profile->ipv4, NULL); update_ip_address_by_qmi(ifname, &profile->ipv4, NULL, profile->metric);
} }
else else
/* Do DHCP using busybox tools */ /* Do DHCP using busybox tools */
@ -580,7 +581,6 @@ void udhcpc_start(PROFILE_T *profile) {
#else #else
pthread_create(&udhcpc_thread_id, NULL, udhcpc_thread_function, (void*)strdup(udhcpc_cmd)); pthread_create(&udhcpc_thread_id, NULL, udhcpc_thread_function, (void*)strdup(udhcpc_cmd));
pthread_join(udhcpc_thread_id, NULL); pthread_join(udhcpc_thread_id, NULL);
if (profile->request_ops == &atc_request_ops) { if (profile->request_ops == &atc_request_ops) {
profile->udhcpc_ip = 0; profile->udhcpc_ip = 0;
ifc_get_addr(ifname, &profile->udhcpc_ip); ifc_get_addr(ifname, &profile->udhcpc_ip);
@ -604,7 +604,7 @@ void udhcpc_start(PROFILE_T *profile) {
if (!ql_netcard_ipv4_address_check(ifname, qmi2addr(profile->ipv4.Address))) { if (!ql_netcard_ipv4_address_check(ifname, qmi2addr(profile->ipv4.Address))) {
//no udhcpc's default.script exist, directly set ip and dns //no udhcpc's default.script exist, directly set ip and dns
update_ip_address_by_qmi(ifname, &profile->ipv4, NULL); update_ip_address_by_qmi(ifname, &profile->ipv4, NULL, profile->metric);
} }
//Add by Demon. check default route //Add by Demon. check default route
FILE *rt_fp = NULL; FILE *rt_fp = NULL;
@ -655,7 +655,7 @@ set_ipv6:
close(forward_fd); close(forward_fd);
} }
update_ip_address_by_qmi(ifname, NULL, &profile->ipv6); update_ip_address_by_qmi(ifname, NULL, &profile->ipv6, profile->metric);
if (profile->ipv6.DnsPrimary[0] || profile->ipv6.DnsSecondary[0]) { if (profile->ipv6.DnsPrimary[0] || profile->ipv6.DnsSecondary[0]) {
char dns1str[64], dns2str[64]; char dns1str[64], dns2str[64];