ps2sdk  1.1
A collection of Open Source libraries used for developing applications on Sony's PlayStation 2® (PS2).
ps2ip.c File Reference
#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>
+ Include dependency graph for ps2ip.c:

Go to the source code of this file.

Functions

static int ethApplyNetIFConfig (int mode)
 
static void EthStatusCheckCb (s32 alarm_id, u16 time, void *common)
 
static int WaitValidNetState (int(*checkingFunction)(void))
 
static int ethGetNetIFLinkStatus (void)
 
static int ethWaitValidNetIFLinkState (void)
 
static int ethGetDHCPStatus (void)
 
static int ethWaitValidDHCPState (void)
 
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)
 
static void ethPrintIPConfig (void)
 
static void ethPrintLinkStatus (void)
 
int main (int argc, char *argv[])
 

Variables

unsigned char DEV9_irx []
 
unsigned int size_DEV9_irx
 
unsigned char SMAP_irx []
 
unsigned int size_SMAP_irx
 
unsigned char NETMAN_irx []
 
unsigned int size_NETMAN_irx
 

Function Documentation

◆ ethApplyIPConfig()

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 
)
static

Definition at line 102 of file ps2ip.c.

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 }
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)
s32 result
Definition: rpc_client.c:23
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
struct in_addr ipaddr
u32 dhcp_enabled
struct in_addr gw
struct in_addr netmask
#define ip_addr_cmp(addr1, addr2)
Definition: tcpip.h:773
#define ip_addr_set(dest, src)
Definition: tcpip.h:764
ip4_addr_t ip_addr_t
Definition: tcpip.h:747

References t_ip_info::dhcp_enabled, dns_getserver(), dns_setserver(), t_ip_info::gw, ip_addr_cmp, ip_addr_set, ip_info, t_ip_info::ipaddr, t_ip_info::netmask, ps2ip_getconfig(), ps2ip_setconfig(), and result.

Referenced by main().

◆ ethApplyNetIFConfig()

static int ethApplyNetIFConfig ( int  mode)
static

Definition at line 32 of file ps2ip.c.

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 }
@ NETMAN_NETIF_ETH_LINK_MODE_AUTO
Definition: netman.h:54
int NetManSetLinkMode(int mode)
Definition: rpc_client.c:332
s32 mode
Definition: rpc_client.c:15

References mode, NETMAN_NETIF_ETH_LINK_MODE_AUTO, NetManSetLinkMode(), and result.

Referenced by main().

◆ ethGetDHCPStatus()

static int ethGetDHCPStatus ( void  )
static

Definition at line 81 of file ps2ip.c.

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 }
@ DHCP_STATE_OFF
Definition: tcpip.h:1260
@ DHCP_STATE_BOUND
Definition: tcpip.h:1270

References t_ip_info::dhcp_enabled, DHCP_STATE_BOUND, DHCP_STATE_OFF, t_ip_info::dhcp_status, ip_info, ps2ip_getconfig(), and result.

Referenced by ethWaitValidDHCPState().

◆ ethGetNetIFLinkStatus()

static int ethGetNetIFLinkStatus ( void  )
static

Definition at line 71 of file ps2ip.c.

72 {
74 }
int NetManIoctl(unsigned int command, void *arg, unsigned int arg_len, void *output, unsigned int length)
Definition: netman.c:109
@ NETMAN_NETIF_ETH_LINK_STATE_UP
Definition: netman.h:69
@ NETMAN_NETIF_IOCTL_GET_LINK_STATUS
Definition: netman.h:96
#define NULL
Definition: tamtypes.h:91

References NETMAN_NETIF_ETH_LINK_STATE_UP, NETMAN_NETIF_IOCTL_GET_LINK_STATUS, NetManIoctl(), and NULL.

Referenced by ethWaitValidNetIFLinkState().

◆ ethPrintIPConfig()

static void ethPrintIPConfig ( void  )
static

Definition at line 147 of file ps2ip.c.

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 }
void scr_printf(const char *,...)
Definition: scr_printf.c:252
unsigned char u8
Definition: tamtypes.h:23
#define ip4_addr1(ipaddr)
Definition: tcpip.h:335
#define ip4_addr4(ipaddr)
Definition: tcpip.h:338
#define ip4_addr3(ipaddr)
Definition: tcpip.h:337
#define ip4_addr2(ipaddr)
Definition: tcpip.h:336

References dns_getserver(), t_ip_info::gw, ip4_addr1, ip4_addr2, ip4_addr3, ip4_addr4, ip_info, t_ip_info::ipaddr, t_ip_info::netmask, ps2ip_getconfig(), and scr_printf().

Referenced by main().

◆ ethPrintLinkStatus()

static void ethPrintLinkStatus ( void  )
static

Definition at line 194 of file ps2ip.c.

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 }
@ 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_100M_FDX
Definition: netman.h:62
@ NETMAN_NETIF_IOCTL_ETH_GET_LINK_MODE
Definition: netman.h:77
#define NETMAN_NETIF_ETH_LINK_DISABLE_PAUSE
Definition: netman.h:50

References mode, NETMAN_NETIF_ETH_LINK_DISABLE_PAUSE, NETMAN_NETIF_ETH_LINK_MODE_100M_FDX, NETMAN_NETIF_ETH_LINK_MODE_100M_HDX, NETMAN_NETIF_ETH_LINK_MODE_10M_FDX, NETMAN_NETIF_ETH_LINK_MODE_10M_HDX, NETMAN_NETIF_ETH_LINK_STATE_UP, NETMAN_NETIF_IOCTL_ETH_GET_LINK_MODE, NETMAN_NETIF_IOCTL_GET_LINK_STATUS, NetManIoctl(), NULL, and scr_printf().

Referenced by main().

◆ EthStatusCheckCb()

static void EthStatusCheckCb ( s32  alarm_id,
u16  time,
void *  common 
)
static

Definition at line 48 of file ps2ip.c.

49 {
50  iWakeupThread(*(int*)common);
51 }
s32 iWakeupThread(s32 thread_id)

References iWakeupThread().

Referenced by WaitValidNetState().

◆ ethWaitValidDHCPState()

static int ethWaitValidDHCPState ( void  )
static

Definition at line 97 of file ps2ip.c.

98 {
100 }
static int ethGetDHCPStatus(void)
Definition: ps2ip.c:81
static int WaitValidNetState(int(*checkingFunction)(void))
Definition: ps2ip.c:53

References ethGetDHCPStatus(), and WaitValidNetState().

Referenced by main().

◆ ethWaitValidNetIFLinkState()

static int ethWaitValidNetIFLinkState ( void  )
static

Definition at line 76 of file ps2ip.c.

77 {
79 }
static int ethGetNetIFLinkStatus(void)
Definition: ps2ip.c:71

References ethGetNetIFLinkStatus(), and WaitValidNetState().

Referenced by main().

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 234 of file ps2ip.c.

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
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 SleepThread(void)
int NetManInit(void)
Definition: netman.c:43
void NetManDeinit(void)
Definition: netman.c:58
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 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 void ethPrintLinkStatus(void)
Definition: ps2ip.c:194
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
void ps2ipDeinit(void)
Definition: ps2ip.c:342
#define ip4_addr_set_zero(ipaddr)
Definition: tcpip.h:274

References DEV9_irx, ethApplyIPConfig(), ethApplyNetIFConfig(), ethPrintIPConfig(), ethPrintLinkStatus(), ethWaitValidDHCPState(), ethWaitValidNetIFLinkState(), init_scr(), ip4_addr_set_zero, NETMAN_irx, NETMAN_NETIF_ETH_LINK_MODE_AUTO, NetManDeinit(), NetManInit(), NULL, ps2ipDeinit(), ps2ipInit(), sbv_patch_enable_lmb(), scr_printf(), SifExecModuleBuffer(), SifExitRpc(), SifInitIopHeap(), SifInitRpc(), SifIopReset(), SifIopSync(), SifLoadFileInit(), size_DEV9_irx, size_NETMAN_irx, size_SMAP_irx, SleepThread(), and SMAP_irx.

◆ WaitValidNetState()

static int WaitValidNetState ( int(*)(void)  checkingFunction)
static

Definition at line 53 of file ps2ip.c.

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 }
s32 SetAlarm(u16 time, void(*callback)(s32 alarm_id, u16 time, void *common), void *common)
s32 GetThreadId(void)
static void EthStatusCheckCb(s32 alarm_id, u16 time, void *common)
Definition: ps2ip.c:48

References EthStatusCheckCb(), GetThreadId(), SetAlarm(), and SleepThread().

Referenced by ethWaitValidDHCPState(), and ethWaitValidNetIFLinkState().

Variable Documentation

◆ DEV9_irx

unsigned char DEV9_irx[]
extern

◆ NETMAN_irx

unsigned char NETMAN_irx[]
extern

◆ size_DEV9_irx

unsigned int size_DEV9_irx
extern

◆ size_NETMAN_irx

unsigned int size_NETMAN_irx
extern

◆ size_SMAP_irx

unsigned int size_SMAP_irx
extern

◆ SMAP_irx

unsigned char SMAP_irx[]
extern