24static int _init_check = 0;
25static int lock_sema = -1;
45 gethostbyname_res_pkt gethostbyname_res_pkt;
46 dns_setserver_pkt dns_setserver_pkt;
47 dns_getserver_res_pkt dns_getserver_res_pkt;
54static ip_addr_t dns_servers[DNS_MAX_SERVERS];
61extern void _ps2sdk_ps2ipc_init(
void);
62extern void _ps2sdk_ps2ipc_deinit(
void);
70 if(SifBindRpc(&_ps2ip, PS2IP_IRX, 0) < 0)
73 if(_ps2ip.server != NULL)
81 sema.option = (u32)
"ps2ipc";
83 lock_sema = CreateSema(&sema);
85 _ps2sdk_ps2ipc_init();
92void ps2ip_deinit(
void)
94 _ps2sdk_ps2ipc_deinit();
97 DeleteSema(lock_sema);
103int ps2ipc_accept(
int s,
struct sockaddr *addr,
int *addrlen)
106 cmd_pkt *pkt = &_rpc_buffer.cmd_pkt;
108 if(!_init_check)
return -1;
114 if (SifCallRpc(&_ps2ip, PS2IPS_ID_ACCEPT, 0, (
void*)pkt,
sizeof(s32), (
void*)pkt,
sizeof(
cmd_pkt), NULL, NULL) < 0)
116 SignalSema(lock_sema);
122 if(pkt->len < *addrlen) *addrlen = pkt->len;
123 memcpy((
void *)addr, (
void *)&pkt->sockaddr, *addrlen);
126 result = pkt->socket;
128 SignalSema(lock_sema);
133int ps2ipc_bind(
int s,
const struct sockaddr *name,
int namelen)
135 cmd_pkt *pkt = &_rpc_buffer.cmd_pkt;
138 if(!_init_check)
return -1;
144 memcpy((
void *)&pkt->sockaddr, (
void *)name,
sizeof(
struct sockaddr));
146 if (SifCallRpc(&_ps2ip, PS2IPS_ID_BIND, 0, (
void*)pkt,
sizeof(
cmd_pkt), (
void*)&_rpc_buffer.result,
sizeof(s32), NULL, NULL) < 0)
148 SignalSema(lock_sema);
152 result = _rpc_buffer.result;
154 SignalSema(lock_sema);
159int ps2ipc_disconnect(
int s)
163 if(!_init_check)
return -1;
169 if (SifCallRpc(&_ps2ip, PS2IPS_ID_DISCONNECT, 0, (
void*)&_rpc_buffer.s,
sizeof(s32), (
void*)&_rpc_buffer.result,
sizeof(s32), NULL, NULL) < 0)
171 SignalSema(lock_sema);
175 result = _rpc_buffer.result;
177 SignalSema(lock_sema);
182int ps2ipc_connect(
int s,
const struct sockaddr *name,
int namelen)
185 cmd_pkt *pkt = &_rpc_buffer.cmd_pkt;
187 if(!_init_check)
return -1;
193 memcpy((
void *)&pkt->sockaddr, (
void *)name,
sizeof(
struct sockaddr));
195 if (SifCallRpc(&_ps2ip, PS2IPS_ID_CONNECT, 0, (
void*)pkt,
sizeof(
cmd_pkt), (
void*)&_rpc_buffer.result,
sizeof(s32), NULL, NULL) < 0)
197 SignalSema(lock_sema);
201 result = _rpc_buffer.result;
203 SignalSema(lock_sema);
208int ps2ipc_listen(
int s,
int backlog)
213 if(!_init_check)
return -1;
218 pkt->backlog = backlog;
220 if (SifCallRpc(&_ps2ip, PS2IPS_ID_LISTEN, 0, (
void*)pkt,
sizeof(
listen_pkt), (
void*)&_rpc_buffer.result,
sizeof(s32), NULL, NULL) < 0)
222 SignalSema(lock_sema);
226 result = _rpc_buffer.result;
228 SignalSema(lock_sema);
233static void recv_intr(
void *data_raw)
235 rests_pkt *rests = UNCACHED_SEG(data_raw);
239 for(i = 0; i < rests->ssize; i++)
240 rests->sbuf[i] = rests->sbuffer[i];
243 for(i = 0; i < rests->esize; i++)
244 rests->ebuf[i] = rests->ebuffer[i];
248int ps2ipc_recv(
int s,
void *mem,
int len,
unsigned int flags)
252 r_recv_pkt *recv_pkt = &_rpc_buffer.r_recv_pkt;
254 if(!_init_check)
return -1;
264 if( !IS_UNCACHED_SEG(mem))
265 SifWriteBackDCache(mem, len);
268 (
void*)recv_pkt,
sizeof(
r_recv_pkt), recv_intr, _intr_data) < 0)
270 SignalSema(lock_sema);
274 result = recv_pkt->ret;
276 SignalSema(lock_sema);
281int ps2ipc_recvfrom(
int s,
void *mem,
int len,
unsigned int flags,
282 struct sockaddr *from,
int *fromlen)
286 r_recv_pkt *recv_pkt = &_rpc_buffer.r_recv_pkt;
288 if(!_init_check)
return -1;
298 if( !IS_UNCACHED_SEG(mem))
299 SifWriteBackDCache(mem, len);
302 (
void*)recv_pkt,
sizeof(
r_recv_pkt), recv_intr, _intr_data) < 0)
304 SignalSema(lock_sema);
308 memcpy((
void *)from, (
void *)&recv_pkt->sockaddr,
sizeof(
struct sockaddr));
311 result = recv_pkt->ret;
313 SignalSema(lock_sema);
318int ps2ipc_send(
int s,
const void *dataptr,
int size,
unsigned int flags)
321 send_pkt *pkt = &_rpc_buffer.send_pkt;
329 pkt->ee_addr = (
void *)dataptr;
331 if((u32)dataptr & 0x3f)
333 miss = 64 - ((u32)dataptr & 0x3f);
334 if(miss > size) miss = size;
343 if( !IS_UNCACHED_SEG(dataptr))
344 SifWriteBackDCache((
void *)dataptr, size);
346 memcpy((
void *)pkt->
malign_buff, UNCACHED_SEG(dataptr), miss);
348 if (SifCallRpc(&_ps2ip, PS2IPS_ID_SEND, 0, (
void*)pkt,
sizeof(
send_pkt),
349 (
void*)&_rpc_buffer.result,
sizeof(s32), NULL, NULL) < 0)
351 SignalSema(lock_sema);
355 result = _rpc_buffer.result;
357 SignalSema(lock_sema);
362int ps2ipc_sendto(
int s,
const void *dataptr,
int size,
unsigned int flags,
363 const struct sockaddr *to,
int tolen)
366 send_pkt *pkt = &_rpc_buffer.send_pkt;
376 pkt->ee_addr = (
void *)dataptr;
377 memcpy((
void *)&pkt->sockaddr, (
void *)to,
sizeof(
struct sockaddr));
379 if((u32)dataptr & 0x3f)
381 miss = 64 - ((u32)dataptr & 0x3f);
382 if(miss > size) miss = size;
391 if( !IS_UNCACHED_SEG(dataptr))
392 SifWriteBackDCache((
void *)dataptr, size);
394 memcpy((
void *)pkt->
malign_buff, UNCACHED_SEG(dataptr), miss);
396 if (SifCallRpc(&_ps2ip, PS2IPS_ID_SENDTO, 0, (
void*)pkt,
sizeof(
send_pkt),
397 (
void*)&_rpc_buffer.result,
sizeof(s32), NULL, NULL) < 0)
399 SignalSema(lock_sema);
403 result = _rpc_buffer.result;
405 SignalSema(lock_sema);
410int ps2ipc_socket(
int domain,
int type,
int protocol)
415 if(!_init_check)
return -1;
419 pkt->domain = domain;
421 pkt->protocol = protocol;
423 if (SifCallRpc(&_ps2ip, PS2IPS_ID_SOCKET, 0, (
void*)pkt,
sizeof(
socket_pkt), (
void*)&_rpc_buffer.result,
sizeof(s32), NULL, NULL) < 0)
425 SignalSema(lock_sema);
429 result = _rpc_buffer.result;
431 SignalSema(lock_sema);
436int ps2ipc_ps2ip_setconfig(
const t_ip_info *ip_info)
440 if(!_init_check)
return -1;
445 memcpy(&_rpc_buffer.ip_info, ip_info,
sizeof(
t_ip_info));
447 if (SifCallRpc(&_ps2ip, PS2IPS_ID_SETCONFIG, 0, (
void*)&_rpc_buffer.ip_info,
sizeof(
t_ip_info), (
void*)&_rpc_buffer.result,
sizeof(s32), NULL, NULL) < 0)
449 SignalSema(lock_sema);
453 result = _rpc_buffer.result;
455 SignalSema(lock_sema);
460int ps2ipc_ps2ip_getconfig(
char *netif_name,
t_ip_info *ip_info)
462 if(!_init_check)
return -1;
467 strncpy(_rpc_buffer.netif_name, netif_name,
sizeof(_rpc_buffer.netif_name));
468 _rpc_buffer.netif_name[
sizeof(_rpc_buffer.netif_name) - 1] =
'\0';
470 if (SifCallRpc(&_ps2ip, PS2IPS_ID_GETCONFIG, 0, (
void*)_rpc_buffer.netif_name,
sizeof(_rpc_buffer.netif_name), (
void*)&_rpc_buffer.ip_info,
sizeof(
t_ip_info), NULL, NULL) < 0)
472 SignalSema(lock_sema);
477 memcpy(ip_info, &_rpc_buffer.ip_info,
sizeof(
t_ip_info));
479 SignalSema(lock_sema);
484int ps2ipc_select(
int maxfdp1,
struct fd_set *readset,
struct fd_set *writeset,
struct fd_set *exceptset,
struct timeval *timeout)
489 if(!_init_check)
return -1;
493 pkt->maxfdp1 = maxfdp1;
494 pkt->readset_p = readset;
495 pkt->writeset_p = writeset;
496 pkt->exceptset_p = exceptset;
497 pkt->timeout_p = timeout;
499 pkt->timeout = *timeout;
502 pkt->readset = *readset;
505 pkt->writeset = *writeset;
508 pkt->exceptset = *exceptset;
510 if (SifCallRpc(&_ps2ip, PS2IPS_ID_SELECT, 0, (
void*)pkt,
sizeof(
select_pkt), (
void*)pkt,
sizeof(
select_pkt), NULL, NULL) < 0)
512 SignalSema(lock_sema);
517 *timeout = pkt->timeout;
520 *readset = pkt->readset;
523 *writeset = pkt->writeset;
526 *exceptset = pkt->exceptset;
528 result = pkt->result;
530 SignalSema(lock_sema);
535int ps2ipc_ioctl(
int s,
long cmd,
void *argp)
540 if(!_init_check)
return -1;
548 pkt->value = *(s32*)argp;
550 if (SifCallRpc(&_ps2ip, PS2IPS_ID_IOCTL, 0, (
void*)pkt,
sizeof(
ioctl_pkt), (
void*)pkt,
sizeof(
ioctl_pkt), NULL, NULL) < 0)
552 SignalSema(lock_sema);
557 *(s32*)argp = pkt->value;
559 result = pkt->result;
561 SignalSema(lock_sema);
566int ps2ipc_getsockname(
int s,
struct sockaddr *name,
int *namelen)
569 cmd_pkt *pkt = &_rpc_buffer.cmd_pkt;
571 if(!_init_check)
return -1;
577 if (SifCallRpc(&_ps2ip, PS2IPS_ID_GETSOCKNAME, 0, (
void*)pkt,
sizeof(pkt->socket), (
void*)pkt,
sizeof(
cmd_pkt), NULL, NULL) < 0)
579 SignalSema(lock_sema);
583 if(pkt->len < *namelen) *namelen = pkt->len;
584 memcpy((
void *)name, (
void *)&pkt->sockaddr, *namelen);
586 result = pkt->socket;
588 SignalSema(lock_sema);
593int ps2ipc_getpeername(
int s,
struct sockaddr *name,
int *namelen)
596 cmd_pkt *pkt = &_rpc_buffer.cmd_pkt;
598 if(!_init_check)
return -1;
604 if (SifCallRpc(&_ps2ip, PS2IPS_ID_GETPEERNAME, 0, (
void*)pkt,
sizeof(pkt->socket), (
void*)pkt,
sizeof(
cmd_pkt), NULL, NULL) < 0)
606 SignalSema(lock_sema);
610 if(pkt->len < *namelen) *namelen = pkt->len;
611 memcpy((
void *)name, (
void *)&pkt->sockaddr, *namelen);
613 result = pkt->socket;
615 SignalSema(lock_sema);
620int ps2ipc_getsockopt(
int s,
int level,
int optname,
void* optval, socklen_t* optlen)
626 if(!_init_check)
return -1;
632 pkt->optname = optname;
636 SignalSema(lock_sema);
640 if(res_pkt->optlen < *optlen) *optlen = res_pkt->optlen;
641 memcpy((
void*)optval, res_pkt->buffer, *optlen);
643 result = res_pkt->result;
645 SignalSema(lock_sema);
650int ps2ipc_setsockopt(
int s,
int level,
int optname,
const void *optval, socklen_t optlen)
655 if(!_init_check)
return -1;
661 pkt->optname = optname;
662 pkt->optlen = optlen;
664 memcpy(pkt->buffer, optval, optlen);
666 if (SifCallRpc(&_ps2ip, PS2IPS_ID_SETSOCKOPT, 0, (
void*)pkt,
sizeof(
setsockopt_pkt), (
void*)&_rpc_buffer.result,
sizeof(s32), NULL, NULL) < 0)
668 SignalSema(lock_sema);
672 result = _rpc_buffer.result;
674 SignalSema(lock_sema);
680struct hostent *ps2ipc_gethostbyname(
const char *name)
682 gethostbyname_res_pkt *res_pkt = &_rpc_buffer.gethostbyname_res_pkt;
688 if(!_init_check)
return NULL;
693 strncpy(_rpc_buffer.hostname, name,
sizeof(_rpc_buffer.hostname));
694 _rpc_buffer.hostname[
sizeof(_rpc_buffer.hostname) - 1] =
'\0';
695 if(SifCallRpc(&_ps2ip, PS2IPS_ID_GETHOSTBYNAME, 0, (
void*)_rpc_buffer.hostname,
sizeof(_rpc_buffer.hostname), (
void*)res_pkt,
sizeof(gethostbyname_res_pkt), NULL, NULL) >=0)
697 if(res_pkt->result == 0)
699 hostent.h_addrtype = res_pkt->hostent.h_addrtype;
700 hostent.h_length = res_pkt->hostent.h_length;
703 memcpy(&addr, &res_pkt->hostent.h_addr,
sizeof(addr));
704 addr_list[0] = &addr;
706 hostent.h_addr_list = (
char**)&addr_list;
711 SignalSema(lock_sema);
716void ps2ipc_dns_setserver(u8 numdns,
const ip_addr_t *dnsserver)
718 dns_setserver_pkt *pkt = &_rpc_buffer.dns_setserver_pkt;
720 if(!_init_check)
return;
724 pkt->numdns = numdns;
725 pkt->dnsserver = (dnsserver != NULL) ? (*dnsserver) : *
IP4_ADDR_ANY;
727 SifCallRpc(&_ps2ip, PS2IPS_ID_DNS_SETSERVER, 0, (
void*)pkt,
sizeof(dns_setserver_pkt), NULL, 0, NULL, NULL);
729 if (numdns < DNS_MAX_SERVERS)
730 dns_servers[numdns] = (dnsserver != NULL) ? (*dnsserver) : *
IP4_ADDR_ANY;
732 SignalSema(lock_sema);
735const ip_addr_t *ps2ipc_dns_getserver(u8 numdns)
737 dns_getserver_res_pkt *res_pkt = &_rpc_buffer.dns_getserver_res_pkt;
740 if ((!_init_check) || (numdns >= DNS_MAX_SERVERS))
745 _rpc_buffer.numdns = numdns;
746 dns = &dns_servers[numdns];
749 if(SifCallRpc(&_ps2ip, PS2IPS_ID_DNS_GETSERVER, 0, (
void*)&_rpc_buffer.numdns,
sizeof(u8), (
void*)res_pkt,
sizeof(dns_getserver_res_pkt), NULL, NULL) >=0)
750 ip_addr_copy(*dns, res_pkt->dnsserver);
752 SignalSema(lock_sema);