PS2SDK
PS2 Homebrew Libraries
Loading...
Searching...
No Matches
ps2ipc_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 <ps2ipc.h>
17
18_libcglue_fdman_socket_ops_t __ps2ipc_fdman_socket_ops;
19_libcglue_fdman_fd_ops_t __ps2ipc_fdman_ops_socket;
20
21extern void __ps2ipcOpsInitializeImpl(void);
22
23__attribute__((constructor))
24static void __ps2ipcOpsInitialize(void)
25{
26 __ps2ipcOpsInitializeImpl();
27}
28
29int __ps2ipcSocketHelper(_libcglue_fdman_fd_info_t *info, int domain, int type, int protocol)
30{
31 int iop_fd;
32
33 iop_fd = ps2ipc_socket(domain, type, protocol);
34 if (iop_fd < 0)
35 {
36 return -ENFILE;
37 }
38 info->userdata = (void *)(uiptr)(u32)iop_fd;
39 info->ops = &__ps2ipc_fdman_ops_socket;
40 return 0;
41}
42
43int __ps2ipcGetFdHelper(void *userdata)
44{
45 return (int)(u32)(uiptr)userdata;
46}
47
48int __ps2ipcFcntlfsetflHelper(void *userdata, int newfl)
49{
50 int fd, res;
51 u32 val;
52
53 fd = __ps2ipcGetFdHelper(userdata);
54
55 if (fd < 0)
56 {
57 return fd;
58 }
59
60 val = (newfl & O_NONBLOCK) ? 1 : 0;
61 res = ps2ipc_ioctl(fd, FIONBIO, &val);
62 if (res < 0)
63 {
64 return -ENFILE;
65 }
66 return res;
67}
68
69int __ps2ipcAcceptHelper(void *userdata, _libcglue_fdman_fd_info_t *info, struct sockaddr *addr, int *addrlen)
70{
71 int fd, new_fd;
72
73 fd = __ps2ipcGetFdHelper(userdata);
74
75 if (fd < 0)
76 {
77 return fd;
78 }
79
80 new_fd = ps2ipc_accept(fd, addr, addrlen);
81 if (new_fd < 0)
82 {
83 return -ENFILE;
84 }
85 info->userdata = (void *)(uiptr)(u32)new_fd;
86 info->ops = &__ps2ipc_fdman_ops_socket;
87 return 0;
88}
89
90int __ps2ipcBindHelper(void *userdata, const struct sockaddr *name, int namelen)
91{
92 int fd, res;
93
94 fd = __ps2ipcGetFdHelper(userdata);
95
96 if (fd < 0)
97 {
98 return fd;
99 }
100
101 res = ps2ipc_bind(fd, name, namelen);
102 if (res < 0)
103 {
104 return -ENFILE;
105 }
106 return res;
107}
108
109int __ps2ipcCloseHelper(void *userdata)
110{
111 int fd, res;
112
113 fd = __ps2ipcGetFdHelper(userdata);
114
115 if (fd < 0)
116 {
117 return fd;
118 }
119
120 res = ps2ipc_disconnect(fd);
121 if (res < 0)
122 {
123 return -ENFILE;
124 }
125 return res;
126}
127
128int __ps2ipcConnectHelper(void *userdata, const struct sockaddr *name, int namelen)
129{
130 int fd, res;
131
132 fd = __ps2ipcGetFdHelper(userdata);
133
134 if (fd < 0)
135 {
136 return fd;
137 }
138
139 res = ps2ipc_connect(fd, name, namelen);
140 if (res < 0)
141 {
142 return -ENFILE;
143 }
144 return res;
145}
146
147int __ps2ipcListenHelper(void *userdata, int backlog)
148{
149 int fd, res;
150
151 fd = __ps2ipcGetFdHelper(userdata);
152
153 if (fd < 0)
154 {
155 return fd;
156 }
157
158 res = ps2ipc_listen(fd, backlog);
159 if (res < 0)
160 {
161 return -ENFILE;
162 }
163 return res;
164}
165
166int __ps2ipcRecvHelper(void *userdata, void *mem, size_t len, int flags)
167{
168 int fd, res;
169
170 fd = __ps2ipcGetFdHelper(userdata);
171
172 if (fd < 0)
173 {
174 return fd;
175 }
176
177 res = ps2ipc_recv(fd, mem, len, flags);
178 if (res < 0)
179 {
180 return -ENFILE;
181 }
182 return res;
183}
184
185int __ps2ipcRecvfromHelper(void *userdata, void *mem, size_t len, int flags, struct sockaddr *from, int *fromlen)
186{
187 int fd, res;
188
189 fd = __ps2ipcGetFdHelper(userdata);
190
191 if (fd < 0)
192 {
193 return fd;
194 }
195
196 res = ps2ipc_recvfrom(fd, mem, len, flags, from, fromlen);
197 if (res < 0)
198 {
199 return -ENFILE;
200 }
201 return res;
202}
203
204int __ps2ipcRecvmsgHelper(void *userdata, struct msghdr *msg, int flags)
205{
206 // TODO
207 return -1;
208}
209
210int __ps2ipcSendHelper(void *userdata, const void *dataptr, size_t len, int flags)
211{
212 int fd, res;
213
214 fd = __ps2ipcGetFdHelper(userdata);
215
216 if (fd < 0)
217 {
218 return fd;
219 }
220
221 res = ps2ipc_send(fd, dataptr, len, flags);
222 if (res < 0)
223 {
224 return -ENFILE;
225 }
226 return res;
227}
228
229int __ps2ipcSendtoHelper(void *userdata, const void *dataptr, size_t len, int flags, const struct sockaddr *to, int tolen)
230{
231 int fd, res;
232
233 fd = __ps2ipcGetFdHelper(userdata);
234
235 if (fd < 0)
236 {
237 return fd;
238 }
239
240 res = ps2ipc_sendto(fd, dataptr, len, flags, to, tolen);
241 if (res < 0)
242 {
243 return -ENFILE;
244 }
245 return res;
246}
247
248int __ps2ipcSendmsgHelper(void *userdata, const struct msghdr *msg, int flags) {
249 // TODO
250 return -1;
251}
252
253
254int __ps2ipcIoctlHelper(void *userdata, int cmd, void *argp)
255{
256 int fd, res;
257
258 fd = __ps2ipcGetFdHelper(userdata);
259
260 if (fd < 0)
261 {
262 return fd;
263 }
264
265 res = ps2ipc_ioctl(fd, cmd, argp);
266 if (res < 0)
267 {
268 return -ENFILE;
269 }
270 return res;
271}
272
273int __ps2ipcGetsocknameHelper(void *userdata, struct sockaddr* name, int* namelen)
274{
275 int fd, res;
276
277 fd = __ps2ipcGetFdHelper(userdata);
278
279 if (fd < 0)
280 {
281 return fd;
282 }
283
284 res = ps2ipc_getsockname(fd, name, namelen);
285 if (res < 0)
286 {
287 return -ENFILE;
288 }
289 return res;
290}
291
292int __ps2ipcGetpeernameHelper(void *userdata, struct sockaddr *name, int *namelen)
293{
294 int fd, res;
295
296 fd = __ps2ipcGetFdHelper(userdata);
297
298 if (fd < 0)
299 {
300 return fd;
301 }
302
303 res = ps2ipc_getpeername(fd, name, namelen);
304 if (res < 0)
305 {
306 return -ENFILE;
307 }
308 return res;
309}
310
311int __ps2ipcGetsockoptHelper(void *userdata, int level, int optname, void* optval, socklen_t* optlen)
312{
313 int fd, res;
314
315 fd = __ps2ipcGetFdHelper(userdata);
316
317 if (fd < 0)
318 {
319 return fd;
320 }
321
322 res = ps2ipc_getsockopt(fd, level, optname, optval, optlen);
323 if (res < 0)
324 {
325 return -ENFILE;
326 }
327 return res;
328}
329
330int __ps2ipcSetsockoptHelper(void *userdata, int level, int optname, const void *optval, socklen_t optlen)
331{
332 int fd, res;
333
334 fd = __ps2ipcGetFdHelper(userdata);
335
336 if (fd < 0)
337 {
338 return fd;
339 }
340
341 res = ps2ipc_setsockopt(fd, level, optname, optval, optlen);
342 if (res < 0)
343 {
344 return -ENFILE;
345 }
346 return res;
347}
348
349struct hostent *ps2ipc_gethostbyaddr(const void *addr, int len, int type) {
350 // TODO
351 return NULL;
352}
353
354void __ps2ipcOpsInitializeImpl(void)
355{
356 memset(&__ps2ipc_fdman_socket_ops, 0, sizeof(__ps2ipc_fdman_socket_ops));
357 __ps2ipc_fdman_socket_ops.setconfig = ps2ipc_ps2ip_setconfig;
358 __ps2ipc_fdman_socket_ops.getconfig = ps2ipc_ps2ip_getconfig;
359 __ps2ipc_fdman_socket_ops.dns_setserver = ps2ipc_dns_setserver;
360 __ps2ipc_fdman_socket_ops.dns_getserver = ps2ipc_dns_getserver;
361 __ps2ipc_fdman_socket_ops.socket = __ps2ipcSocketHelper;
362 __ps2ipc_fdman_socket_ops.select = ps2ipc_select;
363 __ps2ipc_fdman_socket_ops.gethostbyaddr = ps2ipc_gethostbyaddr;
364 __ps2ipc_fdman_socket_ops.gethostbyname = ps2ipc_gethostbyname;
365
366 memset(&__ps2ipc_fdman_ops_socket, 0, sizeof(__ps2ipc_fdman_ops_socket));
367 __ps2ipc_fdman_ops_socket.getfd = __ps2ipcGetFdHelper;
368 __ps2ipc_fdman_ops_socket.fcntl_f_setfl = __ps2ipcFcntlfsetflHelper;
369 __ps2ipc_fdman_ops_socket.accept = __ps2ipcAcceptHelper;
370 __ps2ipc_fdman_ops_socket.bind = __ps2ipcBindHelper;
371 __ps2ipc_fdman_ops_socket.close = __ps2ipcCloseHelper;
372 __ps2ipc_fdman_ops_socket.connect = __ps2ipcConnectHelper;
373 __ps2ipc_fdman_ops_socket.listen = __ps2ipcListenHelper;
374 __ps2ipc_fdman_ops_socket.recv = __ps2ipcRecvHelper;
375 __ps2ipc_fdman_ops_socket.recvfrom = __ps2ipcRecvfromHelper;
376 __ps2ipc_fdman_ops_socket.recvmsg = __ps2ipcRecvmsgHelper;
377 __ps2ipc_fdman_ops_socket.send = __ps2ipcSendHelper;
378 __ps2ipc_fdman_ops_socket.sendto = __ps2ipcSendtoHelper;
379 __ps2ipc_fdman_ops_socket.sendmsg = __ps2ipcSendmsgHelper;
380 __ps2ipc_fdman_ops_socket.ioctl = __ps2ipcIoctlHelper;
381 __ps2ipc_fdman_ops_socket.getsockname = __ps2ipcGetsocknameHelper;
382 __ps2ipc_fdman_ops_socket.getpeername = __ps2ipcGetpeernameHelper;
383 __ps2ipc_fdman_ops_socket.getsockopt = __ps2ipcGetsockoptHelper;
384 __ps2ipc_fdman_ops_socket.setsockopt = __ps2ipcSetsockoptHelper;
385}
386
387/* Backup pointer functions to restore after exit ps2ipc */
388static _libcglue_fdman_socket_ops_t *_backup_libcglue_fdman_socket_ops;
389
390void _ps2sdk_ps2ipc_init()
391{
392 _backup_libcglue_fdman_socket_ops = _libcglue_fdman_socket_ops;
393 _libcglue_fdman_socket_ops = &__ps2ipc_fdman_socket_ops;
394}
395
396void _ps2sdk_ps2ipc_deinit()
397{
398 _libcglue_fdman_socket_ops = _backup_libcglue_fdman_socket_ops;
399}
#define ENFILE
Definition errno.h:65