PS2SDK
PS2 Homebrew Libraries
Loading...
Searching...
No Matches
netman.c
1#include <errno.h>
2#include <intrman.h>
3#include <irx.h>
4#include <loadcore.h>
5#include <sysclib.h>
6#include <sysmem.h>
7#include <sifman.h>
8#include <thevent.h>
9#include <thsemap.h>
10#include <netman.h>
11#include <netman_rpc.h>
12
13#include "rpc_client.h"
14#include "rpc_server.h"
15#include "internal.h"
16
17static struct NetManNetProtStack MainNetProtStack;
18static struct NetManNetIF NetIFs[NETMAN_MAX_NETIF_COUNT];
19static struct NetManNetIF *MainNetIF=NULL;
20static unsigned char IsInitialized=0;
21static char NIFLinkState=0;
22static unsigned char NextNetIFID=0;
23static int NetManIOSemaID;
24
25IRX_ID("Network_Manager", 2, 1);
26
27extern struct irx_export_table _exp_netman;
28
29int _start(int argc, char *argv[]){
30 iop_sema_t sema;
31
32 (void)argc;
33 (void)argv;
34
35 if(RegisterLibraryEntries(&_exp_netman) == 0){
36 sema.attr = 0;
37 sema.option = 0;
38 sema.initial = 1;
39 sema.max = 1;
40 NetManIOSemaID = CreateSema(&sema);
41
42 NetmanInitRPCServer();
43
44 return MODULE_RESIDENT_END;
45 }
46
47 return MODULE_NO_RESIDENT_END;
48}
49
50void *malloc(int size){
51 int OldState;
52 void *result;
53
54 CpuSuspendIntr(&OldState);
55 result = AllocSysMemory(ALLOC_FIRST, size, NULL);
56 CpuResumeIntr(OldState);
57
58 return result;
59}
60
61void free(void *buffer){
62 int OldState;
63
64 CpuSuspendIntr(&OldState);
65 FreeSysMemory(buffer);
66 CpuResumeIntr(OldState);
67}
68
69void NetManToggleGlobalNetIFLinkState(unsigned char state){
70 NIFLinkState = state;
71
72 NetManUpdateStackNIFLinkState();
73}
74
75int NetManGetGlobalNetIFLinkState(void){
76 return NIFLinkState;
77}
78
79void NetManUpdateStackNIFLinkState(void){
80 if(IsInitialized){
81 if(NIFLinkState){
82 MainNetProtStack.LinkStateUp();
83 }
84 else{
85 MainNetProtStack.LinkStateDown();
86 }
87 }
88}
89
90static void UpdateNetIFStatus(void){
91 if(!(MainNetIF->flags&NETMAN_NETIF_IN_USE)) MainNetIF = NULL;
92
93 if(MainNetIF == NULL){
94 unsigned char i;
95
96 for(i=0; i<NETMAN_MAX_NETIF_COUNT; i++){
97 if(NetIFs[i].flags&NETMAN_NETIF_IN_USE){
98 MainNetIF=&NetIFs[i];
99 break;
100 }
101 }
102 }
103
104 NetManToggleGlobalNetIFLinkState((MainNetIF!=NULL && (MainNetIF->flags&NETMAN_NETIF_LINK_UP)) ? 1 : 0);
105}
106
107int NetManRegisterNetworkStack(const struct NetManNetProtStack *stack){
108 WaitSema(NetManIOSemaID);
109
110 if(!IsInitialized){
111 memcpy(&MainNetProtStack, stack, sizeof(MainNetProtStack));
112 IsInitialized=1;
113
114 NetManUpdateStackNIFLinkState();
115 }
116
117 SignalSema(NetManIOSemaID);
118
119 return 0;
120}
121
122void NetManUnregisterNetworkStack(void){
123 unsigned int i;
124
125 WaitSema(NetManIOSemaID);
126
127 for(i=0; i<NETMAN_MAX_NETIF_COUNT; i++){
128 if(NetIFs[i].flags&NETMAN_NETIF_IN_USE){
129 NetIFs[i].flags=0;
130 NetIFs[i].deinit();
131 }
132 }
133
134 UpdateNetIFStatus();
135 memset(&MainNetProtStack, 0, sizeof(MainNetProtStack));
136 IsInitialized=0;
137
138 SignalSema(NetManIOSemaID);
139}
140
141void NetManNetIFXmit(void){
142 if(MainNetIF != NULL)
143 MainNetIF->xmit();
144}
145
146int NetManIoctl(unsigned int command, void *args, unsigned int args_len, void *output, unsigned int length){
147 int result;
148
149 WaitSema(NetManIOSemaID);
150
151 if(MainNetIF!=NULL){
152 result=MainNetIF->ioctl(command, args, args_len, output, length);
153 }
154 else result=-1;
155
156 SignalSema(NetManIOSemaID);
157
158 return result;
159}
160
161int NetManNetIFSetLinkMode(int mode){
162 return NetManIoctl(NETMAN_NETIF_IOCTL_ETH_SET_LINK_MODE, &mode, sizeof(mode), NULL, 0);
163}
164
165void *NetManNetProtStackAllocRxPacket(unsigned int length, void **payload){
166 return IsInitialized?MainNetProtStack.AllocRxPacket(length, payload):NULL;
167}
168
169void NetManNetProtStackFreeRxPacket(void *packet){
170 if(IsInitialized)
171 MainNetProtStack.FreeRxPacket(packet);
172}
173
174void NetManNetProtStackEnQRxPacket(void *packet){
175 if(IsInitialized)
176 MainNetProtStack.EnQRxPacket(packet);
177}
178
179int NetManTxPacketNext(void **payload){
180 return IsInitialized?MainNetProtStack.NextTxPacket(payload):0;
181}
182
183void NetManTxPacketDeQ(void){
184 if(IsInitialized)
185 MainNetProtStack.DeQTxPacket();
186}
187
188int NetManRegisterNetIF(struct NetManNetIF *NetIF){
189 unsigned int i;
190 int result;
191 iop_event_t EventFlag;
192
193 WaitSema(NetManIOSemaID);
194
195 for(i=0, result=-ENOMEM; i<NETMAN_MAX_NETIF_COUNT; i++){
196 if(!(NetIFs[i].flags&NETMAN_NETIF_IN_USE)){
197 if(NetIF->init()==0){
198 EventFlag.attr = 0;
199 EventFlag.option = 0;
200 EventFlag.bits = 0;
201 if((result = NetIF->EventFlagID = CreateEventFlag(&EventFlag)) >= 0){
202 memcpy(&NetIFs[i], NetIF, sizeof(NetIFs[i]));
203 NetIFs[i].flags|=NETMAN_NETIF_IN_USE;
204 result=NetIFs[i].id=((unsigned int)NextNetIFID)<<8|i;
205
206 UpdateNetIFStatus();
207 NextNetIFID++;
208 }
209 }
210 else result=-EIO;
211 break;
212 }
213 }
214
215 SignalSema(NetManIOSemaID);
216
217 return result;
218}
219
220void NetManUnregisterNetIF(const char *name){
221 unsigned int i;
222
223 WaitSema(NetManIOSemaID);
224
225 for(i=0; i<NETMAN_MAX_NETIF_COUNT; i++){
226 if((NetIFs[i].flags&NETMAN_NETIF_IN_USE) && strcmp(name, NetIFs[i].name)==0){
227 NetIFs[i].flags=0;
228 NetIFs[i].deinit();
229 DeleteEventFlag(NetIFs[i].EventFlagID);
230 UpdateNetIFStatus();
231 break;
232 }
233 }
234
235 SignalSema(NetManIOSemaID);
236}
237
238void NetManToggleNetIFLinkState(int NetIFID, unsigned char state){
239 WaitSema(NetManIOSemaID);
240
241 if((NetIFID&0xFF)<NETMAN_MAX_NETIF_COUNT && NetIFID==NetIFs[NetIFID&0xFF].id && (NetIFs[NetIFID&0xFF].flags&NETMAN_NETIF_IN_USE)){
242 struct NetManNetIF *pNetIF;
243
244 pNetIF = &NetIFs[NetIFID&0xFF];
245
246 ClearEventFlag(pNetIF->EventFlagID, ~(NETMAN_NETIF_EVF_UP|NETMAN_NETIF_EVF_DOWN));
247
248 if(state){
249 pNetIF->flags|=NETMAN_NETIF_LINK_UP;
250 SetEventFlag(pNetIF->EventFlagID, NETMAN_NETIF_EVF_UP);
251 }
252 else{
253 pNetIF->flags&=~NETMAN_NETIF_LINK_UP;
254 SetEventFlag(pNetIF->EventFlagID, NETMAN_NETIF_EVF_DOWN);
255 }
256
257 UpdateNetIFStatus();
258 }
259
260 SignalSema(NetManIOSemaID);
261}
262
263int NetManSetMainIF(const char *name){
264 int result, i;
265
266 WaitSema(NetManIOSemaID);
267
268 for(i=0,result=-ENXIO; i<NETMAN_MAX_NETIF_COUNT; i++){
269 if((NetIFs[i].flags&NETMAN_NETIF_IN_USE) && strcmp(name, NetIFs[i].name)==0){
270 MainNetIF=&NetIFs[i];
271 result = 0;
272 break;
273 }
274 }
275
276 SignalSema(NetManIOSemaID);
277
278 return result;
279}
280
281int NetManQueryMainIF(char *name){
282 int result;
283
284 WaitSema(NetManIOSemaID);
285
286 if(MainNetIF!=NULL && (MainNetIF->flags&NETMAN_NETIF_IN_USE)){
287 strcpy(name, MainNetIF->name);
288 result = 0;
289 }else result = -ENXIO;
290
291 SignalSema(NetManIOSemaID);
292
293 return result;
294}
295
296int NetManSetLinkMode(int mode){
297 int result, evfid;
298
299 WaitSema(NetManIOSemaID);
300
301 if(MainNetIF!=NULL){
302 evfid = MainNetIF->EventFlagID;
303
304 ClearEventFlag(evfid, ~NETMAN_NETIF_EVF_DOWN);
305 result = MainNetIF->ioctl(NETMAN_NETIF_IOCTL_ETH_SET_LINK_MODE, &mode, sizeof(mode), NULL, 0);
306 }
307 else{
308 evfid = -1;
309 result = -1;
310 }
311
312 SignalSema(NetManIOSemaID);
313
314 if(result == 0 && evfid >= 0)
315 WaitEventFlag(evfid, NETMAN_NETIF_EVF_DOWN, WEF_OR, NULL); //Wait for the IF to go down.
316
317 return result;
318}
#define ENXIO
Definition errno.h:31
#define ENOMEM
Definition errno.h:43
#define EIO
Definition errno.h:29
int CpuResumeIntr(int state)
Definition intrman.c:227
int CpuSuspendIntr(int *state)
Definition intrman.c:205
@ NETMAN_NETIF_IOCTL_ETH_SET_LINK_MODE
Definition netman.h:93
#define NETMAN_NETIF_LINK_UP
Definition netman.h:148
#define NETMAN_NETIF_IN_USE
Definition netman.h:142
short int id
Definition netman.h:155