Skip to content
Draft
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
36 changes: 36 additions & 0 deletions dprintf.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include <stdio.h>
#include <stdarg.h>
#include <unistd.h>

int dprintf(int fd, const char *fmt, ...)
{
char stack_buffer[256];
char *buf = stack_buffer;
int len = sizeof(stack_buffer);
va_list ap;
int ret = -1;

va_start(ap, fmt);

// Method 1 try to just print it to stack if possible
ret = vsnprintf(buf, len, fmt, ap);

if (ret >= len) {
// Method 2 dynamically allocate buffer
len = ret + 1;
buf = malloc(len);
if (buf) {
va_end(ap);
va_start(ap, fmt);
ret = vsnprintf(buf, len, fmt, ap);
} else {
ret = -1; // Malloc failed due to too little memory??? Couldn't be me
}
}
if (ret >= 0 && (write(fd, buf, ret) != ret)) {
ret = -1;
}
if (buf != stack_buffer) free(buf);
va_end(ap);
return ret;
}
18 changes: 15 additions & 3 deletions server.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,21 @@
#undef _POSIX_C_SOURCE
#define _POSIX_C_SOURCE 200809L

#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#ifdef _WIN32
#include <winsock2.h>
#include <ws2tcpip.h>

// Add definition for pollfd to account for Windows not defining
struct pollfd {
SOCKET fd; // socket instead of int
short events; // same values as POSIX (POLLIN, POLLOUT, etc.)
short revents;
};
#else
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#endif

#pragma RcB2 DEP "server.c"

Expand Down
25 changes: 21 additions & 4 deletions sockssrv.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,24 @@
*/

#define _GNU_SOURCE
#ifdef _WIN32
#include <winsock2.h>
// ^^ Ugly hack to force it to compile
#endif
#include <unistd.h>
#define _POSIX_C_SOURCE 200809L
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <pthread.h>
#include <signal.h>
#include <poll.h>
#include <arpa/inet.h>
#ifdef _WIN32
#include "wsa2unix.h"
#include "dprintf.c"
#else
#include <arpa/inet.h>
#include <poll.h>
#endif
#include <errno.h>
#include <limits.h>
#include "server.h"
Expand Down Expand Up @@ -276,7 +285,11 @@ static void copyloop(int fd1, int fd2) {
/* inactive connections are reaped after 15 min to free resources.
usually programs send keep-alive packets so this should only happen
when a connection is really unused. */
switch(poll(fds, 2, 60*15*1000)) {
#ifdef _WIN32
switch(WSAPoll(fds, 2, 60*15*1000)) {
#else
switch(poll(fds, 2, 60*15*1000)) {
#endif
case 0:
return;
case -1:
Expand Down Expand Up @@ -471,7 +484,11 @@ int main(int argc, char** argv) {
dprintf(2, "error: -1/-w options must be used together with user/pass\n");
return 1;
}
signal(SIGPIPE, SIG_IGN);
#ifdef _WIN32
// This is not needed because Windows doesn't kill processes on pipe failure
#else
signal(SIGPIPE, SIG_IGN);
#endif
struct server s;
sblist *threads = sblist_new(sizeof (struct thread*), 8);
if(server_setup(&s, listenip, port)) {
Expand Down
64 changes: 64 additions & 0 deletions wsa2unix.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/* Define POLLIN values manually if not defined by some compiler */
#ifndef POLLIN
#define POLLIN 0x001
#define POLLPRI 0x002
#define POLLOUT 0x004
#define POLLERR 0x008
#define POLLHUP 0x010
#define POLLNVAL 0x020
#define POLLRDNORM 0x100
#define POLLRDBAND 0x200
#define POLLWRNORM 0x010
#define POLLWRBAND 0x400
#endif

/* Operation would block */
#define EWOULDBLOCK WSAEWOULDBLOCK
#define EAGAIN WSAEWOULDBLOCK

/* Operation now in progress */
#define EINPROGRESS WSAEINPROGRESS
#define EALREADY WSAEALREADY

/* Socket errors */
#define ENOTSOCK WSAENOTSOCK
#define EDESTADDRREQ WSAEDESTADDRREQ
#define EMSGSIZE WSAEMSGSIZE
#define EPROTOTYPE WSAEPROTOTYPE
#define ENOPROTOOPT WSAENOPROTOOPT
#define EPROTONOSUPPORT WSAEPROTONOSUPPORT
#define ESOCKTNOSUPPORT WSAESOCKTNOSUPPORT
#define EOPNOTSUPP WSAEOPNOTSUPP
#define EPFNOSUPPORT WSAEPFNOSUPPORT
#define EAFNOSUPPORT WSAEAFNOSUPPORT

/* Address errors */
#define EADDRINUSE WSAEADDRINUSE
#define EADDRNOTAVAIL WSAEADDRNOTAVAIL

/* Network subsystem errors */
#define ENETDOWN WSAENETDOWN
#define ENETUNREACH WSAENETUNREACH
#define ENETRESET WSAENETRESET

/* Connection errors */
#define ECONNABORTED WSAECONNABORTED
#define ECONNRESET WSAECONNRESET
#define ENOBUFS WSAENOBUFS
#define EISCONN WSAEISCONN
#define ENOTCONN WSAENOTCONN
#define ESHUTDOWN WSAESHUTDOWN
#define ETIMEDOUT WSAETIMEDOUT
#define ECONNREFUSED WSAECONNREFUSED

/* Host errors */
#define EHOSTUNREACH WSAEHOSTUNREACH

/* Some misc stuff */
#define EINTR WSAEINTR
#define EFAULT WSAEFAULT
#define EINVAL WSAEINVAL
#define EMFILE WSAEMFILE
#define EACCES WSAEACCES
#define EPERM WSAEACCES
#define ENOMEM WSA_NOT_ENOUGH_MEMORY