Skip to content

Commit

Permalink
Adding Windows support
Browse files Browse the repository at this point in the history
  • Loading branch information
Eldar Shalev committed Jul 20, 2022
1 parent 943667c commit 1ba0d48
Show file tree
Hide file tree
Showing 11 changed files with 89 additions and 30 deletions.
15 changes: 13 additions & 2 deletions src/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ void printHistogram(uint32_t binSize, std::map<uint32_t, uint32_t> &activeBins,
#else
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE),&csbi);
terminal_width = csbi.dwSize.X;
terminalWidth = csbi.dwSize.X;
#endif
for(itr = activeBins.begin(); itr != activeBins.end(); ++itr) {
currFrequency = itr->second;
Expand Down Expand Up @@ -826,7 +826,9 @@ static int _connect_check(int ifd) {

#ifdef WIN32
fd_set rfds, wfds;
struct timeval tv = { .tv_sec = 0, .tv_usec = POLL_TIMEOUT_MS * 1000, };
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = POLL_TIMEOUT_MS * 1000;

FD_ZERO(&rfds);
FD_ZERO(&wfds);
Expand Down Expand Up @@ -881,6 +883,7 @@ int Client<IoType, SwitchDataIntegrity, SwitchActivityInfo, SwitchCycleDuration,
std::string hostport = sockaddr_to_hostport(p_client_bind_addr);
struct sockaddr_store_t unix_addr;
socklen_t unix_addr_len;
#ifndef WIN32// AF_UNIX with DGRAM isn't supported in Win32
if (p_client_bind_addr->ss_family == AF_UNIX && g_pApp->m_const_params.sock_type == SOCK_DGRAM) { // Need to bind localy
if (p_client_bind_addr->addr_un.sun_path[0] == 0) { // no specific addr client_info was provoided
std::string sun_path = build_client_socket_name(&s_user_params.addr.addr_un, getpid(), ifd);
Expand All @@ -900,6 +903,7 @@ int Client<IoType, SwitchDataIntegrity, SwitchActivityInfo, SwitchCycleDuration,
client_bind_addr_len = unix_addr_len;
}
}
#endif
log_dbg("[fd=%d] Binding to: %s...", ifd, hostport.c_str());
if (bind(ifd, reinterpret_cast<const sockaddr *>(p_client_bind_addr), client_bind_addr_len) < 0) {
log_err("[fd=%d] Can`t bind socket %s", ifd, hostport.c_str());
Expand Down Expand Up @@ -1078,9 +1082,16 @@ void Client<IoType, SwitchDataIntegrity, SwitchActivityInfo, SwitchCycleDuration
// Disarm timer
timer.it_value.tv_sec = 0;
timer.it_value.tv_usec = 0;

#ifdef WIN32
if (SetTimer(NULL, 0, 0, NULL)) {
log_err("ERROR: SetTimer() failed when disarming");
}
#else
if (setitimer(ITIMER_REAL, &timer, NULL)) {
log_err("ERROR: setitimer() failed when disarming");
}
#endif
log_msg("Test end (reached packet number)");
g_b_exit = true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ std::string sockaddr_to_hostport(const struct sockaddr *addr)
if (addr->sa_family == AF_INET6) {
return "[" + std::string(hbuf) + "]:" + std::string(pbuf);
} else if (addr->sa_family == AF_UNIX) {
return std::string(pbuf) + " [UNIX]";
return std::string(addr->sa_data) + " [AF_UNIX]";
} else {
return std::string(hbuf) + ":" + std::string(pbuf);
}
Expand Down
6 changes: 4 additions & 2 deletions src/defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@

#ifdef WIN32
#include <WS2tcpip.h>
#include <sys/types.h>
#include <Winsock2.h>
#include <unordered_map>
#include <Winbase.h>
#include <stdint.h>
#include <afunix.h>
typedef unsigned short int sa_family_t;

#else

Expand All @@ -60,7 +62,6 @@
#include <stdint.h>
#include <unordered_map>
#include <unistd.h> /* getopt() and sleep()*/
#include <inttypes.h> /* printf PRItn */
#include <poll.h>
#include <pthread.h>
#include <sys/time.h> /* timers*/
Expand All @@ -75,6 +76,7 @@

#endif

#include <inttypes.h> /* printf PRItn */
#include <stdlib.h> /* random()*/
#include <string.h>
#include <stdio.h>
Expand Down
2 changes: 1 addition & 1 deletion src/iohandlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ static void print_addresses(const fds_data *data, int &list_count)
NI_NUMERICHOST | NI_NUMERICSERV);
switch (data->server_addr.ss_family) {
case AF_UNIX:
printf("[%2d] Address is %s # UDS type is %s\n", list_count++, pbuf, PRINT_SOCKET_TYPE(data->sock_type));
printf("[%2d] Address is %s # UDS type is %s\n", list_count++, data->server_addr.addr_un.sun_path, PRINT_SOCKET_TYPE(data->sock_type));
break;
default:
printf("[%2d] IP = %-15s PORT = %5s # %s\n", list_count++, hbuf, pbuf, PRINT_PROTOCOL(data->sock_type));
Expand Down
8 changes: 8 additions & 0 deletions src/ip_address.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,13 @@
#include "ip_address.h"
#include "common.h"

#ifdef WIN32
#include <ws2tcpip.h>
#else
#include <arpa/inet.h>
#include <netdb.h>
#include <string.h>
#endif

IPAddress::IPAddress(const IPAddress &rhs) : m_family(rhs.m_family), m_addr6(rhs.m_addr6), m_addr_un(rhs.m_addr_un)
{
Expand Down Expand Up @@ -100,7 +104,11 @@ bool IPAddress::resolve(const char *str, IPAddress &out, std::string &err)
}
}
} else {
#ifdef WIN32
err = gai_strerrorA(res);
#else
err = gai_strerror(res);
#endif
}
freeaddrinfo(result);

Expand Down
14 changes: 14 additions & 0 deletions src/ip_address.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,24 @@
#ifndef IP_ADDRESS_H_
#define IP_ADDRESS_H_

#ifdef WIN32
#include <WS2tcpip.h>
#include <Winsock2.h>
#include <unordered_map>
#include <Winbase.h>
#include <stdint.h>
#include <afunix.h>
typedef unsigned short int sa_family_t;

#else
#ifdef __linux__
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/un.h> /* definitions for UNIX domain sockets */

#endif
#endif

#include <functional>
#include <string>

Expand Down
5 changes: 2 additions & 3 deletions src/os_abstract.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@
* OF SUCH DAMAGE.
*/

#include <stdio.h>
#include "os_abstract.h"

#include <string.h>
#include <stdio.h>

void os_printf_backtrace(void) {
#ifdef WIN32
Expand All @@ -51,7 +50,7 @@ void os_printf_backtrace(void) {
for (i = 0; i < frames; i++) {
SymFromAddr(process, (DWORD64)(stack[i]), 0, symbol);

printf("%i: %s - 0x%0X\n", frames - i - 1, symbol->Name, symbol->Address);
printf("%i: %s - 0x%llu\n", frames - i - 1, symbol->Name, symbol->Address);
}

free(symbol);
Expand Down
43 changes: 36 additions & 7 deletions src/sockperf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1714,7 +1714,13 @@ static int resolve_sockaddr(const char *host, const char *port, int sock_type,
bool is_server_mode, sockaddr *addr, socklen_t &addr_len)
{
struct addrinfo hints;
char os_directory_path;
#ifdef WIN32
os_directory_path = '\\';
#else
os_directory_path = '/';

#endif
memset(&hints, 0, sizeof(hints));
// allow IPv4 or IPv6
hints.ai_family = AF_UNSPEC;
Expand All @@ -1731,10 +1737,19 @@ static int resolve_sockaddr(const char *host, const char *port, int sock_type,
int res;
struct addrinfo *result;

#ifdef WIN32
WSAData wsaData;
if ((res = WSAStartup(MAKEWORD(2, 2), &wsaData)) != 0) {
log_err("WSAStartup failed %d\n", res);
res = SOCKPERF_ERR_SOCKET;
return res;
}

#endif
if (host != NULL) {
std::string path = host;
struct sockaddr_store_t *tmp = ((sockaddr_store_t *)addr);
if (path.size() > 0 && path[0] == '/') {
if (path.size() > 0 && path[0] == os_directory_path) {
log_dbg("provided path for Unix Domain Socket is %s\n", host);
if (path.length() >= MAX_UDS_NAME) {
log_err("length of name is larger than %d bytes", MAX_UDS_NAME);
Expand Down Expand Up @@ -1830,17 +1845,17 @@ static int parse_common_opt(const AOPT_OBJECT *common_obj) {
#endif
if (!strcmp(fd_handle_type, "poll") || !strcmp(fd_handle_type, "p")) {
s_user_params.fd_handler_type = POLL;
} else if (!strcmp(fd_handle_type, "socketxtreme") ||
!strcmp(fd_handle_type, "x")) {
s_user_params.fd_handler_type = SOCKETXTREME;
s_user_params.is_blocked = false;
} else
#endif
if (!strcmp(fd_handle_type, "select") || !strcmp(fd_handle_type, "s")) {
s_user_params.fd_handler_type = SELECT;
} else if (!strcmp(fd_handle_type, "recvfrom") ||
!strcmp(fd_handle_type, "r")) {
s_user_params.fd_handler_type = RECVFROMMUX;
} else if (!strcmp(fd_handle_type, "socketxtreme") ||
!strcmp(fd_handle_type, "x")) {
s_user_params.fd_handler_type = SOCKETXTREME;
s_user_params.is_blocked = false;
} else {
log_msg("'-%c' Invalid muliply io hanlde type: %s", 'F', optarg);
rc = SOCKPERF_ERR_BAD_ARGUMENT;
Expand Down Expand Up @@ -2337,7 +2352,12 @@ void cleanup() {
if (g_fds_array) {
for (ifd = 0; ifd <= s_fd_max; ifd++) {
if (g_fds_array[ifd]) {
#ifdef WIN32
closesocket(ifd);
WSACleanup();
#else
close(ifd);
#endif
if (g_fds_array[ifd]->active_fd_list) {
FREE(g_fds_array[ifd]->active_fd_list);
}
Expand All @@ -2349,13 +2369,19 @@ void cleanup() {
}
if (s_user_params.addr.ss_family == AF_UNIX) {
unlink(s_user_params.client_bind_info.addr_un.sun_path);
#ifndef WIN32 // AF_UNIX with DGRAM isn't supported in Win32
if (s_user_params.mode == MODE_CLIENT && s_user_params.sock_type == SOCK_DGRAM) { // unlink binded client
std::string sun_path = build_client_socket_name(&s_user_params.addr.addr_un, getpid(), ifd);
log_dbg("unlinking %s", sun_path.c_str());
unlink(sun_path.c_str());
}
#endif
if (s_user_params.mode == MODE_SERVER)
#ifdef WIN32
DeleteFileA(g_fds_array[ifd]->server_addr.addr_un.sun_path);
#else
unlink(g_fds_array[ifd]->server_addr.addr_un.sun_path);
#endif
}
delete g_fds_array[ifd];
}
Expand Down Expand Up @@ -3061,7 +3087,7 @@ static int set_sockets_from_feedfile(const char *feedfile_name) {
} else {
sock_type = SOCK_DGRAM;
}

#ifndef WIN32
if (sock_type == SOCK_DGRAM && s_user_params.mode == MODE_CLIENT) {
if (s_user_params.fd_handler_type == SOCKETXTREME &&
is_unspec_addr(s_user_params.client_bind_info)) {
Expand All @@ -3071,6 +3097,7 @@ static int set_sockets_from_feedfile(const char *feedfile_name) {
break;
}
}
#endif

std::unique_ptr<fds_data> tmp{ new fds_data };

Expand Down Expand Up @@ -3332,6 +3359,7 @@ int bringup(const int *p_daemonize) {
/* Setup VMA */
int _vma_pkts_desc_size = 0;

#ifndef WIN32
#ifdef USING_VMA_EXTRA_API
if (!rc && (s_user_params.is_rxfiltercb || s_user_params.is_zcopyread ||
s_user_params.fd_handler_type == SOCKETXTREME)) {
Expand All @@ -3349,11 +3377,12 @@ int bringup(const int *p_daemonize) {
}
#else
if (!rc && (s_user_params.is_rxfiltercb || s_user_params.is_zcopyread ||
s_user_params.fd_handler_type == SOCKETXTREME)) {
s_user_params.fd_handler_type == SOCKETXTREME)) {
errno = EPERM;
exit_with_err("Please compile with VMA Extra API to use these options", SOCKPERF_ERR_FATAL);
}
#endif
#endif

#if defined(DEFINED_TLS)
rc = tls_init();
Expand Down
3 changes: 1 addition & 2 deletions src/ticks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,9 @@

#define __STDC_LIMIT_MACROS // for INT64_MAX in __cplusplus

#include <stdio.h>
#include "ticks.h"

#include <string>
#include <stdio.h>
#include <errno.h>
#include <string.h> // strerror()
#include <stdint.h> // INT64_MAX
Expand Down
5 changes: 0 additions & 5 deletions src/ticks_os.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,6 @@ typedef int64_t ticks_t;
#define usleep(x) Sleep(x / 1000)
#define snprintf _snprintf

struct timespec {
time_t tv_sec; // Seconds.
long int tv_nsec; // Nanoseconds.
};

#endif

/**
Expand Down
16 changes: 9 additions & 7 deletions win/project/sockperf.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,26 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v110</PlatformToolset>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v110</PlatformToolset>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v110</PlatformToolset>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v110</PlatformToolset>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
Expand Down Expand Up @@ -150,9 +150,10 @@
<ClCompile Include="..\..\src\common.cpp" />
<ClCompile Include="..\..\src\Defs.cpp" />
<ClCompile Include="..\..\src\IoHandlers.cpp" />
<ClCompile Include="..\..\src\ip_address.cpp" />
<ClCompile Include="..\..\src\Message.cpp" />
<ClCompile Include="..\..\src\os_abstract.cpp" />
<ClCompile Include="..\..\src\PacketTimes.cpp" />
<ClCompile Include="..\..\src\packet.cpp" />
<ClCompile Include="..\..\src\Playback.cpp" />
<ClCompile Include="..\..\src\Server.cpp" />
<ClCompile Include="..\..\src\SockPerf.cpp" />
Expand All @@ -164,10 +165,11 @@
<ClInclude Include="..\..\src\clock.h" />
<ClInclude Include="..\..\src\common.h" />
<ClInclude Include="..\..\src\Defs.h" />
<ClInclude Include="..\..\src\input_handlers.h" />
<ClInclude Include="..\..\src\IoHandlers.h" />
<ClInclude Include="..\..\src\ip_address.h" />
<ClInclude Include="..\..\src\Message.h" />
<ClInclude Include="..\..\src\os_abstract.h" />
<ClInclude Include="..\..\src\PacketTimes.h" />
<ClInclude Include="..\..\src\Playback.h" />
<ClInclude Include="..\..\src\Server.h" />
<ClInclude Include="..\..\src\Switches.h" />
Expand All @@ -180,4 +182,4 @@
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
</Project>

0 comments on commit 1ba0d48

Please sign in to comment.