📄 pnmisc.c
字号:
/*____________________________________________________________________________
Copyright (C) 1998 Network Associates, Inc.
All rights reserved.
PNmisc.c - PGPnet miscellaneous routines
$Id: PNmisc.c,v 1.39.4.1 1999/06/07 17:21:22 pbj Exp $
____________________________________________________________________________*/
#include <windows.h>
#include <commctrl.h>
#include <shlobj.h>
#include <winsock.h>
#include "resource.h"
#include "PGPnetApp.h"
#include "PGPcl.h"
#include "pgpNetIPC.h"
#include "pgpNetPaths.h"
#include "pgpTrayIPC.h"
#include "pgpOptionList.h"
#include "pgpUserInterface.h"
#define MIN_SECRET_LENGTH 1
#define MIN_SECRET_QUALITY 0
#define IPC_TIMEOUT 2500
extern HINSTANCE g_hinst;
extern HWND g_hwndStatusDlg;
extern PGPContextRef g_context;
extern BOOL g_bWinsock;
extern APPOPTIONSSTRUCT g_AOS;
// ___________________________________________________
//
// Message box routine using string table resource IDs
LPARAM
PNMessageBox (
HWND hWnd,
INT iCaption,
INT iMessage,
ULONG ulFlags)
{
CHAR szCaption [128];
CHAR szMessage [512];
LoadString (g_hinst, iCaption, szCaption, sizeof(szCaption));
LoadString (g_hinst, iMessage, szMessage, sizeof(szMessage));
ulFlags |= MB_SETFOREGROUND;
return (MessageBox (hWnd, szMessage, szCaption, ulFlags));
}
// ____________________________________
//
// cat a string resource onto string, using comma if necessary
VOID
PNCommaCat (
LPSTR psz,
INT ids)
{
CHAR szTemp[32];;
// if string is not empty, cat comma, space onto it
if (psz[0])
{
LoadString (g_hinst, IDS_COMMASPACE, szTemp, sizeof(szTemp));
lstrcat (psz, szTemp);
}
LoadString (g_hinst, ids, szTemp, sizeof(szTemp));
lstrcat (psz, szTemp);
}
// ____________________________________
//
// put up the hourglass cursor while reloading keyrings
BOOL
PNLoadKeyRings (
HWND hwnd,
PGPKeySetRef* pkeyset)
{
HCURSOR hcursorOld;
PGPError err;
hcursorOld = SetCursor (LoadCursor (NULL, IDC_WAIT));
if (PGPKeySetRefIsValid (*pkeyset))
PGPFreeKeySet (*pkeyset);
err = PGPOpenDefaultKeyRings (g_context, 0, pkeyset);
SetCursor (hcursorOld);
if (IsPGPError (err))
{
PNMessageBox (hwnd, IDS_CAPTION, IDS_CANNOTOPENKEYRINGS,
MB_OK|MB_ICONSTOP);
*pkeyset = kInvalidPGPKeySetRef;
return FALSE;
}
else
return TRUE;
}
// ________________________
//
// Query service for status
UINT
PNGetServiceStatus (
HWND hwndCaller)
{
static HWND hwndService = (HWND)0xFFFFFFFF;
HWND hwnd;
DWORD dwResult;
hwnd = FindWindow (PGPNET_SERVICECOMMWINDOWNAME,
PGPNET_SERVICECOMMWINDOWNAME);
if (hwnd)
{
if (SendMessageTimeout (hwnd, PGPNET_M_APPMESSAGE,
(WPARAM)PGPNET_QUERYDRIVERSTATUS, (LPARAM)hwndCaller,
SMTO_NORMAL, IPC_TIMEOUT, &dwResult))
{
if (hwnd != hwndService)
{
// service has come alive => tell it we want SAs
if (hwndService != (HWND)0xFFFFFFFF)
{
Sleep (100);
SendMessageTimeout (hwnd, PGPNET_M_APPMESSAGE,
(WPARAM)PGPNET_ENABLESTATUSMESSAGES,
(LPARAM)hwndCaller,
SMTO_NORMAL, IPC_TIMEOUT, &dwResult);
}
hwndService = hwnd;
}
return dwResult;
}
else
{
if (hwndService)
{
// service was running, now it's not => clear SA list
hwndService = NULL;
PostMessage (g_hwndStatusDlg, PGPNET_M_CLEARSALIST, 0, 0);
}
return PGPNET_SERVICENOTRESPONDING;
}
}
else
{
if (hwndService)
{
// service was running, now it's not => clear SA list
hwndService = NULL;
PostMessage (g_hwndStatusDlg, PGPNET_M_CLEARSALIST, 0, 0);
}
return PGPNET_SERVICENOTAVAILABLE;
}
}
// ________________________
//
// Send message to service, if running
BOOL
PNSendServiceMessage (
UINT uMsg,
WPARAM wParam,
LPARAM lParam)
{
HWND hwnd;
DWORD dwResult;
hwnd = FindWindow (PGPNET_SERVICECOMMWINDOWNAME,
PGPNET_SERVICECOMMWINDOWNAME);
if (hwnd)
{
if (SendMessageTimeout (hwnd, uMsg, wParam, lParam,
SMTO_NORMAL, IPC_TIMEOUT, &dwResult))
return TRUE;
else
return FALSE;
}
else
return FALSE;
}
// ________________________
//
// Send message to tray app, if running
BOOL
PNSendTrayAppMessage (
UINT uMsg,
WPARAM wParam,
LPARAM lParam)
{
HWND hwnd;
DWORD dwResult;
hwnd = FindWindow (PGPTRAY_WINDOWNAME, PGPTRAY_WINDOWNAME);
if (hwnd)
{
SendMessageTimeout (hwnd, uMsg, wParam, lParam,
SMTO_NORMAL, IPC_TIMEOUT, &dwResult);
return TRUE;
}
else
return FALSE;
}
// ________________________
//
// Convert tm to SystemTime
VOID
sTimeToSystemTime (
struct tm* ptm,
SYSTEMTIME* pst)
{
pst->wYear = ptm->tm_year + 1900;
pst->wMonth = ptm->tm_mon + 1;
pst->wDay = ptm->tm_mday;
pst->wDayOfWeek = ptm->tm_wday;
pst->wHour = ptm->tm_hour;
pst->wMinute = ptm->tm_min;
pst->wSecond = ptm->tm_sec;
pst->wMilliseconds = 0;
}
// ______________________________________________________
//
// Convert time to string format based on system settings
VOID
PNConvertTimeToString (
PGPTime Time,
LPSTR sz,
INT iLen)
{
SYSTEMTIME systemtime;
time_t ttTime;
INT iBytes;
struct tm* ptm;
ttTime = PGPGetStdTimeFromPGPTime (Time);
ptm = localtime (&ttTime);
if (ptm) {
sTimeToSystemTime (ptm, &systemtime);
iBytes = GetDateFormat (LOCALE_USER_DEFAULT, DATE_SHORTDATE,
&systemtime, NULL, sz, iLen);
lstrcat (sz, " ");
GetTimeFormat (LOCALE_USER_DEFAULT, 0,
&systemtime, NULL, &sz[iBytes], iLen-iBytes);
}
else
LoadString (g_hinst, IDS_INVALIDDATE, sz, iLen);
}
// ______________________________________________________
//
// get the IP and hostname of this computer
VOID
PNGetLocalHostInfo (
DWORD* pdwAddress,
LPSTR psz,
INT iLen)
{
if (!psz || (iLen == 0))
return;
psz[0] = '\0';
if (pdwAddress)
*pdwAddress = 0;
if (!g_bWinsock)
return;
if (gethostname (psz, iLen) != SOCKET_ERROR)
{
struct hostent* phe = gethostbyname (psz);
if (phe != NULL)
{
// local ip address found
struct in_addr ia;
ia.S_un.S_addr =
((struct in_addr*)(phe->h_addr_list[0]))->s_addr;
if (pdwAddress)
{
*pdwAddress = ia.S_un.S_addr;
lstrcpyn (psz, inet_ntoa (ia), iLen);
}
else
{
lstrcpyn (psz, phe->h_name, iLen);
}
}
}
}
// ______________________________________________________
//
// get the IP of the specified host
BOOL
PNGetRemoteHostIP (
LPSTR psz,
DWORD* pdwAddress)
{
struct hostent* phe;
if (!psz || !pdwAddress)
return FALSE;
*pdwAddress = 0;
if (!g_bWinsock)
return FALSE;
phe = gethostbyname (psz);
if (phe != NULL)
{
// ip address found
struct in_addr ia;
ia.S_un.S_addr =
((struct in_addr*)(phe->h_addr_list[0]))->s_addr;
*pdwAddress = ia.S_un.S_addr;
return TRUE;
}
else
return FALSE;
}
// ____________________________________
//
// get confirmed passphrase for use as shared secret
BOOL
PNGetSharedSecret (
HWND hwnd,
LPSTR pszSecret,
UINT uMaxSecretLength)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -