This repository has been archived by the owner on Feb 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnet.c
149 lines (124 loc) · 3.53 KB
/
net.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include "lwip/debug.h"
#include "lwip/dhcp.h"
#include "lwip/init.h"
#include "lwip/netif.h"
#include "lwip/sys.h"
#include "lwip/tcpip.h"
#include "lwip/timers.h"
#include "netif/etharp.h"
#include "pktdrv.h"
#undef printf // FIXME: I #defined this to debugPrint but now it conflicts
#include <hal/input.h>
#include <hal/xbox.h>
#include <pbkit/pbkit.h>
#include <xboxkrnl/xboxkrnl.h>
#include <debug.h>
#include <string.h>
#include <lwip/opt.h>
#include <lwip/arch.h>
#include <lwip/api.h>
#include <stdio.h>
#include <assert.h>
#include <assert.h>
#include <stdlib.h>
#define USE_DHCP 1
#define PKT_TMR_INTERVAL 1
int check__2 = 0;
static void drm_check_failed(void)
{
check__2 = 1;
}
#include "drm.h"
struct netif nforce_netif, *g_pnetif;
err_t nforceif_init(struct netif *netif);
int hacky_watchdog_timer_start;
int match_connected = 0;
ip4_addr_t central_server_ip;
char xbox_ip_str[16];
char central_server_ip_str[16];
static void packet_timer(void *arg)
{
LWIP_UNUSED_ARG(arg);
Pktdrv_ReceivePackets();
sys_timeout(PKT_TMR_INTERVAL, packet_timer, NULL);
int since_start = (XGetTickCount()-hacky_watchdog_timer_start) / 1000;
// debugPrint("since start = %d\n", since_start);
if (since_start > 15) {
if (!match_connected) {
XReboot();
} else {
// debugPrint("match was connected\n");
return;
}
}
}
static void tcpip_init_done(void *arg)
{
sys_sem_t *init_complete = arg;
sys_sem_signal(init_complete);
}
int init_if(void)
{
sys_sem_t init_complete;
const ip4_addr_t *ip;
static ip4_addr_t ipaddr, netmask, gw;
#if DEBUGGING
// asm volatile ("jmp .");
debug_flags = LWIP_DBG_ON;
#else
debug_flags = 0;
#endif
#if USE_DHCP
IP4_ADDR(&gw, 0,0,0,0);
IP4_ADDR(&ipaddr, 0,0,0,0);
IP4_ADDR(&netmask, 0,0,0,0);
#else
IP4_ADDR(&gw, 10,0,2,1);
IP4_ADDR(&ipaddr, 10,0,2,10);
IP4_ADDR(&netmask, 255,255,255,0);
#endif
/* Initialize the TCP/IP stack. Wait for completion. */
sys_sem_new(&init_complete, 0);
tcpip_init(tcpip_init_done, &init_complete);
sys_sem_wait(&init_complete);
sys_sem_free(&init_complete);
g_pnetif = netif_add(&nforce_netif, &ipaddr, &netmask, &gw,
NULL, nforceif_init, ethernet_input);
assert(g_pnetif != NULL);
netif_set_default(g_pnetif);
netif_set_up(g_pnetif);
#if USE_DHCP
dhcp_start(g_pnetif);
#endif
hacky_watchdog_timer_start = XGetTickCount();
packet_timer(NULL);
#if USE_DHCP
debugPrint("Waiting for DHCP...\n");
while (g_pnetif->dhcp->state != DHCP_STATE_BOUND) {
NtYieldExecution();
}
debugPrint("DHCP bound!\n");
#endif
check_cpuid();
#if USE_DHCP
// Calculate central server
int team = ip4_addr3(netif_ip4_addr(g_pnetif));
assert(team >= 100);
team -= 100;
debugPrint("Team = %d\n", team);
IP4_ADDR(¢ral_server_ip, 10,13,37,team);
#endif
debugPrint("\n");
debugPrint("IP address....... %s\n", ip4addr_ntoa(netif_ip4_addr(g_pnetif)));
strcpy(xbox_ip_str, ip4addr_ntoa(netif_ip4_addr(g_pnetif)));
debugPrint("Mask............. %s\n", ip4addr_ntoa(netif_ip4_netmask(g_pnetif)));
debugPrint("Gateway.......... %s\n", ip4addr_ntoa(netif_ip4_gw(g_pnetif)));
strcpy(central_server_ip_str, ip4addr_ntoa(¢ral_server_ip));
debugPrint("Central Server... %s\n", central_server_ip_str);
debugPrint("\n");
#if !USE_DHCP
strcpy(central_server_ip_str, "10.0.2.2");
#endif
debugPrint("< will reboot in 10 seconds if match is not ready >\n");
return 0;
}