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 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
|
/* Copyright (C) 2010-2016 The RetroArch team
*
* ---------------------------------------------------------------------------------------
* The following license statement only applies to this file (net_compat.c).
* ---------------------------------------------------------------------------------------
*
* Permission is hereby granted, free of charge,
* to any person obtaining a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <stdint.h>
#include <stdlib.h>
#include <sys/types.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <net/net_compat.h>
#include <net/net_socket.h>
#include <compat/strl.h>
#if defined(_XBOX)
/* TODO - implement h_length and h_addrtype */
struct hostent
{
int h_addrtype; /* host address type */
int h_length; /* length of addresses */
char **h_addr_list; /* list of addresses */
};
struct hostent *gethostbyname(const char *name)
{
WSAEVENT event;
static struct hostent he;
static struct in_addr addr;
static char *addr_ptr;
XNDNS *dns = NULL;
he.h_addr_list = &addr_ptr;
addr_ptr = (char*)&addr;
if (!name)
return NULL;
event = WSACreateEvent();
XNetDnsLookup(name, event, &dns);
if (!dns)
goto error;
WaitForSingleObject((HANDLE)event, INFINITE);
if (dns->iStatus)
goto error;
memcpy(&addr, dns->aina, sizeof(addr));
WSACloseEvent(event);
XNetDnsRelease(dns);
return &he;
error:
if (event)
WSACloseEvent(event);
return NULL;
}
#elif defined(VITA)
static void *_net_compat_net_memory = NULL;
#define COMPAT_NET_INIT_SIZE 512*1024
#define INET_ADDRSTRLEN sizeof(struct sockaddr_in)
#define MAX_NAME 512
typedef uint32_t in_addr_t;
struct in_addr
{
in_addr_t s_addr;
};
char *inet_ntoa(struct SceNetInAddr in)
{
static char ip_addr[INET_ADDRSTRLEN + 1];
if(sceNetInetNtop(AF_INET, &in, ip_addr, INET_ADDRSTRLEN) == NULL)
strlcpy(ip_addr, "Invalid", sizeof(ip_addr));
return ip_addr;
}
struct SceNetInAddr inet_aton(const char *ip_addr)
{
SceNetInAddr inaddr;
inet_ptrton(AF_INET, ip_addr, &inaddr);
return inaddr;
}
unsigned int inet_addr(const char *cp)
{
return inet_aton(cp).s_addr;
}
struct hostent *gethostbyname(const char *name)
{
int err;
static struct hostent ent;
static char sname[MAX_NAME] = "";
static struct SceNetInAddr saddr = { 0 };
static char *addrlist[2] = { (char *) &saddr, NULL };
int rid = sceNetResolverCreate("resolver", NULL, 0);
if(rid < 0)
return NULL;
err = sceNetResolverStartNtoa(rid, name, &saddr, 0,0,0);
sceNetResolverDestroy(rid);
if(err < 0)
return NULL;
addrlist[0] = inet_ntoa(saddr);
ent.h_name = sname;
ent.h_aliases = 0;
ent.h_addrtype = AF_INET;
ent.h_length = sizeof(struct in_addr);
ent.h_addr_list = addrlist;
ent.h_addr = addrlist[0];
return &ent;
}
int retro_epoll_fd;
#elif defined(_WIN32)
int inet_aton(const char *cp, struct in_addr *inp)
{
uint32_t addr = 0;
if (cp == 0 || inp == 0)
return -1;
addr = inet_addr(cp);
if (addr == INADDR_NONE || addr == INADDR_ANY)
return -1;
inp->s_addr = addr;
return 1;
}
#endif
int getaddrinfo_retro(const char *node, const char *service,
struct addrinfo *hints, struct addrinfo **res)
{
struct sockaddr_in *in_addr = NULL;
struct addrinfo *info = NULL;
(void)in_addr;
(void)info;
#if defined(_WIN32) || defined(HAVE_SOCKET_LEGACY)
hints->ai_family = AF_INET;
#else
hints->ai_family = AF_UNSPEC;
#endif
#ifdef HAVE_SOCKET_LEGACY
info = (struct addrinfo*)calloc(1, sizeof(*info));
if (!info)
goto error;
info->ai_family = AF_INET;
info->ai_socktype = hints->ai_socktype;
in_addr = (struct sockaddr_in*)
calloc(1, sizeof(*in_addr));
if (!in_addr)
goto error;
info->ai_addrlen = sizeof(*in_addr);
in_addr->sin_family = AF_INET;
in_addr->sin_port = inet_htons(strtoul(service, NULL, 0));
if (!node && (hints->ai_flags & AI_PASSIVE))
in_addr->sin_addr.s_addr = INADDR_ANY;
else if (node && isdigit(*node))
in_addr->sin_addr.s_addr = inet_addr(node);
else if (node && !isdigit(*node))
{
struct hostent *host = (struct hostent*)gethostbyname(node);
if (!host || !host->h_addr_list[0])
goto error;
in_addr->sin_addr.s_addr = inet_addr(host->h_addr_list[0]);
}
else
goto error;
info->ai_addr = (struct sockaddr*)in_addr;
*res = info;
return 0;
error:
if (in_addr)
free(in_addr);
if (info)
free(info);
return -1;
#else
return getaddrinfo(node, service, hints, res);
#endif
}
void freeaddrinfo_retro(struct addrinfo *res)
{
#ifdef HAVE_SOCKET_LEGACY
free(res->ai_addr);
free(res);
#else
freeaddrinfo(res);
#endif
}
/**
* network_init:
*
* Platform specific socket library initialization.
*
* Returns: true (1) if successful, otherwise false (0).
**/
bool network_init(void)
{
#ifdef _WIN32
WSADATA wsaData;
#endif
static bool inited = false;
if (inited)
return true;
#if defined(_WIN32)
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
{
network_deinit();
return false;
}
#elif defined(__CELLOS_LV2__) && !defined(__PSL1GHT__)
int timeout_count = 10;
cellSysmoduleLoadModule(CELL_SYSMODULE_NET);
sys_net_initialize_network();
if (cellNetCtlInit() < 0)
return false;
for (;;)
{
int state;
if (cellNetCtlGetState(&state) < 0)
return false;
if (state == CELL_NET_CTL_STATE_IPObtained)
break;
retro_sleep(500);
timeout_count--;
if (timeout_count < 0)
return 0;
}
#elif defined(VITA)
SceNetInitParam initparam;
if (sceNetShowNetstat() == SCE_NET_ERROR_ENOTINIT)
{
_net_compat_net_memory = malloc(COMPAT_NET_INIT_SIZE);
initparam.memory = _net_compat_net_memory;
initparam.size = COMPAT_NET_INIT_SIZE;
initparam.flags = 0;
sceNetInit(&initparam);
sceNetCtlInit();
}
retro_epoll_fd = sceNetEpollCreate("epoll", 0);
#elif defined(GEKKO)
char t[16];
if (if_config(t, NULL, NULL, TRUE) < 0)
return false;
#else
signal(SIGPIPE, SIG_IGN); /* Do not like SIGPIPE killing our app. */
#endif
inited = true;
return true;
}
/**
* network_deinit:
*
* Deinitialize platform specific socket libraries.
**/
void network_deinit(void)
{
#if defined(_WIN32)
WSACleanup();
#elif defined(__CELLOS_LV2__) && !defined(__PSL1GHT__)
cellNetCtlTerm();
sys_net_finalize_network();
cellSysmoduleUnloadModule(CELL_SYSMODULE_NET);
#elif defined(VITA)
sceNetCtlTerm();
sceNetTerm();
if (_net_compat_net_memory)
{
free(_net_compat_net_memory);
_net_compat_net_memory = NULL;
}
#elif defined(GEKKO) && !defined(HW_DOL)
net_deinit();
#endif
}
uint16_t inet_htons(uint16_t hostshort)
{
#ifdef VITA
return sceNetHtons(hostshort);
#else
return htons(hostshort);
#endif
}
int inet_ptrton(int af, const char *src, void *dst)
{
#if defined(VITA)
return sceNetInetPton(af, src, dst);
#elif defined(GEKKO) || defined(_WIN32)
/* TODO/FIXME - should use InetPton on Vista and later */
return inet_aton(src, (struct in_addr*)dst);
#else
return inet_pton(af, src, dst);
#endif
}
|