PS2SDK
PS2 Homebrew Libraries
Loading...
Searching...
No Matches
socket.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 <sys/socket.h>
14#include <ps2sdkapi.h>
15#include <string.h>
16#include <fcntl.h>
17#include <errno.h>
18#include <netinet/in.h>
19#include <arpa/inet.h>
20
21#include "fdman.h"
22
23/* The following is defined in glue.c */
24extern int __transform_errno(int res);
25
26#ifdef F_socket
27int socket(int domain, int type, int protocol)
28{
29 int fd, iop_fd;
31
32 if (_libcglue_fdman_socket_ops == NULL || _libcglue_fdman_socket_ops->socket == NULL)
33 {
34 errno = ENOSYS;
35 return -1;
36 }
37
38 fd = __fdman_get_new_descriptor();
39 if (fd == -1)
40 {
41 errno = ENOMEM;
42 return -1;
43 }
44
45 info = &(__descriptormap[fd]->info);
46 iop_fd = _libcglue_fdman_socket_ops->socket(info, domain, type, protocol);
47 if (iop_fd < 0)
48 {
49 __fdman_release_descriptor(fd);
50 return __transform_errno(iop_fd);
51 }
52 __descriptormap[fd]->flags = 0;
53
54 return fd;
55}
56#endif
57
58#ifdef F_accept
59int accept(int fd, struct sockaddr *addr, socklen_t *addrlen)
60{
61 int new_iop_fd, new_fd;
63
64 if (!__IS_FD_VALID(fd)) {
65 errno = EBADF;
66 return -1;
67 }
68
70
71 fdinfo = &(__descriptormap[fd]->info);
72 if (fdinfo->ops == NULL || fdinfo->ops->accept == NULL)
73 {
74 errno = ENOSYS;
75 return -1;
76 }
77
78 new_fd = __fdman_get_new_descriptor();
79 if (new_fd == -1)
80 {
81 errno = ENOMEM;
82 return -1;
83 }
84
85 info = &(__descriptormap[new_fd]->info);
86 new_iop_fd = fdinfo->ops->accept(fdinfo->userdata, info, addr, addrlen);
87 if (new_iop_fd < 0)
88 {
89 __fdman_release_descriptor(new_fd);
90 return __transform_errno(new_iop_fd);
91 }
92 __descriptormap[new_fd]->flags = 0;
93 return new_fd;
94}
95#endif
96
97#ifdef F_bind
98int bind(int fd, const struct sockaddr *my_addr, socklen_t addrlen)
99{
100 if (!__IS_FD_VALID(fd)) {
101 errno = EBADF;
102 return -1;
103 }
104
106
107 fdinfo = &(__descriptormap[fd]->info);
108 if (fdinfo->ops == NULL || fdinfo->ops->bind == NULL)
109 {
110 errno = ENOSYS;
111 return -1;
112 }
113 return __transform_errno(fdinfo->ops->bind(fdinfo->userdata, my_addr, addrlen));
114}
115#endif
116
117#ifdef F_connect
118int connect(int fd, const struct sockaddr *serv_addr, socklen_t addrlen)
119{
120 if (!__IS_FD_VALID(fd)) {
121 errno = EBADF;
122 return -1;
123 }
124
126
127 fdinfo = &(__descriptormap[fd]->info);
128 if (fdinfo->ops == NULL || fdinfo->ops->connect == NULL)
129 {
130 errno = ENOSYS;
131 return -1;
132 }
133 return __transform_errno(fdinfo->ops->connect(fdinfo->userdata, serv_addr, addrlen));
134}
135#endif
136
137#ifdef F_listen
138int listen(int fd, int backlog)
139{
140 if (!__IS_FD_VALID(fd)) {
141 errno = EBADF;
142 return -1;
143 }
144
146
147 fdinfo = &(__descriptormap[fd]->info);
148 if (fdinfo->ops == NULL || fdinfo->ops->listen == NULL)
149 {
150 errno = ENOSYS;
151 return -1;
152 }
153 return __transform_errno(fdinfo->ops->listen(fdinfo->userdata, backlog));
154}
155#endif
156
157#ifdef F_recv
158ssize_t recv(int fd, void *buf, size_t len, int flags)
159{
160 if (!__IS_FD_VALID(fd)) {
161 errno = EBADF;
162 return -1;
163 }
164
166
167 fdinfo = &(__descriptormap[fd]->info);
168 if (fdinfo->ops == NULL || fdinfo->ops->recv == NULL)
169 {
170 errno = ENOSYS;
171 return -1;
172 }
173 return __transform_errno(fdinfo->ops->recv(fdinfo->userdata, buf, len, flags));
174}
175#endif
176
177#ifdef F_recvfrom
178ssize_t recvfrom(int fd, void *buf, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen)
179{
180 if (!__IS_FD_VALID(fd)) {
181 errno = EBADF;
182 return -1;
183 }
184
186
187 fdinfo = &(__descriptormap[fd]->info);
188 if (fdinfo->ops == NULL || fdinfo->ops->recvfrom == NULL)
189 {
190 errno = ENOSYS;
191 return -1;
192 }
193 return __transform_errno(fdinfo->ops->recvfrom(fdinfo->userdata, buf, len, flags, from, fromlen));
194}
195#endif
196
197#ifdef F_recvmsg
198ssize_t recvmsg(int fd, struct msghdr *msg, int flags)
199{
200 if (!__IS_FD_VALID(fd)) {
201 errno = EBADF;
202 return -1;
203 }
204
206
207 fdinfo = &(__descriptormap[fd]->info);
208 if (fdinfo->ops == NULL || fdinfo->ops->recvmsg == NULL)
209 {
210 errno = ENOSYS;
211 return -1;
212 }
213 return __transform_errno(fdinfo->ops->recvmsg(fdinfo->userdata, msg, flags));
214}
215#endif
216
217#ifdef F_send
218ssize_t send(int fd, const void *buf, size_t len, int flags)
219{
220 if (!__IS_FD_VALID(fd)) {
221 errno = EBADF;
222 return -1;
223 }
224
226
227 fdinfo = &(__descriptormap[fd]->info);
228 if (fdinfo->ops == NULL || fdinfo->ops->send == NULL)
229 {
230 errno = ENOSYS;
231 return -1;
232 }
233 return __transform_errno(fdinfo->ops->send(fdinfo->userdata, buf, len, flags));
234}
235#endif
236
237#ifdef F_sendto
238ssize_t sendto(int fd, const void *buf, size_t len, int flags, const struct sockaddr *to, socklen_t tolen)
239{
240 if (!__IS_FD_VALID(fd)) {
241 errno = EBADF;
242 return -1;
243 }
244
246
247 fdinfo = &(__descriptormap[fd]->info);
248 if (fdinfo->ops == NULL || fdinfo->ops->sendto == NULL)
249 {
250 errno = ENOSYS;
251 return -1;
252 }
253 return __transform_errno(fdinfo->ops->sendto(fdinfo->userdata, buf, len, flags, to, tolen));
254}
255#endif
256
257#ifdef F_sendmsg
258ssize_t sendmsg(int fd, const struct msghdr *msg, int flags)
259{
260 if (!__IS_FD_VALID(fd)) {
261 errno = EBADF;
262 return -1;
263 }
264
266
267 fdinfo = &(__descriptormap[fd]->info);
268 if (fdinfo->ops == NULL || fdinfo->ops->sendmsg == NULL)
269 {
270 errno = ENOSYS;
271 return -1;
272 }
273 return __transform_errno(fdinfo->ops->sendmsg(fdinfo->userdata, msg, flags));
274}
275#endif
276
277#ifdef F_getsockopt
278int getsockopt(int fd, int level, int optname, void *optval, socklen_t *optlen)
279{
280 if (!__IS_FD_VALID(fd)) {
281 errno = EBADF;
282 return -1;
283 }
284
286
287 fdinfo = &(__descriptormap[fd]->info);
288 if (fdinfo->ops == NULL || fdinfo->ops->getsockopt == NULL)
289 {
290 errno = ENOSYS;
291 return -1;
292 }
293 return __transform_errno(fdinfo->ops->getsockopt(fdinfo->userdata, level, optname, optval, optlen));
294}
295#endif
296
297#ifdef F_setsockopt
298int setsockopt(int fd, int level, int optname, const void *optval, socklen_t optlen)
299{
300 if (!__IS_FD_VALID(fd)) {
301 errno = EBADF;
302 return -1;
303 }
304
306
307 fdinfo = &(__descriptormap[fd]->info);
308 if (fdinfo->ops == NULL || fdinfo->ops->setsockopt == NULL)
309 {
310 errno = ENOSYS;
311 return -1;
312 }
313 return __transform_errno(fdinfo->ops->setsockopt(fdinfo->userdata, level, optname, optval, optlen));
314}
315#endif
316
317#ifdef F_shutdown
318int shutdown(int fd, int how)
319{
320 if (!__IS_FD_VALID(fd)) {
321 errno = EBADF;
322 return -1;
323 }
324
326
327 fdinfo = &(__descriptormap[fd]->info);
328 if (fdinfo->ops == NULL || fdinfo->ops->shutdown == NULL)
329 {
330 errno = ENOSYS;
331 return -1;
332 }
333 return __transform_errno(fdinfo->ops->shutdown(fdinfo->userdata, how));
334}
335#endif
336
337#ifdef F_getpeername
338int getpeername(int fd, struct sockaddr *name, socklen_t *namelen)
339{
340 if (!__IS_FD_VALID(fd)) {
341 errno = EBADF;
342 return -1;
343 }
344
346
347 fdinfo = &(__descriptormap[fd]->info);
348 if (fdinfo->ops == NULL || fdinfo->ops->getpeername == NULL)
349 {
350 errno = ENOSYS;
351 return -1;
352 }
353 return __transform_errno(fdinfo->ops->getpeername(fdinfo->userdata, name, namelen));
354}
355#endif
356
357#ifdef F_getsockname
358int getsockname(int fd, struct sockaddr *name, socklen_t *namelen)
359{
360 if (!__IS_FD_VALID(fd)) {
361 errno = EBADF;
362 return -1;
363 }
364
366
367 fdinfo = &(__descriptormap[fd]->info);
368 if (fdinfo->ops == NULL || fdinfo->ops->getsockname == NULL)
369 {
370 errno = ENOSYS;
371 return -1;
372 }
373 return __transform_errno(fdinfo->ops->getsockname(fdinfo->userdata, name, namelen));
374}
375#endif
376
377#ifdef F_libcglue_inet_addr
378u32 libcglue_inet_addr(const char *cp)
379{
380 if (_libcglue_fdman_inet_ops == NULL || _libcglue_fdman_inet_ops->inet_addr == NULL)
381 {
382 return 0;
383 }
384
385 return _libcglue_fdman_inet_ops->inet_addr(cp);
386}
387#endif
388
389#ifdef F_libcglue_inet_ntoa
390char *libcglue_inet_ntoa(const ip4_addr_t *addr)
391{
392 if (_libcglue_fdman_inet_ops == NULL || _libcglue_fdman_inet_ops->inet_ntoa == NULL)
393 {
394 return NULL;
395 }
396
397 return _libcglue_fdman_inet_ops->inet_ntoa(addr);
398}
399#endif
400
401#ifdef F_libcglue_inet_ntoa_r
402char *libcglue_inet_ntoa_r(const ip4_addr_t *addr, char *buf, int buflen)
403{
404 if (_libcglue_fdman_inet_ops == NULL || _libcglue_fdman_inet_ops->inet_ntoa_r == NULL)
405 {
406 return NULL;
407 }
408
409 return _libcglue_fdman_inet_ops->inet_ntoa_r(addr, buf, buflen);
410}
411#endif
412
413#ifdef F_libcglue_inet_aton
414int libcglue_inet_aton(const char *cp, ip4_addr_t *addr)
415{
416 if (_libcglue_fdman_inet_ops == NULL || _libcglue_fdman_inet_ops->inet_aton == NULL)
417 {
418 return 0;
419 }
420
421 return _libcglue_fdman_inet_ops->inet_aton(cp, addr);
422}
423#endif
424
425#ifdef F_libcglue_ps2ip_setconfig
426int libcglue_ps2ip_setconfig(t_ip_info *ip_info)
427{
428 if (_libcglue_fdman_socket_ops == NULL || _libcglue_fdman_socket_ops->setconfig == NULL)
429 {
430 return 0;
431 }
432
433 return _libcglue_fdman_socket_ops->setconfig(ip_info);
434}
435#endif
436
437#ifdef F_libcglue_ps2ip_getconfig
438int libcglue_ps2ip_getconfig(char *netif_name, t_ip_info *ip_info)
439{
440 if (_libcglue_fdman_socket_ops == NULL || _libcglue_fdman_socket_ops->getconfig == NULL)
441 {
442 return 0;
443 }
444
445 return _libcglue_fdman_socket_ops->getconfig(netif_name, ip_info);
446}
447#endif
448
449#ifdef F_libcglue_dns_setserver
450void libcglue_dns_setserver(u8 numdns, ip_addr_t *dnsserver)
451{
452 if (_libcglue_fdman_socket_ops == NULL || _libcglue_fdman_socket_ops->dns_setserver == NULL)
453 {
454 return;
455 }
456
457 _libcglue_fdman_socket_ops->dns_setserver(numdns, dnsserver);
458}
459#endif
460
461#ifdef F_libcglue_dns_getserver
462const ip_addr_t *libcglue_dns_getserver(u8 numdns)
463{
464 if (_libcglue_fdman_socket_ops == NULL || _libcglue_fdman_socket_ops->dns_getserver == NULL)
465 {
466 return NULL;
467 }
468
469 return _libcglue_fdman_socket_ops->dns_getserver(numdns);
470}
471#endif
#define ENOSYS
Definition errno.h:185
#define ENOMEM
Definition errno.h:43
#define EBADF
Definition errno.h:37