sdk-core v2026.11.05
PS2 SDK - core tier (build-critical EE/IOP modules)
Loading...
Searching...
No Matches
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
15
16#include <types.h>
17#include <stdio.h>
18#include <intrman.h>
19#include <loadcore.h>
20#include <thbase.h>
21#include <sysclib.h>
22#include <thevent.h>
23#include <sysmem.h>
24#include <lwip/memp.h>
25
26#include "lwip/sys.h"
27#include "lwip/tcp.h"
28#include "lwip/tcpip.h"
29#include "lwip/prot/dhcp.h"
30#include "lwip/netif.h"
31#include "lwip/dhcp.h"
32#include "lwip/inet.h"
33#include "netif/etharp.h"
34
35#include "ps2ip_internal.h"
36
37/* lwIP 2.2.1's sockets.c writes errno via set_errno(); the IOP IRX has no
38 libc-provided errno storage, so we define it here. The ".data" section
39 name (with leading dot) is critical: a bare "data" attribute creates a
40 separate section that the IRX loader never allocates into IOP RAM, so
41 every set_errno() write would corrupt random memory. */
42int errno __attribute__((section(".data")));
43
44typedef struct pbuf PBuf;
45typedef struct netif NetIF;
46typedef struct ip4_addr IPAddr;
47
48#define MODNAME "TCP/IP Stack"
49IRX_ID(MODNAME, 2, 3);
50
51extern struct irx_export_table _exp_ps2ip;
52
53#if NOSYS
54static int iTimerARP=0;
55
56#if defined(PS2IP_DHCP)
57static int iTimerDHCP=0;
58#endif //defined(PS2IP_DHCP)
59#endif
60
61int
62ps2ip_getconfig(char* pszName,t_ip_info* pInfo)
63{
64 NetIF* pNetIF=netif_find(pszName);
65
66 if (pNetIF==NULL)
67 {
68
69 //Net interface not found.
70
71 memset(pInfo,0,sizeof(*pInfo));
72 return 0;
73 }
74 strcpy(pInfo->netif_name,pszName);
75 pInfo->ipaddr.s_addr=pNetIF->ip_addr.addr;
76 pInfo->netmask.s_addr=pNetIF->netmask.addr;
77 pInfo->gw.s_addr=pNetIF->gw.addr;
78
79 memcpy(pInfo->hw_addr,pNetIF->hwaddr,sizeof(pInfo->hw_addr));
80
81#if LWIP_DHCP
82 struct dhcp *dhcp = netif_dhcp_data(pNetIF);
83
84 if ((dhcp != NULL) && (dhcp->state != DHCP_STATE_OFF))
85 {
86 pInfo->dhcp_enabled=1;
87 pInfo->dhcp_status=dhcp->state;
88 }
89 else
90 {
91 pInfo->dhcp_enabled=0;
92 pInfo->dhcp_status=DHCP_STATE_OFF;
93 }
94
95#else
96
97 pInfo->dhcp_enabled=0;
98
99#endif
100
101 return 1;
102}
103
104
105int
106ps2ip_setconfig(const t_ip_info* pInfo)
107{
108 NetIF* pNetIF=netif_find(pInfo->netif_name);
109
110 if (pNetIF==NULL)
111 {
112 return 0;
113 }
114
115#if LWIP_DHCP
116 struct dhcp *dhcp = netif_dhcp_data(pNetIF);
117
118 //Enable dhcp here
119
120 if (pInfo->dhcp_enabled)
121 {
122 if ((dhcp == NULL) || (dhcp->state == DHCP_STATE_OFF))
123 {
124 //Set initial IP address
125 netif_set_ipaddr(pNetIF,(const IPAddr*)&pInfo->ipaddr);
126 netif_set_netmask(pNetIF,(const IPAddr*)&pInfo->netmask);
127 netif_set_gw(pNetIF,(const IPAddr*)&pInfo->gw);
128
129 //Start DHCP client
130 dhcp_start(pNetIF);
131 }
132 }
133 else
134 {
135 if ((dhcp != NULL) && (dhcp->state != DHCP_STATE_OFF))
136 {
137 //Release DHCP lease
138 dhcp_release(pNetIF);
139
140 //Stop DHCP client
141 dhcp_stop(pNetIF);
142 }
143
144 netif_set_ipaddr(pNetIF,(const IPAddr*)&pInfo->ipaddr);
145 netif_set_netmask(pNetIF,(const IPAddr*)&pInfo->netmask);
146 netif_set_gw(pNetIF,(const IPAddr*)&pInfo->gw);
147 }
148#else
149 netif_set_ipaddr(pNetIF,(const IPAddr*)&pInfo->ipaddr);
150 netif_set_netmask(pNetIF,(const IPAddr*)&pInfo->netmask);
151 netif_set_gw(pNetIF,(const IPAddr*)&pInfo->gw);
152#endif
153
154 return 1;
155}
156
157static void InitDone(void* pvArg)
158{
159 dbgprintf("InitDone: TCPIP initialized\n");
160 sys_sem_signal((sys_sem_t*)pvArg);
161}
162
163#if NOSYS
164static void TimerThread(void* pvArg)
165{
166 while (1)
167 {
168 //TCP timer.
169 tcp_tmr();
170
171 //ARP timer.
172 iTimerARP+=TCP_TMR_INTERVAL;
173 if (iTimerARP>=ARP_TMR_INTERVAL)
174 {
175 iTimerARP-=ARP_TMR_INTERVAL;
176 etharp_tmr();
177 }
178
179#if defined(PS2IP_DHCP)
180
181 //DHCP timer.
182
183 iTimerDHCP+=TCP_TMR_INTERVAL;
184 if ((iTimerDHCP-TCP_TMR_INTERVAL)/DHCP_FINE_TIMER_MSECS!=iTimerDHCP/DHCP_FINE_TIMER_MSECS)
185 {
186 dhcp_fine_tmr();
187 }
188
189 if (iTimerDHCP>=DHCP_COARSE_TIMER_SECS*1000)
190 {
191 iTimerDHCP-=DHCP_COARSE_TIMER_SECS*1000;
192 dhcp_coarse_tmr();
193 }
194#endif
195
196 DelayThread(TCP_TMR_INTERVAL*250);
197 }
198}
199
200static inline void InitTimer(void)
201{
202 iop_thread_t Thread={TH_C, (u32)"PS2IP-timer", TimerThread, 0x300, 0x16};
203 int iTimerThreadID=CreateThread(&Thread);
204
205 if (iTimerThreadID<0)
206 {
207 printf("InitTimer: Fatal error - Failed to create tcpip timer-thread!\n");
208 }
209
210 //Start timer-thread
211 StartThread(iTimerThreadID, NULL);
212}
213#endif
214
215err_t
216ps2ip_input(PBuf* pInput,NetIF* pNetIF)
217{
218 err_t result;
219
220 if((result = pNetIF->input(pInput, pNetIF)) != ERR_OK)
221 pbuf_free(pInput);
222
223 return result;
224}
225
226int _exit(int argc, char *argv[])
227{
228 (void)argc;
229 (void)argv;
230
231 return MODULE_NO_RESIDENT_END; // return "not resident"!
232}
233
234int _start(int argc, char *argv[]){
235 sys_sem_t Sema;
236 int result;
237
238 (void)argc;
239 (void)argv;
240
241 dbgprintf("PS2IP: Module Loaded.\n");
242
243 if ((result = RegisterLibraryEntries(&_exp_ps2ip)) != 0)
244 {
245 printf("PS2IP: RegisterLibraryEntries returned: %d\n", result);
246 return MODULE_NO_RESIDENT_END;
247 } else {
248 sys_sem_new(&Sema, 0);
249 dbgprintf("PS2IP: Calling tcpip_init\n");
250 tcpip_init(InitDone,&Sema);
251
252 sys_arch_sem_wait(&Sema, 0);
253 sys_sem_free(&Sema);
254
255 dbgprintf("PS2IP: tcpip_init called\n");
256#if NOSYS
257 InitTimer();
258#endif
259
260 dbgprintf("PS2IP: System Initialised\n");
261 }
262
263 return MODULE_RESIDENT_END;
264}
void tcpip_init(tcpip_init_done_fn tcpip_init_done, void *arg)
Definition tcpip.c:659
@ ERR_OK
Definition err.h:55
struct netif * netif_find(const char *name)
Definition netif.c:1755
u8 pbuf_free(struct pbuf *p)
Definition pbuf.c:729
void sys_sem_free(sys_sem_t *sem)
Definition sys_arch.c:334
u32_t sys_arch_sem_wait(sys_sem_t *sem, u32_t timeout)
Definition sys_arch.c:289
void sys_sem_signal(sys_sem_t *sem)
Definition sys_arch.c:329
err_t sys_sem_new(sys_sem_t *sem, u8_t count)
Definition sys_arch.c:263
netif_input_fn input
Definition tcpip.h:1290
ip_addr_t ip_addr
Definition tcpip.h:1277
u8 hwaddr[NETIF_MAX_HWADDR_LEN]
Definition tcpip.h:1349
Definition tcpip.h:234