PS2SDK
PS2 Homebrew Libraries
Loading...
Searching...
No Matches
netman.c
1#include <errno.h>
2#include <string.h>
3#include <kernel.h>
4#include <netman.h>
5#include <netman_rpc.h>
6
7#include "internal.h"
8#include "rpc_client.h"
9#include "rpc_server.h"
10
11static struct NetManNetProtStack MainNetProtStack;
12static unsigned char IsInitialized=0, IsNetStackInitialized=0;
13static char NIFLinkState = 0;
14
15/* Upon stack registration on the IOP side, this function will be called.
16 But the network stack won't be updated then because the RPC wouldn't have been completely initialized,
17 which would prevent the stack from performing some actions like sending Gratuitous Arp packets.
18
19 The NIF status updated will be sent again when the RPC is fully initialized, as shown in NetManRegisterNetworkStack(). */
20void NetManToggleGlobalNetIFLinkState(unsigned char state)
21{
22 NIFLinkState = state;
23
24 NetManUpdateStackNIFLinkState();
25}
26
27int NetManGetGlobalNetIFLinkState(void)
28{
29 return NIFLinkState;
30}
31
32void NetManUpdateStackNIFLinkState(void)
33{
34 if(IsNetStackInitialized)
35 {
36 if(NIFLinkState)
37 MainNetProtStack.LinkStateUp();
38 else
39 MainNetProtStack.LinkStateDown();
40 }
41}
42
43int NetManInit(void)
44{
45 int result;
46
47 if(!IsInitialized)
48 {
49 if((result=NetManInitRPCServer())==0)
50 {
51 if((result=NetManInitRPCClient())==0) IsInitialized = 1;
52 }
53 }else result = 0;
54
55 return result;
56}
57
58void NetManDeinit(void)
59{
60 if(IsInitialized)
61 {
62 NetManUnregisterNetworkStack();
63
64 NetManDeinitRPCClient();
65 NetManDeinitRPCServer();
66 IsInitialized = 0;
67 }
68}
69
70int NetManRegisterNetworkStack(const struct NetManNetProtStack *stack)
71{
72 int result;
73
74 if((result=NetManInit())==0)
75 {
76 if(!IsNetStackInitialized)
77 {
78 if((result=NetManRPCRegisterNetworkStack())==0)
79 {
80 memcpy(&MainNetProtStack, stack, sizeof(MainNetProtStack));
81 IsNetStackInitialized=1;
82 if((result=NetManRPCAllocRxBuffers()) == 0)
83 NetManUpdateStackNIFLinkState();
84 }
85 }
86 else result=0;
87 }
88
89 return result;
90}
91
92void NetManUnregisterNetworkStack(void)
93{
94 if(IsNetStackInitialized)
95 {
96 NetManRPCUnregisterNetworkStack();
97 memset(&MainNetProtStack, 0, sizeof(MainNetProtStack));
98
99 IsNetStackInitialized=0;
100 }
101}
102
103void NetManNetIFXmit(void)
104{
105 if(IsInitialized)
106 NetManRpcNetIFXmit();
107}
108
109int NetManIoctl(unsigned int command, void *arg, unsigned int arg_len, void *output, unsigned int length)
110{
111 return IsInitialized?NetManRpcIoctl(command, arg, arg_len, output, length):-1;
112}
113
114void *NetManNetProtStackAllocRxPacket(unsigned int length, void **payload)
115{
116 return IsNetStackInitialized?MainNetProtStack.AllocRxPacket(length, payload):NULL;
117}
118
119void NetManNetProtStackFreeRxPacket(void *packet)
120{
121 if(IsNetStackInitialized) MainNetProtStack.FreeRxPacket(packet);
122}
123
124void NetManNetProtStackEnQRxPacket(void *packet)
125{
126 if(IsNetStackInitialized)
127 MainNetProtStack.EnQRxPacket(packet);
128}
129
130int NetManTxPacketNext(void **payload)
131{
132 return IsInitialized?MainNetProtStack.NextTxPacket(payload):-1;
133}
134
135void NetManTxPacketDeQ(void)
136{
137 if(IsInitialized)
138 MainNetProtStack.DeQTxPacket();
139}
140
141int NetManTxPacketAfter(void **payload)
142{
143 return IsInitialized?MainNetProtStack.AfterTxPacket(payload):-1;
144}
145
146void NetManNetProtStackReallocRxPacket(void *packet, unsigned int length)
147{
148 if(IsNetStackInitialized) MainNetProtStack.ReallocRxPacket(packet, length);
149}