-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheth.c
More file actions
262 lines (219 loc) · 7.17 KB
/
Copy patheth.c
File metadata and controls
262 lines (219 loc) · 7.17 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*
# _____ ___ ____ ___ ____
# ____| | ____| | | |____|
# | ___| |____ ___| ____| | \ PS2DEV Open Source Project.
#-----------------------------------------------------------------------
# Copyright 2001-2004, ps2dev - http://www.ps2dev.org
# Licenced under Academic Free License version 2.0
# Review ps2sdk README & LICENSE files for further details.
#
*/
#include <stdio.h>
#include <kernel.h>
#include <iopcontrol.h>
#include <iopheap.h>
#include <debug.h>
#include <netman.h>
#include <ps2ip.h>
#include <sifrpc.h>
#include <loadfile.h>
#include <sbv_patches.h>
extern unsigned char DEV9_irx[];
extern unsigned int size_DEV9_irx;
extern unsigned char SMAP_irx[];
extern unsigned int size_SMAP_irx;
extern unsigned char NETMAN_irx[];
extern unsigned int size_NETMAN_irx;
static int ethApplyNetIFConfig(int mode)
{
int result;
//By default, auto-negotiation is used.
static int CurrentMode = NETMAN_NETIF_ETH_LINK_MODE_AUTO;
if(CurrentMode != mode)
{ //Change the setting, only if different.
if((result = NetManSetLinkMode(mode)) == 0)
CurrentMode = mode;
}else
result = 0;
return result;
}
static void EthStatusCheckCb(s32 alarm_id, u16 time, void *common)
{
iWakeupThread(*(int*)common);
}
static int WaitValidNetState(int (*checkingFunction)(void))
{
int ThreadID, retry_cycles;
// Wait for a valid network status;
ThreadID = GetThreadId();
for(retry_cycles = 0; checkingFunction() == 0; retry_cycles++)
{ //Sleep for 1000ms.
SetAlarm(1000 * 16, &EthStatusCheckCb, &ThreadID);
SleepThread();
if(retry_cycles >= 10) //10s = 10*1000ms
return -1;
}
return 0;
}
static int ethGetNetIFLinkStatus(void)
{
return(NetManIoctl(NETMAN_NETIF_IOCTL_GET_LINK_STATUS, NULL, 0, NULL, 0) == NETMAN_NETIF_ETH_LINK_STATE_UP);
}
static int ethWaitValidNetIFLinkState(void)
{
return WaitValidNetState(ðGetNetIFLinkStatus);
}
static int ethApplyIPConfig(int use_dhcp, const struct ip4_addr *ip, const struct ip4_addr *netmask, const struct ip4_addr *gateway, const struct ip4_addr *dns)
{
t_ip_info ip_info;
int result;
//SMAP is registered as the "sm0" device to the TCP/IP stack.
if ((result = ps2ip_getconfig("sm0", &ip_info)) >= 0)
{
const ip_addr_t *dns_curr;
//Obtain the current DNS server settings.
dns_curr = dns_getserver(0);
//Check if it's the same. Otherwise, apply the new configuration.
if ((use_dhcp != ip_info.dhcp_enabled)
|| (!use_dhcp &&
(!ip_addr_cmp(ip, (struct ip4_addr *)&ip_info.ipaddr) ||
!ip_addr_cmp(netmask, (struct ip4_addr *)&ip_info.netmask) ||
!ip_addr_cmp(gateway, (struct ip4_addr *)&ip_info.gw) ||
!ip_addr_cmp(dns, dns_curr))))
{
if (use_dhcp)
{
ip_info.dhcp_enabled = 1;
}
else
{ //Copy over new settings if DHCP is not used.
ip_addr_set((struct ip4_addr *)&ip_info.ipaddr, ip);
ip_addr_set((struct ip4_addr *)&ip_info.netmask, netmask);
ip_addr_set((struct ip4_addr *)&ip_info.gw, gateway);
ip_info.dhcp_enabled = 0;
}
//Update settings.
result = ps2ip_setconfig(&ip_info);
if (!use_dhcp)
dns_setserver(0, dns);
}
else
result = 0;
}
return result;
}
static void ethPrintIPConfig(void)
{
t_ip_info ip_info;
u8 ip_address[4], netmask[4], gateway[4], dns[4];
//SMAP is registered as the "sm0" device to the TCP/IP stack.
if (ps2ip_getconfig("sm0", &ip_info) >= 0)
{
const ip_addr_t *dns_curr;
//Obtain the current DNS server settings.
dns_curr = dns_getserver(0);
ip_address[0] = ip4_addr1((struct ip4_addr *)&ip_info.ipaddr);
ip_address[1] = ip4_addr2((struct ip4_addr *)&ip_info.ipaddr);
ip_address[2] = ip4_addr3((struct ip4_addr *)&ip_info.ipaddr);
ip_address[3] = ip4_addr4((struct ip4_addr *)&ip_info.ipaddr);
netmask[0] = ip4_addr1((struct ip4_addr *)&ip_info.netmask);
netmask[1] = ip4_addr2((struct ip4_addr *)&ip_info.netmask);
netmask[2] = ip4_addr3((struct ip4_addr *)&ip_info.netmask);
netmask[3] = ip4_addr4((struct ip4_addr *)&ip_info.netmask);
gateway[0] = ip4_addr1((struct ip4_addr *)&ip_info.gw);
gateway[1] = ip4_addr2((struct ip4_addr *)&ip_info.gw);
gateway[2] = ip4_addr3((struct ip4_addr *)&ip_info.gw);
gateway[3] = ip4_addr4((struct ip4_addr *)&ip_info.gw);
dns[0] = ip4_addr1(dns_curr);
dns[1] = ip4_addr2(dns_curr);
dns[2] = ip4_addr3(dns_curr);
dns[3] = ip4_addr4(dns_curr);
scr_printf( "IP:\t%d.%d.%d.%d\n"
"NM:\t%d.%d.%d.%d\n"
"GW:\t%d.%d.%d.%d\n"
"DNS:\t%d.%d.%d.%d\n",
ip_address[0], ip_address[1], ip_address[2], ip_address[3],
netmask[0], netmask[1], netmask[2], netmask[3],
gateway[0], gateway[1], gateway[2], gateway[3],
dns[0], dns[1], dns[2], dns[3]);
}
else
{
scr_printf("Unable to read IP address.\n");
}
}
static void ethPrintLinkStatus(void)
{
int mode, baseMode;
//SMAP is registered as the "sm0" device to the TCP/IP stack.
scr_printf("Link:\t");
if (NetManIoctl(NETMAN_NETIF_IOCTL_GET_LINK_STATUS, NULL, 0, NULL, 0) == NETMAN_NETIF_ETH_LINK_STATE_UP)
scr_printf("Up\n");
else
scr_printf("Down\n");
scr_printf("Mode:\t");
mode = NetManIoctl(NETMAN_NETIF_IOCTL_ETH_GET_LINK_MODE, NULL, 0, NULL, 0);
//NETMAN_NETIF_ETH_LINK_MODE_PAUSE is a flag, so file it off first.
baseMode = mode & (~NETMAN_NETIF_ETH_LINK_DISABLE_PAUSE);
switch(baseMode)
{
case NETMAN_NETIF_ETH_LINK_MODE_10M_HDX:
scr_printf("10M HDX");
break;
case NETMAN_NETIF_ETH_LINK_MODE_10M_FDX:
scr_printf("10M FDX");
break;
case NETMAN_NETIF_ETH_LINK_MODE_100M_HDX:
scr_printf("100M HDX");
break;
case NETMAN_NETIF_ETH_LINK_MODE_100M_FDX:
scr_printf("100M FDX");
break;
default:
scr_printf("Unknown");
}
if(!(mode & NETMAN_NETIF_ETH_LINK_DISABLE_PAUSE))
scr_printf(" with ");
else
scr_printf(" without ");
scr_printf("Flow Control\n");
}
static int ethStart(unsigned char * ip, unsigned char * netmask, unsigned char * gateway) {
struct ip4_addr IP, NM, GW, DNS;
int EthernetLinkMode;
//Load modules
SifExecModuleBuffer(DEV9_irx, size_DEV9_irx, 0, NULL, NULL);
SifExecModuleBuffer(NETMAN_irx, size_NETMAN_irx, 0, NULL, NULL);
SifExecModuleBuffer(SMAP_irx, size_SMAP_irx, 0, NULL, NULL);
//Initialize NETMAN
NetManInit();
//The network interface link mode/duplex can be set.
EthernetLinkMode = NETMAN_NETIF_ETH_LINK_MODE_AUTO;
//Attempt to apply the new link setting.
if(ethApplyNetIFConfig(EthernetLinkMode) != 0) {
scr_printf("Error: failed to set link mode.\n");
return 1;
}
//Initialize IP address.
IP4_ADDR(&IP, ip[0], ip[1], ip[2], ip[3]);
IP4_ADDR(&NM, netmask[0], netmask[1], netmask[2], netmask[3]);
IP4_ADDR(&GW, gateway[0], gateway[1], gateway[2], gateway[3]);
//DNS is not required if the DNS service is not used, but this demo will show how it is done.
IP4_ADDR(&DNS, gateway[0], gateway[1], gateway[2], gateway[3]);
//Initialize the TCP/IP protocol stack.
ps2ipInit(&IP, &NM, &GW);
dns_setserver(0, &DNS); //Set DNS server
//Change IP address
//IP4_ADDR(&IP, 192, 168, 0, 10);
ethApplyIPConfig(0, &IP, &NM, &GW, &DNS); // ?
//Wait for the link to become ready.
scr_printf("Waiting for connection ...\n");
if(ethWaitValidNetIFLinkState() != 0) {
scr_printf("Error: failed to get valid link status.\n");
return 1;
}
scr_printf("Initialized:\n");
ethPrintLinkStatus();
ethPrintIPConfig();
return 0;
}