int sockatmark(int sockfd) { int ret = 0; u32 *cmdbuf = getThreadCommandBuffer(); sockfd = soc_get_fd(sockfd); if(sockfd < 0) { errno = -sockfd; return -1; } cmdbuf[0] = IPC_MakeHeader(0x15,1,2); // 0x150042 cmdbuf[1] = (u32)sockfd; cmdbuf[2] = IPC_Desc_CurProcessHandle(); ret = svcSendSyncRequest(SOCU_handle); if(ret != 0) { errno = SYNC_ERROR; return -1; } ret = (int)cmdbuf[1]; if(ret == 0) ret = _net_convert_error(cmdbuf[2]); if(ret < 0) { errno = -ret; return -1; } return ret; }
int socSetsockopt(int sockfd, int level, int optname, const void *optval, socklen_t optlen) { int ret = 0; u32 *cmdbuf = getThreadCommandBuffer(); cmdbuf[0] = IPC_MakeHeader(0x12,4,4); // 0x120104 cmdbuf[1] = (u32)sockfd; cmdbuf[2] = (u32)level; cmdbuf[3] = (u32)optname; cmdbuf[4] = (u32)optlen; cmdbuf[5] = IPC_Desc_CurProcessHandle(); cmdbuf[7] = IPC_Desc_StaticBuffer(optlen,9); cmdbuf[8] = (u32)optval; ret = svcSendSyncRequest(SOCU_handle); if(ret != 0) { return ret; } ret = (int)cmdbuf[1]; if(ret == 0) ret = _net_convert_error(cmdbuf[2]); if(ret < 0) { //errno = -ret; return -1; } return ret; }
int socListen(int sockfd, int max_connections) { Result ret = 0; u32 *cmdbuf = getThreadCommandBuffer(); cmdbuf[0] = IPC_MakeHeader(0x3,2,2); // 0x30082 cmdbuf[1] = (u32)sockfd; cmdbuf[2] = (u32)max_connections; cmdbuf[3] = IPC_Desc_CurProcessHandle(); ret = svcSendSyncRequest(SOCU_handle); if(ret != 0) { //errno = SYNC_ERROR; return ret; } ret = (int)cmdbuf[1]; if(ret == 0) ret = _net_convert_error(cmdbuf[2]); if(ret < 0) { //errno = -ret; return -1; } return 0; }
static int soc_close(struct _reent *r, int fd) { Handle sockfd = *(Handle*)fd; int ret = 0; u32 *cmdbuf = getThreadCommandBuffer(); cmdbuf[0] = IPC_MakeHeader(0xB,1,2); // 0xB0042 cmdbuf[1] = (u32)sockfd; cmdbuf[2] = IPC_Desc_CurProcessHandle(); ret = svcSendSyncRequest(SOCU_handle); if(ret != 0) { errno = SYNC_ERROR; return ret; } ret = (int)cmdbuf[1]; if(ret == 0) ret =_net_convert_error(cmdbuf[2]); if(ret < 0) { errno = -ret; return -1; } return 0; }
static Result sslcipc_Initialize(void) { u32* cmdbuf=getThreadCommandBuffer(); cmdbuf[0]=IPC_MakeHeader(0x1,0,2); // 0x10002 cmdbuf[1]=IPC_Desc_CurProcessHandle(); Result ret=0; if(R_FAILED(ret=svcSendSyncRequest(__sslc_servhandle)))return ret; return cmdbuf[1]; }
Result srvRegisterClient(void) { Result rc = 0; u32* cmdbuf = getThreadCommandBuffer(); cmdbuf[0] = IPC_MakeHeader(0x1,0,2); // 0x10002 cmdbuf[1] = IPC_Desc_CurProcessHandle(); if(R_FAILED(rc = svcSendSyncRequest(srvHandle)))return rc; return cmdbuf[1]; }
int getsockname(int sockfd, struct sockaddr *addr, socklen_t *addrlen) { int ret = 0; u32 *cmdbuf = getThreadCommandBuffer(); u32 saved_threadstorage[2]; u8 tmpaddr[0x1c]; sockfd = soc_get_fd(sockfd); if(sockfd < 0) { errno = -sockfd; return -1; } cmdbuf[0] = IPC_MakeHeader(0x17,2,2); // 0x170082 cmdbuf[1] = (u32)sockfd; cmdbuf[2] = 0x1c; cmdbuf[3] = IPC_Desc_CurProcessHandle(); u32 * staticbufs = getThreadStaticBuffers(); saved_threadstorage[0] = staticbufs[0]; saved_threadstorage[1] = staticbufs[1]; staticbufs[0] = IPC_Desc_StaticBuffer(0x1c,0); staticbufs[1] = (u32)tmpaddr; ret = svcSendSyncRequest(SOCU_handle); staticbufs[0] = saved_threadstorage[0]; staticbufs[1] = saved_threadstorage[1]; if(ret != 0) { errno = SYNC_ERROR; return ret; } ret = (int)cmdbuf[1]; if(ret == 0) ret = _net_convert_error(cmdbuf[2]); if(ret < 0) { errno = -ret; return -1; } if(*addrlen > tmpaddr[0]) *addrlen = tmpaddr[0]; memset(addr, 0, sizeof(struct sockaddr)); addr->sa_family = tmpaddr[1]; memcpy(addr->sa_data, &tmpaddr[2], *addrlen - 2); return ret; }
static Result sslcipc_InitializeConnectionSession(sslcContext *context) { u32* cmdbuf=getThreadCommandBuffer(); cmdbuf[0]=IPC_MakeHeader(0x12,1,2); // 0x120042 cmdbuf[1]=context->sslchandle; cmdbuf[2]=IPC_Desc_CurProcessHandle(); Result ret=0; if(R_FAILED(ret=svcSendSyncRequest(context->servhandle)))return ret; return cmdbuf[1]; }
int socAccept(int sockfd, struct sockaddr *addr, socklen_t *addrlen) { Result ret = 0; int tmp_addrlen = 0x1c; u32 *cmdbuf = getThreadCommandBuffer(); u8 tmpaddr[0x1c]; u32 saved_threadstorage[2]; memset(tmpaddr, 0, 0x1c); cmdbuf[0] = IPC_MakeHeader(0x4,2,2); // 0x40082 cmdbuf[1] = (u32)sockfd; cmdbuf[2] = (u32)tmp_addrlen; cmdbuf[3] = IPC_Desc_CurProcessHandle(); u32 *staticbufs = getThreadStaticBuffers(); saved_threadstorage[0] = staticbufs[0]; saved_threadstorage[1] = staticbufs[1]; staticbufs[0] = IPC_Desc_StaticBuffer(tmp_addrlen,0); staticbufs[1] = (u32)tmpaddr; ret = svcSendSyncRequest(SOCU_handle); staticbufs[0] = saved_threadstorage[0]; staticbufs[1] = saved_threadstorage[1]; if(ret != 0) return ret; ret = (int)cmdbuf[1]; if(ret == 0) ret = _net_convert_error(cmdbuf[2]); if(ret < 0) //errno = -ret; if(ret >= 0 && addr != NULL) { addr->sa_family = tmpaddr[1]; if(*addrlen > tmpaddr[0]) *addrlen = tmpaddr[0]; memcpy(addr->sa_data, &tmpaddr[2], *addrlen - 2); } if(ret < 0) return -1; return ret; }
int socPoll(struct pollfd *fds, nfds_t nfds, int timeout) { int ret = 0; nfds_t i; u32 size = sizeof(struct pollfd)*nfds; u32 *cmdbuf = getThreadCommandBuffer(); u32 saved_threadstorage[2]; if(nfds == 0) { return -1; } for(i = 0; i < nfds; ++i) { fds[i].revents = 0; } cmdbuf[0] = IPC_MakeHeader(0x14,2,4); // 0x140084 cmdbuf[1] = (u32)nfds; cmdbuf[2] = (u32)timeout; cmdbuf[3] = IPC_Desc_CurProcessHandle(); cmdbuf[5] = IPC_Desc_StaticBuffer(size,10); cmdbuf[6] = (u32)fds; u32 * staticbufs = getThreadStaticBuffers(); saved_threadstorage[0] = staticbufs[0]; saved_threadstorage[1] = staticbufs[1]; staticbufs[0] = IPC_Desc_StaticBuffer(size,0); staticbufs[1] = (u32)fds; ret = svcSendSyncRequest(SOCU_handle); staticbufs[0] = saved_threadstorage[0]; staticbufs[1] = saved_threadstorage[1]; if(ret != 0) { return ret; } ret = (int)cmdbuf[1]; if(ret == 0) ret = _net_convert_error(cmdbuf[2]); if(ret < 0) { //errno = -ret; return -1; } return ret; }
int getsockopt(int sockfd, int level, int optname, void *optval, socklen_t *optlen) { int ret = 0; u32 *cmdbuf = getThreadCommandBuffer(); u32 saved_threadstorage[2]; sockfd = soc_get_fd(sockfd); if(sockfd < 0) { errno = -sockfd; return -1; } cmdbuf[0] = IPC_MakeHeader(0x11,4,2); // 0x110102 cmdbuf[1] = (u32)sockfd; cmdbuf[2] = (u32)level; cmdbuf[3] = (u32)optname; cmdbuf[4] = (u32)*optlen; cmdbuf[5] = IPC_Desc_CurProcessHandle(); u32 * staticbufs = getThreadStaticBuffers(); saved_threadstorage[0] = staticbufs[0]; saved_threadstorage[1] = staticbufs[1]; staticbufs[0] = IPC_Desc_StaticBuffer(*optlen,0); staticbufs[1] = (u32)optval; ret = svcSendSyncRequest(SOCU_handle); staticbufs[0] = saved_threadstorage[0]; staticbufs[1] = saved_threadstorage[1]; if(ret != 0) { errno = SYNC_ERROR; return ret; } ret = (int)cmdbuf[1]; if(ret == 0) ret = _net_convert_error(cmdbuf[2]); if(ret < 0) { errno = -ret; return -1; } *optlen = cmdbuf[3]; return ret; }
int socBind(int sockfd, const struct sockaddr *addr, socklen_t addrlen) { Result ret = 0; socklen_t tmp_addrlen = 0; u32 *cmdbuf = getThreadCommandBuffer(); u8 tmpaddr[0x1c]; memset(tmpaddr, 0, 0x1c); if(addr->sa_family == AF_INET) tmp_addrlen = 8; else tmp_addrlen = 0x1c; if(addrlen < tmp_addrlen) { //errno = EINVAL; return -1; } tmpaddr[0] = tmp_addrlen; tmpaddr[1] = addr->sa_family; memcpy(&tmpaddr[2], &addr->sa_data, tmp_addrlen-2); cmdbuf[0] = IPC_MakeHeader(0x5,2,4); // 0x50084 cmdbuf[1] = (u32)sockfd; cmdbuf[2] = (u32)tmp_addrlen; cmdbuf[3] = IPC_Desc_CurProcessHandle(); cmdbuf[5] = IPC_Desc_StaticBuffer(tmp_addrlen,0); cmdbuf[6] = (u32)tmpaddr; ret = svcSendSyncRequest(SOCU_handle); if(ret != 0) { //errno = SYNC_ERROR; return ret; } ret = (int)cmdbuf[1]; if(ret == 0) ret = _net_convert_error(cmdbuf[2]); if(ret < 0) { //errno = -ret; return -1; } return ret; }
static Result SOCU_Initialize(Handle memhandle, u32 memsize) { Result ret = 0; u32 *cmdbuf = getThreadCommandBuffer(); cmdbuf[0] = IPC_MakeHeader(0x1,1,4); // 0x10044 cmdbuf[1] = memsize; cmdbuf[2] = IPC_Desc_CurProcessHandle(); cmdbuf[4] = IPC_Desc_SharedHandles(1); cmdbuf[5] = memhandle; ret = svcSendSyncRequest(SOCU_handle); if(ret != 0) return ret; return cmdbuf[1]; }
Result APT_GetProgramID(u64* pProgramID) { u32* cmdbuf=getThreadCommandBuffer(); cmdbuf[0] = IPC_MakeHeader(0x58,0,2); // 0x580002 cmdbuf[1] = IPC_Desc_CurProcessHandle(); Result ret=0; if(R_FAILED(ret=svcSendSyncRequest(aptuHandle)))return ret; if(ret==0)ret = cmdbuf[1]; if(pProgramID) { if(ret==0) *pProgramID=((u64)cmdbuf[3]<<32)|cmdbuf[2]; } return ret; }
int socSocket(int domain, int type, int protocol) { int ret = 0; u32 *cmdbuf = getThreadCommandBuffer(); // The protocol on the 3DS *must* be 0 to work // To that end, when appropriate, we will make the change for the user if (domain == AF_INET && type == SOCK_STREAM && protocol == IPPROTO_TCP) { protocol = 0; // TCP is the only option, so 0 will work as expected } if (domain == AF_INET && type == SOCK_DGRAM && protocol == IPPROTO_UDP) { protocol = 0; // UDP is the only option, so 0 will work as expected } cmdbuf[0] = IPC_MakeHeader(0x2,3,2); // 0x200C2 cmdbuf[1] = domain; cmdbuf[2] = type; cmdbuf[3] = protocol; cmdbuf[4] = IPC_Desc_CurProcessHandle(); ret = svcSendSyncRequest(SOCU_handle); if(ret != 0) { //errno = SYNC_ERROR; return ret; } ret = (int)cmdbuf[1]; if(ret == 0) ret = cmdbuf[2]; if(ret < 0) { //if(cmdbuf[1] == 0)errno = _net_convert_error(ret); //if(cmdbuf[1] != 0)errno = SYNC_ERROR; return -1; } else return cmdbuf[2]; }
int poll(struct pollfd *fds, nfds_t nfds, int timeout) { int ret = 0; nfds_t i; u32 size = sizeof(struct pollfd)*nfds; u32 *cmdbuf = getThreadCommandBuffer(); u32 saved_threadstorage[2]; if(nfds == 0) { errno = EINVAL; return -1; } struct pollfd *tmp_fds = (struct pollfd*)malloc(sizeof(struct pollfd) * nfds); if(tmp_fds == NULL) { errno = ENOMEM; return -1; } memcpy(tmp_fds, fds, sizeof(struct pollfd) * nfds); for(i = 0; i < nfds; ++i) { tmp_fds[i].fd = soc_get_fd(fds[i].fd); if(tmp_fds[i].fd < 0) { errno = -tmp_fds[i].fd; free(tmp_fds); return -1; } tmp_fds[i].revents = 0; } cmdbuf[0] = IPC_MakeHeader(0x14,2,4); // 0x140084 cmdbuf[1] = (u32)nfds; cmdbuf[2] = (u32)timeout; cmdbuf[3] = IPC_Desc_CurProcessHandle(); cmdbuf[5] = IPC_Desc_StaticBuffer(size,10); cmdbuf[6] = (u32)tmp_fds; u32 * staticbufs = getThreadStaticBuffers(); saved_threadstorage[0] = staticbufs[0]; saved_threadstorage[1] = staticbufs[1]; staticbufs[0] = IPC_Desc_StaticBuffer(size,0); staticbufs[1] = (u32)tmp_fds; ret = svcSendSyncRequest(SOCU_handle); if(ret != 0) { free(tmp_fds); errno = SYNC_ERROR; return ret; } staticbufs[0] = saved_threadstorage[0]; staticbufs[1] = saved_threadstorage[1]; ret = (int)cmdbuf[1]; if(ret == 0) ret = _net_convert_error(cmdbuf[2]); if(ret < 0) { free(tmp_fds); errno = -ret; return -1; } for(i = 0; i < nfds; ++i) fds[i].revents = tmp_fds[i].revents; free(tmp_fds); return ret; }
int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen) { int ret = 0; int tmp_addrlen = 0x1c; int fd, dev; __handle *handle; u32 *cmdbuf = getThreadCommandBuffer(); u8 tmpaddr[0x1c]; u32 saved_threadstorage[2]; sockfd = soc_get_fd(sockfd); if(sockfd < 0) { errno = -sockfd; return -1; } dev = FindDevice("soc:"); if(dev < 0) { errno = ENODEV; return -1; } fd = __alloc_handle(sizeof(__handle) + sizeof(Handle)); if(fd < 0) { errno = ENOMEM; return -1; } handle = __get_handle(fd); handle->device = dev; handle->fileStruct = ((void *)handle) + sizeof(__handle); memset(tmpaddr, 0, 0x1c); cmdbuf[0] = IPC_MakeHeader(0x4,2,2); // 0x40082 cmdbuf[1] = (u32)sockfd; cmdbuf[2] = (u32)tmp_addrlen; cmdbuf[3] = IPC_Desc_CurProcessHandle(); u32 * staticbufs = getThreadStaticBuffers(); saved_threadstorage[0] = staticbufs[0]; saved_threadstorage[1] = staticbufs[1]; staticbufs[0] = IPC_Desc_StaticBuffer(tmp_addrlen,0); staticbufs[1] = (u32)tmpaddr; ret = svcSendSyncRequest(SOCU_handle); staticbufs[0] = saved_threadstorage[0]; staticbufs[1] = saved_threadstorage[1]; if(ret != 0) { __release_handle(fd); errno = SYNC_ERROR; return ret; } ret = (int)cmdbuf[1]; if(ret == 0) ret = _net_convert_error(cmdbuf[2]); if(ret < 0) errno = -ret; if(ret >= 0 && addr != NULL) { addr->sa_family = tmpaddr[1]; if(*addrlen > tmpaddr[0]) *addrlen = tmpaddr[0]; memcpy(addr->sa_data, &tmpaddr[2], *addrlen - 2); } if(ret < 0) { __release_handle(fd); return -1; } else *(Handle*)handle->fileStruct = ret; return fd; }
int socket(int domain, int type, int protocol) { int ret = 0; int fd, dev; __handle *handle; u32 *cmdbuf = getThreadCommandBuffer(); // The protocol on the 3DS *must* be 0 to work // To that end, when appropriate, we will make the change for the user if (domain == AF_INET && type == SOCK_STREAM && protocol == IPPROTO_TCP) { protocol = 0; // TCP is the only option, so 0 will work as expected } if (domain == AF_INET && type == SOCK_DGRAM && protocol == IPPROTO_UDP) { protocol = 0; // UDP is the only option, so 0 will work as expected } cmdbuf[0] = IPC_MakeHeader(0x2,3,2); // 0x200C2 cmdbuf[1] = domain; cmdbuf[2] = type; cmdbuf[3] = protocol; cmdbuf[4] = IPC_Desc_CurProcessHandle(); dev = FindDevice("soc:"); if(dev < 0) { errno = ENODEV; return -1; } fd = __alloc_handle(sizeof(__handle) + sizeof(Handle)); if(fd < 0) { errno = ENOMEM; return -1; } handle = __get_handle(fd); handle->device = dev; handle->fileStruct = ((void *)handle) + sizeof(__handle); ret = svcSendSyncRequest(SOCU_handle); if(ret != 0) { __release_handle(fd); errno = SYNC_ERROR; return ret; } ret = (int)cmdbuf[1]; if(ret == 0)ret = cmdbuf[2]; if(ret < 0) { __release_handle(fd); if(cmdbuf[1] == 0)errno = _net_convert_error(ret); if(cmdbuf[1] != 0)errno = SYNC_ERROR; return -1; } *(Handle*)handle->fileStruct = cmdbuf[2]; return fd; }