-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathdebugnet.c
More file actions
300 lines (254 loc) · 6.6 KB
/
debugnet.c
File metadata and controls
300 lines (254 loc) · 6.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*
* debugnet library for PSP2
* Copyright (C) 2010,2015 Antonio Jose Ramos Marquez (aka bigboss) @psxdev on twitter
* Repository https://github.com/psxdev/debugnet
*/
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <psp2/net/net.h>
#include <psp2/net/netctl.h>
#include <psp2/types.h>
#include <psp2/kernel/clib.h>
#include <psp2/sysmodule.h>
#include "debugnet.h"
int debugnet_external_conf=0;
debugNetConfiguration *dconfig=NULL;
static void *net_memory = NULL;
static SceNetInAddr vita_addr;
struct SceNetSockaddrIn stSockAddr;
/**
* UDP printf for debugnet library,
* use debugNetPrintf() instead unless necessary
*
* @par Example:
* @code
* debugNetUDPPrintf("This is a %s test\n", "real");
* @endcode
*/
void debugNetUDPPrintf(const char* fmt, ...)
{
char buffer[0x800];
va_list arg;
va_start(arg, fmt);
sceClibVsnprintf(buffer, sizeof(buffer), fmt, arg);
va_end(arg);
debugNetUDPSend(buffer);
}
/**
* UDP Raw text send for debugnet library,
* use debugNetPrintf() instead unless necessary
*
* @par Example:
* @code
* debugNetUDPSend("This is a test\n");
* @endcode
*
* @param text - NULL-terminated buffer containing the raw text to send
*/
void debugNetUDPSend(const char *text)
{
sceNetSend(dconfig->SocketFD, text, strlen(text), 0);
}
/**
* Log Level printf for debugnet library
*
* @par Example:
* @code
* debugNetPrintf(DBGNET_INFO,"This is a %s test\n", "real");
* @endcode
*
* @param level - DBGNET_NONE,DBGNET_INFO,DBGNET_ERROR or DBGNET_DEBUG
*/
void debugNetPrintf(int level, const char* format, ...)
{
char msgbuf[0x800];
va_list args;
if (level>dconfig->logLevel)
return;
va_start(args, format);
sceClibVsnprintf(msgbuf,2048, format, args);
msgbuf[2047] = 0;
va_end(args);
if(level>dconfig->logLevel)
{
level=DBGNET_NONE;
}
switch(level)
{
case DBGNET_INFO:
debugNetUDPPrintf("[VITA][INFO]: %s",msgbuf);
break;
case DBGNET_ERROR:
debugNetUDPPrintf("[VITA][ERROR]: %s",msgbuf);
break;
case DBGNET_DEBUG:
debugNetUDPPrintf("[VITA][DEBUG]: %s",msgbuf);
break;
case DBGNET_NONE:
break;
default:
debugNetUDPPrintf("%s",msgbuf);
}
}
/**
* Set log level for debugnet library
*
* @par Example:
* @code
* debugNetSetLogLevel(DBGNET_DEBUG);
* @endcode
* @param level - DBGNET_DEBUG,DBGNET_ERROR,DBGNET_INFO or DBGNET_NONE
*/
void debugNetSetLogLevel(int level)
{
if(dconfig)
{
dconfig->logLevel=level;
}
}
/**
* Init debugnet library
*
* @par Example:
* @code
* #define LOGLEVEL 3
* int ret;
* ret = debugNetInit("172.26.0.2", 18194, DBGNET_DEBUG);
* @endcode
*
* @param serverIP - your pc/mac server ip
* @param port - udp port server
* @param level - DBGNET_DEBUG,DBGNET_ERROR,DBGNET_INFO or DBGNET_NONE
*/
int debugNetInit(const char *serverIp, int port, int level)
{
int ret=0;
SceNetInitParam initparam;
SceNetCtlInfo info;
if(debugNetCreateConf())
{
return dconfig->debugnet_initialized;
}
debugNetSetLogLevel(level);
if (sceSysmoduleIsLoaded(SCE_SYSMODULE_NET) != SCE_SYSMODULE_LOADED)
ret=sceSysmoduleLoadModule(SCE_SYSMODULE_NET);
if (ret >=0) {
/*net initialazation code from xerpi at https://github.com/xerpi/FTPVita/blob/master/ftp.c*/
/* Init Net */
if (sceNetShowNetstat() == SCE_NET_ERROR_ENOTINIT) {
net_memory = malloc(NET_INIT_SIZE);
initparam.memory = net_memory;
initparam.size = NET_INIT_SIZE;
initparam.flags = 0;
ret = sceNetInit(&initparam);
//printf("sceNetInit(): 0x%08X\n", ret);
} else {
//printf("Net is already initialized.\n");
}
/* Init NetCtl */
ret = sceNetCtlInit();
//printf("sceNetCtlInit(): 0x%08X\n", ret);
/* Get IP address */
ret = sceNetCtlInetGetInfo(SCE_NETCTL_INFO_GET_IP_ADDRESS, &info);
//printf("sceNetCtlInetGetInfo(): 0x%08X\n", ret);
/* Save the IP of PSVita to a global variable */
sceNetInetPton(SCE_NET_AF_INET, info.ip_address, &vita_addr);
/* Create datagram udp socket*/
dconfig->SocketFD = sceNetSocket("debugnet_socket",
SCE_NET_AF_INET , SCE_NET_SOCK_DGRAM, SCE_NET_IPPROTO_UDP);
memset(&stSockAddr, 0, sizeof stSockAddr);
/*Populate SceNetSockaddrIn structure values*/
stSockAddr.sin_family = SCE_NET_AF_INET;
stSockAddr.sin_port = sceNetHtons(port);
sceNetInetPton(SCE_NET_AF_INET, serverIp, &stSockAddr.sin_addr);
/*Connect socket to server*/
sceNetConnect(dconfig->SocketFD, (struct SceNetSockaddr *)&stSockAddr, sizeof stSockAddr);
/*Show log on pc/mac side*/
debugNetUDPPrintf("debugnet initialized\n");
debugNetUDPPrintf("Copyright (C) 2010,2015 Antonio Jose Ramos Marquez aka bigboss @psxdev\n");
debugNetUDPPrintf("This Program is subject to the terms of the Mozilla Public\n"
"License, v. 2.0. If a copy of the MPL was not distributed with this\n"
"file, You can obtain one at http://mozilla.org/MPL/2.0/.\n");
debugNetUDPPrintf("ready to have a lot of fun...\n");
/*library debugnet initialized*/
dconfig->debugnet_initialized = 1;
}
return dconfig->debugnet_initialized;
}
debugNetConfiguration *debugNetGetConf()
{
if(dconfig)
{
return dconfig;
}
return NULL;
}
int debugNetSetConf(debugNetConfiguration *conf)
{
if(conf)
{
dconfig=conf;
debugnet_external_conf=1;
return dconfig->debugnet_initialized;
}
return 0;
}
int debugNetInitWithConf(debugNetConfiguration *conf)
{
int ret;
ret=debugNetSetConf(conf);
if(ret)
{
debugNetPrintf(DBGNET_INFO,"debugnet already initialized using configuration from psp2link\n");
debugNetPrintf(DBGNET_INFO,"debugnet_initialized=%d SocketFD=%d logLevel=%d\n",dconfig->debugnet_initialized,dconfig->SocketFD,dconfig->logLevel);
debugNetPrintf(DBGNET_INFO,"ready to have a lot of fun...\n");
return dconfig->debugnet_initialized;
}
else
{
return 0;
}
}
int debugNetCreateConf()
{
if(!dconfig)
{
dconfig=malloc(sizeof(debugNetConfiguration));
dconfig->debugnet_initialized=0;
dconfig->SocketFD = -1;
dconfig->logLevel=DBGNET_INFO;
return 0;
}
if(dconfig->debugnet_initialized)
{
return 1;
}
return 0;
}
/**
* Finish debugnet library
*
* @par Example:
* @code
* debugNetFinish();
* @endcode
*/
void debugNetFinish()
{
if(!debugnet_external_conf)
{
if (dconfig->debugnet_initialized) {
dconfig->debugnet_initialized = 0;
dconfig->SocketFD=-1;
//sceNetCtlTerm();
//sceNetTerm();
//sceSysmoduleUnloadModule(SCE_SYSMODULE_NET);
if (net_memory) {
free(net_memory);
net_memory = NULL;
}
}
}
}