Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

switch: add libsmb2 #116

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions switch/libsmb2/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.tgz
switch-libsmb2
54 changes: 54 additions & 0 deletions switch/libsmb2/PKGBUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Maintainer: Rhys Koedijk <[email protected]>

pkgname=switch-libsmb2
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=aef1888f0f04bb41a38262d3388d7b673e48a1ed")
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 ${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_getaddrinfo_hints.patch"
patch -Np1 -i "$srcdir/../libsmb2-91d6c8a-fix_makefile_libnx_include.patch"
}

build() {
cd ${pkgname}

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

make
}

package() {
cd ${pkgname}

make DESTDIR="$pkgdir" install

install -Dm644 LICENCE-LGPL-2.1.txt "$pkgdir"/opt/devkitpro/portlibs/switch/licenses/$pkgname/LICENCE
}
30 changes: 30 additions & 0 deletions switch/libsmb2/libsmb2-91d6c8a-define_endian_macros.patch
Original file line number Diff line number Diff line change
@@ -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 <machine/endian.h>
+
+# 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
16 changes: 16 additions & 0 deletions switch/libsmb2/libsmb2-91d6c8a-define_getlogin_r.patch
Original file line number Diff line number Diff line change
@@ -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 <errno.h>
+#define getlogin_r(a,b) ENXIO
+#endif // __SWITCH__
+
static int
smb2_parse_args(struct smb2_context *smb2, const char *args)
{
102 changes: 102 additions & 0 deletions switch/libsmb2/libsmb2-91d6c8a-define_net_readv_writev.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
--- a/lib/socket.c
+++ b/lib/socket.c
@@ -80,6 +80,99 @@

#define MAX_URL_SIZE 256

+#ifdef __SWITCH__
+
+#include <netdb.h>
+#include <netinet/tcp.h>
+#include <netinet/in.h>
+#include <poll.h>
+
+
+ssize_t writev(int fd, const struct iovec *vector, int count)
+{
+ size_t bytes = 0;
+
+ for (int i = 0; i < count; ++i)
+ {
+ bytes += vector[i].iov_len;
+ }
+
+ char *buffer = (char *)malloc(bytes);
+
+ if (buffer == NULL)
+ {
+ return -1;
+ }
+
+ 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 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;
+}
+
+
+#endif
+
static int
smb2_get_credit_charge(struct smb2_context *smb2, struct smb2_pdu *pdu)
{
20 changes: 20 additions & 0 deletions switch/libsmb2/libsmb2-91d6c8a-fix_getaddrinfo_hints.patch
Original file line number Diff line number Diff line change
@@ -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;
Comment on lines +10 to +13
Copy link
Contributor Author

@rhyskoedijk rhyskoedijk Oct 19, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getaddrinfo() was as always throwing error 22 without this.
I found an existing issue reporting the same problem, copy/pasted the solution. Probably a better way to fix this though...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fwiw getaddrinfo with hints=NULL is currently broken. However, a libnx branch I'm working on (https://github.com/switchbrew/libnx/tree/socket-cleanup) fixes this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, I'll remove this patch once fixed in libnx then.


/* 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);
12 changes: 12 additions & 0 deletions switch/libsmb2/libsmb2-91d6c8a-fix_makefile_libnx_include.patch
Original file line number Diff line number Diff line change
@@ -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 \
Copy link
Contributor Author

@rhyskoedijk rhyskoedijk Oct 19, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why this is needed, but this is the only way I could get it to compile.
The build doesn't seem to pick up the libnx includes from the -isystem already defined in switchvars.sh.

In configure.ac some of the header checks are failing.
e.g. AC_CHECK_HEADERS([arpa/inet.h]) despite that header actually existing, it reports it as missing for some reason.

"-D_U_=__attribute__((unused))"

libsmb2_la_SOURCES = \
12 changes: 12 additions & 0 deletions switch/libsmb2/libsmb2-91d6c8a-fix_portable_endian_include.patch
Original file line number Diff line number Diff line change
@@ -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 <string.h>

#include "aes.h"
+#include "portable-endian.h"

static void aes_ccm_generate_b0(unsigned char *nonce, int nlen,
int alen, int plen, int mlen,