Skip to content

Commit

Permalink
Merge pull request #344 from Wolf3s/winsock1_support
Browse files Browse the repository at this point in the history
Winsock1 support
  • Loading branch information
sahlberg committed May 29, 2024
2 parents 19933ba + 15e571f commit 38a40d9
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 20 deletions.
5 changes: 5 additions & 0 deletions include/smb2/libsmb2.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,12 @@ struct smb2dirent {
};

#if defined(_WINDOWS)
#ifdef __USE_WINSOCK__
#include <winsock.h>
#else
#include <ws2tcpip.h>
#include <winsock2.h>
#endif
#elif defined(_XBOX)
#include <xtl.h>
#include <winsockx.h>
Expand Down
11 changes: 6 additions & 5 deletions lib/compat.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#if defined(_WINDOWS) || defined(_XBOX)
#include <errno.h>
#include <stdlib.h>

#ifdef _WINDOWS
#define login_num ENXIO
#define getpid_num() GetCurrentProcessId()
Expand Down Expand Up @@ -232,7 +233,7 @@ int smb2_getaddrinfo(const char *node, const char*service,
#else
sin = malloc(sizeof(struct sockaddr_in));
#endif
#if !defined(_XBOX) && !defined(__NDS__)
#if !defined(_XBOX) && !defined(__NDS__) && !defined(__USE_WINSOCK__)
sin->sin_len = sizeof(struct sockaddr_in);
#endif
sin->sin_family=AF_INET;
Expand Down Expand Up @@ -330,7 +331,7 @@ int getlogin_r(char *buf, size_t size)
#endif

#ifdef NEED_WRITEV
ssize_t writev(int fd, const struct iovec *vector, int count)
ssize_t writev(t_socket fd, const struct iovec* vector, int count)
{
/* Find the total number of bytes to be written. */
size_t bytes = 0;
Expand Down Expand Up @@ -367,14 +368,14 @@ ssize_t writev(int fd, const struct iovec *vector, int count)
if (to_copy == 0)
break;
}
bytes_written = write(fd, buffer, bytes);
bytes_written = write((int)fd, buffer, bytes);
free(buffer);
return bytes_written;
}
#endif

#ifdef NEED_READV
ssize_t readv (int fd, const struct iovec *vector, int count)
ssize_t readv(t_socket fd, const struct iovec* vector, int count)
{
/* Find the total number of bytes to be read. */
size_t bytes = 0;
Expand All @@ -396,7 +397,7 @@ ssize_t readv (int fd, const struct iovec *vector, int count)
return -1;

/* Read the data. */
bytes_read = read(fd, buffer, bytes);
bytes_read = read((int)fd, buffer, bytes);
if (bytes_read < 0) {
free(buffer);
return -1;
Expand Down
59 changes: 46 additions & 13 deletions lib/compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,20 @@ extern "C" {
#if defined(_XBOX) || defined(_WINDOWS) || defined(__MINGW32__)
#if defined(_WINDOWS) || defined(__MINGW32__)
#include <windows.h>
#ifdef __USE_WINSOCK__
#include <winsock.h>
#else
#include <ws2tcpip.h>
#include <winsock2.h>
#endif
#elif defined(_XBOX)
#include <xtl.h>
#include <winsockx.h>
#endif
typedef SOCKET t_socket;
#ifndef INVALID_SOCKET
#define INVALID_SOCKET (t_socket)(~0)
#endif
#define SMB2_INVALID_SOCKET INVALID_SOCKET
#define SMB2_VALID_SOCKET(sock) ((sock) != SMB2_INVALID_SOCKET)
#else
Expand All @@ -46,6 +53,10 @@ typedef int t_socket;
#include <stddef.h>
#include <errno.h>

#ifdef __USE_WINSOCK__
#include <io.h>
#endif

#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif /* !WIN32_LEAN_AND_MEAN */
Expand Down Expand Up @@ -126,7 +137,7 @@ typedef unsigned int uintptr_t;
#define EAI_SERVICE 9
#endif

#ifdef _XBOX
#if defined(_XBOX) || defined(__USE_WINSOCK__)
typedef int socklen_t;
#endif

Expand All @@ -150,7 +161,7 @@ typedef int socklen_t;
#define SO_ERROR 0x1007
#endif

#ifdef _XBOX
#if defined(_XBOX) || defined(__USE_WINSOCK__)
struct sockaddr_storage {
#ifdef HAVE_SOCKADDR_SA_LEN
unsigned char ss_len;
Expand All @@ -172,16 +183,21 @@ struct addrinfo {

/* XBOX Defs end */
struct pollfd {
int fd;
t_socket fd;
short events;
short revents;
};

#define SOL_TCP 6
#ifndef SOL_TCP
#define SOL_TCP IPPROTO_TCP
#endif

#ifdef _XBOX
#define inline __inline
#endif

#endif

typedef SSIZE_T ssize_t;

struct iovec
Expand All @@ -190,9 +206,14 @@ struct iovec
void *iov_base;
};

#ifdef _XBOX
#if defined(_XBOX) || defined(__USE_WINSOCK__)
int poll(struct pollfd *fds, unsigned int nfds, int timo);

#ifdef __USE_WINSOCK__
#define write(fd, buf, maxcount) _write(fd, buf, (unsigned int)maxcount)
#define read(fd, buf, maxcount) _read(fd, buf, (unsigned int)maxcount)
#endif

int smb2_getaddrinfo(const char *node, const char*service,
const struct addrinfo *hints,
struct addrinfo **res);
Expand All @@ -208,6 +229,13 @@ void smb2_freeaddrinfo(struct addrinfo *res);

#endif

#ifdef __USE_WINSOCK__

ssize_t writev(t_socket fd, const struct iovec* vector, int count);
ssize_t readv(t_socket fd, const struct iovec* vector, int count);

#else

inline int writev(t_socket sock, struct iovec *iov, int nvecs)
{
DWORD ret;
Expand All @@ -232,8 +260,13 @@ inline int readv(t_socket sock, struct iovec *iov, int nvecs)
}
return -1;
}
#endif

#ifdef __USE_WINSOCK__
#define close(x) _close((int)x)
#else
#define close closesocket
#endif

void srandom(unsigned int seed);
int random(void);
Expand Down Expand Up @@ -280,8 +313,8 @@ int getlogin_r(char *buf, size_t size);
#define TCP_NODELAY 1 /* Don't delay send to coalesce packets */
#define SOL_TCP IPPROTO_TCP

ssize_t writev(int fd, const struct iovec *iov, int iovcnt);
ssize_t readv(int fd, const struct iovec *iov, int iovcnt);
ssize_t writev(t_socket fd, const struct iovec *iov, int iovcnt);
ssize_t readv(t_socket fd, const struct iovec *iov, int iovcnt);

int getlogin_r(char *buf, size_t size);

Expand Down Expand Up @@ -327,8 +360,8 @@ int smb2_getaddrinfo(const char *node, const char*service,
struct addrinfo **res);
void smb2_freeaddrinfo(struct addrinfo *res);
#ifndef __amigaos4__
ssize_t writev(int fd, const struct iovec *iov, int iovcnt);
ssize_t readv(int fd, const struct iovec *iov, int iovcnt);
ssize_t writev(t_socket fd, const struct iovec *iov, int iovcnt);
ssize_t readv(t_socket fd, const struct iovec *iov, int iovcnt);
#endif
#if !defined(HAVE_SOCKADDR_STORAGE)
/*
Expand Down Expand Up @@ -438,8 +471,8 @@ int iop_connect(int sockfd, struct sockaddr *addr, socklen_t addrlen);
#define read(a,b,c) lwip_recv(a,b,c,MSG_DONTWAIT)
#endif

ssize_t writev(int fd, const struct iovec *iov, int iovcnt);
ssize_t readv(int fd, const struct iovec *iov, int iovcnt);
ssize_t writev(t_socket fd, const struct iovec *iov, int iovcnt);
ssize_t readv(t_socket fd, const struct iovec *iov, int iovcnt);

#ifndef SOL_TCP
#define SOL_TCP IPPROTO_TCP
Expand Down Expand Up @@ -482,8 +515,8 @@ int smb2_getaddrinfo(const char *node, const char*service,
struct addrinfo **res);
void smb2_freeaddrinfo(struct addrinfo *res);

ssize_t writev(int fd, const struct iovec *iov, int iovcnt);
ssize_t readv(int fd, const struct iovec *iov, int iovcnt);
ssize_t writev(t_socket fd, const struct iovec *iov, int iovcnt);
ssize_t readv(t_socket fd, const struct iovec *iov, int iovcnt);

#define SOL_TCP IPPROTO_TCP
#define EAI_AGAIN EAGAIN
Expand Down
1 change: 1 addition & 0 deletions lib/socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ smb2_write_to_socket(struct smb2_context *smb2)
tmpiov->iov_len -= (size_t)num_done;
#endif
count = writev(smb2->fd, tmpiov, niov);

if (count == -1) {
if (errno == EAGAIN || errno == EWOULDBLOCK) {
return 0;
Expand Down
3 changes: 1 addition & 2 deletions lib/sync.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,11 @@ struct sync_cb_data {
static int wait_for_reply(struct smb2_context *smb2,
struct sync_cb_data *cb_data)
{
time_t t = time(NULL);
time_t t = time(NULL);

while (!cb_data->is_finished) {
struct pollfd pfd;
memset(&pfd, 0, sizeof(struct pollfd));

pfd.fd = smb2_get_fd(smb2);
pfd.events = smb2_which_events(smb2);

Expand Down

0 comments on commit 38a40d9

Please sign in to comment.