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 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 81 of file ps2ip.c.

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 }
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().

◆ 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 126 of file ps2ip.c.

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 }
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 173 of file ps2ip.c.

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 }
@ 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().

◆ ethWaitValidNetIFLinkState()

static int ethWaitValidNetIFLinkState ( void  )
static

Definition at line 76 of file ps2ip.c.

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

References ethGetNetIFLinkStatus(), and WaitValidNetState().

Referenced by main().

◆ main()

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

Definition at line 213 of file ps2ip.c.

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
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
static void ethPrintIPConfig(void)
Definition: ps2ip.c:126
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 void ethPrintLinkStatus(void)
Definition: ps2ip.c:173
unsigned int size_NETMAN_irx
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(ipaddr, a, b, c, d)
Definition: tcpip.h:254

References DEV9_irx, dns_setserver(), ethApplyIPConfig(), ethApplyNetIFConfig(), ethPrintIPConfig(), ethPrintLinkStatus(), ethWaitValidNetIFLinkState(), init_scr(), IP4_ADDR, 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 ethWaitValidNetIFLinkState().

Variable Documentation

◆ DEV9_irx

unsigned char DEV9_irx[]
extern

Referenced by main().

◆ NETMAN_irx

unsigned char NETMAN_irx[]
extern

Referenced by main().

◆ size_DEV9_irx

unsigned int size_DEV9_irx
extern

Referenced by main().

◆ size_NETMAN_irx

unsigned int size_NETMAN_irx
extern

Referenced by main().

◆ size_SMAP_irx

unsigned int size_SMAP_irx
extern

Referenced by main().

◆ SMAP_irx

unsigned char SMAP_irx[]
extern

Referenced by main().