#include <windows.h>
#include <stdio.h>
#include <string.h>
#include <winlib/winlib.h>

#include "myping.h"


BOOL CALLBACK MainWndProc(HWND hwnd, UINT msg, WPARAM wParam,
               LPARAM lParam)
{
       WORD id;
       static VARS *v;
       char s[82];

       switch (msg) {
               case WM_INITDIALOG:
                       v = (VARS*)lParam;

                       memset(s, 0, 82);
                       if (! GetRegVal("Software\\MyPing", "host", (void*)s, 80))
                               strcpy(s, "localhost");
                       SetDlgItemText(hwnd, IDC_HOST, (LPCTSTR)s);

                       memset(s, 0, 82);
                       if (! GetRegVal("Software\\MyPing", "interval", (void*)s, 80))
                               strcpy(s, "1000");
                       SetDlgItemText(hwnd, IDC_INT, (LPCTSTR)s);

                       SetStatusParts(GetDlgItem(hwnd, IDC_STATUS), 2);
                       break;

               case WM_COMMAND:
                       id = LOWORD(wParam);

                       switch (id) {
                               case IDC_START:
                                       if (v->hIP)
                                               break;

                                       memset(s, 0, 80);
                                       GetDlgItemText(hwnd, IDC_HOST, s, 80);
                                       if (! (v->phostent = gethostbyname(s))) {
                                               MessageBox(hwnd, "Couldn't resolve host", "MyPing",
                                                               MB_OK | MB_ICONERROR);
                                               break;
                                       }

                                       v->dwIPAddr = (DWORD *)(*(v->phostent->h_addr_list));

                                       v->hIP = v->pIcmpCreateFile();
                                       v->I.Ttl = 6;

                                       memset(s, 0, 80);
                                       GetDlgItemText(hwnd, IDC_INT, s, 80);
                                       SetTimer(hwnd, 1, atoi(s), NULL);

                                       ShowStatus(GetDlgItem(hwnd, IDC_STATUS), 0, "Running");
                                       break;

                               case IDC_STOP:
                                       if (! v->hIP)
                                               break;

                                       KillTimer(hwnd, 1);
                                       v->pIcmpCloseHandle(v->hIP);
                                       v->hIP = 0;

                                       ShowStatus(GetDlgItem(hwnd, IDC_STATUS), 0, "");
                                       break;

                               default:
                                       return FALSE;
                       }
                       break;

               case WM_TIMER:
                       v->pIcmpSendEcho(v->hIP, *(v->dwIPAddr), 0, 0, &v->I, &v->E,
                                       sizeof(v->E), 8000);
                       break;

               case WM_CLOSE:
                       DestroyWindow(hwnd);
                       break;

               case WM_DESTROY:
                       if (v->hIP) {
                               KillTimer(hwnd, 1);
                               v->pIcmpCloseHandle(v->hIP);
                       }

                       memset(s, 0, 82);
                       GetDlgItemText(hwnd, IDC_HOST, s, 80);
                       SetRegVal("Software\\MyPing", "host", (void*)s, strlen(s));

                       memset(s, 0, 82);
                       GetDlgItemText(hwnd, IDC_INT, s, 80);
                       SetRegVal("Software\\MyPing", "interval", (void*)s, strlen(s));

                       FreeLibrary(v->hIcmp);
                       WSACleanup();
                       PostQuitMessage(0);
                       break;

               default:
                       return FALSE;
       }
       return TRUE;
}


static VARS* Init(HINSTANCE hInst, LPSTR cmdLine, INT nCmdShow)
{
       WNDCLASS wc;
       VARS *v;

       v = (VARS*)malloc(sizeof(VARS));

       v->hInst = hInst;

       v->hIcmp = LoadLibrary("ICMP.DLL");
       v->hIP = 0;

       v->pIcmpCreateFile = GetProcAddress(v->hIcmp, "IcmpCreateFile");
       v->pIcmpCloseHandle = GetProcAddress(v->hIcmp, "IcmpCloseHandle");
       v->pIcmpSendEcho = GetProcAddress(v->hIcmp, "IcmpSendEcho");

       WSAStartup(0x0101, &v->wsa);

       GetClassInfo(NULL, "#32770", &wc);
       wc.lpszClassName = "MainWndClass";
       if (! RegisterClass(&wc))
               return NULL;

       v->hwndMain = CreateDialogParam(v->hInst, "MainWnd", 0,
                       MainWndProc, (LPARAM)v);

       ShowWindow(v->hwndMain, nCmdShow);
       return v;
}


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
               LPSTR lpCmdLine, INT nCmdShow)
{
       MSG msg;
       VARS *v;
       HACCEL hAccel;

       if (! (v = Init(hInstance, lpCmdLine, nCmdShow)))
               return 0;

       while (GetMessage(&msg, NULL, NULL, NULL)) {
               if (! IsDialogMessage(v->hwndMain, &msg)) {
                       TranslateMessage(&msg);
                       DispatchMessage(&msg);
               }
       }

       return msg.wParam;
}