PS2SDK
PS2 Homebrew Libraries
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
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
37typedef struct pbuf PBuf;
38typedef struct netif NetIF;
39typedef struct ip4_addr IPAddr;
40
41#define MODNAME "TCP/IP Stack"
42IRX_ID(MODNAME, 2, 3);
43
44extern struct irx_export_table _exp_ps2ip;
45
46#if NOSYS
47static int iTimerARP=0;
48
49#if defined(PS2IP_DHCP)
50static int iTimerDHCP=0;
51#endif //defined(PS2IP_DHCP)
52#endif
53
54int
55ps2ip_getconfig(char* pszName,t_ip_info* pInfo)
56{
57 NetIF* pNetIF=netif_find(pszName);
58
59 if (pNetIF==NULL)
60 {
61
62 //Net interface not found.
63
64 memset(pInfo,0,sizeof(*pInfo));
65 return 0;
66 }
67 strcpy(pInfo->netif_name,pszName);
68 pInfo->ipaddr.s_addr=pNetIF->ip_addr.addr;
69 pInfo->netmask.s_addr=pNetIF->netmask.addr;
70 pInfo->gw.s_addr=pNetIF->gw.addr;
71
72 memcpy(pInfo->hw_addr,pNetIF->hwaddr,sizeof(pInfo->hw_addr));
73
74#if LWIP_DHCP
75 struct dhcp *dhcp = netif_dhcp_data(pNetIF);
76
77 if ((dhcp != NULL) && (dhcp->state != DHCP_STATE_OFF))
78 {
79 pInfo->dhcp_enabled=1;
80 pInfo->dhcp_status=dhcp->state;
81 }
82 else
83 {
84 pInfo->dhcp_enabled=0;
85 pInfo->dhcp_status=DHCP_STATE_OFF;
86 }
87
88#else
89
90 pInfo->dhcp_enabled=0;
91
92#endif
93
94 return 1;
95}
96
97
98int
99ps2ip_setconfig(const t_ip_info* pInfo)
100{
101 NetIF* pNetIF=netif_find(pInfo->netif_name);
102
103 if (pNetIF==NULL)
104 {
105 return 0;
106 }
107
108#if LWIP_DHCP
109 struct dhcp *dhcp = netif_dhcp_data(pNetIF);
110
111 //Enable dhcp here
112
113 if (pInfo->dhcp_enabled)
114 {
115 if ((dhcp == NULL) || (dhcp->state == DHCP_STATE_OFF))
116 {
117 //Set initial IP address
118 netif_set_ipaddr(pNetIF,(const IPAddr*)&pInfo->ipaddr);
119 netif_set_netmask(pNetIF,(const IPAddr*)&pInfo->netmask);
120 netif_set_gw(pNetIF,(const IPAddr*)&pInfo->gw);
121
122 //Start DHCP client
123 dhcp_start(pNetIF);
124 }
125 }
126 else
127 {
128 if ((dhcp != NULL) && (dhcp->state != DHCP_STATE_OFF))
129 {
130 //Release DHCP lease
131 dhcp_release(pNetIF);
132
133 //Stop DHCP client
134 dhcp_stop(pNetIF);
135 }
136
137 netif_set_ipaddr(pNetIF,(const IPAddr*)&pInfo->ipaddr);
138 netif_set_netmask(pNetIF,(const IPAddr*)&pInfo->netmask);
139 netif_set_gw(pNetIF,(const IPAddr*)&pInfo->gw);
140 }
141#else
142 netif_set_ipaddr(pNetIF,(const IPAddr*)&pInfo->ipaddr);
143 netif_set_netmask(pNetIF,(const IPAddr*)&pInfo->netmask);
144 netif_set_gw(pNetIF,(const IPAddr*)&pInfo->gw);
145#endif
146
147 return 1;
148}
149
150static void InitDone(void* pvArg)
151{
152 dbgprintf("InitDone: TCPIP initialized\n");
153 sys_sem_signal((sys_sem_t*)pvArg);
154}
155
156#if NOSYS
157static void TimerThread(void* pvArg)
158{
159 while (1)
160 {
161 //TCP timer.
162 tcp_tmr();
163
164 //ARP timer.
165 iTimerARP+=TCP_TMR_INTERVAL;
166 if (iTimerARP>=ARP_TMR_INTERVAL)
167 {
168 iTimerARP-=ARP_TMR_INTERVAL;
169 etharp_tmr();
170 }
171
172#if defined(PS2IP_DHCP)
173
174 //DHCP timer.
175
176 iTimerDHCP+=TCP_TMR_INTERVAL;
177 if ((iTimerDHCP-TCP_TMR_INTERVAL)/DHCP_FINE_TIMER_MSECS!=iTimerDHCP/DHCP_FINE_TIMER_MSECS)
178 {
179 dhcp_fine_tmr();
180 }
181
182 if (iTimerDHCP>=DHCP_COARSE_TIMER_SECS*1000)
183 {
184 iTimerDHCP-=DHCP_COARSE_TIMER_SECS*1000;
185 dhcp_coarse_tmr();
186 }
187#endif
188
189 DelayThread(TCP_TMR_INTERVAL*250);
190 }
191}
192
193static inline void InitTimer(void)
194{
195 iop_thread_t Thread={TH_C, (u32)"PS2IP-timer", TimerThread, 0x300, 0x16};
196 int iTimerThreadID=CreateThread(&Thread);
197
198 if (iTimerThreadID<0)
199 {
200 printf("InitTimer: Fatal error - Failed to create tcpip timer-thread!\n");
201 }
202
203 //Start timer-thread
204 StartThread(iTimerThreadID, NULL);
205}
206#endif
207
208err_t
209ps2ip_input(PBuf* pInput,NetIF* pNetIF)
210{
211 err_t result;
212
213 if((result = pNetIF->input(pInput, pNetIF)) != ERR_OK)
214 pbuf_free(pInput);
215
216 return result;
217}
218
219int _exit(int argc, char *argv[])
220{
221 (void)argc;
222 (void)argv;
223
224 return MODULE_NO_RESIDENT_END; // return "not resident"!
225}
226
227int _start(int argc, char *argv[]){
228 sys_sem_t Sema;
229 int result;
230
231 (void)argc;
232 (void)argv;
233
234 dbgprintf("PS2IP: Module Loaded.\n");
235
236 if ((result = RegisterLibraryEntries(&_exp_ps2ip)) != 0)
237 {
238 printf("PS2IP: RegisterLibraryEntries returned: %d\n", result);
239 return MODULE_NO_RESIDENT_END;
240 } else {
241 sys_sem_new(&Sema, 0);
242 dbgprintf("PS2IP: Calling tcpip_init\n");
243 tcpip_init(InitDone,&Sema);
244
245 sys_arch_sem_wait(&Sema, 0);
246 sys_sem_free(&Sema);
247
248 dbgprintf("PS2IP: tcpip_init called\n");
249#if NOSYS
250 InitTimer();
251#endif
252
253 dbgprintf("PS2IP: System Initialised\n");
254 }
255
256 return MODULE_RESIDENT_END;
257}
struct netif * netif_find(const char *name)
netif_input_fn input
Definition tcpip.h:1252
ip_addr_t ip_addr
Definition tcpip.h:1239
u8 hwaddr[NETIF_MAX_HWADDR_LEN]
Definition tcpip.h:1311
Definition tcpip.h:200