]> Chaos Git - corbenik/ctrulib.git/commitdiff
Add SOCU_GetIPInfo
authorMichael Theall <pigman46@gmail.com>
Fri, 22 Jan 2016 01:21:43 +0000 (19:21 -0600)
committerMichael Theall <pigman46@gmail.com>
Fri, 22 Jan 2016 01:21:43 +0000 (19:21 -0600)
libctru/include/3ds/services/soc.h
libctru/source/services/soc/soc_getipinfo.c [new file with mode: 0644]

index bec38e7f73148dfa64e6087d4bfd69e5e3b0f3d0..dfb39d6d9151aaf3a725f1a428c2e41df935d929 100644 (file)
@@ -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 <netinet/in.h>
 
 /**
  * @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 (file)
index 0000000..d425503
--- /dev/null
@@ -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;
+}