]> Chaos Git - corbenik/ctrulib.git/commitdiff
move h_errno definition to soc_common
authormegazig <megazig@gmail.com>
Tue, 18 Aug 2015 21:55:50 +0000 (16:55 -0500)
committermegazig <megazig@gmail.com>
Tue, 18 Aug 2015 21:55:50 +0000 (16:55 -0500)
add h_addr member to hostent structure
implement gethostbyaddr

libctru/include/netdb.h
libctru/source/services/soc/soc_common.c
libctru/source/services/soc/soc_gethostbyaddr.c [new file with mode: 0644]
libctru/source/services/soc/soc_gethostbyname.c

index b3fac8183787c55dafe7d54858f07f54dd8452e1..dce8ffd644346756967d028b44e168f0567227ea 100644 (file)
@@ -14,6 +14,7 @@ struct hostent {
        int     h_addrtype;
        int     h_length;
        char    **h_addr_list;
+       char    *h_addr;
 };
 
 #ifdef __cplusplus
@@ -22,6 +23,7 @@ extern "C" {
 
        extern int      h_errno;
        struct hostent* gethostbyname(const char *name);
+       struct hostent* gethostbyaddr(const char *addr, socklen_t len, int type);
        void            herror(const char *s);
        const char*     hstrerror(int err);
 
index a610919ea3da2a9a9ce88b3e84d38a03a3767c78..d31d9b249c0b719c782345d439b823bef3d8eba1 100644 (file)
@@ -4,6 +4,7 @@
 
 Handle SOCU_handle = 0;
 Handle socMemhandle = 0;
+int h_errno = 0;
 
 //This is based on the array from libogc network_wii.c.
 static u8 _net_error_code_map[] = {
diff --git a/libctru/source/services/soc/soc_gethostbyaddr.c b/libctru/source/services/soc/soc_gethostbyaddr.c
new file mode 100644 (file)
index 0000000..d19e01b
--- /dev/null
@@ -0,0 +1,72 @@
+#include "soc_common.h"
+#include <netdb.h>
+
+#define MAX_HOSTENT_RESULTS 16
+static struct hostent SOC_hostent;
+static char           *SOC_hostent_results[MAX_HOSTENT_RESULTS+1];
+static char           *SOC_hostent_alias = NULL;
+
+struct hostent* gethostbyaddr(const char *addr, socklen_t len, int type)
+{
+       int ret = 0;
+       u32 *cmdbuf = getThreadCommandBuffer();
+       u32 saved_threadstorage[2];
+       static u8 outbuf[0x1A88];
+
+       h_errno = 0;
+
+       cmdbuf[0] = 0x000E00C2;
+       cmdbuf[1] = len;
+       cmdbuf[2] = type;
+       cmdbuf[3] = sizeof(outbuf);
+       cmdbuf[4] = (len << 14) | 0x1002;
+       cmdbuf[5] = (u32)addr;
+
+       saved_threadstorage[0] = cmdbuf[0x100>>2];
+       saved_threadstorage[1] = cmdbuf[0x104>>2];
+
+       cmdbuf[0x100>>2] = (sizeof(outbuf) << 14) | 2;
+       cmdbuf[0x104>>2] = (u32)outbuf;
+
+       ret = svcSendSyncRequest(SOCU_handle);
+       if(ret != 0) {
+               h_errno = NO_RECOVERY;
+               return NULL;
+       }
+
+
+       cmdbuf[0x100>>2] = saved_threadstorage[0];
+       cmdbuf[0x104>>2] = saved_threadstorage[1];
+
+       ret = (int)cmdbuf[1];
+       if(ret == 0)
+               ret = _net_convert_error(cmdbuf[2]);
+
+       if(ret < 0) {
+               /* TODO: set h_errno based on ret */
+               h_errno = HOST_NOT_FOUND;
+               return NULL;
+       }
+
+       u32 num_results, i;
+       memcpy(&num_results, (char*)outbuf+4, sizeof(num_results));
+       if(num_results > MAX_HOSTENT_RESULTS)
+               num_results = MAX_HOSTENT_RESULTS;
+
+       SOC_hostent.h_name      = (char*)outbuf + 8;
+       SOC_hostent.h_aliases   = &SOC_hostent_alias;
+       SOC_hostent.h_addrtype  = AF_INET;
+       SOC_hostent.h_length    = 4;
+       SOC_hostent.h_addr_list = SOC_hostent_results;
+
+       SOC_hostent_alias = NULL;
+
+       for(i = 0; i < num_results; ++i)
+               SOC_hostent_results[i] = (char*)outbuf + 0x1908 + i*0x10;
+       SOC_hostent_results[num_results] = NULL;
+
+       SOC_hostent.h_addr = SOC_hostent.h_addr_list[0];
+
+       return &SOC_hostent;
+}
+
index ed712db521940e1cf451bb6ec2a282175ead567e..2121c3b2909714eaff6ddc1e58c1372cd5532404 100644 (file)
@@ -6,8 +6,6 @@ static struct hostent SOC_hostent;
 static char           *SOC_hostent_results[MAX_HOSTENT_RESULTS+1];
 static char           *SOC_hostent_alias = NULL;
 
-int h_errno = 0;
-
 struct hostent* gethostbyname(const char *name)
 {
        int ret = 0;
@@ -66,5 +64,7 @@ struct hostent* gethostbyname(const char *name)
                SOC_hostent_results[i] = (char*)outbuf + 0x1908 + i*0x10;
        SOC_hostent_results[num_results] = NULL;
 
+       SOC_hostent.h_addr = SOC_hostent.h_addr_list[0];
+
        return &SOC_hostent;
 }