This commit is contained in:
Manoj Ampalam 2016-02-29 15:17:25 -08:00
parent c2cbcaab0b
commit acebf2b325
5 changed files with 125 additions and 51 deletions

View File

@ -4,6 +4,10 @@
#define PORT "34912" #define PORT "34912"
#define BACKLOG 2 #define BACKLOG 2
int listen_fd, accept_fd, connect_fd, ret;
struct addrinfo hints,*servinfo;
fd_set read_set, write_set, except_set;
int int
unset_nonblock(int fd) unset_nonblock(int fd)
{ {
@ -43,31 +47,105 @@ set_nonblock(int fd)
} }
int listen_fd = -1; void socket_fd_tests()
int accept_fd = -1; {
int connect_fd = -1; fd_set set,*pset;
struct addrinfo *servinfo; pset = &set;
TEST_START("fd_set initial state");
FD_ZERO(pset);
ASSERT_CHAR_EQ(0, FD_ISSET(0, pset));
ASSERT_CHAR_EQ(0, FD_ISSET(1, pset));
ASSERT_CHAR_EQ(0, FD_ISSET(2, pset));
TEST_DONE();
TEST_START("FD_SET");
FD_SET(0, pset);
FD_SET(1, pset);
ASSERT_CHAR_EQ(1, FD_ISSET(0, pset));
ASSERT_CHAR_EQ(1, FD_ISSET(1, pset));
ASSERT_CHAR_EQ(0, FD_ISSET(2, pset));
TEST_DONE();
TEST_START("FD_CLR");
FD_CLR(0, pset);
ASSERT_CHAR_EQ(0, FD_ISSET(0, pset));
ASSERT_CHAR_EQ(1, FD_ISSET(1, pset));
ASSERT_CHAR_EQ(0, FD_ISSET(2, pset));
TEST_DONE();
TEST_START("FD_ZERO");
FD_ZERO(pset);
ASSERT_CHAR_EQ(0, FD_ISSET(0, pset));
ASSERT_CHAR_EQ(0, FD_ISSET(1, pset));
ASSERT_CHAR_EQ(0, FD_ISSET(2, pset));
TEST_DONE();
}
socket_syncio_tests()
{
TEST_START("BAD FDs");
ASSERT_INT_EQ(accept(-1, NULL, NULL), -1);
ASSERT_INT_EQ(errno, EBADF);
ASSERT_INT_EQ(setsockopt(MAX_FDS, 0, 0, NULL, 0), -1);
ASSERT_INT_EQ(errno, EBADF);
/*0,1,2 fd's are initialized */
ASSERT_INT_EQ(getsockopt(3, 0, 0, NULL, NULL), -1);
ASSERT_INT_EQ(errno, EBADF);
ASSERT_INT_EQ(getsockname(4, NULL, NULL), -1);
ASSERT_INT_EQ(errno, EBADF);
ASSERT_INT_EQ(getpeername(5, NULL, NULL), -1);
ASSERT_INT_EQ(errno, EBADF);
ASSERT_INT_EQ(listen(6, 2), -1);
ASSERT_INT_EQ(errno, EBADF);
ASSERT_INT_EQ(bind(7, NULL, 0), -1);
ASSERT_INT_EQ(errno, EBADF);
ASSERT_INT_EQ(connect(8, NULL, 0), -1);
ASSERT_INT_EQ(errno, EBADF);
ASSERT_INT_EQ(recv(9, NULL, 0, 0), -1);
ASSERT_INT_EQ(errno, EBADF);
ASSERT_INT_EQ(send(10, NULL, 0,0), -1);
ASSERT_INT_EQ(errno, EBADF);
ASSERT_INT_EQ(shutdown(11, 0), -1);
ASSERT_INT_EQ(errno, EBADF);
ASSERT_INT_EQ(read(MAX_FDS + 1, NULL, 0), -1);
ASSERT_INT_EQ(errno, EBADF);
ASSERT_INT_EQ(write(INFINITE, NULL, 0), -1);
ASSERT_INT_EQ(errno, EBADF);
ASSERT_INT_EQ(fstat(11, NULL), -1);
ASSERT_INT_EQ(errno, EBADF);
ASSERT_INT_EQ(isatty(12), -1);
ASSERT_INT_EQ(errno, EBADF);
ASSERT_INT_EQ(fdopen(13,NULL), -1);
ASSERT_INT_EQ(errno, EBADF);
ASSERT_INT_EQ(close(14), -1);
ASSERT_INT_EQ(errno, EBADF);
ASSERT_INT_EQ(fcntl(15, 1), -1);
ASSERT_INT_EQ(errno, EBADF);
ASSERT_INT_EQ(dup(16), -1);
ASSERT_INT_EQ(errno, EBADF);
ASSERT_INT_EQ(dup2(17, 18), -1);
ASSERT_INT_EQ(errno, EBADF);
FD_ZERO(&read_set);
FD_SET(20, &read_set);
ASSERT_INT_EQ(select(21, &read_set, NULL, NULL, NULL), -1);
ASSERT_INT_EQ(errno, EBADF);
FD_ZERO(&write_set);
FD_SET(21, &write_set);
ASSERT_INT_EQ(select(22, NULL, &write_set, NULL, NULL), -1);
ASSERT_INT_EQ(errno, EBADF);
TEST_DONE();
}
void socket_tests() void socket_tests()
{ {
TEST_START("test 1");
ASSERT_INT_EQ(1, 1);
w32posix_initialize(); w32posix_initialize();
TEST_DONE(); socket_fd_tests();
socket_syncio_tests();
TEST_START("test 1"); w32posix_done();
ASSERT_INT_EQ(1, 0);
TEST_DONE();
TEST_START("test 1");
ASSERT_INT_EQ(1, 1);
TEST_DONE();
TEST_START("test 1");
ASSERT_INT_EQ(1, 1);
TEST_DONE();
return;
} }
int socket_prepare(char* ip) int socket_prepare(char* ip)

View File

@ -5,11 +5,6 @@ VisualStudioVersion = 14.0.23107.0
MinimumVisualStudioVersion = 10.0.40219.1 MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "win32posix", "win32posix\win32posix.vcxproj", "{D8744F47-1741-4FB8-97D3-EBB9C3A13E67}" Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "win32posix", "win32posix\win32posix.vcxproj", "{D8744F47-1741-4FB8-97D3-EBB9C3A13E67}"
EndProject EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Tests", "Tests\Tests.vcxproj", "{76AFACE0-9135-4D82-9A65-3B82084211E6}"
ProjectSection(ProjectDependencies) = postProject
{D8744F47-1741-4FB8-97D3-EBB9C3A13E67} = {D8744F47-1741-4FB8-97D3-EBB9C3A13E67}
EndProjectSection
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "SampleServer", "SampleServer\SampleServer.vcxproj", "{D52F8255-C3A9-4416-A0A6-8CE63A4D7E43}" Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "SampleServer", "SampleServer\SampleServer.vcxproj", "{D52F8255-C3A9-4416-A0A6-8CE63A4D7E43}"
ProjectSection(ProjectDependencies) = postProject ProjectSection(ProjectDependencies) = postProject
{D8744F47-1741-4FB8-97D3-EBB9C3A13E67} = {D8744F47-1741-4FB8-97D3-EBB9C3A13E67} {D8744F47-1741-4FB8-97D3-EBB9C3A13E67} = {D8744F47-1741-4FB8-97D3-EBB9C3A13E67}
@ -36,14 +31,6 @@ Global
{D8744F47-1741-4FB8-97D3-EBB9C3A13E67}.Release|Win32.Build.0 = Release|Win32 {D8744F47-1741-4FB8-97D3-EBB9C3A13E67}.Release|Win32.Build.0 = Release|Win32
{D8744F47-1741-4FB8-97D3-EBB9C3A13E67}.Release|x64.ActiveCfg = Release|x64 {D8744F47-1741-4FB8-97D3-EBB9C3A13E67}.Release|x64.ActiveCfg = Release|x64
{D8744F47-1741-4FB8-97D3-EBB9C3A13E67}.Release|x64.Build.0 = Release|x64 {D8744F47-1741-4FB8-97D3-EBB9C3A13E67}.Release|x64.Build.0 = Release|x64
{76AFACE0-9135-4D82-9A65-3B82084211E6}.Debug|Win32.ActiveCfg = Debug|Win32
{76AFACE0-9135-4D82-9A65-3B82084211E6}.Debug|Win32.Build.0 = Debug|Win32
{76AFACE0-9135-4D82-9A65-3B82084211E6}.Debug|x64.ActiveCfg = Debug|x64
{76AFACE0-9135-4D82-9A65-3B82084211E6}.Debug|x64.Build.0 = Debug|x64
{76AFACE0-9135-4D82-9A65-3B82084211E6}.Release|Win32.ActiveCfg = Release|Win32
{76AFACE0-9135-4D82-9A65-3B82084211E6}.Release|Win32.Build.0 = Release|Win32
{76AFACE0-9135-4D82-9A65-3B82084211E6}.Release|x64.ActiveCfg = Release|x64
{76AFACE0-9135-4D82-9A65-3B82084211E6}.Release|x64.Build.0 = Release|x64
{D52F8255-C3A9-4416-A0A6-8CE63A4D7E43}.Debug|Win32.ActiveCfg = Debug|Win32 {D52F8255-C3A9-4416-A0A6-8CE63A4D7E43}.Debug|Win32.ActiveCfg = Debug|Win32
{D52F8255-C3A9-4416-A0A6-8CE63A4D7E43}.Debug|Win32.Build.0 = Debug|Win32 {D52F8255-C3A9-4416-A0A6-8CE63A4D7E43}.Debug|Win32.Build.0 = Debug|Win32
{D52F8255-C3A9-4416-A0A6-8CE63A4D7E43}.Debug|x64.ActiveCfg = Debug|x64 {D52F8255-C3A9-4416-A0A6-8CE63A4D7E43}.Debug|x64.ActiveCfg = Debug|x64

View File

@ -5,7 +5,7 @@
FILE* log; FILE* log;
void debug_initialize() { int debug_initialize() {
char filename[MAX_PATH]; char filename[MAX_PATH];
int len = 0; int len = 0;
SYSTEMTIME time; SYSTEMTIME time;
@ -16,6 +16,7 @@ void debug_initialize() {
sprintf(filename + len, "_%d_%d_%d.log", time.wHour, time.wMinute, time.wSecond); sprintf(filename + len, "_%d_%d_%d.log", time.wHour, time.wMinute, time.wSecond);
//sprintf(filename, "%s", "e:\\tmp.log"); //sprintf(filename, "%s", "e:\\tmp.log");
log = fopen(filename, "w"); log = fopen(filename, "w");
return 0;
} }
void debug_done() { void debug_done() {

View File

@ -4,6 +4,6 @@
#define debug2(cformat, ...) write_log(__FILE__, __FUNCTION__, __LINE__, cformat, __VA_ARGS__) #define debug2(cformat, ...) write_log(__FILE__, __FUNCTION__, __LINE__, cformat, __VA_ARGS__)
#define debug3(cformat, ...) write_log(__FILE__, __FUNCTION__, __LINE__, cformat, __VA_ARGS__) #define debug3(cformat, ...) write_log(__FILE__, __FUNCTION__, __LINE__, cformat, __VA_ARGS__)
void debug_initialize(); int debug_initialize();
void debug_done(); void debug_done();
void write_log(const char *source_name,const char *function_name, int line_num, const char *fmt, ...); void write_log(const char *source_name,const char *function_name, int line_num, const char *fmt, ...);

View File

@ -27,7 +27,7 @@ int fd_table_initialize() {
return 0; return 0;
} }
int fd_table_get_min_index() { static int fd_table_get_min_index() {
int min_index = 0; int min_index = 0;
unsigned char* bitmap = fd_table.occupied.bitmap; unsigned char* bitmap = fd_table.occupied.bitmap;
unsigned char tmp; unsigned char tmp;
@ -54,13 +54,13 @@ int fd_table_get_min_index() {
return min_index; return min_index;
} }
void fd_table_set(struct w32_io* pio, int index) { static void fd_table_set(struct w32_io* pio, int index) {
fd_table.w32_ios[index] = pio; fd_table.w32_ios[index] = pio;
pio->table_index = index; pio->table_index = index;
FD_SET(index, &(fd_table.occupied)); FD_SET(index, &(fd_table.occupied));
} }
void fd_table_clear(int index) static void fd_table_clear(int index)
{ {
fd_table.w32_ios[index]->table_index = -1; fd_table.w32_ios[index]->table_index = -1;
fd_table.w32_ios[index] = NULL; fd_table.w32_ios[index] = NULL;
@ -68,9 +68,10 @@ void fd_table_clear(int index)
} }
void w32posix_initialize() { void w32posix_initialize() {
debug_initialize(); if ((debug_initialize() != 0)
fd_table_initialize(); || (fd_table_initialize() != 0)
socketio_initialize(); || (socketio_initialize() != 0))
abort();
} }
void w32posix_done() { void w32posix_done() {
@ -95,7 +96,7 @@ BOOL w32_io_is_io_available(struct w32_io* pio, BOOL rd) {
int w32_io_on_select(struct w32_io* pio, BOOL rd) int w32_io_on_select(struct w32_io* pio, BOOL rd)
{ {
if ((pio->type == LISTEN_FD) || (pio->type == SOCK_FD)) { if ((pio->type <= SOCK_FD)) {
return socketio_on_select(pio, rd); return socketio_on_select(pio, rd);
} }
else { else {
@ -104,10 +105,21 @@ int w32_io_on_select(struct w32_io* pio, BOOL rd)
} }
#define CHECK_FD(fd) do { \
errno = 0; \
if ((fd < 0) || (fd > MAX_FDS - 1) || fd_table.w32_ios[fd] == NULL) { \
errno = EBADF; \
debug("bad fd: %d", fd); \
return -1; \
} \
} while (0)
int w32_socket(int domain, int type, int protocol) { int w32_socket(int domain, int type, int protocol) {
int min_index = fd_table_get_min_index(); int min_index = fd_table_get_min_index();
struct w32_io* pio = NULL; struct w32_io* pio = NULL;
errno = 0;
if (min_index == -1) if (min_index == -1)
return -1; return -1;
@ -124,6 +136,7 @@ int w32_socket(int domain, int type, int protocol) {
int w32_accept(int fd, struct sockaddr* addr, int* addrlen) int w32_accept(int fd, struct sockaddr* addr, int* addrlen)
{ {
debug3("fd:%d", fd); debug3("fd:%d", fd);
CHECK_FD(fd);
int min_index = fd_table_get_min_index(); int min_index = fd_table_get_min_index();
struct w32_io* pio = NULL; struct w32_io* pio = NULL;
@ -140,15 +153,6 @@ int w32_accept(int fd, struct sockaddr* addr, int* addrlen)
return min_index; return min_index;
} }
#define CHECK_FD(fd) \
do { \
if ((fd > MAX_FDS - 1) || fd_table.w32_ios[fd] == NULL) { \
errno = EBADF; \
debug("bad fd: %d", fd); \
return -1; \
} \
} while (0)
int w32_setsockopt(int fd, int level, int optname, const char* optval, int optlen) { int w32_setsockopt(int fd, int level, int optname, const char* optval, int optlen) {
debug3("fd:%d", fd); debug3("fd:%d", fd);
CHECK_FD(fd); CHECK_FD(fd);
@ -216,6 +220,7 @@ int w32_pipe(int *pfds){
int read_index, write_index; int read_index, write_index;
struct w32_io* pio[2]; struct w32_io* pio[2];
errno = 0;
read_index = fd_table_get_min_index(); read_index = fd_table_get_min_index();
if (read_index == -1) if (read_index == -1)
return -1; return -1;
@ -243,6 +248,7 @@ int w32_open(const char *pathname, int flags, ...) {
int min_index = fd_table_get_min_index(); int min_index = fd_table_get_min_index();
struct w32_io* pio; struct w32_io* pio;
errno = 0;
if (min_index == -1) if (min_index == -1)
return -1; return -1;
@ -280,6 +286,7 @@ int w32_isatty(int fd) {
} }
FILE* w32_fdopen(int fd, const char *mode) { FILE* w32_fdopen(int fd, const char *mode) {
errno = 0;
if ((fd > MAX_FDS - 1) || fd_table.w32_ios[fd] == NULL) { if ((fd > MAX_FDS - 1) || fd_table.w32_ios[fd] == NULL) {
errno = EBADF; errno = EBADF;
debug("bad fd: %d", fd); debug("bad fd: %d", fd);
@ -339,6 +346,7 @@ int w32_select(int fds, fd_set* readfds, fd_set* writefds, fd_set* exceptfds, co
unsigned int time_milliseconds = timeout->tv_sec * 100 + timeout->tv_usec / 1000; unsigned int time_milliseconds = timeout->tv_sec * 100 + timeout->tv_usec / 1000;
ULONGLONG ticks_start = GetTickCount64(), ticks_now; ULONGLONG ticks_start = GetTickCount64(), ticks_now;
errno = 0;
memset(&read_ready_fds, 0, sizeof(fd_set)); memset(&read_ready_fds, 0, sizeof(fd_set));
memset(&write_ready_fds, 0, sizeof(fd_set)); memset(&write_ready_fds, 0, sizeof(fd_set));