WINAPI - WinSock TCP Wrapper
by putrid - Thursday June 13, 2024 at 04:43 AM
#1
What is Winsock?
Winsock (Windows Sockets API) is a technical specification that defines how Windows network software should access network services, especially TCP/IP. This wrapper simplifies the use of Winsock functions for common networking tasks.

Source Code:
#include <winsock2.h> #include <stdio.h> #pragma comment(lib, "ws2_32.lib") /* Defines */ #define CONNECTION_DEAD -1 #define CONNECTION_ALIVE 1 #define CONNECTION_NN    0      //nothing new typedef INT WINERROR; #define ERROR_UNSUCCESSFULL 0xffffffff /* Structures */ typedef struct {     PCHAR  pData;          //pointer to allocated data     USHORT Length;          //length of allocated data } DATA_CONTEXT, *PDATA_CONTEXT; typedef struct {     DATA_CONTEXT Ctx;      //data structure     USHORT      DataLength;    //total length of incoming data     CHAR        Buffer[2];    //buffer saving data from incomplete stream     SHORT        BufferLength;  //length of saved data } INCOMING_DATA, *PINCOMING_DATA; /* *  SocketInitialize: Initialize Winsock */ INT SocketInitialize() {     WSADATA wsa;     if (WSAStartup(0x202, &wsa) != 0)         return -1;     return 0; } /* *  SocketConnect: Send data by length to target socket file descriptor *      @Socket - Target socket file descriptor *      @Host  - Target host *      @Port  - Target port */ static SOCKET SocketConnect(     SOCKET Socket,     PCHAR Host,     PCHAR Port ) {     struct sockaddr_in Address;     Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);     if (Socket == SOCKET_ERROR)         return -1;     Address.sin_addr.s_addr = inet_addr(Host);     Address.sin_port    = htons(atoi(Port));     Address.sin_family  = AF_INET;     if (connect(Socket, (struct sockaddr *)&Address, sizeof(struct sockaddr_in)) == SOCKET_ERROR)         return -1;     return Socket; } /* *  SocketRead: Receive exact amount of incoming data *      @Socket - Target socket file descriptor *      @Data  - Pointer to data context structure */ WINERROR SocketRead(     SOCKET      Socket,     PINCOMING_DATA  Data ) {     WINERROR Status = NO_ERROR;     SHORT Read;     fd_set FD;     do     {         FD_ZERO(&FD);         FD_SET(Socket, &FD);         //poll fd for data         if (select(0, &FD, NULL, NULL, NULL) == SOCKET_ERROR)         {             printf("Error: select() (%d)\n", GetLastError());             Status = CONNECTION_DEAD;             break;         }         //check for pending incomming data         if (Data->DataLength == 0)         {             //receive the length of incomming data             if (Data->BufferLength == 0)                 Read = recv(Socket, Data->Buffer, 2, 0);             else                 Read = recv(Socket, &Data->Buffer[1], 1, 0);                         if (Read <= 0)             {                 Status = CONNECTION_DEAD;                 break;             }             //check for 2 bytes, then allocate enough space for actual data             Data->BufferLength += Read;             if (Data->BufferLength == 2)             {                 Data->DataLength = *(USHORT *)Data->Buffer;                 if (Data->DataLength == 0)                 {                     //no new data                     Data->BufferLength = 0;                     Status = CONNECTION_NN;                     break;                 }                 //allocate enough memory data                 Data->Ctx.pData = (PCHAR)malloc(Data->DataLength);                 if (Data->Ctx.pData == NULL)                 {                     Status = ERROR_NOT_ENOUGH_MEMORY;                     break;                 }             }         }         else         {             //receive actual data             Read = recv(Socket, Data->Ctx.pData + Data->Ctx.Length, Data->DataLength, 0);             if (Read <= 0)             {                 Status = CONNECTION_DEAD;                 break;             }                         Data->Ctx.pData[Read] = 0;             Data->Ctx.Length += Read;                         if (Data->Ctx.Length == Data->DataLength)             {                 Status = CONNECTION_ALIVE;                 break;             }         }     } while (FALSE);     return (Status); } /* *  SocketSend: Send data by length to target socket file descriptor *      @Socket - Target socket file descriptor *      @Buffer - Data to be sent *      @Length - Exact length of data */ WINERROR SocketSend(     SOCKET  Socket,     PCHAR  Buffer,     USHORT  Length ) {     WINERROR Status = NO_ERROR;     do     {         //send length of data         if (send(Socket, (PCHAR)&Length, 2, 0) == SOCKET_ERROR)         {             Status = ERROR_UNSUCCESSFULL;             break;         }                 //send actual data         if (send(Socket, Buffer, Length, 0) == SOCKET_ERROR)         {             Status = ERROR_UNSUCCESSFULL;             break;         }     } while (FALSE);     return (Status); } /* *  SocketResetData: Reset socket data context structure *      @Data - Pointer to INCOMING_DATA context structure */ VOID SocketResetData(     PINCOMING_DATA Data ) {     if (Data->Ctx.pData == NULL)         return;     if (Data->DataLength > 0)     {         free(Data->Ctx.pData);         Data->Ctx.Length = 0;         Data->DataLength = 0;     }     Data->BufferLength = 0; } /* *  SocketShutdown: Shutdown and clean-up socket *      @Socket - Target socket file descriptor *      @Data  - Pointer to INCOMING_DATA context structure */ VOID SocketShutdown(     SOCKET Socket,     PINCOMING_DATA Data ) {     SocketResetData(&Data);     WSACleanup();     shutdown(Socket, SD_BOTH);     closesocket(Socket); }

How To Use:
  • Compile the code using a C compiler (such as Visual Studio, or mingw).
  • Ensure you have the Winsock library linked to the compiler.

Example:
int main() {     if (SocketInitialize() != 0) {         printf("Failed to initialize Winsock.\n");         return -1;     }     SOCKET sock;     sock = SocketConnect(sock, "127.0.0.1", "8080");     if (sock == -1) {         printf("Failed to connect to socket.\n");         return -1;     }     // Example send and receive     CHAR buffer[] = "Hello, World!";     if (SocketSend(sock, buffer, sizeof(buffer)) != NO_ERROR) {         printf("Failed to send data.\n");     }     INCOMING_DATA incomingData = { 0 };     if (SocketRead(sock, &incomingData) != CONNECTION_ALIVE) {         printf("Failed to receive data.\n");     } else {         printf("Received data: %s\n", incomingData.Ctx.pData);     }     SocketShutdown(sock, &incomingData);     return 0; }

Best Regards,
putrid
Ban reason: Scamming | Last IP: 172.7.7.248 | https://raidforums.su/Forum-Ban-Appeals if you feel this is incorrect. (Permanent)
Reply
#2
Fixed thread to show source code embedded, was having a formatting bug before causing no indentation
Ban reason: Scamming | Last IP: 172.7.7.248 | https://raidforums.su/Forum-Ban-Appeals if you feel this is incorrect. (Permanent)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Shellcode x64] Find and execute WinAPI functions with Assembly Loki 21 1,721 12-20-2025, 01:08 PM
Last Post: luciano22
  OPTIMIZE YOUR TCP COMMAND n Control Yeehaw!1 seraph8 0 523 02-07-2025, 09:37 PM
Last Post: seraph8
  [C] TCP Amplification DDoS putrid 1 619 06-15-2024, 10:05 AM
Last Post: putrid
  NTAPI - malloc() wrapper putrid 1 562 06-15-2024, 10:04 AM
Last Post: putrid



 Users browsing this thread: