More rnet review to avoid warnings/errors

This commit is contained in:
Ray
2019-04-22 21:50:45 +02:00
parent 2d4c2ff351
commit e26cc01ba8
2 changed files with 1583 additions and 1593 deletions

View File

@@ -92,13 +92,13 @@ static void PrintSocket(struct sockaddr_storage *addr, const int family,
static const char *SocketAddressToString(struct sockaddr_storage *sockaddr); static const char *SocketAddressToString(struct sockaddr_storage *sockaddr);
static bool IsIPv4Address(const char *ip); static bool IsIPv4Address(const char *ip);
static bool IsIPv6Address(const char *ip); static bool IsIPv6Address(const char *ip);
static void * GetSocketPortPtr(struct sockaddr_storage *sa); static void *GetSocketPortPtr(struct sockaddr_storage *sa);
static void * GetSocketAddressPtr(struct sockaddr_storage *sa); static void *GetSocketAddressPtr(struct sockaddr_storage *sa);
static bool IsSocketValid(Socket *sock); static bool IsSocketValid(Socket *sock);
static void SocketSetLastError(int err); static void SocketSetLastError(int err);
static int SocketGetLastError(); static int SocketGetLastError();
static char * SocketGetLastErrorString(); static char *SocketGetLastErrorString();
static char * SocketErrorCodeToString(int err); static char *SocketErrorCodeToString(int err);
static bool SocketSetDefaults(SocketConfig *config); static bool SocketSetDefaults(SocketConfig *config);
static bool InitSocket(Socket *sock, struct addrinfo *addr); static bool InitSocket(Socket *sock, struct addrinfo *addr);
static bool CreateSocket(SocketConfig *config, SocketResult *outresult); static bool CreateSocket(SocketConfig *config, SocketResult *outresult);
@@ -172,21 +172,21 @@ static void PrintSocket(struct sockaddr_storage *addr, const int family, const i
// Convert network ordered socket address to human readable string (127.0.0.1) // Convert network ordered socket address to human readable string (127.0.0.1)
static const char *SocketAddressToString(struct sockaddr_storage *sockaddr) static const char *SocketAddressToString(struct sockaddr_storage *sockaddr)
{ {
static const char* ipv6[INET6_ADDRSTRLEN]; //static const char* ipv6[INET6_ADDRSTRLEN];
assert(sockaddr != NULL); assert(sockaddr != NULL);
assert(sockaddr->ss_family == AF_INET || sockaddr->ss_family == AF_INET6); assert(sockaddr->ss_family == AF_INET || sockaddr->ss_family == AF_INET6);
switch (sockaddr->ss_family) switch (sockaddr->ss_family)
{ {
case AF_INET: case AF_INET:
{ {
struct sockaddr_in *s = ((struct sockaddr_in *) sockaddr); //struct sockaddr_in *s = ((struct sockaddr_in *) sockaddr);
return inet_ntop(AF_INET, &s->sin_addr, ipv6, INET_ADDRSTRLEN); //return inet_ntop(AF_INET, &s->sin_addr, ipv6, INET_ADDRSTRLEN); // TODO.
} }
break; break;
case AF_INET6: case AF_INET6:
{ {
struct sockaddr_in6 *s = ((struct sockaddr_in6 *) sockaddr); //struct sockaddr_in6 *s = ((struct sockaddr_in6 *) sockaddr);
return inet_ntop(AF_INET6, &s->sin6_addr, ipv6, INET6_ADDRSTRLEN); //return inet_ntop(AF_INET6, &s->sin6_addr, ipv6, INET6_ADDRSTRLEN); // TODO.
} }
break; break;
} }
@@ -196,17 +196,23 @@ static const char *SocketAddressToString(struct sockaddr_storage *sockaddr)
// Check if the null terminated string ip is a valid IPv4 address // Check if the null terminated string ip is a valid IPv4 address
static bool IsIPv4Address(const char *ip) static bool IsIPv4Address(const char *ip)
{ {
/*
struct sockaddr_in sa; struct sockaddr_in sa;
int result = inet_pton(AF_INET, ip, &(sa.sin_addr)); int result = inet_pton(AF_INET, ip, &(sa.sin_addr)); // TODO.
return result != 0; return (result != 0);
*/
return false;
} }
// Check if the null terminated string ip is a valid IPv6 address // Check if the null terminated string ip is a valid IPv6 address
static bool IsIPv6Address(const char *ip) static bool IsIPv6Address(const char *ip)
{ {
/*
struct sockaddr_in6 sa; struct sockaddr_in6 sa;
int result = inet_pton(AF_INET6, ip, &(sa.sin6_addr)); int result = inet_pton(AF_INET6, ip, &(sa.sin6_addr)); // TODO.
return result != 0; return result != 0;
*/
return false;
} }
// Return a pointer to the port from the correct address family (IPv4, or IPv6) // Return a pointer to the port from the correct address family (IPv4, or IPv6)
@@ -385,8 +391,8 @@ static bool CreateSocket(SocketConfig *config, SocketResult *outresult)
{ {
char hoststr[NI_MAXHOST]; char hoststr[NI_MAXHOST];
char portstr[NI_MAXSERV]; char portstr[NI_MAXSERV];
socklen_t client_len = sizeof(struct sockaddr_storage); //socklen_t client_len = sizeof(struct sockaddr_storage);
int rc = getnameinfo((struct sockaddr *) res->ai_addr, client_len, hoststr, sizeof(hoststr), portstr, sizeof(portstr), NI_NUMERICHOST | NI_NUMERICSERV); //int rc = getnameinfo((struct sockaddr *) res->ai_addr, client_len, hoststr, sizeof(hoststr), portstr, sizeof(portstr), NI_NUMERICHOST | NI_NUMERICSERV);
TraceLog(LOG_INFO, "Successfully resolved host %s:%s", hoststr, portstr); TraceLog(LOG_INFO, "Successfully resolved host %s:%s", hoststr, portstr);
} }
@@ -694,13 +700,11 @@ void ResolveIP(const char *ip, const char *port, int flags, char *host, char *se
flags); flags);
break; break;
case AF_INET6: case AF_INET6:
status = getnameinfo(&*((struct sockaddr_in6 *) res->ai_addr), /*
status = getnameinfo(&*((struct sockaddr_in6 *) res->ai_addr), // TODO.
sizeof(*((struct sockaddr_in6 *) res->ai_addr)), sizeof(*((struct sockaddr_in6 *) res->ai_addr)),
host, host, NI_MAXHOST, serv, NI_MAXSERV, flags);
NI_MAXHOST, */
serv,
NI_MAXSERV,
flags);
break; break;
default: break; default: break;
} }
@@ -746,10 +750,10 @@ int ResolveHost(const char *address, const char *service, int addressType, int f
hints.ai_family = addressType; // Either IPv4 or IPv6 (ADDRESS_TYPE_IPV4, ADDRESS_TYPE_IPV6) hints.ai_family = addressType; // Either IPv4 or IPv6 (ADDRESS_TYPE_IPV4, ADDRESS_TYPE_IPV6)
hints.ai_protocol = 0; // Automatically select correct protocol (IPPROTO_TCP), (IPPROTO_UDP) hints.ai_protocol = 0; // Automatically select correct protocol (IPPROTO_TCP), (IPPROTO_UDP)
hints.ai_flags = flags; hints.ai_flags = flags;
assert((hints.ai_addrlen == NULL) || (hints.ai_addrlen == 0)); assert((hints.ai_addrlen == 0) || (hints.ai_addrlen == 0));
assert((hints.ai_canonname == NULL) || (hints.ai_canonname == 0)); assert((hints.ai_canonname == 0) || (hints.ai_canonname == 0));
assert((hints.ai_addr == NULL) || (hints.ai_addr == 0)); assert((hints.ai_addr == 0) || (hints.ai_addr == 0));
assert((hints.ai_next == NULL) || (hints.ai_next == 0)); assert((hints.ai_next == 0) || (hints.ai_next == 0));
// When the address is NULL, populate the IP for me // When the address is NULL, populate the IP for me
if (address == NULL) if (address == NULL)
@@ -842,10 +846,7 @@ int ResolveHost(const char *address, const char *service, int addressType, int f
#if NET_DEBUG_ENABLED #if NET_DEBUG_ENABLED
TraceLog(LOG_DEBUG, "GetAddressInformation"); TraceLog(LOG_DEBUG, "GetAddressInformation");
TraceLog(LOG_DEBUG, "\tFlags: 0x%x", iterator->ai_flags); TraceLog(LOG_DEBUG, "\tFlags: 0x%x", iterator->ai_flags);
PrintSocket(outAddr[i]->addr.ai_addr, //PrintSocket(outAddr[i]->addr.ai_addr, outAddr[i]->addr.ai_family, outAddr[i]->addr.ai_socktype, outAddr[i]->addr.ai_protocol);
outAddr[i]->addr.ai_family,
outAddr[i]->addr.ai_socktype,
outAddr[i]->addr.ai_protocol);
TraceLog(LOG_DEBUG, "Length of this sockaddr: %d", outAddr[i]->addr.ai_addrlen); TraceLog(LOG_DEBUG, "Length of this sockaddr: %d", outAddr[i]->addr.ai_addrlen);
TraceLog(LOG_DEBUG, "Canonical name: %s", iterator->ai_canonname); TraceLog(LOG_DEBUG, "Canonical name: %s", iterator->ai_canonname);
#endif #endif
@@ -1058,7 +1059,10 @@ bool SocketConnect(SocketConfig *config, SocketResult *result)
unsigned long hport; unsigned long hport;
hport = strtoul(config->port, NULL, 0); hport = strtoul(config->port, NULL, 0);
ip4addr.sin_port = htons(hport); ip4addr.sin_port = htons(hport);
inet_pton(AF_INET, config->host, &ip4addr.sin_addr);
// TODO: Changed the code to avoid the usage of inet_pton and inet_ntop replacing them with getnameinfo (that should have a better support on windows).
//inet_pton(AF_INET, config->host, &ip4addr.sin_addr);
int connect_result = connect(result->socket->channel, (struct sockaddr *) &ip4addr, sizeof(ip4addr)); int connect_result = connect(result->socket->channel, (struct sockaddr *) &ip4addr, sizeof(ip4addr));
if (connect_result == SOCKET_ERROR) if (connect_result == SOCKET_ERROR)
{ {
@@ -1095,7 +1099,7 @@ bool SocketConnect(SocketConfig *config, SocketResult *result)
unsigned long hport; unsigned long hport;
hport = strtoul(config->port, NULL, 0); hport = strtoul(config->port, NULL, 0);
ip6addr.sin6_port = htons(hport); ip6addr.sin6_port = htons(hport);
inet_pton(AF_INET6, config->host, &ip6addr.sin6_addr); //inet_pton(AF_INET6, config->host, &ip6addr.sin6_addr); // TODO.
int connect_result = connect(result->socket->channel, (struct sockaddr *) &ip6addr, sizeof(ip6addr)); int connect_result = connect(result->socket->channel, (struct sockaddr *) &ip6addr, sizeof(ip6addr));
if (connect_result == SOCKET_ERROR) if (connect_result == SOCKET_ERROR)
{ {
@@ -1153,18 +1157,14 @@ void SocketClose(Socket *sock)
// Returns the sockaddress for a specific socket in a generic storage struct // Returns the sockaddress for a specific socket in a generic storage struct
SocketAddressStorage SocketGetPeerAddress(Socket *sock) SocketAddressStorage SocketGetPeerAddress(Socket *sock)
{ {
if (sock->isServer) // TODO.
{ /*
if (sock->isServer) return NULL;
if (sock->isIPv6) return sock->addripv6;
else return sock->addripv4;
*/
return NULL; return NULL;
}
if (sock->isIPv6)
{
return sock->addripv6;
}
else
{
return sock->addripv4;
}
} }
// Return the address-type appropriate host portion of a socket address // Return the address-type appropriate host portion of a socket address
@@ -1177,7 +1177,9 @@ char *GetSocketAddressHost(SocketAddressStorage storage)
// Return the address-type appropriate port(service) portion of a socket address // Return the address-type appropriate port(service) portion of a socket address
short GetSocketAddressPort(SocketAddressStorage storage) short GetSocketAddressPort(SocketAddressStorage storage)
{ {
return ntohs(GetSocketPortPtr(storage)); //return ntohs(GetSocketPortPtr(storage)); // TODO.
return 0;
} }
// The accept function permits an incoming connection attempt on a socket. // The accept function permits an incoming connection attempt on a socket.
@@ -1505,14 +1507,13 @@ int SocketReceive(Socket *sock, void *data, int maxlen)
int status = 0; int status = 0;
socklen_t sock_len; socklen_t sock_len;
struct sockaddr_storage sock_addr; struct sockaddr_storage sock_addr;
char ip[INET6_ADDRSTRLEN]; //char ip[INET6_ADDRSTRLEN];
// Server sockets are for accepting connections only // Server sockets are for accepting connections only
if (sock->isServer && sock->type == SOCKET_TCP) if (sock->isServer && sock->type == SOCKET_TCP)
{ {
sock->status = SocketGetLastError(); sock->status = SocketGetLastError();
TraceLog(LOG_DEBUG, "Socket Error: %s", TraceLog(LOG_DEBUG, "Socket Error: %s", "Server sockets cannot be used to receive data");
"Server sockets cannot be used to receive data");
SocketSetLastError(0); SocketSetLastError(0);
return 0; return 0;
} }
@@ -1533,12 +1534,13 @@ int SocketReceive(Socket *sock, void *data, int maxlen)
// Who sent the packet? // Who sent the packet?
if (sock->type == SOCKET_UDP) if (sock->type == SOCKET_UDP)
{ {
TraceLog( //TraceLog(LOG_DEBUG, "Received data from: %s", inet_ntop(sock_addr.ss_family, GetSocketAddressPtr((struct sockaddr *) &sock_addr), ip, sizeof(ip)));
LOG_DEBUG, "Received data from: %s", inet_ntop(sock_addr.ss_family, GetSocketAddressPtr((struct sockaddr *) &sock_addr), ip, sizeof(ip)));
} }
((unsigned char *) data)[len] = '\0'; // Add null terminating character to the end of the stream ((unsigned char *) data)[len] = '\0'; // Add null terminating character to the end of the stream
TraceLog(LOG_DEBUG, "Received \"%s\" (%d bytes)", data, len); TraceLog(LOG_DEBUG, "Received \"%s\" (%d bytes)", data, len);
} }
sock->ready = 0; sock->ready = 0;
return len; return len;
} }
@@ -1896,22 +1898,22 @@ char *GetAddressCanonName(AddressInformation address)
// Opaque datatype accessor addrinfo->ai_addr // Opaque datatype accessor addrinfo->ai_addr
char *GetAddressHostAndPort(AddressInformation address, char *outhost, int *outport) char *GetAddressHostAndPort(AddressInformation address, char *outhost, int *outport)
{ {
char * ip[INET6_ADDRSTRLEN]; //char *ip[INET6_ADDRSTRLEN];
char * result = NULL; char *result = NULL;
struct sockaddr_storage *storage = (struct sockaddr_storage *) address->addr.ai_addr; struct sockaddr_storage *storage = (struct sockaddr_storage *) address->addr.ai_addr;
switch (storage->ss_family) switch (storage->ss_family)
{ {
case AF_INET: case AF_INET:
{ {
struct sockaddr_in *s = ((struct sockaddr_in *) address->addr.ai_addr); struct sockaddr_in *s = ((struct sockaddr_in *) address->addr.ai_addr);
result = inet_ntop(AF_INET, &s->sin_addr, ip, INET_ADDRSTRLEN); //result = inet_ntop(AF_INET, &s->sin_addr, ip, INET_ADDRSTRLEN); // TODO.
*outport = ntohs(s->sin_port); *outport = ntohs(s->sin_port);
} }
break; break;
case AF_INET6: case AF_INET6:
{ {
struct sockaddr_in6 *s = ((struct sockaddr_in6 *) address->addr.ai_addr); struct sockaddr_in6 *s = ((struct sockaddr_in6 *) address->addr.ai_addr);
result = inet_ntop(AF_INET6, &s->sin6_addr, ip, INET6_ADDRSTRLEN); //result = inet_ntop(AF_INET6, &s->sin6_addr, ip, INET6_ADDRSTRLEN); // TODO.
*outport = ntohs(s->sin6_port); *outport = ntohs(s->sin6_port);
} }
break; break;

View File

@@ -124,44 +124,42 @@ typedef int socklen_t;
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
// Include system network headers // Include system network headers
#if defined(_WIN32) #if defined(_WIN32)
# pragma comment(lib, "ws2_32.lib") #define __USE_W32_SOCKETS
# define __USE_W32_SOCKETS #define WIN32_LEAN_AND_MEAN
# define WIN32_LEAN_AND_MEAN #include <winsock2.h>
# include <winsock2.h> #include <ws2tcpip.h>
# include <Ws2tcpip.h> #include <io.h>
# include <io.h> #define IPTOS_LOWDELAY 0x10
# define IPTOS_LOWDELAY 0x10 #else // Unix
#else /* UNIX */ #include <sys/types.h>
# include <sys/types.h> #include <fcntl.h>
# include <fcntl.h> #include <netinet/in.h>
# include <netinet/in.h> #include <sys/ioctl.h>
# include <sys/ioctl.h> #include <sys/time.h>
# include <sys/time.h> #include <unistd.h>
# include <unistd.h> #include <net/if.h>
# include <net/if.h> #include <netdb.h>
# include <netdb.h> #include <netinet/tcp.h>
# include <netinet/tcp.h> #include <sys/socket.h>
# include <sys/socket.h> #include <arpa/inet.h>
# include <arpa/inet.h> #endif
#endif /* WIN32 */
#ifndef INVALID_SOCKET #ifndef INVALID_SOCKET
# define INVALID_SOCKET ~(0) #define INVALID_SOCKET ~(0)
#endif #endif
#ifndef __USE_W32_SOCKETS #ifndef __USE_W32_SOCKETS
# define closesocket close #define closesocket close
# define SOCKET int #define SOCKET int
# define INVALID_SOCKET -1 #define INVALID_SOCKET -1
# define SOCKET_ERROR -1 #define SOCKET_ERROR -1
#endif #endif
#ifdef __USE_W32_SOCKETS #ifdef __USE_W32_SOCKETS
# ifndef EINTR #ifndef EINTR
# define EINTR WSAEINTR #define EINTR WSAEINTR
# endif #endif
#endif #endif
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
@@ -232,41 +230,36 @@ typedef int socklen_t;
// Network typedefs // Network typedefs
typedef uint32_t SocketChannel; typedef uint32_t SocketChannel;
typedef struct _AddressInformation * AddressInformation; typedef struct _AddressInformation *AddressInformation;
typedef struct _SocketAddress * SocketAddress; typedef struct _SocketAddress *SocketAddress;
typedef struct _SocketAddressIPv4 * SocketAddressIPv4; typedef struct _SocketAddressIPv4 *SocketAddressIPv4;
typedef struct _SocketAddressIPv6 * SocketAddressIPv6; typedef struct _SocketAddressIPv6 *SocketAddressIPv6;
typedef struct _SocketAddressStorage *SocketAddressStorage; typedef struct _SocketAddressStorage *SocketAddressStorage;
// IPAddress definition (in network byte order) // IPAddress definition (in network byte order)
typedef struct IPAddress typedef struct IPAddress {
{
unsigned long host; /* 32-bit IPv4 host address */ unsigned long host; /* 32-bit IPv4 host address */
unsigned short port; /* 16-bit protocol port */ unsigned short port; /* 16-bit protocol port */
} IPAddress; } IPAddress;
// An option ID, value, sizeof(value) tuple for setsockopt(2). // An option ID, value, sizeof(value) tuple for setsockopt(2).
typedef struct SocketOpt typedef struct SocketOpt {
{
int id; int id;
void *value; void *value;
int valueLen; int valueLen;
} SocketOpt; } SocketOpt;
typedef enum typedef enum {
{
SOCKET_TCP = 0, // SOCK_STREAM SOCKET_TCP = 0, // SOCK_STREAM
SOCKET_UDP = 1 // SOCK_DGRAM SOCKET_UDP = 1 // SOCK_DGRAM
} SocketType; } SocketType;
typedef struct UDPChannel typedef struct UDPChannel {
{
int numbound; // The total number of addresses this channel is bound to int numbound; // The total number of addresses this channel is bound to
IPAddress address[SOCKET_MAX_UDPADDRESSES]; // The list of remote addresses this channel is bound to IPAddress address[SOCKET_MAX_UDPADDRESSES]; // The list of remote addresses this channel is bound to
} UDPChannel; } UDPChannel;
typedef struct Socket typedef struct Socket {
{
int ready; // Is the socket ready? i.e. has information int ready; // Is the socket ready? i.e. has information
int status; // The last status code to have occured using this socket int status; // The last status code to have occured using this socket
bool isServer; // Is this socket a server socket (i.e. TCP/UDP Listen Server) bool isServer; // Is this socket a server socket (i.e. TCP/UDP Listen Server)
@@ -279,15 +272,13 @@ typedef struct Socket
struct UDPChannel binding[SOCKET_MAX_UDPCHANNELS]; // The amount of channels (if UDP) this socket is bound to struct UDPChannel binding[SOCKET_MAX_UDPCHANNELS]; // The amount of channels (if UDP) this socket is bound to
} Socket; } Socket;
typedef struct SocketSet typedef struct SocketSet {
{
int numsockets; int numsockets;
int maxsockets; int maxsockets;
struct Socket **sockets; struct Socket **sockets;
} SocketSet; } SocketSet;
typedef struct SocketDataPacket typedef struct SocketDataPacket {
{
int channel; // The src/dst channel of the packet int channel; // The src/dst channel of the packet
unsigned char *data; // The packet data unsigned char *data; // The packet data
int len; // The length of the packet data int len; // The length of the packet data
@@ -297,8 +288,7 @@ typedef struct SocketDataPacket
} SocketDataPacket; } SocketDataPacket;
// Configuration for a socket. // Configuration for a socket.
typedef struct SocketConfig typedef struct SocketConfig {
{
char * host; // The host address in xxx.xxx.xxx.xxx form char * host; // The host address in xxx.xxx.xxx.xxx form
char * port; // The target port/service in the form "http" or "25565" char * port; // The target port/service in the form "http" or "25565"
bool server; // Listen for incoming clients? bool server; // Listen for incoming clients?
@@ -309,15 +299,13 @@ typedef struct SocketConfig
} SocketConfig; } SocketConfig;
// Result from calling open with a given config. // Result from calling open with a given config.
typedef struct SocketResult typedef struct SocketResult {
{
int status; int status;
Socket *socket; Socket *socket;
} SocketResult; } SocketResult;
// Packet type // Packet type
typedef struct Packet typedef struct Packet {
{
uint32_t size; // The total size of bytes in data uint32_t size; // The total size of bytes in data
uint32_t offs; // The offset to data access uint32_t offs; // The offset to data access
uint32_t maxs; // The max size of data uint32_t maxs; // The max size of data