More rnet review to avoid warnings/errors
This commit is contained in:
parent
2d4c2ff351
commit
e26cc01ba8
2 changed files with 1583 additions and 1593 deletions
104
src/rnet.c
104
src/rnet.c
|
@ -92,13 +92,13 @@ static void PrintSocket(struct sockaddr_storage *addr, const int family,
|
|||
static const char *SocketAddressToString(struct sockaddr_storage *sockaddr);
|
||||
static bool IsIPv4Address(const char *ip);
|
||||
static bool IsIPv6Address(const char *ip);
|
||||
static void * GetSocketPortPtr(struct sockaddr_storage *sa);
|
||||
static void * GetSocketAddressPtr(struct sockaddr_storage *sa);
|
||||
static void *GetSocketPortPtr(struct sockaddr_storage *sa);
|
||||
static void *GetSocketAddressPtr(struct sockaddr_storage *sa);
|
||||
static bool IsSocketValid(Socket *sock);
|
||||
static void SocketSetLastError(int err);
|
||||
static int SocketGetLastError();
|
||||
static char * SocketGetLastErrorString();
|
||||
static char * SocketErrorCodeToString(int err);
|
||||
static char *SocketGetLastErrorString();
|
||||
static char *SocketErrorCodeToString(int err);
|
||||
static bool SocketSetDefaults(SocketConfig *config);
|
||||
static bool InitSocket(Socket *sock, struct addrinfo *addr);
|
||||
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)
|
||||
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->ss_family == AF_INET || sockaddr->ss_family == AF_INET6);
|
||||
switch (sockaddr->ss_family)
|
||||
{
|
||||
case AF_INET:
|
||||
{
|
||||
struct sockaddr_in *s = ((struct sockaddr_in *) sockaddr);
|
||||
return inet_ntop(AF_INET, &s->sin_addr, ipv6, INET_ADDRSTRLEN);
|
||||
//struct sockaddr_in *s = ((struct sockaddr_in *) sockaddr);
|
||||
//return inet_ntop(AF_INET, &s->sin_addr, ipv6, INET_ADDRSTRLEN); // TODO.
|
||||
}
|
||||
break;
|
||||
case AF_INET6:
|
||||
{
|
||||
struct sockaddr_in6 *s = ((struct sockaddr_in6 *) sockaddr);
|
||||
return inet_ntop(AF_INET6, &s->sin6_addr, ipv6, INET6_ADDRSTRLEN);
|
||||
//struct sockaddr_in6 *s = ((struct sockaddr_in6 *) sockaddr);
|
||||
//return inet_ntop(AF_INET6, &s->sin6_addr, ipv6, INET6_ADDRSTRLEN); // TODO.
|
||||
}
|
||||
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
|
||||
static bool IsIPv4Address(const char *ip)
|
||||
{
|
||||
/*
|
||||
struct sockaddr_in sa;
|
||||
int result = inet_pton(AF_INET, ip, &(sa.sin_addr));
|
||||
return result != 0;
|
||||
int result = inet_pton(AF_INET, ip, &(sa.sin_addr)); // TODO.
|
||||
return (result != 0);
|
||||
*/
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check if the null terminated string ip is a valid IPv6 address
|
||||
static bool IsIPv6Address(const char *ip)
|
||||
{
|
||||
/*
|
||||
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 false;
|
||||
}
|
||||
|
||||
// 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 portstr[NI_MAXSERV];
|
||||
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);
|
||||
//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);
|
||||
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);
|
||||
break;
|
||||
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)),
|
||||
host,
|
||||
NI_MAXHOST,
|
||||
serv,
|
||||
NI_MAXSERV,
|
||||
flags);
|
||||
host, NI_MAXHOST, serv, NI_MAXSERV, flags);
|
||||
*/
|
||||
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_protocol = 0; // Automatically select correct protocol (IPPROTO_TCP), (IPPROTO_UDP)
|
||||
hints.ai_flags = flags;
|
||||
assert((hints.ai_addrlen == NULL) || (hints.ai_addrlen == 0));
|
||||
assert((hints.ai_canonname == NULL) || (hints.ai_canonname == 0));
|
||||
assert((hints.ai_addr == NULL) || (hints.ai_addr == 0));
|
||||
assert((hints.ai_next == NULL) || (hints.ai_next == 0));
|
||||
assert((hints.ai_addrlen == 0) || (hints.ai_addrlen == 0));
|
||||
assert((hints.ai_canonname == 0) || (hints.ai_canonname == 0));
|
||||
assert((hints.ai_addr == 0) || (hints.ai_addr == 0));
|
||||
assert((hints.ai_next == 0) || (hints.ai_next == 0));
|
||||
|
||||
// When the address is NULL, populate the IP for me
|
||||
if (address == NULL)
|
||||
|
@ -842,10 +846,7 @@ int ResolveHost(const char *address, const char *service, int addressType, int f
|
|||
#if NET_DEBUG_ENABLED
|
||||
TraceLog(LOG_DEBUG, "GetAddressInformation");
|
||||
TraceLog(LOG_DEBUG, "\tFlags: 0x%x", iterator->ai_flags);
|
||||
PrintSocket(outAddr[i]->addr.ai_addr,
|
||||
outAddr[i]->addr.ai_family,
|
||||
outAddr[i]->addr.ai_socktype,
|
||||
outAddr[i]->addr.ai_protocol);
|
||||
//PrintSocket(outAddr[i]->addr.ai_addr, 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, "Canonical name: %s", iterator->ai_canonname);
|
||||
#endif
|
||||
|
@ -1058,7 +1059,10 @@ bool SocketConnect(SocketConfig *config, SocketResult *result)
|
|||
unsigned long hport;
|
||||
hport = strtoul(config->port, NULL, 0);
|
||||
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));
|
||||
if (connect_result == SOCKET_ERROR)
|
||||
{
|
||||
|
@ -1095,7 +1099,7 @@ bool SocketConnect(SocketConfig *config, SocketResult *result)
|
|||
unsigned long hport;
|
||||
hport = strtoul(config->port, NULL, 0);
|
||||
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));
|
||||
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
|
||||
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;
|
||||
}
|
||||
if (sock->isIPv6)
|
||||
{
|
||||
return sock->addripv6;
|
||||
}
|
||||
else
|
||||
{
|
||||
return sock->addripv4;
|
||||
}
|
||||
}
|
||||
|
||||
// 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
|
||||
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.
|
||||
|
@ -1505,14 +1507,13 @@ int SocketReceive(Socket *sock, void *data, int maxlen)
|
|||
int status = 0;
|
||||
socklen_t sock_len;
|
||||
struct sockaddr_storage sock_addr;
|
||||
char ip[INET6_ADDRSTRLEN];
|
||||
//char ip[INET6_ADDRSTRLEN];
|
||||
|
||||
// Server sockets are for accepting connections only
|
||||
if (sock->isServer && sock->type == SOCKET_TCP)
|
||||
{
|
||||
sock->status = SocketGetLastError();
|
||||
TraceLog(LOG_DEBUG, "Socket Error: %s",
|
||||
"Server sockets cannot be used to receive data");
|
||||
TraceLog(LOG_DEBUG, "Socket Error: %s", "Server sockets cannot be used to receive data");
|
||||
SocketSetLastError(0);
|
||||
return 0;
|
||||
}
|
||||
|
@ -1533,12 +1534,13 @@ int SocketReceive(Socket *sock, void *data, int maxlen)
|
|||
// Who sent the packet?
|
||||
if (sock->type == SOCKET_UDP)
|
||||
{
|
||||
TraceLog(
|
||||
LOG_DEBUG, "Received data from: %s", inet_ntop(sock_addr.ss_family, GetSocketAddressPtr((struct sockaddr *) &sock_addr), ip, sizeof(ip)));
|
||||
//TraceLog(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
|
||||
TraceLog(LOG_DEBUG, "Received \"%s\" (%d bytes)", data, len);
|
||||
}
|
||||
|
||||
sock->ready = 0;
|
||||
return len;
|
||||
}
|
||||
|
@ -1896,22 +1898,22 @@ char *GetAddressCanonName(AddressInformation address)
|
|||
// Opaque datatype accessor addrinfo->ai_addr
|
||||
char *GetAddressHostAndPort(AddressInformation address, char *outhost, int *outport)
|
||||
{
|
||||
char * ip[INET6_ADDRSTRLEN];
|
||||
char * result = NULL;
|
||||
//char *ip[INET6_ADDRSTRLEN];
|
||||
char *result = NULL;
|
||||
struct sockaddr_storage *storage = (struct sockaddr_storage *) address->addr.ai_addr;
|
||||
switch (storage->ss_family)
|
||||
{
|
||||
case AF_INET:
|
||||
{
|
||||
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);
|
||||
}
|
||||
break;
|
||||
case AF_INET6:
|
||||
{
|
||||
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);
|
||||
}
|
||||
break;
|
||||
|
|
94
src/rnet.h
94
src/rnet.h
|
@ -124,44 +124,42 @@ typedef int socklen_t;
|
|||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Include system network headers
|
||||
|
||||
#if defined(_WIN32)
|
||||
# pragma comment(lib, "ws2_32.lib")
|
||||
# define __USE_W32_SOCKETS
|
||||
# define WIN32_LEAN_AND_MEAN
|
||||
# include <winsock2.h>
|
||||
# include <Ws2tcpip.h>
|
||||
# include <io.h>
|
||||
# define IPTOS_LOWDELAY 0x10
|
||||
#else /* UNIX */
|
||||
# include <sys/types.h>
|
||||
# include <fcntl.h>
|
||||
# include <netinet/in.h>
|
||||
# include <sys/ioctl.h>
|
||||
# include <sys/time.h>
|
||||
# include <unistd.h>
|
||||
# include <net/if.h>
|
||||
# include <netdb.h>
|
||||
# include <netinet/tcp.h>
|
||||
# include <sys/socket.h>
|
||||
# include <arpa/inet.h>
|
||||
#endif /* WIN32 */
|
||||
#define __USE_W32_SOCKETS
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <winsock2.h>
|
||||
#include <ws2tcpip.h>
|
||||
#include <io.h>
|
||||
#define IPTOS_LOWDELAY 0x10
|
||||
#else // Unix
|
||||
#include <sys/types.h>
|
||||
#include <fcntl.h>
|
||||
#include <netinet/in.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/time.h>
|
||||
#include <unistd.h>
|
||||
#include <net/if.h>
|
||||
#include <netdb.h>
|
||||
#include <netinet/tcp.h>
|
||||
#include <sys/socket.h>
|
||||
#include <arpa/inet.h>
|
||||
#endif
|
||||
|
||||
#ifndef INVALID_SOCKET
|
||||
# define INVALID_SOCKET ~(0)
|
||||
#define INVALID_SOCKET ~(0)
|
||||
#endif
|
||||
|
||||
#ifndef __USE_W32_SOCKETS
|
||||
# define closesocket close
|
||||
# define SOCKET int
|
||||
# define INVALID_SOCKET -1
|
||||
# define SOCKET_ERROR -1
|
||||
#define closesocket close
|
||||
#define SOCKET int
|
||||
#define INVALID_SOCKET -1
|
||||
#define SOCKET_ERROR -1
|
||||
#endif
|
||||
|
||||
#ifdef __USE_W32_SOCKETS
|
||||
# ifndef EINTR
|
||||
# define EINTR WSAEINTR
|
||||
# endif
|
||||
#ifndef EINTR
|
||||
#define EINTR WSAEINTR
|
||||
#endif
|
||||
#endif
|
||||
|
||||
//----------------------------------------------------------------------------------
|
||||
|
@ -232,41 +230,36 @@ typedef int socklen_t;
|
|||
|
||||
// Network typedefs
|
||||
typedef uint32_t SocketChannel;
|
||||
typedef struct _AddressInformation * AddressInformation;
|
||||
typedef struct _SocketAddress * SocketAddress;
|
||||
typedef struct _SocketAddressIPv4 * SocketAddressIPv4;
|
||||
typedef struct _SocketAddressIPv6 * SocketAddressIPv6;
|
||||
typedef struct _AddressInformation *AddressInformation;
|
||||
typedef struct _SocketAddress *SocketAddress;
|
||||
typedef struct _SocketAddressIPv4 *SocketAddressIPv4;
|
||||
typedef struct _SocketAddressIPv6 *SocketAddressIPv6;
|
||||
typedef struct _SocketAddressStorage *SocketAddressStorage;
|
||||
|
||||
// IPAddress definition (in network byte order)
|
||||
typedef struct IPAddress
|
||||
{
|
||||
typedef struct IPAddress {
|
||||
unsigned long host; /* 32-bit IPv4 host address */
|
||||
unsigned short port; /* 16-bit protocol port */
|
||||
} IPAddress;
|
||||
|
||||
// An option ID, value, sizeof(value) tuple for setsockopt(2).
|
||||
typedef struct SocketOpt
|
||||
{
|
||||
typedef struct SocketOpt {
|
||||
int id;
|
||||
void *value;
|
||||
int valueLen;
|
||||
} SocketOpt;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
typedef enum {
|
||||
SOCKET_TCP = 0, // SOCK_STREAM
|
||||
SOCKET_UDP = 1 // SOCK_DGRAM
|
||||
} SocketType;
|
||||
|
||||
typedef struct UDPChannel
|
||||
{
|
||||
typedef struct UDPChannel {
|
||||
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
|
||||
} UDPChannel;
|
||||
|
||||
typedef struct Socket
|
||||
{
|
||||
typedef struct Socket {
|
||||
int ready; // Is the socket ready? i.e. has information
|
||||
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)
|
||||
|
@ -279,15 +272,13 @@ typedef struct Socket
|
|||
struct UDPChannel binding[SOCKET_MAX_UDPCHANNELS]; // The amount of channels (if UDP) this socket is bound to
|
||||
} Socket;
|
||||
|
||||
typedef struct SocketSet
|
||||
{
|
||||
typedef struct SocketSet {
|
||||
int numsockets;
|
||||
int maxsockets;
|
||||
struct Socket **sockets;
|
||||
} SocketSet;
|
||||
|
||||
typedef struct SocketDataPacket
|
||||
{
|
||||
typedef struct SocketDataPacket {
|
||||
int channel; // The src/dst channel of the packet
|
||||
unsigned char *data; // The packet data
|
||||
int len; // The length of the packet data
|
||||
|
@ -297,8 +288,7 @@ typedef struct SocketDataPacket
|
|||
} SocketDataPacket;
|
||||
|
||||
// Configuration for a socket.
|
||||
typedef struct SocketConfig
|
||||
{
|
||||
typedef struct SocketConfig {
|
||||
char * host; // The host address in xxx.xxx.xxx.xxx form
|
||||
char * port; // The target port/service in the form "http" or "25565"
|
||||
bool server; // Listen for incoming clients?
|
||||
|
@ -309,15 +299,13 @@ typedef struct SocketConfig
|
|||
} SocketConfig;
|
||||
|
||||
// Result from calling open with a given config.
|
||||
typedef struct SocketResult
|
||||
{
|
||||
typedef struct SocketResult {
|
||||
int status;
|
||||
Socket *socket;
|
||||
} SocketResult;
|
||||
|
||||
// Packet type
|
||||
typedef struct Packet
|
||||
{
|
||||
typedef struct Packet {
|
||||
uint32_t size; // The total size of bytes in data
|
||||
uint32_t offs; // The offset to data access
|
||||
uint32_t maxs; // The max size of data
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue