/* 设置状态回调 */
ppp_set_notify_phase_callback(g_lwip_ctx.ppp_pcb, ppp_lwip_status_callback);
+
+ /* 设置网络接口状态回调 */
+ netif_set_status_callback(g_lwip_ctx.netif, ppp_lwip_netif_status_callback);
+
+ /* 使用标准PPP/LCP配置 */
+ PPP_DEBUG_INFO("Using standard PPP/LCP configuration...");
+ /* 使用标准PPP/LCP配置 */
+ PPP_DEBUG_INFO("Using standard PPP/LCP configuration...");
+
/* 保存回调函数 */
g_lwip_ctx.event_callback = config->event_callback;
g_lwip_ctx.event_callback_arg = config->event_callback_arg;
}
}
++/* PPP协议解析辅助函数 */
++static const char* ppp_get_protocol_name(uint16_t protocol)
++{
++ switch (protocol) {
++ case 0xC021: return "LCP";
++ case 0x8021: return "IPCP";
++ case 0xC023: return "PAP";
++ case 0xC223: return "CHAP";
++ case 0x0021: return "IP";
++ case 0x002D: return "VJ_COMP_TCP";
++ case 0x002F: return "VJ_UNCOMP_TCP";
++ default: return "UNKNOWN";
++ }
++}
++
++static const char* ppp_get_lcp_code_name(uint8_t code)
++{
++ switch (code) {
++ case 1: return "Configure-Request";
++ case 2: return "Configure-Ack";
++ case 3: return "Configure-Nak";
++ case 4: return "Configure-Reject";
++ case 5: return "Terminate-Request";
++ case 6: return "Terminate-Ack";
++ case 7: return "Code-Reject";
++ case 8: return "Protocol-Reject";
++ case 9: return "Echo-Request";
++ case 10: return "Echo-Reply";
++ case 11: return "Discard-Request";
++ default: return "Unknown";
++ }
++}
++
+/**
+ * @brief PPP网络接口状态回调函数
+ */
+static void ppp_lwip_netif_status_callback(struct netif *netif)
+{
+ if (!netif || netif != g_lwip_ctx.netif) {
+ return;
+ }
+
+ PPP_DEBUG_INFO("PPP netif status changed - flags: 0x%02x", netif->flags);
+ PPP_DEBUG_INFO("IP: 0x%08x, GW: 0x%08x, Netmask: 0x%08x",
+ ip4_addr_get_u32(&netif->ip_addr),
+ ip4_addr_get_u32(&netif->gw),
+ ip4_addr_get_u32(&netif->netmask));
+
+ /* 检查是否需要设置为默认网络接口 */
+ bool should_set_default = false;
+
+ /* 条件1:接口UP且有有效IP地址 */
+ if (netif_is_up(netif) && netif->ip_addr.addr != 0) {
+ should_set_default = true;
+ PPP_DEBUG_INFO("Condition met: interface UP with valid IP");
+ }
+ /* 条件2:接口UP且链路UP,即使IP暂时为0(可能IPCP还在协商) */
+ else if (netif_is_up(netif) && netif_is_link_up(netif)) {
+ /* 给IPCP协商一些时间,然后再次检查 */
+ PPP_DEBUG_INFO("Interface UP and link UP, but IP is 0 - will check again");
+ /* 这里不阻塞,让lwIP自然完成协商 */
+ }
+
+ if (should_set_default) {
+ /* 检查当前默认接口 */
+ struct netif *current_default = netif_default;
+ if (current_default != netif) {
+ PPP_DEBUG_INFO("Setting PPP interface as default (was: %s)...",
+ current_default ? "other" : "none");
+ netif_set_default(netif);
+ PPP_DEBUG_CRITICAL("PPP interface set as default network interface");
+ } else {
+ PPP_DEBUG_INFO("PPP interface is already the default");
+ }
+
+ /* 更新本地IP地址信息 */
+ uint32_t old_local_ip = g_lwip_ctx.local_ip;
+ g_lwip_ctx.local_ip = ip4_addr_get_u32(&netif->ip_addr);
+ g_lwip_ctx.remote_ip = ip4_addr_get_u32(&netif->gw);
+
+ /* 配置DNS服务器 - 从PPP协商结果中获取 */
+ if (g_lwip_ctx.ppp_pcb && g_lwip_ctx.ppp_pcb->ipcp_gotoptions.dnsaddr[0] != 0) {
+ ip_addr_t dns_server;
+ IP_ADDR4(&dns_server,
+ (g_lwip_ctx.ppp_pcb->ipcp_gotoptions.dnsaddr[0] >> 0) & 0xFF,
+ (g_lwip_ctx.ppp_pcb->ipcp_gotoptions.dnsaddr[0] >> 8) & 0xFF,
+ (g_lwip_ctx.ppp_pcb->ipcp_gotoptions.dnsaddr[0] >> 16) & 0xFF,
+ (g_lwip_ctx.ppp_pcb->ipcp_gotoptions.dnsaddr[0] >> 24) & 0xFF);
+ dns_setserver(0, &dns_server);
+ PPP_DEBUG_CRITICAL("DNS1 configured: %u.%u.%u.%u",
+ (g_lwip_ctx.ppp_pcb->ipcp_gotoptions.dnsaddr[0] >> 0) & 0xFF,
+ (g_lwip_ctx.ppp_pcb->ipcp_gotoptions.dnsaddr[0] >> 8) & 0xFF,
+ (g_lwip_ctx.ppp_pcb->ipcp_gotoptions.dnsaddr[0] >> 16) & 0xFF,
+ (g_lwip_ctx.ppp_pcb->ipcp_gotoptions.dnsaddr[0] >> 24) & 0xFF);
+ }
+
+ if (g_lwip_ctx.ppp_pcb && g_lwip_ctx.ppp_pcb->ipcp_gotoptions.dnsaddr[1] != 0) {
+ ip_addr_t dns_server;
+ IP_ADDR4(&dns_server,
+ (g_lwip_ctx.ppp_pcb->ipcp_gotoptions.dnsaddr[1] >> 0) & 0xFF,
+ (g_lwip_ctx.ppp_pcb->ipcp_gotoptions.dnsaddr[1] >> 8) & 0xFF,
+ (g_lwip_ctx.ppp_pcb->ipcp_gotoptions.dnsaddr[1] >> 16) & 0xFF,
+ (g_lwip_ctx.ppp_pcb->ipcp_gotoptions.dnsaddr[1] >> 24) & 0xFF);
+ dns_setserver(1, &dns_server);
+ PPP_DEBUG_CRITICAL("DNS2 configured: %u.%u.%u.%u",
+ (g_lwip_ctx.ppp_pcb->ipcp_gotoptions.dnsaddr[1] >> 0) & 0xFF,
+ (g_lwip_ctx.ppp_pcb->ipcp_gotoptions.dnsaddr[1] >> 8) & 0xFF,
+ (g_lwip_ctx.ppp_pcb->ipcp_gotoptions.dnsaddr[1] >> 16) & 0xFF,
+ (g_lwip_ctx.ppp_pcb->ipcp_gotoptions.dnsaddr[1] >> 24) & 0xFF);
+ }
+
+ /* 如果PPP协商没有提供DNS,使用公共DNS作为备用 */
+ const ip_addr_t *current_dns1 = dns_getserver(0);
+ if (!current_dns1 || ip_addr_isany(current_dns1)) {
+ ip_addr_t public_dns;
+ IP_ADDR4(&public_dns, 8, 8, 8, 8); // Google DNS
+ dns_setserver(0, &public_dns);
+ PPP_DEBUG_INFO("Using fallback DNS1: 8.8.8.8");
+ }
+
+ const ip_addr_t *current_dns2 = dns_getserver(1);
+ if (!current_dns2 || ip_addr_isany(current_dns2)) {
+ ip_addr_t public_dns;
+ IP_ADDR4(&public_dns, 8, 8, 4, 4); // Google DNS
+ dns_setserver(1, &public_dns);
+ PPP_DEBUG_INFO("Using fallback DNS2: 8.8.4.4");
+ }
+
+ if (old_local_ip != g_lwip_ctx.local_ip) {
+ PPP_DEBUG_CRITICAL("IP address updated - Local: %u.%u.%u.%u, Remote: %u.%u.%u.%u",
+ (g_lwip_ctx.local_ip >> 0) & 0xFF,
+ (g_lwip_ctx.local_ip >> 8) & 0xFF,
+ (g_lwip_ctx.local_ip >> 16) & 0xFF,
+ (g_lwip_ctx.local_ip >> 24) & 0xFF,
+ (g_lwip_ctx.remote_ip >> 0) & 0xFF,
+ (g_lwip_ctx.remote_ip >> 8) & 0xFF,
+ (g_lwip_ctx.remote_ip >> 16) & 0xFF,
+ (g_lwip_ctx.remote_ip >> 24) & 0xFF);
+ }
+ }
+}
+
/* PPP协议解析辅助函数 */
static const char* ppp_get_protocol_name(uint16_t protocol)
{