// checkdun.c - main (built with build.bat)

// Copyright NAT Software, 2001

#include <windows.h>
#include <ras.h>

#define OK 1
#define SYSERR -1

char cur_dir[1024];
char sys_dir[1024];
char win_dir[1024];

OSVERSIONINFO version;
int platform;
int platform2;
int completed;

HINSTANCE hRasDll;
int ras_loaded;
char pbk_file[8192];

// RAS function prototypes

DWORD APIENTRY (*pRasEnumEntries)(LPTSTR, LPTSTR, LPRASENTRYNAME, LPDWORD, LPDWORD);

char buf[4096];

// Function prototypes

DWORD FindNet95(HKEY hStartKey , char *pKeyName, char *rbuf);

//
// kprintf - low-level printf
//
int kprintf(char *fmt, ...)
{
    va_list args;
    int i;
    int w;
    char buf[0x8000];                       /* buf will be at top of stack */

    va_start(args, fmt);
    vsprintf(buf, fmt, args);
    va_end(args);

    return MessageBox(NULL, buf, "NAT32 Dial-Up Connection Checker", MB_OKCANCEL | MB_SETFOREGROUND);

}

int main(int argc, char **argv)
{
    if (argc != 1) {
        printf("Usage: checkdun\n");
        return 0;
    }

    GetCurrentDirectory(1024, buf);
    GetShortPathName(buf, cur_dir, 1024);
    GetWindowsDirectory(win_dir, 1024);
    GetSystemDirectory(sys_dir, 1024);

    version.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
    GetVersionEx(&version);

    if (version.dwPlatformId == VER_PLATFORM_WIN32_NT)
        platform = 1;

    if (version.dwMajorVersion == 5)
        platform2 = 1;          // Windows 2000

    if (platform2 == 0)
        load_ras();
                
    CheckConnections();

    return 0;
}

//
// load_ras - load the RASAPI32.DLL if available
//

int load_ras()
{
    
    hRasDll = LoadLibraryEx("rasapi32.dll", NULL, 0);
    
    if (hRasDll == 0) {
        ras_loaded = 0;
        return SYSERR;
    }
    
    // Load all needed functions
    
    if ((pRasEnumEntries = (int (*) ()) GetProcAddress(hRasDll, "RasEnumEntriesA")) == 0) {
        ras_loaded = 0;
        return SYSERR;
    }
    ras_loaded = 1;
    return OK;
}

//
// CheckConnections - check all default phonebook RAS connections for
//                    IP Header Compression, turning it off if needed.
//

int CheckConnections()
{
    RASENTRYNAME names[256];
    int i, n, size;
    int entries = 0;
    
    if (ras_loaded == 0) {
        printf("RAS not installed\n");
        return SYSERR;
    }

    strcpy(pbk_file, sys_dir);
    strcat(pbk_file, "\\ras\\rasphone.pbk");

    names[0].dwSize = sizeof(RASENTRYNAME);
    size = sizeof(names);
    n = pRasEnumEntries(NULL, NULL, names, &size, &entries);
    if (n)
        return OK;
    else
        for (i=0; i<entries; i++) {
            printf("%2d %s ", i, names[i].szEntryName);
            if (GetHeaderCompression(names[i].szEntryName) == 1) {
                ModifyRasProfile(names[i].szEntryName);
                printf("<----- MODIFIED\n");                
            }
            else
                printf("OK\n");
        }
    return OK;
}

//
// GetHeaderCompression - get the current setting for the named connection
//

int GetHeaderCompression(char *name)
{

    HKEY    RemoteAccessKey;
    HKEY    ProfileKey;
    HKEY    NameKey;
    
    LONG    Status;

    DWORD   RegType;
    DWORD   size=128;
    BYTE    buf[128];
    int i;

    if (platform) {
        i = GetPrivateProfileInt(name, "IPHeaderCompression", SYSERR, pbk_file);
        return i;
    }
    
    Status = RegOpenKeyEx(HKEY_CURRENT_USER,
                          TEXT("RemoteAccess"),
                          0,
                          KEY_READ,
                          &RemoteAccessKey);

    if (Status == ERROR_SUCCESS) {

        Status = RegOpenKeyEx(RemoteAccessKey,
                              TEXT("Profile"),
                              0,
                              KEY_READ,
                              &ProfileKey);

        if (Status == ERROR_SUCCESS) {

            Status = RegOpenKeyEx(ProfileKey,
                                  name,
                                  0,
                                  KEY_READ,
                                  &NameKey);

            if (Status == ERROR_SUCCESS) {

                Status = RegQueryValueEx(NameKey,
                                         TEXT("IP"),
                                         NULL,
                                         &RegType,
                                         (LPBYTE)buf,
                                         &size);
                if (Status == ERROR_SUCCESS) {
                    if ((buf[4] & 4) != 4)
                        Status = 1;         // IP Header Compression is ON
                    else
                        Status = 0;         // IP Header Compression is OFF
                }
                RegCloseKey(NameKey);
            }
            RegCloseKey(ProfileKey);
        }
        RegCloseKey(RemoteAccessKey);
    }
    return Status;
}

//
// ModifyRasProfile - actually change the IP Header Compression setting
//                    for the named connection
//

int ModifyRasProfile(char *name)
{

    HKEY    RemoteAccessKey;
    HKEY    ProfileKey;
    HKEY    NameKey;
    
    LONG    Status;

    DWORD   RegType;
    DWORD   size=128;
    BYTE    buf[128];
    int i;
    
    if (platform) {
        WritePrivateProfileString(name, "IPHeaderCompression", "0", pbk_file);
        return OK;
    }
        
    Status = RegOpenKeyEx(HKEY_CURRENT_USER,
                          TEXT("RemoteAccess"),
                          0,
                          KEY_READ,
                          &RemoteAccessKey);

    if (Status == ERROR_SUCCESS) {

        Status = RegOpenKeyEx(RemoteAccessKey,
                              TEXT("Profile"),
                              0,
                              KEY_READ,
                              &ProfileKey);

        if (Status == ERROR_SUCCESS) {

            Status = RegOpenKeyEx(ProfileKey,
                                  name,
                                  0,
                                  KEY_READ,
                                  &NameKey);

            if (Status == ERROR_SUCCESS) {

                Status = RegQueryValueEx(NameKey,
                                         TEXT("IP"),
                                         NULL,
                                         &RegType,
                                         (LPBYTE)buf,
                                         &size);
                if (Status == ERROR_SUCCESS) {
                    if ((buf[4] & 4) != 4) {  // Modify IP Header Compression
                        buf[4] |= 4;
                        Status = RegSetValueEx(NameKey,
                                               TEXT("IP"),
                                               NULL,
                                               RegType,
                                               (LPBYTE)buf,
                                               size);
                    }
                }
                RegCloseKey(NameKey);
            }
            RegCloseKey(ProfileKey);
        }
        RegCloseKey(RemoteAccessKey);
    }
    return Status;
}


