Skip to content

Commit eed3752

Browse files
thrimborLukeUsher
authored andcommitted
nxdk: Implement network initialization API
1 parent 901fe3f commit eed3752

File tree

5 files changed

+204
-152
lines changed

5 files changed

+204
-152
lines changed

lib/nxdk/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ NXDK_SRCS := \
22
$(NXDK_DIR)/lib/nxdk/configsector.c \
33
$(NXDK_DIR)/lib/nxdk/format.c \
44
$(NXDK_DIR)/lib/nxdk/mount.c \
5+
$(NXDK_DIR)/lib/nxdk/net.c \
56
$(NXDK_DIR)/lib/nxdk/path.c \
67
$(NXDK_DIR)/lib/nxdk/xbe.c
78

lib/nxdk/net.c

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
// SPDX-License-Identifier: MIT
2+
3+
// SPDX-FileCopyrightText: 2022 Stefan Schmidt
4+
5+
#include "net.h"
6+
7+
#include <assert.h>
8+
#include <stdbool.h>
9+
10+
#include <nxdk/configsector.h>
11+
#include <netif/etharp.h>
12+
#include <lwip/dhcp.h>
13+
#include <lwip/dns.h>
14+
#include <lwip/netif.h>
15+
#include <lwip/netifapi.h>
16+
#include <lwip/tcpip.h>
17+
#include <lwip/timeouts.h>
18+
#include <pktdrv.h>
19+
#include <xboxkrnl/xboxkrnl.h>
20+
21+
22+
err_t nforceif_init(struct netif *netif);
23+
24+
struct netif *g_pnetif;
25+
static struct netif nforce_netif;
26+
27+
static void tcpip_init_done(void *arg)
28+
{
29+
KEVENT *init_complete = arg;
30+
KeSetEvent(init_complete, IO_NO_INCREMENT, FALSE);
31+
}
32+
33+
static void packet_timer(void *arg)
34+
{
35+
LWIP_UNUSED_ARG(arg);
36+
Pktdrv_ReceivePackets();
37+
sys_timeout(5 /* ms */, packet_timer, NULL);
38+
}
39+
40+
int nxNetInit(const nx_net_parameters_t *parameters)
41+
{
42+
ip4_addr_t ipaddr, netmask, gateway;
43+
bool ipv4_dhcp;
44+
ip_addr_t dns[2];
45+
bool dns_override = false;
46+
47+
if (!parameters || parameters->ipv4_mode == NX_NET_AUTO) {
48+
nxdk_network_config_sector_t configSector;
49+
if (!nxLoadNetworkConfig(&configSector)) {
50+
return -1;
51+
}
52+
53+
if (configSector.dhcpFlags & NXDK_NETWORK_CONFIG_MANUAL_IP) {
54+
ipv4_dhcp = false;
55+
IP4_ADDR(&ipaddr, configSector.manual.ip >> 0 & 0xff,
56+
(configSector.manual.ip >> 8 & 0xff),
57+
(configSector.manual.ip >> 16 & 0xff),
58+
(configSector.manual.ip >> 24 & 0xff));
59+
IP4_ADDR(&gateway, configSector.manual.defaultGateway >> 0 & 0xff,
60+
(configSector.manual.defaultGateway >> 8 & 0xff),
61+
(configSector.manual.defaultGateway >> 16 & 0xff),
62+
(configSector.manual.defaultGateway >> 24 & 0xff));
63+
IP4_ADDR(&netmask, configSector.manual.subnetMask >> 0 & 0xff,
64+
(configSector.manual.subnetMask >> 8 & 0xff),
65+
(configSector.manual.subnetMask >> 16 & 0xff),
66+
(configSector.manual.subnetMask >> 24 & 0xff));;
67+
} else {
68+
ipv4_dhcp = true;
69+
}
70+
71+
if (configSector.dhcpFlags & NXDK_NETWORK_CONFIG_MANUAL_DNS) {
72+
IP4_ADDR(&dns[0].u_addr.ip4, configSector.manual.primaryDns >> 0 & 0xff,
73+
(configSector.manual.primaryDns >> 8 & 0xff),
74+
(configSector.manual.primaryDns >> 16 & 0xff),
75+
(configSector.manual.primaryDns >> 24 & 0xff));
76+
IP4_ADDR(&dns[1].u_addr.ip4, configSector.manual.secondaryDns >> 0 & 0xff,
77+
(configSector.manual.secondaryDns >> 8 & 0xff),
78+
(configSector.manual.secondaryDns >> 16 & 0xff),
79+
(configSector.manual.secondaryDns >> 24 & 0xff));
80+
dns[0].type = IPADDR_TYPE_V4;
81+
dns[1].type = IPADDR_TYPE_V4;
82+
dns_override = true;
83+
}
84+
} else if (parameters->ipv4_mode == NX_NET_DHCP) {
85+
ipv4_dhcp = true;
86+
} else if (parameters->ipv4_mode == NX_NET_STATIC) {
87+
ipv4_dhcp = false;
88+
IP4_ADDR(&ipaddr, parameters->ipv4_ip >> 0 & 0xff,
89+
(parameters->ipv4_ip >> 8 & 0xff),
90+
(parameters->ipv4_ip >> 16 & 0xff),
91+
(parameters->ipv4_ip >> 24 & 0xff));
92+
IP4_ADDR(&gateway, parameters->ipv4_gateway >> 0 & 0xff,
93+
(parameters->ipv4_gateway >> 8 & 0xff),
94+
(parameters->ipv4_gateway >> 16 & 0xff),
95+
(parameters->ipv4_gateway >> 24 & 0xff));
96+
IP4_ADDR(&netmask, parameters->ipv4_netmask >> 0 & 0xff,
97+
(parameters->ipv4_netmask >> 8 & 0xff),
98+
(parameters->ipv4_netmask >> 16 & 0xff),
99+
(parameters->ipv4_netmask >> 24 & 0xff));
100+
} else {
101+
assert(false);
102+
return -1;
103+
}
104+
105+
if (ipv4_dhcp) {
106+
IP4_ADDR(&gateway, 0, 0, 0, 0);
107+
IP4_ADDR(&ipaddr, 0, 0, 0, 0);
108+
IP4_ADDR(&netmask, 0, 0, 0, 0);
109+
}
110+
111+
KEVENT tcpip_init_complete;
112+
KeInitializeEvent(&tcpip_init_complete, SynchronizationEvent, FALSE);
113+
tcpip_init(tcpip_init_done, &tcpip_init_complete);
114+
KeWaitForSingleObject(&tcpip_init_complete, Executive, KernelMode, FALSE, NULL);
115+
116+
g_pnetif = &nforce_netif;
117+
err_t err = netifapi_netif_add(&nforce_netif, &ipaddr, &netmask, &gateway, NULL, nforceif_init, ethernet_input);
118+
if (err != ERR_OK) {
119+
debugPrint("netif_add failed\n");
120+
return -1;
121+
}
122+
123+
netifapi_netif_set_default(&nforce_netif);
124+
netifapi_netif_set_up(&nforce_netif);
125+
126+
packet_timer(NULL);
127+
128+
if (ipv4_dhcp) {
129+
netifapi_dhcp_start(&nforce_netif);
130+
131+
LARGE_INTEGER duration;
132+
duration.QuadPart = ((LONGLONG)1000) * -10000;
133+
int i = 0;
134+
while (dhcp_supplied_address(&nforce_netif) == 0) {
135+
i++;
136+
if (i == 10) {
137+
return -2;
138+
}
139+
KeDelayExecutionThread(KernelMode, FALSE, &duration);
140+
}
141+
}
142+
143+
if (parameters) {
144+
if (parameters->ipv4_dns1 != 0 || parameters->ipv4_dns2 != 0) {
145+
IP4_ADDR(&dns[0].u_addr.ip4, parameters->ipv4_dns1 >> 0 & 0xff,
146+
(parameters->ipv4_dns1 >> 8 & 0xff),
147+
(parameters->ipv4_dns1 >> 16 & 0xff),
148+
(parameters->ipv4_dns1 >> 24 & 0xff));
149+
IP4_ADDR(&dns[1].u_addr.ip4, parameters->ipv4_dns2 >> 0 & 0xff,
150+
(parameters->ipv4_dns2 >> 8 & 0xff),
151+
(parameters->ipv4_dns2 >> 16 & 0xff),
152+
(parameters->ipv4_dns2 >> 24 & 0xff));
153+
dns[0].type = IPADDR_TYPE_V4;
154+
dns[1].type = IPADDR_TYPE_V4;
155+
dns_override = true;
156+
}
157+
}
158+
159+
if (dns_override) {
160+
dns_setserver(2, dns);
161+
}
162+
163+
return 0;
164+
}

lib/nxdk/net.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#ifndef __NXDK_NET_H__
2+
#define __NXDK_NET_H__
3+
4+
#include <stdint.h>
5+
6+
typedef enum nx_net_mode_t_
7+
{
8+
NX_NET_AUTO = 0,
9+
NX_NET_DHCP,
10+
NX_NET_STATIC
11+
} nx_net_mode_t;
12+
13+
typedef struct nx_net_parameters_t_
14+
{
15+
nx_net_mode_t ipv4_mode;
16+
nx_net_mode_t ipv6_mode;
17+
18+
uint32_t ipv4_ip;
19+
uint32_t ipv4_gateway;
20+
uint32_t ipv4_netmask;
21+
uint32_t ipv4_dns1;
22+
uint32_t ipv4_dns2;
23+
// TODO:
24+
// ipv6 static ip fields
25+
} nx_net_parameters_t;
26+
27+
int nxNetInit(const nx_net_parameters_t *parameters);
28+
int nxNetShutdown();
29+
30+
31+
#endif

samples/httpd/main.c

Lines changed: 5 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,20 @@
1-
#include "lwip/debug.h"
2-
#include "lwip/dhcp.h"
3-
#include "lwip/init.h"
4-
#include "lwip/netif.h"
5-
#include "lwip/sys.h"
6-
#include "lwip/tcpip.h"
7-
#include "lwip/timeouts.h"
8-
#include "netif/etharp.h"
9-
#include "pktdrv.h"
1+
#include <lwip/debug.h>
2+
#include <lwip/tcpip.h>
103
#include <hal/video.h>
11-
#include <hal/xbox.h>
12-
#include <xboxkrnl/xboxkrnl.h>
134
#include <hal/debug.h>
5+
#include <nxdk/net.h>
146

15-
#define USE_DHCP 1
16-
#define PKT_TMR_INTERVAL 5 /* ms */
177
#define DEBUGGING 0
188

199
#include "httpserver.h"
2010

21-
struct netif nforce_netif, *g_pnetif;
22-
23-
err_t nforceif_init(struct netif *netif);
24-
void http_server_netconn_init(void);
25-
static void packet_timer(void *arg);
26-
27-
static void tcpip_init_done(void *arg)
28-
{
29-
sys_sem_t *init_complete = arg;
30-
sys_sem_signal(init_complete);
31-
}
32-
33-
static void packet_timer(void *arg)
34-
{
35-
LWIP_UNUSED_ARG(arg);
36-
Pktdrv_ReceivePackets();
37-
sys_timeout(PKT_TMR_INTERVAL, packet_timer, NULL);
38-
}
11+
extern struct netif *g_pnetif;
3912

4013
int main(void)
4114
{
42-
sys_sem_t init_complete;
43-
const ip4_addr_t *ip;
44-
static ip4_addr_t ipaddr, netmask, gw;
45-
46-
#if DEBUGGING
47-
asm volatile ("jmp .");
48-
debug_flags = LWIP_DBG_ON;
49-
#else
50-
debug_flags = 0;
51-
#endif
52-
53-
#if USE_DHCP
54-
IP4_ADDR(&gw, 0,0,0,0);
55-
IP4_ADDR(&ipaddr, 0,0,0,0);
56-
IP4_ADDR(&netmask, 0,0,0,0);
57-
#else
58-
IP4_ADDR(&gw, 10,0,1,1);
59-
IP4_ADDR(&ipaddr, 10,0,1,7);
60-
IP4_ADDR(&netmask, 255,255,255,0);
61-
#endif
62-
63-
/* Initialize the TCP/IP stack. Wait for completion. */
64-
sys_sem_new(&init_complete, 0);
65-
tcpip_init(tcpip_init_done, &init_complete);
66-
sys_sem_wait(&init_complete);
67-
sys_sem_free(&init_complete);
68-
6915
XVideoSetMode(640, 480, 32, REFRESH_DEFAULT);
7016

71-
g_pnetif = netif_add(&nforce_netif, &ipaddr, &netmask, &gw,
72-
NULL, nforceif_init, ethernet_input);
73-
if (!g_pnetif) {
74-
debugPrint("netif_add failed\n");
75-
return 1;
76-
}
77-
78-
netif_set_default(g_pnetif);
79-
netif_set_up(g_pnetif);
80-
81-
#if USE_DHCP
82-
dhcp_start(g_pnetif);
83-
#endif
84-
85-
packet_timer(NULL);
86-
87-
#if USE_DHCP
88-
debugPrint("Waiting for DHCP...\n");
89-
while (dhcp_supplied_address(g_pnetif) == 0)
90-
NtYieldExecution();
91-
debugPrint("DHCP bound!\n");
92-
#endif
17+
nxNetInit(NULL);
9318

9419
debugPrint("\n");
9520
debugPrint("IP address.. %s\n", ip4addr_ntoa(netif_ip4_addr(g_pnetif)));
@@ -99,6 +24,5 @@ int main(void)
9924

10025
http_server_netconn_init();
10126
while (1) NtYieldExecution();
102-
Pktdrv_Quit();
10327
return 0;
10428
}

0 commit comments

Comments
 (0)