ps2sdk  1.1
A collection of Open Source libraries used for developing applications on Sony's PlayStation 2® (PS2).
ps2ip.c
Go to the documentation of this file.
1 /*
2 # _____ ___ ____ ___ ____
3 # ____| | ____| | | |____|
4 # | ___| |____ ___| ____| | \ PS2DEV Open Source Project.
5 #-----------------------------------------------------------------------
6 # Copyright 2001-2004, ps2dev - http://www.ps2dev.org
7 # Licenced under Academic Free License version 2.0
8 # Review ps2sdk README & LICENSE files for further details.
9 #
10 */
11 
12 #include <stdio.h>
13 #include <kernel.h>
14 #include <iopcontrol.h>
15 #include <iopheap.h>
16 #include <debug.h>
17 #include <netman.h>
18 #include <ps2ip.h>
19 #include <sifrpc.h>
20 #include <loadfile.h>
21 #include <sbv_patches.h>
22 
23 extern unsigned char DEV9_irx[];
24 extern unsigned int size_DEV9_irx;
25 
26 extern unsigned char SMAP_irx[];
27 extern unsigned int size_SMAP_irx;
28 
29 extern unsigned char NETMAN_irx[];
30 extern unsigned int size_NETMAN_irx;
31 
32 static int ethApplyNetIFConfig(int mode)
33 {
34  int result;
35  //By default, auto-negotiation is used.
36  static int CurrentMode = NETMAN_NETIF_ETH_LINK_MODE_AUTO;
37 
38  if(CurrentMode != mode)
39  { //Change the setting, only if different.
40  if((result = NetManSetLinkMode(mode)) == 0)
41  CurrentMode = mode;
42  }else
43  result = 0;
44 
45  return result;
46 }
47 
48 static void EthStatusCheckCb(s32 alarm_id, u16 time, void *common)
49 {
50  iWakeupThread(*(int*)common);
51 }
52 
53 static int WaitValidNetState(int (*checkingFunction)(void))
54 {
55  int ThreadID, retry_cycles;
56 
57  // Wait for a valid network status;
58  ThreadID = GetThreadId();
59  for(retry_cycles = 0; checkingFunction() == 0; retry_cycles++)
60  { //Sleep for 1000ms.
61  SetAlarm(1000 * 16, &EthStatusCheckCb, &ThreadID);
62  SleepThread();
63 
64  if(retry_cycles >= 10) //10s = 10*1000ms
65  return -1;
66  }
67 
68  return 0;
69 }
70 
71 static int ethGetNetIFLinkStatus(void)
72 {
74 }
75 
77 {
79 }
80 
81 static int ethGetDHCPStatus(void)
82 {
84  int result;
85 
86  if ((result = ps2ip_getconfig("sm0", &ip_info)) >= 0)
87  { //Check for a successful state if DHCP is enabled.
90  else
91  result = -1;
92  }
93 
94  return result;
95 }
96 
97 static int ethWaitValidDHCPState(void)
98 {
100 }
101 
102 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)
103 {
105  const ip_addr_t *dns_curr;
106  int result;
107 
108  //SMAP is registered as the "sm0" device to the TCP/IP stack.
109  if ((result = ps2ip_getconfig("sm0", &ip_info)) >= 0)
110  {
111  //Obtain the current DNS server settings.
112  dns_curr = dns_getserver(0);
113 
114  //Check if it's the same. Otherwise, apply the new configuration.
115  if ((use_dhcp != ip_info.dhcp_enabled)
116  || (!use_dhcp &&
117  (!ip_addr_cmp(ip, (struct ip4_addr *)&ip_info.ipaddr) ||
118  !ip_addr_cmp(netmask, (struct ip4_addr *)&ip_info.netmask) ||
119  !ip_addr_cmp(gateway, (struct ip4_addr *)&ip_info.gw) ||
120  !ip_addr_cmp(dns, dns_curr))))
121  {
122  if (use_dhcp)
123  {
124  ip_info.dhcp_enabled = 1;
125  }
126  else
127  { //Copy over new settings if DHCP is not used.
128  ip_addr_set((struct ip4_addr *)&ip_info.ipaddr, ip);
129  ip_addr_set((struct ip4_addr *)&ip_info.netmask, netmask);
130  ip_addr_set((struct ip4_addr *)&ip_info.gw, gateway);
131 
132  ip_info.dhcp_enabled = 0;
133  }
134 
135  //Update settings.
137  if (!use_dhcp)
138  dns_setserver(0, dns);
139  }
140  else
141  result = 0;
142  }
143 
144  return result;
145 }
146 
147 static void ethPrintIPConfig(void)
148 {
150  const ip_addr_t *dns_curr;
151  u8 ip_address[4], netmask[4], gateway[4], dns[4];
152 
153  //SMAP is registered as the "sm0" device to the TCP/IP stack.
154  if (ps2ip_getconfig("sm0", &ip_info) >= 0)
155  {
156  //Obtain the current DNS server settings.
157  dns_curr = dns_getserver(0);
158 
159  ip_address[0] = ip4_addr1((struct ip4_addr *)&ip_info.ipaddr);
160  ip_address[1] = ip4_addr2((struct ip4_addr *)&ip_info.ipaddr);
161  ip_address[2] = ip4_addr3((struct ip4_addr *)&ip_info.ipaddr);
162  ip_address[3] = ip4_addr4((struct ip4_addr *)&ip_info.ipaddr);
163 
164  netmask[0] = ip4_addr1((struct ip4_addr *)&ip_info.netmask);
165  netmask[1] = ip4_addr2((struct ip4_addr *)&ip_info.netmask);
166  netmask[2] = ip4_addr3((struct ip4_addr *)&ip_info.netmask);
167  netmask[3] = ip4_addr4((struct ip4_addr *)&ip_info.netmask);
168 
169  gateway[0] = ip4_addr1((struct ip4_addr *)&ip_info.gw);
170  gateway[1] = ip4_addr2((struct ip4_addr *)&ip_info.gw);
171  gateway[2] = ip4_addr3((struct ip4_addr *)&ip_info.gw);
172  gateway[3] = ip4_addr4((struct ip4_addr *)&ip_info.gw);
173 
174  dns[0] = ip4_addr1(dns_curr);
175  dns[1] = ip4_addr2(dns_curr);
176  dns[2] = ip4_addr3(dns_curr);
177  dns[3] = ip4_addr4(dns_curr);
178 
179  scr_printf( "IP:\t%d.%d.%d.%d\n"
180  "NM:\t%d.%d.%d.%d\n"
181  "GW:\t%d.%d.%d.%d\n"
182  "DNS:\t%d.%d.%d.%d\n",
183  ip_address[0], ip_address[1], ip_address[2], ip_address[3],
184  netmask[0], netmask[1], netmask[2], netmask[3],
185  gateway[0], gateway[1], gateway[2], gateway[3],
186  dns[0], dns[1], dns[2], dns[3]);
187  }
188  else
189  {
190  scr_printf("Unable to read IP address.\n");
191  }
192 }
193 
194 static void ethPrintLinkStatus(void)
195 {
196  int mode, baseMode;
197 
198  //SMAP is registered as the "sm0" device to the TCP/IP stack.
199  scr_printf("Link:\t");
201  scr_printf("Up\n");
202  else
203  scr_printf("Down\n");
204 
205  scr_printf("Mode:\t");
207 
208  //NETMAN_NETIF_ETH_LINK_MODE_PAUSE is a flag, so file it off first.
210  switch(baseMode)
211  {
213  scr_printf("10M HDX");
214  break;
216  scr_printf("10M FDX");
217  break;
219  scr_printf("100M HDX");
220  break;
222  scr_printf("100M FDX");
223  break;
224  default:
225  scr_printf("Unknown");
226  }
228  scr_printf(" with ");
229  else
230  scr_printf(" without ");
231  scr_printf("Flow Control\n");
232 }
233 
234 int main(int argc, char *argv[])
235 {
236  struct ip4_addr IP, NM, GW, DNS;
237  int EthernetLinkMode;
238 
239  //Reboot IOP
240  SifInitRpc(0);
241  while(!SifIopReset("", 0)){};
242  while(!SifIopSync()){};
243 
244  //Initialize SIF services
245  SifInitRpc(0);
246  SifLoadFileInit();
247  SifInitIopHeap();
249 
250  //Load modules
254 
255  //Initialize NETMAN
256  NetManInit();
257 
258  init_scr();
259 
260  //The network interface link mode/duplex can be set.
261  EthernetLinkMode = NETMAN_NETIF_ETH_LINK_MODE_AUTO;
262 
263  //Attempt to apply the new link setting.
264  if(ethApplyNetIFConfig(EthernetLinkMode) != 0) {
265  scr_printf("Error: failed to set link mode.\n");
266  goto end;
267  }
268 
269  //Initialize IP address.
270  //In this example, DHCP is enabled, hence the IP, NM, GW and DNS fields are cleared to 0..
271  ip4_addr_set_zero(&IP);
272  ip4_addr_set_zero(&NM);
273  ip4_addr_set_zero(&GW);
274  ip4_addr_set_zero(&DNS);
275 
276  //Initialize the TCP/IP protocol stack.
277  ps2ipInit(&IP, &NM, &GW);
278 
279  //Enable DHCP
280  ethApplyIPConfig(1, &IP, &NM, &GW, &DNS);
281 
282  //Wait for the link to become ready.
283  scr_printf("Waiting for connection...\n");
284  if(ethWaitValidNetIFLinkState() != 0) {
285  scr_printf("Error: failed to get valid link status.\n");
286  goto end;
287  }
288 
289  scr_printf("Waiting for DHCP lease...");
290  //Wait for DHCP to initialize, if DHCP is enabled.
291  if (ethWaitValidDHCPState() != 0)
292  {
293  scr_printf("DHCP failed\n.");
294  goto end;
295  }
296  scr_printf("done!\n");
297 
298  scr_printf("Initialized:\n");
301 
302  //At this point, network support has been initialized and the PS2 can be pinged.
303  SleepThread();
304 
305 end:
306  //To cleanup, just call these functions.
307  ps2ipDeinit();
308  NetManDeinit();
309 
310  //Deinitialize SIF services
311  SifExitRpc();
312 
313  return 0;
314 }
void init_scr(void)
Definition: scr_printf.c:185
void scr_printf(const char *,...)
Definition: scr_printf.c:252
int SifExecModuleBuffer(void *ptr, u32 size, u32 arg_len, const char *args, int *mod_res)
int SifLoadFileInit(void)
int SifIopReset(const char *arg, int mode)
int SifIopSync(void)
int SifInitIopHeap(void)
s32 iWakeupThread(s32 thread_id)
s32 SleepThread(void)
s32 SetAlarm(u16 time, void(*callback)(s32 alarm_id, u16 time, void *common), void *common)
s32 GetThreadId(void)
u32 time
Definition: libmouse.c:37
int NetManIoctl(unsigned int command, void *arg, unsigned int arg_len, void *output, unsigned int length)
Definition: netman.c:109
int NetManInit(void)
Definition: netman.c:43
void NetManDeinit(void)
Definition: netman.c:58
@ NETMAN_NETIF_ETH_LINK_MODE_100M_HDX
Definition: netman.h:60
@ NETMAN_NETIF_ETH_LINK_MODE_10M_FDX
Definition: netman.h:58
@ NETMAN_NETIF_ETH_LINK_MODE_10M_HDX
Definition: netman.h:56
@ NETMAN_NETIF_ETH_LINK_MODE_AUTO
Definition: netman.h:54
@ NETMAN_NETIF_ETH_LINK_MODE_100M_FDX
Definition: netman.h:62
@ NETMAN_NETIF_ETH_LINK_STATE_UP
Definition: netman.h:69
@ NETMAN_NETIF_IOCTL_ETH_GET_LINK_MODE
Definition: netman.h:77
@ NETMAN_NETIF_IOCTL_GET_LINK_STATUS
Definition: netman.h:96
#define NETMAN_NETIF_ETH_LINK_DISABLE_PAUSE
Definition: netman.h:50
t_ip_info ip_info
Definition: ps2ipc.c:37
void dns_setserver(u8 numdns, ip_addr_t *dnsserver)
const ip_addr_t * dns_getserver(u8 numdns)
int NetManSetLinkMode(int mode)
Definition: rpc_client.c:332
s32 mode
Definition: rpc_client.c:15
s32 result
Definition: rpc_client.c:23
int main(int argc, char *argv[])
Definition: ps2ip.c:213
unsigned char NETMAN_irx[]
unsigned int size_DEV9_irx
unsigned char DEV9_irx[]
unsigned char SMAP_irx[]
unsigned int size_SMAP_irx
unsigned int size_NETMAN_irx
static void ethPrintIPConfig(void)
Definition: ps2ip.c:147
static int ethGetDHCPStatus(void)
Definition: ps2ip.c:81
static void EthStatusCheckCb(s32 alarm_id, u16 time, void *common)
Definition: ps2ip.c:48
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)
Definition: ps2ip.c:102
static int ethWaitValidDHCPState(void)
Definition: ps2ip.c:97
static int ethWaitValidNetIFLinkState(void)
Definition: ps2ip.c:76
static int ethGetNetIFLinkStatus(void)
Definition: ps2ip.c:71
static void ethPrintLinkStatus(void)
Definition: ps2ip.c:194
static int WaitValidNetState(int(*checkingFunction)(void))
Definition: ps2ip.c:53
static int ethApplyNetIFConfig(int mode)
Definition: ps2ip.c:32
int sbv_patch_enable_lmb(void)
void SifInitRpc(int mode)
void SifExitRpc(void)
int ps2ipInit(struct ip4_addr *ip_address, struct ip4_addr *subnet_mask, struct ip4_addr *gateway)
Definition: ps2ip.c:316
int ps2ip_getconfig(char *pszName, t_ip_info *pInfo)
Definition: ps2ip.c:41
int ps2ip_setconfig(const t_ip_info *pInfo)
Definition: ps2ip.c:79
void ps2ipDeinit(void)
Definition: ps2ip.c:342
struct in_addr ipaddr
u32 dhcp_enabled
struct in_addr gw
struct in_addr netmask
#define NULL
Definition: tamtypes.h:91
signed int s32
Definition: tamtypes.h:58
unsigned short u16
Definition: tamtypes.h:24
unsigned char u8
Definition: tamtypes.h:23
#define ip_addr_cmp(addr1, addr2)
Definition: tcpip.h:773
#define ip4_addr1(ipaddr)
Definition: tcpip.h:335
#define ip_addr_set(dest, src)
Definition: tcpip.h:764
#define ip4_addr4(ipaddr)
Definition: tcpip.h:338
#define ip4_addr_set_zero(ipaddr)
Definition: tcpip.h:274
@ DHCP_STATE_OFF
Definition: tcpip.h:1260
@ DHCP_STATE_BOUND
Definition: tcpip.h:1270
ip4_addr_t ip_addr_t
Definition: tcpip.h:747
#define ip4_addr3(ipaddr)
Definition: tcpip.h:337
#define ip4_addr2(ipaddr)
Definition: tcpip.h:336