-
Notifications
You must be signed in to change notification settings - Fork 0
/
MiscUtils.h
155 lines (142 loc) · 7.33 KB
/
MiscUtils.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
* XMail by Davide Libenzi (Intranet and Internet mail server)
* Copyright (C) 1999,..,2010 Davide Libenzi
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Davide Libenzi <[email protected]>
*
*/
#ifndef _MISCUTILS_H
#define _MISCUTILS_H
#include "Hash.h"
#define LOCK_FILE_WAITSTEP 1
#define INVALID_FSCAN_HANDLE ((FSCAN_HANDLE) 0)
#define HASH_INIT_VALUE 5381
#define THCF_USE_SSL (1 << 0)
#define THCF_SHUTDOWN (1 << 1)
typedef struct FSCAN_HANDLE_struct {
} *FSCAN_HANDLE;
struct AddressFilter {
SYS_INET_ADDR Addr;
SYS_UINT8 Mask[sizeof(SYS_INET_ADDR)];
};
struct ThreadConfig {
char const *pszName;
unsigned int (*pfThreadProc)(void *);
long (*pfThreadCnt)(ThreadConfig const *);
SHB_HANDLE hThShb;
unsigned long ulFlags;
int iNumAddr;
SYS_INET_ADDR SvrAddr[MAX_ACCEPT_ADDRESSES];
int iNumSockFDs;
SYS_SOCKET SockFDs[MAX_ACCEPT_ADDRESSES];
};
struct ThreadCreateCtx {
SYS_SOCKET SockFD;
ThreadConfig const *pThCfg;
};
int MscDatumAlloc(Datum *pDm, void const *pData, size_t sSize);
LstDatum *MscLstDatumAlloc(void const *pData, size_t sSize);
int MscLstDatumAddT(SysListHead *pHead, void const *pData, size_t sSize);
void MscFreeDatumList(SysListHead *pHead);
int MscUniqueFile(char const *pszDir, char *pszFilePath, size_t sMaxPath);
void MscSafeGetTmpFile(char *pszPath, size_t sMaxPath);
int MscRecvTextFile(char const *pszFileName, BSOCK_HANDLE hBSock, int iTimeout,
int (*pStopProc) (void *) = NULL, void *pParam = NULL);
ssize_t MscSendTextFile(char const *pszFileName, BSOCK_HANDLE hBSock, int iTimeout,
int (*pStopProc) (void *) = NULL, void *pParam = NULL);
int MscSendFileCRLF(char const *pszFilePath, BSOCK_HANDLE hBSock, int iTimeout);
char *MscTranslatePath(char *pszPath);
void *MscLoadFile(char const *pszFilePath, size_t *pFileSize);
int MscLockFile(char const *pszFileName, int iMaxWait, int iWaitStep = LOCK_FILE_WAITSTEP);
int MscGetTimeNbrString(char *pszTimeStr, int iStringSize, time_t tTime = 0);
int MscGetTime(struct tm &tmLocal, int &iDiffHours, int &iDiffMins, time_t tCurr = 0);
char *MscStrftime(struct tm const *ptmTime, char *pszDateStr, size_t sSize);
int MscGetTimeStr(char *pszTimeStr, size_t sStringSize, time_t tCurr = 0);
int MscGetDirectorySize(char const *pszPath, bool bRecurse, SYS_OFF_T &llDirSize,
unsigned long &ulNumFiles, int (*pFNValidate) (char const *) = NULL);
FSCAN_HANDLE MscFirstFile(char const *pszPath, int iListDirs, char *pszFileName,
size_t sSize);
int MscNextFile(FSCAN_HANDLE hFileScan, char *pszFileName, size_t sSize);
void MscCloseFindFile(FSCAN_HANDLE hFileScan);
int MscGetFileList(char const *pszPath, int iListDirs, SysListHead *pHead);
int MscCreateEmptyFile(char const *pszFileName);
int MscClearDirectory(char const *pszPath, int iRecurseSubs = 1);
int MscCopyFile(char const *pszCopyTo, char const *pszCopyFrom);
int MscAppendFile(char const *pszCopyTo, char const *pszCopyFrom);
int MscCopyFile(FILE *pFileOut, FILE *pFileIn, SYS_OFF_T llBaseOffset,
SYS_OFF_T llCopySize);
int MscDos2UnixFile(FILE *pFileOut, FILE *pFileIn);
int MscMoveFile(char const *pszOldName, char const *pszNewName);
char *MscGetString(FILE *pFile, char *pszBuffer, size_t sMaxChars,
int *piGotNL = NULL);
char *MscFGets(char *pszLine, int iLineSize, FILE *pFile);
char *MscGetConfigLine(char *pszLine, int iLineSize, FILE *pFile, bool bSkipComments = true);
int MscGetPeerHost(SYS_SOCKET SockFD, char *pszFQDN, int iSize);
int MscGetSockHost(SYS_SOCKET SockFD, char *pszFQDN, int iSize);
int MscGetServerAddress(char const *pszServer, SYS_INET_ADDR &SvrAddr, int iPortNo = 0);
int MscSplitFQDN(char const *pszFQDN, char *pszHost, size_t sHSize,
char *pszDomain, size_t sDSize);
char *MscLogFilePath(char const *pszLogFile, char *pszLogFilePath);
int MscFileLog(char const *pszLogFile, char const *pszFormat, ...);
int MscSplitPath(char const *pszFilePath, char *pszDir, int iDSize,
char *pszFName, int iFSize, char *pszExt, int iESize);
int MscGetFileName(char const *pszFilePath, char *pszFileName);
int MscCreateClientSocket(char const *pszServer, int iPortNo, int iSockType,
SYS_SOCKET *pSockFD, SYS_INET_ADDR *pSvrAddr,
SYS_INET_ADDR *pSockAddr, int iTimeout);
int MscCreateServerSockets(int iNumAddr, SYS_INET_ADDR const *pSvrAddr, int iFamily,
int iPortNo, int iListenSize, SYS_SOCKET *pSockFDs,
int &iNumSockFDs);
int MscGetMaxSockFD(SYS_SOCKET const *pSockFDs, int iNumSockFDs);
int MscAcceptServerConnection(SYS_SOCKET const *pSockFDs, int iNumSockFDs,
SYS_SOCKET *pConnSockFD, int &iNumConnSockFD, int iTimeout);
int MscLoadAddressFilter(char const *const *ppszFilter, int iNumTokens, AddressFilter &AF);
int MscAddressMatch(AddressFilter const &AF, SYS_INET_ADDR const &TestAddr);
int MscCheckAllowedIP(char const *pszMapFile, const SYS_INET_ADDR &PeerInfo, bool bDefault);
char **MscGetIPProperties(char const *pszFileName, const SYS_INET_ADDR *pPeerInfo);
int MscHostSubMatch(char const *pszHostName, char const *pszHostMatch);
char **MscGetHNProperties(char const *pszFileName, char const *pszHostName);
int MscMD5Authenticate(char const *pszPassword, char const *pszTimeStamp, char const *pszDigest);
char *MscExtractServerTimeStamp(char const *pszResponse, char *pszTimeStamp,
size_t sMaxTimeStamp);
int MscRootedName(char const *pszHostName);
int MscCramMD5(char const *pszSecret, char const *pszChallenge, char *pszDigest);
unsigned long MscHashString(char const *pszBuffer, size_t sLength,
unsigned long ulHashInit = HASH_INIT_VALUE);
int MscSplitAddressPort(char const *pszConnSpec, char *pszAddress, int &iPortNo, int iDefPortNo);
SYS_UINT16 MscReadUint16(void const *pData);
SYS_UINT32 MscReadUint32(void const *pData);
SYS_UINT64 MscReadUint64(void const *pData);
void *MscWriteUint16(void *pData, SYS_UINT16 uValue);
void *MscWriteUint32(void *pData, SYS_UINT32 uValue);
void *MscWriteUint64(void *pData, SYS_UINT64 uValue);
int MscCmdStringCheck(char const *pszString);
int MscGetSectionSize(FileSection const *pFS, SYS_OFF_T *pllSize);
int MscIsIPDomain(char const *pszDomain, char *pszIP, int iIPSize);
int MscReplaceTokens(char **ppszTokens, char *(*pLkupProc)(void *, char const *, size_t),
void *pPriv);
int MscGetAddrString(SYS_INET_ADDR const &AddrInfo, char *pszAStr, int iSize);
unsigned int MscServiceThread(void *pThreadData);
int MscSslEnvCB(void *pPrivate, int iID, void const *pData);
int MscParseOptions(char const *pszOpts, int (*pfAssign)(void *, char const *, char const *),
void *pPrivate);
void MscSysFreeCB(void *pPrivate, void *pData);
void MscRandomizeStringsOrder(char **ppszStrings);
unsigned long MscStringHashCB(void *pPrivate, HashDatum const *pDatum);
int MscStringCompareCB(void *pPrivate, HashDatum const *pDatum1,
HashDatum const *pDatum2);
#endif