<commit_before>#ifdef _WIN32
#include <ws2tcpip.h>
#include <windows.h>
//#include <winsock.h>
#else // _WIN32
#endif // _WIN32

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

// H file
typedef struct Net_TCPSockAddr_S Net_TCPSockAddr_T;
typedef struct Net_TCPSock_S     Net_TCPSock_T;
extern const size_t Net_TCPSockAddrSize;
extern const size_t Net_TCPSockSize;

#define NET_DONTBLOCK 0
#define NET_BLOCK     1

// C file
struct Net_TCPSockAddr_S
{
   int valid_addr_flag;
   struct sockaddr_storage address;
   socklen_t len;
};

struct Net_TCPSock_S
{
   int type;
   int socket_file;
   Net_TCPSockAddr_T addr_local;
   Net_TCPSockAddr_T addr_remote;
};

size_t const Net_TCPSockAddrSize = sizeof(Net_TCPSockAddr_T);
size_t const Net_TCPSockSize     = sizeof(Net_TCPSock_T);

#define TYPE_TCPCLIENT 0
#define TYPE_TCPSERVER 1

int Net_Init(void)
{
   int error;

   WSADATA wsaData;
   if(WSAStartup(MAKEWORD(2,0), &wsaData) != 0)
   {
      printf("WinSock Failed to Startup\n");
      error = 1;
   }
   else
   {
      error = 0;
   }
   return error;
}

void Net_Shutdown(void)
{
   WSACleanup();
}

Net_TCPSockAddr_T * Net_TCPGetRemoteAddr(Net_TCPSock_T * sock)
{
   return &sock->addr_remote;
}

Net_TCPSockAddr_T * Net_TCPGetLocalAddr(Net_TCPSock_T * sock)
{
   return &sock->addr_local;
}
#define INVALID_SOCKET_NAME "<INVALID>"

size_t Net_TCPAddrToString(Net_TCPSockAddr_T * addr, char * string, size_t size)
{
   size_t out_size;
   if(addr->valid_addr_flag == 1)
   {

      if(addr->address.ss_family == AF_INET)
      {
         out_size = INET_ADDRSTRLEN;
      }
      else if(addr->address.ss_family == AF_INET6)
      {
         out_size = INET6_ADDRSTRLEN;
      }
      else
      {
         out_size = 0;
      }

      if(string != NULL && out_size > 0)
      {
         inet_ntop(addr->address.ss_family, &addr->address, string, size);
      }
   }
   else 
   {
      out_size = sizeof(INVALID_SOCKET_NAME);
      if(string != NULL)<commit_msg>Added some protection code. Added a way to check for a valid address<commit_after>#ifdef _WIN32
#include <ws2tcpip.h>
#include <windows.h>
//#include <winsock.h>
#else // _WIN32
#endif // _WIN32

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

// H file
typedef struct Net_TCPSockAddr_S Net_TCPSockAddr_T;
typedef struct Net_TCPSock_S     Net_TCPSock_T;
extern const size_t Net_TCPSockAddrSize;
extern const size_t Net_TCPSockSize;

#define NET_DONTBLOCK 0
#define NET_BLOCK     1

// C file
struct Net_TCPSockAddr_S
{
   int valid_addr_flag;
   struct sockaddr_storage address;
   socklen_t len;
};

struct Net_TCPSock_S
{
   int type;
   int socket_file;
   Net_TCPSockAddr_T addr_local;
   Net_TCPSockAddr_T addr_remote;
};

size_t const Net_TCPSockAddrSize = sizeof(Net_TCPSockAddr_T);
size_t const Net_TCPSockSize     = sizeof(Net_TCPSock_T);

#define TYPE_TCPCLIENT 0
#define TYPE_TCPSERVER 1

int Net_Init(void)
{
   int error;

   WSADATA wsaData;
   if(WSAStartup(MAKEWORD(2,0), &wsaData) != 0)
   {
      printf("WinSock Failed to Startup\n");
      error = 1;
   }
   else
   {
      error = 0;
   }
   return error;
}

void Net_Shutdown(void)
{
   WSACleanup();
}

Net_TCPSockAddr_T * Net_TCPGetRemoteAddr(Net_TCPSock_T * sock)
{
   Net_TCPSockAddr_T * result;
   if(sock == NULL)
   {
      result = NULL;
   }
   else
   {
      result = &sock->addr_remote;
   }
   return result;
}

Net_TCPSockAddr_T * Net_TCPGetLocalAddr(Net_TCPSock_T * sock)
{
   Net_TCPSockAddr_T * result;
   if(sock == NULL)
   {
      result = NULL;
   }
   else
   {
      result = &sock->addr_local;
   }
   return result;
}

int Net_TCPIsAddrValid(Net_TCPSockAddr_T * addr)
{
   int result;
   if(addr == NULL)
   {
      result = 0;
   }
   else
   {
      result = addr->valid_addr_flag;
   }
   return result;
}

#define INVALID_SOCKET_NAME "<INVALID>"

size_t Net_TCPAddrToString(Net_TCPSockAddr_T * addr, char * string, size_t size)
{
   size_t out_size;
   if(addr->valid_addr_flag == 1)
   {

      if(addr->address.ss_family == AF_INET)
      {
         out_size = INET_ADDRSTRLEN;
      }
      else if(addr->address.ss_family == AF_INET6)
      {
         out_size = INET6_ADDRSTRLEN;
      }
      else
      {
         out_size = 0;
      }

      if(string != NULL && out_size > 0)
      {
         inet_ntop(addr->address.ss_family, &addr->address, string, size);
      }
   }
   else 
   {
      out_size = sizeof(INVALID_SOCKET_NAME);
      if(string != NULL)
{'fraction_non_alphanumeric': 0.10388457269700332, 'fraction_numerical': 0.009544950055493896, 'mean_word_length': 3.01246660730187, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5534454', 'n_tokens_mistral': 1819, 'n_tokens_neox': 1767, 'n_words': 345}