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 ethApplyIPConfig(int use_dhcp, const struct ip4_addr *ip, const struct ip4_addr *netmask, const struct ip4_addr *gateway, const struct ip4_addr *dns)
82 {
84  const ip_addr_t *dns_curr;
85  int result;
86 
87  //SMAP is registered as the "sm0" device to the TCP/IP stack.
88  if ((result = ps2ip_getconfig("sm0", &ip_info)) >= 0)
89  {
90  //Obtain the current DNS server settings.
91  dns_curr = dns_getserver(0);
92 
93  //Check if it's the same. Otherwise, apply the new configuration.
94  if ((use_dhcp != ip_info.dhcp_enabled)
95  || (!use_dhcp &&
96  (!ip_addr_cmp(ip, (struct ip4_addr *)&ip_info.ipaddr) ||
97  !ip_addr_cmp(netmask, (struct ip4_addr *)&ip_info.netmask) ||
98  !ip_addr_cmp(gateway, (struct ip4_addr *)&ip_info.gw) ||
99  !ip_addr_cmp(dns, dns_curr))))
100  {
101  if (use_dhcp)
102  {
103  ip_info.dhcp_enabled = 1;
104  }
105  else
106  { //Copy over new settings if DHCP is not used.
107  ip_addr_set((struct ip4_addr *)&ip_info.ipaddr, ip);
108  ip_addr_set((struct ip4_addr *)&ip_info.netmask, netmask);
109  ip_addr_set((struct ip4_addr *)&ip_info.gw, gateway);
110 
111  ip_info.dhcp_enabled = 0;
112  }
113 
114  //Update settings.
116  if (!use_dhcp)
117  dns_setserver(0, dns);
118  }
119  else
120  result = 0;
121  }
122 
123  return result;
124 }
125 
126 static void ethPrintIPConfig(void)
127 {
129  const ip_addr_t *dns_curr;
130  u8 ip_address[4], netmask[4], gateway[4], dns[4];
131 
132  //SMAP is registered as the "sm0" device to the TCP/IP stack.
133  if (ps2ip_getconfig("sm0", &ip_info) >= 0)
134  {
135  //Obtain the current DNS server settings.
136  dns_curr = dns_getserver(0);
137 
138  ip_address[0] = ip4_addr1((struct ip4_addr *)&ip_info.ipaddr);
139  ip_address[1] = ip4_addr2((struct ip4_addr *)&ip_info.ipaddr);
140  ip_address[2] = ip4_addr3((struct ip4_addr *)&ip_info.ipaddr);
141  ip_address[3] = ip4_addr4((struct ip4_addr *)&ip_info.ipaddr);
142 
143  netmask[0] = ip4_addr1((struct ip4_addr *)&ip_info.netmask);
144  netmask[1] = ip4_addr2((struct ip4_addr *)&ip_info.netmask);
145  netmask[2] = ip4_addr3((struct ip4_addr *)&ip_info.netmask);
146  netmask[3] = ip4_addr4((struct ip4_addr *)&ip_info.netmask);
147 
148  gateway[0] = ip4_addr1((struct ip4_addr *)&ip_info.gw);
149  gateway[1] = ip4_addr2((struct ip4_addr *)&ip_info.gw);
150  gateway[2] = ip4_addr3((struct ip4_addr *)&ip_info.gw);
151  gateway[3] = ip4_addr4((struct ip4_addr *)&ip_info.gw);
152 
153  dns[0] = ip4_addr1(dns_curr);
154  dns[1] = ip4_addr2(dns_curr);
155  dns[2] = ip4_addr3(dns_curr);
156  dns[3] = ip4_addr4(dns_curr);
157 
158  scr_printf( "IP:\t%d.%d.%d.%d\n"
159  "NM:\t%d.%d.%d.%d\n"
160  "GW:\t%d.%d.%d.%d\n"
161  "DNS:\t%d.%d.%d.%d\n",
162  ip_address[0], ip_address[1], ip_address[2], ip_address[3],
163  netmask[0], netmask[1], netmask[2], netmask[3],
164  gateway[0], gateway[1], gateway[2], gateway[3],
165  dns[0], dns[1], dns[2], dns[3]);
166  }
167  else
168  {
169  scr_printf("Unable to read IP address.\n");
170  }
171 }
172 
173 static void ethPrintLinkStatus(void)
174 {
175  int mode, baseMode;
176 
177  //SMAP is registered as the "sm0" device to the TCP/IP stack.
178  scr_printf("Link:\t");
180  scr_printf("Up\n");
181  else
182  scr_printf("Down\n");
183 
184  scr_printf("Mode:\t");
186 
187  //NETMAN_NETIF_ETH_LINK_MODE_PAUSE is a flag, so file it off first.
189  switch(baseMode)
190  {
192  scr_printf("10M HDX");
193  break;
195  scr_printf("10M FDX");
196  break;
198  scr_printf("100M HDX");
199  break;
201  scr_printf("100M FDX");
202  break;
203  default:
204  scr_printf("Unknown");
205  }
207  scr_printf(" with ");
208  else
209  scr_printf(" without ");
210  scr_printf("Flow Control\n");
211 }
212 
213 int main(int argc, char *argv[])
214 {
215  struct ip4_addr IP, NM, GW, DNS;
216  int EthernetLinkMode;
217 
218  //Reboot IOP
219  SifInitRpc(0);
220  while(!SifIopReset("", 0)){};
221  while(!SifIopSync()){};
222 
223  //Initialize SIF services
224  SifInitRpc(0);
225  SifLoadFileInit();
226  SifInitIopHeap();
228 
229  //Load modules
233 
234  //Initialize NETMAN
235  NetManInit();
236 
237  init_scr();
238 
239  //The network interface link mode/duplex can be set.
240  EthernetLinkMode = NETMAN_NETIF_ETH_LINK_MODE_AUTO;
241 
242  //Attempt to apply the new link setting.
243  if(ethApplyNetIFConfig(EthernetLinkMode) != 0) {
244  scr_printf("Error: failed to set link mode.\n");
245  goto end;
246  }
247 
248  //Initialize IP address.
249  IP4_ADDR(&IP, 192, 168, 0, 80);
250  IP4_ADDR(&NM, 255, 255, 255, 0);
251  IP4_ADDR(&GW, 192, 168, 0, 1);
252  //DNS is not required if the DNS service is not used, but this demo will show how it is done.
253  IP4_ADDR(&DNS, 192, 168, 0, 1);
254 
255  //Initialize the TCP/IP protocol stack.
256  ps2ipInit(&IP, &NM, &GW);
257  dns_setserver(0, &DNS); //Set DNS server
258 
259  //Change IP address
260  IP4_ADDR(&IP, 192, 168, 0, 10);
261  ethApplyIPConfig(0, &IP, &NM, &GW, &DNS);
262 
263  //Wait for the link to become ready.
264  scr_printf("Waiting for connection...\n");
265  if(ethWaitValidNetIFLinkState() != 0) {
266  scr_printf("Error: failed to get valid link status.\n");
267  goto end;
268  }
269 
270  scr_printf("Initialized:\n");
273 
274  //At this point, network support has been initialized and the PS2 can be pinged.
275  SleepThread();
276 
277 end:
278  //To cleanup, just call these functions.
279  ps2ipDeinit();
280  NetManDeinit();
281 
282  //Deinitialize SIF services
283  SifExitRpc();
284 
285  return 0;
286 }
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
static void ethPrintIPConfig(void)
Definition: ps2ip.c:126
static void EthStatusCheckCb(s32 alarm_id, u16 time, void *common)
Definition: ps2ip.c:48
unsigned char DEV9_irx[]
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:81
unsigned char SMAP_irx[]
static int ethWaitValidNetIFLinkState(void)
Definition: ps2ip.c:76
unsigned int size_SMAP_irx
static int ethGetNetIFLinkStatus(void)
Definition: ps2ip.c:71
static void ethPrintLinkStatus(void)
Definition: ps2ip.c:173
unsigned int size_NETMAN_irx
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_ADDR(ipaddr, a, b, c, d)
Definition: tcpip.h:254
#define ip4_addr4(ipaddr)
Definition: tcpip.h:338
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