sdk-core v2026.11.05
PS2 SDK - core tier (build-critical EE/IOP modules)
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
26int socket(int domain, int type, int protocol)
27{
28 int fd, iop_fd;
29 _libcglue_fdman_fd_info_t *info;
30
31 if (_libcglue_fdman_socket_ops == NULL || _libcglue_fdman_socket_ops->socket == NULL)
32 {
33 errno = ENOSYS;
34 return -1;
35 }
36
37 fd = __fdman_get_new_descriptor();
38 if (fd == -1)
39 {
40 errno = ENOMEM;
41 return -1;
42 }
43
44 info = &(__descriptormap[fd]->info);
45 iop_fd = _libcglue_fdman_socket_ops->socket(info, domain, type, protocol);
46 if (iop_fd < 0)
47 {
48 __fdman_release_descriptor(fd);
49 return __transform_errno(iop_fd);
50 }
51 __descriptormap[fd]->flags = 0;
52
53 return fd;
54}
55
56int accept(int fd, struct sockaddr *addr, socklen_t *addrlen)
57{
58 int new_iop_fd, new_fd;
59 _libcglue_fdman_fd_info_t *info;
60
61 if (!__IS_FD_VALID(fd)) {
62 errno = EBADF;
63 return -1;
64 }
65
66 _libcglue_fdman_fd_info_t *fdinfo;
67
68 fdinfo = &(__descriptormap[fd]->info);
69 if (fdinfo->ops == NULL || fdinfo->ops->accept == NULL)
70 {
71 errno = ENOSYS;
72 return -1;
73 }
74
75 new_fd = __fdman_get_new_descriptor();
76 if (new_fd == -1)
77 {
78 errno = ENOMEM;
79 return -1;
80 }
81
82 info = &(__descriptormap[new_fd]->info);
83 new_iop_fd = fdinfo->ops->accept(fdinfo->userdata, info, addr, addrlen);
84 if (new_iop_fd < 0)
85 {
86 __fdman_release_descriptor(new_fd);
87 return __transform_errno(new_iop_fd);
88 }
89 __descriptormap[new_fd]->flags = 0;
90 return new_fd;
91}
92
93int bind(int fd, const struct sockaddr *my_addr, socklen_t addrlen)
94{
95 if (!__IS_FD_VALID(fd)) {
96 errno = EBADF;
97 return -1;
98 }
99
100 _libcglue_fdman_fd_info_t *fdinfo;
101
102 fdinfo = &(__descriptormap[fd]->info);
103 if (fdinfo->ops == NULL || fdinfo->ops->bind == NULL)
104 {
105 errno = ENOSYS;
106 return -1;
107 }
108 return __transform_errno(fdinfo->ops->bind(fdinfo->userdata, my_addr, addrlen));
109}
110
111int connect(int fd, const struct sockaddr *serv_addr, socklen_t addrlen)
112{
113 if (!__IS_FD_VALID(fd)) {
114 errno = EBADF;
115 return -1;
116 }
117
118 _libcglue_fdman_fd_info_t *fdinfo;
119
120 fdinfo = &(__descriptormap[fd]->info);
121 if (fdinfo->ops == NULL || fdinfo->ops->connect == NULL)
122 {
123 errno = ENOSYS;
124 return -1;
125 }
126 return __transform_errno(fdinfo->ops->connect(fdinfo->userdata, serv_addr, addrlen));
127}
128
129int listen(int fd, int backlog)
130{
131 if (!__IS_FD_VALID(fd)) {
132 errno = EBADF;
133 return -1;
134 }
135
136 _libcglue_fdman_fd_info_t *fdinfo;
137
138 fdinfo = &(__descriptormap[fd]->info);
139 if (fdinfo->ops == NULL || fdinfo->ops->listen == NULL)
140 {
141 errno = ENOSYS;
142 return -1;
143 }
144 return __transform_errno(fdinfo->ops->listen(fdinfo->userdata, backlog));
145}
146
147ssize_t recv(int fd, void *buf, size_t len, int flags)
148{
149 if (!__IS_FD_VALID(fd)) {
150 errno = EBADF;
151 return -1;
152 }
153
154 _libcglue_fdman_fd_info_t *fdinfo;
155
156 fdinfo = &(__descriptormap[fd]->info);
157 if (fdinfo->ops == NULL || fdinfo->ops->recv == NULL)
158 {
159 errno = ENOSYS;
160 return -1;
161 }
162 return __transform_errno(fdinfo->ops->recv(fdinfo->userdata, buf, len, flags));
163}
164
165ssize_t recvfrom(int fd, void *buf, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen)
166{
167 if (!__IS_FD_VALID(fd)) {
168 errno = EBADF;
169 return -1;
170 }
171
172 _libcglue_fdman_fd_info_t *fdinfo;
173
174 fdinfo = &(__descriptormap[fd]->info);
175 if (fdinfo->ops == NULL || fdinfo->ops->recvfrom == NULL)
176 {
177 errno = ENOSYS;
178 return -1;
179 }
180 return __transform_errno(fdinfo->ops->recvfrom(fdinfo->userdata, buf, len, flags, from, fromlen));
181}
182
183ssize_t recvmsg(int fd, struct msghdr *msg, int flags)
184{
185 if (!__IS_FD_VALID(fd)) {
186 errno = EBADF;
187 return -1;
188 }
189
190 _libcglue_fdman_fd_info_t *fdinfo;
191
192 fdinfo = &(__descriptormap[fd]->info);
193 if (fdinfo->ops == NULL || fdinfo->ops->recvmsg == NULL)
194 {
195 errno = ENOSYS;
196 return -1;
197 }
198 return __transform_errno(fdinfo->ops->recvmsg(fdinfo->userdata, msg, flags));
199}
200
201ssize_t send(int fd, const void *buf, size_t len, int flags)
202{
203 if (!__IS_FD_VALID(fd)) {
204 errno = EBADF;
205 return -1;
206 }
207
208 _libcglue_fdman_fd_info_t *fdinfo;
209
210 fdinfo = &(__descriptormap[fd]->info);
211 if (fdinfo->ops == NULL || fdinfo->ops->send == NULL)
212 {
213 errno = ENOSYS;
214 return -1;
215 }
216 return __transform_errno(fdinfo->ops->send(fdinfo->userdata, buf, len, flags));
217}
218
219ssize_t sendto(int fd, const void *buf, size_t len, int flags, const struct sockaddr *to, socklen_t tolen)
220{
221 if (!__IS_FD_VALID(fd)) {
222 errno = EBADF;
223 return -1;
224 }
225
226 _libcglue_fdman_fd_info_t *fdinfo;
227
228 fdinfo = &(__descriptormap[fd]->info);
229 if (fdinfo->ops == NULL || fdinfo->ops->sendto == NULL)
230 {
231 errno = ENOSYS;
232 return -1;
233 }
234 return __transform_errno(fdinfo->ops->sendto(fdinfo->userdata, buf, len, flags, to, tolen));
235}
236
237ssize_t sendmsg(int fd, const struct msghdr *msg, int flags)
238{
239 if (!__IS_FD_VALID(fd)) {
240 errno = EBADF;
241 return -1;
242 }
243
244 _libcglue_fdman_fd_info_t *fdinfo;
245
246 fdinfo = &(__descriptormap[fd]->info);
247 if (fdinfo->ops == NULL || fdinfo->ops->sendmsg == NULL)
248 {
249 errno = ENOSYS;
250 return -1;
251 }
252 return __transform_errno(fdinfo->ops->sendmsg(fdinfo->userdata, msg, flags));
253}
254
255int getsockopt(int fd, int level, int optname, void *optval, socklen_t *optlen)
256{
257 if (!__IS_FD_VALID(fd)) {
258 errno = EBADF;
259 return -1;
260 }
261
262 _libcglue_fdman_fd_info_t *fdinfo;
263
264 fdinfo = &(__descriptormap[fd]->info);
265 if (fdinfo->ops == NULL || fdinfo->ops->getsockopt == NULL)
266 {
267 errno = ENOSYS;
268 return -1;
269 }
270 return __transform_errno(fdinfo->ops->getsockopt(fdinfo->userdata, level, optname, optval, optlen));
271}
272
273int setsockopt(int fd, int level, int optname, const void *optval, socklen_t optlen)
274{
275 if (!__IS_FD_VALID(fd)) {
276 errno = EBADF;
277 return -1;
278 }
279
280 _libcglue_fdman_fd_info_t *fdinfo;
281
282 fdinfo = &(__descriptormap[fd]->info);
283 if (fdinfo->ops == NULL || fdinfo->ops->setsockopt == NULL)
284 {
285 errno = ENOSYS;
286 return -1;
287 }
288 return __transform_errno(fdinfo->ops->setsockopt(fdinfo->userdata, level, optname, optval, optlen));
289}
290
291int shutdown(int fd, int how)
292{
293 if (!__IS_FD_VALID(fd)) {
294 errno = EBADF;
295 return -1;
296 }
297
298 _libcglue_fdman_fd_info_t *fdinfo;
299
300 fdinfo = &(__descriptormap[fd]->info);
301 if (fdinfo->ops == NULL || fdinfo->ops->shutdown == NULL)
302 {
303 errno = ENOSYS;
304 return -1;
305 }
306 return __transform_errno(fdinfo->ops->shutdown(fdinfo->userdata, how));
307}
308
309int getpeername(int fd, struct sockaddr *name, socklen_t *namelen)
310{
311 if (!__IS_FD_VALID(fd)) {
312 errno = EBADF;
313 return -1;
314 }
315
316 _libcglue_fdman_fd_info_t *fdinfo;
317
318 fdinfo = &(__descriptormap[fd]->info);
319 if (fdinfo->ops == NULL || fdinfo->ops->getpeername == NULL)
320 {
321 errno = ENOSYS;
322 return -1;
323 }
324 return __transform_errno(fdinfo->ops->getpeername(fdinfo->userdata, name, namelen));
325}
326
327int getsockname(int fd, struct sockaddr *name, socklen_t *namelen)
328{
329 if (!__IS_FD_VALID(fd)) {
330 errno = EBADF;
331 return -1;
332 }
333
334 _libcglue_fdman_fd_info_t *fdinfo;
335
336 fdinfo = &(__descriptormap[fd]->info);
337 if (fdinfo->ops == NULL || fdinfo->ops->getsockname == NULL)
338 {
339 errno = ENOSYS;
340 return -1;
341 }
342 return __transform_errno(fdinfo->ops->getsockname(fdinfo->userdata, name, namelen));
343}
344
345u32 libcglue_inet_addr(const char *cp)
346{
347 if (_libcglue_fdman_inet_ops == NULL || _libcglue_fdman_inet_ops->inet_addr == NULL)
348 {
349 return 0;
350 }
351
352 return _libcglue_fdman_inet_ops->inet_addr(cp);
353}
354
355char *libcglue_inet_ntoa(const ip4_addr_t *addr)
356{
357 if (_libcglue_fdman_inet_ops == NULL || _libcglue_fdman_inet_ops->inet_ntoa == NULL)
358 {
359 return NULL;
360 }
361
362 return _libcglue_fdman_inet_ops->inet_ntoa(addr);
363}
364
365char *libcglue_inet_ntoa_r(const ip4_addr_t *addr, char *buf, int buflen)
366{
367 if (_libcglue_fdman_inet_ops == NULL || _libcglue_fdman_inet_ops->inet_ntoa_r == NULL)
368 {
369 return NULL;
370 }
371
372 return _libcglue_fdman_inet_ops->inet_ntoa_r(addr, buf, buflen);
373}
374
375int libcglue_inet_aton(const char *cp, ip4_addr_t *addr)
376{
377 if (_libcglue_fdman_inet_ops == NULL || _libcglue_fdman_inet_ops->inet_aton == NULL)
378 {
379 return 0;
380 }
381
382 return _libcglue_fdman_inet_ops->inet_aton(cp, addr);
383}
384
#define ENOSYS
Definition errno.h:194
#define ENOMEM
Definition errno.h:52
#define EBADF
Definition errno.h:46
struct ip4_addr ip4_addr_t
Definition ps2ip_rpc.h:41