summaryrefslogtreecommitdiff
path: root/devel/psptoolchain-newlib/files/patch-newlib-libc-sys-psp-netdb.c
blob: 5753ebb5af3b959bccbf9e1bd53f6b5e20cae3c4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
--- ./newlib/libc/sys/psp/netdb.c.orig	2012-01-25 19:33:12.000000000 +0000
+++ ./newlib/libc/sys/psp/netdb.c	2012-01-25 19:33:12.000000000 +0000
@@ -0,0 +1,103 @@
+/* Simple gethostbyname and gethostbyaddr replacements using the resolver lib */
+#include <stdio.h>
+#include <netdb.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <sys/socket.h>
+#include <pspkerneltypes.h>
+#include <pspnet_resolver.h>
+
+#define MAX_NAME 512
+
+#ifdef F_h_errno
+int h_errno = NETDB_SUCCESS;
+#endif
+
+#ifdef F_gethostbyaddr
+struct hostent *gethostbyaddr(const void *addr, int len, int type)
+{
+	static struct hostent ent;
+    static char * aliases[1] = { NULL };
+	char buf[1024];
+	static char sname[MAX_NAME] = "";
+	static struct in_addr saddr = { 0 };
+	static char *addrlist[2] = { (char *) &saddr, NULL };
+	int rid;
+	int err;
+
+	if((len != sizeof(struct in_addr)) || (type != AF_INET) || (addr == NULL))
+	{
+		h_errno = HOST_NOT_FOUND;
+		return NULL;
+	}
+
+	memcpy(&saddr, addr, len);
+
+	if(sceNetResolverCreate(&rid, buf, sizeof(buf)) < 0)
+	{
+		h_errno = NO_RECOVERY;
+		return NULL;
+	}
+
+	err = sceNetResolverStartAtoN(rid, &saddr, sname, sizeof(sname), 2, 3);
+	sceNetResolverStop(rid);
+	sceNetResolverDelete(rid);
+	if(err < 0)
+	{
+		h_errno = HOST_NOT_FOUND;
+		return NULL;
+	}
+
+	ent.h_name = sname;
+	ent.h_aliases = aliases;
+	ent.h_addrtype = AF_INET;
+	ent.h_length = sizeof(struct in_addr);
+	ent.h_addr_list = addrlist;
+	ent.h_addr = (char *) &saddr;
+
+	return &ent;
+}
+#endif
+
+#ifdef F_gethostbyname
+struct hostent *gethostbyname(const char *name)
+{
+	static struct hostent ent;
+	char buf[1024];
+	static char sname[MAX_NAME] = "";
+	static struct in_addr saddr = { 0 };
+	static char *addrlist[2] = { (char *) &saddr, NULL };
+	int rid;
+
+	if(sceNetInetInetAton(name, &saddr) == 0)
+	{
+		int err;
+
+		if(sceNetResolverCreate(&rid, buf, sizeof(buf)) < 0)
+		{
+			h_errno = NO_RECOVERY;
+			return NULL;
+		}
+
+		err = sceNetResolverStartNtoA(rid, name, &saddr, 2, 3);
+		sceNetResolverDelete(rid);
+		if(err < 0)
+		{
+			h_errno = HOST_NOT_FOUND;
+			return NULL;
+		}
+
+	}
+
+	snprintf(sname, MAX_NAME, "%s", name);
+	ent.h_name = sname;
+	ent.h_aliases = 0;
+	ent.h_addrtype = AF_INET;
+	ent.h_length = sizeof(struct in_addr);
+	ent.h_addr_list = addrlist;
+	ent.h_addr = (char *) &saddr;
+
+	return &ent;
+}
+
+#endif