X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=Usermode%2FLibraries%2Flibpsocket.so_src%2Fgetaddrinfo.c;h=5c21c06ea555621970653f23049a3e832a75a6c9;hb=f8cde3fab5eb27ebacf9d76a7ac478a68a36a38b;hp=c26ce321f9b0c5db9367e6ff4d2ffdd055142cc0;hpb=67be203adb74a30134d277c346ca2f82e0fe0850;p=tpg%2Facess2.git diff --git a/Usermode/Libraries/libpsocket.so_src/getaddrinfo.c b/Usermode/Libraries/libpsocket.so_src/getaddrinfo.c index c26ce321..5c21c06e 100644 --- a/Usermode/Libraries/libpsocket.so_src/getaddrinfo.c +++ b/Usermode/Libraries/libpsocket.so_src/getaddrinfo.c @@ -10,14 +10,31 @@ #include // Net_ParseAddress #include // malloc #include // memcpy +#include // strtol #include +static const struct { + const char *Name; + int SockType; + int Protocol; + int Port; +} caLocalServices[] = { + {"telnet", SOCK_STREAM, 0, 23}, + {"http", SOCK_STREAM, 0, 80}, +}; +static const int ciNumLocalServices = sizeof(caLocalServices)/sizeof(caLocalServices[0]); + // === CODE === int getaddrinfo(const char *node, const char *service, const struct addrinfo *hints, struct addrinfo **res) { - static const struct addrinfo defhints = {.ai_flags = AI_V4MAPPED | AI_ADDRCONFIG}; + static const struct addrinfo defhints = { + .ai_family = AF_UNSPEC, + .ai_flags = AI_V4MAPPED | AI_ADDRCONFIG + }; struct addrinfo *ret = NULL; + _SysDebug("getaddrinfo('%s','%s',%p,%p)", node, service, hints, res); + // Error checks if( !node && !service ) return EAI_NONAME; @@ -34,7 +51,6 @@ int getaddrinfo(const char *node, const char *service, const struct addrinfo *hi else { // 1. Check if the node is an IP address - // TODO: Break this function out into inet_pton? { int type; char addrdata[16]; @@ -66,9 +82,12 @@ int getaddrinfo(const char *node, const char *service, const struct addrinfo *hi // - No luck with above, and hints->ai_flags doesn't have AI_NUMERICHOST set if( !ret && !(hints->ai_flags & AI_NUMERICHOST) ) { + _SysDebug("getaddrinfo: TODO DNS Lookups"); // TODO: DNS Lookups // ? /Acess/Conf/Nameservers // ? /Acess/Conf/Hosts + //count = Net_LookupDNS(node, service, NULL); + // } // 3. No Match, chuck sad @@ -78,18 +97,54 @@ int getaddrinfo(const char *node, const char *service, const struct addrinfo *hi } } - int default_socktype = 0; - int default_protocol = 0; + int default_socktype = hints->ai_socktype; + int default_protocol = hints->ai_protocol; int default_port = 0; + #if 0 + if( default_protocol == 0 ) + { + switch(default_socktype) + { + case SOCK_DGRAM: + default_protocol = + } + } + #endif + // Convert `node` into types if( service ) { - // TODO: Read something like /Acess/Conf/services + const char *end; + + default_port = strtol(service, (char**)&end, 0); + if( *end != '\0' && (hints->ai_flags & AI_NUMERICSERV) ) + { + return EAI_NONAME; + } + + if( *end != '\0' ) + { + // TODO: Read something like /Acess/Conf/services + for( int i = 0; i < ciNumLocalServices; i ++ ) + { + if( strcmp(service, caLocalServices[i].Name) == 0 ) { + end = service + strlen(service); + default_socktype = caLocalServices[i].SockType; + default_protocol = caLocalServices[i].Protocol; + default_port = caLocalServices[i].Port; + break; + } + } + } + + if( *end != '\0' ) + { + _SysDebug("getaddrinfo: TODO Service translation"); + } } - struct addrinfo *ai; - for( ai = ret; ai; ai = ai->ai_next) + for( struct addrinfo *ai = ret; ai; ai = ai->ai_next) { struct sockaddr_in *in = (void*)ai->ai_addr; struct sockaddr_in6 *in6 = (void*)ai->ai_addr; @@ -106,10 +161,16 @@ int getaddrinfo(const char *node, const char *service, const struct addrinfo *hi case AF_INET: if( in->sin_port == 0 ) in->sin_port = default_port; + _SysDebug("%p: IPv4 [%s]:%i %i,%i", + ai, Net_PrintAddress(4, &in->sin_addr), + in->sin_port, ai->ai_socktype, ai->ai_protocol); break; case AF_INET6: if( in6->sin6_port == 0 ) in6->sin6_port = default_port; + _SysDebug("%p: IPv6 [%s]:%i %i,%i", + ai, Net_PrintAddress(6, &in6->sin6_addr), + in6->sin6_port, ai->ai_socktype, ai->ai_protocol); break; default: _SysDebug("getaddrinfo: Unknown address family %i (setting port)", ai->ai_family); @@ -175,3 +236,8 @@ const char *gai_strerror(int errnum) } } +struct hostent *gethostbyname(const char *name) +{ + return NULL; +} +