forked from libretro/RetroArch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnetlogger.c
165 lines (139 loc) · 4.19 KB
/
netlogger.c
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
/* RetroArch - A frontend for libretro.
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
* Copyright (C) 2011-2015 - Daniel De Matteis
*
* RetroArch is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
#if defined(__CELLOS_LV2__) || defined(__PSL1GHT__)
#include "../defines/ps3_defines.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <retro_miscellaneous.h>
#include <net/net_compat.h>
#include <retro_log.h>
#if !defined(PC_DEVELOPMENT_IP_ADDRESS)
#error "An IP address for the PC logging server was not set in the Makefile, cannot continue."
#endif
#if !defined(PC_DEVELOPMENT_UDP_PORT)
#error "An UDP port for the PC logging server was not set in the Makefile, cannot continue."
#endif
static int g_sid;
static struct sockaddr_in target;
static char sendbuf[4096];
#ifdef VITA
static void *net_memory = NULL;
#define NET_INIT_SIZE 512*1024
#endif
static int network_interface_up(struct sockaddr_in *target, int index,
const char *ip_address, unsigned udp_port, int *s)
{
(void)index;
#if defined(VITA)
if (sceNetShowNetstat() == PSP2_NET_ERROR_ENOTINIT)
{
SceNetInitParam initparam;
net_memory = malloc(NET_INIT_SIZE);
initparam.memory = net_memory;
initparam.size = NET_INIT_SIZE;
initparam.flags = 0;
sceNetInit(&initparam);
}
*s = sceNetSocket("RA_netlogger", PSP2_NET_AF_INET, PSP2_NET_SOCK_DGRAM, 0);
target->sin_family = PSP2_NET_AF_INET;
target->sin_port = sceNetHtons(udp_port);
sceNetInetPton(PSP2_NET_AF_INET, ip_address, &target->sin_addr);
#else
#if defined(__CELLOS_LV2__) && !defined(__PSL1GHT__)
int ret = 0;
int state, timeout_count = 10;
ret = cellNetCtlInit();
if (ret < 0)
return -1;
for (;;)
{
ret = cellNetCtlGetState(&state);
if (ret < 0)
return -1;
if (state == CELL_NET_CTL_STATE_IPObtained)
break;
retro_sleep(500);
timeout_count--;
if (index && timeout_count < 0)
return 0;
}
#elif defined(GEKKO)
char t[16];
if (if_config(t, NULL, NULL, TRUE) < 0)
ret = -1;
#endif
if (ret < 0)
return -1;
*s = socket(AF_INET, SOCK_DGRAM, 0);
target->sin_family = AF_INET;
target->sin_port = htons(udp_port);
#ifdef GEKKO
target->sin_len = 8;
#endif
inet_pton(AF_INET, ip_address, &target->sin_addr);
#endif
return 0;
}
static int network_interface_down(struct sockaddr_in *target, int *s)
{
int ret = 0;
#if defined(_WIN32) && !defined(_XBOX360)
/* WinSock has headers from the stone age. */
ret = closesocket(*s);
#elif defined(__CELLOS_LV2__)
ret = socketclose(*s);
#elif defined(VITA)
if (net_memory)
free(net_memory);
sceNetSocketClose(*s);
#else
ret = close(*s);
#endif
#if defined(__CELLOS_LV2__) && !defined(__PSL1GHT__)
cellNetCtlTerm();
#elif defined(GEKKO) && !defined(HW_DOL)
net_deinit();
#endif
return ret;
}
void logger_init (void)
{
if (network_interface_up(&target, 1,
PC_DEVELOPMENT_IP_ADDRESS,PC_DEVELOPMENT_UDP_PORT, &g_sid) < 0)
printf("Could not initialize network logger interface.\n");
}
void logger_shutdown (void)
{
if (network_interface_down(&target, &g_sid) < 0)
printf("Could not deinitialize network logger interface.\n");
}
void logger_send(const char *__format,...)
{
va_list args;
va_start(args,__format);
logger_send_v(__format, args);
va_end(args);
}
void logger_send_v(const char *__format, va_list args)
{
int len;
vsnprintf(sendbuf,4000,__format, args);
len = strlen(sendbuf);
sendto(g_sid,sendbuf,len,MSG_DONTWAIT,(struct sockaddr*)&target,sizeof(target));
}