PS2SDK
PS2 Homebrew Libraries
Loading...
Searching...
No Matches
ps2ip_ps2sdk.c
1/*
2# _____ ___ ____ ___ ____
3# ____| | ____| | | |____|
4# | ___| |____ ___| ____| | \ PS2DEV Open Source Project.
5#-----------------------------------------------------------------------
6# Copyright ps2dev - http://www.ps2dev.org
7# Licenced under Academic Free License version 2.0
8# Review ps2sdk README & LICENSE files for further details.
9*/
10
11#define LIBCGLUE_SYS_SOCKET_NO_ALIASES
12#define LIBCGLUE_ARPA_INET_NO_ALIASES
13#include <ps2sdkapi.h>
14#include <string.h>
15#include <errno.h>
16#include <ps2ipee.h>
17
18_libcglue_fdman_socket_ops_t __ps2ipee_fdman_socket_ops;
19_libcglue_fdman_inet_ops_t __ps2ipee_fdman_inet_ops;
20_libcglue_fdman_fd_ops_t __ps2ipee_fdman_ops_socket;
21
22extern void __ps2ipeeOpsInitializeImpl(void);
23
24__attribute__((constructor))
25static void __ps2ipeeOpsInitialize(void)
26{
27 __ps2ipeeOpsInitializeImpl();
28}
29
30int __ps2ipeeSocketHelper(_libcglue_fdman_fd_info_t *info, int domain, int type, int protocol)
31{
32 int iop_fd;
33
34 iop_fd = lwip_socket(domain, type, protocol);
35 if (iop_fd < 0)
36 {
37 return -errno;
38 }
39 info->userdata = (void *)(uiptr)(u32)iop_fd;
40 info->ops = &__ps2ipee_fdman_ops_socket;
41 return 0;
42}
43
44int __ps2ipeeGetFdHelper(void *userdata)
45{
46 return (int)(u32)(uiptr)userdata;
47}
48
49int __ps2ipeeFcntlfsetflHelper(void *userdata, int newfl)
50{
51 int fd, res;
52 u32 val;
53
54 fd = __ps2ipeeGetFdHelper(userdata);
55
56 if (fd < 0)
57 {
58 return fd;
59 }
60
61 val = (newfl & O_NONBLOCK) ? 1 : 0;
62 res = lwip_ioctl(fd, FIONBIO, &val);
63 if (res < 0)
64 {
65 return -ENFILE;
66 }
67 return res;
68}
69
70int __ps2ipeeAcceptHelper(void *userdata, _libcglue_fdman_fd_info_t *info, struct sockaddr *addr, int *addrlen)
71{
72 int fd, new_fd;
73
74 fd = __ps2ipeeGetFdHelper(userdata);
75
76 if (fd < 0)
77 {
78 return fd;
79 }
80
81 new_fd = lwip_accept(fd, addr, addrlen);
82 if (new_fd < 0)
83 {
84 return -errno;
85 }
86 info->userdata = (void *)(uiptr)(u32)new_fd;
87 info->ops = &__ps2ipee_fdman_ops_socket;
88 return 0;
89}
90
91int __ps2ipeeBindHelper(void *userdata, const struct sockaddr *name, int namelen)
92{
93 int fd, res;
94
95 fd = __ps2ipeeGetFdHelper(userdata);
96
97 if (fd < 0)
98 {
99 return fd;
100 }
101
102 res = lwip_bind(fd, name, namelen);
103 if (res < 0)
104 {
105 return -errno;
106 }
107 return res;
108}
109
110int __ps2ipeeCloseHelper(void *userdata)
111{
112 int fd, res;
113
114 fd = __ps2ipeeGetFdHelper(userdata);
115
116 if (fd < 0)
117 {
118 return fd;
119 }
120
121 res = lwip_close(fd);
122 if (res < 0)
123 {
124 return -errno;
125 }
126 return res;
127}
128
129int __ps2ipeeConnectHelper(void *userdata, const struct sockaddr *name, int namelen)
130{
131 int fd, res;
132
133 fd = __ps2ipeeGetFdHelper(userdata);
134
135 if (fd < 0)
136 {
137 return fd;
138 }
139
140 res = lwip_connect(fd, name, namelen);
141 if (res < 0)
142 {
143 return -errno;
144 }
145 return res;
146}
147
148int __ps2ipeeListenHelper(void *userdata, int backlog)
149{
150 int fd, res;
151
152 fd = __ps2ipeeGetFdHelper(userdata);
153
154 if (fd < 0)
155 {
156 return fd;
157 }
158
159 res = lwip_listen(fd, backlog);
160 if (res < 0)
161 {
162 return -errno;
163 }
164 return res;
165}
166
167int __ps2ipeeRecvHelper(void *userdata, void *mem, size_t len, int flags)
168{
169 int fd, res;
170
171 fd = __ps2ipeeGetFdHelper(userdata);
172
173 if (fd < 0)
174 {
175 return fd;
176 }
177
178 res = lwip_recv(fd, mem, len, flags);
179 if (res < 0)
180 {
181 return -errno;
182 }
183 return res;
184}
185
186int __ps2ipeeRecvfromHelper(void *userdata, void *mem, size_t len, int flags, struct sockaddr *from, int *fromlen)
187{
188 int fd, res;
189
190 fd = __ps2ipeeGetFdHelper(userdata);
191
192 if (fd < 0)
193 {
194 return fd;
195 }
196
197 res = lwip_recvfrom(fd, mem, len, flags, from, fromlen);
198 if (res < 0)
199 {
200 return -errno;
201 }
202 return res;
203}
204
205int __ps2ipeeRecvmsgHelper(void *userdata, struct msghdr *msg, int flags)
206{
207 // TODO
208 return -1;
209}
210
211int __ps2ipeeSendHelper(void *userdata, const void *dataptr, size_t size, int flags)
212{
213 int fd, res;
214
215 fd = __ps2ipeeGetFdHelper(userdata);
216
217 if (fd < 0)
218 {
219 return fd;
220 }
221
222 res = lwip_send(fd, dataptr, size, flags);
223 if (res < 0)
224 {
225 return -errno;
226 }
227 return res;
228}
229
230int __ps2ipeeSendtoHelper(void *userdata, const void *dataptr, size_t size, int flags, const struct sockaddr *to, int tolen)
231{
232 int fd, res;
233
234 fd = __ps2ipeeGetFdHelper(userdata);
235
236 if (fd < 0)
237 {
238 return fd;
239 }
240
241 res = lwip_sendto(fd, dataptr, size, flags, to, tolen);
242 if (res < 0)
243 {
244 return -errno;
245 }
246 return res;
247}
248
249int __ps2ipeeSendmsgHelper(void *userdata, const struct msghdr *msg, int flags) {
250 // TODO
251 return -1;
252}
253
254int __ps2ipeeIoctlHelper(void *userdata, int cmd, void *argp)
255{
256 int fd, res;
257
258 fd = __ps2ipeeGetFdHelper(userdata);
259
260 if (fd < 0)
261 {
262 return fd;
263 }
264
265 res = lwip_ioctl(fd, cmd, argp);
266 if (res < 0)
267 {
268 return -errno;
269 }
270 return res;
271}
272
273int __ps2ipeeGetsocknameHelper(void *userdata, struct sockaddr* name, int* namelen)
274{
275 int fd, res;
276
277 fd = __ps2ipeeGetFdHelper(userdata);
278
279 if (fd < 0)
280 {
281 return fd;
282 }
283
284 res = lwip_getsockname(fd, name, namelen);
285 if (res < 0)
286 {
287 return -errno;
288 }
289 return res;
290}
291
292int __ps2ipeeGetpeernameHelper(void *userdata, struct sockaddr *name, int *namelen)
293{
294 int fd, res;
295
296 fd = __ps2ipeeGetFdHelper(userdata);
297
298 if (fd < 0)
299 {
300 return fd;
301 }
302
303 res = lwip_getpeername(fd, name, namelen);
304 if (res < 0)
305 {
306 return -errno;
307 }
308 return res;
309}
310
311int __ps2ipeeGetsockoptHelper(void *userdata, int level, int optname, void* optval, socklen_t* optlen)
312{
313 int fd, res;
314
315 fd = __ps2ipeeGetFdHelper(userdata);
316
317 if (fd < 0)
318 {
319 return fd;
320 }
321
322 res = lwip_getsockopt(fd, level, optname, optval, optlen);
323 if (res < 0)
324 {
325 return -errno;
326 }
327 return res;
328}
329
330int __ps2ipeeSetsockoptHelper(void *userdata, int level, int optname, const void *optval, socklen_t optlen)
331{
332 int fd, res;
333
334 fd = __ps2ipeeGetFdHelper(userdata);
335
336 if (fd < 0)
337 {
338 return fd;
339 }
340
341 res = lwip_setsockopt(fd, level, optname, optval, optlen);
342 if (res < 0)
343 {
344 return -errno;
345 }
346 return res;
347}
348
349int __ps2ipeeShutdownHelper(void *userdata, int how)
350{
351 int fd, res;
352
353 fd = __ps2ipeeGetFdHelper(userdata);
354
355 if (fd < 0)
356 {
357 return fd;
358 }
359
360 res = lwip_shutdown(fd, how);
361 if (res < 0)
362 {
363 return -errno;
364 }
365 return res;
366}
367
368int __ps2ipeeReadHelper(void *userdata, void *mem, int len)
369{
370 int fd, res;
371
372 fd = __ps2ipeeGetFdHelper(userdata);
373
374 if (fd < 0)
375 {
376 return fd;
377 }
378
379 res = lwip_read(fd, mem, len);
380 if (res < 0)
381 {
382 return -errno;
383 }
384 return res;
385}
386
387int __ps2ipeeWriteHelper(void *userdata, const void *mem, int len)
388{
389 int fd, res;
390
391 fd = __ps2ipeeGetFdHelper(userdata);
392
393 if (fd < 0)
394 {
395 return fd;
396 }
397
398 res = lwip_write(fd, mem, len);
399 if (res < 0)
400 {
401 return -errno;
402 }
403 return res;
404}
405
406struct hostent *ps2ip_gethostbyaddr(const void *addr, int len, int type) {
407 // TODO
408 return NULL;
409}
410
411
412void __ps2ipeeOpsInitializeImpl(void)
413{
414 memset(&__ps2ipee_fdman_socket_ops, 0, sizeof(__ps2ipee_fdman_socket_ops));
415 __ps2ipee_fdman_socket_ops.setconfig = ps2ip_setconfig;
416 __ps2ipee_fdman_socket_ops.getconfig = ps2ip_getconfig;
417 __ps2ipee_fdman_socket_ops.dns_setserver = dns_setserver;
418 __ps2ipee_fdman_socket_ops.dns_getserver = dns_getserver;
419 __ps2ipee_fdman_socket_ops.socket = __ps2ipeeSocketHelper;
420 __ps2ipee_fdman_socket_ops.select = lwip_select;
421 __ps2ipee_fdman_socket_ops.gethostbyaddr = ps2ip_gethostbyaddr;
422 __ps2ipee_fdman_socket_ops.gethostbyname = lwip_gethostbyname;
423 __ps2ipee_fdman_socket_ops.gethostbyname_r = lwip_gethostbyname_r;
424 __ps2ipee_fdman_socket_ops.freeaddrinfo = lwip_freeaddrinfo;
425 __ps2ipee_fdman_socket_ops.getaddrinfo = lwip_getaddrinfo;
426
427 memset(&__ps2ipee_fdman_inet_ops, 0, sizeof(__ps2ipee_fdman_inet_ops));
428 __ps2ipee_fdman_inet_ops.inet_addr = ipaddr_addr;
429 __ps2ipee_fdman_inet_ops.inet_ntoa = ip4addr_ntoa;
430 __ps2ipee_fdman_inet_ops.inet_ntoa_r = ip4addr_ntoa_r;
431 __ps2ipee_fdman_inet_ops.inet_aton = ip4addr_aton;
432
433 memset(&__ps2ipee_fdman_ops_socket, 0, sizeof(__ps2ipee_fdman_ops_socket));
434 __ps2ipee_fdman_ops_socket.getfd = __ps2ipeeGetFdHelper;
435 __ps2ipee_fdman_ops_socket.fcntl_f_setfl = __ps2ipeeFcntlfsetflHelper;
436
437 __ps2ipee_fdman_ops_socket.accept = __ps2ipeeAcceptHelper;
438 __ps2ipee_fdman_ops_socket.bind = __ps2ipeeBindHelper;
439 __ps2ipee_fdman_ops_socket.close = __ps2ipeeCloseHelper;
440 __ps2ipee_fdman_ops_socket.connect = __ps2ipeeConnectHelper;
441 __ps2ipee_fdman_ops_socket.listen = __ps2ipeeListenHelper;
442 __ps2ipee_fdman_ops_socket.recv = __ps2ipeeRecvHelper;
443 __ps2ipee_fdman_ops_socket.recvfrom = __ps2ipeeRecvfromHelper;
444 __ps2ipee_fdman_ops_socket.recvmsg = __ps2ipeeRecvmsgHelper;
445 __ps2ipee_fdman_ops_socket.send = __ps2ipeeSendHelper;
446 __ps2ipee_fdman_ops_socket.sendto = __ps2ipeeSendtoHelper;
447 __ps2ipee_fdman_ops_socket.sendmsg = __ps2ipeeSendmsgHelper;
448 __ps2ipee_fdman_ops_socket.ioctl = __ps2ipeeIoctlHelper;
449 __ps2ipee_fdman_ops_socket.getsockname = __ps2ipeeGetsocknameHelper;
450 __ps2ipee_fdman_ops_socket.getpeername = __ps2ipeeGetpeernameHelper;
451 __ps2ipee_fdman_ops_socket.getsockopt = __ps2ipeeGetsockoptHelper;
452 __ps2ipee_fdman_ops_socket.setsockopt = __ps2ipeeSetsockoptHelper;
453 __ps2ipee_fdman_ops_socket.shutdown = __ps2ipeeShutdownHelper;
454 __ps2ipee_fdman_ops_socket.read = __ps2ipeeReadHelper;
455 __ps2ipee_fdman_ops_socket.write = __ps2ipeeWriteHelper;
456}
457
458/* Backup pointer functions to restore after exit ps2ipee */
459static _libcglue_fdman_socket_ops_t *_backup_libcglue_fdman_socket_ops;
460
461void _ps2sdk_ps2ipee_init(void)
462{
463 _backup_libcglue_fdman_socket_ops = _libcglue_fdman_socket_ops;
464 _libcglue_fdman_socket_ops = &__ps2ipee_fdman_socket_ops;
465}
466
467void _ps2sdk_ps2ipee_deinit(void)
468{
469 _libcglue_fdman_socket_ops = _backup_libcglue_fdman_socket_ops;
470}
#define ENFILE
Definition errno.h:65