/** * opencoreC - Network Stack Server (Ring 3 User Space) * Centralized network daemon managing RTL8139 hardware NIC, ARP, DHCP, DNS, TCP/IP. * Listens on NET_ENDPOINT (6) and handles synchronous IPC requests from sh and gui_server. */ #include "stdio.h" #include "string.h" #include "abi/syscalls.h" #include "abi/ipc.h" #include "abi/net.h" extern "C" void _start(void) { puts("[NET-SERVER] Initializing Hardware RTL8139 NIC & TCP/IP Stack in Ring 3..."); net_init(); // Acquire DHCP lease on startup int dhcp_res = net_dhcp_discover(); char ip_str[32], gw_str[32]; format_ip(g_net_cfg.ip, ip_str); format_ip(g_net_cfg.gateway, gw_str); if (dhcp_res == 0) { printf("[NET-SERVER] DHCP OK: IP %s, Gateway %s\n", ip_str, gw_str); } else { printf("[NET-SERVER] DHCP fallback: IP %s, Gateway %s\n", ip_str, gw_str); } uint64_t last_client = 0; uint64_t reply_val0 = 0; uint64_t reply_val1 = 0; while (1) { uint64_t sender_tid = 0; uint64_t opcode = 0; uint64_t arg0 = 0; uint64_t arg1 = 0; sysret_t ret = sys_ipc_reply_recv_raw( NET_ENDPOINT, last_client, reply_val0, reply_val1, &sender_tid, &opcode, &arg0, &arg1 ); if (ret == 0 && sender_tid != 0) { last_client = sender_tid; if (opcode == NET_OP_GET_CONFIG) { // reply_val0 = ip (low 32 bits) | (gateway << 32) // reply_val1 = netmask (low 32 bits) | (dns_server << 32) reply_val0 = ((uint64_t)g_net_cfg.gateway << 32) | (uint64_t)g_net_cfg.ip; reply_val1 = ((uint64_t)g_net_cfg.dns_server << 32) | (uint64_t)g_net_cfg.netmask; } else if (opcode == NET_OP_PING) { uint32_t target_ip = (uint32_t)arg0; uint16_t seq = (uint16_t)(arg1 ? arg1 : 1); int ping_res = net_send_icmp_ping(target_ip, seq); reply_val0 = (ping_res == 0) ? 0 : (uint64_t)-1; reply_val1 = 0; } else if (opcode == NET_OP_DNS) { // Domain string passed in arg0 (8 bytes) + arg1 (8 bytes) char domain[20] = {0}; memcpy(domain, &arg0, 8); memcpy(domain + 8, &arg1, 8); uint32_t resolved_ip = 0; int res = net_dns_resolve(domain, &resolved_ip); reply_val0 = (res == 0) ? (uint64_t)resolved_ip : 0; reply_val1 = (uint64_t)res; } else if (opcode == NET_OP_DHCP) { int res = net_dhcp_discover(); reply_val0 = (res == 0) ? (uint64_t)g_net_cfg.ip : 0; reply_val1 = (uint64_t)g_net_cfg.gateway; } else { reply_val0 = (uint64_t)-1; reply_val1 = 0; } } else { last_client = 0; sys_yield(); } } }