diff --git a/contrib/win32/w32-posix-prototype/win32posix/Tests/Tests.vcxproj b/contrib/win32/w32-posix-prototype/win32posix/Tests/Tests.vcxproj index 5e424fe..41d872c 100644 --- a/contrib/win32/w32-posix-prototype/win32posix/Tests/Tests.vcxproj +++ b/contrib/win32/w32-posix-prototype/win32posix/Tests/Tests.vcxproj @@ -60,6 +60,7 @@ Windows true $(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories) + Ws2_32.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies);C:\openssh\Win32-OpenSSH_\contrib\win32\w32-posix-prototype\win32posix\Debug\win32posix.lib diff --git a/contrib/win32/w32-posix-prototype/win32posix/Tests/unittest1.cpp b/contrib/win32/w32-posix-prototype/win32posix/Tests/unittest1.cpp index 2858315..9cdeabf 100644 --- a/contrib/win32/w32-posix-prototype/win32posix/Tests/unittest1.cpp +++ b/contrib/win32/w32-posix-prototype/win32posix/Tests/unittest1.cpp @@ -1,5 +1,7 @@ #include "CppUnitTest.h" +extern "C" { #include "..\win32posix\w32posix.h" +} using namespace Microsoft::VisualStudio::CppUnitTestFramework; @@ -29,9 +31,66 @@ namespace UnitTests Assert::AreEqual(0, FD_ISSET(1, set), L"", LINE_INFO()); Assert::AreEqual(0, FD_ISSET(2, set), L"", LINE_INFO()); + w32posix_initialize(); + int sockfd, new_fd; // listen on sock_fd, new connection on new_fd + struct addrinfo hints, *servinfo, *p; + struct sockaddr_storage their_addr; // connector's address information + socklen_t sin_size; + int yes = 1; + char s[INET6_ADDRSTRLEN]; + int rv; +#define PORT "3490" // the port users will be connecting to + +#define BACKLOG 10 // how many pending connections queue will hold + + memset(&hints, 0, sizeof hints); + hints.ai_family = AF_UNSPEC; + hints.ai_socktype = SOCK_STREAM; + hints.ai_flags = AI_PASSIVE; // use my IP + + if ((rv = getaddrinfo(NULL, PORT, &hints, &servinfo)) != 0) { + fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv)); + } + + // loop through all the results and bind to the first we can + for (p = servinfo; p != NULL; p = p->ai_next) { + if ((sockfd = socket(p->ai_family, p->ai_socktype, + p->ai_protocol)) == -1) { + perror("server: socket"); + continue; + } + + if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (char*)&yes, + sizeof(int)) == -1) { + perror("setsockopt"); + exit(1); + } + + if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1) { + close(sockfd); + perror("server: bind"); + continue; + } + + break; + } + + freeaddrinfo(servinfo); // all done with this structure + + if (p == NULL) { + fprintf(stderr, "server: failed to bind\n"); + exit(1); + } + + if (listen(sockfd, BACKLOG) == -1) { + perror("listen"); + exit(1); + } } + + }; } \ No newline at end of file diff --git a/contrib/win32/w32-posix-prototype/win32posix/win32posix/socketio.c b/contrib/win32/w32-posix-prototype/win32posix/win32posix/socketio.c index b4924a5..4be7a84 100644 --- a/contrib/win32/w32-posix-prototype/win32posix/win32posix/socketio.c +++ b/contrib/win32/w32-posix-prototype/win32posix/win32posix/socketio.c @@ -24,6 +24,16 @@ static int getWSAErrno() return wsaerrno; } +int socketio_initialize() { + WSADATA wsaData = { 0 }; + int iResult = 0; + iResult = WSAStartup(MAKEWORD(2, 2), &wsaData); + if (iResult != 0) { + wprintf(L"WSAStartup failed: %d\n", iResult); + return iResult; + } +} + struct w32_io* socketio_socket(int domain, int type, int protocol) { struct w32_io *pio = (struct w32_io*)malloc(sizeof(struct w32_io)); if (!pio) { @@ -39,20 +49,66 @@ struct w32_io* socketio_socket(int domain, int type, int protocol) { return NULL; } + pio->type = SOCK_FD; return pio; } struct w32_io* socketio_accept(struct w32_io* pio, struct sockaddr* addr, int* addrlen) { - struct w32_io *accept_io = (struct w32_io*)malloc(sizeof(struct w32_io)); + struct w32_io *accept_io = NULL; + + accept_io = (struct w32_io*)malloc(sizeof(struct w32_io)); + if (!accept_io) + { + errno = ENOMEM; + return NULL; + } + + accept_io->sock = accept(pio->sock, addr, addrlen); + if (accept_io->sock == INVALID_SOCKET) { + errno = getWSAErrno(); + free(accept_io); + return NULL; + } + + pio->type = SOCK_FD; return accept_io; } -int socketio_setsockopt(struct w32_io* pio, int level, int optname, const char* optval, int optlen); -int socketio_getsockopt(struct w32_io* pio, int level, int optname, char* optval, int* optlen); -int socketio_getsockname(struct w32_io* pio, struct sockaddr* name, int* namelen); -int socketio_getpeername(struct w32_io* pio, struct sockaddr* name, int* namelen); -int socketio_listen(struct w32_io* pio, int backlog); -int socketio_bind(struct w32_io* pio, const struct sockaddr *name, int namelen); -int socketio_connect(struct w32_io* pio, const struct sockaddr* name, int namelen); -int socketio_shutdown(struct w32_io* pio, int how); \ No newline at end of file +int socketio_setsockopt(struct w32_io* pio, int level, int optname, const char* optval, int optlen) { + return setsockopt(pio->sock, level, optname, optval, optlen); +} + +int socketio_getsockopt(struct w32_io* pio, int level, int optname, char* optval, int* optlen) { + return getsockopt(pio->sock, level, optname, optval, optlen); +} + +int socketio_getsockname(struct w32_io* pio, struct sockaddr* name, int* namelen) { + return getsockname(pio->sock, name, namelen); +} + +int socketio_getpeername(struct w32_io* pio, struct sockaddr* name, int* namelen) { + return 0; +} + +int socketio_listen(struct w32_io* pio, int backlog) { + return listen(pio->sock, backlog); +} + +int socketio_bind(struct w32_io* pio, const struct sockaddr *name, int namelen) { + return bind(pio->sock, name, namelen); +} + +int socketio_connect(struct w32_io* pio, const struct sockaddr* name, int namelen) { + return connect(pio->sock, name, namelen); +} + +int socketio_shutdown(struct w32_io* pio, int how) { + return shutdown(pio->sock, how); +} + +int socketio_close(struct w32_io* pio) { + closesocket(pio->sock); + //todo- wait for pending io to abort + free(pio); +} \ No newline at end of file diff --git a/contrib/win32/w32-posix-prototype/win32posix/win32posix/w32fd.c b/contrib/win32/w32-posix-prototype/win32posix/win32posix/w32fd.c index 2260694..9e3ca5f 100644 --- a/contrib/win32/w32-posix-prototype/win32posix/win32posix/w32fd.c +++ b/contrib/win32/w32-posix-prototype/win32posix/win32posix/w32fd.c @@ -11,6 +11,7 @@ struct w32fd_table fd_table; int fd_table_initialize() { memset(&fd_table, 0, sizeof(fd_table)); //set stdin, stdout and stderr + return 0; } int fd_table_get_min_index() { @@ -44,10 +45,16 @@ void fd_table_set(struct w32_io* pio, int index) { void fd_table_clear(int index) { + struct w32_pio* pio = fd_table.w32fds[index]; + //pio->table_index = -1; fd_table.w32fds[index] = NULL; FD_SET(index, &(fd_table.occupied)); } +void w32posix_initialize() { + socketio_initialize(); +} + BOOL w32_io_is_blocking(struct w32_io* pio) { return (pio->fd_status_flags & O_NONBLOCK) ? TRUE : FALSE; @@ -67,9 +74,7 @@ int w32_socket(int domain, int type, int protocol) { return -1; } - pio->type = SOCK_FD; fd_table_set(pio, min_index); - return min_index; } @@ -89,7 +94,7 @@ int w32_accept(int fd, struct sockaddr* addr, int* addrlen) } fd_table_set(pio, min_index); - + return min_index; } int w32_setsockopt(int fd, int level, int optname, const char* optval, int optlen) { @@ -127,6 +132,7 @@ int w32_shutdown(int fd, int how) { int w32_close(int fd) { struct w32_io* pio = fd_table.w32fds[fd]; + fd_table_clear(pio->table_index); if ((pio->type == LISTEN_FD) || (pio->type == SOCK_FD)) { socketio_close(pio); } diff --git a/contrib/win32/w32-posix-prototype/win32posix/win32posix/w32fd.h b/contrib/win32/w32-posix-prototype/win32posix/win32posix/w32fd.h index ab51e87..411d0b4 100644 --- a/contrib/win32/w32-posix-prototype/win32posix/win32posix/w32fd.h +++ b/contrib/win32/w32-posix-prototype/win32posix/win32posix/w32fd.h @@ -46,8 +46,8 @@ int fd_table_initialize(); int fd_table_add(struct w32_io*); int fd_table_delete(struct w32_io*); +int socketio_initialize(); struct w32_io* socketio_socket(int domain, int type, int protocol); - struct w32_io* socketio_accept(struct w32_io* pio, struct sockaddr* addr, int* addrlen); int socketio_setsockopt(struct w32_io* pio, int level, int optname, const char* optval, int optlen); int socketio_getsockopt(struct w32_io* pio, int level, int optname, char* optval, int* optlen); diff --git a/contrib/win32/w32-posix-prototype/win32posix/win32posix/w32posix.h b/contrib/win32/w32-posix-prototype/win32posix/win32posix/w32posix.h index 51d1aa4..7b3b606 100644 --- a/contrib/win32/w32-posix-prototype/win32posix/win32posix/w32posix.h +++ b/contrib/win32/w32-posix-prototype/win32posix/win32posix/w32posix.h @@ -36,15 +36,20 @@ typedef struct w32_fd_set_ { #define socket w32_socket -/*w32 prototypes of posix functions*/ -FILE* w32_fdopen(int fd, const char *mode); -int w32_fstat(int fd, struct stat *buf); -int w32_isatty(int fd); - +void w32posix_initialize(); /*network i/o*/ +#define socket w32_socket +#define accept w32_accept +#define setsockopt w32_setsockopt +#define getsockopt w32_getsockopt +#define getsockname w32_getsockname +#define getpeername w32_getpeername +#define listen w32_listen +#define bind w32_bind +#define connect w32_connect +#define shutdown w32_shutdown int w32_socket(int domain, int type, int protocol); - int w32_accept(int fd, struct sockaddr* addr, int* addrlen); int w32_setsockopt(int fd, int level, int optname, const char* optval, int optlen); int w32_getsockopt(int fd, int level, int optname, char* optval, int* optlen); @@ -56,16 +61,30 @@ int w32_connect(int fd, const struct sockaddr* name, int namelen); int w32_shutdown(int fd, int how); /*non-network i/o*/ +#define pipe w32_pipe +#define open w32_open +#define creat w32_creat +#define read w32_read +#define write w32_write +#define fstat w32_fstat +#define isatty w32_isatty +#define fdopen w32_fdopen int w32_pipe(int *pfds); int w32_open(const char *pathname, int flags, ...); -int w32_wopen(const wchar_t *pathname, int flags, ...); int w32_creat(const char *pathname, int mode); int w32_read(int fd, void *dst, unsigned int max); int w32_write(int fd, const void *buf, unsigned int max); -int w32_close(int fd); +int w32_fstat(int fd, struct stat *buf); +int w32_isatty(int fd); +FILE* w32_fdopen(int fd, const char *mode); -/*operations on fds*/ -int w32_ioctl(int d, int request, ...); +/*common i/o*/ +#define close w32_close +#define select w32_select +#define fcntl w32_fcntl +#define dup w32_dup +#define dup2 w32_dup2 +int w32_close(int fd); int w32_select(int fds, fd_set* readfds, fd_set* writefds, fd_set* exceptfds, const struct timeval *timeout); int w32_fcntl(int fd, int cmd, ... /* arg */); int w32_dup(int oldfd);