minor fix to host resolution

This commit is contained in:
Jak Barnes 2019-03-18 21:00:05 +00:00
parent 7c2f816852
commit 739f951fba
3 changed files with 51 additions and 9 deletions

View File

@ -22,6 +22,9 @@
#include "raylib.h" #include "raylib.h"
char buffer[ADDRESS_IPV6_ADDRSTRLEN];
uint16_t port = 0;
int main() int main()
{ {
// Setup // Setup
@ -36,7 +39,7 @@ int main()
// Networking // Networking
InitNetwork(); InitNetwork();
AddressInformation* addr = NULL; AddressInformation* addr = AllocAddressList(1);
int count = ResolveHost( int count = ResolveHost(
NULL, NULL,
"5210", "5210",
@ -50,8 +53,12 @@ int main()
, ,
addr addr
); );
ResolveIP("8.8.8.8", NULL, NAME_INFO_DEFAULT, NULL, NULL);
ResolveIP("2001:4860:4860::8888", "80", NAME_INFO_NUMERICSERV, NULL, NULL); if (count > 0)
{
GetAddressHostAndPort(addr[0], buffer, &port);
TraceLog(LOG_INFO, "Resolved to ip %s::%d\n", buffer, port);
}
// Main game loop // Main game loop
while (!WindowShouldClose()) while (!WindowShouldClose())

View File

@ -1533,9 +1533,11 @@ RLAPI void CloseNetwork(void);
// Address API // Address API
RLAPI void ResolveIP(const char *ip, const char *service, int flags, char *outhost, char *outserv); RLAPI void ResolveIP(const char *ip, const char *service, int flags, char *outhost, char *outserv);
RLAPI int ResolveHost(const char *address, const char *service, int addressType, int flags, AddressInformation* outAddr); RLAPI int ResolveHost(const char *address, const char *service, int addressType, int flags, AddressInformation* outAddr);
RLAPI int GetAddressFamily(); RLAPI int GetAddressFamily(AddressInformation address);
RLAPI int GetAddressSocketType(AddressInformation address); RLAPI int GetAddressSocketType(AddressInformation address);
RLAPI int GetAddressProtocol(AddressInformation address); RLAPI int GetAddressProtocol(AddressInformation address);
RLAPI char* GetAddressCanonName(AddressInformation address);
RLAPI char *GetAddressHostAndPort(AddressInformation address, char *outhost, int *outport);
RLAPI void PrintAddressInfo(AddressInformation address); RLAPI void PrintAddressInfo(AddressInformation address);
RLAPI AddressInformation AllocAddress(); RLAPI AddressInformation AllocAddress();
RLAPI void FreeAddress(AddressInformation* addressInfo); RLAPI void FreeAddress(AddressInformation* addressInfo);

View File

@ -708,7 +708,7 @@ int ResolveHost(const char *address, const char *service, int addressType, int f
// If not address list was allocated, allocate it dynamically with the known address size // If not address list was allocated, allocate it dynamically with the known address size
if (outAddr == NULL) { if (outAddr == NULL) {
outAddr = AllocAddressList(size); outAddr = (AddressInformation *) malloc(size * sizeof(AddressInformation));
} }
// Dynamically allocate an array of address information structs // Dynamically allocate an array of address information structs
@ -1562,6 +1562,12 @@ AddressInformation *AllocAddressList(int size)
return addr; return addr;
} }
// Opaque datatype accessor addrinfo->ai_family
int GetAddressFamily(AddressInformation address)
{
return address->addr.ai_family;
}
// Opaque datatype accessor addrinfo->ai_socktype // Opaque datatype accessor addrinfo->ai_socktype
int GetAddressSocketType(AddressInformation address) int GetAddressSocketType(AddressInformation address)
{ {
@ -1574,10 +1580,37 @@ int GetAddressProtocol(AddressInformation address)
return address->addr.ai_protocol; return address->addr.ai_protocol;
} }
// Opaque datatype accessor addrinfo->ai_family // Opaque datatype accessor addrinfo->ai_canonname
int GetAddressFamily(AddressInformation address) char *GetAddressCanonName(AddressInformation address)
{ {
return address->addr.ai_family; return address->addr.ai_canonname;
}
// Opaque datatype accessor addrinfo->ai_addr
char *GetAddressHostAndPort(AddressInformation address, char *outhost, int *outport)
{
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);
*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);
*outport = ntohs(s->sin6_port);
} break;
}
if (result == NULL) {
TraceLog(LOG_WARNING, "Socket Error: %s", SocketErrorCodeToString(SocketGetLastError()));
SocketSetLastError(0);
} else {
strcpy(outhost, result);
}
return result;
} }
// //