[CMAKE]
[reactos.git] / base / applications / network / ipconfig / ipconfig.c
index 4d496d5..2657614 100644 (file)
 HINSTANCE hInstance;
 HANDLE ProcessHeap;
 
+int LoadStringAndOem(HINSTANCE hInst,
+               UINT uID,
+               LPTSTR szNode,
+               int Siz
+)      
+{
+  TCHAR szTmp[25];
+  int res = LoadString(hInst, uID, szTmp, sizeof(szTmp)); 
+  CharToOem(szTmp, szNode);
+  return(res);
+}
 
 LPTSTR GetNodeTypeName(UINT NodeType)
 {
@@ -33,27 +44,27 @@ LPTSTR GetNodeTypeName(UINT NodeType)
     switch (NodeType)
     {
         case 1:
-            if (!LoadString(hInstance, IDS_BCAST, szNode, sizeof(szNode)))
+            if (!LoadStringAndOem(hInstance, IDS_BCAST, szNode,  sizeof(szNode)))
                 return NULL;
             break;
 
         case 2:
-            if (!LoadString(hInstance, IDS_P2P, szNode, sizeof(szNode)))
+            if (!LoadStringAndOem(hInstance, IDS_P2P, szNode,  sizeof(szNode)))
                 return NULL;
             break;
 
         case 4:
-            if (!LoadString(hInstance, IDS_MIXED, szNode,  sizeof(szNode)))
+            if (!LoadStringAndOem(hInstance, IDS_MIXED, szNode,  sizeof(szNode)))
                 return NULL;
             break;
 
         case 8:
-            if (!LoadString(hInstance, IDS_HYBRID, szNode,  sizeof(szNode)))
+            if (!LoadStringAndOem(hInstance, IDS_HYBRID, szNode,  sizeof(szNode)))
                 return NULL;
             break;
 
         default :
-            if (!LoadString(hInstance, IDS_UNKNOWN, szNode,  sizeof(szNode)))
+            if (!LoadStringAndOem(hInstance, IDS_UNKNOWN, szNode,  sizeof(szNode)))
                 return NULL;
             break;
     }
@@ -69,42 +80,42 @@ LPTSTR GetInterfaceTypeName(UINT InterfaceType)
     switch (InterfaceType)
     {
         case MIB_IF_TYPE_OTHER:
-            if (!LoadString(hInstance, IDS_OTHER, szIntType, sizeof(szIntType)))
+            if (!LoadStringAndOem(hInstance, IDS_OTHER, szIntType, sizeof(szIntType)))
                 return NULL;
             break;
 
         case MIB_IF_TYPE_ETHERNET:
-            if (!LoadString(hInstance, IDS_ETH, szIntType, sizeof(szIntType)))
+            if (!LoadStringAndOem(hInstance, IDS_ETH, szIntType, sizeof(szIntType)))
                 return NULL;
             break;
 
         case MIB_IF_TYPE_TOKENRING:
-            if (!LoadString(hInstance, IDS_TOKEN, szIntType, sizeof(szIntType)))
+            if (!LoadStringAndOem(hInstance, IDS_TOKEN, szIntType, sizeof(szIntType)))
                 return NULL;
             break;
 
         case MIB_IF_TYPE_FDDI:
-            if (!LoadString(hInstance, IDS_FDDI, szIntType, sizeof(szIntType)))
+            if (!LoadStringAndOem(hInstance, IDS_FDDI, szIntType, sizeof(szIntType)))
                 return NULL;
             break;
 
         case MIB_IF_TYPE_PPP:
-            if (!LoadString(hInstance, IDS_PPP, szIntType, sizeof(szIntType)))
+            if (!LoadStringAndOem(hInstance, IDS_PPP, szIntType, sizeof(szIntType)))
                 return NULL;
             break;
 
         case MIB_IF_TYPE_LOOPBACK:
-            if (!LoadString(hInstance, IDS_LOOP, szIntType, sizeof(szIntType)))
+            if (!LoadStringAndOem(hInstance, IDS_LOOP, szIntType, sizeof(szIntType)))
                 return NULL;
             break;
 
         case MIB_IF_TYPE_SLIP:
-            if (!LoadString(hInstance, IDS_SLIP, szIntType, sizeof(szIntType)))
+            if (!LoadStringAndOem(hInstance, IDS_SLIP, szIntType, sizeof(szIntType)))
                 return NULL;
             break;
 
         default:
-            if (!LoadString(hInstance, IDS_UNKNOWN, szIntType, sizeof(szIntType)))
+            if (!LoadStringAndOem(hInstance, IDS_UNKNOWN, szIntType, sizeof(szIntType)))
                 return NULL;
             break;
     }
@@ -153,6 +164,7 @@ LPTSTR GetConnectionType(LPTSTR lpClass)
 {
     HKEY hKey = NULL;
     LPTSTR ConType = NULL;
+    LPTSTR ConTypeTmp = NULL;
     TCHAR Path[256];
     LPTSTR PrePath  = _T("SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}\\");
     LPTSTR PostPath = _T("\\Connection");
@@ -180,20 +192,34 @@ LPTSTR GetConnectionType(LPTSTR lpClass)
                            NULL,
                            &dwDataSize) == ERROR_SUCCESS)
         {
-            ConType = (LPTSTR)HeapAlloc(ProcessHeap,
+            ConTypeTmp = (LPTSTR)HeapAlloc(ProcessHeap,
                                         0,
                                         dwDataSize);
-            if (ConType == NULL)
-                return NULL;
 
-            if(RegQueryValueEx(hKey,
-                               _T("Name"),
-                               NULL,
-                               &dwType,
-                               (PBYTE)ConType,
-                               &dwDataSize) != ERROR_SUCCESS)
+            ConType = (LPTSTR)HeapAlloc(ProcessHeap,
+                                        0,
+                                        dwDataSize);
+            if (ConType && ConTypeTmp)
             {
-                ConType = NULL;
+                if(RegQueryValueEx(hKey,
+                                   _T("Name"),
+                                   NULL,
+                                   &dwType,
+                                   (PBYTE)ConTypeTmp,
+                                   &dwDataSize) != ERROR_SUCCESS)
+                {
+                    HeapFree(ProcessHeap,
+                             0,
+                             ConType);
+
+                    HeapFree(ProcessHeap,
+                             0,
+                             ConTypeTmp);
+
+                    ConType = NULL;
+                }
+
+                if (ConType) CharToOem(ConTypeTmp, ConType);
             }
         }
     }
@@ -355,20 +381,24 @@ CLEANUP:
 
 VOID ShowInfo(BOOL bAll)
 {
+    MIB_IFROW mibEntry;
     PIP_ADAPTER_INFO pAdapterInfo = NULL;
     PIP_ADAPTER_INFO pAdapter = NULL;
     ULONG adaptOutBufLen = 0;
     PFIXED_INFO pFixedInfo = NULL;
     ULONG netOutBufLen = 0;
+    ULONG ret = 0;
 
     /* call GetAdaptersInfo to obtain the adapter info */
-    if (GetAdaptersInfo(pAdapterInfo, &adaptOutBufLen) == ERROR_BUFFER_OVERFLOW)
+    ret = GetAdaptersInfo(pAdapterInfo, &adaptOutBufLen);
+    if (ret == ERROR_BUFFER_OVERFLOW)
     {
         pAdapterInfo = (IP_ADAPTER_INFO *)HeapAlloc(ProcessHeap, 0, adaptOutBufLen);
         if (pAdapterInfo == NULL)
             return;
 
-        if (GetAdaptersInfo(pAdapterInfo, &adaptOutBufLen) != NO_ERROR)
+        ret = GetAdaptersInfo(pAdapterInfo, &adaptOutBufLen);
+        if (ret != NO_ERROR)
         {
             DoFormatMessage(0);
             HeapFree(ProcessHeap, 0, pAdapterInfo);
@@ -377,8 +407,11 @@ VOID ShowInfo(BOOL bAll)
     }
     else
     {
-        DoFormatMessage(0);
-        return;
+        if( ERROR_NO_DATA != ret )
+        {
+            DoFormatMessage(0);
+            return;
+        }
     }
 
     /* call GetNetworkParams to obtain the network info */
@@ -386,17 +419,24 @@ VOID ShowInfo(BOOL bAll)
     {
         pFixedInfo = (FIXED_INFO *)HeapAlloc(ProcessHeap, 0, netOutBufLen);
         if (pFixedInfo == NULL)
+        {
+            if (pAdapterInfo)
+                HeapFree(ProcessHeap, 0, pAdapterInfo);
             return;
-
+        }
         if (GetNetworkParams(pFixedInfo, &netOutBufLen) != NO_ERROR)
         {
             DoFormatMessage(0);
+            if (pAdapterInfo)
+                HeapFree(ProcessHeap, 0, pAdapterInfo);
             HeapFree(ProcessHeap, 0, pFixedInfo);
             return;
         }
     }
     else
     {
+        if (pAdapterInfo)
+            HeapFree(ProcessHeap, 0, pAdapterInfo);
         DoFormatMessage(0);
         return;
     }
@@ -413,7 +453,7 @@ VOID ShowInfo(BOOL bAll)
             _tprintf(_T("\tIP Routing Enabled. . . . . . . . : Yes\n"));
         else
             _tprintf(_T("\tIP Routing Enabled. . . . . . . . : No\n"));
-        if (pAdapter->HaveWins)
+        if (pAdapter && pAdapter->HaveWins)
             _tprintf(_T("\tWINS Proxy enabled. . . . . . . . : Yes\n"));
         else
             _tprintf(_T("\tWINS Proxy enabled. . . . . . . . : No\n"));
@@ -424,6 +464,9 @@ VOID ShowInfo(BOOL bAll)
     {
         LPTSTR IntType, myConType;
 
+        mibEntry.dwIndex = pAdapter->Index;
+        GetIfEntry(&mibEntry);
+
         IntType = GetInterfaceTypeName(pAdapter->Type);
         myConType = GetConnectionType(pAdapter->AdapterName);
 
@@ -432,7 +475,7 @@ VOID ShowInfo(BOOL bAll)
         if (myConType != NULL) HeapFree(ProcessHeap, 0, myConType);
 
         /* check if the adapter is connected to the media */
-        if (_tcscmp(pAdapter->IpAddressList.IpAddress.String, "0.0.0.0") == 0)
+        if (mibEntry.dwOperStatus != MIB_IF_OPER_STATUS_CONNECTED && mibEntry.dwOperStatus != MIB_IF_OPER_STATUS_OPERATIONAL)
         {
             _tprintf(_T("\tMedia State . . . . . . . . . . . : Media disconnected\n"));
             pAdapter = pAdapter->Next;
@@ -443,7 +486,9 @@ VOID ShowInfo(BOOL bAll)
 
         if (bAll)
         {
-            _tprintf(_T("\tDescription . . . . . . . . . . . : %s\n"), GetConnectionDescription(pAdapter->AdapterName));
+            LPTSTR lpDesc = GetConnectionDescription(pAdapter->AdapterName);
+            _tprintf(_T("\tDescription . . . . . . . . . . . : %s\n"), lpDesc);
+            HeapFree(ProcessHeap, 0, lpDesc);
             _tprintf(_T("\tPhysical Address. . . . . . . . . : %s\n"), PrintMacAddr(pAdapter->Address));
             if (pAdapter->DhcpEnabled)
                 _tprintf(_T("\tDHCP Enabled. . . . . . . . . . . : Yes\n"));
@@ -454,7 +499,10 @@ VOID ShowInfo(BOOL bAll)
 
         _tprintf(_T("\tIP Address. . . . . . . . . . . . : %s\n"), pAdapter->IpAddressList.IpAddress.String);
         _tprintf(_T("\tSubnet Mask . . . . . . . . . . . : %s\n"), pAdapter->IpAddressList.IpMask.String);
-        _tprintf(_T("\tDefault Gateway . . . . . . . . . : %s\n"), pAdapter->GatewayList.IpAddress.String);
+        if (pAdapter->GatewayList.IpAddress.String[0] != '0')
+            _tprintf(_T("\tDefault Gateway . . . . . . . . . : %s\n"), pAdapter->GatewayList.IpAddress.String);
+        else
+            _tprintf(_T("\tDefault Gateway . . . . . . . . . :\n"));
 
         if (bAll)
         {
@@ -491,13 +539,15 @@ VOID ShowInfo(BOOL bAll)
     }
 
     HeapFree(ProcessHeap, 0, pFixedInfo);
-    HeapFree(ProcessHeap, 0, pAdapterInfo);
+    if (pAdapterInfo)
+        HeapFree(ProcessHeap, 0, pAdapterInfo);
 }
 
 VOID Release(LPTSTR Index)
 {
     IP_ADAPTER_INDEX_MAP AdapterInfo;
     DWORD ret;
+    DWORD i;
 
     /* if interface is not given, query GetInterfaceInfo */
     if (Index == NULL)
@@ -513,8 +563,19 @@ VOID Release(LPTSTR Index)
 
             if (GetInterfaceInfo(pInfo, &ulOutBufLen) == NO_ERROR )
             {
-                CopyMemory(&AdapterInfo, &pInfo->Adapter[0], sizeof(IP_ADAPTER_INDEX_MAP));
-                _tprintf(_T("name - %S\n"), pInfo->Adapter[0].Name);
+                for (i = 0; i < pInfo->NumAdapters; i++)
+                {
+                     CopyMemory(&AdapterInfo, &pInfo->Adapter[i], sizeof(IP_ADAPTER_INDEX_MAP));
+                     _tprintf(_T("name - %S\n"), pInfo->Adapter[i].Name);
+
+                     /* Call IpReleaseAddress to release the IP address on the specified adapter. */
+                     if ((ret = IpReleaseAddress(&AdapterInfo)) != NO_ERROR)
+                     {
+                         _tprintf(_T("\nAn error occured while releasing interface %S : \n"), AdapterInfo.Name);
+                         DoFormatMessage(ret);
+                     }
+                }
+
                 HeapFree(ProcessHeap, 0, pInfo);
             }
             else
@@ -539,15 +600,6 @@ VOID Release(LPTSTR Index)
          *      ipconfig /release *con* will release all cards with 'con' in their name
          */
     }
-
-
-    /* Call IpReleaseAddress to release the IP address on the specified adapter. */
-    if ((ret = IpReleaseAddress(&AdapterInfo)) != NO_ERROR)
-    {
-        _tprintf(_T("\nAn error occured while releasing interface %S : \n"), AdapterInfo.Name);
-        DoFormatMessage(ret);
-    }
-
 }
 
 
@@ -556,6 +608,7 @@ VOID Release(LPTSTR Index)
 VOID Renew(LPTSTR Index)
 {
     IP_ADAPTER_INDEX_MAP AdapterInfo;
+    DWORD i;
 
     /* if interface is not given, query GetInterfaceInfo */
     if (Index == NULL)
@@ -586,8 +639,19 @@ VOID Renew(LPTSTR Index)
         /* Make a second call to GetInterfaceInfo to get the actual data we want */
         if (GetInterfaceInfo(pInfo, &ulOutBufLen) == NO_ERROR )
         {
-            CopyMemory(&AdapterInfo, &pInfo->Adapter[0], sizeof(IP_ADAPTER_INDEX_MAP));
-            _tprintf(_T("name - %S\n"), pInfo->Adapter[0].Name);
+            for (i = 0; i < pInfo->NumAdapters; i++)
+            {
+                CopyMemory(&AdapterInfo, &pInfo->Adapter[i], sizeof(IP_ADAPTER_INDEX_MAP));
+                _tprintf(_T("name - %S\n"), pInfo->Adapter[i].Name);
+
+
+                /* Call IpRenewAddress to renew the IP address on the specified adapter. */
+                if (IpRenewAddress(&AdapterInfo) != NO_ERROR)
+                {
+                    _tprintf(_T("\nAn error occured while renew interface %s : "), _T("*name*"));
+                    DoFormatMessage(0);
+                }
+            }
         }
         else
         {
@@ -606,14 +670,6 @@ VOID Renew(LPTSTR Index)
          *      ipconfig /renew *con* will renew all cards with 'con' in their name
          */
     }
-
-
-    /* Call IpRenewAddress to renew the IP address on the specified adapter. */
-    if (IpRenewAddress(&AdapterInfo) != NO_ERROR)
-    {
-        _tprintf(_T("\nAn error occured while renew interface %s : "), _T("*name*"));
-        DoFormatMessage(0);
-    }
 }
 
 
@@ -640,7 +696,7 @@ VOID Usage(VOID)
             if (lpUsage == NULL)
                 return;
 
-            if (LoadString(hInstance,
+            if (LoadStringAndOem(hInstance,
                            IDS_USAGE,
                            lpUsage,
                            Size))
@@ -669,7 +725,7 @@ int main(int argc, char *argv[])
     ProcessHeap = GetProcessHeap();
 
     /* Parse command line for options we have been given. */
-    if ( (argc > 1)&&(argv[1][0]=='/') )
+    if ( (argc > 1)&&(argv[1][0]=='/' || argv[1][0]=='-') )
     {
         if( !_tcsicmp( &argv[1][1], _T("?") ))
         {