win-power-mgmt-flush-test-20041003
[openafs.git] / src / WINNT / afsd / lanahelper.cpp
index 5a8d368..5cdc27b 100644 (file)
@@ -1,4 +1,28 @@
-#include <afx.h>
+/*
+
+Copyright 2004 by the Massachusetts Institute of Technology
+
+All rights reserved.
+
+Permission to use, copy, modify, and distribute this software and its
+documentation for any purpose and without fee is hereby granted,
+provided that the above copyright notice appear in all copies and that
+both that copyright notice and this permission notice appear in
+supporting documentation, and that the name of the Massachusetts
+Institute of Technology (M.I.T.) not be used in advertising or publicity
+pertaining to distribution of the software without specific, written
+prior permission.
+
+M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
+ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
+M.I.T. BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
+ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
+WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
+ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
+SOFTWARE.
+
+*/
+
 #include <windows.h>
 #include <winreg.h>
 #include <nb30.h>
 #include <wtypes.h>
 #include <string.h>
 #include <malloc.h>
+#include <stdio.h>
 #include <lanahelper.h>
 
 #define NOLOGGING
 #ifndef NOLOGGING
 extern "C" {
-    void afsi_log(...);
+#include <stdarg.h>
+
+    void afsi_log(TCHAR *p, ...) {
+        va_list marker;
+        TCHAR buffer[200];
+
+        va_start(marker,p);
+        _vstprintf(buffer,p,marker);
+        va_end(marker);
+        _tcscat(buffer,_T("\n"));
+
+        OutputDebugString(buffer);
+    }
 }
 #endif
 
@@ -357,6 +394,35 @@ extern "C" lana_number_t lana_FindLoopback(void)
     return LANA_INVALID;
 }
 
+/* Returns TRUE if all adapters are loopback adapters */
+extern "C" BOOL lana_OnlyLoopback(void)
+{
+    NCB ncb;
+    LANA_ENUM lana_list;
+    int status;
+    int i;
+
+    memset(&ncb, 0, sizeof(ncb));
+    ncb.ncb_command = NCBENUM;
+    ncb.ncb_buffer = (UCHAR *) &lana_list;
+    ncb.ncb_length = sizeof(lana_list);
+    status = Netbios(&ncb);
+    if (status != 0) {
+#ifndef NOLOGGING
+        afsi_log("Netbios NCBENUM failed: status %ld", status);
+#endif
+        return FALSE;
+    }
+    for (i = 0; i < lana_list.length; i++) {
+       if (!lana_IsLoopback(lana_list.lana[i])) {
+           // Found one non-Loopback adapter
+           return FALSE;
+       }
+    }
+    // All adapters are loopback
+    return TRUE;
+}
+
 // Is the given lana a Windows Loopback Adapter?
 // TODO: implement a better check for loopback
 // TODO: also check for proper bindings (IPv4)
@@ -439,9 +505,9 @@ extern "C" long lana_GetUncServerNameEx(char *buffer, lana_number_t * pLana, int
        LONG rv;
        int regLana;
        int regGateway, regNoFindLanaByName;
-       char regNbName[MAX_NB_NAME_LENGTH];
-       char nbName[MAX_NB_NAME_LENGTH];
-       char hostname[MAX_COMPUTERNAME_LENGTH+1];
+       TCHAR regNbName[MAX_NB_NAME_LENGTH];
+       TCHAR nbName[MAX_NB_NAME_LENGTH];
+       TCHAR hostname[MAX_COMPUTERNAME_LENGTH+1];
 
        rv = RegOpenKeyEx(HKEY_LOCAL_MACHINE,szAFSConfigKeyName,0,KEY_READ,&hkConfig);
        if(rv == ERROR_SUCCESS) {
@@ -505,14 +571,11 @@ extern "C" long lana_GetUncServerNameEx(char *buffer, lana_number_t * pLana, int
             regLana = nLana;
        }
 
-       if(regLana >=0 && lana_IsLoopback((lana_number_t) regLana)) {
-               if(regNbName[0]) {
-                       strncpy(nbName,regNbName,15);
-                       nbName[16] = 0;
-                       strupr(nbName);
-               }
-               else
-                       strcpy(nbName,"AFS");
+       if(regNbName[0] &&
+       (regLana >=0 && lana_IsLoopback((lana_number_t) regLana))) {
+        strncpy(nbName,regNbName,15);
+        nbName[16] = 0;
+        strupr(nbName);
        } else {
                char * dot;
 
@@ -572,8 +635,9 @@ extern "C" void lana_GetUncServerName(TCHAR *name, int type) {
 
 extern "C" void lana_GetAfsNameString(int lanaNumber, BOOL isGateway, TCHAR* name)
 {
-    lana_GetUncServerNameDynamic(lanaNumber, isGateway, name,LANA_NETBIOS_NAME_FULL);
-    _stprintf(name, _T("Your UNC name to reach the root of AFS is \\\\%s\\all"), name);
+    TCHAR netbiosName[32];
+    lana_GetUncServerNameDynamic(lanaNumber, isGateway, netbiosName, LANA_NETBIOS_NAME_FULL);
+    _stprintf(name, _T("Your UNC name to reach the root of AFS is \\\\%s\\all"), netbiosName);
 }
 
 extern "C" void lana_GetNetbiosName(LPTSTR pszName, int type)