From eb4ee6de59f8ed87bc1efe15b8f5ba7ae015f15e Mon Sep 17 00:00:00 2001 From: fujr Date: Thu, 24 Oct 2024 17:59:20 +0800 Subject: [PATCH] add mertric option --- application/quectel_CM_5G_M/src/QMIThread.h | 6 ++--- application/quectel_CM_5G_M/src/main.c | 19 ++++++++++--- application/quectel_CM_5G_M/src/udhcpc.c | 30 ++++++++++----------- 3 files changed, 34 insertions(+), 21 deletions(-) diff --git a/application/quectel_CM_5G_M/src/QMIThread.h b/application/quectel_CM_5G_M/src/QMIThread.h index 6d65053..039ee68 100644 --- a/application/quectel_CM_5G_M/src/QMIThread.h +++ b/application/quectel_CM_5G_M/src/QMIThread.h @@ -194,6 +194,7 @@ typedef struct __PROFILE { const char *password; int auth; int iptype; + const char *metric; const char *pincode; char proxy[32]; int pdp;//pdp_context @@ -247,7 +248,6 @@ typedef struct __PROFILE { char old_password[64]; int old_auth; int old_iptype; - int metric; const struct qmi_device_ops *qmi_ops; const struct request_ops *request_ops; @@ -372,8 +372,8 @@ extern int debug_qmi; extern int qmidevice_control_fd[2]; extern int g_donot_exit_when_modem_hangup; 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_ipv6_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, char *metric); int reattach_driver(PROFILE_T *profile); extern void no_trunc_strncpy(char *dest, const char *src, size_t dest_size); diff --git a/application/quectel_CM_5G_M/src/main.c b/application/quectel_CM_5G_M/src/main.c index 158920a..428629f 100644 --- a/application/quectel_CM_5G_M/src/main.c +++ b/application/quectel_CM_5G_M/src/main.c @@ -251,6 +251,7 @@ static int usage(const char *progname) { dbg_time("-b Enable network interface bridge function (default 0)"); dbg_time("-v Verbose log mode, for debug purpose."); 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("Example 1: %s ", 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'; } break; - case 't': + + case 'M': 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; - + default: return usage(argv[0]); break; } } + if (!profile->metric){ + profile->metric = "0"; + } + if (profile->enable_ipv4 != 1 && profile->enable_ipv6 != 1) { // default enable IPv4 profile->enable_ipv4 = 1; } diff --git a/application/quectel_CM_5G_M/src/udhcpc.c b/application/quectel_CM_5G_M/src/udhcpc.c index 5c2c12a..1a0de99 100644 --- a/application/quectel_CM_5G_M/src/udhcpc.c +++ b/application/quectel_CM_5G_M/src/udhcpc.c @@ -248,7 +248,7 @@ static const char *ipv6Str(const UCHAR Address[16]) { 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]; @@ -263,7 +263,7 @@ void update_ipv4_address(const char *ifname, const char *ip, const char *gw, uns ql_system(shell_cmd); //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); } else { 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); //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)); - 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); } } -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]; (void)gw; @@ -293,18 +294,18 @@ void update_ipv6_address(const char *ifname, const char *ip, const char *gw, uns ql_system(shell_cmd); //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); } else { snprintf(shell_cmd, sizeof(shell_cmd), "ifconfig %s %s/%d", ifname, ip, prefix); 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); } } -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; 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); //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)); 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); //Adding DNS @@ -522,7 +523,7 @@ void udhcpc_start(PROFILE_T *profile) { #if 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; } #endif @@ -531,7 +532,7 @@ void udhcpc_start(PROFILE_T *profile) { goto set_ipv6; 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 /* Do DHCP using busybox tools */ @@ -580,7 +581,6 @@ void udhcpc_start(PROFILE_T *profile) { #else pthread_create(&udhcpc_thread_id, NULL, udhcpc_thread_function, (void*)strdup(udhcpc_cmd)); pthread_join(udhcpc_thread_id, NULL); - if (profile->request_ops == &atc_request_ops) { profile->udhcpc_ip = 0; 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))) { //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 FILE *rt_fp = NULL; @@ -655,7 +655,7 @@ set_ipv6: 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]) { char dns1str[64], dns2str[64];