-
Notifications
You must be signed in to change notification settings - Fork 0
/
ps2ipcfglib.c
223 lines (189 loc) · 6.73 KB
/
ps2ipcfglib.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
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
/*
# _____ ___ ____ ___ ____
# ____| | ____| | | |____|
# | ___| |____ ___| ____| | \ 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 <stdlib.h>
#include <kernel.h>
#include <iopcontrol.h>
#include <iopheap.h>
#include <debug.h>
#include <netman.h>
#include <ps2ip.h>
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;
}
void EthStatusCheckCb(s32 alarm_id, u16 time, void *common)
{
iWakeupThread(*(int *)common);
}
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;
}
int ethGetNetIFLinkStatus(void)
{
return (NetManIoctl(NETMAN_NETIF_IOCTL_GET_LINK_STATUS, NULL, 0, NULL, 0) == NETMAN_NETIF_ETH_LINK_STATE_UP);
}
int ethWaitValidNetIFLinkState(void)
{
return WaitValidNetState(ðGetNetIFLinkStatus);
}
int ethGetDHCPStatus(void)
{
t_ip_info ip_info;
int result;
if ((result = ps2ip_getconfig("sm0", &ip_info)) >= 0)
{ // Check for a successful state if DHCP is enabled.
if (ip_info.dhcp_enabled)
result = (ip_info.dhcp_status == DHCP_STATE_BOUND || (ip_info.dhcp_status == DHCP_STATE_OFF));
else
result = -1;
}
return result;
}
int ethWaitValidDHCPState(void)
{
return WaitValidNetState(ðGetDHCPStatus);
}
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;
}
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");
}
}
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");
}