From 5af3dd73de61a2ce0c62869257d29d2b332bc410 Mon Sep 17 00:00:00 2001 From: Michael Theall Date: Thu, 21 Jan 2016 19:21:43 -0600 Subject: [PATCH] Add SOCU_GetIPInfo --- libctru/include/3ds/services/soc.h | 7 +++ libctru/source/services/soc/soc_getipinfo.c | 61 +++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 libctru/source/services/soc/soc_getipinfo.c diff --git a/libctru/include/3ds/services/soc.h b/libctru/include/3ds/services/soc.h index bec38e7..dfb39d6 100644 --- a/libctru/include/3ds/services/soc.h +++ b/libctru/include/3ds/services/soc.h @@ -5,6 +5,7 @@ * After initializing this service you will be able to use system calls from netdb.h, sys/socket.h etc. */ #pragma once +#include /** * @brief Initializes the SOC service. @@ -33,3 +34,9 @@ int gethostname(char *name, size_t namelen); int SOCU_ShutdownSockets(); int SOCU_CloseSockets(); + +/** + * @brief Gets the system's IP address, netmask, and subnet broadcast + * @return error + */ +int SOCU_GetIPInfo(struct in_addr *ip, struct in_addr *netmask, struct in_addr *broadcast); diff --git a/libctru/source/services/soc/soc_getipinfo.c b/libctru/source/services/soc/soc_getipinfo.c new file mode 100644 index 0000000..d425503 --- /dev/null +++ b/libctru/source/services/soc/soc_getipinfo.c @@ -0,0 +1,61 @@ +#include "soc_common.h" +#include <3ds/ipc.h> +#include <3ds/result.h> + +typedef struct +{ + struct in_addr ip; + struct in_addr netmask; + struct in_addr broadcast; +} SOCU_IPInfo_t; + +int SOCU_GetIPInfo(struct in_addr *ip, struct in_addr *netmask, struct in_addr *broadcast) +{ + int i, ret; + u32 *cmdbuf = getThreadCommandBuffer(); + u32 *staticbufs = getThreadStaticBuffers(); + u32 saved_threadstorage[2]; + SOCU_IPInfo_t info; + + cmdbuf[0] = IPC_MakeHeader(0x1A,3,0); //0x1A00C0 + cmdbuf[1] = 0xFFFE; + cmdbuf[2] = 0x4003; + cmdbuf[3] = sizeof(info); + + // Save the thread storage values + for(i = 0 ; i < 2 ; ++i) + saved_threadstorage[i] = staticbufs[i]; + + staticbufs[0] = IPC_Desc_StaticBuffer(sizeof(info), 0); + staticbufs[1] = (u32)&info; + + ret = svcSendSyncRequest(SOCU_handle); + + // Restore the thread storage values + for(i = 0 ; i < 2 ; ++i) + staticbufs[i] = saved_threadstorage[i]; + + if(R_FAILED(ret)) { + errno = SYNC_ERROR; + return ret; + } + + ret = cmdbuf[1]; + if(R_FAILED(ret)) { + errno = SYNC_ERROR; + return ret; + } + if(cmdbuf[2] != 0) + { + return cmdbuf[2]; + } + + if(ip != NULL) + *ip = info.ip; + if(netmask != NULL) + *netmask = info.netmask; + if(broadcast != NULL) + *broadcast = info.broadcast; + + return 0; +}