Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define WIN32_LEAN_AND_MEAN
- #include <inttypes.h>
- #include <stdbool.h>
- #include <stdint.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <vcruntime.h>
- #include <windows.h>
- #include <winsock2.h>
- #include <ws2tcpip.h>
- // Need to link with Ws2_32.lib, Mswsock.lib, and Advapi32.lib
- #pragma comment(lib, "Ws2_32.lib")
- #pragma comment(lib, "Mswsock.lib")
- #pragma comment(lib, "AdvApi32.lib")
- #define DEFAULT_BUFLEN 512
- #define DEFAULT_PORT "27015"
- #define lengthof(x) (sizeof(x) / sizeof((x)[0]))
- void *safemalloc(size_t size) {
- void *block = malloc(size);
- if (!block) {
- (void)fputs("Memory allocation has failed.", stderr);
- abort();
- }
- return block;
- }
- void *saferealloc(void *block, size_t size) {
- char *newBlock = realloc(block, size);
- if (!newBlock) {
- (void)fputs("Memory reallocation has failed.", stderr);
- abort();
- }
- return newBlock;
- }
- char *input(uint16_t maxLength) {
- if (!maxLength) {
- return NULL;
- }
- int32_t inputStringSize = maxLength + 2;
- char *inputString = safemalloc(inputStringSize);
- if (!fgets(inputString, inputStringSize, stdin)) {
- free(inputString);
- while (getchar() != '\n') {
- }
- return NULL;
- }
- if (inputString[0] == '\n') {
- free(inputString);
- return NULL;
- }
- size_t inputStringLength = strlen(inputString);
- char *lastStringCharacterPtr = &(inputString[inputStringLength - 1]);
- if (*lastStringCharacterPtr != '\n') {
- free(inputString);
- while (getchar() != '\n') {
- }
- return NULL;
- }
- *lastStringCharacterPtr = '\0';
- return saferealloc(inputString, inputStringLength);
- }
- uint64_t abs64(int64_t integer) { return integer < 0 ? -(uint64_t)integer : integer; }
- typedef struct {
- int64_t integer;
- const char *error;
- } StringToIntResult;
- StringToIntResult *stringToInt(const char *string) {
- StringToIntResult *result = safemalloc(sizeof(StringToIntResult));
- if (!string) {
- *result = (StringToIntResult){0, "Passed NULL."};
- return result;
- }
- if (string[0] == '\0') {
- *result = (StringToIntResult){0, "Invalid string."};
- return result;
- }
- bool startsWithNegativeSign = (string[0] == '-');
- if (startsWithNegativeSign) {
- string = string + 1;
- }
- uint64_t maxInt = abs64(startsWithNegativeSign ? INT64_MIN : INT64_MAX);
- uint8_t maxDigitNumber = snprintf( // NOLINT(clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling)
- NULL, 0, "%" PRIu64, maxInt);
- size_t stringLength = strlen(string);
- if (stringLength == 0 || string[0] == '0' || stringLength > maxDigitNumber) {
- *result = (StringToIntResult){0, "Invalid string."};
- return result;
- }
- for (uint32_t i = 0; i < stringLength; ++i) {
- if (string[i] < '0' || string[i] > '9') {
- *result = (StringToIntResult){0, "The string contains non numerical characters."};
- return result;
- }
- }
- if (stringLength == maxDigitNumber) {
- char *maxString = safemalloc(maxDigitNumber + 1);
- (void)snprintf(maxString, // NOLINT(clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling)
- maxDigitNumber + 1, "%" PRIu64, maxInt);
- for (uint32_t i = 0; i < stringLength; ++i) {
- if (string[i] > maxString[i]) {
- free(maxString);
- *result = (StringToIntResult){0, "The resulting integer is too large."};
- return result;
- }
- }
- free(maxString);
- }
- int64_t integer = strtoll(string, NULL, 10);
- if (startsWithNegativeSign) {
- integer = -integer;
- }
- *result = (StringToIntResult){integer, NULL};
- return result;
- }
- int __cdecl main(int argc, char **argv) {
- WSADATA wsaData;
- SOCKET ConnectSocket = INVALID_SOCKET;
- struct addrinfo *result = NULL, *ptr = NULL, hints;
- char recvbuf[DEFAULT_BUFLEN];
- int iResult;
- int recvbuflen = DEFAULT_BUFLEN;
- // Validate the parameters
- if (argc != 2) {
- printf("usage: %s server-name\n", argv[0]);
- return 1;
- }
- // Initialize Winsock
- iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
- if (iResult != 0) {
- printf("WSAStartup failed with error: %d\n", iResult);
- return 1;
- }
- ZeroMemory(&hints, sizeof(hints));
- hints.ai_family = AF_UNSPEC;
- hints.ai_socktype = SOCK_STREAM;
- hints.ai_protocol = IPPROTO_TCP;
- // Resolve the server address and port
- iResult = getaddrinfo(argv[1], DEFAULT_PORT, &hints, &result);
- if (iResult != 0) {
- printf("getaddrinfo failed with error: %d\n", iResult);
- WSACleanup();
- return 1;
- }
- // Attempt to connect to an address until one succeeds
- for (ptr = result; ptr != NULL; ptr = ptr->ai_next) {
- // Create a SOCKET for connecting to server
- ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);
- if (ConnectSocket == INVALID_SOCKET) {
- printf("socket failed with error: %ld\n", WSAGetLastError());
- WSACleanup();
- return 1;
- }
- // Connect to server.
- iResult = connect(ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
- if (iResult == SOCKET_ERROR) {
- closesocket(ConnectSocket);
- ConnectSocket = INVALID_SOCKET;
- continue;
- }
- break;
- }
- freeaddrinfo(result);
- if (ConnectSocket == INVALID_SOCKET) {
- printf("Unable to connect to server!\n");
- WSACleanup();
- return 1;
- }
- // Receive until the peer closes the connection
- do {
- char *inputString = input(9);
- if (!inputString) {
- puts("Invalid input");
- continue;
- }
- if (!strcmp("exit", inputString)) {
- free(inputString);
- puts("exiting connection");
- break;
- }
- StringToIntResult *result = stringToInt(inputString);
- free(inputString);
- if (result->error) {
- printf("%s\n", result->error);
- free(result);
- continue;
- }
- char sendbuf[DEFAULT_BUFLEN] = {0};
- sprintf(sendbuf, "%lld", result->integer);
- iResult = send(ConnectSocket, sendbuf, (int)strlen(sendbuf), 0);
- if (iResult == SOCKET_ERROR) {
- printf("send failed with error: %d\n", WSAGetLastError());
- closesocket(ConnectSocket);
- WSACleanup();
- return 1;
- }
- for (uint32_t i = 0; i < recvbuflen; ++i) {
- recvbuf[i] = '\0';
- }
- iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
- if (iResult > 0) {
- printf("Bytes received: %d\n", iResult);
- printf("%s", recvbuf);
- } else if (iResult == 0)
- printf("Connection closed\n");
- else
- printf("recv failed with error: %d\n", WSAGetLastError());
- } while (iResult > 0);
- // cleanup
- closesocket(ConnectSocket);
- WSACleanup();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement