Usermode/libunicode - Add C++ wrappers
[tpg/acess2.git] / Usermode / Libraries / libpsocket.so_src / getaddrinfo.c
index d4d6ce8..5c21c06 100644 (file)
 #include <stdlib.h>    // strtol
 #include <acess/sys.h>
 
+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)
 {
@@ -40,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];
@@ -76,6 +86,8 @@ int getaddrinfo(const char *node, const char *service, const struct addrinfo *hi
                        // TODO: DNS Lookups
                        // ? /Acess/Conf/Nameservers
                        // ? /Acess/Conf/Hosts
+                       //count = Net_LookupDNS(node, service, NULL);
+                       //
                }
                
                // 3. No Match, chuck sad
@@ -103,23 +115,36 @@ int getaddrinfo(const char *node, const char *service, const struct addrinfo *hi
        // Convert `node` into types
        if( service )
        {
-               char *end;
+               const char *end;
                
-               default_port = strtol(service, &end, 0);
-               if( *end != '\0' && !(hints->ai_flags & AI_NUMERICSERV) )
+               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
-                       _SysDebug("getaddrinfo: TODO Service translation");
+                       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' )
                {
-                       return EAI_NONAME;
+                       _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;
@@ -211,3 +236,8 @@ const char *gai_strerror(int errnum)
        }
 }
 
+struct hostent *gethostbyname(const char *name)
+{
+       return NULL;
+}
+

UCC git Repository :: git.ucc.asn.au