From 1a75c3b8a205f808b157ccb8ef9ed1fd26ee71d4 Mon Sep 17 00:00:00 2001 From: Wolf3s Date: Wed, 15 Nov 2023 22:24:05 -0300 Subject: [PATCH 01/23] Initial Commit of the Xbox 360 support by @BDC --- README | 17 +- Xbox 360/libsmb2.sln | 32 ++ Xbox 360/libsmb2.vcxproj | 329 +++++++++++++++ Xbox 360/libsmb2.vcxproj.filters | 292 ++++++++++++++ include/asprintf.h | 19 +- include/msvc/sys/socket.h | 9 +- include/portable-endian.h | 35 +- include/smb2/libsmb2.h | 6 +- include/xbox/addrinfo.h | 122 ++++++ include/xbox/addrsize.h | 36 ++ include/xbox/bittypes.h | 68 ++++ include/xbox/emu_socket.cpp | 623 ++++++++++++++++++++++++++++ include/xbox/emu_socket.h | 84 ++++ include/xbox/getaddrinfo.c | 672 +++++++++++++++++++++++++++++++ include/xbox/getnameinfo.c | 243 +++++++++++ include/xbox/inet_aton.c | 148 +++++++ include/xbox/inet_ntop.c | 200 +++++++++ include/xbox/inet_pton.c | 234 +++++++++++ include/xbox/sockstorage.h | 38 ++ include/xbox/xb_emu_socket.h | 85 ++++ lib/aes.c | 12 +- lib/compat.c | 32 +- lib/compat.h | 61 +++ lib/hmac-md5.h | 2 +- lib/init.c | 19 +- lib/libsmb2.c | 6 +- lib/smb2-cmd-set-info.c | 3 +- lib/socket.c | 68 +++- lib/unicode.c | 16 +- 29 files changed, 3441 insertions(+), 70 deletions(-) create mode 100644 Xbox 360/libsmb2.sln create mode 100644 Xbox 360/libsmb2.vcxproj create mode 100644 Xbox 360/libsmb2.vcxproj.filters create mode 100644 include/xbox/addrinfo.h create mode 100644 include/xbox/addrsize.h create mode 100644 include/xbox/bittypes.h create mode 100644 include/xbox/emu_socket.cpp create mode 100644 include/xbox/emu_socket.h create mode 100644 include/xbox/getaddrinfo.c create mode 100644 include/xbox/getnameinfo.c create mode 100644 include/xbox/inet_aton.c create mode 100644 include/xbox/inet_ntop.c create mode 100644 include/xbox/inet_pton.c create mode 100644 include/xbox/sockstorage.h create mode 100644 include/xbox/xb_emu_socket.h diff --git a/README b/README index f2582690..350037f5 100644 --- a/README +++ b/README @@ -265,9 +265,16 @@ and set it up. Then to build libsmb2, run $ cd lib $ make -f Makefile.DC_KOS clean install + +Xbox 360 (Xbox 360 SDK) +---------------------- +Xbox 360 CPU is a PPC(PowerPC) Xenon which only supports big endian values. +To compile libsmb2 for the Xbox 360, first install the Xbox 360 SDK(with all features), Microsoft Visual C++ 2010 Ultimate and Windows XP(Recommended) or Windows 7. -The process will copy the resulting libsmb2.a and the include/smb2 headers to your -KallistiOS toolchain install location addons folder. -NOTE: There is not yet a kos-ports entry for libsmb2 but once a versioned release -that includes Dreamcast support is created installing from kos-ports will become -the preferred method of installation. +Then to build libsmb2, go to Xbox 360 folder +and open the provided .sln file, Then pres the green button to build: + +The process will generate the resulting libsmb2.lib. So you copy the include files +to your Xbox 360 project. + +NOTE: This port was based on XBMC-360 port by BDC(Brent De Cartet) and now being updated to libsmb2 standards to best performance. diff --git a/Xbox 360/libsmb2.sln b/Xbox 360/libsmb2.sln new file mode 100644 index 00000000..9f351e11 --- /dev/null +++ b/Xbox 360/libsmb2.sln @@ -0,0 +1,32 @@ + +Microsoft Visual Studio Solution File, Format Version 11.00 +# Visual Studio 2010 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libsmb2", "libsmb2.vcxproj", "{B2F1D3EE-7D56-4862-9286-5937E1A554A4}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + CodeAnalysis|Xbox 360 = CodeAnalysis|Xbox 360 + Debug|Xbox 360 = Debug|Xbox 360 + Profile_FastCap|Xbox 360 = Profile_FastCap|Xbox 360 + Profile|Xbox 360 = Profile|Xbox 360 + Release_LTCG|Xbox 360 = Release_LTCG|Xbox 360 + Release|Xbox 360 = Release|Xbox 360 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {B2F1D3EE-7D56-4862-9286-5937E1A554A4}.CodeAnalysis|Xbox 360.ActiveCfg = CodeAnalysis|Xbox 360 + {B2F1D3EE-7D56-4862-9286-5937E1A554A4}.CodeAnalysis|Xbox 360.Build.0 = CodeAnalysis|Xbox 360 + {B2F1D3EE-7D56-4862-9286-5937E1A554A4}.Debug|Xbox 360.ActiveCfg = Debug|Xbox 360 + {B2F1D3EE-7D56-4862-9286-5937E1A554A4}.Debug|Xbox 360.Build.0 = Debug|Xbox 360 + {B2F1D3EE-7D56-4862-9286-5937E1A554A4}.Profile_FastCap|Xbox 360.ActiveCfg = Profile_FastCap|Xbox 360 + {B2F1D3EE-7D56-4862-9286-5937E1A554A4}.Profile_FastCap|Xbox 360.Build.0 = Profile_FastCap|Xbox 360 + {B2F1D3EE-7D56-4862-9286-5937E1A554A4}.Profile|Xbox 360.ActiveCfg = Profile|Xbox 360 + {B2F1D3EE-7D56-4862-9286-5937E1A554A4}.Profile|Xbox 360.Build.0 = Profile|Xbox 360 + {B2F1D3EE-7D56-4862-9286-5937E1A554A4}.Release_LTCG|Xbox 360.ActiveCfg = Release_LTCG|Xbox 360 + {B2F1D3EE-7D56-4862-9286-5937E1A554A4}.Release_LTCG|Xbox 360.Build.0 = Release_LTCG|Xbox 360 + {B2F1D3EE-7D56-4862-9286-5937E1A554A4}.Release|Xbox 360.ActiveCfg = Release|Xbox 360 + {B2F1D3EE-7D56-4862-9286-5937E1A554A4}.Release|Xbox 360.Build.0 = Release|Xbox 360 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/Xbox 360/libsmb2.vcxproj b/Xbox 360/libsmb2.vcxproj new file mode 100644 index 00000000..cd90bf0d --- /dev/null +++ b/Xbox 360/libsmb2.vcxproj @@ -0,0 +1,329 @@ + + + + + CodeAnalysis + Xbox 360 + + + Debug + Xbox 360 + + + Profile + Xbox 360 + + + Profile_FastCap + Xbox 360 + + + Release + Xbox 360 + + + Release_LTCG + Xbox 360 + + + + {B2F1D3EE-7D56-4862-9286-5937E1A554A4} + Xbox360Proj + + + + StaticLibrary + MultiByte + + + StaticLibrary + MultiByte + + + StaticLibrary + MultiByte + + + StaticLibrary + MultiByte + + + StaticLibrary + MultiByte + true + + + StaticLibrary + true + MultiByte + + + + + + + + + + + + + + + + + + + + + + + + + $(OutDir)$(ProjectName).lib + + + $(OutDir)$(ProjectName).lib + + + $(OutDir)$(ProjectName).lib + + + $(OutDir)$(ProjectName).lib + + + $(OutDir)$(ProjectName).lib + + + $(OutDir)$(ProjectName).lib + + + + NotUsing + Level3 + ProgramDatabase + Disabled + false + true + false + $(OutDir)$(ProjectName).pch + MultiThreadedDebug + _DEBUG;_XBOX;_LIB;HAVE_STDINT_H;HAVE_STDLIB_H;HAVE_STRING_H;HAVE_SYS_TYPES_H;HAVE_SYS_STAT_H;HAVE_TIME_H;STDC_HEADERS;XBOX_360_PLATFORM + Callcap + CompileAsC + ..\include\;..\include\smb2 + + + true + + + + + NotUsing + Level4 + ProgramDatabase + Disabled + false + true + AnalyzeOnly + false + $(OutDir)$(ProjectName).pch + MultiThreadedDebug + _DEBUG;_XBOX;_LIB + Callcap + + + true + + + + + Level3 + NotUsing + Full + true + false + true + ProgramDatabase + Size + false + $(OutDir)$(ProjectName).pch + MultiThreaded + NDEBUG;_XBOX;PROFILE;_LIB + Callcap + + + true + false + xapilib.lib + true + + + + + Level3 + NotUsing + Full + true + false + true + ProgramDatabase + Fastcap + Size + false + $(OutDir)$(ProjectName).pch + MultiThreaded + NDEBUG;_XBOX;PROFILE;FASTCAP;_LIB + + + true + false + true + + + + + Level3 + NotUsing + Full + true + true + ProgramDatabase + Size + false + false + $(OutDir)$(ProjectName).pch + MultiThreaded + NDEBUG;_XBOX;_LIB;__BYTE_ORDER == BIG_ENDIAN;HAVE_STDINT_H;HAVE_STDLIB_H;HAVE_STRING_H;HAVE_SYS_TYPES_H;HAVE_SYS_STAT_H;HAVE_TIME_H;STDC_HEADERS;NEED_POLL;XBOX_360_PLATFORM;HAVE_SYS_SOCKET_H + ..\include\;..\include\smb2;..\include\msvc;..\include\msvc\sys + CompileAsC + AnySuitable + + + true + true + true + + + + + Level3 + NotUsing + Full + true + true + ProgramDatabase + Size + false + false + $(OutDir)$(ProjectName).pch + MultiThreaded + NDEBUG;_XBOX;LTCG;_LIB + + + true + true + true + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + CompileAsCpp + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Xbox 360/libsmb2.vcxproj.filters b/Xbox 360/libsmb2.vcxproj.filters new file mode 100644 index 00000000..b00f7f8f --- /dev/null +++ b/Xbox 360/libsmb2.vcxproj.filters @@ -0,0 +1,292 @@ + + + + + include + + + lib + + + + + + + {028f1ea9-92cf-4908-a035-80459a8cf956} + + + {28810bac-fb60-4810-8ce9-1d5c989d4c75} + + + {8f3e89d9-a92f-4650-a970-3af5fddd92fe} + + + {84dc3fac-09d1-446a-97da-fc0fee2bf6c3} + + + {afbefc44-9daf-45e5-aacd-fe33021175a2} + + + {4c1d54ae-7522-4157-9082-b22eb34be541} + + + + + include\smb2 + + + include\smb2 + + + include\smb2 + + + include\smb2 + + + include\smb2 + + + include\smb2 + + + include\smb2 + + + include + + + include + + + include + + + lib + + + lib + + + lib + + + lib + + + lib + + + lib + + + lib + + + lib + + + lib + + + lib + + + lib + + + lib + + + include + + + include\msvc + + + include\msvc\sys + + + include\xbox + + + include\xbox + + + include\xbox + + + include\xbox + + + include\xbox + + + include\xbox + + + + + lib + + + lib + + + lib + + + lib + + + lib + + + lib + + + lib + + + lib + + + lib + + + lib + + + lib + + + lib + + + lib + + + lib + + + lib + + + lib + + + lib + + + lib + + + lib + + + lib + + + lib + + + lib + + + lib + + + lib + + + lib + + + lib + + + lib + + + lib + + + lib + + + lib + + + lib + + + lib + + + lib + + + lib + + + lib + + + lib + + + lib + + + lib + + + lib + + + lib + + + lib + + + lib + + + lib + + + lib + + + lib + + + lib + + + lib + + + lib + + + include\xbox + + + include\xbox + + + include\xbox + + + include\xbox + + + include\xbox + + + include\xbox + + + \ No newline at end of file diff --git a/include/asprintf.h b/include/asprintf.h index 88c83cc0..977b42cc 100644 --- a/include/asprintf.h +++ b/include/asprintf.h @@ -7,6 +7,11 @@ #include #include +#ifdef _XBOX +#define inline __inline +#endif + +#ifndef XBOX_360_PLATFORM #ifndef _vscprintf /* For some reason, MSVC fails to honour this #ifndef. */ /* Hence function renamed to _vscprintf_so(). */ @@ -19,14 +24,21 @@ static inline int _vscprintf_so(const char * format, va_list pargs) { return retval; } #endif // _vscprintf +#endif #ifndef vasprintf static inline int vasprintf(char **strp, const char *fmt, va_list ap) { +#ifdef XBOX_360_PLATFORM + int len = _vscprintf(fmt, ap); +#else int len = _vscprintf_so(fmt, ap); +#endif + char *str; + int r; if (len == -1) return -1; - char *str = malloc((size_t)len + 1); + str = malloc((size_t)len + 1); if (!str) return -1; - int r = vsnprintf(str, len + 1, fmt, ap); /* "secure" version of vsprintf */ + r = vsnprintf(str, len + 1, fmt, ap); /* "secure" version of vsprintf */ if (r == -1) return free(str), -1; *strp = str; return r; @@ -35,9 +47,10 @@ static inline int vasprintf(char **strp, const char *fmt, va_list ap) { #ifndef asprintf static inline int asprintf(char *strp[], const char *fmt, ...) { + int r; va_list ap; va_start(ap, fmt); - int r = vasprintf(strp, fmt, ap); + r = vasprintf(strp, fmt, ap); va_end(ap); return r; } diff --git a/include/msvc/sys/socket.h b/include/msvc/sys/socket.h index e81a2e56..4b666da6 100644 --- a/include/msvc/sys/socket.h +++ b/include/msvc/sys/socket.h @@ -6,6 +6,13 @@ extern "C" { #endif +#ifdef XBOX_360_PLATFORM +#include +#include +#include +#include +#define inline __inline +#else #ifndef WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN #endif // !WIN32_LEAN_AND_MEAN @@ -13,7 +20,7 @@ extern "C" { #include #include #include - +#endif #include #ifndef EBADF diff --git a/include/portable-endian.h b/include/portable-endian.h index dcd154b8..d630e058 100644 --- a/include/portable-endian.h +++ b/include/portable-endian.h @@ -7,10 +7,8 @@ #ifndef PORTABLE_ENDIAN_H__ #define PORTABLE_ENDIAN_H__ -#if (defined(_WIN16) || defined(_WIN32) || defined(_WIN64)) && !defined(__WINDOWS__) - -# define __WINDOWS__ - +#if (defined(_WIN16) || defined(_WIN32) || defined(_WIN64)) && !defined(__WINDOWS__) && !defined(XBOX_360_PLATFORM) +#define __WINDOWS__ #endif #if defined(ESP_PLATFORM) @@ -169,7 +167,7 @@ # define be64toh(x) (x) # define le64toh(x) __builtin_bswap64(x) -#elif defined(__WINDOWS__) +#elif defined(__WINDOWS__) # include @@ -190,6 +188,29 @@ # define htole64(x) (x) # define be64toh(x) _byteswap_uint64(x) # define le64toh(x) (x) +#endif + +#elif defined(XBOX_360_PLATFORM) + +# define htobe16(x) (x) +# define htole16(x) _byteswap_ushort(x) +# define be16toh(x) (x) +# define le16toh(x) _byteswap_ushort(x) + +# define htobe32(x) (x) +# define htole32(x) _byteswap_ulong(x) +# define be32toh(x) (x) +# define le32toh(x) _byteswap_ulong(x) + +# define htobe64(x) (x) +# define htole64(x) _byteswap_uint64(x) +# define be64toh(x) (x) +# define le64toh(x) _byteswap_uint64(x) + +# define __BYTE_ORDER BYTE_ORDER +# define __BIG_ENDIAN BIG_ENDIAN +# define __LITTLE_ENDIAN LITTLE_ENDIAN +# define __PDP_ENDIAN PDP_ENDIAN # elif defined(__GNUC__) || defined(__clang__) @@ -208,10 +229,6 @@ # define be64toh(x) __builtin_bswap64(x) # define le64toh(x) (x) -# else -# error platform not supported -# endif - #else # error platform not supported #endif diff --git a/include/smb2/libsmb2.h b/include/smb2/libsmb2.h index 37c96ce0..91a1c168 100644 --- a/include/smb2/libsmb2.h +++ b/include/smb2/libsmb2.h @@ -76,9 +76,13 @@ struct smb2dirent { struct smb2_stat_64 st; }; -#ifdef _MSC_VER +#if defined(_WINDOWS) && defined(_MSC_VER) && !defined(XBOX_360_PLATFORM) #include typedef SOCKET t_socket; +#elif defined(XBOX_360_PLATFORM) && defined(_MSC_VER) +#include +#include +typedef SOCKET t_socket; #else typedef int t_socket; #endif diff --git a/include/xbox/addrinfo.h b/include/xbox/addrinfo.h new file mode 100644 index 00000000..f1e6e56b --- /dev/null +++ b/include/xbox/addrinfo.h @@ -0,0 +1,122 @@ +/* + * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the project nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +/* $Id: addrinfo.h 31144 2012-07-15 15:38:56Z buzz $ */ + +#ifndef HAVE_ADDRINFO + +/* + * Error return codes from getaddrinfo() + */ +#define EAI_ADDRFAMILY 1 /* address family for hostname not supported */ +#define EAI_AGAIN 2 /* temporary failure in name resolution */ +#define EAI_BADFLAGS 3 /* invalid value for ai_flags */ +#define EAI_FAIL 4 /* non-recoverable failure in name resolution */ +#define EAI_FAMILY 5 /* ai_family not supported */ +#define EAI_MEMORY 6 /* memory allocation failure */ +#define EAI_NODATA 7 /* no address associated with hostname */ +#define EAI_NONAME 8 /* hostname nor servname provided, or not known */ +#define EAI_SERVICE 9 /* servname not supported for ai_socktype */ +#define EAI_SOCKTYPE 10 /* ai_socktype not supported */ +#define EAI_SYSTEM 11 /* system error returned in errno */ +#define EAI_BADHINTS 12 +#define EAI_PROTOCOL 13 +#define EAI_MAX 14 + +/* internal error */ +#define NETDB_INTERNAL -1 /* see errno */ + +/* + * Flag values for getaddrinfo() + */ +#define AI_PASSIVE 0x00000001 /* get address to use bind() */ +#define AI_CANONNAME 0x00000002 /* fill ai_canonname */ +#define AI_NUMERICHOST 0x00000004 /* prevent name resolution */ +/* valid flags for addrinfo */ +#define AI_MASK (AI_PASSIVE | AI_CANONNAME | AI_NUMERICHOST) + +#define AI_ALL 0x00000100 /* IPv6 and IPv4-mapped (with AI_V4MAPPED) */ +#define AI_V4MAPPED_CFG 0x00000200 /* accept IPv4-mapped if kernel supports */ +#define AI_ADDRCONFIG 0x00000400 /* only if any address is assigned */ +#define AI_V4MAPPED 0x00000800 /* accept IPv4-mapped IPv6 address */ +/* special recommended flags for getipnodebyname */ +#define AI_DEFAULT (AI_V4MAPPED_CFG | AI_ADDRCONFIG) + +struct addrinfo { + int ai_flags; /* AI_PASSIVE, AI_CANONNAME */ + int ai_family; /* PF_xxx */ + int ai_socktype; /* SOCK_xxx */ + int ai_protocol; /* 0 or IPPROTO_xxx for IPv4 and IPv6 */ + size_t ai_addrlen; /* length of ai_addr */ + char *ai_canonname; /* canonical name for hostname */ + struct sockaddr *ai_addr; /* binary address */ + struct addrinfo *ai_next; /* next structure in linked list */ +}; + +extern void freeaddrinfo (struct addrinfo *); +extern void freehostent (struct hostent *); +extern char *gai_strerror (int); +extern int getaddrinfo (const char *, const char *, + const struct addrinfo *, struct addrinfo **); +extern int getnameinfo (const struct sockaddr *, size_t, char *, + size_t, char *, size_t, int); +extern struct hostent *getipnodebyaddr (const void *, size_t, int, int *); +extern struct hostent *getipnodebyname (const char *, int, int, int *); +extern int inet_pton (int, const char *, void *); +extern const char *inet_ntop (int, const void *, char *, size_t); +#endif /* HAVE_ADDRINFO */ + +/* + * Constants for getnameinfo() + */ +#ifndef NI_MAXHOST +#define NI_MAXHOST 1025 +#endif +#ifndef NI_MAXSERV +#define NI_MAXSERV 32 +#endif + +/* + * Flag values for getnameinfo() + */ +#ifndef NI_NOFQDN +#define NI_NOFQDN 0x00000001 +#endif +#ifndef NI_NUMERICHOST +#define NI_NUMERICHOST 0x00000002 +#endif +#ifndef NI_NAMEREQD +#define NI_NAMEREQD 0x00000004 +#endif +#ifndef NI_NUMERICSERV +#define NI_NUMERICSERV 0x00000008 +#endif +#ifndef NI_DGRAM +#define NI_DGRAM 0x00000010 +#endif diff --git a/include/xbox/addrsize.h b/include/xbox/addrsize.h new file mode 100644 index 00000000..13db4da9 --- /dev/null +++ b/include/xbox/addrsize.h @@ -0,0 +1,36 @@ +/* + * Copyright (C) 1999 WIDE Project. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the project nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ +#ifndef INADDRSZ +#define INADDRSZ 4 /* IPv4 T_A */ +#endif + +#ifndef IN6ADDRSZ +#define IN6ADDRSZ 16 /* IPv6 T_AAAA */ +#endif + diff --git a/include/xbox/bittypes.h b/include/xbox/bittypes.h new file mode 100644 index 00000000..405e0cb9 --- /dev/null +++ b/include/xbox/bittypes.h @@ -0,0 +1,68 @@ +/* + * Copyright (C) 1999 WIDE Project. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the project nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ +#ifndef HAVE_U_INT8_T + +typedef unsigned __int8 u_int8_t; +typedef unsigned __int16 u_int16_t; +typedef unsigned __int32 u_int32_t; +typedef unsigned __int64 u_int64_t; + +#endif /* HAVE_U_INT64_T */ + +#ifndef PRId64 +#ifdef _MSC_EXTENSIONS +#define PRId64 "I64d" +#else /* _MSC_EXTENSIONS */ +#define PRId64 "lld" +#endif /* _MSC_EXTENSIONS */ +#endif /* PRId64 */ + +#ifndef PRIo64 +#ifdef _MSC_EXTENSIONS +#define PRIo64 "I64o" +#else /* _MSC_EXTENSIONS */ +#define PRIo64 "llo" +#endif /* _MSC_EXTENSIONS */ +#endif /* PRIo64 */ + +#ifndef PRIx64 +#ifdef _MSC_EXTENSIONS +#define PRIx64 "I64x" +#else /* _MSC_EXTENSIONS */ +#define PRIx64 "llx" +#endif /* _MSC_EXTENSIONS */ +#endif /* PRIx64 */ + +#ifndef PRIu64 +#ifdef _MSC_EXTENSIONS +#define PRIu64 "I64u" +#else /* _MSC_EXTENSIONS */ +#define PRIu64 "llu" +#endif /* _MSC_EXTENSIONS */ +#endif /* PRIu64 */ diff --git a/include/xbox/emu_socket.cpp b/include/xbox/emu_socket.cpp new file mode 100644 index 00000000..e7e2bce3 --- /dev/null +++ b/include/xbox/emu_socket.cpp @@ -0,0 +1,623 @@ +/* + * Copyright (C) 2005-2013 Team XBMC + * http://xbmc.org + * + * This Program 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 Foundation; either version 2, or (at your option) + * any later version. + * + * This Program 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 XBMC; see the file COPYING. If not, see + * . + * + */ +#include "emu_socket.h" +#include + +#define MAX_SOCKETS 100 + +#define MSG_PEEK 0x2 + +static bool m_bSocketsInit = false; +typedef struct +{ + SOCKET sock; + char* data; + char* start; + char* end; +} SSocketData; +static SSocketData m_sockets[MAX_SOCKETS + 1]; + +void InitSockets() +{ + m_bSocketsInit = true; + memset(m_sockets, 0, sizeof(SSocketData) * (MAX_SOCKETS + 1)); + for(int i = 0; i < MAX_SOCKETS; i++) + m_sockets[i].sock = INVALID_SOCKET; +} + +SOCKET GetSocketForIndex(int iIndex) +{ + if (iIndex < 3 || iIndex >= MAX_SOCKETS) + { + printf("GetSocketForIndex() invalid index: %i", iIndex); + return INVALID_SOCKET; + } + + if (InterlockedCompareExchangePointer((PVOID*)&m_sockets[iIndex].sock, (PVOID)INVALID_SOCKET, (PVOID)INVALID_SOCKET) == (PVOID)INVALID_SOCKET) + { + printf("GetSocketForIndex() invalid socket for index: %i", iIndex); + } + + return m_sockets[iIndex].sock; +} + +int GetIndexForSocket(SOCKET iSocket) +{ + for (int i = 0; i < MAX_SOCKETS; i++) + { + if (InterlockedCompareExchangePointer((PVOID*)&m_sockets[i].sock, (PVOID)iSocket, (PVOID)iSocket) == (PVOID)iSocket) + return i; + } + return -1; +} + +int AddSocket(SOCKET iSocket) +{ + if(!m_bSocketsInit) + InitSockets(); + + if(iSocket == INVALID_SOCKET) + return -1; + + for (int i = 3; i < MAX_SOCKETS; i++) + { + if (InterlockedCompareExchangePointer((PVOID*)&m_sockets[i].sock, (PVOID)iSocket, (PVOID)INVALID_SOCKET) == (PVOID)INVALID_SOCKET) + { + if(m_sockets[i].data) delete m_sockets[i].data; + m_sockets[i].data = NULL; + m_sockets[i].start = NULL; + m_sockets[i].end = NULL; + return i; + } + } + printf(__FUNCTION__ " - Unable to add socket to internal list, no space left"); + return -1; +} + +void ReleaseSocket(int iIndex) +{ + if (iIndex < 3 || iIndex >= MAX_SOCKETS) + { + printf("ReleaseSocket() invalid index:%i", iIndex); + return ; + } + + if (InterlockedExchangePointer((PVOID*)&m_sockets[iIndex].sock, (PVOID)INVALID_SOCKET) == (PVOID)INVALID_SOCKET) + printf("ReleaseSocket() invalid socket for index:%i", iIndex); + + if(m_sockets[iIndex].data) delete m_sockets[iIndex].data; + m_sockets[iIndex].data = NULL; + m_sockets[iIndex].start = NULL; + m_sockets[iIndex].end = NULL; +} + +extern "C" +{ + +#ifdef _XBOX + int __stdcall sckemu_gethostname(char* name, int namelen) + { + if ((unsigned int)namelen < strlen("xbox") + 1) return -1; + strcpy(name, "xbox"); + return 0; + } +#else + int __stdcall sckemu_gethostname(char* name, int namelen) + { + return gethostname(name, namelen); + } +#endif + + static struct mphostent hbn_hostent; + static char* hbn_cAliases[]= { NULL, NULL }; // only one NULL is needed acutally + static char* hbn_dwlist1[] = {NULL, NULL, NULL}; + static DWORD hbn_dwList2[] = {0, 0, 0}; + static char hbn_hostname[128]; + +#ifdef _XBOX + struct mphostent* __stdcall sckemu_gethostbyname(const char* name) // TODO: Not currently used or needed + { + printf("Socket Emu - gethostbyname() not implemented."); + + return NULL; +#if 0 + CStdString strIpAdres; + + hbn_hostent.h_name = hbn_hostname; + hbn_hostname[0] = '\0'; // clear hostname + hbn_hostent.h_aliases = hbn_cAliases; + hbn_hostent.h_addrtype = AF_INET; + hbn_hostent.h_length = 4; + hbn_hostent.h_addr_list = hbn_dwlist1; + hbn_hostent.h_addr_list[0] = (char*)hbn_dwList2; + + sckemu_gethostname(hbn_hostname, 128); + + if (!strcmp(hbn_hostname, name)) + { + if(g_application.getNetwork().IsAvailable()) + hbn_dwList2[0] = inet_addr(g_application.getNetwork().m_networkinfo.ip); + + return &hbn_hostent; + } + if (CDNSNameCache::Lookup(name, strIpAdres)) + { + strcpy(hbn_hostname, name); + hbn_dwList2[0] = inet_addr(strIpAdres.c_str()); + return &hbn_hostent; + } + else + { + OutputDebugString("DNS:no ipadres found\n"); + return NULL; + } + } + + // libRV.lib depends on gethostbyname + struct hostent; + struct hostent* gethostbyname(const char* name) + { + return (hostent*)sckemu_gethostbyname(name); +#endif + } +#else + struct mphostent* __stdcall sckemu_gethostbyname(const char* name) + { + return (mphostent*)gethostbyname(name); + } +#endif + + int __stdcall sckemu_connect(int s, const struct sockaddr FAR *name, int namelen) + { + SOCKET socket = GetSocketForIndex(s); + struct sockaddr_in* pTmp = (struct sockaddr_in*)name; + char szBuf[128]; +// sprintf(szBuf, "connect: family:%i port:%i ip:%i.%i.%i.%i\n", + // pTmp->sin_family, + // ntohs(pTmp->sin_port), + // pTmp->sin_addr.S_un.S_addr&0xff, + // (pTmp->sin_addr.S_un.S_addr&0xff00) >> 8, + // (pTmp->sin_addr.S_un.S_addr&0xff0000) >> 16, + // (pTmp->sin_addr.S_un.S_addr) >> 24 + // ); + OutputDebugString(szBuf); + + int iResult = connect( socket, name, namelen); + if(iResult == SOCKET_ERROR) + { + errno = WSAGetLastError(); + // sprintf(szBuf, "connect returned:%i %i\n", iResult, WSAGetLastError()); + // Don't OutputDebugString here, as no debugger attached + // this will (strangely) reset WSAGetLastError() + //OutputDebugString(szBuf); + } + + return iResult; + } + + int __stdcall sckemu_send(int s, const char *buf, int len, int flags) + { + SOCKET socket = GetSocketForIndex(s); + //flags Unsupported; must be zero. + int res = send(socket, buf, len, 0); + if(res == SOCKET_ERROR) + errno = WSAGetLastError(); + return res; + } + + int __stdcall sckemu_socket(int af, int type, int protocol) + { + SOCKET sock = socket(af, type, protocol); + if(sock == INVALID_SOCKET) + errno = WSAGetLastError(); + return AddSocket(sock);; + } + + int __stdcall sckemu_bind(int s, const struct sockaddr FAR * name, int namelen) + { + SOCKET socket = GetSocketForIndex(s); + struct sockaddr_in address2; + + if( name->sa_family == AF_INET + && namelen >= sizeof(sockaddr_in) ) + { + struct sockaddr_in* address = (struct sockaddr_in*)name; + +// CLog::Log(LOGDEBUG, "sckemu_bind: family:%i port:%i ip:%i.%i.%i.%i\n", +// address->sin_family, + // ntohs(address->sin_port), + // address->sin_addr.S_un.S_un_b.s_b1, + // address->sin_addr.S_un.S_un_b.s_b2, + // address->sin_addr.S_un.S_un_b.s_b3, + // address->sin_addr.S_un.S_un_b.s_b4); + + + // if( address->sin_addr.S_un.S_addr == inet_addr(g_application.getNetwork().m_networkinfo.ip) + // || address->sin_addr.S_un.S_addr == inet_addr("127.0.0.1") ) + { + // local xbox, correct for xbox stack + address2 = *address; + address2.sin_addr.S_un.S_addr = 0; + name = (sockaddr*)&address2; + namelen = sizeof(address2); + } + } + + int iResult = bind(socket, name, namelen); + if(iResult == SOCKET_ERROR) + { + errno = WSAGetLastError(); + printf("bind returned:%i %i\n", iResult, WSAGetLastError()); + } + return iResult; + } + + int __stdcall sckemu_closesocket(int s) + { + SOCKET socket = GetSocketForIndex(s); + + int iResult = closesocket(socket); + if(iResult == SOCKET_ERROR) + errno = WSAGetLastError(); + + ReleaseSocket(s); + return iResult; + } + + int __stdcall sckemu_getsockopt(int s, int level, int optname, char FAR * optval, int FAR * optlen) + { + SOCKET socket = GetSocketForIndex(s); + + if(optname == SO_ERROR) + { + /* unsupported option */ + /* just hope there was no error, we could probably check connetion state if we wanted */ + *((int*)optval) = 0; + return 0; + } + else + return getsockopt(socket, level, optname, optval, optlen); + } + + int __stdcall sckemu_ioctlsocket(int s, long cmd, DWORD FAR * argp) + { + if (s < 3 || s >= MAX_SOCKETS) + { + printf("sckemu_ioctlsocket invalid index:%i", s); + return SOCKET_ERROR; + } + SSocketData& socket = m_sockets[s]; + + int res = ioctlsocket(socket.sock, cmd, argp); + if( res == 0 && cmd == FIONREAD && socket.data ) + *argp += socket.end - socket.start; + + return res; + } + + int __stdcall sckemu_recv(int s, char FAR * buf, int len, int flags) + { + if (s < 3 || s >= MAX_SOCKETS) + { + printf("sckemu_recv invalid index:%i", s); + return SOCKET_ERROR; + } + SSocketData& socket = m_sockets[s]; + int len2, len3; + + if(flags & MSG_PEEK) + { + printf(__FUNCTION__" - called with MSG_PEEK set, attempting workaround"); + // work around for peek, it will give garbage as data + + if(socket.data == NULL) + { + socket.data = new char[len]; + len2 = recv(socket.sock, socket.data, len, 0); + if(len2 == SOCKET_ERROR) + { + // SAFE_DELETE(socket.data); + return SOCKET_ERROR; + } + socket.start = socket.data; + socket.end = socket.start + len2; + } +// len2 = std::min(len, socket.end - socket.start); + memcpy(buf, socket.start, len2); + return len2; + } + + if(flags) + printf(__FUNCTION__" - called with flags %d that will be ignored", flags); + flags = 0; + + len2 = 0; + if(socket.start < socket.end) + { +// len2 = std::min(len, socket.end - socket.start); + memcpy(buf, socket.start, len2); + socket.start += len2; + buf++; + len--; + } + + if(socket.start >= socket.end) + { + delete[] socket.data; + socket.data = NULL; + socket.start = NULL; + socket.end = NULL; + } + + if(len == 0) + return 0; + + len3 = recv(socket.sock, buf, len, flags); + if(len3 == SOCKET_ERROR) + return SOCKET_ERROR; + + return len2 + len3; + } + + int __stdcall sckemu_select(int nfds, fd_set FAR * readfds, fd_set FAR * writefds, fd_set FAR *exceptfds, const struct timeval FAR * timeout) + { + fd_set readset, writeset, exceptset; + unsigned int i; + + fd_set* preadset = &readset; + fd_set* pwriteset = &writeset; + fd_set* pexceptset = &exceptset; + + if (!readfds) preadset = NULL; + if (!writefds) pwriteset = NULL; + if (!exceptfds) pexceptset = NULL; + + FD_ZERO(&readset); + FD_ZERO(&writeset); + FD_ZERO(&exceptset); + + if(readfds) + { + for (i = 0; i < readfds->fd_count; ++i) + FD_SET(GetSocketForIndex(readfds->fd_array[i]), &readset); + } + if(writefds) + { + for (i = 0; i < writefds->fd_count; ++i) + FD_SET(GetSocketForIndex(writefds->fd_array[i]), &writeset); + } + if(exceptfds) + { + for (i = 0; i < exceptfds->fd_count; ++i) + FD_SET(GetSocketForIndex(exceptfds->fd_array[i]), &exceptset); + } + + int ifd = select(nfds, preadset, pwriteset, pexceptset, timeout); + + // convert real socket identifiers back to our custom ones and clean results + if (readfds) + { + FD_ZERO(readfds); + for (i = 0; i < readset.fd_count; i++) FD_SET(GetIndexForSocket(readset.fd_array[i]), readfds); + } + if (writefds) + { + FD_ZERO(writefds); + for (i = 0; i < writeset.fd_count; i++) FD_SET(GetIndexForSocket(writeset.fd_array[i]), writefds); + } + if (exceptfds) + { + FD_ZERO(exceptfds); + for (i = 0; i < exceptset.fd_count; i++) FD_SET(GetIndexForSocket(exceptset.fd_array[i]), exceptfds); + } + + return ifd; + } + + int __stdcall sckemu_sendto(int s, const char FAR * buf, int len, int flags, const struct sockaddr FAR * to, int tolen) + { + SOCKET socket = GetSocketForIndex(s); + int res = sendto(socket, buf, len, flags, to, tolen); + if(res == SOCKET_ERROR) + errno = WSAGetLastError(); + return res; + } + + int __stdcall sckemu_setsockopt(int s, int level, int optname, const char FAR * optval, int optlen) + { + SOCKET socket = GetSocketForIndex(s); + int res = setsockopt(socket, level, optname, optval, optlen); + if(res == SOCKET_ERROR) + errno = WSAGetLastError(); + return res; + } + + int __stdcall sckemu_accept(int s, struct sockaddr FAR * addr, OUT int FAR * addrlen) + { + SOCKET socket = GetSocketForIndex(s); + SOCKET res = accept(socket, addr, addrlen); + if(res == INVALID_SOCKET) + errno = WSAGetLastError(); + return AddSocket(res); + } + + int __stdcall sckemu_getsockname(int s, struct sockaddr* name, int* namelen) + { + SOCKET socket = GetSocketForIndex(s); + int res = getsockname(socket, name, namelen); + if(res == SOCKET_ERROR) + { + errno = WSAGetLastError(); + return res; + } + + if( name->sa_family == AF_INET + && *namelen >= sizeof(sockaddr_in) ) + { + sockaddr_in *addr = (sockaddr_in*)name; + if( addr->sin_port > 0 && addr->sin_addr.S_un.S_addr == 0 ) + { + // unspecifed address will always be on local xbox ip + // some dll's assume this will return a proper address + // even if windows standard doesn't gurantee it + // if( g_application.getNetwork().IsAvailable() ) + // addr->sin_addr.S_un.S_addr = inet_addr(g_application.getNetwork().m_networkinfo.ip); + } + } + return res; + } + + int __stdcall sckemu_listen(int s, int backlog) + { + SOCKET socket = GetSocketForIndex(s); + int res = listen(socket, backlog); + if(res == SOCKET_ERROR) + errno = WSAGetLastError(); + return res; + } + + u_short __stdcall sckemu_ntohs(u_short netshort) + { + return ntohs(netshort); + } + + int __stdcall sckemu_recvfrom(int s, char* buf, int len, int flags, struct sockaddr* from, int* fromlen) + { + SOCKET socket = GetSocketForIndex(s); + int res = recvfrom(socket, buf, len, flags, from, fromlen); + if(res == SOCKET_ERROR) + errno = WSAGetLastError(); + return res; + } + + int __stdcall sckemu_WSAFDIsSet(int s, fd_set* set) + { + fd_set real_set; // the set with real socket id's + int real_socket = GetSocketForIndex(s); + + FD_ZERO(&real_set); + if (set) + { + for (unsigned int i = 0; i < set->fd_count; ++i) + FD_SET(GetSocketForIndex(set->fd_array[i]), &real_set); + } + + return __WSAFDIsSet(real_socket, &real_set); + } + + int __stdcall sckemu_shutdown(int s, int how) + { + SOCKET socket = GetSocketForIndex(s); + return shutdown(socket, how); + } + + char* __stdcall sckemu_ntoa (struct in_addr in) + { + static char _inetaddress[32]; +// sprintf(_inetaddress, "%d.%d.%d.%d", in.S_un.S_un_b.s_b1, in.S_un.S_un_b.s_b2, in.S_un.S_un_b.s_b3, in.S_un.S_un_b.s_b4); + return _inetaddress; + } + +#ifdef _XBOX + + static struct mphostent hba_hostent; + static char* hba_cAliases[]= { NULL, NULL }; // only one NULL is needed acutally + static char* hba_dwlist1[] = {NULL, NULL, NULL}; + static DWORD hba_dwList2[] = {0, 0, 0}; + static char hba_hostname[128]; + + struct mphostent* __stdcall sckemu_gethostbyaddr(const char* addr, int len, int type) + { + printf("Untested function sckemu_gethostbyaddr called!"); + XNADDR xna; + DWORD dwState; + + hba_hostent.h_name = hba_hostname; + hba_hostname[0] = '\0'; // clear hostname + hba_hostent.h_aliases = hba_cAliases; + hba_hostent.h_addrtype = AF_INET; + hba_hostent.h_length = 4; + hba_hostent.h_addr_list = hba_dwlist1; + hba_hostent.h_addr_list[0] = (char*)hba_dwList2; + + do dwState = XNetGetTitleXnAddr(&xna); + while (dwState == XNET_GET_XNADDR_PENDING); + + if (!memcmp(addr, &(xna.ina.s_addr), 4)) + { + //get title hostent + sckemu_gethostname(hba_hostname, 128); + hba_dwList2[0] = xna.ina.S_un.S_addr; + return &hba_hostent; + } + else + { + //get client hostent + in_addr client = {{addr[0], addr[1], addr[2], addr[3]}}; + strcpy(hba_hostname, sckemu_ntoa(client)); + hba_dwList2[0] = client.S_un.S_addr; + return &hba_hostent; + } + return 0; + } +#endif + + struct servent* __stdcall sckemu_getservbyname(const char* name, const char* proto) + { + OutputDebugString("TODO: getservbyname\n"); + return NULL; + } + + struct servent* __stdcall sckemu_getservbyport(int port, const char* proto) + { + OutputDebugString("TODO: getservbyport\n"); + return NULL; + } + + struct protoent* __stdcall sckemu_getprotobyname(const char* name) + { + OutputDebugString("TODO: getprotobyname\n"); + return NULL; + } + + int __stdcall sckemu_getpeername(int s, struct sockaddr FAR *name, int FAR *namelen) + { + int socket = GetSocketForIndex(s); + return getpeername(socket, name, namelen); + } + + int getaddrinfo(const char *hostname, const char *servname, const struct addrinfo *hints, struct addrinfo **res); + int __stdcall sckemu_getaddrinfo(const char* nodename, const char* servname, const struct addrinfo* hints, struct addrinfo** res) + { + return getaddrinfo(nodename, servname, hints, res); + } + + int getnameinfo(const struct sockaddr *sa, size_t salen, char *host, size_t hostlen, char *serv, size_t servlen, int flags); + int __stdcall sckemu_getnameinfo(const struct sockaddr *sa, size_t salen, char *host, size_t hostlen, char *serv, size_t servlen, int flags) + { + return getnameinfo(sa, salen, host, hostlen, serv, servlen, flags); + } + + void freeaddrinfo(struct addrinfo *ai); + void __stdcall sckemu_freeaddrinfo(struct addrinfo *ai) + { + return freeaddrinfo(ai); + } +} diff --git a/include/xbox/emu_socket.h b/include/xbox/emu_socket.h new file mode 100644 index 00000000..0d178661 --- /dev/null +++ b/include/xbox/emu_socket.h @@ -0,0 +1,84 @@ +#ifndef _EMU_SOCKET_EMU_SOCKET_H_ +#define _EMU_SOCKET_EMU_SOCKET_H_ + +/* + * Copyright (C) 2005-2013 Team XBMC + * http://xbmc.org + * + * This Program 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 Foundation; either version 2, or (at your option) + * any later version. + * + * This Program 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 XBMC; see the file COPYING. If not, see + * . + * + */ + +#ifdef _XBOX +#include +#endif + +#define SOCK_RAW 3 +#define SO_ERROR 0x1007 + +//#define EAFNOSUPPORT WSAEAFNOSUPPORT + +#define INET_ADDRSTRLEN 16 + +struct mphostent { + char *h_name; /* official name of host */ + char **h_aliases; /* alias list */ + short h_addrtype; /* host address type */ + short h_length; /* length of address */ + char **h_addr_list; /* list of addresses from name server */ +}; + +#ifdef _XBOX +typedef struct servent { + char FAR* s_name; + char FAR FAR** s_aliases; + short s_port; + char FAR* s_proto; +} servent; + +typedef struct protoent { + char FAR* p_name; + char FAR FAR** p_aliases; + short p_proto; +} protoent; +#endif + +#ifndef IN_MULTICAST +#define IN_MULTICAST(i) (((i) & 0xf0000000U) == 0xe0000000U) +#endif + +#ifndef IN_EXPERIMENTAL +#define IN_EXPERIMENTAL(i) (((i) & 0xe0000000U) == 0xe0000000U) +#endif + +#ifndef IN_LOOPBACKNET +#define IN_LOOPBACKNET 127 +#endif + +/* +typedef struct addrinfo +{ + int ai_flags; // AI_PASSIVE, AI_CANONNAME, AI_NUMERICHOST + int ai_family; // PF_xxx + int ai_socktype; // SOCK_xxx + int ai_protocol; // 0 or IPPROTO_xxx for IPv4 and IPv6 + size_t ai_addrlen; // Length of ai_addr + char * ai_canonname; // Canonical name for nodename + struct sockaddr * ai_addr; // Binary address + struct addrinfo * ai_next; // Next structure in linked list +} +ADDRINFOA, *PADDRINFOA; +*/ +#endif //_EMU_SOCKET_EMU_SOCKET_H_ diff --git a/include/xbox/getaddrinfo.c b/include/xbox/getaddrinfo.c new file mode 100644 index 00000000..69ea2e8a --- /dev/null +++ b/include/xbox/getaddrinfo.c @@ -0,0 +1,672 @@ +/* + * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the project nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +/* + * "#ifdef FAITH" part is local hack for supporting IPv4-v6 translator. + * + * Issues to be discussed: + * - Thread safe-ness must be checked. + * - Return values. There are nonstandard return values defined and used + * in the source code. This is because RFC2553 is silent about which error + * code must be returned for which situation. + * Note: + * - We use getipnodebyname() just for thread-safeness. There's no intent + * to let it do PF_UNSPEC (actually we never pass PF_UNSPEC to + * getipnodebyname(). + * - The code filters out AFs that are not supported by the kernel, + * when globbing NULL hostname (to loopback, or wildcard). Is it the right + * thing to do? What is the relationship with post-RFC2553 AI_ADDRCONFIG + * in ai_flags? + */ + +#ifdef _XBOX +#include +#include +#else +#include +#endif +#include +#include "addrinfo.h" +#include "emu_socket.h" +#include +#include +#include +#include +#include +#include +#include + +#ifdef _XBOX +extern struct mphostent* __stdcall sckemu_gethostbyname(const char* name); +#define gethostbyname(name) sckemu_gethostbyname(name) + +extern struct mphostent* __stdcall sckemu_gethostbyaddr(const char* addr, int len, int type); +#define gethostbyaddr(addr, len, type) sckemu_gethostbyaddr(addr, len, type) + +extern struct servent* __stdcall sckemu_getservbyname(const char* name, const char* proto); +#define getservbyname(name, proto) sckemu_getservbyname(name, proto) + +#define hostent mphostent +#endif + +#ifndef HAVE_U_INT32_T +#include "bittypes.h" +#endif + +#ifndef HAVE_SOCKADDR_STORAGE +#include "sockstorage.h" +#endif + +#ifdef NEED_ADDRINFO_H +#include "addrinfo.h" +#endif + +#if defined(__KAME__) && defined(ENABLE_IPV6) +# define FAITH +#endif + +#define SUCCESS 0 +#define GAI_ANY 0 +#define YES 1 +#define NO 0 + +#ifdef FAITH +static int translate = NO; +static struct in6_addr faith_prefix = IN6ADDR_GAI_ANY_INIT; +#endif + +static const char in_addrany[] = { 0, 0, 0, 0 }; +static const char in6_addrany[] = { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 +}; +static const char in_loopback[] = { 127, 0, 0, 1 }; +static const char in6_loopback[] = { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 +}; + +struct sockinet { + u_char si_len; + u_char si_family; + u_short si_port; +}; + +static struct gai_afd { + int a_af; + int a_addrlen; + int a_socklen; + int a_off; + const char *a_addrany; + const char *a_loopback; +} gai_afdl [] = { +#ifdef ENABLE_IPV6 +#define N_INET6 0 + {PF_INET6, sizeof(struct in6_addr), + sizeof(struct sockaddr_in6), + offsetof(struct sockaddr_in6, sin6_addr), + in6_addrany, in6_loopback}, +#define N_INET 1 +#else +#define N_INET 0 +#endif + {PF_INET, sizeof(struct in_addr), + sizeof(struct sockaddr_in), + offsetof(struct sockaddr_in, sin_addr), + in_addrany, in_loopback}, + {0, 0, 0, 0, NULL, NULL}, +}; + +#ifdef ENABLE_IPV6 +#define PTON_MAX 16 +#else +#define PTON_MAX 4 +#endif + +#ifndef IN_MULTICAST +#define IN_MULTICAST(i) (((i) & 0xf0000000U) == 0xe0000000U) +#endif + +#ifndef IN_EXPERIMENTAL +#define IN_EXPERIMENTAL(i) (((i) & 0xe0000000U) == 0xe0000000U) +#endif + +#ifndef IN_LOOPBACKNET +#define IN_LOOPBACKNET 127 +#endif + +static int get_name (const char *, struct gai_afd *, struct addrinfo **, char *, struct addrinfo *, int); +static int get_addr (const char *, int, struct addrinfo **, + struct addrinfo *, int); +static int str_isnumber (const char *); + +static char *ai_errlist[] = { + "success.", + "address family for hostname not supported.", /* EAI_ADDRFAMILY */ + "temporary failure in name resolution.", /* EAI_AGAIN */ + "invalid value for ai_flags.", /* EAI_BADFLAGS */ + "non-recoverable failure in name resolution.", /* EAI_FAIL */ + "ai_family not supported.", /* EAI_FAMILY */ + "memory allocation failure.", /* EAI_MEMORY */ + "no address associated with hostname.", /* EAI_NODATA */ + "hostname nor servname provided, or not known.",/* EAI_NONAME */ + "servname not supported for ai_socktype.", /* EAI_SERVICE */ + "ai_socktype not supported.", /* EAI_SOCKTYPE */ + "system error returned in errno.", /* EAI_SYSTEM */ + "invalid value for hints.", /* EAI_BADHINTS */ + "resolved protocol is unknown.", /* EAI_PROTOCOL */ + "unknown error.", /* EAI_MAX */ +}; + +#define GET_CANONNAME(ai, str) \ +if (pai->ai_flags & AI_CANONNAME) {\ + if (((ai)->ai_canonname = (char *)malloc(strlen(str) + 1)) != NULL) {\ + strcpy((ai)->ai_canonname, (str));\ + } else {\ + error = EAI_MEMORY;\ + goto free;\ + }\ +} + +#ifdef HAVE_SOCKADDR_SA_LEN +#define GET_AI(ai, gai_afd, addr, port) {\ + char *p;\ + if (((ai) = (struct addrinfo *)malloc(sizeof(struct addrinfo) +\ + ((gai_afd)->a_socklen)))\ + == NULL) goto free;\ + memcpy(ai, pai, sizeof(struct addrinfo));\ + (ai)->ai_addr = (struct sockaddr *)((ai) + 1);\ + memset((ai)->ai_addr, 0, (gai_afd)->a_socklen);\ + (ai)->ai_addr->sa_len = (ai)->ai_addrlen = (gai_afd)->a_socklen;\ + (ai)->ai_addr->sa_family = (ai)->ai_family = (gai_afd)->a_af;\ + ((struct sockinet *)(ai)->ai_addr)->si_port = port;\ + p = (char *)((ai)->ai_addr);\ + memcpy(p + (gai_afd)->a_off, (addr), (gai_afd)->a_addrlen);\ +} +#else +#define GET_AI(ai, gai_afd, addr, port) {\ + char *p;\ + if (((ai) = (struct addrinfo *)malloc(sizeof(struct addrinfo) +\ + ((gai_afd)->a_socklen)))\ + == NULL) goto free;\ + memcpy(ai, pai, sizeof(struct addrinfo));\ + (ai)->ai_addr = (struct sockaddr *)((ai) + 1);\ + memset((ai)->ai_addr, 0, (gai_afd)->a_socklen);\ + (ai)->ai_addrlen = (gai_afd)->a_socklen;\ + (ai)->ai_addr->sa_family = (ai)->ai_family = (gai_afd)->a_af;\ + ((struct sockinet *)(ai)->ai_addr)->si_port = port;\ + p = (char *)((ai)->ai_addr);\ + memcpy(p + (gai_afd)->a_off, (addr), (gai_afd)->a_addrlen);\ +} +#endif + +#define ERR(err) { error = (err); goto bad; } + +char * +gai_strerror(int ecode) +{ + if (ecode < 0 || ecode > EAI_MAX) + ecode = EAI_MAX; + return ai_errlist[ecode]; +} + +void +freeaddrinfo(struct addrinfo *ai) +{ + struct addrinfo *next; + + do { + next = ai->ai_next; + if (ai->ai_canonname) + free(ai->ai_canonname); + /* no need to free(ai->ai_addr) */ + free(ai); + } while ((ai = next) != NULL); +} + +static int +str_isnumber(const char *p) +{ + unsigned char *q = (unsigned char *)p; + while (*q) { + if (! isdigit(*q)) + return NO; + q++; + } + return YES; +} + +int +getaddrinfo(const char*hostname, const char*servname, + const struct addrinfo *hints, struct addrinfo **res) +{ + struct addrinfo sentinel; + struct addrinfo *top = NULL; + struct addrinfo *cur; + int i, error = 0; + char pton[PTON_MAX]; + struct addrinfo ai; + struct addrinfo *pai; + u_short port; + +#ifdef FAITH + static int firsttime = 1; + + if (firsttime) { + /* translator hack */ + { + char *q = getenv("GAI"); + if (q && inet_pton(AF_INET6, q, &faith_prefix) == 1) + translate = YES; + } + firsttime = 0; + } +#endif + + /* initialize file static vars */ + sentinel.ai_next = NULL; + cur = &sentinel; + pai = &ai; + pai->ai_flags = 0; + pai->ai_family = PF_UNSPEC; + pai->ai_socktype = GAI_ANY; + pai->ai_protocol = GAI_ANY; + pai->ai_addrlen = 0; + pai->ai_canonname = NULL; + pai->ai_addr = NULL; + pai->ai_next = NULL; + port = GAI_ANY; + + if (hostname == NULL && servname == NULL) + return EAI_NONAME; + if (hints) { + /* error check for hints */ + if (hints->ai_addrlen || hints->ai_canonname || + hints->ai_addr || hints->ai_next) + ERR(EAI_BADHINTS); /* xxx */ + if (hints->ai_flags & ~AI_MASK) + ERR(EAI_BADFLAGS); + switch (hints->ai_family) { + case PF_UNSPEC: + case PF_INET: +#ifdef ENABLE_IPV6 + case PF_INET6: +#endif + break; + default: + ERR(EAI_FAMILY); + } + memcpy(pai, hints, sizeof(*pai)); + switch (pai->ai_socktype) { + case GAI_ANY: + switch (pai->ai_protocol) { + case GAI_ANY: + break; + case IPPROTO_UDP: + pai->ai_socktype = SOCK_DGRAM; + break; + case IPPROTO_TCP: + pai->ai_socktype = SOCK_STREAM; + break; + default: +#ifdef _XBOX + pai->ai_socktype = SOCK_STREAM; +#else + pai->ai_socktype = SOCK_RAW; +#endif //_XBOX + break; + } + break; +#ifndef _XBOX + case SOCK_RAW: + break; +#endif //_XBOX + case SOCK_DGRAM: + if (pai->ai_protocol != IPPROTO_UDP && + pai->ai_protocol != GAI_ANY) + ERR(EAI_BADHINTS); /*xxx*/ + pai->ai_protocol = IPPROTO_UDP; + break; + case SOCK_STREAM: + if (pai->ai_protocol != IPPROTO_TCP && + pai->ai_protocol != GAI_ANY) + ERR(EAI_BADHINTS); /*xxx*/ + pai->ai_protocol = IPPROTO_TCP; + break; + default: + ERR(EAI_SOCKTYPE); + /* unreachable */ + } + } + + /* + * service port + */ + if (servname) { + if (str_isnumber(servname)) { + if (pai->ai_socktype == GAI_ANY) { + /* caller accept *GAI_ANY* socktype */ + pai->ai_socktype = SOCK_DGRAM; + pai->ai_protocol = IPPROTO_UDP; + } + port = htons((u_short)atoi(servname)); + } else { + struct servent *sp; + char *proto; + + proto = NULL; + switch (pai->ai_socktype) { + case GAI_ANY: + proto = NULL; + break; + case SOCK_DGRAM: + proto = "udp"; + break; + case SOCK_STREAM: + proto = "tcp"; + break; + default: + fprintf(stderr, "panic!\n"); + break; + } + if ((sp = getservbyname(servname, proto)) == NULL) + ERR(EAI_SERVICE); + port = sp->s_port; + if (pai->ai_socktype == GAI_ANY) { + if (strcmp(sp->s_proto, "udp") == 0) { + pai->ai_socktype = SOCK_DGRAM; + pai->ai_protocol = IPPROTO_UDP; + } else if (strcmp(sp->s_proto, "tcp") == 0) { + pai->ai_socktype = SOCK_STREAM; + pai->ai_protocol = IPPROTO_TCP; + } else + ERR(EAI_PROTOCOL); /*xxx*/ + } + } + } + + /* + * hostname == NULL. + * passive socket -> anyaddr (0.0.0.0 or ::) + * non-passive socket -> localhost (127.0.0.1 or ::1) + */ + if (hostname == NULL) { + struct gai_afd *gai_afd; + + for (gai_afd = &gai_afdl[0]; gai_afd->a_af; gai_afd++) { + if (!(pai->ai_family == PF_UNSPEC + || pai->ai_family == gai_afd->a_af)) { + continue; + } + + if (pai->ai_flags & AI_PASSIVE) { + GET_AI(cur->ai_next, gai_afd, gai_afd->a_addrany, port); + /* xxx meaningless? + * GET_CANONNAME(cur->ai_next, "anyaddr"); + */ + } else { + GET_AI(cur->ai_next, gai_afd, gai_afd->a_loopback, + port); + /* xxx meaningless? + * GET_CANONNAME(cur->ai_next, "localhost"); + */ + } + cur = cur->ai_next; + } + top = sentinel.ai_next; + if (top) + goto good; + else + ERR(EAI_FAMILY); + } + + /* hostname as numeric name */ + for (i = 0; gai_afdl[i].a_af; i++) { + if (inet_pton(gai_afdl[i].a_af, hostname, pton)) { + u_long v4a; +#ifdef ENABLE_IPV6 + u_char pfx; +#endif + + switch (gai_afdl[i].a_af) { + case AF_INET: + v4a = ((struct in_addr *)pton)->s_addr; + v4a = ntohl(v4a); + if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a)) + pai->ai_flags &= ~AI_CANONNAME; + v4a >>= IN_CLASSA_NSHIFT; + if (v4a == 0 || v4a == IN_LOOPBACKNET) + pai->ai_flags &= ~AI_CANONNAME; + break; +#ifdef ENABLE_IPV6 + case AF_INET6: + pfx = ((struct in6_addr *)pton)->s6_addr8[0]; + if (pfx == 0 || pfx == 0xfe || pfx == 0xff) + pai->ai_flags &= ~AI_CANONNAME; + break; +#endif + } + + if (pai->ai_family == gai_afdl[i].a_af || + pai->ai_family == PF_UNSPEC) { + if (! (pai->ai_flags & AI_CANONNAME)) { + GET_AI(top, &gai_afdl[i], pton, port); + goto good; + } + /* + * if AI_CANONNAME and if reverse lookup + * fail, return ai anyway to pacify + * calling application. + * + * XXX getaddrinfo() is a name->address + * translation function, and it looks strange + * that we do addr->name translation here. + */ + get_name(pton, &gai_afdl[i], &top, pton, pai, port); + goto good; + } else + ERR(EAI_FAMILY); /*xxx*/ + } + } + + if (pai->ai_flags & AI_NUMERICHOST) + ERR(EAI_NONAME); + + /* hostname as alphabetical name */ + error = get_addr(hostname, pai->ai_family, &top, pai, port); + if (error == 0) { + if (top) { + good: + *res = top; + return SUCCESS; + } else + error = EAI_FAIL; + } + free: + if (top) + freeaddrinfo(top); + bad: + *res = NULL; + return error; +} + +static int +get_name(addr, gai_afd, res, numaddr, pai, port0) + const char *addr; + struct gai_afd *gai_afd; + struct addrinfo **res; + char *numaddr; + struct addrinfo *pai; + int port0; +{ + u_short port = port0 & 0xffff; + struct hostent *hp; + struct addrinfo *cur; + int error = 0; +#ifdef ENABLE_IPV6 + int h_error; +#endif + +#ifdef ENABLE_IPV6 + hp = getipnodebyaddr(addr, gai_afd->a_addrlen, gai_afd->a_af, &h_error); +#else + hp = gethostbyaddr(addr, gai_afd->a_addrlen, AF_INET); +#endif + if (hp && hp->h_name && hp->h_name[0] && hp->h_addr_list[0]) { + GET_AI(cur, gai_afd, hp->h_addr_list[0], port); + GET_CANONNAME(cur, hp->h_name); + } else + GET_AI(cur, gai_afd, numaddr, port); + +#ifdef ENABLE_IPV6 + if (hp) + freehostent(hp); +#endif + *res = cur; + return SUCCESS; + free: + if (cur) + freeaddrinfo(cur); +#ifdef ENABLE_IPV6 + if (hp) + freehostent(hp); +#endif + /* bad: */ + *res = NULL; + return error; +} + +static int +get_addr(hostname, af, res, pai, port0) + const char *hostname; + int af; + struct addrinfo **res; + struct addrinfo *pai; + int port0; +{ + u_short port = port0 & 0xffff; + struct addrinfo sentinel; + struct hostent *hp; + struct addrinfo *top, *cur; + struct gai_afd *gai_afd; + int i, error = 0, h_error; + char *ap; + + top = NULL; + sentinel.ai_next = NULL; + cur = &sentinel; +#ifdef ENABLE_IPV6 + if (af == AF_UNSPEC) { + hp = getipnodebyname(hostname, AF_INET6, + AI_ADDRCONFIG|AI_ALL|AI_V4MAPPED, &h_error); + } else + hp = getipnodebyname(hostname, af, AI_ADDRCONFIG, &h_error); +#else + hp = gethostbyname(hostname); + h_error = h_errno; +#endif + if (hp == NULL) { + switch (h_error) { + case HOST_NOT_FOUND: + case NO_DATA: + error = EAI_NODATA; + break; + case TRY_AGAIN: + error = EAI_AGAIN; + break; + case NO_RECOVERY: + default: + error = EAI_FAIL; + break; + } + goto free; + } + + if ((hp->h_name == NULL) || (hp->h_name[0] == 0) || + (hp->h_addr_list[0] == NULL)) { + error = EAI_FAIL; + goto free; + } + + for (i = 0; (ap = hp->h_addr_list[i]) != NULL; i++) { + switch (af) { +#ifdef ENABLE_IPV6 + case AF_INET6: + gai_afd = &gai_afdl[N_INET6]; + break; +#endif +#ifndef ENABLE_IPV6 + default: /* AF_UNSPEC */ +#endif + case AF_INET: + gai_afd = &gai_afdl[N_INET]; + break; +#ifdef ENABLE_IPV6 + default: /* AF_UNSPEC */ + if (IN6_IS_ADDR_V4MAPPED((struct in6_addr *)ap)) { + ap += sizeof(struct in6_addr) - + sizeof(struct in_addr); + gai_afd = &gai_afdl[N_INET]; + } else + gai_afd = &gai_afdl[N_INET6]; + break; +#endif + } +#ifdef FAITH + if (translate && gai_afd->a_af == AF_INET) { + struct in6_addr *in6; + + GET_AI(cur->ai_next, &gai_afdl[N_INET6], ap, port); + in6 = &((struct sockaddr_in6 *)cur->ai_next->ai_addr)->sin6_addr; + memcpy(&in6->s6_addr32[0], &faith_prefix, + sizeof(struct in6_addr) - sizeof(struct in_addr)); + memcpy(&in6->s6_addr32[3], ap, sizeof(struct in_addr)); + } else +#endif /* FAITH */ + GET_AI(cur->ai_next, gai_afd, ap, port); + if (cur == &sentinel) { + top = cur->ai_next; + GET_CANONNAME(top, hp->h_name); + } + cur = cur->ai_next; + } +#ifdef ENABLE_IPV6 + freehostent(hp); +#endif + *res = top; + return SUCCESS; + free: + if (top) + freeaddrinfo(top); +#ifdef ENABLE_IPV6 + if (hp) + freehostent(hp); +#endif +/* bad: */ + *res = NULL; + return error; +} \ No newline at end of file diff --git a/include/xbox/getnameinfo.c b/include/xbox/getnameinfo.c new file mode 100644 index 00000000..cca252c0 --- /dev/null +++ b/include/xbox/getnameinfo.c @@ -0,0 +1,243 @@ +/* + * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the project nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +/* + * Issues to be discussed: + * - Thread safe-ness must be checked + * - Return values. There seems to be no standard for return value (RFC2553) + * but INRIA implementation returns EAI_xxx defined for getaddrinfo(). + * - RFC2553 says that we should raise error on short buffer. X/Open says + * we need to truncate the result. We obey RFC2553 (and X/Open should be + * modified). + */ + +#ifdef _XBOX +#include +#include +#else +#include +#endif +#include +#include "addrinfo.h" +#include "emu_socket.h" +#include +#include +#include +#include +#include +#include +#include + +#ifdef _XBOX +extern struct mphostent* __stdcall sckemu_gethostbyname(const char* name); +#define gethostbyname(name) sckemu_gethostbyname(name) + +extern struct mphostent* __stdcall sckemu_gethostbyaddr(const char* addr, int len, int type); +#define gethostbyaddr(addr, len, type) sckemu_gethostbyaddr(addr, len, type) +#endif + +extern struct servent* __stdcall sckemu_getservbyname(const char* name, const char* proto); +#define getservbyname(name, proto) sckemu_getservbyname(name, proto) + +extern struct servent* __stdcall sckemu_getservbyport(int port, const char* proto); +#define getservbyport(port, proto) sckemu_getservbyport(port, proto) + +#define hostent mphostent + +#ifndef HAVE_U_INT32_T +#include "bittypes.h" +#endif + +#ifdef NEED_ADDRINFO_H +#include "addrinfo.h" +#endif + +#define SUCCESS 0 +#define YES 1 +#define NO 0 + +static struct gni_afd { + int a_af; + int a_addrlen; + int a_socklen; + int a_off; +} gni_afdl [] = { +#ifdef ENABLE_IPV6 + {PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6), + offsetof(struct sockaddr_in6, sin6_addr)}, +#endif + {PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in), + offsetof(struct sockaddr_in, sin_addr)}, + {0, 0, 0}, +}; + +struct gni_sockinet { + u_char si_len; + u_char si_family; + u_short si_port; +}; + +#define ENI_NOSOCKET 0 +#define ENI_NOSERVNAME 1 +#define ENI_NOHOSTNAME 2 +#define ENI_MEMORY 3 +#define ENI_SYSTEM 4 +#define ENI_FAMILY 5 +#define ENI_SALEN 6 + +/* forward declaration to make gcc happy */ +int getnameinfo (const struct sockaddr *, size_t, char *, size_t, char *, size_t, int); + +int +getnameinfo(sa, salen, host, hostlen, serv, servlen, flags) + const struct sockaddr *sa; + size_t salen; + char *host; + size_t hostlen; + char *serv; + size_t servlen; + int flags; +{ + struct gni_afd *gni_afd; + struct servent *sp; + struct hostent *hp; + u_short port; + int family, len, i; + char *addr, *p; + u_long v4a; +#ifdef ENABLE_IPV6 + u_char pfx; +#endif + int h_error; + char numserv[512]; + char numaddr[512]; + + if (sa == NULL) + return ENI_NOSOCKET; + +#ifdef HAVE_SOCKADDR_SA_LEN + len = sa->sa_len; + if (len != salen) return ENI_SALEN; +#else + len = salen; +#endif + + family = sa->sa_family; + for (i = 0; gni_afdl[i].a_af; i++) + if (gni_afdl[i].a_af == family) { + gni_afd = &gni_afdl[i]; + goto found; + } + return ENI_FAMILY; + + found: + if (len != gni_afd->a_socklen) return ENI_SALEN; + + port = ((struct gni_sockinet *)sa)->si_port; /* network byte order */ + addr = (char *)sa + gni_afd->a_off; + + if (serv == NULL || servlen == 0) { + /* what we should do? */ + } else if (flags & NI_NUMERICSERV) { + sprintf(numserv, "%d", ntohs(port)); + if (strlen(numserv) > servlen) + return ENI_MEMORY; + strcpy(serv, numserv); + } else { + sp = getservbyport(port, (flags & NI_DGRAM) ? "udp" : "tcp"); + if (sp) { + if (strlen(sp->s_name) > servlen) + return ENI_MEMORY; + strcpy(serv, sp->s_name); + } else + return ENI_NOSERVNAME; + } + + switch (sa->sa_family) { + case AF_INET: + v4a = ((struct sockaddr_in *)sa)->sin_addr.s_addr; + if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a)) + flags |= NI_NUMERICHOST; + v4a >>= IN_CLASSA_NSHIFT; + if (v4a == 0 || v4a == IN_LOOPBACKNET) + flags |= NI_NUMERICHOST; + break; +#ifdef ENABLE_IPV6 + case AF_INET6: + pfx = ((struct sockaddr_in6 *)sa)->sin6_addr.s6_addr8[0]; + if (pfx == 0 || pfx == 0xfe || pfx == 0xff) + flags |= NI_NUMERICHOST; + break; +#endif + } + if (host == NULL || hostlen == 0) { + /* what should we do? */ + } else if (flags & NI_NUMERICHOST) { + if (inet_ntop(gni_afd->a_af, addr, numaddr, sizeof(numaddr)) + == NULL) + return ENI_SYSTEM; + if (strlen(numaddr) > hostlen) + return ENI_MEMORY; + strcpy(host, numaddr); + } else { +#ifdef ENABLE_IPV6 + hp = getipnodebyaddr(addr, gni_afd->a_addrlen, gni_afd->a_af, &h_error); +#else + hp = gethostbyaddr(addr, gni_afd->a_addrlen, gni_afd->a_af); + h_error = h_errno; +#endif + + if (hp) { + if (flags & NI_NOFQDN) { + p = strchr(hp->h_name, '.'); + if (p) *p = '\0'; + } + if (strlen(hp->h_name) > hostlen) { +#ifdef ENABLE_IPV6 + freehostent(hp); +#endif + return ENI_MEMORY; + } + strcpy(host, hp->h_name); +#ifdef ENABLE_IPV6 + freehostent(hp); +#endif + } else { + if (flags & NI_NAMEREQD) + return ENI_NOHOSTNAME; + if (inet_ntop(gni_afd->a_af, addr, numaddr, sizeof(numaddr)) + == NULL) + return ENI_NOHOSTNAME; + if (strlen(numaddr) > hostlen) + return ENI_MEMORY; + strcpy(host, numaddr); + } + } + return SUCCESS; +} diff --git a/include/xbox/inet_aton.c b/include/xbox/inet_aton.c new file mode 100644 index 00000000..59fce171 --- /dev/null +++ b/include/xbox/inet_aton.c @@ -0,0 +1,148 @@ +/*- + * Copyright (c) 2001 Charles Mott + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifdef _XBOX +#include +#include +#else +#include +#endif +typedef unsigned long in_addr_t; +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef INADDR_NONE +#define INADDR_NONE 0xffffffff +#endif + +int +inet_aton(const char *cp, struct in_addr *addr) +{ + u_long parts[4]; + in_addr_t val; + const char *c; + char *endptr; + int gotend, n; + + c = (const char *)cp; + n = 0; + + /* + * Run through the string, grabbing numbers until + * the end of the string, or some error + */ + gotend = 0; + while (!gotend) { + unsigned long l; + + l = strtoul(c, &endptr, 0); + + if (l == ULONG_MAX || (l == 0 && endptr == c)) + return (0); + + val = (in_addr_t)l; + + /* + * If the whole string is invalid, endptr will equal + * c.. this way we can make sure someone hasn't + * gone '.12' or something which would get past + * the next check. + */ + if (endptr == c) + return (0); + parts[n] = val; + c = endptr; + + /* Check the next character past the previous number's end */ + switch (*c) { + case '.' : + + /* Make sure we only do 3 dots .. */ + if (n == 3) /* Whoops. Quit. */ + return (0); + n++; + c++; + break; + + case '\0': + gotend = 1; + break; + + default: + if (isspace((unsigned char)*c)) { + gotend = 1; + break; + } else { + + /* Invalid character, then fail. */ + return (0); + } + } + + } + + /* Concoct the address according to the number of parts specified. */ + switch (n) { + case 0: /* a -- 32 bits */ + + /* + * Nothing is necessary here. Overflow checking was + * already done in strtoul(). + */ + break; + case 1: /* a.b -- 8.24 bits */ + if (val > 0xffffff || parts[0] > 0xff) + return (0); + val |= parts[0] << 24; + break; + + case 2: /* a.b.c -- 8.8.16 bits */ + if (val > 0xffff || parts[0] > 0xff || parts[1] > 0xff) + return (0); + val |= (parts[0] << 24) | (parts[1] << 16); + break; + + case 3: /* a.b.c.d -- 8.8.8.8 bits */ + if (val > 0xff || parts[0] > 0xff || parts[1] > 0xff || + parts[2] > 0xff) + return (0); + val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8); + break; + } + + if (addr != NULL) + addr->s_addr = htonl(val); + return (1); +} + +#ifdef __cplusplus +} +#endif diff --git a/include/xbox/inet_ntop.c b/include/xbox/inet_ntop.c new file mode 100644 index 00000000..fbe5d231 --- /dev/null +++ b/include/xbox/inet_ntop.c @@ -0,0 +1,200 @@ +/* + * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") + * Copyright (c) 1996-1999 by Internet Software Consortium. + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT + * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#ifdef _XBOX +#include +#include +#else +#include +#endif +typedef int socklen_t; +#define snprintf _snprintf +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef IN6ADDRSZ +#define IN6ADDRSZ 16 /* IPv6 T_AAAA */ +#endif + +#ifndef INT16SZ +#define INT16SZ 2 /* word size */ +#endif + +#ifndef HAVE_STRLCPY +#define strlcpy(d,s,l) (strncpy(d,s,l), (d)[(l)-1] = '\0') +#endif + +/*% + * WARNING: Don't even consider trying to compile this on a system where + * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX. + */ + +static const char *inet_ntop4(const u_char *src, char *dst, socklen_t size); +static const char *inet_ntop6(const u_char *src, char *dst, socklen_t size); + +/* char * + * inet_ntop(af, src, dst, size) + * convert a network format address to presentation format. + * return: + * pointer to presentation format address (`dst'), or NULL (see errno). + * author: + * Paul Vixie, 1996. + */ +const char * +inet_ntop(int af, const void * src, char * dst, + socklen_t size) +{ + switch (af) { + case AF_INET: + return (inet_ntop4((u_char*)src, dst, size)); + default: + return (NULL); + } + /* NOTREACHED */ +} + +/* const char * + * inet_ntop4(src, dst, size) + * format an IPv4 address + * return: + * `dst' (as a const) + * notes: + * (1) uses no statics + * (2) takes a u_char* not an in_addr as input + * author: + * Paul Vixie, 1996. + */ +static const char * +inet_ntop4(const u_char *src, char *dst, socklen_t size) +{ + static const char fmt[] = "%u.%u.%u.%u"; + char tmp[sizeof "255.255.255.255"]; + int l; + + l = snprintf(tmp, sizeof(tmp), fmt, src[0], src[1], src[2], src[3]); + if (l <= 0 || (socklen_t) l >= size) { + return (NULL); + } + strlcpy(dst, tmp, size); + return (dst); +} + +/* const char * + * inet_ntop6(src, dst, size) + * convert IPv6 binary address into presentation (printable) format + * author: + * Paul Vixie, 1996. + */ +static const char * +inet_ntop6(const u_char *src, char *dst, socklen_t size) +{ + /* + * Note that int32_t and int16_t need only be "at least" large enough + * to contain a value of the specified size. On some systems, like + * Crays, there is no such thing as an integer variable with 16 bits. + * Keep this in mind if you think this function should have been coded + * to use pointer overlays. All the world's not a VAX. + */ + char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"], *tp; + struct { int base, len; } best, cur; +#define NS_IN6ADDRSZ 16 +#define NS_INT16SZ 2 + u_int words[NS_IN6ADDRSZ / NS_INT16SZ]; + int i; + + /* + * Preprocess: + * Copy the input (bytewise) array into a wordwise array. + * Find the longest run of 0x00's in src[] for :: shorthanding. + */ + memset(words, '\0', sizeof words); + for (i = 0; i < NS_IN6ADDRSZ; i++) + words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3)); + best.base = -1; + best.len = 0; + cur.base = -1; + cur.len = 0; + for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) { + if (words[i] == 0) { + if (cur.base == -1) + cur.base = i, cur.len = 1; + else + cur.len++; + } else { + if (cur.base != -1) { + if (best.base == -1 || cur.len > best.len) + best = cur; + cur.base = -1; + } + } + } + if (cur.base != -1) { + if (best.base == -1 || cur.len > best.len) + best = cur; + } + if (best.base != -1 && best.len < 2) + best.base = -1; + + /* + * Format the result. + */ + tp = tmp; + for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) { + /* Are we inside the best run of 0x00's? */ + if (best.base != -1 && i >= best.base && + i < (best.base + best.len)) { + if (i == best.base) + *tp++ = ':'; + continue; + } + /* Are we following an initial run of 0x00s or any real hex? */ + if (i != 0) + *tp++ = ':'; + /* Is this address an encapsulated IPv4? */ + if (i == 6 && best.base == 0 && (best.len == 6 || + (best.len == 7 && words[7] != 0x0001) || + (best.len == 5 && words[5] == 0xffff))) { + if (!inet_ntop4(src+12, tp, sizeof tmp - (tp - tmp))) + return (NULL); + tp += strlen(tp); + break; + } + tp += sprintf(tp, "%x", words[i]); + } + /* Was it a trailing run of 0x00's? */ + if (best.base != -1 && (best.base + best.len) == + (NS_IN6ADDRSZ / NS_INT16SZ)) + *tp++ = ':'; + *tp++ = '\0'; + + /* + * Check for overflow, copy, and we're done. + */ + if ((socklen_t)(tp - tmp) > size) { + return (NULL); + } + strcpy(dst, tmp); + return (dst); +} + +#ifdef __cplusplus +} +#endif diff --git a/include/xbox/inet_pton.c b/include/xbox/inet_pton.c new file mode 100644 index 00000000..3f7e7390 --- /dev/null +++ b/include/xbox/inet_pton.c @@ -0,0 +1,234 @@ +/* + * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") + * Copyright (c) 1996,1999 by Internet Software Consortium. + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT + * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#ifdef _XBOX +#include +#include +#else +#include +#endif +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef __STDC__ +#define __weak_reference(sym,alias) \ + __asm__(".weak " #alias); \ + __asm__(".equ " #alias ", " #sym) +#else +#define __weak_reference(sym,alias) \ + __asm__(".weak alias"); \ + __asm__(".equ alias, sym") +#endif /* __STDC__ */ + +/*% + * WARNING: Don't even consider trying to compile this on a system where + * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX. + */ + +static int inet_pton4(const char *src, u_char *dst); +static int inet_pton6(const char *src, u_char *dst); + +/* int + * inet_pton(af, src, dst) + * convert from presentation format (which usually means ASCII printable) + * to network format (which is usually some kind of binary format). + * return: + * 1 if the address was valid for the specified address family + * 0 if the address wasn't valid (`dst' is untouched in this case) + * -1 if some other error occurred (`dst' is untouched in this case, too) + * author: + * Paul Vixie, 1996. + */ +int +inet_pton(int af, const char * src, void * dst) +{ + switch (af) { + case AF_INET: + return (inet_pton4(src, (u_char*)dst)); + default: + return (-1); + } + /* NOTREACHED */ +} + +/* int + * inet_pton4(src, dst) + * like inet_aton() but without all the hexadecimal and shorthand. + * return: + * 1 if `src' is a valid dotted quad, else 0. + * notice: + * does not touch `dst' unless it's returning 1. + * author: + * Paul Vixie, 1996. + */ +static int +inet_pton4(const char *src, u_char *dst) +{ + static const char digits[] = "0123456789"; + int saw_digit, octets, ch; +#define NS_INADDRSZ 4 + u_char tmp[NS_INADDRSZ], *tp; + + saw_digit = 0; + octets = 0; + *(tp = tmp) = 0; + while ((ch = *src++) != '\0') { + const char *pch; + + if ((pch = strchr(digits, ch)) != NULL) { + u_int c_new = *tp * 10 + (pch - digits); + + if (saw_digit && *tp == 0) + return (0); + if (c_new > 255) + return (0); + *tp = c_new; + if (!saw_digit) { + if (++octets > 4) + return (0); + saw_digit = 1; + } + } else if (ch == '.' && saw_digit) { + if (octets == 4) + return (0); + *++tp = 0; + saw_digit = 0; + } else + return (0); + } + if (octets < 4) + return (0); + memcpy(dst, tmp, NS_INADDRSZ); + return (1); +} + +/* int + * inet_pton6(src, dst) + * convert presentation level address to network order binary form. + * return: + * 1 if `src' is a valid [RFC1884 2.2] address, else 0. + * notice: + * (1) does not touch `dst' unless it's returning 1. + * (2) :: in a full address is silently ignored. + * credit: + * inspired by Mark Andrews. + * author: + * Paul Vixie, 1996. + */ +static int +inet_pton6(const char *src, u_char *dst) +{ + static const char xdigits_l[] = "0123456789abcdef", + xdigits_u[] = "0123456789ABCDEF"; +#define NS_IN6ADDRSZ 16 +#define NS_INT16SZ 2 + u_char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp; + const char *xdigits, *curtok; + int ch, seen_xdigits; + u_int val; + + memset((tp = tmp), '\0', NS_IN6ADDRSZ); + endp = tp + NS_IN6ADDRSZ; + colonp = NULL; + /* Leading :: requires some special handling. */ + if (*src == ':') + if (*++src != ':') + return (0); + curtok = src; + seen_xdigits = 0; + val = 0; + while ((ch = *src++) != '\0') { + const char *pch; + + if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL) + pch = strchr((xdigits = xdigits_u), ch); + if (pch != NULL) { + val <<= 4; + val |= (pch - xdigits); + if (++seen_xdigits > 4) + return (0); + continue; + } + if (ch == ':') { + curtok = src; + if (!seen_xdigits) { + if (colonp) + return (0); + colonp = tp; + continue; + } else if (*src == '\0') { + return (0); + } + if (tp + NS_INT16SZ > endp) + return (0); + *tp++ = (u_char) (val >> 8) & 0xff; + *tp++ = (u_char) val & 0xff; + seen_xdigits = 0; + val = 0; + continue; + } + if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) && + inet_pton4(curtok, tp) > 0) { + tp += NS_INADDRSZ; + seen_xdigits = 0; + break; /*%< '\\0' was seen by inet_pton4(). */ + } + return (0); + } + if (seen_xdigits) { + if (tp + NS_INT16SZ > endp) + return (0); + *tp++ = (u_char) (val >> 8) & 0xff; + *tp++ = (u_char) val & 0xff; + } + if (colonp != NULL) { + /* + * Since some memmove()'s erroneously fail to handle + * overlapping regions, we'll do the shift by hand. + */ + const int n = tp - colonp; + int i; + + if (tp == endp) + return (0); + for (i = 1; i <= n; i++) { + endp[- i] = colonp[n - i]; + colonp[n - i] = 0; + } + tp = endp; + } + if (tp != endp) + return (0); + memcpy(dst, tmp, NS_IN6ADDRSZ); + return (1); +} + +/* + * Weak aliases for applications that use certain private entry points, + * and fail to include . + */ +#ifndef _WIN32 +#undef inet_pton +__weak_reference(__inet_pton, inet_pton); +#endif + +#ifdef __cplusplus +} +#endif diff --git a/include/xbox/sockstorage.h b/include/xbox/sockstorage.h new file mode 100644 index 00000000..e7333289 --- /dev/null +++ b/include/xbox/sockstorage.h @@ -0,0 +1,38 @@ +/* + * Copyright (C) 1999 WIDE Project. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the project nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ +struct sockaddr_storage { +#ifdef HAVE_SOCKADDR_SA_LEN + u_int8_t ss_len; + u_int8_t ss_family; + u_int8_t fill[126]; +#else + u_int8_t ss_family; + u_int8_t fill[127]; +#endif /* HAVE_SOCKADDR_SA_LEN */ +}; diff --git a/include/xbox/xb_emu_socket.h b/include/xbox/xb_emu_socket.h new file mode 100644 index 00000000..6525b5e7 --- /dev/null +++ b/include/xbox/xb_emu_socket.h @@ -0,0 +1,85 @@ +#ifndef _EMU_SOCKET_H +#define _EMU_SOCKET_H + +/* + * Copyright (C) 2005-2013 Team XBMC + * http://xbmc.org + * + * This Program 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 Foundation; either version 2, or (at your option) + * any later version. + * + * This Program 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 XBMC; see the file COPYING. If not, see + * . + * + */ + +#include "emu_socket.h" +#include "addrinfo.h" +#include "bittypes.h" +#include "sockstorage.h" + +// OS support start +typedef int socklen_t; + +#define POLLIN 0x0001 /* There is data to read */ +#define POLLPRI 0x0002 /* There is urgent data to read */ +#define POLLOUT 0x0004 /* Writing now will not block */ +#define POLLERR 0x0008 /* Error condition */ +#define POLLHUP 0x0010 /* Hung up */ + +#define ssize_t __int64 + +// OS support end + +#ifdef __cplusplus +extern "C" +{ +#endif + + struct mphostent* __stdcall sckemu_gethostbyname(const char* name); +//char* __stdcall inet_ntoa(in_addr in); + int __stdcall sckemu_connect(int s, const struct sockaddr FAR *name, int namelen); + int __stdcall sckemu_send(int s, const char FAR *buf, int len, int flags); + int __stdcall sckemu_socket(int af, int type, int protocol); + int __stdcall sckemu_bind(int s, const struct sockaddr FAR * name, int namelen); + int __stdcall sckemu_closesocket(int s); + int __stdcall sckemu_getsockopt(int s, int level, int optname, char FAR * optval, int FAR * optlen); + int __stdcall sckemu_ioctlsocket(int s, long cmd, DWORD FAR * argp); + int __stdcall sckemu_recv(int s, char FAR * buf, int len, int flags); + int __stdcall sckemu_select(int nfds, fd_set FAR * readfds, fd_set FAR * writefds, fd_set FAR *exceptfds, const struct timeval FAR * timeout); + int __stdcall sckemu_sendto(int s, const char FAR * buf, int len, int flags, const struct sockaddr FAR * to, int tolen); + int __stdcall sckemu_setsockopt(int s, int level, int optname, const char FAR * optval, int optlen); + int __stdcall sckemu_WSAFDIsSet(int fd, fd_set* set); + + int __stdcall sckemu_accept(int s, struct sockaddr FAR * addr, OUT int FAR * addrlen); + int __stdcall sckemu_gethostname(char* name, int namelen); + int __stdcall sckemu_getsockname(int s, struct sockaddr* name, int* namelen); + int __stdcall sckemu_listen(int s, int backlog); + u_short __stdcall sckemu_ntohs(u_short netshort); + int __stdcall sckemu_recvfrom(int s, char* buf, int len, int flags, struct sockaddr* from, int* fromlen); + int __stdcall sckemu_shutdown(int s, int how); + char* __stdcall sckemu_ntoa(struct in_addr in); + + struct servent* __stdcall sckemu_getservbyname(const char* name,const char* proto); + struct protoent* __stdcall sckemu_getprotobyname(const char* name); + int __stdcall sckemu_getpeername(int s, struct sockaddr FAR *name, int FAR *namelen); + struct servent* __stdcall sckemu_getservbyport(int port, const char* proto); + struct mphostent* __stdcall sckemu_gethostbyaddr(const char* addr, int len, int type); + + int __stdcall sckemu_getaddrinfo(const char* nodename, const char* servname, const struct addrinfo* hints, struct addrinfo** res); + int __stdcall sckemu_getnameinfo(const struct sockaddr *sa, size_t salen, char *host, size_t hostlen, char *serv, size_t servlen, int flags); + void __stdcall sckemu_freeaddrinfo(struct addrinfo *ai); + +#ifdef __cplusplus +} +#endif + +#endif // _EMU_SOCKET_H \ No newline at end of file diff --git a/lib/aes.c b/lib/aes.c index 1526dfde..96e7350e 100644 --- a/lib/aes.c +++ b/lib/aes.c @@ -443,12 +443,12 @@ static void BlockCopy(uint8_t* output, uint8_t* input) void AES128_ECB_encrypt(uint8_t* input, const uint8_t* key, uint8_t* output) { - // Copy input to output, and work in-memory on output - BlockCopy(output, input); - // The array that stores the round keys. uint8_t roundKey[176]; + // Copy input to output, and work in-memory on output + BlockCopy(output, input); + KeyExpansion(key, roundKey); // The next function call encrypts the PlainText with the Key using AES algorithm. @@ -457,11 +457,11 @@ void AES128_ECB_encrypt(uint8_t* input, const uint8_t* key, uint8_t* output) void AES128_ECB_decrypt(uint8_t* input, const uint8_t* key, uint8_t *output) { - // Copy input to output, and work in-memory on output - BlockCopy(output, input); - // The array that stores the round keys. uint8_t roundKey[176]; + + // Copy input to output, and work in-memory on output + BlockCopy(output, input); // The KeyExpansion routine must be called before encryption. KeyExpansion(key, roundKey); diff --git a/lib/compat.c b/lib/compat.c index f84ec6f2..cb479834 100644 --- a/lib/compat.c +++ b/lib/compat.c @@ -173,7 +173,13 @@ ssize_t writev(int fd, const struct iovec *vector, int count) { /* Find the total number of bytes to be written. */ size_t bytes = 0; - for (int i = 0; i < count; ++i) { + int i; + char *buffer; + char *bp; + size_t to_copy; + ssize_t bytes_written; + + for (i = 0; i < count; ++i) { /* Check for ssize_t overflow. */ if (((ssize_t)-1) - bytes < vector[i].iov_len) { errno = EINVAL; @@ -182,16 +188,16 @@ ssize_t writev(int fd, const struct iovec *vector, int count) bytes += vector[i].iov_len; } - char *buffer = (char *)malloc(bytes); + buffer = (char *)malloc(bytes); if (buffer == NULL) /* XXX I don't know whether it is acceptable to try writing the data in chunks. Probably not so we just fail here. */ return -1; /* Copy the data into BUFFER. */ - size_t to_copy = bytes; - char *bp = buffer; - for (int i = 0; i < count; ++i) { + to_copy = bytes; + bp = buffer; + for (i = 0; i < count; ++i) { size_t copy = (vector[i].iov_len < to_copy) ? vector[i].iov_len : to_copy; memcpy((void *)bp, (void *)vector[i].iov_base, copy); @@ -202,7 +208,7 @@ ssize_t writev(int fd, const struct iovec *vector, int count) break; } - ssize_t bytes_written = write(fd, buffer, bytes); + bytes_written = write(fd, buffer, bytes); free(buffer); return bytes_written; @@ -214,7 +220,11 @@ ssize_t readv (int fd, const struct iovec *vector, int count) { /* Find the total number of bytes to be read. */ size_t bytes = 0; - for (int i = 0; i < count; ++i) + int i; + char *buffer; + ssize_t bytes_read; + char *bp; + for (i = 0; i < count; ++i) { /* Check for ssize_t overflow. */ if (((ssize_t)-1) - bytes < vector[i].iov_len) { @@ -224,12 +234,12 @@ ssize_t readv (int fd, const struct iovec *vector, int count) bytes += vector[i].iov_len; } - char *buffer = (char *)malloc(bytes); + buffer = (char *)malloc(bytes); if (buffer == NULL) return -1; /* Read the data. */ - ssize_t bytes_read = read(fd, buffer, bytes); + bytes_read = read(fd, buffer, bytes); if (bytes_read < 0) { free(buffer); return -1; @@ -237,8 +247,8 @@ ssize_t readv (int fd, const struct iovec *vector, int count) /* Copy the data from BUFFER into the memory specified by VECTOR. */ bytes = bytes_read; - char *bp = buffer; - for (int i = 0; i < count; ++i) { + bp = buffer; + for (i = 0; i < count; ++i) { size_t copy = (vector[i].iov_len < bytes) ? vector[i].iov_len : bytes; memcpy((void *)vector[i].iov_base, (void *)bp, copy); diff --git a/lib/compat.h b/lib/compat.h index 3b67db29..9d65e2d7 100644 --- a/lib/compat.h +++ b/lib/compat.h @@ -19,6 +19,67 @@ #ifndef _COMPAT_H_ #define _COMPAT_H_ +#ifdef _XBOX + +/* XBOX Defs begin */ +#include +#include + +#ifdef ORIGINAL_XBOX_PLATFORM /* MSVC 2003 Doesn´t have stdint.h header */ +typedef unsigned char uint8_t; + +#define uint64_t unsigned __int64 +#define uint32_t unsigned __int32 +#define uint16_t unsigned __int16 +#define uint_t unsigned int + +typedef short int16_t; +typedef short int_least16_t; +typedef int int32_t; +typedef long long int64_t; +typedef int intptr_t; +#else +#include /* XBOX 360 */ +#endif + +#ifdef _XBOX +#include "xbox/xb_emu_socket.h" +#endif + +#define snprintf _snprintf +#define _U_ + +//#define offsetof(st, m) ((size_t)((char *)&((st *)(1024))->m - (char *)1024)) + +/* XBOX Defs end */ + +#define POLLIN 0x0001 /* There is data to read */ +#define POLLPRI 0x0002 /* There is urgent data to read */ +#define POLLOUT 0x0004 /* Writing now will not block */ +#define POLLERR 0x0008 /* Error condition */ +#define POLLHUP 0x0010 /* Hung up */ + +struct pollfd { + int fd; + short events; + short revents; +}; + +typedef unsigned long nfds_t; + +#define sys_close(a) closesocket(a) + +#define SOL_TCP 6 + +#define inline __inline + +int poll(struct pollfd *fds, unsigned int nfds, int timo); + +/* just pretend they are the same so we compile */ +#define sockaddr_in6 sockaddr_in + +#endif //_XBOX + #ifdef PICO_PLATFORM #include "lwip/netdb.h" diff --git a/lib/hmac-md5.h b/lib/hmac-md5.h index 196dde9a..be6d92b8 100644 --- a/lib/hmac-md5.h +++ b/lib/hmac-md5.h @@ -15,7 +15,7 @@ #include #endif -#if (__BYTE_ORDER == __BIG_ENDIAN) +#if (__BYTE_ORDER == __BIG_ENDIAN) || defined(XBOX_360_PLATFORM) # define WORDS_BIGENDIAN 1 #endif diff --git a/lib/init.c b/lib/init.c index cb8469b7..b826dc2f 100644 --- a/lib/init.c +++ b/lib/init.c @@ -44,7 +44,7 @@ #include #include -#if !defined(PS2_IOP_PLATFORM) +#ifdef HAVE_TIME_H #include #endif @@ -63,6 +63,9 @@ #include "compat.h" #ifdef _MSC_VER +#ifdef XBOX_360_PLATFORM +#include +#endif #include #define getlogin_r(a,b) ENXIO #define srandom srand @@ -183,6 +186,8 @@ struct smb2_url *smb2_parse_url(struct smb2_context *smb2, const char *url) struct smb2_url *u; char *ptr, *tmp, str[MAX_URL_SIZE]; char *args; + char *shared_folder; + int len_shared_folder; if (strncmp(url, "smb://", 6)) { smb2_set_error(smb2, "URL does not start with 'smb://'"); @@ -210,12 +215,12 @@ struct smb2_url *smb2_parse_url(struct smb2_context *smb2, const char *url) ptr = str; - char *shared_folder = strchr(ptr, '/'); + shared_folder = strchr(ptr, '/'); if (!shared_folder) { smb2_set_error(smb2, "Wrong URL format"); return NULL; } - int len_shared_folder = strlen(shared_folder); + len_shared_folder = strlen(shared_folder); /* domain */ if ((tmp = strchr(ptr, ';')) != NULL && strlen(tmp) > len_shared_folder) { @@ -273,9 +278,11 @@ struct smb2_context *smb2_init_context(void) char buf[1024]; int i, ret; static int ctr; - +#ifdef XBOX_360_PLATFORM + srandom(time(NULL) ^ 1 ^ ctr++); +#else srandom(time(NULL) ^ getpid() ^ ctr++); - +#endif smb2 = calloc(1, sizeof(struct smb2_context)); if (smb2 == NULL) { return NULL; @@ -469,7 +476,7 @@ void smb2_set_security_mode(struct smb2_context *smb2, uint16_t security_mode) smb2->security_mode = security_mode; } -#if !defined(PS2_IOP_PLATFORM) +#if !defined(XBOX_360_PLATFORM) && !defined(PS2_IOP_PLATFORM) static void smb2_set_password_from_file(struct smb2_context *smb2) { char *name = NULL; diff --git a/lib/libsmb2.c b/lib/libsmb2.c index 19e9616b..12867da0 100644 --- a/lib/libsmb2.c +++ b/lib/libsmb2.c @@ -60,7 +60,7 @@ #include #endif -#if !defined(PS2_IOP_PLATFORM) || defined(__GNUC__) || defined(HAVE_TIME_H) || defined(_MSC_VER) +#if !defined(PS2_IOP_PLATFORM) || defined(__GNUC__) || defined(HAVE_FCNTL_H) || defined(_MSC_VER) #include #endif @@ -181,7 +181,7 @@ smb2_close_context(struct smb2_context *smb2) if (smb2->change_fd) { smb2->change_fd(smb2, smb2->fd, SMB2_DEL_FD); } - close(smb2->fd); + close(smb2->fd); smb2->fd = -1; } @@ -2524,7 +2524,7 @@ disconnect_cb_2(struct smb2_context *smb2, int status, if (smb2->change_fd) { smb2->change_fd(smb2, smb2->fd, SMB2_DEL_FD); } - close(smb2->fd); + close(smb2->fd); smb2->fd = -1; } diff --git a/lib/smb2-cmd-set-info.c b/lib/smb2-cmd-set-info.c index c408348c..08c2a5eb 100644 --- a/lib/smb2-cmd-set-info.c +++ b/lib/smb2-cmd-set-info.c @@ -59,6 +59,7 @@ smb2_encode_set_info_request(struct smb2_context *smb2, struct smb2_iovec *iov; struct smb2_file_end_of_file_info *eofi; struct smb2_file_rename_info *rni; + struct utf16 *name; len = SMB2_SET_INFO_REQUEST_SIZE & 0xfffffffe; buf = calloc(len, sizeof(uint8_t)); @@ -112,7 +113,7 @@ smb2_encode_set_info_request(struct smb2_context *smb2, case SMB2_FILE_RENAME_INFORMATION: rni = req->input_data; - struct utf16 *name = utf8_to_utf16((char *)(rni->file_name)); + name = utf8_to_utf16((char *)(rni->file_name)); if (name == NULL) { smb2_set_error(smb2, "Could not convert name into UTF-16"); return -1; diff --git a/lib/socket.c b/lib/socket.c index d7525bec..5a522409 100644 --- a/lib/socket.c +++ b/lib/socket.c @@ -72,11 +72,11 @@ #include "portable-endian.h" #include -#if !defined(PS2_IOP_PLATFORM) || defined(__GNUC__) || defined(HAVE_TIME_H) || defined(_MSC_VER) +#if defined(__GNUC__) || defined(HAVE_FCNTL_H) || defined(_MSC_VER) #include #endif -#if defined(HAVE_SYS_SOCKET_H) || defined(_WINDOWS) +#if defined(HAVE_SYS_SOCKET_H) || defined(_WINDOWS) #include #endif @@ -88,6 +88,8 @@ #include "smb3-seal.h" #include "libsmb2-private.h" + + #define MAX_URL_SIZE 1024 /* Timeout in ms between 2 consecutive socket connection. @@ -318,7 +320,7 @@ static int smb2_read_data(struct smb2_context *smb2, read_func func, /* Read into our trimmed iovectors */ count = func(smb2, tmpiov, niov); if (count < 0) { -#ifdef _WIN32 +#if defined(_WIN32) || defined(XBOX_360_PLATFORM) int err = WSAGetLastError(); if (err == WSAEINTR || err == WSAEWOULDBLOCK) { #else @@ -744,9 +746,13 @@ smb2_service_fd(struct smb2_context *smb2, int fd, int revents) if (smb2->fd == -1 && revents & POLLOUT) { int err = 0; socklen_t err_size = sizeof(err); - +#ifdef XBOX_360_PLATFORM + if (sckemu_getsockopt(fd, SOL_SOCKET, SO_ERROR, + (char *)&err, &err_size) != 0 || err != 0) { +#else if (getsockopt(fd, SOL_SOCKET, SO_ERROR, (char *)&err, &err_size) != 0 || err != 0) { +#endif if (err == 0) { err = errno; } @@ -819,7 +825,7 @@ smb2_service(struct smb2_context *smb2, int revents) static void set_nonblocking(t_socket fd) { -#if defined(WIN32) +#if defined(WIN32) || defined(XBOX_360_PLATFORM) unsigned long opt = 1; ioctlsocket(fd, FIONBIO, &opt); #else @@ -833,7 +839,7 @@ static int set_tcp_sockopt(t_socket sockfd, int optname, int value) { int level; -#ifndef SOL_TCP +#if !defined(SOL_TCP) && !defined(XBOX_360_PLATFORM) struct protoent *buf; if ((buf = getprotobyname("tcp")) != NULL) { @@ -855,8 +861,17 @@ connect_async_ai(struct smb2_context *smb2, const struct addrinfo *ai, int *fd_o t_socket fd; socklen_t socksize; struct sockaddr_storage ss; - - memset(&ss, 0, sizeof(ss)); +#ifdef XBOX_360_PLATFORM + BOOL bBroadcast = FALSE; +#endif +#ifndef XBOX_360_PLATFORM +#if 0 == CONFIGURE_OPTION_TCP_LINGER + int const yes; + struct LingerStruct { int l_onoff; /* linger active */ int l_linger; /* how many seconds to linger for */ }; + struct LingerStruct const lin = { .l_onoff = 1, .l_linger = 0 }; /* if l_linger is zero, sends RST after FIN */ +#endif +#endif + memset(&ss, 0, sizeof(ss)); switch (ai->ai_family) { case AF_INET: socksize = sizeof(struct sockaddr_in); @@ -865,6 +880,7 @@ connect_async_ai(struct smb2_context *smb2, const struct addrinfo *ai, int *fd_o ((struct sockaddr_in *)&ss)->sin_len = socksize; #endif break; +#ifdef ENABLE_IPV6 case AF_INET6: #if !defined(PICO_PLATFORM) || defined(LWIP_INETV6) socksize = sizeof(struct sockaddr_in6); @@ -872,6 +888,7 @@ connect_async_ai(struct smb2_context *smb2, const struct addrinfo *ai, int *fd_o #ifdef HAVE_SOCK_SIN_LEN ((struct sockaddr_in6 *)&ss)->sin6_len = socksize; #endif +#endif #endif break; default: @@ -890,15 +907,25 @@ connect_async_ai(struct smb2_context *smb2, const struct addrinfo *ai, int *fd_o return -EIO; } +#ifdef _XBOX + if(setsockopt(fd, SOL_SOCKET, 0x5801, (PCSTR)&bBroadcast, sizeof(BOOL) ) != 0 ) + { + //return 0; + } + if(setsockopt(fd, SOL_SOCKET, 0x5802, (PCSTR)&bBroadcast, sizeof(BOOL)) != 0) + { + //return 0; + } +#endif + set_nonblocking(fd); set_tcp_sockopt(fd, TCP_NODELAY, 1); - +#ifndef XBOX_360_PLATFORM #if 0 == CONFIGURE_OPTION_TCP_LINGER - int const yes = 1; + yes = 1; setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof yes); - struct LingerStruct { int l_onoff; /* linger active */ int l_linger; /* how many seconds to linger for */ }; - struct LingerStruct const lin = { .l_onoff = 1, .l_linger = 0 }; /* if l_linger is zero, sends RST after FIN */ setsockopt(fd, SOL_SOCKET, SO_LINGER, &lin, sizeof lin); +#endif #endif if (connect(fd, (struct sockaddr *)&ss, socksize) != 0 @@ -909,7 +936,7 @@ connect_async_ai(struct smb2_context *smb2, const struct addrinfo *ai, int *fd_o #endif smb2_set_error(smb2, "Connect failed with errno : " "%s(%d)", strerror(errno), errno); - close(fd); + close(fd); return -EIO; } @@ -1024,15 +1051,24 @@ smb2_connect_async(struct smb2_context *smb2, const char *server, } /* is it a hostname ? */ +#ifdef XBOX_360_PLATFORM + err = sckemu_getaddrinfo(host, port, NULL, &smb2->addrinfos); +#else err = getaddrinfo(host, port, NULL, &smb2->addrinfos); +#endif if (err != 0) { free(addr); -#ifdef _WINDOWS +#if defined(_WINDOWS) || defined(XBOX_360_PLATFORM) if (err == WSANOTINITIALISED) { - smb2_set_error(smb2, "Winsock was not initialized. " +#ifdef XBOX_360_PLATFORM + smb2_set_error(smb2, "Winsockx was not initialized. " + "Please call WSAStartup()."); +#else + smb2_set_error(smb2, "Winsock was not initialized. " "Please call WSAStartup()."); - return -WSANOTINITIALISED; +#endif + return -WSANOTINITIALISED; } else #endif diff --git a/lib/unicode.c b/lib/unicode.c index 60c0c2a3..980c7967 100644 --- a/lib/unicode.c +++ b/lib/unicode.c @@ -71,8 +71,8 @@ validate_utf8_cp(const char **utf8, uint16_t *ret) { int c = *(*utf8)++; int l, l_tmp; + uint32_t cp; l = l_tmp = l1(c); - uint32_t cp; switch (l) { case 0: @@ -208,12 +208,12 @@ utf16_size(const uint16_t *utf16, int utf16_len) } else if (code < 0xd800 || code - 0xe000 < 0x2000) { length += 3; } else if (code < 0xdc00) { /* Surrogate pair */ - + uint32_t trail; if (utf16 == utf16_end) { /* It's possible the stream ends with a leading code unit, which is an error */ return length + 3; /* Replacement char */ } - uint32_t trail = le16toh(*utf16); + trail = le16toh(*utf16); if (trail - 0xdc00 < 0x400) { /* Check that 0xdc00 <= trail < 0xe000 */ code = 0x10000 + ((code & 0x3ff) << 10) + (trail & 0x3ff); if (code < 0x10000) { @@ -241,6 +241,7 @@ utf16_to_utf8(const uint16_t *utf16, int utf16_len) { int utf8_len = 1; char *str, *tmp; + const uint16_t *utf16_end; /* How many bytes do we need for utf8 ? */ utf8_len += utf16_size(utf16, utf16_len); @@ -250,8 +251,9 @@ utf16_to_utf8(const uint16_t *utf16, int utf16_len) } str[utf8_len - 1] = 0; - const uint16_t *utf16_end = utf16 + utf16_len; - while (utf16 < utf16_end) { + utf16_end = utf16 + utf16_len; + + while (utf16 < utf16_end) { uint32_t code = le16toh(*utf16++); if (code < 0x80) { @@ -264,13 +266,13 @@ utf16_to_utf8(const uint16_t *utf16, int utf16_len) *tmp++ = 0x80 | ((code >> 6) & 0x3f); *tmp++ = 0x80 | ((code ) & 0x3f); } else if (code < 0xdc00) { /* Surrogate pair */ - + uint32_t trail; if (utf16 == utf16_end) { /* It's possible the stream ends with a leading code unit, which is an error */ *tmp++ = 0xef; *tmp++ = 0xbf; *tmp++ = 0xbd; /* Replacement char */ return str; } - uint32_t trail = le16toh(*utf16); + trail = le16toh(*utf16); if (trail - 0xdc00 < 0x400) { /* Check that 0xdc00 <= trail < 0xe000 */ code = 0x10000 + ((code & 0x3ff) << 10) + (trail & 0x3ff); if (code < 0x10000) { From 01f141b0f3bdc0601ed64bc5547bf51117907f5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Guilherme?= <75867486+Wolf3s@users.noreply.github.com> Date: Wed, 15 Nov 2023 22:38:10 -0300 Subject: [PATCH 02/23] Update socket.c --- lib/socket.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/socket.c b/lib/socket.c index 5a522409..298ed5f6 100644 --- a/lib/socket.c +++ b/lib/socket.c @@ -866,7 +866,7 @@ connect_async_ai(struct smb2_context *smb2, const struct addrinfo *ai, int *fd_o #endif #ifndef XBOX_360_PLATFORM #if 0 == CONFIGURE_OPTION_TCP_LINGER - int const yes; + int yes; struct LingerStruct { int l_onoff; /* linger active */ int l_linger; /* how many seconds to linger for */ }; struct LingerStruct const lin = { .l_onoff = 1, .l_linger = 0 }; /* if l_linger is zero, sends RST after FIN */ #endif From a60775f904a570a32ef12c1e429218fe72785042 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Guilherme?= <75867486+Wolf3s@users.noreply.github.com> Date: Thu, 16 Nov 2023 08:15:22 -0300 Subject: [PATCH 03/23] Update README --- README | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README b/README index 350037f5..9d648049 100644 --- a/README +++ b/README @@ -272,9 +272,9 @@ Xbox 360 CPU is a PPC(PowerPC) Xenon which only supports big endian values. To compile libsmb2 for the Xbox 360, first install the Xbox 360 SDK(with all features), Microsoft Visual C++ 2010 Ultimate and Windows XP(Recommended) or Windows 7. Then to build libsmb2, go to Xbox 360 folder -and open the provided .sln file, Then pres the green button to build: +and open the provided .sln file, Then hit the green button to build: -The process will generate the resulting libsmb2.lib. So you copy the include files -to your Xbox 360 project. +The process will result a libsmb2.lib. So you copy the include files +and the .lib file to your Xbox 360 project. NOTE: This port was based on XBMC-360 port by BDC(Brent De Cartet) and now being updated to libsmb2 standards to best performance. From fef302e38e4dbc1c57ee6b160d27575e2d2e5c76 Mon Sep 17 00:00:00 2001 From: Wolf3s Date: Fri, 17 Nov 2023 19:59:10 -0300 Subject: [PATCH 04/23] Add original xbox support --- Xbox 360/libsmb2.vcxproj | 2 +- Xbox/libsmb2.sln | 30 +++ Xbox/libsmb2.vcproj | 489 +++++++++++++++++++++++++++++++++++ include/asprintf.h | 8 +- include/msvc/sys/socket.h | 9 +- include/portable-endian.h | 30 ++- include/smb2/libsmb2.h | 4 +- include/xbox/xb_emu_socket.h | 5 +- lib/compat.h | 20 +- lib/errors.c | 33 ++- lib/init.c | 15 +- lib/socket.c | 26 +- 12 files changed, 625 insertions(+), 46 deletions(-) create mode 100644 Xbox/libsmb2.sln create mode 100644 Xbox/libsmb2.vcproj diff --git a/Xbox 360/libsmb2.vcxproj b/Xbox 360/libsmb2.vcxproj index cd90bf0d..2322a221 100644 --- a/Xbox 360/libsmb2.vcxproj +++ b/Xbox 360/libsmb2.vcxproj @@ -108,7 +108,7 @@ false $(OutDir)$(ProjectName).pch MultiThreadedDebug - _DEBUG;_XBOX;_LIB;HAVE_STDINT_H;HAVE_STDLIB_H;HAVE_STRING_H;HAVE_SYS_TYPES_H;HAVE_SYS_STAT_H;HAVE_TIME_H;STDC_HEADERS;XBOX_360_PLATFORM + _DEBUG;_XBOX;_LIB;HAVE_STDINT_H;HAVE_STDLIB_H;HAVE_STRING_H;HAVE_SYS_TYPES_H;HAVE_SYS_STAT_H;HAVE_TIME_H;STDC_HEADERS;NEED_POLL;XBOX_360_PLATFORM;HAVE_SYS_SOCKET_H Callcap CompileAsC ..\include\;..\include\smb2 diff --git a/Xbox/libsmb2.sln b/Xbox/libsmb2.sln new file mode 100644 index 00000000..d5b20889 --- /dev/null +++ b/Xbox/libsmb2.sln @@ -0,0 +1,30 @@ +Microsoft Visual Studio Solution File, Format Version 8.00 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libsmb2", "libsmb2.vcproj", "{7326DFF9-17C3-478F-9EAB-15FC697DC053}" + ProjectSection(ProjectDependencies) = postProject + EndProjectSection +EndProject +Global + GlobalSection(SolutionConfiguration) = preSolution + Debug = Debug + Profile = Profile + Profile_FastCap = Profile_FastCap + Release = Release + Release_LTCG = Release_LTCG + EndGlobalSection + GlobalSection(ProjectConfiguration) = postSolution + {7326DFF9-17C3-478F-9EAB-15FC697DC053}.Debug.ActiveCfg = Debug|Xbox + {7326DFF9-17C3-478F-9EAB-15FC697DC053}.Debug.Build.0 = Debug|Xbox + {7326DFF9-17C3-478F-9EAB-15FC697DC053}.Profile.ActiveCfg = Profile|Xbox + {7326DFF9-17C3-478F-9EAB-15FC697DC053}.Profile.Build.0 = Profile|Xbox + {7326DFF9-17C3-478F-9EAB-15FC697DC053}.Profile_FastCap.ActiveCfg = Profile_FastCap|Xbox + {7326DFF9-17C3-478F-9EAB-15FC697DC053}.Profile_FastCap.Build.0 = Profile_FastCap|Xbox + {7326DFF9-17C3-478F-9EAB-15FC697DC053}.Release.ActiveCfg = Release|Xbox + {7326DFF9-17C3-478F-9EAB-15FC697DC053}.Release.Build.0 = Release|Xbox + {7326DFF9-17C3-478F-9EAB-15FC697DC053}.Release_LTCG.ActiveCfg = Release_LTCG|Xbox + {7326DFF9-17C3-478F-9EAB-15FC697DC053}.Release_LTCG.Build.0 = Release_LTCG|Xbox + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + EndGlobalSection + GlobalSection(ExtensibilityAddIns) = postSolution + EndGlobalSection +EndGlobal diff --git a/Xbox/libsmb2.vcproj b/Xbox/libsmb2.vcproj new file mode 100644 index 00000000..53d1e964 --- /dev/null +++ b/Xbox/libsmb2.vcproj @@ -0,0 +1,489 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/include/asprintf.h b/include/asprintf.h index 977b42cc..b6562669 100644 --- a/include/asprintf.h +++ b/include/asprintf.h @@ -11,7 +11,7 @@ #define inline __inline #endif -#ifndef XBOX_360_PLATFORM +#ifndef _XBOX #ifndef _vscprintf /* For some reason, MSVC fails to honour this #ifndef. */ /* Hence function renamed to _vscprintf_so(). */ @@ -28,7 +28,7 @@ static inline int _vscprintf_so(const char * format, va_list pargs) { #ifndef vasprintf static inline int vasprintf(char **strp, const char *fmt, va_list ap) { -#ifdef XBOX_360_PLATFORM +#ifdef _XBOX int len = _vscprintf(fmt, ap); #else int len = _vscprintf_so(fmt, ap); @@ -38,7 +38,11 @@ static inline int vasprintf(char **strp, const char *fmt, va_list ap) { if (len == -1) return -1; str = malloc((size_t)len + 1); if (!str) return -1; +#ifdef _XBOX + r = _vsnprintf(str, len + 1, fmt, ap); /* "secure" version of vsprintf */ +#else r = vsnprintf(str, len + 1, fmt, ap); /* "secure" version of vsprintf */ +#endif if (r == -1) return free(str), -1; *strp = str; return r; diff --git a/include/msvc/sys/socket.h b/include/msvc/sys/socket.h index 4b666da6..8a9d6ea5 100644 --- a/include/msvc/sys/socket.h +++ b/include/msvc/sys/socket.h @@ -6,21 +6,26 @@ extern "C" { #endif +#ifdef _XBOX #ifdef XBOX_360_PLATFORM #include +#else +#include "..\..\..\lib\compat.h" +#endif #include -#include -#include #define inline __inline #else + #ifndef WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN #endif // !WIN32_LEAN_AND_MEAN + #include #include #include #endif + #include #ifndef EBADF diff --git a/include/portable-endian.h b/include/portable-endian.h index d630e058..78bb44a9 100644 --- a/include/portable-endian.h +++ b/include/portable-endian.h @@ -7,7 +7,7 @@ #ifndef PORTABLE_ENDIAN_H__ #define PORTABLE_ENDIAN_H__ -#if (defined(_WIN16) || defined(_WIN32) || defined(_WIN64)) && !defined(__WINDOWS__) && !defined(XBOX_360_PLATFORM) +#if (defined(_WIN16) || defined(_WIN32) || defined(_WIN64)) && !defined(__WINDOWS__) && !defined(_XBOX) #define __WINDOWS__ #endif @@ -191,6 +191,9 @@ #endif #elif defined(XBOX_360_PLATFORM) +# include + +# include # define htobe16(x) (x) # define htole16(x) _byteswap_ushort(x) @@ -212,6 +215,31 @@ # define __LITTLE_ENDIAN LITTLE_ENDIAN # define __PDP_ENDIAN PDP_ENDIAN +# elif defined(XBOX_PLATFORM) +# include + +# include + +# define htobe16(x) (x) +# define htole16(x) _byteswap_ushort(x) +# define be16toh(x) (x) +# define le16toh(x) _byteswap_ushort(x) + +# define htobe32(x) (x) +# define htole32(x) _byteswap_ulong(x) +# define be32toh(x) (x) +# define le32toh(x) _byteswap_ulong(x) + +# define htobe64(x) (x) +# define htole64(x) _byteswap_uint64(x) +# define be64toh(x) (x) +# define le64toh(x) _byteswap_uint64(x) + +# define __BYTE_ORDER BYTE_ORDER +//# define __BIG_ENDIAN BIG_ENDIAN +# define __LITTLE_ENDIAN LITTLE_ENDIAN +# define __PDP_ENDIAN PDP_ENDIAN + # elif defined(__GNUC__) || defined(__clang__) # define htobe16(x) __builtin_bswap16(x) diff --git a/include/smb2/libsmb2.h b/include/smb2/libsmb2.h index 91a1c168..c55fba47 100644 --- a/include/smb2/libsmb2.h +++ b/include/smb2/libsmb2.h @@ -76,10 +76,10 @@ struct smb2dirent { struct smb2_stat_64 st; }; -#if defined(_WINDOWS) && defined(_MSC_VER) && !defined(XBOX_360_PLATFORM) +#if defined(_WINDOWS) && defined(_MSC_VER) #include typedef SOCKET t_socket; -#elif defined(XBOX_360_PLATFORM) && defined(_MSC_VER) +#elif defined(_XBOX) && defined(_MSC_VER) #include #include typedef SOCKET t_socket; diff --git a/include/xbox/xb_emu_socket.h b/include/xbox/xb_emu_socket.h index 6525b5e7..82c799db 100644 --- a/include/xbox/xb_emu_socket.h +++ b/include/xbox/xb_emu_socket.h @@ -35,8 +35,9 @@ typedef int socklen_t; #define POLLERR 0x0008 /* Error condition */ #define POLLHUP 0x0010 /* Hung up */ -#define ssize_t __int64 - +#ifndef XBOX_PLATFORM +#define ssize_t __int64 /* MSVC 2003 yells about this one. */ +#endif // OS support end #ifdef __cplusplus diff --git a/lib/compat.h b/lib/compat.h index 9d65e2d7..40c0981a 100644 --- a/lib/compat.h +++ b/lib/compat.h @@ -24,27 +24,27 @@ /* XBOX Defs begin */ #include #include +#include -#ifdef ORIGINAL_XBOX_PLATFORM /* MSVC 2003 Doesn´t have stdint.h header */ -typedef unsigned char uint8_t; - -#define uint64_t unsigned __int64 -#define uint32_t unsigned __int32 -#define uint16_t unsigned __int16 -#define uint_t unsigned int - +#ifdef XBOX_PLATFORM /* MSVC 2003 Doesn´t have stdint.h header */ +typedef char int8_t; typedef short int16_t; typedef short int_least16_t; typedef int int32_t; typedef long long int64_t; typedef int intptr_t; + +typedef unsigned char uint8_t; +typedef unsigned short uint16_t; +typedef unsigned int uint32_t; +typedef unsigned long long uint64_t; +typedef unsigned int uint_t; +typedef unsigned int uintptr_t; #else #include /* XBOX 360 */ #endif -#ifdef _XBOX #include "xbox/xb_emu_socket.h" -#endif #define snprintf _snprintf #define _U_ diff --git a/lib/errors.c b/lib/errors.c index 497f40e0..d01c4cac 100644 --- a/lib/errors.c +++ b/lib/errors.c @@ -1064,9 +1064,11 @@ int nterror_to_errno(uint32_t status) { return 0; case SMB2_STATUS_PENDING: return EAGAIN; +#ifndef XBOX_PLATFORM case SMB2_STATUS_ABORTED: return ECONNRESET; - case SMB2_STATUS_NO_SUCH_FILE: +#endif + case SMB2_STATUS_NO_SUCH_FILE: case SMB2_STATUS_NO_SUCH_DEVICE: case SMB2_STATUS_BAD_NETWORK_NAME: case SMB2_STATUS_OBJECT_NAME_NOT_FOUND: @@ -1107,22 +1109,29 @@ int nterror_to_errno(uint32_t status) { case SMB2_STATUS_CANNOT_DELETE: case SMB2_STATUS_FILE_DELETED: return EPERM; - case SMB2_STATUS_NO_MORE_FILES: +#ifndef XBOX_PLATFORM + case SMB2_STATUS_NO_MORE_FILES: return ENODATA; + case SMB2_STATUS_LOGON_FAILURE: return ECONNREFUSED; - case SMB2_STATUS_NOT_A_DIRECTORY: +#endif + case SMB2_STATUS_NOT_A_DIRECTORY: return ENOTDIR; case SMB2_STATUS_NOT_IMPLEMENTED: case SMB2_STATUS_INVALID_DEVICE_REQUEST: + case SMB2_STATUS_ILLEGAL_FUNCTION: - case SMB2_STATUS_INVALID_PARAMETER: + + case SMB2_STATUS_INVALID_PARAMETER: case SMB2_STATUS_NOT_SUPPORTED: case SMB2_STATUS_NOT_A_REPARSE_POINT: return EINVAL; +#ifndef XBOX_PLATFORM case SMB2_STATUS_STOPPED_ON_SYMLINK: return ENOLINK; - case SMB2_STATUS_TOO_MANY_OPENED_FILES: +#endif + case SMB2_STATUS_TOO_MANY_OPENED_FILES: return EMFILE; case SMB2_STATUS_SECTION_TOO_BIG: case SMB2_STATUS_TOO_MANY_PAGING_FILES: @@ -1130,9 +1139,11 @@ int nterror_to_errno(uint32_t status) { return ENOMEM; case SMB2_STATUS_NOT_SAME_DEVICE: return EXDEV; +#ifndef XBOX_PLATFORM case SMB2_STATUS_SHARING_VIOLATION: return ETXTBSY; - case SMB2_STATUS_FILE_LOCK_CONFLICT: +#endif + case SMB2_STATUS_FILE_LOCK_CONFLICT: case SMB2_STATUS_LOCK_NOT_GRANTED: return EDEADLK; case SMB2_STATUS_OBJECT_NAME_COLLISION: @@ -1158,18 +1169,22 @@ int nterror_to_errno(uint32_t status) { case SMB2_STATUS_CONNECTION_INVALID: case SMB2_STATUS_CONNECTION_ABORTED: case SMB2_STATUS_NETWORK_NAME_DELETED: - case SMB2_STATUS_INVALID_NETWORK_RESPONSE: +#ifndef XBOX_PLATFORM + case SMB2_STATUS_INVALID_NETWORK_RESPONSE: // We return this errno with the intention that caller can // retry when any of these are received. return ENETRESET; - case SMB2_STATUS_PATH_NOT_COVERED: +#endif + case SMB2_STATUS_PATH_NOT_COVERED: // We do not have an errno which can be an equivalent of this // NT_STATUS code. To handle this, return a code which will not // be used as we are operating over a network. return ENOEXEC; +#ifndef XBOX_PLATFORM case SMB2_STATUS_IO_TIMEOUT: return ETIMEDOUT; - case SMB2_STATUS_INSUFFICIENT_RESOURCES: +#endif + case SMB2_STATUS_INSUFFICIENT_RESOURCES: return EBUSY; case SMB2_STATUS_INTERNAL_ERROR: // Fall through. diff --git a/lib/init.c b/lib/init.c index b826dc2f..e9d41130 100644 --- a/lib/init.c +++ b/lib/init.c @@ -63,7 +63,7 @@ #include "compat.h" #ifdef _MSC_VER -#ifdef XBOX_360_PLATFORM +#ifdef _XBOX #include #endif #include @@ -278,7 +278,7 @@ struct smb2_context *smb2_init_context(void) char buf[1024]; int i, ret; static int ctr; -#ifdef XBOX_360_PLATFORM +#ifdef _XBOX srandom(time(NULL) ^ 1 ^ ctr++); #else srandom(time(NULL) ^ getpid() ^ ctr++); @@ -414,9 +414,12 @@ static void smb2_set_error_string(struct smb2_context *smb2, const char * error_ { #ifndef PS2_IOP_PLATFORM char errstr[MAX_ERROR_SIZE] = {0}; - - if (vsnprintf(errstr, MAX_ERROR_SIZE, error_string, args) < 0) { - strncpy(errstr, "could not format error string!", +#ifdef _XBOX + if (_vsnprintf(errstr, MAX_ERROR_SIZE, error_string, args) < 0) { +#else + if (vsnprintf(errstr, MAX_ERROR_SIZE, error_string, args) < 0) { +#endif + strncpy(errstr, "could not format error string!", MAX_ERROR_SIZE); } strncpy(smb2->error_string, errstr, MAX_ERROR_SIZE); @@ -476,7 +479,7 @@ void smb2_set_security_mode(struct smb2_context *smb2, uint16_t security_mode) smb2->security_mode = security_mode; } -#if !defined(XBOX_360_PLATFORM) && !defined(PS2_IOP_PLATFORM) +#if !defined(_XBOX) && !defined(PS2_IOP_PLATFORM) static void smb2_set_password_from_file(struct smb2_context *smb2) { char *name = NULL; diff --git a/lib/socket.c b/lib/socket.c index 298ed5f6..a3b31f46 100644 --- a/lib/socket.c +++ b/lib/socket.c @@ -247,8 +247,12 @@ smb2_write_to_socket(struct smb2_context *smb2) count = writev(smb2->fd, tmpiov, niov); if (count == -1) { - if (errno == EAGAIN || errno == EWOULDBLOCK) { - return 0; +#ifdef XBOX_PLATFORM + if (errno == EAGAIN) { +#else + if (errno == EAGAIN || errno == EWOULDBLOCK) { +#endif + return 0; } smb2_set_error(smb2, "Error when writing to " "socket :%d %s", errno, @@ -320,7 +324,7 @@ static int smb2_read_data(struct smb2_context *smb2, read_func func, /* Read into our trimmed iovectors */ count = func(smb2, tmpiov, niov); if (count < 0) { -#if defined(_WIN32) || defined(XBOX_360_PLATFORM) +#if defined(_WIN32) || defined(_XBOX) int err = WSAGetLastError(); if (err == WSAEINTR || err == WSAEWOULDBLOCK) { #else @@ -746,7 +750,7 @@ smb2_service_fd(struct smb2_context *smb2, int fd, int revents) if (smb2->fd == -1 && revents & POLLOUT) { int err = 0; socklen_t err_size = sizeof(err); -#ifdef XBOX_360_PLATFORM +#ifdef _XBOX if (sckemu_getsockopt(fd, SOL_SOCKET, SO_ERROR, (char *)&err, &err_size) != 0 || err != 0) { #else @@ -825,7 +829,7 @@ smb2_service(struct smb2_context *smb2, int revents) static void set_nonblocking(t_socket fd) { -#if defined(WIN32) || defined(XBOX_360_PLATFORM) +#if defined(WIN32) || defined(_XBOX) unsigned long opt = 1; ioctlsocket(fd, FIONBIO, &opt); #else @@ -861,10 +865,10 @@ connect_async_ai(struct smb2_context *smb2, const struct addrinfo *ai, int *fd_o t_socket fd; socklen_t socksize; struct sockaddr_storage ss; -#ifdef XBOX_360_PLATFORM +#ifdef _XBOX BOOL bBroadcast = FALSE; #endif -#ifndef XBOX_360_PLATFORM +#if !defined(_XBOX) #if 0 == CONFIGURE_OPTION_TCP_LINGER int yes; struct LingerStruct { int l_onoff; /* linger active */ int l_linger; /* how many seconds to linger for */ }; @@ -920,7 +924,7 @@ connect_async_ai(struct smb2_context *smb2, const struct addrinfo *ai, int *fd_o set_nonblocking(fd); set_tcp_sockopt(fd, TCP_NODELAY, 1); -#ifndef XBOX_360_PLATFORM +#ifndef _XBOX #if 0 == CONFIGURE_OPTION_TCP_LINGER yes = 1; setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof yes); @@ -1051,17 +1055,17 @@ smb2_connect_async(struct smb2_context *smb2, const char *server, } /* is it a hostname ? */ -#ifdef XBOX_360_PLATFORM +#ifdef _XBOX err = sckemu_getaddrinfo(host, port, NULL, &smb2->addrinfos); #else err = getaddrinfo(host, port, NULL, &smb2->addrinfos); #endif if (err != 0) { free(addr); -#if defined(_WINDOWS) || defined(XBOX_360_PLATFORM) +#if defined(_WINDOWS) || defined(_XBOX) if (err == WSANOTINITIALISED) { -#ifdef XBOX_360_PLATFORM +#ifdef _XBOX smb2_set_error(smb2, "Winsockx was not initialized. " "Please call WSAStartup()."); #else From feb9036897c7433a60829054dc252c7841c14a58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Guilherme?= <75867486+Wolf3s@users.noreply.github.com> Date: Fri, 17 Nov 2023 20:03:24 -0300 Subject: [PATCH 05/23] Update README --- README | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/README b/README index 9d648049..d709627e 100644 --- a/README +++ b/README @@ -265,7 +265,20 @@ and set it up. Then to build libsmb2, run $ cd lib $ make -f Makefile.DC_KOS clean install - + +Xbox (Xbox XDK) +---------------------- +Xbox CPU is acustom Intel Pentium III Coppermine-based processor which only supports litlle endian values. +To compile libsmb2 for the Xbox, first install the Xbox XDK(with all features), Microsoft Visual C++ 2003 Professional and Windows XP(Recommended). + +Then to build libsmb2, go to Xbox folder +and open the provided .sln file, Then hit the green button to build: + +The process will result a libsmb2.lib. So you copy the include files +and the .lib file to your Xbox project. + +NOTE: This port was based on XBMC-360 port by BDC(Brent De Cartet) and now being updated to libsmb2 standards to best performance. + Xbox 360 (Xbox 360 SDK) ---------------------- Xbox 360 CPU is a PPC(PowerPC) Xenon which only supports big endian values. From c973f973fc9598b4e0fcf51d40d324a3a7cadab6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Guilherme?= <75867486+Wolf3s@users.noreply.github.com> Date: Sat, 18 Nov 2023 08:28:10 -0300 Subject: [PATCH 06/23] Update README --- README | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README b/README index d709627e..e2ed15ae 100644 --- a/README +++ b/README @@ -269,7 +269,7 @@ Then to build libsmb2, run Xbox (Xbox XDK) ---------------------- Xbox CPU is acustom Intel Pentium III Coppermine-based processor which only supports litlle endian values. -To compile libsmb2 for the Xbox, first install the Xbox XDK(with all features), Microsoft Visual C++ 2003 Professional and Windows XP(Recommended). +To compile libsmb2 for the Xbox, first install the Xbox XDK(with all features), Microsoft Visual C++ 2003 Professional and Windows XP. Then to build libsmb2, go to Xbox folder and open the provided .sln file, Then hit the green button to build: From 2c58181c5d83d99cb42c540bdd0d5055b8865648 Mon Sep 17 00:00:00 2001 From: Wolf3s Date: Sat, 18 Nov 2023 09:09:14 -0300 Subject: [PATCH 07/23] Code cleanup --- include/msvc/sys/socket.h | 4 ++-- include/portable-endian.h | 1 - lib/compat.h | 14 -------------- lib/libsmb2.c | 4 ++-- 4 files changed, 4 insertions(+), 19 deletions(-) diff --git a/include/msvc/sys/socket.h b/include/msvc/sys/socket.h index 8a9d6ea5..817fefc1 100644 --- a/include/msvc/sys/socket.h +++ b/include/msvc/sys/socket.h @@ -1,4 +1,3 @@ - #ifndef _MSVC_SYS_SOCKET_H_ #define _MSVC_SYS_SOCKET_H_ @@ -13,14 +12,15 @@ extern "C" { #include "..\..\..\lib\compat.h" #endif #include +#ifndef XBOX_PLATFORM #define inline __inline +#endif #else #ifndef WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN #endif // !WIN32_LEAN_AND_MEAN - #include #include #include diff --git a/include/portable-endian.h b/include/portable-endian.h index 78bb44a9..42f121c9 100644 --- a/include/portable-endian.h +++ b/include/portable-endian.h @@ -236,7 +236,6 @@ # define le64toh(x) _byteswap_uint64(x) # define __BYTE_ORDER BYTE_ORDER -//# define __BIG_ENDIAN BIG_ENDIAN # define __LITTLE_ENDIAN LITTLE_ENDIAN # define __PDP_ENDIAN PDP_ENDIAN diff --git a/lib/compat.h b/lib/compat.h index 40c0981a..55c54458 100644 --- a/lib/compat.h +++ b/lib/compat.h @@ -24,7 +24,6 @@ /* XBOX Defs begin */ #include #include -#include #ifdef XBOX_PLATFORM /* MSVC 2003 Doesn´t have stdint.h header */ typedef char int8_t; @@ -49,26 +48,13 @@ typedef unsigned int uintptr_t; #define snprintf _snprintf #define _U_ -//#define offsetof(st, m) ((size_t)((char *)&((st *)(1024))->m - (char *)1024)) - /* XBOX Defs end */ - -#define POLLIN 0x0001 /* There is data to read */ -#define POLLPRI 0x0002 /* There is urgent data to read */ -#define POLLOUT 0x0004 /* Writing now will not block */ -#define POLLERR 0x0008 /* Error condition */ -#define POLLHUP 0x0010 /* Hung up */ - struct pollfd { int fd; short events; short revents; }; -typedef unsigned long nfds_t; - -#define sys_close(a) closesocket(a) - #define SOL_TCP 6 #define inline __inline diff --git a/lib/libsmb2.c b/lib/libsmb2.c index 12867da0..ee207fd3 100644 --- a/lib/libsmb2.c +++ b/lib/libsmb2.c @@ -60,7 +60,7 @@ #include #endif -#if !defined(PS2_IOP_PLATFORM) || defined(__GNUC__) || defined(HAVE_FCNTL_H) || defined(_MSC_VER) +#if defined(__GNUC__) || defined(HAVE_FCNTL_H) || defined(_MSC_VER) #include #endif @@ -68,7 +68,7 @@ #include #endif -#ifdef _WIN32 +#if defined(_WIN32) || defined(_XBOX) #include "asprintf.h" #endif From a791664fae14345d3cb65a567cd2a2376d179c32 Mon Sep 17 00:00:00 2001 From: Wolf3s Date: Sat, 18 Nov 2023 09:50:35 -0300 Subject: [PATCH 08/23] Fix Debug Build --- Xbox 360/libsmb2.vcxproj | 4 +++- lib/socket.c | 11 +++++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/Xbox 360/libsmb2.vcxproj b/Xbox 360/libsmb2.vcxproj index 2322a221..5cd2ba79 100644 --- a/Xbox 360/libsmb2.vcxproj +++ b/Xbox 360/libsmb2.vcxproj @@ -111,7 +111,8 @@ _DEBUG;_XBOX;_LIB;HAVE_STDINT_H;HAVE_STDLIB_H;HAVE_STRING_H;HAVE_SYS_TYPES_H;HAVE_SYS_STAT_H;HAVE_TIME_H;STDC_HEADERS;NEED_POLL;XBOX_360_PLATFORM;HAVE_SYS_SOCKET_H Callcap CompileAsC - ..\include\;..\include\smb2 + ..\include\;..\include\smb2;..\include\msvc;..\include\msvc\sys + AnySuitable true @@ -268,6 +269,7 @@ CompileAsCpp + CompileAsCpp diff --git a/lib/socket.c b/lib/socket.c index a3b31f46..7d874db4 100644 --- a/lib/socket.c +++ b/lib/socket.c @@ -843,11 +843,14 @@ static int set_tcp_sockopt(t_socket sockfd, int optname, int value) { int level; -#if !defined(SOL_TCP) && !defined(XBOX_360_PLATFORM) +#if !defined(SOL_TCP) struct protoent *buf; - +#ifdef _XBOX + if ((buf = sckemu_getprotobyname("tcp")) != NULL) { +#else if ((buf = getprotobyname("tcp")) != NULL) { - level = buf->p_proto; +#endif + level = buf->p_proto; } else { return -1; } @@ -868,7 +871,7 @@ connect_async_ai(struct smb2_context *smb2, const struct addrinfo *ai, int *fd_o #ifdef _XBOX BOOL bBroadcast = FALSE; #endif -#if !defined(_XBOX) +#ifndef _XBOX #if 0 == CONFIGURE_OPTION_TCP_LINGER int yes; struct LingerStruct { int l_onoff; /* linger active */ int l_linger; /* how many seconds to linger for */ }; From 12376a464422e79d46c41573ddfb0845e694a93f Mon Sep 17 00:00:00 2001 From: Wolf3s Date: Mon, 20 Nov 2023 10:27:15 -0300 Subject: [PATCH 09/23] Update socket.c --- lib/socket.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/socket.c b/lib/socket.c index 7d874db4..8e132c83 100644 --- a/lib/socket.c +++ b/lib/socket.c @@ -873,7 +873,7 @@ connect_async_ai(struct smb2_context *smb2, const struct addrinfo *ai, int *fd_o #endif #ifndef _XBOX #if 0 == CONFIGURE_OPTION_TCP_LINGER - int yes; + int const yes = 1; struct LingerStruct { int l_onoff; /* linger active */ int l_linger; /* how many seconds to linger for */ }; struct LingerStruct const lin = { .l_onoff = 1, .l_linger = 0 }; /* if l_linger is zero, sends RST after FIN */ #endif @@ -929,7 +929,6 @@ connect_async_ai(struct smb2_context *smb2, const struct addrinfo *ai, int *fd_o set_tcp_sockopt(fd, TCP_NODELAY, 1); #ifndef _XBOX #if 0 == CONFIGURE_OPTION_TCP_LINGER - yes = 1; setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof yes); setsockopt(fd, SOL_SOCKET, SO_LINGER, &lin, sizeof lin); #endif From 695431e39347ab0dd3c672051b5ff5a06f5c4a0e Mon Sep 17 00:00:00 2001 From: Wolfywolfy Date: Mon, 20 Nov 2023 21:14:35 -0300 Subject: [PATCH 10/23] Add config.h support on xbox --- Xbox 360/libsmb2.vcxproj | 30 +++---- Xbox/libsmb2.vcproj | 28 +++--- include/xbox 360/config.h | 118 +++++++++++++++++++++++++ include/xbox/config.h | 119 ++++++++++++++++++++++++++ {include => lib}/xbox/addrinfo.h | 0 {include => lib}/xbox/addrsize.h | 0 {include => lib}/xbox/bittypes.h | 0 {include => lib}/xbox/emu_socket.cpp | 0 {include => lib}/xbox/emu_socket.h | 0 {include => lib}/xbox/getaddrinfo.c | 0 {include => lib}/xbox/getnameinfo.c | 0 {include => lib}/xbox/inet_aton.c | 0 {include => lib}/xbox/inet_ntop.c | 0 {include => lib}/xbox/inet_pton.c | 0 {include => lib}/xbox/sockstorage.h | 0 {include => lib}/xbox/xb_emu_socket.h | 0 16 files changed, 266 insertions(+), 29 deletions(-) create mode 100644 include/xbox 360/config.h create mode 100644 include/xbox/config.h rename {include => lib}/xbox/addrinfo.h (100%) rename {include => lib}/xbox/addrsize.h (100%) rename {include => lib}/xbox/bittypes.h (100%) rename {include => lib}/xbox/emu_socket.cpp (100%) rename {include => lib}/xbox/emu_socket.h (100%) rename {include => lib}/xbox/getaddrinfo.c (100%) rename {include => lib}/xbox/getnameinfo.c (100%) rename {include => lib}/xbox/inet_aton.c (100%) rename {include => lib}/xbox/inet_ntop.c (100%) rename {include => lib}/xbox/inet_pton.c (100%) rename {include => lib}/xbox/sockstorage.h (100%) rename {include => lib}/xbox/xb_emu_socket.h (100%) diff --git a/Xbox 360/libsmb2.vcxproj b/Xbox 360/libsmb2.vcxproj index 5cd2ba79..b6cd8a21 100644 --- a/Xbox 360/libsmb2.vcxproj +++ b/Xbox 360/libsmb2.vcxproj @@ -108,10 +108,10 @@ false $(OutDir)$(ProjectName).pch MultiThreadedDebug - _DEBUG;_XBOX;_LIB;HAVE_STDINT_H;HAVE_STDLIB_H;HAVE_STRING_H;HAVE_SYS_TYPES_H;HAVE_SYS_STAT_H;HAVE_TIME_H;STDC_HEADERS;NEED_POLL;XBOX_360_PLATFORM;HAVE_SYS_SOCKET_H + _DEBUG;_XBOX;_LIB;NEED_POLL;XBOX_360_PLATFORM Callcap CompileAsC - ..\include\;..\include\smb2;..\include\msvc;..\include\msvc\sys + ..\include\;..\include\smb2;..\include\msvc;..\include\msvc\sys;..\include\smb2\xbox 360 AnySuitable @@ -195,8 +195,8 @@ false $(OutDir)$(ProjectName).pch MultiThreaded - NDEBUG;_XBOX;_LIB;__BYTE_ORDER == BIG_ENDIAN;HAVE_STDINT_H;HAVE_STDLIB_H;HAVE_STRING_H;HAVE_SYS_TYPES_H;HAVE_SYS_STAT_H;HAVE_TIME_H;STDC_HEADERS;NEED_POLL;XBOX_360_PLATFORM;HAVE_SYS_SOCKET_H - ..\include\;..\include\smb2;..\include\msvc;..\include\msvc\sys + NDEBUG;_XBOX;_LIB;__BYTE_ORDER == BIG_ENDIAN;NEED_POLL;XBOX_360_PLATFORM; + ..\include\;..\include\smb2;..\include\msvc;..\include\msvc\sys;..\include\smb2\xbox 360 CompileAsC AnySuitable @@ -247,12 +247,12 @@ - - - - - - + + + + + + @@ -271,11 +271,11 @@ CompileAsCpp CompileAsCpp - - - - - + + + + + diff --git a/Xbox/libsmb2.vcproj b/Xbox/libsmb2.vcproj index 53d1e964..bf39477c 100644 --- a/Xbox/libsmb2.vcproj +++ b/Xbox/libsmb2.vcproj @@ -22,7 +22,7 @@ InlineFunctionExpansion="2" OptimizeForProcessor="2" AdditionalIncludeDirectories="..\include\;..\include\msvc;..\include\xbox;..\include\smb2" - PreprocessorDefinitions="_DEBUG;_XBOX;_LIB;XBOX_PLATFORM;HAVE_STDLIB_H;HAVE_STRING_H;HAVE_SYS_TYPES_H;HAVE_SYS_STAT_H;HAVE_TIME_H;STDC_HEADERS;NEED_POLL;HAVE_SYS_SOCKET_H" + PreprocessorDefinitions="_DEBUG;_XBOX;_LIB;XBOX_PLATFORM;NEED_POLL" MinimalRebuild="TRUE" BasicRuntimeChecks="3" RuntimeLibrary="1" @@ -126,7 +126,7 @@ OmitFramePointers="TRUE" OptimizeForProcessor="2" AdditionalIncludeDirectories="..\include\;..\include\msvc;..\include\xbox;..\include\smb2" - PreprocessorDefinitions="NDEBUG;_XBOX;_LIB;XBOX_PLATFORM;HAVE_STDLIB_H;HAVE_STRING_H;HAVE_SYS_TYPES_H;HAVE_SYS_STAT_H;HAVE_TIME_H;STDC_HEADERS;NEED_POLL;HAVE_SYS_SOCKET_H" + PreprocessorDefinitions="NDEBUG;_XBOX;_LIB;XBOX_PLATFORM;NEED_POLL;" StringPooling="TRUE" RuntimeLibrary="0" BufferSecurityCheck="TRUE" @@ -222,16 +222,16 @@ Name="xbox" Filter=""> + RelativePath="..\lib\xbox\addrinfo.h"> + RelativePath="..\lib\xbox\addrsize.h"> + RelativePath="..\lib\xbox\bittypes.h"> + RelativePath="..\lib\xbox\emu_socket.cpp"> + RelativePath="..\lib\xbox\emu_socket.h"> + RelativePath="..\lib\xbox\getaddrinfo.c"> + RelativePath="..\lib\xbox\getnameinfo.c"> + RelativePath="..\lib\xbox\inet_aton.c"> + RelativePath="..\lib\xbox\inet_ntop.c"> + RelativePath="..\lib\xbox\inet_pton.c"> + RelativePath="..\lib\xbox\sockstorage.h"> + RelativePath="..\lib\xbox\xb_emu_socket.h"> header file. */ +/* #define HAVE_ARPA_INET_H 1 */ + +/* Define to 1 if you have the header file. */ +/* #define HAVE_DLFCN_H 1 */ + +/* Define to 1 if you have the header file. */ +/* #define HAVE_FCNTL_H 1 */ + +/* Define to 1 if you have the header file. */ +/* #define HAVE_GSSAPI_GSSAPI_H 1 */ + +/* Define to 1 if you have the header file. */ +/* #define HAVE_INTTYPES_H 1 */ + +/* Whether we use gssapi_krb5 or not */ +/* #define HAVE_LIBKRB5 1 */ + +/* Define to 1 if you have the `nsl' library (-lnsl). */ +/* #undef HAVE_LIBNSL */ + +/* Define to 1 if you have the `socket' library (-lsocket). */ +/* #undef HAVE_LIBSOCKET */ + +/* Define to 1 if you have the header file. */ +/* #define HAVE_NETDB_H 1 */ + +/* Define to 1 if you have the header file. */ +/* #define HAVE_NETINET_IN_H 1 */ + +/* Define to 1 if you have the header file. */ +/* #define HAVE_NETINET_TCP_H 1 */ + +/* Define to 1 if you have the header file. */ +/* #define HAVE_POLL_H 1 */ + +/* Whether sockaddr struct has sa_len */ +/* #undef HAVE_SOCKADDR_LEN */ + +/* Whether we have sockaddr_Storage */ +/* #define HAVE_SOCKADDR_STORAGE 1 */ + +/* Define to 1 if you have the header file. */ +#define HAVE_STDINT_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_STDIO_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_STDLIB_H 1 + +/* Define to 1 if you have the header file. */ +/* #define HAVE_STRINGS_H 1 */ + +/* Define to 1 if you have the header file. */ +#define HAVE_STRING_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_SYS_IOCTL_H 1 + +/* Define to 1 if you have the header file. */ +/* #define HAVE_SYS_POLL_H 1 */ + +/* Define to 1 if you have the header file. */ +#define HAVE_SYS_SOCKET_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_SYS_STAT_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_SYS_TYPES_H 1 + +/* Define to 1 if you have the header file. */ +/* #define HAVE_SYS_UIO_H 1 */ + +/* Define to 1 if you have the header file. */ +#define HAVE_TIME_H 1 + +/* Define to 1 if you have the header file. */ +/* #define HAVE_UNISTD_H 1 */ + +/* Define to the sub-directory where libtool stores uninstalled libraries. */ +#define LT_OBJDIR ".libs/" + +/* Name of package */ +#define PACKAGE "libsmb2" + +/* Define to the address where bug reports for this package should be sent. */ +#define PACKAGE_BUGREPORT "ronniesahlberg@gmail.com" + +/* Define to the full name of this package. */ +#define PACKAGE_NAME "libsmb2" + +/* Define to the full name and version of this package. */ +#define PACKAGE_STRING "libsmb2 4.0.0" + +/* Define to the one symbol short name of this package. */ +#define PACKAGE_TARNAME "libsmb2" + +/* Define to the home page for this package. */ +#define PACKAGE_URL "" + +/* Define to the version of this package. */ +#define PACKAGE_VERSION "4.0.0" + +/* Define to 1 if all of the C90 standard headers exist (not just the ones + required in a freestanding environment). This macro is provided for + backward compatibility; new code need not use it. */ +#define STDC_HEADERS 1 + +/* Version number of package */ +#define VERSION "4.0.0" diff --git a/include/xbox/config.h b/include/xbox/config.h new file mode 100644 index 00000000..5bc14b05 --- /dev/null +++ b/include/xbox/config.h @@ -0,0 +1,119 @@ +/* config.h. Generated from config.h.in by configure. */ +/* config.h.in. Generated from configure.ac by autoheader. */ + +/* Whether or not TCP sockets should be allowed to linger after closure */ +#define CONFIGURE_OPTION_TCP_LINGER 1 +#define _U_ /* __attribute__((unused)) */ + +/* Define to 1 if you have the header file. */ +/* #define HAVE_ARPA_INET_H 1 */ + +/* Define to 1 if you have the header file. */ +/* #define HAVE_DLFCN_H 1 */ + +/* Define to 1 if you have the header file. */ +/* #define HAVE_FCNTL_H 1 */ + +/* Define to 1 if you have the header file. */ +/* #define HAVE_GSSAPI_GSSAPI_H 1 */ + +/* Define to 1 if you have the header file. */ +/* #define HAVE_INTTYPES_H 1 */ + +/* Whether we use gssapi_krb5 or not */ +/* #define HAVE_LIBKRB5 1 */ + +/* Define to 1 if you have the `nsl' library (-lnsl). */ +/* #undef HAVE_LIBNSL */ + +/* Define to 1 if you have the `socket' library (-lsocket). */ +/* #undef HAVE_LIBSOCKET */ + +/* Define to 1 if you have the header file. */ +/* #define HAVE_NETDB_H 1 */ + +/* Define to 1 if you have the header file. */ +/* #define HAVE_NETINET_IN_H 1 */ + +/* Define to 1 if you have the header file. */ +/* #define HAVE_NETINET_TCP_H 1 */ + +/* Define to 1 if you have the header file. */ +/* #define HAVE_POLL_H 1 */ + +/* Whether sockaddr struct has sa_len */ +/* #undef HAVE_SOCKADDR_LEN */ + +/* Whether we have sockaddr_Storage */ +#define HAVE_SOCKADDR_STORAGE 1 + +/* Define to 1 if you have the header file. */ +/* #define HAVE_STDINT_H 1 */ + +/* Define to 1 if you have the header file. */ +#define HAVE_STDIO_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_STDLIB_H 1 + +/* Define to 1 if you have the header file. */ +/* #define HAVE_STRINGS_H 1 */ + +/* Define to 1 if you have the header file. */ +#define HAVE_STRING_H 1 + +/* Define to 1 if you have the header file. */ +/* #define HAVE_SYS_IOCTL_H 1 */ + +/* Define to 1 if you have the header file. */ +/* #define HAVE_SYS_POLL_H 1 */ + +/* Define to 1 if you have the header file. */ +#define HAVE_SYS_SOCKET_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_SYS_STAT_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_SYS_TYPES_H 1 + +/* Define to 1 if you have the header file. */ +/* #define HAVE_SYS_UIO_H 1 */ + +/* Define to 1 if you have the header file. */ +#define HAVE_TIME_H 1 + +/* Define to 1 if you have the header file. */ +/* #define HAVE_UNISTD_H 1 */ + +/* Define to the sub-directory where libtool stores uninstalled libraries. */ +#define LT_OBJDIR ".libs/" + +/* Name of package */ +#define PACKAGE "libsmb2" + +/* Define to the address where bug reports for this package should be sent. */ +#define PACKAGE_BUGREPORT "ronniesahlberg@gmail.com" + +/* Define to the full name of this package. */ +#define PACKAGE_NAME "libsmb2" + +/* Define to the full name and version of this package. */ +#define PACKAGE_STRING "libsmb2 4.0.0" + +/* Define to the one symbol short name of this package. */ +#define PACKAGE_TARNAME "libsmb2" + +/* Define to the home page for this package. */ +#define PACKAGE_URL "" + +/* Define to the version of this package. */ +#define PACKAGE_VERSION "4.0.0" + +/* Define to 1 if all of the C90 standard headers exist (not just the ones + required in a freestanding environment). This macro is provided for + backward compatibility; new code need not use it. */ +#define STDC_HEADERS 1 + +/* Version number of package */ +#define VERSION "4.0.0" diff --git a/include/xbox/addrinfo.h b/lib/xbox/addrinfo.h similarity index 100% rename from include/xbox/addrinfo.h rename to lib/xbox/addrinfo.h diff --git a/include/xbox/addrsize.h b/lib/xbox/addrsize.h similarity index 100% rename from include/xbox/addrsize.h rename to lib/xbox/addrsize.h diff --git a/include/xbox/bittypes.h b/lib/xbox/bittypes.h similarity index 100% rename from include/xbox/bittypes.h rename to lib/xbox/bittypes.h diff --git a/include/xbox/emu_socket.cpp b/lib/xbox/emu_socket.cpp similarity index 100% rename from include/xbox/emu_socket.cpp rename to lib/xbox/emu_socket.cpp diff --git a/include/xbox/emu_socket.h b/lib/xbox/emu_socket.h similarity index 100% rename from include/xbox/emu_socket.h rename to lib/xbox/emu_socket.h diff --git a/include/xbox/getaddrinfo.c b/lib/xbox/getaddrinfo.c similarity index 100% rename from include/xbox/getaddrinfo.c rename to lib/xbox/getaddrinfo.c diff --git a/include/xbox/getnameinfo.c b/lib/xbox/getnameinfo.c similarity index 100% rename from include/xbox/getnameinfo.c rename to lib/xbox/getnameinfo.c diff --git a/include/xbox/inet_aton.c b/lib/xbox/inet_aton.c similarity index 100% rename from include/xbox/inet_aton.c rename to lib/xbox/inet_aton.c diff --git a/include/xbox/inet_ntop.c b/lib/xbox/inet_ntop.c similarity index 100% rename from include/xbox/inet_ntop.c rename to lib/xbox/inet_ntop.c diff --git a/include/xbox/inet_pton.c b/lib/xbox/inet_pton.c similarity index 100% rename from include/xbox/inet_pton.c rename to lib/xbox/inet_pton.c diff --git a/include/xbox/sockstorage.h b/lib/xbox/sockstorage.h similarity index 100% rename from include/xbox/sockstorage.h rename to lib/xbox/sockstorage.h diff --git a/include/xbox/xb_emu_socket.h b/lib/xbox/xb_emu_socket.h similarity index 100% rename from include/xbox/xb_emu_socket.h rename to lib/xbox/xb_emu_socket.h From 90bba4bcc8e34bddf8b1d28423ea6ead34ccdb8f Mon Sep 17 00:00:00 2001 From: Wolf3s Date: Sat, 25 Nov 2023 09:44:25 -0300 Subject: [PATCH 11/23] Fix xbox build --- Xbox 360/libsmb2.vcxproj | 11 +++--- Xbox 360/libsmb2.vcxproj.filters | 58 +++++++++++++++++--------------- Xbox/libsmb2.vcproj | 4 +-- include/xbox 360/config.h | 4 +-- lib/socket.c | 3 ++ 5 files changed, 44 insertions(+), 36 deletions(-) diff --git a/Xbox 360/libsmb2.vcxproj b/Xbox 360/libsmb2.vcxproj index b6cd8a21..4b3c9094 100644 --- a/Xbox 360/libsmb2.vcxproj +++ b/Xbox 360/libsmb2.vcxproj @@ -108,10 +108,10 @@ false $(OutDir)$(ProjectName).pch MultiThreadedDebug - _DEBUG;_XBOX;_LIB;NEED_POLL;XBOX_360_PLATFORM + _DEBUG;_XBOX;_LIB;NEED_POLL;XBOX_360_PLATFORM;HAVE_CONFIG_H Callcap CompileAsC - ..\include\;..\include\smb2;..\include\msvc;..\include\msvc\sys;..\include\smb2\xbox 360 + ..\include\;..\include\smb2;..\include\msvc;..\include\msvc\sys;..\include\xbox 360 AnySuitable @@ -195,8 +195,8 @@ false $(OutDir)$(ProjectName).pch MultiThreaded - NDEBUG;_XBOX;_LIB;__BYTE_ORDER == BIG_ENDIAN;NEED_POLL;XBOX_360_PLATFORM; - ..\include\;..\include\smb2;..\include\msvc;..\include\msvc\sys;..\include\smb2\xbox 360 + NDEBUG;_XBOX;_LIB;__BYTE_ORDER == BIG_ENDIAN;NEED_POLL;XBOX_360_PLATFORM;HAVE_CONFIG_H + ..\include\;..\include\smb2;..\include\msvc;..\include\msvc\sys;..\include\xbox 360 CompileAsC AnySuitable @@ -247,6 +247,7 @@ + @@ -267,7 +268,7 @@ - + CompileAsCpp CompileAsCpp diff --git a/Xbox 360/libsmb2.vcxproj.filters b/Xbox 360/libsmb2.vcxproj.filters index b00f7f8f..404746f6 100644 --- a/Xbox 360/libsmb2.vcxproj.filters +++ b/Xbox 360/libsmb2.vcxproj.filters @@ -26,8 +26,11 @@ {afbefc44-9daf-45e5-aacd-fe33021175a2} - - {4c1d54ae-7522-4157-9082-b22eb34be541} + + {2ee310d9-8aad-4af1-9726-53f9518d0978} + + + {6c01c6cd-316c-4bff-82ea-f611e60057e9} @@ -106,23 +109,26 @@ include\msvc\sys - - include\xbox + + lib\xbox + + + lib\xbox - - include\xbox + + lib\xbox - - include\xbox + + lib\xbox - - include\xbox + + lib\xbox - - include\xbox + + lib\xbox - - include\xbox + + include\xbox 360 @@ -270,23 +276,21 @@ lib - - include\xbox - - - include\xbox + + lib\xbox - - include\xbox + + lib\xbox - - include\xbox + + lib\xbox - - include\xbox + + lib\xbox - - include\xbox + + lib\xbox + \ No newline at end of file diff --git a/Xbox/libsmb2.vcproj b/Xbox/libsmb2.vcproj index bf39477c..fea3c2bb 100644 --- a/Xbox/libsmb2.vcproj +++ b/Xbox/libsmb2.vcproj @@ -22,7 +22,7 @@ InlineFunctionExpansion="2" OptimizeForProcessor="2" AdditionalIncludeDirectories="..\include\;..\include\msvc;..\include\xbox;..\include\smb2" - PreprocessorDefinitions="_DEBUG;_XBOX;_LIB;XBOX_PLATFORM;NEED_POLL" + PreprocessorDefinitions="_DEBUG;_XBOX;_LIB;XBOX_PLATFORM;NEED_POLL;HAVE_CONFIG_H" MinimalRebuild="TRUE" BasicRuntimeChecks="3" RuntimeLibrary="1" @@ -126,7 +126,7 @@ OmitFramePointers="TRUE" OptimizeForProcessor="2" AdditionalIncludeDirectories="..\include\;..\include\msvc;..\include\xbox;..\include\smb2" - PreprocessorDefinitions="NDEBUG;_XBOX;_LIB;XBOX_PLATFORM;NEED_POLL;" + PreprocessorDefinitions="NDEBUG;_XBOX;_LIB;XBOX_PLATFORM;NEED_POLL;HAVE_CONFIG_H" StringPooling="TRUE" RuntimeLibrary="0" BufferSecurityCheck="TRUE" diff --git a/include/xbox 360/config.h b/include/xbox 360/config.h index 2914277f..299f77de 100644 --- a/include/xbox 360/config.h +++ b/include/xbox 360/config.h @@ -11,7 +11,7 @@ /* #define HAVE_DLFCN_H 1 */ /* Define to 1 if you have the header file. */ -/* #define HAVE_FCNTL_H 1 */ +#define HAVE_FCNTL_H 1 /* Define to 1 if you have the header file. */ /* #define HAVE_GSSAPI_GSSAPI_H 1 */ @@ -62,7 +62,7 @@ #define HAVE_STRING_H 1 /* Define to 1 if you have the header file. */ -#define HAVE_SYS_IOCTL_H 1 +/* #define HAVE_SYS_IOCTL_H 1 */ /* Define to 1 if you have the header file. */ /* #define HAVE_SYS_POLL_H 1 */ diff --git a/lib/socket.c b/lib/socket.c index ed2e941b..5083395f 100644 --- a/lib/socket.c +++ b/lib/socket.c @@ -884,6 +884,9 @@ connect_async_ai(struct smb2_context *smb2, const struct addrinfo *ai, int *fd_o #if 0 == CONFIGURE_OPTION_TCP_LINGER int const yes = 1; struct LingerStruct const lin = { 1, 0 }; /* if l_linger is zero, sends RST after FIN */ +#endif +#ifdef _XBOX + BOOL bBroadcast = TRUE; #endif memset(&ss, 0, sizeof(ss)); switch (ai->ai_family) { From 321fc945a66296fdf65598529765c4176e53c1e7 Mon Sep 17 00:00:00 2001 From: Wolf3s Date: Sun, 26 Nov 2023 10:55:46 -0300 Subject: [PATCH 12/23] Enable HAVE_FCNTL_H --- Xbox/libsmb2.vcproj | 87 +++++++++++++++++++++++------------------- include/smb2/libsmb2.h | 4 +- include/xbox/config.h | 2 +- 3 files changed, 50 insertions(+), 43 deletions(-) diff --git a/Xbox/libsmb2.vcproj b/Xbox/libsmb2.vcproj index fea3c2bb..09af523c 100644 --- a/Xbox/libsmb2.vcproj +++ b/Xbox/libsmb2.vcproj @@ -222,46 +222,7 @@ Name="xbox" Filter=""> - - - - - - - - - - - - - - - - - - - - - - - - - + RelativePath="..\include\xbox\config.h"> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/include/smb2/libsmb2.h b/include/smb2/libsmb2.h index 18e61aa9..8e9be343 100644 --- a/include/smb2/libsmb2.h +++ b/include/smb2/libsmb2.h @@ -76,10 +76,10 @@ struct smb2dirent { struct smb2_stat_64 st; }; -#if defined(_WINDOWS) && defined(_MSC_VER) +#if defined(_WINDOWS) #include typedef SOCKET t_socket; -#elif defined(_XBOX) && defined(_MSC_VER) +#elif defined(_XBOX) #include #include typedef SOCKET t_socket; diff --git a/include/xbox/config.h b/include/xbox/config.h index 5bc14b05..2c523a47 100644 --- a/include/xbox/config.h +++ b/include/xbox/config.h @@ -12,7 +12,7 @@ /* #define HAVE_DLFCN_H 1 */ /* Define to 1 if you have the header file. */ -/* #define HAVE_FCNTL_H 1 */ +#define HAVE_FCNTL_H 1 /* Define to 1 if you have the header file. */ /* #define HAVE_GSSAPI_GSSAPI_H 1 */ From 713139e6b67f6d73cf7612f99dc4006e6b3aaf80 Mon Sep 17 00:00:00 2001 From: Wolf3s Date: Sun, 26 Nov 2023 11:10:44 -0300 Subject: [PATCH 13/23] Remove macro __BYTE_ORDER on vcxproj --- Xbox 360/libsmb2.vcxproj | 2 +- lib/md5.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Xbox 360/libsmb2.vcxproj b/Xbox 360/libsmb2.vcxproj index 4b3c9094..ebc169b6 100644 --- a/Xbox 360/libsmb2.vcxproj +++ b/Xbox 360/libsmb2.vcxproj @@ -195,7 +195,7 @@ false $(OutDir)$(ProjectName).pch MultiThreaded - NDEBUG;_XBOX;_LIB;__BYTE_ORDER == BIG_ENDIAN;NEED_POLL;XBOX_360_PLATFORM;HAVE_CONFIG_H + NDEBUG;_XBOX;_LIB;NEED_POLL;XBOX_360_PLATFORM;HAVE_CONFIG_H ..\include\;..\include\smb2;..\include\msvc;..\include\msvc\sys;..\include\xbox 360 CompileAsC AnySuitable diff --git a/lib/md5.h b/lib/md5.h index 2311994d..03dd7cbb 100644 --- a/lib/md5.h +++ b/lib/md5.h @@ -38,7 +38,7 @@ #include #endif -#if !defined(_WIN32) && (__BYTE_ORDER == __BIG_ENDIAN) +#if !defined(_WIN32) && (__BYTE_ORDER == __BIG_ENDIAN) || defined(XBOX_360_PLATFORM) # define WORDS_BIGENDIAN 1 #endif From 18802aed149cd284597c75485c8fc6bdfe8fb394 Mon Sep 17 00:00:00 2001 From: Wolf3s Date: Sun, 26 Nov 2023 12:02:04 -0300 Subject: [PATCH 14/23] Add missing macros for smb error handler --- lib/compat.h | 20 ++++++++++++++++++++ lib/errors.c | 9 --------- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/lib/compat.h b/lib/compat.h index d24473b6..d68c084c 100644 --- a/lib/compat.h +++ b/lib/compat.h @@ -45,6 +45,26 @@ typedef unsigned int uintptr_t; #include "xbox/xb_emu_socket.h" +#ifndef ENETRESET +#define ENETRESET WSAENETRESET +#endif + +#ifndef ECONNREFUSED +#define ECONNREFUSED WSAECONNREFUSED +#endif + +#ifndef ETIMEDOUT +#define ETIMEDOUT WSAETIMEDOUT +#endif + +#ifndef ECONNRESET +#define ECONNRESET WSAECONNRESET +#endif + +#ifndef ENODATA +#define ENODATA WSANO_DATA +#endif + #define snprintf _snprintf #define _U_ diff --git a/lib/errors.c b/lib/errors.c index d01c4cac..1ea90a77 100644 --- a/lib/errors.c +++ b/lib/errors.c @@ -1064,10 +1064,8 @@ int nterror_to_errno(uint32_t status) { return 0; case SMB2_STATUS_PENDING: return EAGAIN; -#ifndef XBOX_PLATFORM case SMB2_STATUS_ABORTED: return ECONNRESET; -#endif case SMB2_STATUS_NO_SUCH_FILE: case SMB2_STATUS_NO_SUCH_DEVICE: case SMB2_STATUS_BAD_NETWORK_NAME: @@ -1109,13 +1107,10 @@ int nterror_to_errno(uint32_t status) { case SMB2_STATUS_CANNOT_DELETE: case SMB2_STATUS_FILE_DELETED: return EPERM; -#ifndef XBOX_PLATFORM case SMB2_STATUS_NO_MORE_FILES: return ENODATA; - case SMB2_STATUS_LOGON_FAILURE: return ECONNREFUSED; -#endif case SMB2_STATUS_NOT_A_DIRECTORY: return ENOTDIR; case SMB2_STATUS_NOT_IMPLEMENTED: @@ -1169,21 +1164,17 @@ int nterror_to_errno(uint32_t status) { case SMB2_STATUS_CONNECTION_INVALID: case SMB2_STATUS_CONNECTION_ABORTED: case SMB2_STATUS_NETWORK_NAME_DELETED: -#ifndef XBOX_PLATFORM case SMB2_STATUS_INVALID_NETWORK_RESPONSE: // We return this errno with the intention that caller can // retry when any of these are received. return ENETRESET; -#endif case SMB2_STATUS_PATH_NOT_COVERED: // We do not have an errno which can be an equivalent of this // NT_STATUS code. To handle this, return a code which will not // be used as we are operating over a network. return ENOEXEC; -#ifndef XBOX_PLATFORM case SMB2_STATUS_IO_TIMEOUT: return ETIMEDOUT; -#endif case SMB2_STATUS_INSUFFICIENT_RESOURCES: return EBUSY; case SMB2_STATUS_INTERNAL_ERROR: From ceaf9c6692b8b21a58f0f98626a5f9925ccd2eb9 Mon Sep 17 00:00:00 2001 From: Wolf3s Date: Wed, 29 Nov 2023 21:58:20 -0300 Subject: [PATCH 15/23] Replace socket emulation on Xbox 360 --- lib/socket.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/socket.c b/lib/socket.c index 7b18e4ce..3d7047f8 100644 --- a/lib/socket.c +++ b/lib/socket.c @@ -763,7 +763,7 @@ smb2_service_fd(struct smb2_context *smb2, t_socket fd, int revents) if (smb2->fd == -1 && revents & POLLOUT) { int err = 0; socklen_t err_size = sizeof(err); -#ifdef _XBOX +#ifdef XBOX_PLATFORM if (sckemu_getsockopt(fd, SOL_SOCKET, SO_ERROR, (char *)&err, &err_size) != 0 || err != 0) { #else @@ -858,7 +858,7 @@ set_tcp_sockopt(t_socket sockfd, int optname, int value) int level; #if !defined(SOL_TCP) struct protoent *buf; -#ifdef _XBOX +#ifdef XBOX_PLATFORM if ((buf = sckemu_getprotobyname("tcp")) != NULL) { #else if ((buf = getprotobyname("tcp")) != NULL) { @@ -1071,7 +1071,7 @@ smb2_connect_async(struct smb2_context *smb2, const char *server, } /* is it a hostname ? */ -#ifdef _XBOX +#ifdef XBOX_PLATFORM err = sckemu_getaddrinfo(host, port, NULL, &smb2->addrinfos); #else err = getaddrinfo(host, port, NULL, &smb2->addrinfos); From f78f55cc17520a860769e2d20a76e9af6bedcb78 Mon Sep 17 00:00:00 2001 From: Wolf3s Date: Wed, 29 Nov 2023 22:46:35 -0300 Subject: [PATCH 16/23] Update README Remove socket emulation --- README | 10 ++++------ lib/socket.c | 10 +--------- 2 files changed, 5 insertions(+), 15 deletions(-) diff --git a/README b/README index e2ed15ae..034c672b 100644 --- a/README +++ b/README @@ -274,20 +274,18 @@ To compile libsmb2 for the Xbox, first install the Xbox XDK(with all features), Then to build libsmb2, go to Xbox folder and open the provided .sln file, Then hit the green button to build: -The process will result a libsmb2.lib. So you copy the include files +The process will result a libsmb2.lib. So you can copy the include files and the .lib file to your Xbox project. -NOTE: This port was based on XBMC-360 port by BDC(Brent De Cartet) and now being updated to libsmb2 standards to best performance. - Xbox 360 (Xbox 360 SDK) ---------------------- -Xbox 360 CPU is a PPC(PowerPC) Xenon which only supports big endian values. +Xbox 360 CPU is a PPC(PowerPC) Xenon which only supports only big endian values. To compile libsmb2 for the Xbox 360, first install the Xbox 360 SDK(with all features), Microsoft Visual C++ 2010 Ultimate and Windows XP(Recommended) or Windows 7. Then to build libsmb2, go to Xbox 360 folder and open the provided .sln file, Then hit the green button to build: -The process will result a libsmb2.lib. So you copy the include files +The process will result a libsmb2.lib. So you can copy the include files and the .lib file to your Xbox 360 project. -NOTE: This port was based on XBMC-360 port by BDC(Brent De Cartet) and now being updated to libsmb2 standards to best performance. +NOTE: Both ports was based on XBMC-360 port by BDC(Brent De Cartet) and now being updated to libsmb2 standards to best performance. diff --git a/lib/socket.c b/lib/socket.c index 3d7047f8..c4b14a4d 100644 --- a/lib/socket.c +++ b/lib/socket.c @@ -763,13 +763,9 @@ smb2_service_fd(struct smb2_context *smb2, t_socket fd, int revents) if (smb2->fd == -1 && revents & POLLOUT) { int err = 0; socklen_t err_size = sizeof(err); -#ifdef XBOX_PLATFORM - if (sckemu_getsockopt(fd, SOL_SOCKET, SO_ERROR, - (char *)&err, &err_size) != 0 || err != 0) { -#else + if (getsockopt(fd, SOL_SOCKET, SO_ERROR, (char *)&err, &err_size) != 0 || err != 0) { -#endif if (err == 0) { err = errno; } @@ -1071,11 +1067,7 @@ smb2_connect_async(struct smb2_context *smb2, const char *server, } /* is it a hostname ? */ -#ifdef XBOX_PLATFORM - err = sckemu_getaddrinfo(host, port, NULL, &smb2->addrinfos); -#else err = getaddrinfo(host, port, NULL, &smb2->addrinfos); -#endif if (err != 0) { free(addr); #if defined(_WINDOWS) || defined(_XBOX) From 2e65612fcef3aa5c6dbc1cdd9c8fd34299e632bf Mon Sep 17 00:00:00 2001 From: Wolf3s Date: Fri, 1 Dec 2023 19:09:21 -0300 Subject: [PATCH 17/23] Update portable-endian.h --- include/portable-endian.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/portable-endian.h b/include/portable-endian.h index 42f121c9..dc2e2e7f 100644 --- a/include/portable-endian.h +++ b/include/portable-endian.h @@ -235,9 +235,9 @@ # define be64toh(x) (x) # define le64toh(x) _byteswap_uint64(x) -# define __BYTE_ORDER BYTE_ORDER +/* # define __BYTE_ORDER BYTE_ORDER */ # define __LITTLE_ENDIAN LITTLE_ENDIAN -# define __PDP_ENDIAN PDP_ENDIAN +/* # define __PDP_ENDIAN PDP_ENDIAN */ # elif defined(__GNUC__) || defined(__clang__) From 4abb1381bfc9ca37758b9a7861c5e6ccbe4eb8d9 Mon Sep 17 00:00:00 2001 From: Wolf3s Date: Sun, 17 Dec 2023 11:14:33 -0300 Subject: [PATCH 18/23] Xbox fixes --- include/msvc/sys/socket.h | 5 +---- include/portable-endian.h | 21 ++++++++++++++++++++- include/smb2/libsmb2.h | 7 ++++++- lib/socket.c | 2 -- 4 files changed, 27 insertions(+), 8 deletions(-) diff --git a/include/msvc/sys/socket.h b/include/msvc/sys/socket.h index 817fefc1..b89a3f88 100644 --- a/include/msvc/sys/socket.h +++ b/include/msvc/sys/socket.h @@ -8,15 +8,12 @@ extern "C" { #ifdef _XBOX #ifdef XBOX_360_PLATFORM #include +#define inline __inline #else #include "..\..\..\lib\compat.h" #endif #include -#ifndef XBOX_PLATFORM -#define inline __inline -#endif #else - #ifndef WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN #endif // !WIN32_LEAN_AND_MEAN diff --git a/include/portable-endian.h b/include/portable-endian.h index dc2e2e7f..967b25d9 100644 --- a/include/portable-endian.h +++ b/include/portable-endian.h @@ -188,7 +188,26 @@ # define htole64(x) (x) # define be64toh(x) _byteswap_uint64(x) # define le64toh(x) (x) -#endif +# elif defined(__GNUC__) || defined(__clang__) + +# define htobe16(x) __builtin_bswap16(x) +# define htole16(x) (x) +# define be16toh(x) __builtin_bswap16(x) +# define le16toh(x) (x) + +# define htobe32(x) __builtin_bswap32(x) +# define htole32(x) (x) +# define be32toh(x) __builtin_bswap32(x) +# define le32toh(x) (x) + +# define htobe64(x) __builtin_bswap64(x) +# define htole64(x) (x) +# define be64toh(x) __builtin_bswap64(x) +# define le64toh(x) (x) + +# else +# error platform not supported +# endif #elif defined(XBOX_360_PLATFORM) # include diff --git a/include/smb2/libsmb2.h b/include/smb2/libsmb2.h index 8e9be343..e533002f 100644 --- a/include/smb2/libsmb2.h +++ b/include/smb2/libsmb2.h @@ -78,6 +78,12 @@ struct smb2dirent { #if defined(_WINDOWS) #include +#elif defined(_XBOX) +#include +#include +#endif + +#if defined(_WINDOWS) || defined(_XBOX) typedef SOCKET t_socket; #elif defined(_XBOX) #include @@ -86,7 +92,6 @@ typedef SOCKET t_socket; #else typedef int t_socket; #endif - /* * Create an SMB2 context. * Function returns diff --git a/lib/socket.c b/lib/socket.c index c4b14a4d..cf0d59f7 100644 --- a/lib/socket.c +++ b/lib/socket.c @@ -933,11 +933,9 @@ connect_async_ai(struct smb2_context *smb2, const struct addrinfo *ai, int *fd_o set_nonblocking(fd); set_tcp_sockopt(fd, TCP_NODELAY, 1); -#ifndef _XBOX #if 0 == CONFIGURE_OPTION_TCP_LINGER setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof yes); setsockopt(fd, SOL_SOCKET, SO_LINGER, &lin, sizeof lin); -#endif #endif if (connect(fd, (struct sockaddr *)&ss, socksize) != 0 From 187c03065f0f3a2050fda46ef41ba730466fa35e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Guilherme?= <75867486+Wolf3s@users.noreply.github.com> Date: Mon, 18 Dec 2023 14:47:24 -0300 Subject: [PATCH 19/23] Update init.c --- lib/init.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/lib/init.c b/lib/init.c index a3e01cad..d01a5af0 100644 --- a/lib/init.c +++ b/lib/init.c @@ -67,9 +67,6 @@ #include "compat.h" #ifdef _MSC_VER -#ifdef _XBOX -#include -#endif #include #define getlogin_r(a,b) ENXIO #define srandom srand From dd6ee33db09e6fb17fefb810a07fd95db839271c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Guilherme?= <75867486+Wolf3s@users.noreply.github.com> Date: Mon, 18 Dec 2023 14:48:52 -0300 Subject: [PATCH 20/23] Fix codeql warning --- lib/socket.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/socket.c b/lib/socket.c index 932178c5..10b5c29e 100644 --- a/lib/socket.c +++ b/lib/socket.c @@ -925,11 +925,15 @@ connect_async_ai(struct smb2_context *smb2, const struct addrinfo *ai, int *fd_o #ifdef _XBOX if(setsockopt(fd, SOL_SOCKET, 0x5801, (PCSTR)&bBroadcast, sizeof(BOOL) ) != 0 ) { - //return 0; +#if 0 + return 0; +#endif } if(setsockopt(fd, SOL_SOCKET, 0x5802, (PCSTR)&bBroadcast, sizeof(BOOL)) != 0) { - //return 0; +#if 0 + return 0; +#endif } #endif From 0914cd5e6b7823fe6e555feaa9f7bffc3ec636b1 Mon Sep 17 00:00:00 2001 From: Wolf3s Date: Tue, 19 Dec 2023 10:14:48 -0300 Subject: [PATCH 21/23] Remove GPL Socket emulation(except the wide code) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Remove duplicate codes in libsmb2.h and aes.c Add missing macros Remove some useless code Xbox XDK and Xbox 360 SDK doesn´t support getpid Fix readme use compat.h header in sys/socket.h --- README | 6 + Xbox 360/libsmb2.vcxproj | 15 - Xbox 360/libsmb2.vcxproj.filters | 37 -- Xbox/libsmb2.vcproj | 46 --- include/msvc/sys/socket.h | 6 - include/smb2/libsmb2.h | 4 - include/xbox 360/config.h | 1 + lib/aes.c | 3 - lib/compat.c | 36 ++ lib/compat.h | 71 +++- lib/errors.c | 4 - lib/init.c | 8 +- lib/socket.c | 15 +- lib/xbox/addrinfo.h | 122 ------ lib/xbox/addrsize.h | 36 -- lib/xbox/bittypes.h | 68 ---- lib/xbox/emu_socket.cpp | 623 ---------------------------- lib/xbox/emu_socket.h | 84 ---- lib/xbox/getaddrinfo.c | 672 ------------------------------- lib/xbox/getnameinfo.c | 243 ----------- lib/xbox/inet_aton.c | 148 ------- lib/xbox/inet_ntop.c | 200 --------- lib/xbox/inet_pton.c | 234 ----------- lib/xbox/sockstorage.h | 38 -- lib/xbox/xb_emu_socket.h | 86 ---- 25 files changed, 117 insertions(+), 2689 deletions(-) delete mode 100644 lib/xbox/addrinfo.h delete mode 100644 lib/xbox/addrsize.h delete mode 100644 lib/xbox/bittypes.h delete mode 100644 lib/xbox/emu_socket.cpp delete mode 100644 lib/xbox/emu_socket.h delete mode 100644 lib/xbox/getaddrinfo.c delete mode 100644 lib/xbox/getnameinfo.c delete mode 100644 lib/xbox/inet_aton.c delete mode 100644 lib/xbox/inet_ntop.c delete mode 100644 lib/xbox/inet_pton.c delete mode 100644 lib/xbox/sockstorage.h delete mode 100644 lib/xbox/xb_emu_socket.h diff --git a/README b/README index 08fb6ffa..dfa71c20 100644 --- a/README +++ b/README @@ -275,6 +275,12 @@ Then to build libsmb2, run $ cd lib $ make -f Makefile.DC_KOS clean install +The process will copy the resulting libsmb2.a and the include/smb2 headers to your +KallistiOS toolchain install location addons folder. +NOTE: There is not yet a kos-ports entry for libsmb2 but once a versioned release +that includes Dreamcast support is created installing from kos-ports will become +the preferred method of installation. + Xbox (Xbox XDK) ---------------------- Xbox CPU is acustom Intel Pentium III Coppermine-based processor which only supports litlle endian values. diff --git a/Xbox 360/libsmb2.vcxproj b/Xbox 360/libsmb2.vcxproj index ebc169b6..904966d3 100644 --- a/Xbox 360/libsmb2.vcxproj +++ b/Xbox 360/libsmb2.vcxproj @@ -248,12 +248,6 @@ - - - - - - @@ -268,15 +262,6 @@ - - CompileAsCpp - CompileAsCpp - - - - - - diff --git a/Xbox 360/libsmb2.vcxproj.filters b/Xbox 360/libsmb2.vcxproj.filters index 404746f6..e7d51e9d 100644 --- a/Xbox 360/libsmb2.vcxproj.filters +++ b/Xbox 360/libsmb2.vcxproj.filters @@ -26,9 +26,6 @@ {afbefc44-9daf-45e5-aacd-fe33021175a2} - - {2ee310d9-8aad-4af1-9726-53f9518d0978} - {6c01c6cd-316c-4bff-82ea-f611e60057e9} @@ -109,24 +106,6 @@ include\msvc\sys - - lib\xbox - - - lib\xbox - - - lib\xbox - - - lib\xbox - - - lib\xbox - - - lib\xbox - include\xbox 360 @@ -276,21 +255,5 @@ lib - - lib\xbox - - - lib\xbox - - - lib\xbox - - - lib\xbox - - - lib\xbox - - \ No newline at end of file diff --git a/Xbox/libsmb2.vcproj b/Xbox/libsmb2.vcproj index 09af523c..2a1eab7e 100644 --- a/Xbox/libsmb2.vcproj +++ b/Xbox/libsmb2.vcproj @@ -434,52 +434,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/include/msvc/sys/socket.h b/include/msvc/sys/socket.h index b89a3f88..85a7237f 100644 --- a/include/msvc/sys/socket.h +++ b/include/msvc/sys/socket.h @@ -6,13 +6,7 @@ extern "C" { #endif #ifdef _XBOX -#ifdef XBOX_360_PLATFORM -#include -#define inline __inline -#else #include "..\..\..\lib\compat.h" -#endif -#include #else #ifndef WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN diff --git a/include/smb2/libsmb2.h b/include/smb2/libsmb2.h index e533002f..4ba1173c 100644 --- a/include/smb2/libsmb2.h +++ b/include/smb2/libsmb2.h @@ -85,10 +85,6 @@ struct smb2dirent { #if defined(_WINDOWS) || defined(_XBOX) typedef SOCKET t_socket; -#elif defined(_XBOX) -#include -#include -typedef SOCKET t_socket; #else typedef int t_socket; #endif diff --git a/include/xbox 360/config.h b/include/xbox 360/config.h index 299f77de..d70f970c 100644 --- a/include/xbox 360/config.h +++ b/include/xbox 360/config.h @@ -3,6 +3,7 @@ /* Whether or not TCP sockets should be allowed to linger after closure */ #define CONFIGURE_OPTION_TCP_LINGER 1 +#define _U_ /* __attribute__((unused)) */ /* Define to 1 if you have the header file. */ /* #define HAVE_ARPA_INET_H 1 */ diff --git a/lib/aes.c b/lib/aes.c index 210e674b..96e7350e 100644 --- a/lib/aes.c +++ b/lib/aes.c @@ -463,9 +463,6 @@ void AES128_ECB_decrypt(uint8_t* input, const uint8_t* key, uint8_t *output) // Copy input to output, and work in-memory on output BlockCopy(output, input); - // Copy input to output, and work in-memory on output - BlockCopy(output, input); - // The KeyExpansion routine must be called before encryption. KeyExpansion(key, roundKey); diff --git a/lib/compat.c b/lib/compat.c index 095254dc..68802f25 100644 --- a/lib/compat.c +++ b/lib/compat.c @@ -42,6 +42,42 @@ #endif /* PICO_PLATFORM */ +#ifdef _XBOX + +int smb2_getaddrinfo(const char *node, const char*service, + const struct addrinfo *hints, + struct addrinfo **res) +{ + struct sockaddr_in *sin; + + sin = malloc(sizeof(struct sockaddr_in)); + sin->sin_family=AF_INET; + + /* Some error checking would be nice */ + sin->sin_addr.s_addr = inet_addr(node); + + sin->sin_port=0; + if (service) { + sin->sin_port=htons(atoi(service)); + } + + *res = malloc(sizeof(struct addrinfo)); + + (*res)->ai_family = AF_INET; + (*res)->ai_addrlen = sizeof(struct sockaddr_in); + (*res)->ai_addr = (struct sockaddr *)sin; + + return 0; +} + +void smb2_freeaddrinfo(struct addrinfo *res) +{ + free(res->ai_addr); + free(res); +} + +#endif + #ifdef PS2_EE_PLATFORM #include diff --git a/lib/compat.h b/lib/compat.h index 37516c60..6e596925 100644 --- a/lib/compat.h +++ b/lib/compat.h @@ -42,8 +42,7 @@ typedef unsigned int uintptr_t; #else #include /* XBOX 360 */ #endif - -#include "xbox/xb_emu_socket.h" +#include #ifndef ENETRESET #define ENETRESET WSAENETRESET @@ -65,8 +64,66 @@ typedef unsigned int uintptr_t; #define ENODATA WSANO_DATA #endif +#ifndef ETXTBSY +#define ETXTBSY 139 +#endif + +#ifndef ENOLINK +#define ENOLINK 121 +#endif + +#ifndef EWOULDBLOCK +#define EWOULDBLOCK WSAEWOULDBLOCK +#endif + #define snprintf _snprintf -#define _U_ + +#define EAI_AGAIN EAGAIN +#define EAI_FAIL 4 +#define EAI_MEMORY 6 +#define EAI_NONAME 8 +#define EAI_SERVICE 9 + +typedef int socklen_t; + +#ifndef POLLIN +#define POLLIN 0x0001 /* There is data to read */ +#endif +#ifndef POLLPRI +#define POLLPRI 0x0002 /* There is urgent data to read */ +#endif +#ifndef POLLOUT +#define POLLOUT 0x0004 /* Writing now will not block */ +#endif +#ifndef POLLERR +#define POLLERR 0x0008 /* Error condition */ +#endif +#ifndef POLLHUP +#define POLLHUP 0x0010 /* Hung up */ +#endif + +#ifndef SO_ERROR +#define SO_ERROR 0x1007 +#endif + +struct sockaddr_storage { +#ifdef HAVE_SOCKADDR_SA_LEN + unsigned char ss_len; +#endif /* HAVE_SOCKADDR_SA_LEN */ + unsigned char ss_family; + unsigned char fill[127]; +}; + +struct addrinfo { + int ai_flags; /* AI_PASSIVE, AI_CANONNAME */ + int ai_family; /* PF_xxx */ + int ai_socktype; /* SOCK_xxx */ + int ai_protocol; /* 0 or IPPROTO_xxx for IPv4 and IPv6 */ + size_t ai_addrlen; /* length of ai_addr */ + char *ai_canonname; /* canonical name for hostname */ + struct sockaddr *ai_addr; /* binary address */ + struct addrinfo *ai_next; /* next structure in linked list */ +}; /* XBOX Defs end */ struct pollfd { @@ -81,6 +138,14 @@ struct pollfd { int poll(struct pollfd *fds, unsigned int nfds, int timo); +int smb2_getaddrinfo(const char *node, const char*service, + const struct addrinfo *hints, + struct addrinfo **res); +void smb2_freeaddrinfo(struct addrinfo *res); + +#define getaddrinfo smb2_getaddrinfo +#define freeaddrinfo smb2_freeaddrinfo + /* just pretend they are the same so we compile */ #define sockaddr_in6 sockaddr_in diff --git a/lib/errors.c b/lib/errors.c index 538e4513..a6bd765d 100644 --- a/lib/errors.c +++ b/lib/errors.c @@ -1122,10 +1122,8 @@ int nterror_to_errno(uint32_t status) { case SMB2_STATUS_NOT_SUPPORTED: case SMB2_STATUS_NOT_A_REPARSE_POINT: return EINVAL; -#ifndef XBOX_PLATFORM case SMB2_STATUS_STOPPED_ON_SYMLINK: return ENOLINK; -#endif case SMB2_STATUS_TOO_MANY_OPENED_FILES: return EMFILE; case SMB2_STATUS_SECTION_TOO_BIG: @@ -1134,10 +1132,8 @@ int nterror_to_errno(uint32_t status) { return ENOMEM; case SMB2_STATUS_NOT_SAME_DEVICE: return EXDEV; -#ifndef XBOX_PLATFORM case SMB2_STATUS_SHARING_VIOLATION: return ETXTBSY; -#endif case SMB2_STATUS_FILE_LOCK_CONFLICT: case SMB2_STATUS_LOCK_NOT_GRANTED: return EDEADLK; diff --git a/lib/init.c b/lib/init.c index d01a5af0..a4ba29c1 100644 --- a/lib/init.c +++ b/lib/init.c @@ -71,7 +71,9 @@ #define getlogin_r(a,b) ENXIO #define srandom srand #define random rand +#ifndef _XBOX #define getpid GetCurrentProcessId +#endif #endif /* _MSC_VER */ #ifdef ESP_PLATFORM @@ -280,11 +282,11 @@ struct smb2_context *smb2_init_context(void) int i, ret; static int ctr; #ifdef _XBOX - srandom(time(NULL) ^ 1 ^ ctr++); + srandom(time(NULL) ^ ctr++); #else srandom(time(NULL) ^ getpid() ^ ctr++); #endif - smb2 = calloc(1, sizeof(struct smb2_context)); + smb2 = calloc(1, sizeof(struct smb2_context)); if (smb2 == NULL) { return NULL; } @@ -418,7 +420,7 @@ static void smb2_set_error_string(struct smb2_context *smb2, const char * error_ #ifdef _XBOX if (_vsnprintf(errstr, MAX_ERROR_SIZE, error_string, args) < 0) { #else - if (vsnprintf(errstr, MAX_ERROR_SIZE, error_string, args) < 0) { + if (vsnprintf(errstr, MAX_ERROR_SIZE, error_string, args) < 0) { #endif strncpy(errstr, "could not format error string!", MAX_ERROR_SIZE); diff --git a/lib/socket.c b/lib/socket.c index 10b5c29e..5fe822d9 100644 --- a/lib/socket.c +++ b/lib/socket.c @@ -262,11 +262,7 @@ smb2_write_to_socket(struct smb2_context *smb2) count = writev(smb2->fd, tmpiov, niov); if (count == -1) { -#ifdef XBOX_PLATFORM - if (errno == EAGAIN) { -#else if (errno == EAGAIN || errno == EWOULDBLOCK) { -#endif return 0; } smb2_set_error(smb2, "Error when writing to " @@ -856,11 +852,7 @@ set_tcp_sockopt(t_socket sockfd, int optname, int value) int level; #if !defined(SOL_TCP) struct protoent *buf; -#ifdef XBOX_PLATFORM - if ((buf = sckemu_getprotobyname("tcp")) != NULL) { -#else if ((buf = getprotobyname("tcp")) != NULL) { -#endif level = buf->p_proto; } else { return -1; @@ -895,7 +887,7 @@ connect_async_ai(struct smb2_context *smb2, const struct addrinfo *ai, int *fd_o ((struct sockaddr_in *)&ss)->sin_len = socksize; #endif break; -#ifdef ENABLE_IPV6 +#ifndef _XBOX case AF_INET6: #if !defined(PICO_PLATFORM) || defined(LWIP_INETV6) socksize = sizeof(struct sockaddr_in6); @@ -1081,13 +1073,8 @@ smb2_connect_async(struct smb2_context *smb2, const char *server, #if defined(_WINDOWS) || defined(_XBOX) if (err == WSANOTINITIALISED) { -#ifdef _XBOX - smb2_set_error(smb2, "Winsockx was not initialized. " - "Please call WSAStartup()."); -#else smb2_set_error(smb2, "Winsock was not initialized. " "Please call WSAStartup()."); -#endif return -WSANOTINITIALISED; } else diff --git a/lib/xbox/addrinfo.h b/lib/xbox/addrinfo.h deleted file mode 100644 index f1e6e56b..00000000 --- a/lib/xbox/addrinfo.h +++ /dev/null @@ -1,122 +0,0 @@ -/* - * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the project nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -/* $Id: addrinfo.h 31144 2012-07-15 15:38:56Z buzz $ */ - -#ifndef HAVE_ADDRINFO - -/* - * Error return codes from getaddrinfo() - */ -#define EAI_ADDRFAMILY 1 /* address family for hostname not supported */ -#define EAI_AGAIN 2 /* temporary failure in name resolution */ -#define EAI_BADFLAGS 3 /* invalid value for ai_flags */ -#define EAI_FAIL 4 /* non-recoverable failure in name resolution */ -#define EAI_FAMILY 5 /* ai_family not supported */ -#define EAI_MEMORY 6 /* memory allocation failure */ -#define EAI_NODATA 7 /* no address associated with hostname */ -#define EAI_NONAME 8 /* hostname nor servname provided, or not known */ -#define EAI_SERVICE 9 /* servname not supported for ai_socktype */ -#define EAI_SOCKTYPE 10 /* ai_socktype not supported */ -#define EAI_SYSTEM 11 /* system error returned in errno */ -#define EAI_BADHINTS 12 -#define EAI_PROTOCOL 13 -#define EAI_MAX 14 - -/* internal error */ -#define NETDB_INTERNAL -1 /* see errno */ - -/* - * Flag values for getaddrinfo() - */ -#define AI_PASSIVE 0x00000001 /* get address to use bind() */ -#define AI_CANONNAME 0x00000002 /* fill ai_canonname */ -#define AI_NUMERICHOST 0x00000004 /* prevent name resolution */ -/* valid flags for addrinfo */ -#define AI_MASK (AI_PASSIVE | AI_CANONNAME | AI_NUMERICHOST) - -#define AI_ALL 0x00000100 /* IPv6 and IPv4-mapped (with AI_V4MAPPED) */ -#define AI_V4MAPPED_CFG 0x00000200 /* accept IPv4-mapped if kernel supports */ -#define AI_ADDRCONFIG 0x00000400 /* only if any address is assigned */ -#define AI_V4MAPPED 0x00000800 /* accept IPv4-mapped IPv6 address */ -/* special recommended flags for getipnodebyname */ -#define AI_DEFAULT (AI_V4MAPPED_CFG | AI_ADDRCONFIG) - -struct addrinfo { - int ai_flags; /* AI_PASSIVE, AI_CANONNAME */ - int ai_family; /* PF_xxx */ - int ai_socktype; /* SOCK_xxx */ - int ai_protocol; /* 0 or IPPROTO_xxx for IPv4 and IPv6 */ - size_t ai_addrlen; /* length of ai_addr */ - char *ai_canonname; /* canonical name for hostname */ - struct sockaddr *ai_addr; /* binary address */ - struct addrinfo *ai_next; /* next structure in linked list */ -}; - -extern void freeaddrinfo (struct addrinfo *); -extern void freehostent (struct hostent *); -extern char *gai_strerror (int); -extern int getaddrinfo (const char *, const char *, - const struct addrinfo *, struct addrinfo **); -extern int getnameinfo (const struct sockaddr *, size_t, char *, - size_t, char *, size_t, int); -extern struct hostent *getipnodebyaddr (const void *, size_t, int, int *); -extern struct hostent *getipnodebyname (const char *, int, int, int *); -extern int inet_pton (int, const char *, void *); -extern const char *inet_ntop (int, const void *, char *, size_t); -#endif /* HAVE_ADDRINFO */ - -/* - * Constants for getnameinfo() - */ -#ifndef NI_MAXHOST -#define NI_MAXHOST 1025 -#endif -#ifndef NI_MAXSERV -#define NI_MAXSERV 32 -#endif - -/* - * Flag values for getnameinfo() - */ -#ifndef NI_NOFQDN -#define NI_NOFQDN 0x00000001 -#endif -#ifndef NI_NUMERICHOST -#define NI_NUMERICHOST 0x00000002 -#endif -#ifndef NI_NAMEREQD -#define NI_NAMEREQD 0x00000004 -#endif -#ifndef NI_NUMERICSERV -#define NI_NUMERICSERV 0x00000008 -#endif -#ifndef NI_DGRAM -#define NI_DGRAM 0x00000010 -#endif diff --git a/lib/xbox/addrsize.h b/lib/xbox/addrsize.h deleted file mode 100644 index 13db4da9..00000000 --- a/lib/xbox/addrsize.h +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright (C) 1999 WIDE Project. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the project nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ -#ifndef INADDRSZ -#define INADDRSZ 4 /* IPv4 T_A */ -#endif - -#ifndef IN6ADDRSZ -#define IN6ADDRSZ 16 /* IPv6 T_AAAA */ -#endif - diff --git a/lib/xbox/bittypes.h b/lib/xbox/bittypes.h deleted file mode 100644 index 405e0cb9..00000000 --- a/lib/xbox/bittypes.h +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Copyright (C) 1999 WIDE Project. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the project nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ -#ifndef HAVE_U_INT8_T - -typedef unsigned __int8 u_int8_t; -typedef unsigned __int16 u_int16_t; -typedef unsigned __int32 u_int32_t; -typedef unsigned __int64 u_int64_t; - -#endif /* HAVE_U_INT64_T */ - -#ifndef PRId64 -#ifdef _MSC_EXTENSIONS -#define PRId64 "I64d" -#else /* _MSC_EXTENSIONS */ -#define PRId64 "lld" -#endif /* _MSC_EXTENSIONS */ -#endif /* PRId64 */ - -#ifndef PRIo64 -#ifdef _MSC_EXTENSIONS -#define PRIo64 "I64o" -#else /* _MSC_EXTENSIONS */ -#define PRIo64 "llo" -#endif /* _MSC_EXTENSIONS */ -#endif /* PRIo64 */ - -#ifndef PRIx64 -#ifdef _MSC_EXTENSIONS -#define PRIx64 "I64x" -#else /* _MSC_EXTENSIONS */ -#define PRIx64 "llx" -#endif /* _MSC_EXTENSIONS */ -#endif /* PRIx64 */ - -#ifndef PRIu64 -#ifdef _MSC_EXTENSIONS -#define PRIu64 "I64u" -#else /* _MSC_EXTENSIONS */ -#define PRIu64 "llu" -#endif /* _MSC_EXTENSIONS */ -#endif /* PRIu64 */ diff --git a/lib/xbox/emu_socket.cpp b/lib/xbox/emu_socket.cpp deleted file mode 100644 index e7e2bce3..00000000 --- a/lib/xbox/emu_socket.cpp +++ /dev/null @@ -1,623 +0,0 @@ -/* - * Copyright (C) 2005-2013 Team XBMC - * http://xbmc.org - * - * This Program 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 Foundation; either version 2, or (at your option) - * any later version. - * - * This Program 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 XBMC; see the file COPYING. If not, see - * . - * - */ -#include "emu_socket.h" -#include - -#define MAX_SOCKETS 100 - -#define MSG_PEEK 0x2 - -static bool m_bSocketsInit = false; -typedef struct -{ - SOCKET sock; - char* data; - char* start; - char* end; -} SSocketData; -static SSocketData m_sockets[MAX_SOCKETS + 1]; - -void InitSockets() -{ - m_bSocketsInit = true; - memset(m_sockets, 0, sizeof(SSocketData) * (MAX_SOCKETS + 1)); - for(int i = 0; i < MAX_SOCKETS; i++) - m_sockets[i].sock = INVALID_SOCKET; -} - -SOCKET GetSocketForIndex(int iIndex) -{ - if (iIndex < 3 || iIndex >= MAX_SOCKETS) - { - printf("GetSocketForIndex() invalid index: %i", iIndex); - return INVALID_SOCKET; - } - - if (InterlockedCompareExchangePointer((PVOID*)&m_sockets[iIndex].sock, (PVOID)INVALID_SOCKET, (PVOID)INVALID_SOCKET) == (PVOID)INVALID_SOCKET) - { - printf("GetSocketForIndex() invalid socket for index: %i", iIndex); - } - - return m_sockets[iIndex].sock; -} - -int GetIndexForSocket(SOCKET iSocket) -{ - for (int i = 0; i < MAX_SOCKETS; i++) - { - if (InterlockedCompareExchangePointer((PVOID*)&m_sockets[i].sock, (PVOID)iSocket, (PVOID)iSocket) == (PVOID)iSocket) - return i; - } - return -1; -} - -int AddSocket(SOCKET iSocket) -{ - if(!m_bSocketsInit) - InitSockets(); - - if(iSocket == INVALID_SOCKET) - return -1; - - for (int i = 3; i < MAX_SOCKETS; i++) - { - if (InterlockedCompareExchangePointer((PVOID*)&m_sockets[i].sock, (PVOID)iSocket, (PVOID)INVALID_SOCKET) == (PVOID)INVALID_SOCKET) - { - if(m_sockets[i].data) delete m_sockets[i].data; - m_sockets[i].data = NULL; - m_sockets[i].start = NULL; - m_sockets[i].end = NULL; - return i; - } - } - printf(__FUNCTION__ " - Unable to add socket to internal list, no space left"); - return -1; -} - -void ReleaseSocket(int iIndex) -{ - if (iIndex < 3 || iIndex >= MAX_SOCKETS) - { - printf("ReleaseSocket() invalid index:%i", iIndex); - return ; - } - - if (InterlockedExchangePointer((PVOID*)&m_sockets[iIndex].sock, (PVOID)INVALID_SOCKET) == (PVOID)INVALID_SOCKET) - printf("ReleaseSocket() invalid socket for index:%i", iIndex); - - if(m_sockets[iIndex].data) delete m_sockets[iIndex].data; - m_sockets[iIndex].data = NULL; - m_sockets[iIndex].start = NULL; - m_sockets[iIndex].end = NULL; -} - -extern "C" -{ - -#ifdef _XBOX - int __stdcall sckemu_gethostname(char* name, int namelen) - { - if ((unsigned int)namelen < strlen("xbox") + 1) return -1; - strcpy(name, "xbox"); - return 0; - } -#else - int __stdcall sckemu_gethostname(char* name, int namelen) - { - return gethostname(name, namelen); - } -#endif - - static struct mphostent hbn_hostent; - static char* hbn_cAliases[]= { NULL, NULL }; // only one NULL is needed acutally - static char* hbn_dwlist1[] = {NULL, NULL, NULL}; - static DWORD hbn_dwList2[] = {0, 0, 0}; - static char hbn_hostname[128]; - -#ifdef _XBOX - struct mphostent* __stdcall sckemu_gethostbyname(const char* name) // TODO: Not currently used or needed - { - printf("Socket Emu - gethostbyname() not implemented."); - - return NULL; -#if 0 - CStdString strIpAdres; - - hbn_hostent.h_name = hbn_hostname; - hbn_hostname[0] = '\0'; // clear hostname - hbn_hostent.h_aliases = hbn_cAliases; - hbn_hostent.h_addrtype = AF_INET; - hbn_hostent.h_length = 4; - hbn_hostent.h_addr_list = hbn_dwlist1; - hbn_hostent.h_addr_list[0] = (char*)hbn_dwList2; - - sckemu_gethostname(hbn_hostname, 128); - - if (!strcmp(hbn_hostname, name)) - { - if(g_application.getNetwork().IsAvailable()) - hbn_dwList2[0] = inet_addr(g_application.getNetwork().m_networkinfo.ip); - - return &hbn_hostent; - } - if (CDNSNameCache::Lookup(name, strIpAdres)) - { - strcpy(hbn_hostname, name); - hbn_dwList2[0] = inet_addr(strIpAdres.c_str()); - return &hbn_hostent; - } - else - { - OutputDebugString("DNS:no ipadres found\n"); - return NULL; - } - } - - // libRV.lib depends on gethostbyname - struct hostent; - struct hostent* gethostbyname(const char* name) - { - return (hostent*)sckemu_gethostbyname(name); -#endif - } -#else - struct mphostent* __stdcall sckemu_gethostbyname(const char* name) - { - return (mphostent*)gethostbyname(name); - } -#endif - - int __stdcall sckemu_connect(int s, const struct sockaddr FAR *name, int namelen) - { - SOCKET socket = GetSocketForIndex(s); - struct sockaddr_in* pTmp = (struct sockaddr_in*)name; - char szBuf[128]; -// sprintf(szBuf, "connect: family:%i port:%i ip:%i.%i.%i.%i\n", - // pTmp->sin_family, - // ntohs(pTmp->sin_port), - // pTmp->sin_addr.S_un.S_addr&0xff, - // (pTmp->sin_addr.S_un.S_addr&0xff00) >> 8, - // (pTmp->sin_addr.S_un.S_addr&0xff0000) >> 16, - // (pTmp->sin_addr.S_un.S_addr) >> 24 - // ); - OutputDebugString(szBuf); - - int iResult = connect( socket, name, namelen); - if(iResult == SOCKET_ERROR) - { - errno = WSAGetLastError(); - // sprintf(szBuf, "connect returned:%i %i\n", iResult, WSAGetLastError()); - // Don't OutputDebugString here, as no debugger attached - // this will (strangely) reset WSAGetLastError() - //OutputDebugString(szBuf); - } - - return iResult; - } - - int __stdcall sckemu_send(int s, const char *buf, int len, int flags) - { - SOCKET socket = GetSocketForIndex(s); - //flags Unsupported; must be zero. - int res = send(socket, buf, len, 0); - if(res == SOCKET_ERROR) - errno = WSAGetLastError(); - return res; - } - - int __stdcall sckemu_socket(int af, int type, int protocol) - { - SOCKET sock = socket(af, type, protocol); - if(sock == INVALID_SOCKET) - errno = WSAGetLastError(); - return AddSocket(sock);; - } - - int __stdcall sckemu_bind(int s, const struct sockaddr FAR * name, int namelen) - { - SOCKET socket = GetSocketForIndex(s); - struct sockaddr_in address2; - - if( name->sa_family == AF_INET - && namelen >= sizeof(sockaddr_in) ) - { - struct sockaddr_in* address = (struct sockaddr_in*)name; - -// CLog::Log(LOGDEBUG, "sckemu_bind: family:%i port:%i ip:%i.%i.%i.%i\n", -// address->sin_family, - // ntohs(address->sin_port), - // address->sin_addr.S_un.S_un_b.s_b1, - // address->sin_addr.S_un.S_un_b.s_b2, - // address->sin_addr.S_un.S_un_b.s_b3, - // address->sin_addr.S_un.S_un_b.s_b4); - - - // if( address->sin_addr.S_un.S_addr == inet_addr(g_application.getNetwork().m_networkinfo.ip) - // || address->sin_addr.S_un.S_addr == inet_addr("127.0.0.1") ) - { - // local xbox, correct for xbox stack - address2 = *address; - address2.sin_addr.S_un.S_addr = 0; - name = (sockaddr*)&address2; - namelen = sizeof(address2); - } - } - - int iResult = bind(socket, name, namelen); - if(iResult == SOCKET_ERROR) - { - errno = WSAGetLastError(); - printf("bind returned:%i %i\n", iResult, WSAGetLastError()); - } - return iResult; - } - - int __stdcall sckemu_closesocket(int s) - { - SOCKET socket = GetSocketForIndex(s); - - int iResult = closesocket(socket); - if(iResult == SOCKET_ERROR) - errno = WSAGetLastError(); - - ReleaseSocket(s); - return iResult; - } - - int __stdcall sckemu_getsockopt(int s, int level, int optname, char FAR * optval, int FAR * optlen) - { - SOCKET socket = GetSocketForIndex(s); - - if(optname == SO_ERROR) - { - /* unsupported option */ - /* just hope there was no error, we could probably check connetion state if we wanted */ - *((int*)optval) = 0; - return 0; - } - else - return getsockopt(socket, level, optname, optval, optlen); - } - - int __stdcall sckemu_ioctlsocket(int s, long cmd, DWORD FAR * argp) - { - if (s < 3 || s >= MAX_SOCKETS) - { - printf("sckemu_ioctlsocket invalid index:%i", s); - return SOCKET_ERROR; - } - SSocketData& socket = m_sockets[s]; - - int res = ioctlsocket(socket.sock, cmd, argp); - if( res == 0 && cmd == FIONREAD && socket.data ) - *argp += socket.end - socket.start; - - return res; - } - - int __stdcall sckemu_recv(int s, char FAR * buf, int len, int flags) - { - if (s < 3 || s >= MAX_SOCKETS) - { - printf("sckemu_recv invalid index:%i", s); - return SOCKET_ERROR; - } - SSocketData& socket = m_sockets[s]; - int len2, len3; - - if(flags & MSG_PEEK) - { - printf(__FUNCTION__" - called with MSG_PEEK set, attempting workaround"); - // work around for peek, it will give garbage as data - - if(socket.data == NULL) - { - socket.data = new char[len]; - len2 = recv(socket.sock, socket.data, len, 0); - if(len2 == SOCKET_ERROR) - { - // SAFE_DELETE(socket.data); - return SOCKET_ERROR; - } - socket.start = socket.data; - socket.end = socket.start + len2; - } -// len2 = std::min(len, socket.end - socket.start); - memcpy(buf, socket.start, len2); - return len2; - } - - if(flags) - printf(__FUNCTION__" - called with flags %d that will be ignored", flags); - flags = 0; - - len2 = 0; - if(socket.start < socket.end) - { -// len2 = std::min(len, socket.end - socket.start); - memcpy(buf, socket.start, len2); - socket.start += len2; - buf++; - len--; - } - - if(socket.start >= socket.end) - { - delete[] socket.data; - socket.data = NULL; - socket.start = NULL; - socket.end = NULL; - } - - if(len == 0) - return 0; - - len3 = recv(socket.sock, buf, len, flags); - if(len3 == SOCKET_ERROR) - return SOCKET_ERROR; - - return len2 + len3; - } - - int __stdcall sckemu_select(int nfds, fd_set FAR * readfds, fd_set FAR * writefds, fd_set FAR *exceptfds, const struct timeval FAR * timeout) - { - fd_set readset, writeset, exceptset; - unsigned int i; - - fd_set* preadset = &readset; - fd_set* pwriteset = &writeset; - fd_set* pexceptset = &exceptset; - - if (!readfds) preadset = NULL; - if (!writefds) pwriteset = NULL; - if (!exceptfds) pexceptset = NULL; - - FD_ZERO(&readset); - FD_ZERO(&writeset); - FD_ZERO(&exceptset); - - if(readfds) - { - for (i = 0; i < readfds->fd_count; ++i) - FD_SET(GetSocketForIndex(readfds->fd_array[i]), &readset); - } - if(writefds) - { - for (i = 0; i < writefds->fd_count; ++i) - FD_SET(GetSocketForIndex(writefds->fd_array[i]), &writeset); - } - if(exceptfds) - { - for (i = 0; i < exceptfds->fd_count; ++i) - FD_SET(GetSocketForIndex(exceptfds->fd_array[i]), &exceptset); - } - - int ifd = select(nfds, preadset, pwriteset, pexceptset, timeout); - - // convert real socket identifiers back to our custom ones and clean results - if (readfds) - { - FD_ZERO(readfds); - for (i = 0; i < readset.fd_count; i++) FD_SET(GetIndexForSocket(readset.fd_array[i]), readfds); - } - if (writefds) - { - FD_ZERO(writefds); - for (i = 0; i < writeset.fd_count; i++) FD_SET(GetIndexForSocket(writeset.fd_array[i]), writefds); - } - if (exceptfds) - { - FD_ZERO(exceptfds); - for (i = 0; i < exceptset.fd_count; i++) FD_SET(GetIndexForSocket(exceptset.fd_array[i]), exceptfds); - } - - return ifd; - } - - int __stdcall sckemu_sendto(int s, const char FAR * buf, int len, int flags, const struct sockaddr FAR * to, int tolen) - { - SOCKET socket = GetSocketForIndex(s); - int res = sendto(socket, buf, len, flags, to, tolen); - if(res == SOCKET_ERROR) - errno = WSAGetLastError(); - return res; - } - - int __stdcall sckemu_setsockopt(int s, int level, int optname, const char FAR * optval, int optlen) - { - SOCKET socket = GetSocketForIndex(s); - int res = setsockopt(socket, level, optname, optval, optlen); - if(res == SOCKET_ERROR) - errno = WSAGetLastError(); - return res; - } - - int __stdcall sckemu_accept(int s, struct sockaddr FAR * addr, OUT int FAR * addrlen) - { - SOCKET socket = GetSocketForIndex(s); - SOCKET res = accept(socket, addr, addrlen); - if(res == INVALID_SOCKET) - errno = WSAGetLastError(); - return AddSocket(res); - } - - int __stdcall sckemu_getsockname(int s, struct sockaddr* name, int* namelen) - { - SOCKET socket = GetSocketForIndex(s); - int res = getsockname(socket, name, namelen); - if(res == SOCKET_ERROR) - { - errno = WSAGetLastError(); - return res; - } - - if( name->sa_family == AF_INET - && *namelen >= sizeof(sockaddr_in) ) - { - sockaddr_in *addr = (sockaddr_in*)name; - if( addr->sin_port > 0 && addr->sin_addr.S_un.S_addr == 0 ) - { - // unspecifed address will always be on local xbox ip - // some dll's assume this will return a proper address - // even if windows standard doesn't gurantee it - // if( g_application.getNetwork().IsAvailable() ) - // addr->sin_addr.S_un.S_addr = inet_addr(g_application.getNetwork().m_networkinfo.ip); - } - } - return res; - } - - int __stdcall sckemu_listen(int s, int backlog) - { - SOCKET socket = GetSocketForIndex(s); - int res = listen(socket, backlog); - if(res == SOCKET_ERROR) - errno = WSAGetLastError(); - return res; - } - - u_short __stdcall sckemu_ntohs(u_short netshort) - { - return ntohs(netshort); - } - - int __stdcall sckemu_recvfrom(int s, char* buf, int len, int flags, struct sockaddr* from, int* fromlen) - { - SOCKET socket = GetSocketForIndex(s); - int res = recvfrom(socket, buf, len, flags, from, fromlen); - if(res == SOCKET_ERROR) - errno = WSAGetLastError(); - return res; - } - - int __stdcall sckemu_WSAFDIsSet(int s, fd_set* set) - { - fd_set real_set; // the set with real socket id's - int real_socket = GetSocketForIndex(s); - - FD_ZERO(&real_set); - if (set) - { - for (unsigned int i = 0; i < set->fd_count; ++i) - FD_SET(GetSocketForIndex(set->fd_array[i]), &real_set); - } - - return __WSAFDIsSet(real_socket, &real_set); - } - - int __stdcall sckemu_shutdown(int s, int how) - { - SOCKET socket = GetSocketForIndex(s); - return shutdown(socket, how); - } - - char* __stdcall sckemu_ntoa (struct in_addr in) - { - static char _inetaddress[32]; -// sprintf(_inetaddress, "%d.%d.%d.%d", in.S_un.S_un_b.s_b1, in.S_un.S_un_b.s_b2, in.S_un.S_un_b.s_b3, in.S_un.S_un_b.s_b4); - return _inetaddress; - } - -#ifdef _XBOX - - static struct mphostent hba_hostent; - static char* hba_cAliases[]= { NULL, NULL }; // only one NULL is needed acutally - static char* hba_dwlist1[] = {NULL, NULL, NULL}; - static DWORD hba_dwList2[] = {0, 0, 0}; - static char hba_hostname[128]; - - struct mphostent* __stdcall sckemu_gethostbyaddr(const char* addr, int len, int type) - { - printf("Untested function sckemu_gethostbyaddr called!"); - XNADDR xna; - DWORD dwState; - - hba_hostent.h_name = hba_hostname; - hba_hostname[0] = '\0'; // clear hostname - hba_hostent.h_aliases = hba_cAliases; - hba_hostent.h_addrtype = AF_INET; - hba_hostent.h_length = 4; - hba_hostent.h_addr_list = hba_dwlist1; - hba_hostent.h_addr_list[0] = (char*)hba_dwList2; - - do dwState = XNetGetTitleXnAddr(&xna); - while (dwState == XNET_GET_XNADDR_PENDING); - - if (!memcmp(addr, &(xna.ina.s_addr), 4)) - { - //get title hostent - sckemu_gethostname(hba_hostname, 128); - hba_dwList2[0] = xna.ina.S_un.S_addr; - return &hba_hostent; - } - else - { - //get client hostent - in_addr client = {{addr[0], addr[1], addr[2], addr[3]}}; - strcpy(hba_hostname, sckemu_ntoa(client)); - hba_dwList2[0] = client.S_un.S_addr; - return &hba_hostent; - } - return 0; - } -#endif - - struct servent* __stdcall sckemu_getservbyname(const char* name, const char* proto) - { - OutputDebugString("TODO: getservbyname\n"); - return NULL; - } - - struct servent* __stdcall sckemu_getservbyport(int port, const char* proto) - { - OutputDebugString("TODO: getservbyport\n"); - return NULL; - } - - struct protoent* __stdcall sckemu_getprotobyname(const char* name) - { - OutputDebugString("TODO: getprotobyname\n"); - return NULL; - } - - int __stdcall sckemu_getpeername(int s, struct sockaddr FAR *name, int FAR *namelen) - { - int socket = GetSocketForIndex(s); - return getpeername(socket, name, namelen); - } - - int getaddrinfo(const char *hostname, const char *servname, const struct addrinfo *hints, struct addrinfo **res); - int __stdcall sckemu_getaddrinfo(const char* nodename, const char* servname, const struct addrinfo* hints, struct addrinfo** res) - { - return getaddrinfo(nodename, servname, hints, res); - } - - int getnameinfo(const struct sockaddr *sa, size_t salen, char *host, size_t hostlen, char *serv, size_t servlen, int flags); - int __stdcall sckemu_getnameinfo(const struct sockaddr *sa, size_t salen, char *host, size_t hostlen, char *serv, size_t servlen, int flags) - { - return getnameinfo(sa, salen, host, hostlen, serv, servlen, flags); - } - - void freeaddrinfo(struct addrinfo *ai); - void __stdcall sckemu_freeaddrinfo(struct addrinfo *ai) - { - return freeaddrinfo(ai); - } -} diff --git a/lib/xbox/emu_socket.h b/lib/xbox/emu_socket.h deleted file mode 100644 index 0d178661..00000000 --- a/lib/xbox/emu_socket.h +++ /dev/null @@ -1,84 +0,0 @@ -#ifndef _EMU_SOCKET_EMU_SOCKET_H_ -#define _EMU_SOCKET_EMU_SOCKET_H_ - -/* - * Copyright (C) 2005-2013 Team XBMC - * http://xbmc.org - * - * This Program 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 Foundation; either version 2, or (at your option) - * any later version. - * - * This Program 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 XBMC; see the file COPYING. If not, see - * . - * - */ - -#ifdef _XBOX -#include -#endif - -#define SOCK_RAW 3 -#define SO_ERROR 0x1007 - -//#define EAFNOSUPPORT WSAEAFNOSUPPORT - -#define INET_ADDRSTRLEN 16 - -struct mphostent { - char *h_name; /* official name of host */ - char **h_aliases; /* alias list */ - short h_addrtype; /* host address type */ - short h_length; /* length of address */ - char **h_addr_list; /* list of addresses from name server */ -}; - -#ifdef _XBOX -typedef struct servent { - char FAR* s_name; - char FAR FAR** s_aliases; - short s_port; - char FAR* s_proto; -} servent; - -typedef struct protoent { - char FAR* p_name; - char FAR FAR** p_aliases; - short p_proto; -} protoent; -#endif - -#ifndef IN_MULTICAST -#define IN_MULTICAST(i) (((i) & 0xf0000000U) == 0xe0000000U) -#endif - -#ifndef IN_EXPERIMENTAL -#define IN_EXPERIMENTAL(i) (((i) & 0xe0000000U) == 0xe0000000U) -#endif - -#ifndef IN_LOOPBACKNET -#define IN_LOOPBACKNET 127 -#endif - -/* -typedef struct addrinfo -{ - int ai_flags; // AI_PASSIVE, AI_CANONNAME, AI_NUMERICHOST - int ai_family; // PF_xxx - int ai_socktype; // SOCK_xxx - int ai_protocol; // 0 or IPPROTO_xxx for IPv4 and IPv6 - size_t ai_addrlen; // Length of ai_addr - char * ai_canonname; // Canonical name for nodename - struct sockaddr * ai_addr; // Binary address - struct addrinfo * ai_next; // Next structure in linked list -} -ADDRINFOA, *PADDRINFOA; -*/ -#endif //_EMU_SOCKET_EMU_SOCKET_H_ diff --git a/lib/xbox/getaddrinfo.c b/lib/xbox/getaddrinfo.c deleted file mode 100644 index 69ea2e8a..00000000 --- a/lib/xbox/getaddrinfo.c +++ /dev/null @@ -1,672 +0,0 @@ -/* - * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the project nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -/* - * "#ifdef FAITH" part is local hack for supporting IPv4-v6 translator. - * - * Issues to be discussed: - * - Thread safe-ness must be checked. - * - Return values. There are nonstandard return values defined and used - * in the source code. This is because RFC2553 is silent about which error - * code must be returned for which situation. - * Note: - * - We use getipnodebyname() just for thread-safeness. There's no intent - * to let it do PF_UNSPEC (actually we never pass PF_UNSPEC to - * getipnodebyname(). - * - The code filters out AFs that are not supported by the kernel, - * when globbing NULL hostname (to loopback, or wildcard). Is it the right - * thing to do? What is the relationship with post-RFC2553 AI_ADDRCONFIG - * in ai_flags? - */ - -#ifdef _XBOX -#include -#include -#else -#include -#endif -#include -#include "addrinfo.h" -#include "emu_socket.h" -#include -#include -#include -#include -#include -#include -#include - -#ifdef _XBOX -extern struct mphostent* __stdcall sckemu_gethostbyname(const char* name); -#define gethostbyname(name) sckemu_gethostbyname(name) - -extern struct mphostent* __stdcall sckemu_gethostbyaddr(const char* addr, int len, int type); -#define gethostbyaddr(addr, len, type) sckemu_gethostbyaddr(addr, len, type) - -extern struct servent* __stdcall sckemu_getservbyname(const char* name, const char* proto); -#define getservbyname(name, proto) sckemu_getservbyname(name, proto) - -#define hostent mphostent -#endif - -#ifndef HAVE_U_INT32_T -#include "bittypes.h" -#endif - -#ifndef HAVE_SOCKADDR_STORAGE -#include "sockstorage.h" -#endif - -#ifdef NEED_ADDRINFO_H -#include "addrinfo.h" -#endif - -#if defined(__KAME__) && defined(ENABLE_IPV6) -# define FAITH -#endif - -#define SUCCESS 0 -#define GAI_ANY 0 -#define YES 1 -#define NO 0 - -#ifdef FAITH -static int translate = NO; -static struct in6_addr faith_prefix = IN6ADDR_GAI_ANY_INIT; -#endif - -static const char in_addrany[] = { 0, 0, 0, 0 }; -static const char in6_addrany[] = { - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 -}; -static const char in_loopback[] = { 127, 0, 0, 1 }; -static const char in6_loopback[] = { - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 -}; - -struct sockinet { - u_char si_len; - u_char si_family; - u_short si_port; -}; - -static struct gai_afd { - int a_af; - int a_addrlen; - int a_socklen; - int a_off; - const char *a_addrany; - const char *a_loopback; -} gai_afdl [] = { -#ifdef ENABLE_IPV6 -#define N_INET6 0 - {PF_INET6, sizeof(struct in6_addr), - sizeof(struct sockaddr_in6), - offsetof(struct sockaddr_in6, sin6_addr), - in6_addrany, in6_loopback}, -#define N_INET 1 -#else -#define N_INET 0 -#endif - {PF_INET, sizeof(struct in_addr), - sizeof(struct sockaddr_in), - offsetof(struct sockaddr_in, sin_addr), - in_addrany, in_loopback}, - {0, 0, 0, 0, NULL, NULL}, -}; - -#ifdef ENABLE_IPV6 -#define PTON_MAX 16 -#else -#define PTON_MAX 4 -#endif - -#ifndef IN_MULTICAST -#define IN_MULTICAST(i) (((i) & 0xf0000000U) == 0xe0000000U) -#endif - -#ifndef IN_EXPERIMENTAL -#define IN_EXPERIMENTAL(i) (((i) & 0xe0000000U) == 0xe0000000U) -#endif - -#ifndef IN_LOOPBACKNET -#define IN_LOOPBACKNET 127 -#endif - -static int get_name (const char *, struct gai_afd *, struct addrinfo **, char *, struct addrinfo *, int); -static int get_addr (const char *, int, struct addrinfo **, - struct addrinfo *, int); -static int str_isnumber (const char *); - -static char *ai_errlist[] = { - "success.", - "address family for hostname not supported.", /* EAI_ADDRFAMILY */ - "temporary failure in name resolution.", /* EAI_AGAIN */ - "invalid value for ai_flags.", /* EAI_BADFLAGS */ - "non-recoverable failure in name resolution.", /* EAI_FAIL */ - "ai_family not supported.", /* EAI_FAMILY */ - "memory allocation failure.", /* EAI_MEMORY */ - "no address associated with hostname.", /* EAI_NODATA */ - "hostname nor servname provided, or not known.",/* EAI_NONAME */ - "servname not supported for ai_socktype.", /* EAI_SERVICE */ - "ai_socktype not supported.", /* EAI_SOCKTYPE */ - "system error returned in errno.", /* EAI_SYSTEM */ - "invalid value for hints.", /* EAI_BADHINTS */ - "resolved protocol is unknown.", /* EAI_PROTOCOL */ - "unknown error.", /* EAI_MAX */ -}; - -#define GET_CANONNAME(ai, str) \ -if (pai->ai_flags & AI_CANONNAME) {\ - if (((ai)->ai_canonname = (char *)malloc(strlen(str) + 1)) != NULL) {\ - strcpy((ai)->ai_canonname, (str));\ - } else {\ - error = EAI_MEMORY;\ - goto free;\ - }\ -} - -#ifdef HAVE_SOCKADDR_SA_LEN -#define GET_AI(ai, gai_afd, addr, port) {\ - char *p;\ - if (((ai) = (struct addrinfo *)malloc(sizeof(struct addrinfo) +\ - ((gai_afd)->a_socklen)))\ - == NULL) goto free;\ - memcpy(ai, pai, sizeof(struct addrinfo));\ - (ai)->ai_addr = (struct sockaddr *)((ai) + 1);\ - memset((ai)->ai_addr, 0, (gai_afd)->a_socklen);\ - (ai)->ai_addr->sa_len = (ai)->ai_addrlen = (gai_afd)->a_socklen;\ - (ai)->ai_addr->sa_family = (ai)->ai_family = (gai_afd)->a_af;\ - ((struct sockinet *)(ai)->ai_addr)->si_port = port;\ - p = (char *)((ai)->ai_addr);\ - memcpy(p + (gai_afd)->a_off, (addr), (gai_afd)->a_addrlen);\ -} -#else -#define GET_AI(ai, gai_afd, addr, port) {\ - char *p;\ - if (((ai) = (struct addrinfo *)malloc(sizeof(struct addrinfo) +\ - ((gai_afd)->a_socklen)))\ - == NULL) goto free;\ - memcpy(ai, pai, sizeof(struct addrinfo));\ - (ai)->ai_addr = (struct sockaddr *)((ai) + 1);\ - memset((ai)->ai_addr, 0, (gai_afd)->a_socklen);\ - (ai)->ai_addrlen = (gai_afd)->a_socklen;\ - (ai)->ai_addr->sa_family = (ai)->ai_family = (gai_afd)->a_af;\ - ((struct sockinet *)(ai)->ai_addr)->si_port = port;\ - p = (char *)((ai)->ai_addr);\ - memcpy(p + (gai_afd)->a_off, (addr), (gai_afd)->a_addrlen);\ -} -#endif - -#define ERR(err) { error = (err); goto bad; } - -char * -gai_strerror(int ecode) -{ - if (ecode < 0 || ecode > EAI_MAX) - ecode = EAI_MAX; - return ai_errlist[ecode]; -} - -void -freeaddrinfo(struct addrinfo *ai) -{ - struct addrinfo *next; - - do { - next = ai->ai_next; - if (ai->ai_canonname) - free(ai->ai_canonname); - /* no need to free(ai->ai_addr) */ - free(ai); - } while ((ai = next) != NULL); -} - -static int -str_isnumber(const char *p) -{ - unsigned char *q = (unsigned char *)p; - while (*q) { - if (! isdigit(*q)) - return NO; - q++; - } - return YES; -} - -int -getaddrinfo(const char*hostname, const char*servname, - const struct addrinfo *hints, struct addrinfo **res) -{ - struct addrinfo sentinel; - struct addrinfo *top = NULL; - struct addrinfo *cur; - int i, error = 0; - char pton[PTON_MAX]; - struct addrinfo ai; - struct addrinfo *pai; - u_short port; - -#ifdef FAITH - static int firsttime = 1; - - if (firsttime) { - /* translator hack */ - { - char *q = getenv("GAI"); - if (q && inet_pton(AF_INET6, q, &faith_prefix) == 1) - translate = YES; - } - firsttime = 0; - } -#endif - - /* initialize file static vars */ - sentinel.ai_next = NULL; - cur = &sentinel; - pai = &ai; - pai->ai_flags = 0; - pai->ai_family = PF_UNSPEC; - pai->ai_socktype = GAI_ANY; - pai->ai_protocol = GAI_ANY; - pai->ai_addrlen = 0; - pai->ai_canonname = NULL; - pai->ai_addr = NULL; - pai->ai_next = NULL; - port = GAI_ANY; - - if (hostname == NULL && servname == NULL) - return EAI_NONAME; - if (hints) { - /* error check for hints */ - if (hints->ai_addrlen || hints->ai_canonname || - hints->ai_addr || hints->ai_next) - ERR(EAI_BADHINTS); /* xxx */ - if (hints->ai_flags & ~AI_MASK) - ERR(EAI_BADFLAGS); - switch (hints->ai_family) { - case PF_UNSPEC: - case PF_INET: -#ifdef ENABLE_IPV6 - case PF_INET6: -#endif - break; - default: - ERR(EAI_FAMILY); - } - memcpy(pai, hints, sizeof(*pai)); - switch (pai->ai_socktype) { - case GAI_ANY: - switch (pai->ai_protocol) { - case GAI_ANY: - break; - case IPPROTO_UDP: - pai->ai_socktype = SOCK_DGRAM; - break; - case IPPROTO_TCP: - pai->ai_socktype = SOCK_STREAM; - break; - default: -#ifdef _XBOX - pai->ai_socktype = SOCK_STREAM; -#else - pai->ai_socktype = SOCK_RAW; -#endif //_XBOX - break; - } - break; -#ifndef _XBOX - case SOCK_RAW: - break; -#endif //_XBOX - case SOCK_DGRAM: - if (pai->ai_protocol != IPPROTO_UDP && - pai->ai_protocol != GAI_ANY) - ERR(EAI_BADHINTS); /*xxx*/ - pai->ai_protocol = IPPROTO_UDP; - break; - case SOCK_STREAM: - if (pai->ai_protocol != IPPROTO_TCP && - pai->ai_protocol != GAI_ANY) - ERR(EAI_BADHINTS); /*xxx*/ - pai->ai_protocol = IPPROTO_TCP; - break; - default: - ERR(EAI_SOCKTYPE); - /* unreachable */ - } - } - - /* - * service port - */ - if (servname) { - if (str_isnumber(servname)) { - if (pai->ai_socktype == GAI_ANY) { - /* caller accept *GAI_ANY* socktype */ - pai->ai_socktype = SOCK_DGRAM; - pai->ai_protocol = IPPROTO_UDP; - } - port = htons((u_short)atoi(servname)); - } else { - struct servent *sp; - char *proto; - - proto = NULL; - switch (pai->ai_socktype) { - case GAI_ANY: - proto = NULL; - break; - case SOCK_DGRAM: - proto = "udp"; - break; - case SOCK_STREAM: - proto = "tcp"; - break; - default: - fprintf(stderr, "panic!\n"); - break; - } - if ((sp = getservbyname(servname, proto)) == NULL) - ERR(EAI_SERVICE); - port = sp->s_port; - if (pai->ai_socktype == GAI_ANY) { - if (strcmp(sp->s_proto, "udp") == 0) { - pai->ai_socktype = SOCK_DGRAM; - pai->ai_protocol = IPPROTO_UDP; - } else if (strcmp(sp->s_proto, "tcp") == 0) { - pai->ai_socktype = SOCK_STREAM; - pai->ai_protocol = IPPROTO_TCP; - } else - ERR(EAI_PROTOCOL); /*xxx*/ - } - } - } - - /* - * hostname == NULL. - * passive socket -> anyaddr (0.0.0.0 or ::) - * non-passive socket -> localhost (127.0.0.1 or ::1) - */ - if (hostname == NULL) { - struct gai_afd *gai_afd; - - for (gai_afd = &gai_afdl[0]; gai_afd->a_af; gai_afd++) { - if (!(pai->ai_family == PF_UNSPEC - || pai->ai_family == gai_afd->a_af)) { - continue; - } - - if (pai->ai_flags & AI_PASSIVE) { - GET_AI(cur->ai_next, gai_afd, gai_afd->a_addrany, port); - /* xxx meaningless? - * GET_CANONNAME(cur->ai_next, "anyaddr"); - */ - } else { - GET_AI(cur->ai_next, gai_afd, gai_afd->a_loopback, - port); - /* xxx meaningless? - * GET_CANONNAME(cur->ai_next, "localhost"); - */ - } - cur = cur->ai_next; - } - top = sentinel.ai_next; - if (top) - goto good; - else - ERR(EAI_FAMILY); - } - - /* hostname as numeric name */ - for (i = 0; gai_afdl[i].a_af; i++) { - if (inet_pton(gai_afdl[i].a_af, hostname, pton)) { - u_long v4a; -#ifdef ENABLE_IPV6 - u_char pfx; -#endif - - switch (gai_afdl[i].a_af) { - case AF_INET: - v4a = ((struct in_addr *)pton)->s_addr; - v4a = ntohl(v4a); - if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a)) - pai->ai_flags &= ~AI_CANONNAME; - v4a >>= IN_CLASSA_NSHIFT; - if (v4a == 0 || v4a == IN_LOOPBACKNET) - pai->ai_flags &= ~AI_CANONNAME; - break; -#ifdef ENABLE_IPV6 - case AF_INET6: - pfx = ((struct in6_addr *)pton)->s6_addr8[0]; - if (pfx == 0 || pfx == 0xfe || pfx == 0xff) - pai->ai_flags &= ~AI_CANONNAME; - break; -#endif - } - - if (pai->ai_family == gai_afdl[i].a_af || - pai->ai_family == PF_UNSPEC) { - if (! (pai->ai_flags & AI_CANONNAME)) { - GET_AI(top, &gai_afdl[i], pton, port); - goto good; - } - /* - * if AI_CANONNAME and if reverse lookup - * fail, return ai anyway to pacify - * calling application. - * - * XXX getaddrinfo() is a name->address - * translation function, and it looks strange - * that we do addr->name translation here. - */ - get_name(pton, &gai_afdl[i], &top, pton, pai, port); - goto good; - } else - ERR(EAI_FAMILY); /*xxx*/ - } - } - - if (pai->ai_flags & AI_NUMERICHOST) - ERR(EAI_NONAME); - - /* hostname as alphabetical name */ - error = get_addr(hostname, pai->ai_family, &top, pai, port); - if (error == 0) { - if (top) { - good: - *res = top; - return SUCCESS; - } else - error = EAI_FAIL; - } - free: - if (top) - freeaddrinfo(top); - bad: - *res = NULL; - return error; -} - -static int -get_name(addr, gai_afd, res, numaddr, pai, port0) - const char *addr; - struct gai_afd *gai_afd; - struct addrinfo **res; - char *numaddr; - struct addrinfo *pai; - int port0; -{ - u_short port = port0 & 0xffff; - struct hostent *hp; - struct addrinfo *cur; - int error = 0; -#ifdef ENABLE_IPV6 - int h_error; -#endif - -#ifdef ENABLE_IPV6 - hp = getipnodebyaddr(addr, gai_afd->a_addrlen, gai_afd->a_af, &h_error); -#else - hp = gethostbyaddr(addr, gai_afd->a_addrlen, AF_INET); -#endif - if (hp && hp->h_name && hp->h_name[0] && hp->h_addr_list[0]) { - GET_AI(cur, gai_afd, hp->h_addr_list[0], port); - GET_CANONNAME(cur, hp->h_name); - } else - GET_AI(cur, gai_afd, numaddr, port); - -#ifdef ENABLE_IPV6 - if (hp) - freehostent(hp); -#endif - *res = cur; - return SUCCESS; - free: - if (cur) - freeaddrinfo(cur); -#ifdef ENABLE_IPV6 - if (hp) - freehostent(hp); -#endif - /* bad: */ - *res = NULL; - return error; -} - -static int -get_addr(hostname, af, res, pai, port0) - const char *hostname; - int af; - struct addrinfo **res; - struct addrinfo *pai; - int port0; -{ - u_short port = port0 & 0xffff; - struct addrinfo sentinel; - struct hostent *hp; - struct addrinfo *top, *cur; - struct gai_afd *gai_afd; - int i, error = 0, h_error; - char *ap; - - top = NULL; - sentinel.ai_next = NULL; - cur = &sentinel; -#ifdef ENABLE_IPV6 - if (af == AF_UNSPEC) { - hp = getipnodebyname(hostname, AF_INET6, - AI_ADDRCONFIG|AI_ALL|AI_V4MAPPED, &h_error); - } else - hp = getipnodebyname(hostname, af, AI_ADDRCONFIG, &h_error); -#else - hp = gethostbyname(hostname); - h_error = h_errno; -#endif - if (hp == NULL) { - switch (h_error) { - case HOST_NOT_FOUND: - case NO_DATA: - error = EAI_NODATA; - break; - case TRY_AGAIN: - error = EAI_AGAIN; - break; - case NO_RECOVERY: - default: - error = EAI_FAIL; - break; - } - goto free; - } - - if ((hp->h_name == NULL) || (hp->h_name[0] == 0) || - (hp->h_addr_list[0] == NULL)) { - error = EAI_FAIL; - goto free; - } - - for (i = 0; (ap = hp->h_addr_list[i]) != NULL; i++) { - switch (af) { -#ifdef ENABLE_IPV6 - case AF_INET6: - gai_afd = &gai_afdl[N_INET6]; - break; -#endif -#ifndef ENABLE_IPV6 - default: /* AF_UNSPEC */ -#endif - case AF_INET: - gai_afd = &gai_afdl[N_INET]; - break; -#ifdef ENABLE_IPV6 - default: /* AF_UNSPEC */ - if (IN6_IS_ADDR_V4MAPPED((struct in6_addr *)ap)) { - ap += sizeof(struct in6_addr) - - sizeof(struct in_addr); - gai_afd = &gai_afdl[N_INET]; - } else - gai_afd = &gai_afdl[N_INET6]; - break; -#endif - } -#ifdef FAITH - if (translate && gai_afd->a_af == AF_INET) { - struct in6_addr *in6; - - GET_AI(cur->ai_next, &gai_afdl[N_INET6], ap, port); - in6 = &((struct sockaddr_in6 *)cur->ai_next->ai_addr)->sin6_addr; - memcpy(&in6->s6_addr32[0], &faith_prefix, - sizeof(struct in6_addr) - sizeof(struct in_addr)); - memcpy(&in6->s6_addr32[3], ap, sizeof(struct in_addr)); - } else -#endif /* FAITH */ - GET_AI(cur->ai_next, gai_afd, ap, port); - if (cur == &sentinel) { - top = cur->ai_next; - GET_CANONNAME(top, hp->h_name); - } - cur = cur->ai_next; - } -#ifdef ENABLE_IPV6 - freehostent(hp); -#endif - *res = top; - return SUCCESS; - free: - if (top) - freeaddrinfo(top); -#ifdef ENABLE_IPV6 - if (hp) - freehostent(hp); -#endif -/* bad: */ - *res = NULL; - return error; -} \ No newline at end of file diff --git a/lib/xbox/getnameinfo.c b/lib/xbox/getnameinfo.c deleted file mode 100644 index cca252c0..00000000 --- a/lib/xbox/getnameinfo.c +++ /dev/null @@ -1,243 +0,0 @@ -/* - * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the project nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -/* - * Issues to be discussed: - * - Thread safe-ness must be checked - * - Return values. There seems to be no standard for return value (RFC2553) - * but INRIA implementation returns EAI_xxx defined for getaddrinfo(). - * - RFC2553 says that we should raise error on short buffer. X/Open says - * we need to truncate the result. We obey RFC2553 (and X/Open should be - * modified). - */ - -#ifdef _XBOX -#include -#include -#else -#include -#endif -#include -#include "addrinfo.h" -#include "emu_socket.h" -#include -#include -#include -#include -#include -#include -#include - -#ifdef _XBOX -extern struct mphostent* __stdcall sckemu_gethostbyname(const char* name); -#define gethostbyname(name) sckemu_gethostbyname(name) - -extern struct mphostent* __stdcall sckemu_gethostbyaddr(const char* addr, int len, int type); -#define gethostbyaddr(addr, len, type) sckemu_gethostbyaddr(addr, len, type) -#endif - -extern struct servent* __stdcall sckemu_getservbyname(const char* name, const char* proto); -#define getservbyname(name, proto) sckemu_getservbyname(name, proto) - -extern struct servent* __stdcall sckemu_getservbyport(int port, const char* proto); -#define getservbyport(port, proto) sckemu_getservbyport(port, proto) - -#define hostent mphostent - -#ifndef HAVE_U_INT32_T -#include "bittypes.h" -#endif - -#ifdef NEED_ADDRINFO_H -#include "addrinfo.h" -#endif - -#define SUCCESS 0 -#define YES 1 -#define NO 0 - -static struct gni_afd { - int a_af; - int a_addrlen; - int a_socklen; - int a_off; -} gni_afdl [] = { -#ifdef ENABLE_IPV6 - {PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6), - offsetof(struct sockaddr_in6, sin6_addr)}, -#endif - {PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in), - offsetof(struct sockaddr_in, sin_addr)}, - {0, 0, 0}, -}; - -struct gni_sockinet { - u_char si_len; - u_char si_family; - u_short si_port; -}; - -#define ENI_NOSOCKET 0 -#define ENI_NOSERVNAME 1 -#define ENI_NOHOSTNAME 2 -#define ENI_MEMORY 3 -#define ENI_SYSTEM 4 -#define ENI_FAMILY 5 -#define ENI_SALEN 6 - -/* forward declaration to make gcc happy */ -int getnameinfo (const struct sockaddr *, size_t, char *, size_t, char *, size_t, int); - -int -getnameinfo(sa, salen, host, hostlen, serv, servlen, flags) - const struct sockaddr *sa; - size_t salen; - char *host; - size_t hostlen; - char *serv; - size_t servlen; - int flags; -{ - struct gni_afd *gni_afd; - struct servent *sp; - struct hostent *hp; - u_short port; - int family, len, i; - char *addr, *p; - u_long v4a; -#ifdef ENABLE_IPV6 - u_char pfx; -#endif - int h_error; - char numserv[512]; - char numaddr[512]; - - if (sa == NULL) - return ENI_NOSOCKET; - -#ifdef HAVE_SOCKADDR_SA_LEN - len = sa->sa_len; - if (len != salen) return ENI_SALEN; -#else - len = salen; -#endif - - family = sa->sa_family; - for (i = 0; gni_afdl[i].a_af; i++) - if (gni_afdl[i].a_af == family) { - gni_afd = &gni_afdl[i]; - goto found; - } - return ENI_FAMILY; - - found: - if (len != gni_afd->a_socklen) return ENI_SALEN; - - port = ((struct gni_sockinet *)sa)->si_port; /* network byte order */ - addr = (char *)sa + gni_afd->a_off; - - if (serv == NULL || servlen == 0) { - /* what we should do? */ - } else if (flags & NI_NUMERICSERV) { - sprintf(numserv, "%d", ntohs(port)); - if (strlen(numserv) > servlen) - return ENI_MEMORY; - strcpy(serv, numserv); - } else { - sp = getservbyport(port, (flags & NI_DGRAM) ? "udp" : "tcp"); - if (sp) { - if (strlen(sp->s_name) > servlen) - return ENI_MEMORY; - strcpy(serv, sp->s_name); - } else - return ENI_NOSERVNAME; - } - - switch (sa->sa_family) { - case AF_INET: - v4a = ((struct sockaddr_in *)sa)->sin_addr.s_addr; - if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a)) - flags |= NI_NUMERICHOST; - v4a >>= IN_CLASSA_NSHIFT; - if (v4a == 0 || v4a == IN_LOOPBACKNET) - flags |= NI_NUMERICHOST; - break; -#ifdef ENABLE_IPV6 - case AF_INET6: - pfx = ((struct sockaddr_in6 *)sa)->sin6_addr.s6_addr8[0]; - if (pfx == 0 || pfx == 0xfe || pfx == 0xff) - flags |= NI_NUMERICHOST; - break; -#endif - } - if (host == NULL || hostlen == 0) { - /* what should we do? */ - } else if (flags & NI_NUMERICHOST) { - if (inet_ntop(gni_afd->a_af, addr, numaddr, sizeof(numaddr)) - == NULL) - return ENI_SYSTEM; - if (strlen(numaddr) > hostlen) - return ENI_MEMORY; - strcpy(host, numaddr); - } else { -#ifdef ENABLE_IPV6 - hp = getipnodebyaddr(addr, gni_afd->a_addrlen, gni_afd->a_af, &h_error); -#else - hp = gethostbyaddr(addr, gni_afd->a_addrlen, gni_afd->a_af); - h_error = h_errno; -#endif - - if (hp) { - if (flags & NI_NOFQDN) { - p = strchr(hp->h_name, '.'); - if (p) *p = '\0'; - } - if (strlen(hp->h_name) > hostlen) { -#ifdef ENABLE_IPV6 - freehostent(hp); -#endif - return ENI_MEMORY; - } - strcpy(host, hp->h_name); -#ifdef ENABLE_IPV6 - freehostent(hp); -#endif - } else { - if (flags & NI_NAMEREQD) - return ENI_NOHOSTNAME; - if (inet_ntop(gni_afd->a_af, addr, numaddr, sizeof(numaddr)) - == NULL) - return ENI_NOHOSTNAME; - if (strlen(numaddr) > hostlen) - return ENI_MEMORY; - strcpy(host, numaddr); - } - } - return SUCCESS; -} diff --git a/lib/xbox/inet_aton.c b/lib/xbox/inet_aton.c deleted file mode 100644 index 59fce171..00000000 --- a/lib/xbox/inet_aton.c +++ /dev/null @@ -1,148 +0,0 @@ -/*- - * Copyright (c) 2001 Charles Mott - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#ifdef _XBOX -#include -#include -#else -#include -#endif -typedef unsigned long in_addr_t; -#include -#include -#include - -#ifdef __cplusplus -extern "C" { -#endif - -#ifndef INADDR_NONE -#define INADDR_NONE 0xffffffff -#endif - -int -inet_aton(const char *cp, struct in_addr *addr) -{ - u_long parts[4]; - in_addr_t val; - const char *c; - char *endptr; - int gotend, n; - - c = (const char *)cp; - n = 0; - - /* - * Run through the string, grabbing numbers until - * the end of the string, or some error - */ - gotend = 0; - while (!gotend) { - unsigned long l; - - l = strtoul(c, &endptr, 0); - - if (l == ULONG_MAX || (l == 0 && endptr == c)) - return (0); - - val = (in_addr_t)l; - - /* - * If the whole string is invalid, endptr will equal - * c.. this way we can make sure someone hasn't - * gone '.12' or something which would get past - * the next check. - */ - if (endptr == c) - return (0); - parts[n] = val; - c = endptr; - - /* Check the next character past the previous number's end */ - switch (*c) { - case '.' : - - /* Make sure we only do 3 dots .. */ - if (n == 3) /* Whoops. Quit. */ - return (0); - n++; - c++; - break; - - case '\0': - gotend = 1; - break; - - default: - if (isspace((unsigned char)*c)) { - gotend = 1; - break; - } else { - - /* Invalid character, then fail. */ - return (0); - } - } - - } - - /* Concoct the address according to the number of parts specified. */ - switch (n) { - case 0: /* a -- 32 bits */ - - /* - * Nothing is necessary here. Overflow checking was - * already done in strtoul(). - */ - break; - case 1: /* a.b -- 8.24 bits */ - if (val > 0xffffff || parts[0] > 0xff) - return (0); - val |= parts[0] << 24; - break; - - case 2: /* a.b.c -- 8.8.16 bits */ - if (val > 0xffff || parts[0] > 0xff || parts[1] > 0xff) - return (0); - val |= (parts[0] << 24) | (parts[1] << 16); - break; - - case 3: /* a.b.c.d -- 8.8.8.8 bits */ - if (val > 0xff || parts[0] > 0xff || parts[1] > 0xff || - parts[2] > 0xff) - return (0); - val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8); - break; - } - - if (addr != NULL) - addr->s_addr = htonl(val); - return (1); -} - -#ifdef __cplusplus -} -#endif diff --git a/lib/xbox/inet_ntop.c b/lib/xbox/inet_ntop.c deleted file mode 100644 index fbe5d231..00000000 --- a/lib/xbox/inet_ntop.c +++ /dev/null @@ -1,200 +0,0 @@ -/* - * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") - * Copyright (c) 1996-1999 by Internet Software Consortium. - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT - * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#ifdef _XBOX -#include -#include -#else -#include -#endif -typedef int socklen_t; -#define snprintf _snprintf -#include -#include - -#ifdef __cplusplus -extern "C" { -#endif - -#ifndef IN6ADDRSZ -#define IN6ADDRSZ 16 /* IPv6 T_AAAA */ -#endif - -#ifndef INT16SZ -#define INT16SZ 2 /* word size */ -#endif - -#ifndef HAVE_STRLCPY -#define strlcpy(d,s,l) (strncpy(d,s,l), (d)[(l)-1] = '\0') -#endif - -/*% - * WARNING: Don't even consider trying to compile this on a system where - * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX. - */ - -static const char *inet_ntop4(const u_char *src, char *dst, socklen_t size); -static const char *inet_ntop6(const u_char *src, char *dst, socklen_t size); - -/* char * - * inet_ntop(af, src, dst, size) - * convert a network format address to presentation format. - * return: - * pointer to presentation format address (`dst'), or NULL (see errno). - * author: - * Paul Vixie, 1996. - */ -const char * -inet_ntop(int af, const void * src, char * dst, - socklen_t size) -{ - switch (af) { - case AF_INET: - return (inet_ntop4((u_char*)src, dst, size)); - default: - return (NULL); - } - /* NOTREACHED */ -} - -/* const char * - * inet_ntop4(src, dst, size) - * format an IPv4 address - * return: - * `dst' (as a const) - * notes: - * (1) uses no statics - * (2) takes a u_char* not an in_addr as input - * author: - * Paul Vixie, 1996. - */ -static const char * -inet_ntop4(const u_char *src, char *dst, socklen_t size) -{ - static const char fmt[] = "%u.%u.%u.%u"; - char tmp[sizeof "255.255.255.255"]; - int l; - - l = snprintf(tmp, sizeof(tmp), fmt, src[0], src[1], src[2], src[3]); - if (l <= 0 || (socklen_t) l >= size) { - return (NULL); - } - strlcpy(dst, tmp, size); - return (dst); -} - -/* const char * - * inet_ntop6(src, dst, size) - * convert IPv6 binary address into presentation (printable) format - * author: - * Paul Vixie, 1996. - */ -static const char * -inet_ntop6(const u_char *src, char *dst, socklen_t size) -{ - /* - * Note that int32_t and int16_t need only be "at least" large enough - * to contain a value of the specified size. On some systems, like - * Crays, there is no such thing as an integer variable with 16 bits. - * Keep this in mind if you think this function should have been coded - * to use pointer overlays. All the world's not a VAX. - */ - char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"], *tp; - struct { int base, len; } best, cur; -#define NS_IN6ADDRSZ 16 -#define NS_INT16SZ 2 - u_int words[NS_IN6ADDRSZ / NS_INT16SZ]; - int i; - - /* - * Preprocess: - * Copy the input (bytewise) array into a wordwise array. - * Find the longest run of 0x00's in src[] for :: shorthanding. - */ - memset(words, '\0', sizeof words); - for (i = 0; i < NS_IN6ADDRSZ; i++) - words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3)); - best.base = -1; - best.len = 0; - cur.base = -1; - cur.len = 0; - for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) { - if (words[i] == 0) { - if (cur.base == -1) - cur.base = i, cur.len = 1; - else - cur.len++; - } else { - if (cur.base != -1) { - if (best.base == -1 || cur.len > best.len) - best = cur; - cur.base = -1; - } - } - } - if (cur.base != -1) { - if (best.base == -1 || cur.len > best.len) - best = cur; - } - if (best.base != -1 && best.len < 2) - best.base = -1; - - /* - * Format the result. - */ - tp = tmp; - for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) { - /* Are we inside the best run of 0x00's? */ - if (best.base != -1 && i >= best.base && - i < (best.base + best.len)) { - if (i == best.base) - *tp++ = ':'; - continue; - } - /* Are we following an initial run of 0x00s or any real hex? */ - if (i != 0) - *tp++ = ':'; - /* Is this address an encapsulated IPv4? */ - if (i == 6 && best.base == 0 && (best.len == 6 || - (best.len == 7 && words[7] != 0x0001) || - (best.len == 5 && words[5] == 0xffff))) { - if (!inet_ntop4(src+12, tp, sizeof tmp - (tp - tmp))) - return (NULL); - tp += strlen(tp); - break; - } - tp += sprintf(tp, "%x", words[i]); - } - /* Was it a trailing run of 0x00's? */ - if (best.base != -1 && (best.base + best.len) == - (NS_IN6ADDRSZ / NS_INT16SZ)) - *tp++ = ':'; - *tp++ = '\0'; - - /* - * Check for overflow, copy, and we're done. - */ - if ((socklen_t)(tp - tmp) > size) { - return (NULL); - } - strcpy(dst, tmp); - return (dst); -} - -#ifdef __cplusplus -} -#endif diff --git a/lib/xbox/inet_pton.c b/lib/xbox/inet_pton.c deleted file mode 100644 index 3f7e7390..00000000 --- a/lib/xbox/inet_pton.c +++ /dev/null @@ -1,234 +0,0 @@ -/* - * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") - * Copyright (c) 1996,1999 by Internet Software Consortium. - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT - * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#ifdef _XBOX -#include -#include -#else -#include -#endif -#include - -#ifdef __cplusplus -extern "C" { -#endif - -#ifdef __STDC__ -#define __weak_reference(sym,alias) \ - __asm__(".weak " #alias); \ - __asm__(".equ " #alias ", " #sym) -#else -#define __weak_reference(sym,alias) \ - __asm__(".weak alias"); \ - __asm__(".equ alias, sym") -#endif /* __STDC__ */ - -/*% - * WARNING: Don't even consider trying to compile this on a system where - * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX. - */ - -static int inet_pton4(const char *src, u_char *dst); -static int inet_pton6(const char *src, u_char *dst); - -/* int - * inet_pton(af, src, dst) - * convert from presentation format (which usually means ASCII printable) - * to network format (which is usually some kind of binary format). - * return: - * 1 if the address was valid for the specified address family - * 0 if the address wasn't valid (`dst' is untouched in this case) - * -1 if some other error occurred (`dst' is untouched in this case, too) - * author: - * Paul Vixie, 1996. - */ -int -inet_pton(int af, const char * src, void * dst) -{ - switch (af) { - case AF_INET: - return (inet_pton4(src, (u_char*)dst)); - default: - return (-1); - } - /* NOTREACHED */ -} - -/* int - * inet_pton4(src, dst) - * like inet_aton() but without all the hexadecimal and shorthand. - * return: - * 1 if `src' is a valid dotted quad, else 0. - * notice: - * does not touch `dst' unless it's returning 1. - * author: - * Paul Vixie, 1996. - */ -static int -inet_pton4(const char *src, u_char *dst) -{ - static const char digits[] = "0123456789"; - int saw_digit, octets, ch; -#define NS_INADDRSZ 4 - u_char tmp[NS_INADDRSZ], *tp; - - saw_digit = 0; - octets = 0; - *(tp = tmp) = 0; - while ((ch = *src++) != '\0') { - const char *pch; - - if ((pch = strchr(digits, ch)) != NULL) { - u_int c_new = *tp * 10 + (pch - digits); - - if (saw_digit && *tp == 0) - return (0); - if (c_new > 255) - return (0); - *tp = c_new; - if (!saw_digit) { - if (++octets > 4) - return (0); - saw_digit = 1; - } - } else if (ch == '.' && saw_digit) { - if (octets == 4) - return (0); - *++tp = 0; - saw_digit = 0; - } else - return (0); - } - if (octets < 4) - return (0); - memcpy(dst, tmp, NS_INADDRSZ); - return (1); -} - -/* int - * inet_pton6(src, dst) - * convert presentation level address to network order binary form. - * return: - * 1 if `src' is a valid [RFC1884 2.2] address, else 0. - * notice: - * (1) does not touch `dst' unless it's returning 1. - * (2) :: in a full address is silently ignored. - * credit: - * inspired by Mark Andrews. - * author: - * Paul Vixie, 1996. - */ -static int -inet_pton6(const char *src, u_char *dst) -{ - static const char xdigits_l[] = "0123456789abcdef", - xdigits_u[] = "0123456789ABCDEF"; -#define NS_IN6ADDRSZ 16 -#define NS_INT16SZ 2 - u_char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp; - const char *xdigits, *curtok; - int ch, seen_xdigits; - u_int val; - - memset((tp = tmp), '\0', NS_IN6ADDRSZ); - endp = tp + NS_IN6ADDRSZ; - colonp = NULL; - /* Leading :: requires some special handling. */ - if (*src == ':') - if (*++src != ':') - return (0); - curtok = src; - seen_xdigits = 0; - val = 0; - while ((ch = *src++) != '\0') { - const char *pch; - - if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL) - pch = strchr((xdigits = xdigits_u), ch); - if (pch != NULL) { - val <<= 4; - val |= (pch - xdigits); - if (++seen_xdigits > 4) - return (0); - continue; - } - if (ch == ':') { - curtok = src; - if (!seen_xdigits) { - if (colonp) - return (0); - colonp = tp; - continue; - } else if (*src == '\0') { - return (0); - } - if (tp + NS_INT16SZ > endp) - return (0); - *tp++ = (u_char) (val >> 8) & 0xff; - *tp++ = (u_char) val & 0xff; - seen_xdigits = 0; - val = 0; - continue; - } - if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) && - inet_pton4(curtok, tp) > 0) { - tp += NS_INADDRSZ; - seen_xdigits = 0; - break; /*%< '\\0' was seen by inet_pton4(). */ - } - return (0); - } - if (seen_xdigits) { - if (tp + NS_INT16SZ > endp) - return (0); - *tp++ = (u_char) (val >> 8) & 0xff; - *tp++ = (u_char) val & 0xff; - } - if (colonp != NULL) { - /* - * Since some memmove()'s erroneously fail to handle - * overlapping regions, we'll do the shift by hand. - */ - const int n = tp - colonp; - int i; - - if (tp == endp) - return (0); - for (i = 1; i <= n; i++) { - endp[- i] = colonp[n - i]; - colonp[n - i] = 0; - } - tp = endp; - } - if (tp != endp) - return (0); - memcpy(dst, tmp, NS_IN6ADDRSZ); - return (1); -} - -/* - * Weak aliases for applications that use certain private entry points, - * and fail to include . - */ -#ifndef _WIN32 -#undef inet_pton -__weak_reference(__inet_pton, inet_pton); -#endif - -#ifdef __cplusplus -} -#endif diff --git a/lib/xbox/sockstorage.h b/lib/xbox/sockstorage.h deleted file mode 100644 index e7333289..00000000 --- a/lib/xbox/sockstorage.h +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright (C) 1999 WIDE Project. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the project nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ -struct sockaddr_storage { -#ifdef HAVE_SOCKADDR_SA_LEN - u_int8_t ss_len; - u_int8_t ss_family; - u_int8_t fill[126]; -#else - u_int8_t ss_family; - u_int8_t fill[127]; -#endif /* HAVE_SOCKADDR_SA_LEN */ -}; diff --git a/lib/xbox/xb_emu_socket.h b/lib/xbox/xb_emu_socket.h deleted file mode 100644 index 82c799db..00000000 --- a/lib/xbox/xb_emu_socket.h +++ /dev/null @@ -1,86 +0,0 @@ -#ifndef _EMU_SOCKET_H -#define _EMU_SOCKET_H - -/* - * Copyright (C) 2005-2013 Team XBMC - * http://xbmc.org - * - * This Program 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 Foundation; either version 2, or (at your option) - * any later version. - * - * This Program 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 XBMC; see the file COPYING. If not, see - * . - * - */ - -#include "emu_socket.h" -#include "addrinfo.h" -#include "bittypes.h" -#include "sockstorage.h" - -// OS support start -typedef int socklen_t; - -#define POLLIN 0x0001 /* There is data to read */ -#define POLLPRI 0x0002 /* There is urgent data to read */ -#define POLLOUT 0x0004 /* Writing now will not block */ -#define POLLERR 0x0008 /* Error condition */ -#define POLLHUP 0x0010 /* Hung up */ - -#ifndef XBOX_PLATFORM -#define ssize_t __int64 /* MSVC 2003 yells about this one. */ -#endif -// OS support end - -#ifdef __cplusplus -extern "C" -{ -#endif - - struct mphostent* __stdcall sckemu_gethostbyname(const char* name); -//char* __stdcall inet_ntoa(in_addr in); - int __stdcall sckemu_connect(int s, const struct sockaddr FAR *name, int namelen); - int __stdcall sckemu_send(int s, const char FAR *buf, int len, int flags); - int __stdcall sckemu_socket(int af, int type, int protocol); - int __stdcall sckemu_bind(int s, const struct sockaddr FAR * name, int namelen); - int __stdcall sckemu_closesocket(int s); - int __stdcall sckemu_getsockopt(int s, int level, int optname, char FAR * optval, int FAR * optlen); - int __stdcall sckemu_ioctlsocket(int s, long cmd, DWORD FAR * argp); - int __stdcall sckemu_recv(int s, char FAR * buf, int len, int flags); - int __stdcall sckemu_select(int nfds, fd_set FAR * readfds, fd_set FAR * writefds, fd_set FAR *exceptfds, const struct timeval FAR * timeout); - int __stdcall sckemu_sendto(int s, const char FAR * buf, int len, int flags, const struct sockaddr FAR * to, int tolen); - int __stdcall sckemu_setsockopt(int s, int level, int optname, const char FAR * optval, int optlen); - int __stdcall sckemu_WSAFDIsSet(int fd, fd_set* set); - - int __stdcall sckemu_accept(int s, struct sockaddr FAR * addr, OUT int FAR * addrlen); - int __stdcall sckemu_gethostname(char* name, int namelen); - int __stdcall sckemu_getsockname(int s, struct sockaddr* name, int* namelen); - int __stdcall sckemu_listen(int s, int backlog); - u_short __stdcall sckemu_ntohs(u_short netshort); - int __stdcall sckemu_recvfrom(int s, char* buf, int len, int flags, struct sockaddr* from, int* fromlen); - int __stdcall sckemu_shutdown(int s, int how); - char* __stdcall sckemu_ntoa(struct in_addr in); - - struct servent* __stdcall sckemu_getservbyname(const char* name,const char* proto); - struct protoent* __stdcall sckemu_getprotobyname(const char* name); - int __stdcall sckemu_getpeername(int s, struct sockaddr FAR *name, int FAR *namelen); - struct servent* __stdcall sckemu_getservbyport(int port, const char* proto); - struct mphostent* __stdcall sckemu_gethostbyaddr(const char* addr, int len, int type); - - int __stdcall sckemu_getaddrinfo(const char* nodename, const char* servname, const struct addrinfo* hints, struct addrinfo** res); - int __stdcall sckemu_getnameinfo(const struct sockaddr *sa, size_t salen, char *host, size_t hostlen, char *serv, size_t servlen, int flags); - void __stdcall sckemu_freeaddrinfo(struct addrinfo *ai); - -#ifdef __cplusplus -} -#endif - -#endif // _EMU_SOCKET_H \ No newline at end of file From 2cbae8a4f72be1a2a3779b8ad1cea3a42c8792a6 Mon Sep 17 00:00:00 2001 From: Wolf3s Date: Thu, 28 Dec 2023 09:41:27 -0300 Subject: [PATCH 22/23] Remove stdint.h from compat.h Add missing macros in config headers --- include/xbox 360/config.h | 9 +++++++++ include/xbox/config.h | 9 +++++++++ lib/compat.h | 5 ++--- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/include/xbox 360/config.h b/include/xbox 360/config.h index d70f970c..711aed81 100644 --- a/include/xbox 360/config.h +++ b/include/xbox 360/config.h @@ -14,6 +14,9 @@ /* Define to 1 if you have the header file. */ #define HAVE_FCNTL_H 1 +/* Define to 1 if you have the header file. */ +/* #undef HAVE_SYS_FCNTL_H 1 */ + /* Define to 1 if you have the header file. */ /* #define HAVE_GSSAPI_GSSAPI_H 1 */ @@ -83,9 +86,15 @@ /* Define to 1 if you have the header file. */ #define HAVE_TIME_H 1 +/* Define to 1 if you have the header file. */ +/* #undef HAVE_SYS_TIME_H 1 */ + /* Define to 1 if you have the header file. */ /* #define HAVE_UNISTD_H 1 */ +/* Define to 1 if you have the header file. */ +/* #undef HAVE_SYS_UNISTD_H 1 */ + /* Define to the sub-directory where libtool stores uninstalled libraries. */ #define LT_OBJDIR ".libs/" diff --git a/include/xbox/config.h b/include/xbox/config.h index 2c523a47..9d430efb 100644 --- a/include/xbox/config.h +++ b/include/xbox/config.h @@ -14,6 +14,9 @@ /* Define to 1 if you have the header file. */ #define HAVE_FCNTL_H 1 +/* Define to 1 if you have the header file. */ +/* #undef HAVE_SYS_FCNTL_H 1 */ + /* Define to 1 if you have the header file. */ /* #define HAVE_GSSAPI_GSSAPI_H 1 */ @@ -83,9 +86,15 @@ /* Define to 1 if you have the header file. */ #define HAVE_TIME_H 1 +/* Define to 1 if you have the header file. */ +/* #undef HAVE_SYS_TIME_H 1 */ + /* Define to 1 if you have the header file. */ /* #define HAVE_UNISTD_H 1 */ +/* Define to 1 if you have the header file. */ +/* #undef HAVE_SYS_UNISTD_H 1 */ + /* Define to the sub-directory where libtool stores uninstalled libraries. */ #define LT_OBJDIR ".libs/" diff --git a/lib/compat.h b/lib/compat.h index f7ee51d1..2869d18f 100644 --- a/lib/compat.h +++ b/lib/compat.h @@ -39,9 +39,8 @@ typedef unsigned int uint32_t; typedef unsigned long long uint64_t; typedef unsigned int uint_t; typedef unsigned int uintptr_t; -#else -#include /* XBOX 360 */ #endif + #include #ifndef ENETRESET @@ -149,7 +148,7 @@ void smb2_freeaddrinfo(struct addrinfo *res); /* just pretend they are the same so we compile */ #define sockaddr_in6 sockaddr_in -#endif //_XBOX +#endif /* _XBOX */ #ifdef PICO_PLATFORM From fd85cf3d61dd55284a531121df6a97b229144036 Mon Sep 17 00:00:00 2001 From: Wolf3s Date: Mon, 1 Jan 2024 17:36:13 -0300 Subject: [PATCH 23/23] Code format --- lib/aes.c | 2 +- lib/errors.c | 16 +++++++--------- lib/init.c | 2 +- lib/libsmb2.c | 4 ++-- lib/socket.c | 32 +++++++++++++++----------------- 5 files changed, 26 insertions(+), 30 deletions(-) diff --git a/lib/aes.c b/lib/aes.c index 96e7350e..ebe841a1 100644 --- a/lib/aes.c +++ b/lib/aes.c @@ -459,7 +459,7 @@ void AES128_ECB_decrypt(uint8_t* input, const uint8_t* key, uint8_t *output) { // The array that stores the round keys. uint8_t roundKey[176]; - + // Copy input to output, and work in-memory on output BlockCopy(output, input); diff --git a/lib/errors.c b/lib/errors.c index a6bd765d..b30b8381 100644 --- a/lib/errors.c +++ b/lib/errors.c @@ -1066,7 +1066,7 @@ int nterror_to_errno(uint32_t status) { return EAGAIN; case SMB2_STATUS_ABORTED: return ECONNRESET; - case SMB2_STATUS_NO_SUCH_FILE: + case SMB2_STATUS_NO_SUCH_FILE: case SMB2_STATUS_NO_SUCH_DEVICE: case SMB2_STATUS_BAD_NETWORK_NAME: case SMB2_STATUS_OBJECT_NAME_NOT_FOUND: @@ -1107,24 +1107,22 @@ int nterror_to_errno(uint32_t status) { case SMB2_STATUS_CANNOT_DELETE: case SMB2_STATUS_FILE_DELETED: return EPERM; - case SMB2_STATUS_NO_MORE_FILES: + case SMB2_STATUS_NO_MORE_FILES: return ENODATA; case SMB2_STATUS_LOGON_FAILURE: return ECONNREFUSED; - case SMB2_STATUS_NOT_A_DIRECTORY: + case SMB2_STATUS_NOT_A_DIRECTORY: return ENOTDIR; case SMB2_STATUS_NOT_IMPLEMENTED: case SMB2_STATUS_INVALID_DEVICE_REQUEST: - case SMB2_STATUS_ILLEGAL_FUNCTION: - - case SMB2_STATUS_INVALID_PARAMETER: + case SMB2_STATUS_INVALID_PARAMETER: case SMB2_STATUS_NOT_SUPPORTED: case SMB2_STATUS_NOT_A_REPARSE_POINT: return EINVAL; case SMB2_STATUS_STOPPED_ON_SYMLINK: return ENOLINK; - case SMB2_STATUS_TOO_MANY_OPENED_FILES: + case SMB2_STATUS_TOO_MANY_OPENED_FILES: return EMFILE; case SMB2_STATUS_SECTION_TOO_BIG: case SMB2_STATUS_TOO_MANY_PAGING_FILES: @@ -1134,7 +1132,7 @@ int nterror_to_errno(uint32_t status) { return EXDEV; case SMB2_STATUS_SHARING_VIOLATION: return ETXTBSY; - case SMB2_STATUS_FILE_LOCK_CONFLICT: + case SMB2_STATUS_FILE_LOCK_CONFLICT: case SMB2_STATUS_LOCK_NOT_GRANTED: return EDEADLK; case SMB2_STATUS_OBJECT_NAME_COLLISION: @@ -1175,7 +1173,7 @@ int nterror_to_errno(uint32_t status) { return ENOEXEC; case SMB2_STATUS_IO_TIMEOUT: return ETIMEDOUT; - case SMB2_STATUS_INSUFFICIENT_RESOURCES: + case SMB2_STATUS_INSUFFICIENT_RESOURCES: return EBUSY; case SMB2_STATUS_INTERNAL_ERROR: /* Fall through. */ diff --git a/lib/init.c b/lib/init.c index 202a4ab2..38b68baa 100644 --- a/lib/init.c +++ b/lib/init.c @@ -294,7 +294,7 @@ struct smb2_context *smb2_init_context(void) #else srandom(time(NULL) ^ getpid() ^ ctr++); #endif - smb2 = calloc(1, sizeof(struct smb2_context)); + smb2 = calloc(1, sizeof(struct smb2_context)); if (smb2 == NULL) { return NULL; } diff --git a/lib/libsmb2.c b/lib/libsmb2.c index e693fb6b..ce8271e1 100644 --- a/lib/libsmb2.c +++ b/lib/libsmb2.c @@ -198,7 +198,7 @@ smb2_close_context(struct smb2_context *smb2) if (smb2->change_fd) { smb2->change_fd(smb2, smb2->fd, SMB2_DEL_FD); } - close(smb2->fd); + close(smb2->fd); smb2->fd = -1; } @@ -2541,7 +2541,7 @@ disconnect_cb_2(struct smb2_context *smb2, int status, if (smb2->change_fd) { smb2->change_fd(smb2, smb2->fd, SMB2_DEL_FD); } - close(smb2->fd); + close(smb2->fd); smb2->fd = -1; } diff --git a/lib/socket.c b/lib/socket.c index 7a5b1b5e..6957f48d 100644 --- a/lib/socket.c +++ b/lib/socket.c @@ -103,8 +103,6 @@ #include "smb3-seal.h" #include "libsmb2-private.h" - - #define MAX_URL_SIZE 1024 /* Timeout in ms between 2 consecutive socket connection. @@ -270,8 +268,8 @@ smb2_write_to_socket(struct smb2_context *smb2) count = writev(smb2->fd, tmpiov, niov); if (count == -1) { - if (errno == EAGAIN || errno == EWOULDBLOCK) { - return 0; + if (errno == EAGAIN || errno == EWOULDBLOCK) { + return 0; } smb2_set_error(smb2, "Error when writing to " "socket :%d %s", errno, @@ -861,7 +859,7 @@ set_tcp_sockopt(t_socket sockfd, int optname, int value) #if !defined(SOL_TCP) struct protoent *buf; if ((buf = getprotobyname("tcp")) != NULL) { - level = buf->p_proto; + level = buf->p_proto; } else { return -1; } @@ -884,7 +882,7 @@ connect_async_ai(struct smb2_context *smb2, const struct addrinfo *ai, int *fd_o struct LingerStruct const lin = { 1, 0 }; /* if l_linger is zero, sends RST after FIN */ #endif #ifdef _XBOX - BOOL bBroadcast = TRUE; + BOOL bBroadcast = TRUE; #endif memset(&ss, 0, sizeof(ss)); switch (ai->ai_family) { @@ -923,18 +921,18 @@ connect_async_ai(struct smb2_context *smb2, const struct addrinfo *ai, int *fd_o } #ifdef _XBOX - if(setsockopt(fd, SOL_SOCKET, 0x5801, (PCSTR)&bBroadcast, sizeof(BOOL) ) != 0 ) - { + if(setsockopt(fd, SOL_SOCKET, 0x5801, (PCSTR)&bBroadcast, sizeof(BOOL) ) != 0 ) + { #if 0 - return 0; + return 0; #endif - } - if(setsockopt(fd, SOL_SOCKET, 0x5802, (PCSTR)&bBroadcast, sizeof(BOOL)) != 0) - { + } + if(setsockopt(fd, SOL_SOCKET, 0x5802, (PCSTR)&bBroadcast, sizeof(BOOL)) != 0) + { #if 0 - return 0; + return 0; #endif - } + } #endif set_nonblocking(fd); @@ -952,7 +950,7 @@ connect_async_ai(struct smb2_context *smb2, const struct addrinfo *ai, int *fd_o #endif smb2_set_error(smb2, "Connect failed with errno : " "%s(%d)", strerror(errno), errno); - close(fd); + close(fd); return -EIO; } @@ -1081,9 +1079,9 @@ smb2_connect_async(struct smb2_context *smb2, const char *server, #if defined(_WINDOWS) || defined(_XBOX) if (err == WSANOTINITIALISED) { - smb2_set_error(smb2, "Winsock was not initialized. " + smb2_set_error(smb2, "Winsock was not initialized. " "Please call WSAStartup()."); - return -WSANOTINITIALISED; + return -WSANOTINITIALISED; } else #endif