From 4913208057ef67a251618c1ce3e4d754fa811878 Mon Sep 17 00:00:00 2001 From: Rhys Koedijk Date: Sun, 29 Sep 2019 17:07:50 +1300 Subject: [PATCH 1/6] libsmb2 first draft --- switch/libsmb2/PKGBUILD | 43 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 switch/libsmb2/PKGBUILD diff --git a/switch/libsmb2/PKGBUILD b/switch/libsmb2/PKGBUILD new file mode 100644 index 00000000..650df557 --- /dev/null +++ b/switch/libsmb2/PKGBUILD @@ -0,0 +1,43 @@ +# Maintainer: Rhys Koedijk + +pkgname=switch-libsmb2 +pkgver=3.0.0 +pkgrel=2 +pkgdesc='SMB2/3 userspace client' +arch=('any') +url='https://github.com/sahlberg/libsmb2' +license=('(L)GPL') +options=(!strip libtool staticlibs) +source=("https://github.com/sahlberg/libsmb2/archive/v${pkgver}.tar.gz") +sha256sums=('36d10d1628ad34d56646bf9e7005a8abe8ce6b67ba3a949d537ae3e5f08f701f') +makedepends=('switch-pkg-config' 'devkitpro-pkgbuild-helpers') +groups=('switch-portlibs') + +prepare() { + cd . +} + +build() { + cd libsmb2-$pkgver + + source /opt/devkitpro/switchvars.sh + + libtoolize + aclocal + autoheader + automake --add-missing + autoconf + + ./configure --prefix="${PORTLIBS_PREFIX}" --host=aarch64-none-elf \ + --disable-shared --enable-static --without-libkrb5 + + #cp config.h /lib + + make +} + +package() { + cd libsmb2-$pkgver + + make DESTDIR="$pkgdir" install +} From 761172950179d287ef9c85acf3288bcfd322d358 Mon Sep 17 00:00:00 2001 From: Rhys Koedijk Date: Mon, 30 Sep 2019 13:28:40 +1300 Subject: [PATCH 2/6] Added src patches, build package from git head (newer), cleaned up package output --- switch/libsmb2/.gitignore | 2 ++ switch/libsmb2/PKGBUILD | 31 ++++++++++++------- ...libsmb2-91d6c8a-define_endian_macros.patch | 30 ++++++++++++++++++ .../libsmb2-91d6c8a-define_getlogin_r.patch | 16 ++++++++++ ...-91d6c8a-fix_portable_endian_include.patch | 12 +++++++ 5 files changed, 80 insertions(+), 11 deletions(-) create mode 100644 switch/libsmb2/.gitignore create mode 100644 switch/libsmb2/libsmb2-91d6c8a-define_endian_macros.patch create mode 100644 switch/libsmb2/libsmb2-91d6c8a-define_getlogin_r.patch create mode 100644 switch/libsmb2/libsmb2-91d6c8a-fix_portable_endian_include.patch diff --git a/switch/libsmb2/.gitignore b/switch/libsmb2/.gitignore new file mode 100644 index 00000000..a96471be --- /dev/null +++ b/switch/libsmb2/.gitignore @@ -0,0 +1,2 @@ +*.tgz +switch-libsmb2 \ No newline at end of file diff --git a/switch/libsmb2/PKGBUILD b/switch/libsmb2/PKGBUILD index 650df557..434fe4fa 100644 --- a/switch/libsmb2/PKGBUILD +++ b/switch/libsmb2/PKGBUILD @@ -1,24 +1,32 @@ # Maintainer: Rhys Koedijk pkgname=switch-libsmb2 -pkgver=3.0.0 -pkgrel=2 +pkgver=r285.91d6c8a +pkgrel=1 pkgdesc='SMB2/3 userspace client' arch=('any') url='https://github.com/sahlberg/libsmb2' license=('(L)GPL') options=(!strip libtool staticlibs) -source=("https://github.com/sahlberg/libsmb2/archive/v${pkgver}.tar.gz") -sha256sums=('36d10d1628ad34d56646bf9e7005a8abe8ce6b67ba3a949d537ae3e5f08f701f') -makedepends=('switch-pkg-config' 'devkitpro-pkgbuild-helpers') +source=(${pkgname}::"git+https://github.com/sahlberg/libsmb2.git#commit=91d6c8a44b6f6adaa879cc556cf96ff6077bbb4f") +sha256sums=('SKIP') +makedepends=('git' 'switch-pkg-config' 'devkitpro-pkgbuild-helpers') groups=('switch-portlibs') +pkgver() { + cd "$srcdir/${pkgname}" + printf 'r%s.%s' "$(git rev-list --count HEAD)" "$(git rev-parse --short HEAD)" +} + prepare() { - cd . + cd ${pkgname} + patch -Np1 -i "$srcdir/../libsmb2-91d6c8a-define_endian_macros.patch" + patch -Np1 -i "$srcdir/../libsmb2-91d6c8a-define_getlogin_r.patch" + patch -Np1 -i "$srcdir/../libsmb2-91d6c8a-fix_portable_endian_include.patch" } build() { - cd libsmb2-$pkgver + cd ${pkgname} source /opt/devkitpro/switchvars.sh @@ -31,13 +39,14 @@ build() { ./configure --prefix="${PORTLIBS_PREFIX}" --host=aarch64-none-elf \ --disable-shared --enable-static --without-libkrb5 - #cp config.h /lib - make } package() { - cd libsmb2-$pkgver - + cd ${pkgname} + make DESTDIR="$pkgdir" install + + install -Dm644 LICENCE "$pkgdir"/opt/devkitpro/portlibs/switch/licenses/$pkgname/LICENCE-LGPL-2.1.txt + rm -r "$pkgdir"/opt/devkitpro/portlibs/switch/share } diff --git a/switch/libsmb2/libsmb2-91d6c8a-define_endian_macros.patch b/switch/libsmb2/libsmb2-91d6c8a-define_endian_macros.patch new file mode 100644 index 00000000..e2341d2f --- /dev/null +++ b/switch/libsmb2/libsmb2-91d6c8a-define_endian_macros.patch @@ -0,0 +1,30 @@ +diff --git a/include/portable-endian.h b/include/portable-endian.h +index e0730b2..17e1b3f 100644 +--- a/include/portable-endian.h ++++ b/include/portable-endian.h +@@ -110,6 +110,25 @@ + # error platform not supported + # endif + ++#elif defined(__SWITCH__) ++ ++# include ++ ++# define htobe16(x) __bswap16(x) ++# define htole16(x) (x) ++# define be16toh(x) __bswap16(x) ++# define le16toh(x) (x) ++ ++# define htobe32(x) __bswap32(x) ++# define htole32(x) (x) ++# define be32toh(x) __bswap32(x) ++# define le32toh(x) (x) ++ ++# define htobe64(x) __bswap64(x) ++# define htole64(x) (x) ++# define be64toh(x) __bswap64(x) ++# define le64toh(x) (x) ++ + #else + # error platform not supported + #endif \ No newline at end of file diff --git a/switch/libsmb2/libsmb2-91d6c8a-define_getlogin_r.patch b/switch/libsmb2/libsmb2-91d6c8a-define_getlogin_r.patch new file mode 100644 index 00000000..586d9545 --- /dev/null +++ b/switch/libsmb2/libsmb2-91d6c8a-define_getlogin_r.patch @@ -0,0 +1,16 @@ +diff --git a/lib/init.c b/lib/init.c +index aeb1982..f349cd4 100644 +--- a/lib/init.c ++++ b/lib/init.c +@@ -74,6 +74,11 @@ + #endif + #endif // __ANDROID__ + ++#ifdef __SWITCH__ ++#include ++#define getlogin_r(a,b) ENXIO ++#endif // __SWITCH__ ++ + static int + smb2_parse_args(struct smb2_context *smb2, const char *args) + { \ No newline at end of file diff --git a/switch/libsmb2/libsmb2-91d6c8a-fix_portable_endian_include.patch b/switch/libsmb2/libsmb2-91d6c8a-fix_portable_endian_include.patch new file mode 100644 index 00000000..6998751f --- /dev/null +++ b/switch/libsmb2/libsmb2-91d6c8a-fix_portable_endian_include.patch @@ -0,0 +1,12 @@ +diff --git a/lib/aes128ccm.c b/lib/aes128ccm.c +index 10bd11d..bcc492d 100644 +--- a/lib/aes128ccm.c ++++ b/lib/aes128ccm.c +@@ -21,6 +21,7 @@ + #include + + #include "aes.h" ++#include "portable-endian.h" + + static void aes_ccm_generate_b0(unsigned char *nonce, int nlen, + int alen, int plen, int mlen, \ No newline at end of file From 7072ec194e18f3f2f49c49a28c0ba58075968e38 Mon Sep 17 00:00:00 2001 From: Rhys Koedijk Date: Sat, 19 Oct 2019 16:20:04 +1300 Subject: [PATCH 3/6] Fix build on switch --- switch/libsmb2/PKGBUILD | 9 +-- ...smb2-91d6c8a-define_net_readv_writev.patch | 57 +++++++++++++++++++ ...2-91d6c8a-fix_makefile_libnx_include.patch | 12 ++++ 3 files changed, 74 insertions(+), 4 deletions(-) create mode 100644 switch/libsmb2/libsmb2-91d6c8a-define_net_readv_writev.patch create mode 100644 switch/libsmb2/libsmb2-91d6c8a-fix_makefile_libnx_include.patch diff --git a/switch/libsmb2/PKGBUILD b/switch/libsmb2/PKGBUILD index 434fe4fa..2beab89d 100644 --- a/switch/libsmb2/PKGBUILD +++ b/switch/libsmb2/PKGBUILD @@ -1,14 +1,14 @@ # Maintainer: Rhys Koedijk pkgname=switch-libsmb2 -pkgver=r285.91d6c8a +pkgver=r307.aef1888 pkgrel=1 pkgdesc='SMB2/3 userspace client' arch=('any') url='https://github.com/sahlberg/libsmb2' license=('(L)GPL') options=(!strip libtool staticlibs) -source=(${pkgname}::"git+https://github.com/sahlberg/libsmb2.git#commit=91d6c8a44b6f6adaa879cc556cf96ff6077bbb4f") +source=(${pkgname}::"git+https://github.com/sahlberg/libsmb2.git#commit=aef1888f0f04bb41a38262d3388d7b673e48a1ed") sha256sums=('SKIP') makedepends=('git' 'switch-pkg-config' 'devkitpro-pkgbuild-helpers') groups=('switch-portlibs') @@ -22,7 +22,9 @@ prepare() { cd ${pkgname} patch -Np1 -i "$srcdir/../libsmb2-91d6c8a-define_endian_macros.patch" patch -Np1 -i "$srcdir/../libsmb2-91d6c8a-define_getlogin_r.patch" + patch -Np1 -i "$srcdir/../libsmb2-91d6c8a-define_net_readv_writev.patch" patch -Np1 -i "$srcdir/../libsmb2-91d6c8a-fix_portable_endian_include.patch" + patch -Np1 -i "$srcdir/../libsmb2-91d6c8a-fix_makefile_libnx_include.patch" } build() { @@ -47,6 +49,5 @@ package() { make DESTDIR="$pkgdir" install - install -Dm644 LICENCE "$pkgdir"/opt/devkitpro/portlibs/switch/licenses/$pkgname/LICENCE-LGPL-2.1.txt - rm -r "$pkgdir"/opt/devkitpro/portlibs/switch/share + install -Dm644 LICENCE-LGPL-2.1.txt "$pkgdir"/opt/devkitpro/portlibs/switch/licenses/$pkgname/LICENCE } diff --git a/switch/libsmb2/libsmb2-91d6c8a-define_net_readv_writev.patch b/switch/libsmb2/libsmb2-91d6c8a-define_net_readv_writev.patch new file mode 100644 index 00000000..a9493e80 --- /dev/null +++ b/switch/libsmb2/libsmb2-91d6c8a-define_net_readv_writev.patch @@ -0,0 +1,57 @@ +diff --git a/lib/socket.c b/lib/socket.c +index 978f06d..a7fde76 100644 +--- a/lib/socket.c ++++ b/lib/socket.c +@@ -80,6 +80,52 @@ + + #define MAX_URL_SIZE 256 + ++#ifdef __SWITCH__ ++ ++#include ++#include ++#include ++#include ++ ++ssize_t writev(int fd, const struct iovec *iov, int iovcnt) ++{ ++ int total = 0; ++ for (int i = 0; i < iovcnt; i++) { ++ int left = iov[i].iov_len; ++ while (left > 0) { ++ int count = write(fd, iov[i].iov_base, left); ++ if (count == -1) { ++ return -1; ++ } ++ total += count; ++ left -= count; ++ } ++ } ++ return total; ++} ++ ++ssize_t readv(int fd, const struct iovec *iov, int iovcnt) ++{ ++ ssize_t total = 0; ++ for (int i = 0; i < iovcnt; i++) { ++ int left = iov[i].iov_len; ++ while (left > 0) { ++ int count = read(fd, iov[i].iov_base, left); ++ if (count == -1) { ++ return -1; ++ } ++ if (count == 0) { ++ return total; ++ } ++ total += count; ++ left -= count; ++ } ++ } ++ return total; ++} ++ ++#endif ++ + static int + smb2_get_credit_charge(struct smb2_context *smb2, struct smb2_pdu *pdu) + { diff --git a/switch/libsmb2/libsmb2-91d6c8a-fix_makefile_libnx_include.patch b/switch/libsmb2/libsmb2-91d6c8a-fix_makefile_libnx_include.patch new file mode 100644 index 00000000..b406c752 --- /dev/null +++ b/switch/libsmb2/libsmb2-91d6c8a-fix_makefile_libnx_include.patch @@ -0,0 +1,12 @@ +diff --git a/lib/Makefile.am b/lib/Makefile.am +index 72a86ec..fe133eb 100644 +--- a/lib/Makefile.am ++++ b/lib/Makefile.am +@@ -4,6 +4,7 @@ lib_LTLIBRARIES = libsmb2.la + + libsmb2_la_CPPFLAGS = -I$(abs_top_srcdir)/include \ + -I$(abs_top_srcdir)/include/smb2 \ ++ -I$(DEVKITPRO)/libnx/include \ + "-D_U_=__attribute__((unused))" + + libsmb2_la_SOURCES = \ \ No newline at end of file From ed21830debf10892775b787152604ee6857484cd Mon Sep 17 00:00:00 2001 From: Rhys Koedijk Date: Sat, 19 Oct 2019 18:25:56 +1300 Subject: [PATCH 4/6] Fix for getaddrinfo --- switch/libsmb2/PKGBUILD | 1 + ...ibsmb2-91d6c8a-fix_getaddrinfo_hints.patch | 20 +++++++++++++++++++ 2 files changed, 21 insertions(+) create mode 100644 switch/libsmb2/libsmb2-91d6c8a-fix_getaddrinfo_hints.patch diff --git a/switch/libsmb2/PKGBUILD b/switch/libsmb2/PKGBUILD index 2beab89d..524d323b 100644 --- a/switch/libsmb2/PKGBUILD +++ b/switch/libsmb2/PKGBUILD @@ -24,6 +24,7 @@ prepare() { patch -Np1 -i "$srcdir/../libsmb2-91d6c8a-define_getlogin_r.patch" patch -Np1 -i "$srcdir/../libsmb2-91d6c8a-define_net_readv_writev.patch" patch -Np1 -i "$srcdir/../libsmb2-91d6c8a-fix_portable_endian_include.patch" + patch -Np1 -i "$srcdir/../libsmb2-91d6c8a-fix_getaddrinfo_hints.patch" patch -Np1 -i "$srcdir/../libsmb2-91d6c8a-fix_makefile_libnx_include.patch" } diff --git a/switch/libsmb2/libsmb2-91d6c8a-fix_getaddrinfo_hints.patch b/switch/libsmb2/libsmb2-91d6c8a-fix_getaddrinfo_hints.patch new file mode 100644 index 00000000..e05168dc --- /dev/null +++ b/switch/libsmb2/libsmb2-91d6c8a-fix_getaddrinfo_hints.patch @@ -0,0 +1,20 @@ +diff --git a/lib/socket.c b/lib/socket.c +index 978f06d..156fad5 100644 +--- a/lib/socket.c ++++ b/lib/socket.c +@@ -701,9 +747,14 @@ smb2_connect_async(struct smb2_context *smb2, const char *server, + } else { + port = "445"; + } ++ ++ struct addrinfo hints; ++ memset(&hints, 0, sizeof hints); ++ hints.ai_family = AF_INET; // AF_INET or AF_INET6 to force version ++ hints.ai_socktype = SOCK_STREAM; + + /* is it a hostname ? */ +- if (getaddrinfo(host, port, NULL, &ai) != 0) { ++ if (getaddrinfo(host, port, &hints, &ai) != 0) { + free(addr); + smb2_set_error(smb2, "Invalid address:%s " + "Can not resolv into IPv4/v6.", server); From d62d4cb256a86eab53163a5fd9496ac954182e83 Mon Sep 17 00:00:00 2001 From: Blake Warner Date: Wed, 6 Nov 2019 12:27:41 -0500 Subject: [PATCH 5/6] fix readv writev --- ...smb2-91d6c8a-define_net_readv_writev.patch | 112 +++++++++++++----- 1 file changed, 82 insertions(+), 30 deletions(-) diff --git a/switch/libsmb2/libsmb2-91d6c8a-define_net_readv_writev.patch b/switch/libsmb2/libsmb2-91d6c8a-define_net_readv_writev.patch index a9493e80..732f2b1e 100644 --- a/switch/libsmb2/libsmb2-91d6c8a-define_net_readv_writev.patch +++ b/switch/libsmb2/libsmb2-91d6c8a-define_net_readv_writev.patch @@ -1,8 +1,6 @@ -diff --git a/lib/socket.c b/lib/socket.c -index 978f06d..a7fde76 100644 --- a/lib/socket.c +++ b/lib/socket.c -@@ -80,6 +80,52 @@ +@@ -80,6 +80,106 @@ #define MAX_URL_SIZE 256 @@ -13,43 +11,97 @@ index 978f06d..a7fde76 100644 +#include +#include + -+ssize_t writev(int fd, const struct iovec *iov, int iovcnt) ++ ++ssize_t writev(int fd, const struct iovec *vector, int count) +{ -+ int total = 0; -+ for (int i = 0; i < iovcnt; i++) { -+ int left = iov[i].iov_len; -+ while (left > 0) { -+ int count = write(fd, iov[i].iov_base, left); -+ if (count == -1) { ++ size_t bytes = 0; ++ ++ for (int i = 0; i < count; ++i) ++ { ++ if ((SIZE_MAX/2) - bytes < vector[i].iov_len) ++ { ++ return -1; ++ } ++ bytes += vector[i].iov_len; ++ } ++ ++ char *buffer = (char *)malloc(bytes); ++ ++ if (buffer == NULL) ++ { + return -1; -+ } -+ total += count; -+ left -= count; + } -+ } -+ return total; ++ ++ size_t to_copy = bytes; ++ char *bp = buffer; ++ ++ for (int i = 0; i < count; ++i) ++ { ++ size_t copy = MIN(vector[i].iov_len, to_copy); ++ bp = mempcpy((void *)bp, (void *)vector[i].iov_base, copy); ++ to_copy -= copy; ++ if (to_copy == 0) ++ { ++ break; ++ } ++ } ++ ++ ssize_t bytes_written = write(fd, buffer, bytes); ++ free(buffer); ++ return bytes_written; +} + -+ssize_t readv(int fd, const struct iovec *iov, int iovcnt) ++ssize_t readv(int fd, const struct iovec *vector, int count) +{ -+ ssize_t total = 0; -+ for (int i = 0; i < iovcnt; i++) { -+ int left = iov[i].iov_len; -+ while (left > 0) { -+ int count = read(fd, iov[i].iov_base, left); -+ if (count == -1) { ++ size_t bytes = 0; ++ ++ for (int i = 0; i < count; ++i) ++ { ++ if ((SIZE_MAX/2) - bytes < vector[i].iov_len) ++ { ++ return -1; ++ } ++ bytes += vector[i].iov_len; ++ } ++ ++ char *buffer = (char *)malloc(bytes); ++ char *bp = buffer; ++ ++ buffer = (char *)malloc(bytes); ++ ++ if (buffer == NULL) ++ { ++ return -1; ++ } ++ ++ ssize_t bytes_read = read(fd, buffer, bytes); ++ ++ if (bytes_read < 0) ++ { ++ free((void*)buffer); + return -1; -+ } -+ if (count == 0) { -+ return total; -+ } -+ total += count; -+ left -= count; + } -+ } -+ return total; ++ ++ bytes = bytes_read; ++ ++ for (int i = 0; i < count; ++i) ++ { ++ size_t copy = MIN(vector[i].iov_len, bytes); ++ memcpy((void *)vector[i].iov_base, (void *)bp, copy); ++ bp += copy; ++ bytes -= copy; ++ ++ if (bytes == 0) ++ { ++ break; ++ } ++ } ++ ++ free((void*)buffer); ++ return bytes_read; +} + ++ +#endif + static int From b4b456ff84a5ca448cf3fec1a271b060e0b3d12a Mon Sep 17 00:00:00 2001 From: Blake Warner Date: Wed, 6 Nov 2019 14:05:32 -0500 Subject: [PATCH 6/6] fixed readv bug --- ...smb2-91d6c8a-define_net_readv_writev.patch | 97 +++++++++---------- 1 file changed, 45 insertions(+), 52 deletions(-) diff --git a/switch/libsmb2/libsmb2-91d6c8a-define_net_readv_writev.patch b/switch/libsmb2/libsmb2-91d6c8a-define_net_readv_writev.patch index 732f2b1e..b4edf9c3 100644 --- a/switch/libsmb2/libsmb2-91d6c8a-define_net_readv_writev.patch +++ b/switch/libsmb2/libsmb2-91d6c8a-define_net_readv_writev.patch @@ -1,6 +1,6 @@ --- a/lib/socket.c +++ b/lib/socket.c -@@ -80,6 +80,106 @@ +@@ -80,6 +80,99 @@ #define MAX_URL_SIZE 256 @@ -18,10 +18,6 @@ + + for (int i = 0; i < count; ++i) + { -+ if ((SIZE_MAX/2) - bytes < vector[i].iov_len) -+ { -+ return -1; -+ } + bytes += vector[i].iov_len; + } + @@ -51,54 +47,51 @@ + return bytes_written; +} + -+ssize_t readv(int fd, const struct iovec *vector, int count) ++ssize_t readv(int fd, const struct iovec *iov, int iovcnt) +{ -+ size_t bytes = 0; -+ -+ for (int i = 0; i < count; ++i) -+ { -+ if ((SIZE_MAX/2) - bytes < vector[i].iov_len) -+ { -+ return -1; -+ } -+ bytes += vector[i].iov_len; -+ } -+ -+ char *buffer = (char *)malloc(bytes); -+ char *bp = buffer; -+ -+ buffer = (char *)malloc(bytes); -+ -+ if (buffer == NULL) -+ { -+ return -1; -+ } -+ -+ ssize_t bytes_read = read(fd, buffer, bytes); -+ -+ if (bytes_read < 0) -+ { -+ free((void*)buffer); -+ return -1; -+ } -+ -+ bytes = bytes_read; -+ -+ for (int i = 0; i < count; ++i) -+ { -+ size_t copy = MIN(vector[i].iov_len, bytes); -+ memcpy((void *)vector[i].iov_base, (void *)bp, copy); -+ bp += copy; -+ bytes -= copy; -+ -+ if (bytes == 0) -+ { -+ break; -+ } -+ } -+ -+ free((void*)buffer); -+ return bytes_read; ++ ssize_t total = 0, bytes_read = 0; ++ ++ for (int j = 0; j < iovcnt; j++) { ++ total += iov[j].iov_len; ++ } ++ ++ char *buffer = (char *)malloc(total); ++ char *bp = buffer; ++ ++ if(buffer == NULL) { ++ return -1; ++ } ++ ++ int bytes_left = read(fd, buffer, total); ++ ++ if (bytes_left == -1) { ++ free((void*)buffer); ++ return -1; ++ } ++ ++ if(bytes_left == 0) ++ { ++ return 0; ++ } ++ ++ for (int i = 0; i < iovcnt; i++) ++ { ++ int sz = MIN(iov[i].iov_len, bytes_left); ++ ++ memcpy(iov[i].iov_base, bp, sz); ++ ++ bp += sz; ++ bytes_read += sz; ++ bytes_left -= sz; ++ ++ if(bytes_left < 1) ++ { ++ break; ++ } ++ } ++ ++ free((void*)buffer); ++ return bytes_read; +} + +