/* TO DO: */
/* Randomize local connection port */
#include <windowsx.h>
#include <windows.h>
#include <winsock.h>
#include <stdio.h>
#include <shellapi.h>
#include <winsock.h>
#include <string.h>
#include "resource.h"
#include "DynDNS95.h"
#define REMOTEPORT 50
#define REMOTEHOST "master.dyndns.com"
#define NAME "DYNDNS.COM Win95/NT Client BETA"
#define VERSION "4.95-client-a"
#define AUTHOR "Gus Hurwitz"
#define CONTACT "ghurwitz@dyndns.com"
#define RegDynDNSKey "Software\\DynDNS2000"
#define DUMMYPORT 2454 // This should be changed to something random
NOTIFYICONDATA tnid;
HINSTANCE gInstance;
HWND mDlg;
SOCKET s_internal_connect;
SOCKET s_internal_bind;
SOCKET s_dyndns;
int hidden=0;
int update=0;
int WSAStarted=0;
int AccountStatus=0;
int RunningUpdate=0;
int DEBUG2=0;
DWORD i;
char uname[64], passwd[64], domain[64], IP[20], MBuf[8192], MRec[2048], *RBuf;
char fulldomain[64];
char scratchpad[64], bigpad[1024];
static DWORD ToUpdate, IPCheckDelay, SinceUpdate, RetryDelay;
int UpdateIP(HWND, char *, char *, char *, char *);
char FAR *Name2Addr(char *);
int IPCurrent(HWND);
BOOL CALLBACK DynDNSProc(HWND, UINT, WPARAM, LPARAM);
BOOL CALLBACK DynDNSSetupProc(HWND, UINT, WPARAM, LPARAM);
BOOL MySetDlgItemText(HWND, int, LPCTSTR, BOOL);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
MSG msg;
HICON hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(SmallIcon));
gInstance=hInstance;
IP [0] = '\0';
memset(MBuf, 0, 8192);
memset(MRec, 0, 2048);
tnid.cbSize = sizeof(NOTIFYICONDATA);
tnid.uID = 3265;
tnid.hWnd = NULL;
tnid.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
tnid.uCallbackMessage = WM_NOTIFYICON;
tnid.hIcon = hIcon;
strcpy(tnid.szTip, "DynDNS.com Updater");
tnid.hWnd = CreateDialog(hInstance, MAKEINTRESOURCE(IDD_DynDNS), NULL, DynDNSProc);
Shell_NotifyIcon(NIM_ADD, &tnid);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (msg.wParam);
}
BOOL CALLBACK DynDNSSetupProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
HKEY hKey;
DWORD cbData;
char debug[64];
switch(uMsg)
{
case WM_INITDIALOG:
case WM_CREATE:
{
if(RegOpenKeyEx(HKEY_CURRENT_USER, RegDynDNSKey, 0, KEY_QUERY_VALUE,&hKey)!=ERROR_SUCCESS)
{
}
else
{
cbData=64; RegQueryValueEx(hKey, "domain", NULL, NULL, scratchpad, &cbData);scratchpad[cbData]='\0';
MySetDlgItemText(hDlg, IDC_DOMAIN, scratchpad, 0);
cbData=64; RegQueryValueEx(hKey, "passwd", NULL, NULL, scratchpad, &cbData);scratchpad[cbData]='\0';
MySetDlgItemText(hDlg, IDC_PASSWD, scratchpad, 0);
RegCloseKey(hKey);
}
}
break;
case WM_DESTROY:
break;
case WM_COMMAND:
switch(wParam)
{
case IDC_OK:
if(RegCreateKeyEx(HKEY_CURRENT_USER, RegDynDNSKey, 0, NULL, REG_OPTION_NON_VOLATILE,
KEY_SET_VALUE, NULL, &hKey, &cbData)!=ERROR_SUCCESS)
{
MessageBox(hDlg, "Unable to access registry! Quiting.", "Fatal Error", MB_OK);
exit(-1);
}
cbData = GetDlgItemText(hDlg, IDC_PASSWD,passwd, sizeof(scratchpad))+1;
RegSetValueEx(hKey, "passwd", 0, REG_SZ, passwd, cbData);
cbData = GetDlgItemText(hDlg, IDC_DOMAIN,domain, sizeof(scratchpad))+1;
RegSetValueEx(hKey, "domain", 0, REG_SZ, domain, cbData);
IPCheckDelay=300;
RetryDelay=60;
RegSetValueEx(hKey, "uname", 0, REG_SZ,"postmaster", strlen("postmaster"));
RegSetValueEx(hKey, "IP", 0, REG_SZ, "AUTO", strlen("AUTO"));
RegSetValueEx(hKey, "IPCheckDelay", 0, REG_DWORD, (LPBYTE)&IPCheckDelay, sizeof(DWORD));
RegSetValueEx(hKey, "RetryDelay", 0, REG_DWORD, (LPBYTE)&RetryDelay, sizeof(DWORD));
RegCloseKey(hKey);
EndDialog(hDlg, 0);
break;
case IDC_CANCEL:
cbData = GetDlgItemText(hDlg, IDC_DOMAIN,debug, sizeof(scratchpad))+1;
if(!strncmp("debug--1", debug, 8))
{
MessageBox(hDlg, MRec, "Debug", MB_OK);
}
else
if(!strncmp("debug--2", debug, 8))
{
DEBUG2=(DEBUG2?0:1);
MessageBox(hDlg, "Debug level 2 toggeled", "Debug", MB_OK);
}
EndDialog(hDlg, 0);
break;
}
break;
default:
return(FALSE);
break;
}
return(TRUE);
}
BOOL CALLBACK DynDNSProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg)
{
case WM_INITDIALOG:
case WM_CREATE:
{
WSADATA WSAdata;
HKEY hKey;
DWORD cbData;
if(RegOpenKeyEx(HKEY_CURRENT_USER, RegDynDNSKey, 0, KEY_QUERY_VALUE,&hKey)!=ERROR_SUCCESS)
{
DialogBox(gInstance, MAKEINTRESOURCE(IDD_DynDNSSetup), hDlg, DynDNSSetupProc);
if(!strncmp("postmaster", uname, 10))
{
sprintf(fulldomain, "%s", domain);
}
else
{
sprintf(fulldomain, "%s.%s", uname, domain);
}
MySetDlgItemText(hDlg, IDC_STATIC_DOMAIN, fulldomain, 0);
MySetDlgItemText(hDlg, IDC_STATIC_IP, IP, 0);
MySetDlgItemText(hDlg, IDC_STATIC_STATUS, "Setup Complete", 1);
if(RegOpenKeyEx(HKEY_CURRENT_USER, RegDynDNSKey, 0, KEY_QUERY_VALUE,&hKey)!=ERROR_SUCCESS)
{
MessageBox(hDlg, "Key Creation Failed! Quiting.", "Fatal Error", MB_OK);
exit(-1);
}
}
cbData=64; RegQueryValueEx(hKey, "uname", NULL, NULL, uname, &cbData);
cbData=64; RegQueryValueEx(hKey, "passwd", NULL, NULL, passwd, &cbData);
cbData=64; RegQueryValueEx(hKey, "domain", NULL, NULL, domain, &cbData);
cbData=20; RegQueryValueEx(hKey, "IP", NULL, NULL, IP, &cbData);
cbData=sizeof(DWORD); RegQueryValueEx(hKey, "IPCheckDelay", NULL, NULL, (LPBYTE)&IPCheckDelay, &cbData);
cbData=sizeof(DWORD); RegQueryValueEx(hKey, "RetryDelay", NULL, NULL, (LPBYTE)&RetryDelay, &cbData);
ToUpdate=IPCheckDelay;
sprintf(scratchpad, "%.2d:%.2d:%.2d", ToUpdate/3600, ToUpdate/60, ToUpdate%60);
MySetDlgItemText(hDlg, IDC_STATIC_ToUpdate, scratchpad, 0);
RegCloseKey(hKey);
MySetDlgItemText(hDlg, IDC_STATIC_STATUS, "Initializing...", 1);
if(WSAStartup(0x0101, &WSAdata))
{
MySetDlgItemText(hDlg, IDC_STATIC_STATUS, "WSAStartup Error", 1);
}
else
{
WSAStarted=1;
}
if(!strncmp("postmaster", uname, 10))
{
sprintf(fulldomain, "%s", domain);
}
else
{
sprintf(fulldomain, "%s.%s", uname, domain);
}
MySetDlgItemText(hDlg, IDC_STATIC_DOMAIN, fulldomain, 0);
MySetDlgItemText(hDlg, IDC_STATIC_IP, IP, 0);
MySetDlgItemText(hDlg, IDC_STATIC_STATUS, "Client Initialized", 1);
}
SetTimer(hDlg, SecondCount, 1000, NULL);
mDlg = hDlg;
break;
case WM_DESTROY:
Shell_NotifyIcon(NIM_DELETE, &tnid);
WSACleanup();
WSAStarted=0;
break;
case WM_TIMER:
switch(wParam)
{
case SecondCount:
if(!RunningUpdate)
{
ToUpdate--;
sprintf(scratchpad, "%.2d:%.2d:%.2d", ToUpdate/3600, ToUpdate/60, ToUpdate%60);
MySetDlgItemText(hDlg, IDC_STATIC_ToUpdate, scratchpad, 0);
}
SinceUpdate++;
sprintf(scratchpad, "%.2d:%.2d:%.2d", SinceUpdate/3600, SinceUpdate/60, SinceUpdate%60);
MySetDlgItemText(hDlg, IDC_STATIC_SinceUpdate, scratchpad, 0);
if((!ToUpdate) && (!RunningUpdate))
{
MySetDlgItemText(hDlg, IDC_STATIC_STATUS, "Checking IP", 1);
if(update)
{
MySetDlgItemText(hDlg, IDC_STATIC_STATUS, "Forced IP Update", 1);
if(UpdateIP(hDlg, uname, passwd, domain, IP))
{
ToUpdate=IPCheckDelay;
sprintf(scratchpad, "%.2d:%.2d:%.2d", ToUpdate/3600, ToUpdate/60, ToUpdate%60);
MySetDlgItemText(hDlg, IDC_STATIC_ToUpdate, scratchpad, 0);
SinceUpdate=0;
sprintf(scratchpad, "%.2d:%.2d:%.2d", SinceUpdate/3600, SinceUpdate/60, SinceUpdate%60);
MySetDlgItemText(hDlg, IDC_STATIC_SinceUpdate, scratchpad, 0);
RunningUpdate=0;
}
else
{