From 5ce470dcfb1148d0ed36acfece3c63a4f0dd5ddc Mon Sep 17 00:00:00 2001
From: Joel Cunningham <joel.cunningham@me.com>
Date: Thu, 19 Jan 2017 22:13:30 -0600
Subject: [PATCH] Add interface support to shell and VS project file

This commit adds interface API support to shell and adds the
interface files to th Visual Studio project
---
 apps/shell/shell.c                    | 45 +++++++++++++++++++++++++++++++++++
 ports/win32/msvc/lwIP.vcxproj         |  2 ++
 ports/win32/msvc/lwIP.vcxproj.filters |  6 +++++
 3 files changed, 53 insertions(+)

diff --git a/apps/shell/shell.c b/apps/shell/shell.c
index 64b1cb1..af2c1ed 100644
--- a/apps/shell/shell.c
+++ b/apps/shell/shell.c
@@ -45,6 +45,10 @@
 #include "lwip/api.h"
 #include "lwip/stats.h"
 
+#if LWIP_SOCKET
+#include "lwip/if.h"
+#endif
+
 #ifdef WIN32
 #define NEWLINE "\r\n"
 #else /* WIN32 */
@@ -95,6 +99,8 @@ usnd [connection #] [message]: sends a message on a UDP connection."NEWLINE"\
 recv [connection #]: recieves data on a TCP or UDP connection."NEWLINE"\
 clos [connection #]: closes a TCP or UDP connection."NEWLINE"\
 stat: prints out lwIP statistics."NEWLINE"\
+idxtoname [index]: outputs interface name from index."NEWLINE"\
+nametoidx [name]: outputs interface index from name."NEWLINE"\
 quit: quits."NEWLINE"";
 
 #if LWIP_STATS
@@ -955,6 +961,37 @@ com_usnd(struct command *com)
   return ESUCCESS;
 }
 /*-----------------------------------------------------------------------------------*/
+#if LWIP_SOCKET
+/*-----------------------------------------------------------------------------------*/
+static s8_t
+com_idxtoname(struct command *com)
+{
+  long i = strtol(com->args[0], NULL, 10);
+
+  if (if_indextoname(i, (char *)buffer)) {
+    netconn_write(com->conn, buffer, strlen((const char *)buffer), NETCONN_COPY);
+    sendstr(NEWLINE, com->conn);
+  } else {
+    sendstr("No interface found"NEWLINE, com->conn);
+  }
+  return ESUCCESS;
+}
+/*-----------------------------------------------------------------------------------*/
+static s8_t
+com_nametoidx(struct command *com)
+{
+  unsigned int index = if_nametoindex(com->args[0]);
+
+  if (index) {
+    snprintf((char *)buffer, sizeof(buffer), "%u"NEWLINE, index);
+    netconn_write(com->conn, buffer, strlen((const char *)buffer), NETCONN_COPY);
+  } else {
+    sendstr("No interface found"NEWLINE, com->conn);
+  }
+  return ESUCCESS;
+}
+#endif /* LWIP_SOCKET */
+/*-----------------------------------------------------------------------------------*/
 static s8_t
 com_help(struct command *com)
 {
@@ -1007,6 +1044,14 @@ parse_command(struct command *com, u32_t len)
   } else if (strncmp((const char *)buffer, "usnd", 4) == 0) {
     com->exec = com_usnd;
     com->nargs = 2;
+#if LWIP_SOCKET
+  } else if (strncmp((const char *)buffer, "idxtoname", 9) == 0) {
+    com->exec = com_idxtoname;
+    com->nargs = 1;
+  } else if (strncmp((const char *)buffer, "nametoidx", 9) == 0) {
+    com->exec = com_nametoidx;
+    com->nargs = 1;
+#endif /* LWIP_SOCKET */
   } else if (strncmp((const char *)buffer, "help", 4) == 0) {
     com->exec = com_help;
     com->nargs = 0;
diff --git a/ports/win32/msvc/lwIP.vcxproj b/ports/win32/msvc/lwIP.vcxproj
index d900766..b15dd23 100644
--- a/ports/win32/msvc/lwIP.vcxproj
+++ b/ports/win32/msvc/lwIP.vcxproj
@@ -248,6 +248,7 @@
     <ClCompile Include="..\..\..\..\lwip\src\api\api_lib.c" />
     <ClCompile Include="..\..\..\..\lwip\src\api\api_msg.c" />
     <ClCompile Include="..\..\..\..\lwip\src\api\err.c" />
+    <ClCompile Include="..\..\..\..\lwip\src\api\if.c" />
     <ClCompile Include="..\..\..\..\lwip\src\api\netbuf.c" />
     <ClCompile Include="..\..\..\..\lwip\src\api\netdb.c" />
     <ClCompile Include="..\..\..\..\lwip\src\api\netifapi.c" />
@@ -374,6 +375,7 @@
     <ClInclude Include="..\..\..\..\lwip\src\include\lwip\apps\tftp_opts.h" />
     <ClInclude Include="..\..\..\..\lwip\src\include\lwip\apps\tftp_server.h" />
     <ClInclude Include="..\..\..\..\lwip\src\include\lwip\errno.h" />
+    <ClInclude Include="..\..\..\..\lwip\src\include\lwip\if.h" />
     <ClInclude Include="..\..\..\..\lwip\src\include\lwip\priv\api_msg.h" />
     <ClInclude Include="..\..\..\..\lwip\src\include\lwip\priv\memp_priv.h" />
     <ClInclude Include="..\..\..\..\lwip\src\include\lwip\priv\memp_std.h" />
diff --git a/ports/win32/msvc/lwIP.vcxproj.filters b/ports/win32/msvc/lwIP.vcxproj.filters
index d74a4a2..9228c8e 100644
--- a/ports/win32/msvc/lwIP.vcxproj.filters
+++ b/ports/win32/msvc/lwIP.vcxproj.filters
@@ -457,6 +457,9 @@
     <ClCompile Include="..\..\..\..\lwip\src\apps\mqtt\mqtt.c">
       <Filter>src\apps\mqtt</Filter>
     </ClCompile>
+    <ClCompile Include="..\..\..\..\lwip\src\api\if.c">
+      <Filter>src\api</Filter>
+    </ClCompile>
   </ItemGroup>
   <ItemGroup>
     <ClInclude Include="..\..\..\..\lwip\src\include\lwip\apps\lwiperf.h">
@@ -885,6 +888,9 @@
     <ClInclude Include="..\..\..\..\lwip\src\include\lwip\apps\mqtt.h">
       <Filter>src\include\lwip\apps</Filter>
     </ClInclude>
+    <ClInclude Include="..\..\..\..\lwip\src\include\lwip\if.h">
+      <Filter>src\include\lwip</Filter>
+    </ClInclude>
   </ItemGroup>
   <ItemGroup>
     <CustomBuildStep Include="..\lwipcfg_msvc.h.example">
-- 
1.9.5.msysgit.0

