From 43e1b0ab4fc5d8082d21c142d4f66204c02ef5d1 Mon Sep 17 00:00:00 2001 From: James Benton Date: Fri, 2 Jun 2017 11:52:30 +0100 Subject: [PATCH] Add UDP log server. --- tools/CMakeLists.txt | 1 + tools/udplogserver/CMakeLists.txt | 13 +++++ tools/udplogserver/main.cpp | 97 +++++++++++++++++++++++++++++++ 3 files changed, 111 insertions(+) create mode 100644 tools/udplogserver/CMakeLists.txt create mode 100644 tools/udplogserver/main.cpp diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt index fb4a5df..c2f7b66 100644 --- a/tools/CMakeLists.txt +++ b/tools/CMakeLists.txt @@ -7,3 +7,4 @@ include_directories("common") add_subdirectory(elf2rpl) add_subdirectory(readrpl) add_subdirectory(implcheck) +add_subdirectory(udplogserver) diff --git a/tools/udplogserver/CMakeLists.txt b/tools/udplogserver/CMakeLists.txt new file mode 100644 index 0000000..5ae37d8 --- /dev/null +++ b/tools/udplogserver/CMakeLists.txt @@ -0,0 +1,13 @@ +project(udplogserver) + +file(GLOB_RECURSE SOURCE_FILES *.cpp) +file(GLOB_RECURSE HEADER_FILES *.h) + +add_executable(udplogserver ${SOURCE_FILES} ${HEADER_FILES}) +set_target_properties(udplogserver PROPERTIES FOLDER tools) + +if (MSVC) + target_link_libraries(udplogserver PRIVATE ws2_32) +endif() + +install(TARGETS udplogserver RUNTIME DESTINATION "${CMAKE_INSTALL_PREFIX}/bin") diff --git a/tools/udplogserver/main.cpp b/tools/udplogserver/main.cpp new file mode 100644 index 0000000..ccbee65 --- /dev/null +++ b/tools/udplogserver/main.cpp @@ -0,0 +1,97 @@ +#ifdef _WIN32 +#include +#else +#include +#include +#include +#endif + +#include +#include + +#define SERVER_PORT 4405 + +int main(int argc, char **argv) +{ + struct sockaddr_in addr; + unsigned short port = SERVER_PORT; + + if (argc == 2) { + port = atoi(argv[1]); + } + +#ifdef _WIN32 + WSADATA wsaData; + if (WSAStartup(MAKEWORD(2, 2), &wsaData) == SOCKET_ERROR) { + return -1; + } +#endif + + // Create socket + auto fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); +#ifdef _WIN32 + if (fd == INVALID_SOCKET) { + WSACleanup(); +#else + if (fd < 0) { +#endif + return -1; + } + + // Set non blocking +#ifdef _WIN32 + u_long mode = 1; + ioctlsocket(fd, FIONBIO, &mode); +#else + int flags = fcntl(fd, F_GETFL, 0); + fcntl(fd, F_SETFL, flags | O_NONBLOCK); +#endif + + // Bind socket + memset(&addr, 0, sizeof(addr)); + addr.sin_family = AF_INET; + addr.sin_addr.s_addr = htonl(INADDR_ANY); + addr.sin_port = htons(port); + if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) { +#ifdef _WIN32 + closesocket(fd); + WSACleanup(); +#else + close(fd); +#endif + return -1; + } + + // Receive data + char buffer[2048]; + bool running = true; + + while (running) { + fd_set fdsRead; + FD_ZERO(&fdsRead); + FD_SET(fd, &fdsRead); + + struct timeval tv; + tv.tv_sec = 0; + tv.tv_usec = 10000; + + if (select(fd + 1, &fdsRead, NULL, NULL, &tv) == 1) { + struct sockaddr_in from; + int fromLen = sizeof(from); + int recvd = recvfrom(fd, buffer, sizeof(buffer), 0, (struct sockaddr *) &from, &fromLen); + + if (recvd > 0) { + buffer[recvd] = 0; + std::cout << buffer << std::endl; + } + } + } + +#ifdef _WIN32 + closesocket(fd); + WSACleanup(); +#else + close(fd); +#endif + return 0; +}