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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022
|
/*
* XMail by Davide Libenzi ( Intranet and Internet mail server )
* Copyright (C) 1999,..,2004 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 <davidel@xmailserver.org>
*
*/
#include "SysInclude.h"
#include "SysDep.h"
#include "SvrDefines.h"
#include "ShBlocks.h"
#include "ResLocks.h"
#include "StrUtils.h"
#include "SList.h"
#include "MD5.h"
#include "Base64Enc.h"
#include "BuffSock.h"
#include "MailConfig.h"
#include "UsrUtils.h"
#include "SvrUtils.h"
#include "AppDefines.h"
#include "MiscUtils.h"
#include "SMTPUtils.h"
#include "MessQueue.h"
#include "SMAILUtils.h"
#define QUEF_SHUTDOWN (1 << 0)
#define QUMF_DELETED (1 << 0)
#define QUMF_FREEZE (1 << 1)
#define QUE_MASK_TMPFLAGS(v) ((v) & ~(QUMF_DELETED | QUMF_FREEZE))
#define QUE_ARENA_SCAN_INTERVAL 15
#define QUE_ARENA_SCAN_WAIT 2
#define QUE_SCAN_THREAD_MAXWAIT 60
struct MessageQueue {
SysListHead ReadyQueue;
SysListHead RsndArenaQueue;
int iReadyCount;
int iRsndArenaCount;
SYS_MUTEX hMutex;
SYS_EVENT hReadyEvent;
char *pszRootPath;
int iMaxRetry;
int iRetryTimeout;
int iRetryIncrRatio;
int iNumDirsLevel;
unsigned long ulFlags;
SYS_THREAD hRsndScanThread;
};
struct QueueMessage {
SysListHead LLink;
int iLevel1;
int iLevel2;
char const *pszQueueDir;
char *pszFileName;
int iNumTries;
time_t tLastTry;
unsigned long ulFlags;
};
static int QueCreateStruct(char const *pszRootPath);
static int QueLoad(MessageQueue *pMQ);
static int QueLoadMessages(MessageQueue *pMQ, int iLevel1, int iLevel2);
static QueueMessage *QueAllocMessage(int iLevel1, int iLevel2, char const *pszQueueDir,
char const *pszFileName, int iNumTries, time_t tLastTry);
static int QueFreeMessage(QueueMessage *pQM);
static int QueFreeMessList(SysListHead *pHead);
static int QueLoadMessageStat(MessageQueue *pMQ, QueueMessage *pQM);
static int QueStatMessage(MessageQueue *pMQ, QueueMessage *pQM);
static int QueGetFilePath(MessageQueue *pMQ, QueueMessage *pQM, char *pszFilePath,
char const *pszQueueDir = NULL);
static int QueDoMessageCleanup(QUEUE_HANDLE hQueue, QMSG_HANDLE hMessage);
static int QueAddNew(MessageQueue *pMQ, QueueMessage *pQM);
static bool QueMessageExpired(MessageQueue *pMQ, QueueMessage *pQM);
static time_t QueNextRetryOp(int iNumTries, unsigned int uRetryTimeout,
unsigned int uRetryIncrRatio);
static bool QueMessageReadyToSend(MessageQueue *pMQ, QueueMessage *pQM);
static int QueAddRsnd(MessageQueue *pMQ, QueueMessage *pQM);
static unsigned int QueRsndThread(void *pThreadData);
static int QueScanRsndArena(MessageQueue *pMQ);
static bool QueMessageDestMatch(MessageQueue *pMQ, QueueMessage *pQM,
char const *pszAddressMatch);
QUEUE_HANDLE QueOpen(char const *pszRootPath, int iMaxRetry, int iRetryTimeout,
int iRetryIncrRatio, int iNumDirsLevel)
{
MessageQueue *pMQ = (MessageQueue *) SysAlloc(sizeof(MessageQueue));
if (pMQ == NULL)
return INVALID_QUEUE_HANDLE;
SYS_INIT_LIST_HEAD(&pMQ->ReadyQueue);
SYS_INIT_LIST_HEAD(&pMQ->RsndArenaQueue);
pMQ->iReadyCount = 0;
pMQ->iRsndArenaCount = 0;
pMQ->iMaxRetry = iMaxRetry;
pMQ->iRetryTimeout = iRetryTimeout;
pMQ->iRetryIncrRatio = iRetryIncrRatio;
pMQ->iNumDirsLevel = iNumDirsLevel;
pMQ->ulFlags = 0;
if ((pMQ->hMutex = SysCreateMutex()) == SYS_INVALID_MUTEX) {
SysFree(pMQ);
return INVALID_QUEUE_HANDLE;
}
if ((pMQ->hReadyEvent = SysCreateEvent(1)) == SYS_INVALID_EVENT) {
SysCloseMutex(pMQ->hMutex);
SysFree(pMQ);
return INVALID_QUEUE_HANDLE;
}
/* Set the queue root path */
char szRootPath[SYS_MAX_PATH] = "";
StrSNCpy(szRootPath, pszRootPath);
AppendSlash(szRootPath);
pMQ->pszRootPath = SysStrDup(szRootPath);
/* Load queue status */
if (QueLoad(pMQ) < 0) {
ErrorPush();
SysFree(pMQ->pszRootPath);
SysCloseEvent(pMQ->hReadyEvent);
SysCloseMutex(pMQ->hMutex);
SysFree(pMQ);
ErrSetErrorCode(ErrorPop());
return INVALID_QUEUE_HANDLE;
}
/* Start rsnd arena scan thread */
if ((pMQ->hRsndScanThread = SysCreateThread(QueRsndThread, pMQ)) == SYS_INVALID_THREAD) {
ErrorPush();
QueFreeMessList(&pMQ->ReadyQueue);
QueFreeMessList(&pMQ->RsndArenaQueue);
SysFree(pMQ->pszRootPath);
SysCloseEvent(pMQ->hReadyEvent);
SysCloseMutex(pMQ->hMutex);
SysFree(pMQ);
ErrSetErrorCode(ErrorPop());
return INVALID_QUEUE_HANDLE;
}
return (QUEUE_HANDLE) pMQ;
}
int QueClose(QUEUE_HANDLE hQueue)
{
MessageQueue *pMQ = (MessageQueue *) hQueue;
/* Set the shutdown flag and wait for rsnd scan thread to terminate */
pMQ->ulFlags |= QUEF_SHUTDOWN;
SysWaitThread(pMQ->hRsndScanThread, QUE_SCAN_THREAD_MAXWAIT);
SysCloseThread(pMQ->hRsndScanThread, 1);
/* Clear queues */
QueFreeMessList(&pMQ->ReadyQueue);
QueFreeMessList(&pMQ->RsndArenaQueue);
SysCloseEvent(pMQ->hReadyEvent);
SysCloseMutex(pMQ->hMutex);
SysFree(pMQ->pszRootPath);
SysFree(pMQ);
return 0;
}
int QueGetDirsLevel(QUEUE_HANDLE hQueue)
{
MessageQueue *pMQ = (MessageQueue *) hQueue;
return pMQ->iNumDirsLevel;
}
char const *QueGetRootPath(QUEUE_HANDLE hQueue)
{
MessageQueue *pMQ = (MessageQueue *) hQueue;
return pMQ->pszRootPath;
}
static int QueCreateStruct(char const *pszRootPath)
{
/* Create message dir ( new messages queue ) */
char szDirPath[SYS_MAX_PATH] = "";
StrSNCpy(szDirPath, pszRootPath);
AppendSlash(szDirPath);
StrSNCat(szDirPath, QUEUE_MESS_DIR);
if (!SysExistDir(szDirPath) && SysMakeDir(szDirPath) < 0)
return ErrGetErrorCode();
/* Create message resend dir ( resend messages queue ) */
StrSNCpy(szDirPath, pszRootPath);
AppendSlash(szDirPath);
StrSNCat(szDirPath, QUEUE_RSND_DIR);
if (!SysExistDir(szDirPath) && SysMakeDir(szDirPath) < 0)
return ErrGetErrorCode();
/* Create info dir */
StrSNCpy(szDirPath, pszRootPath);
AppendSlash(szDirPath);
StrSNCat(szDirPath, QUEUE_INFO_DIR);
if (!SysExistDir(szDirPath) && SysMakeDir(szDirPath) < 0)
return ErrGetErrorCode();
/* Create temp dir */
StrSNCpy(szDirPath, pszRootPath);
AppendSlash(szDirPath);
StrSNCat(szDirPath, QUEUE_TEMP_DIR);
if (!SysExistDir(szDirPath) && SysMakeDir(szDirPath) < 0)
return ErrGetErrorCode();
/* Create send log dir */
StrSNCpy(szDirPath, pszRootPath);
AppendSlash(szDirPath);
StrSNCat(szDirPath, QUEUE_SLOG_DIR);
if (!SysExistDir(szDirPath) && SysMakeDir(szDirPath) < 0)
return ErrGetErrorCode();
/* Create custom message processing dir */
StrSNCpy(szDirPath, pszRootPath);
AppendSlash(szDirPath);
StrSNCat(szDirPath, QUEUE_CUST_DIR);
if (!SysExistDir(szDirPath) && SysMakeDir(szDirPath) < 0)
return ErrGetErrorCode();
/* Create user custom message processing dir ( mailproc.tab cache ) */
StrSNCpy(szDirPath, pszRootPath);
AppendSlash(szDirPath);
StrSNCat(szDirPath, QUEUE_MPRC_DIR);
if (!SysExistDir(szDirPath) && SysMakeDir(szDirPath) < 0)
return ErrGetErrorCode();
/* Create frozen dir */
StrSNCpy(szDirPath, pszRootPath);
AppendSlash(szDirPath);
StrSNCat(szDirPath, QUEUE_FROZ_DIR);
if (!SysExistDir(szDirPath) && SysMakeDir(szDirPath) < 0)
return ErrGetErrorCode();
return 0;
}
static int QueLoad(MessageQueue *pMQ)
{
char szCurrPath[SYS_MAX_PATH] = "";
for (int i = 0; i < pMQ->iNumDirsLevel; i++) {
SysSNPrintf(szCurrPath, sizeof(szCurrPath) - 1, "%s%d", pMQ->pszRootPath, i);
if (!SysExistDir(szCurrPath) && (SysMakeDir(szCurrPath) < 0))
return ErrGetErrorCode();
for (int j = 0; j < pMQ->iNumDirsLevel; j++) {
SysSNPrintf(szCurrPath, sizeof(szCurrPath) - 1, "%s%d%s%d",
pMQ->pszRootPath, i, SYS_SLASH_STR, j);
if (!SysExistDir(szCurrPath) && (SysMakeDir(szCurrPath) < 0))
return ErrGetErrorCode();
if (QueCreateStruct(szCurrPath) < 0 ||
QueLoadMessages(pMQ, i, j) < 0)
return ErrGetErrorCode();
}
}
return 0;
}
static int QueLoadMessages(MessageQueue *pMQ, int iLevel1, int iLevel2)
{
/* File scan the new messages dir */
char szDirPath[SYS_MAX_PATH] = "";
SysSNPrintf(szDirPath, sizeof(szDirPath) - 1, "%s%d%s%d%s%s",
pMQ->pszRootPath, iLevel1, SYS_SLASH_STR, iLevel2,
SYS_SLASH_STR, QUEUE_MESS_DIR);
char szMsgFileName[SYS_MAX_PATH] = "";
FSCAN_HANDLE hFileScan = MscFirstFile(szDirPath, 0, szMsgFileName,
sizeof(szMsgFileName));
if (hFileScan != INVALID_FSCAN_HANDLE) {
do {
if (!IsDotFilename(szMsgFileName)) {
QueueMessage *pQM =
QueAllocMessage(iLevel1, iLevel2, QUEUE_MESS_DIR,
szMsgFileName, 0, 0);
if (pQM != NULL) {
/* Add the file to the message queue */
SYS_LIST_ADDT(&pQM->LLink, &pMQ->ReadyQueue);
++pMQ->iReadyCount;
}
}
} while (MscNextFile(hFileScan, szMsgFileName, sizeof(szMsgFileName)));
MscCloseFindFile(hFileScan);
/* Set the mess event if the queue is not empty */
if (pMQ->iReadyCount > 0)
SysSetEvent(pMQ->hReadyEvent);
}
/* File scan the resend messages dir */
SysSNPrintf(szDirPath, sizeof(szDirPath) - 1, "%s%d%s%d%s%s",
pMQ->pszRootPath, iLevel1, SYS_SLASH_STR, iLevel2,
SYS_SLASH_STR, QUEUE_RSND_DIR);
if ((hFileScan = MscFirstFile(szDirPath, 0, szMsgFileName,
sizeof(szMsgFileName))) != INVALID_FSCAN_HANDLE) {
do {
if (!IsDotFilename(szMsgFileName)) {
QueueMessage *pQM = QueAllocMessage(iLevel1, iLevel2,
QUEUE_RSND_DIR,
szMsgFileName, 0, 0);
if (pQM != NULL) {
/* Load message statistics */
if (QueLoadMessageStat(pMQ, pQM) < 0) {
SysLogMessage(LOG_LEV_ERROR,
"Error loading queue file: '%s%d%s%d%s%s%s%s'\n",
pMQ->pszRootPath, iLevel1,
SYS_SLASH_STR, iLevel2,
SYS_SLASH_STR, QUEUE_RSND_DIR,
SYS_SLASH_STR, szMsgFileName);
QueFreeMessage(pQM);
} else {
/* Add the file to the resend queue */
SYS_LIST_ADDT(&pQM->LLink, &pMQ->RsndArenaQueue);
++pMQ->iRsndArenaCount;
}
}
}
} while (MscNextFile(hFileScan, szMsgFileName, sizeof(szMsgFileName)));
MscCloseFindFile(hFileScan);
}
return 0;
}
static QueueMessage *QueAllocMessage(int iLevel1, int iLevel2, char const *pszQueueDir,
char const *pszFileName, int iNumTries, time_t tLastTry)
{
QueueMessage *pQM = (QueueMessage *) SysAlloc(sizeof(QueueMessage));
if (pQM == NULL)
return NULL;
SYS_INIT_LIST_LINK(&pQM->LLink);
pQM->iLevel1 = iLevel1;
pQM->iLevel2 = iLevel2;
pQM->pszQueueDir = pszQueueDir;
pQM->pszFileName = SysStrDup(pszFileName);
pQM->iNumTries = iNumTries;
pQM->tLastTry = tLastTry;
pQM->ulFlags = 0;
return pQM;
}
static int QueFreeMessage(QueueMessage *pQM)
{
SysFree(pQM->pszFileName);
SysFree(pQM);
return 0;
}
static int QueFreeMessList(SysListHead *pHead)
{
SysListHead *pLLink;
while ((pLLink = SYS_LIST_FIRST(pHead)) != NULL) {
QueueMessage *pQM = SYS_LIST_ENTRY(pLLink, QueueMessage, LLink);
SYS_LIST_DEL(pLLink);
QueFreeMessage(pQM);
}
return 0;
}
char *QueLoadLastLogEntry(char const *pszLogFilePath)
{
FILE *pLogFile = fopen(pszLogFilePath, "rb");
if (pLogFile == NULL) {
ErrSetErrorCode(ERR_FILE_OPEN, pszLogFilePath);
return NULL;
}
/* Walk log entries */
unsigned long ulCurrOffset = 0, ulBaseOffset = (unsigned long) -1;
unsigned long ulEndOffset, ulPeekTime;
char szLogLine[1024] = "";
for (;;) {
ulCurrOffset = (unsigned long) ftell(pLogFile);
if (MscFGets(szLogLine, sizeof(szLogLine) - 1, pLogFile) == NULL)
break;
if (sscanf(szLogLine, "[PeekTime] %lu", &ulPeekTime) == 1)
ulBaseOffset = ulCurrOffset;
}
if (ulBaseOffset == (unsigned long) -1) {
fclose(pLogFile);
ErrSetErrorCode(ERR_EMPTY_LOG, pszLogFilePath);
return NULL;
}
/* Get end offset ( end of file ) */
fseek(pLogFile, 0, SEEK_END);
ulEndOffset = (unsigned long) ftell(pLogFile);
/* Load last entry */
unsigned int uEntrySize = (unsigned int) (ulEndOffset - ulBaseOffset);
char *pszEntry = (char *) SysAlloc(uEntrySize + 1);
if (pszEntry == NULL) {
fclose(pLogFile);
return NULL;
}
fseek(pLogFile, ulBaseOffset, SEEK_SET);
if (!fread(pszEntry, uEntrySize, 1, pLogFile)) {
SysFree(pszEntry);
fclose(pLogFile);
ErrSetErrorCode(ERR_FILE_READ, pszLogFilePath);
return NULL;
}
pszEntry[uEntrySize] = '\0';
fclose(pLogFile);
return pszEntry;
}
static int QueLoadMessageStat(MessageQueue *pMQ, QueueMessage *pQM)
{
/* Build the slog file path */
char szSlogFilePath[SYS_MAX_PATH] = "";
QueGetFilePath(pMQ, pQM, szSlogFilePath, QUEUE_SLOG_DIR);
/* Try to load message statistics */
FILE *pLogFile = fopen(szSlogFilePath, "rt");
if (pLogFile != NULL) {
int iNumTries = 0;
unsigned long ulLastTime = 0;
unsigned long ulPeekTime;
char szLogLine[1024] = "";
while (MscFGets(szLogLine, sizeof(szLogLine) - 1, pLogFile) != NULL)
if (sscanf(szLogLine, "[PeekTime] %lu", &ulPeekTime) == 1)
++iNumTries, ulLastTime = ulPeekTime;
fclose(pLogFile);
pQM->iNumTries = iNumTries;
pQM->tLastTry = (time_t) ulLastTime;
}
return 0;
}
static int QueStatMessage(MessageQueue *pMQ, QueueMessage *pQM)
{
/* Build the slog file path */
char szSlogFilePath[SYS_MAX_PATH] = "";
QueGetFilePath(pMQ, pQM, szSlogFilePath, QUEUE_SLOG_DIR);
FILE *pLogFile = fopen(szSlogFilePath, "a+t");
if (pLogFile == NULL) {
ErrSetErrorCode(ERR_FILE_OPEN, szSlogFilePath);
return ERR_FILE_OPEN;
}
/* Dump peek time */
time_t tCurr = time(NULL);
char szTime[128] = "";
MscGetTimeStr(szTime, sizeof(szTime) - 1, tCurr);
fprintf(pLogFile, "[PeekTime] %lu : %s\n", (unsigned long) tCurr, szTime);
fclose(pLogFile);
return 0;
}
QMSG_HANDLE QueCreateMessage(QUEUE_HANDLE hQueue)
{
MessageQueue *pMQ = (MessageQueue *) hQueue;
/* Initialize random number generator */
SRand();
/* Build message file path */
int iLevel1 = rand() % pMQ->iNumDirsLevel;
int iLevel2 = rand() % pMQ->iNumDirsLevel;
char szSubPath[SYS_MAX_PATH] = "";
char szMsgFilePath[SYS_MAX_PATH] = "";
SysSNPrintf(szSubPath, sizeof(szSubPath) - 1, "%s%d%s%d%s%s",
pMQ->pszRootPath, iLevel1, SYS_SLASH_STR,
iLevel2, SYS_SLASH_STR, QUEUE_TEMP_DIR);
if (MscUniqueFile(szSubPath, szMsgFilePath) < 0)
return INVALID_QMSG_HANDLE;
/* Extract file name */
char szMsgFileName[SYS_MAX_PATH] = "";
MscGetFileName(szMsgFilePath, szMsgFileName);
/* Create queue message data */
QueueMessage *pQM = QueAllocMessage(iLevel1, iLevel2, QUEUE_TEMP_DIR,
szMsgFileName, 0, 0);
if (pQM == NULL)
return INVALID_QMSG_HANDLE;
return (QMSG_HANDLE) pQM;
}
static int QueGetFilePath(MessageQueue *pMQ, QueueMessage *pQM, char *pszFilePath,
char const *pszQueueDir)
{
if (pszQueueDir == NULL)
pszQueueDir = pQM->pszQueueDir;
SysSNPrintf(pszFilePath, SYS_MAX_PATH - 1, "%s%d%s%d%s%s%s%s",
pMQ->pszRootPath, pQM->iLevel1, SYS_SLASH_STR,
pQM->iLevel2, SYS_SLASH_STR, pszQueueDir, SYS_SLASH_STR, pQM->pszFileName);
return 0;
}
int QueGetFilePath(QUEUE_HANDLE hQueue, QMSG_HANDLE hMessage, char *pszFilePath,
char const *pszQueueDir)
{
MessageQueue *pMQ = (MessageQueue *) hQueue;
QueueMessage *pQM = (QueueMessage *) hMessage;
return QueGetFilePath(pMQ, pQM, pszFilePath, pszQueueDir);
}
int QueCloseMessage(QUEUE_HANDLE hQueue, QMSG_HANDLE hMessage)
{
MessageQueue *pMQ = (MessageQueue *) hQueue;
QueueMessage *pQM = (QueueMessage *) hMessage;
if (pQM->ulFlags & QUMF_DELETED)
QueDoMessageCleanup(hQueue, hMessage);
QueFreeMessage(pQM);
return 0;
}
QMSG_HANDLE QueGetHandle(QUEUE_HANDLE hQueue, int iLevel1, int iLevel2, char const *pszQueueDir,
char const *pszFileName)
{
MessageQueue *pMQ = (MessageQueue *) hQueue;
QueueMessage *pQM = QueAllocMessage(iLevel1, iLevel2, pszQueueDir, pszFileName, 0, 0);
if (pQM == NULL)
return INVALID_QMSG_HANDLE;
/* Load message statistics */
QueLoadMessageStat(pMQ, pQM);
return (QMSG_HANDLE) pQM;
}
char const *QueGetFileName(QMSG_HANDLE hMessage)
{
QueueMessage *pQM = (QueueMessage *) hMessage;
return pQM->pszFileName;
}
char const *QueGetQueueDir(QMSG_HANDLE hMessage)
{
QueueMessage *pQM = (QueueMessage *) hMessage;
return pQM->pszQueueDir;
}
int QueGetLevel1(QMSG_HANDLE hMessage)
{
QueueMessage *pQM = (QueueMessage *) hMessage;
return pQM->iLevel1;
}
int QueGetLevel2(QMSG_HANDLE hMessage)
{
QueueMessage *pQM = (QueueMessage *) hMessage;
return pQM->iLevel2;
}
int QueGetTryCount(QMSG_HANDLE hMessage)
{
QueueMessage *pQM = (QueueMessage *) hMessage;
return pQM->iNumTries;
}
time_t QueGetLastTryTime(QMSG_HANDLE hMessage)
{
QueueMessage *pQM = (QueueMessage *) hMessage;
return pQM->tLastTry;
}
time_t QueGetMessageNextOp(QUEUE_HANDLE hQueue, QMSG_HANDLE hMessage)
{
MessageQueue *pMQ = (MessageQueue *) hQueue;
QueueMessage *pQM = (QueueMessage *) hMessage;
return pQM->tLastTry + QueNextRetryOp(pQM->iNumTries, (unsigned int) pMQ->iRetryTimeout,
(unsigned int) pMQ->iRetryIncrRatio);
}
int QueInitMessageStats(QUEUE_HANDLE hQueue, QMSG_HANDLE hMessage)
{
MessageQueue *pMQ = (MessageQueue *) hQueue;
QueueMessage *pQM = (QueueMessage *) hMessage;
char szQueueFilePath[SYS_MAX_PATH] = "";
QueGetFilePath(pMQ, pQM, szQueueFilePath, QUEUE_SLOG_DIR);
SysRemove(szQueueFilePath);
/* Init message statistics */
pQM->iNumTries = 0;
pQM->tLastTry = 0;
return 0;
}
static int QueDoMessageCleanup(QUEUE_HANDLE hQueue, QMSG_HANDLE hMessage)
{
MessageQueue *pMQ = (MessageQueue *) hQueue;
QueueMessage *pQM = (QueueMessage *) hMessage;
char szQueueFilePath[SYS_MAX_PATH] = "";
if (pQM->ulFlags & QUMF_FREEZE) {
/* Move message file */
char szTargetFile[SYS_MAX_PATH] = "";
QueGetFilePath(pMQ, pQM, szQueueFilePath);
QueGetFilePath(pMQ, pQM, szTargetFile, QUEUE_FROZ_DIR);
if (SysMoveFile(szQueueFilePath, szTargetFile) < 0)
return ErrGetErrorCode();
/* Change message location */
pQM->pszQueueDir = QUEUE_FROZ_DIR;
} else {
/* Clean message file */
QueGetFilePath(pMQ, pQM, szQueueFilePath);
SysRemove(szQueueFilePath);
/* Clean 'info' file */
QueGetFilePath(pMQ, pQM, szQueueFilePath, QUEUE_INFO_DIR);
SysRemove(szQueueFilePath);
/* Clean 'slog' file */
QueGetFilePath(pMQ, pQM, szQueueFilePath, QUEUE_SLOG_DIR);
SysRemove(szQueueFilePath);
}
/* Clean 'temp' file */
QueGetFilePath(pMQ, pQM, szQueueFilePath, QUEUE_TEMP_DIR);
SysRemove(szQueueFilePath);
/* Clean 'cust' file */
QueGetFilePath(pMQ, pQM, szQueueFilePath, QUEUE_CUST_DIR);
SysRemove(szQueueFilePath);
/* Clean 'mprc' file */
QueGetFilePath(pMQ, pQM, szQueueFilePath, QUEUE_MPRC_DIR);
SysRemove(szQueueFilePath);
return 0;
}
int QueCleanupMessage(QUEUE_HANDLE hQueue, QMSG_HANDLE hMessage, bool bFreeze)
{
MessageQueue *pMQ = (MessageQueue *) hQueue;
QueueMessage *pQM = (QueueMessage *) hMessage;
pQM->ulFlags |= QUMF_DELETED;
if (bFreeze)
pQM->ulFlags |= QUMF_FREEZE;
return 0;
}
int QueCommitMessage(QUEUE_HANDLE hQueue, QMSG_HANDLE hMessage)
{
MessageQueue *pMQ = (MessageQueue *) hQueue;
QueueMessage *pQM = (QueueMessage *) hMessage;
/* Move message file ( if not in mess ) */
if (strcmp(pQM->pszQueueDir, QUEUE_MESS_DIR) != 0) {
char szSourceFile[SYS_MAX_PATH] = "";
char szTargetFile[SYS_MAX_PATH] = "";
QueGetFilePath(pMQ, pQM, szSourceFile);
QueGetFilePath(pMQ, pQM, szTargetFile, QUEUE_MESS_DIR);
if (SysMoveFile(szSourceFile, szTargetFile) < 0)
return ErrGetErrorCode();
/* Change message location */
pQM->pszQueueDir = QUEUE_MESS_DIR;
}
/* Unmask temporary flags */
pQM->ulFlags = QUE_MASK_TMPFLAGS(pQM->ulFlags);
/* Add to queue */
if (QueAddNew(pMQ, pQM) < 0)
return ErrGetErrorCode();
return 0;
}
static int QueAddNew(MessageQueue *pMQ, QueueMessage *pQM)
{
/* Add the queue entry */
if (SysLockMutex(pMQ->hMutex, SYS_INFINITE_TIMEOUT) < 0)
return ErrGetErrorCode();
SYS_LIST_ADDT(&pQM->LLink, &pMQ->ReadyQueue);
++pMQ->iReadyCount;
SysSetEvent(pMQ->hReadyEvent);
SysUnlockMutex(pMQ->hMutex);
return 0;
}
static bool QueMessageExpired(MessageQueue *pMQ, QueueMessage *pQM)
{
return pQM->iNumTries >= pMQ->iMaxRetry;
}
static time_t QueNextRetryOp(int iNumTries, unsigned int uRetryTimeout,
unsigned int uRetryIncrRatio)
{
unsigned int uNextOp = uRetryTimeout;
if (uRetryIncrRatio != 0)
for (int i = 1; i < iNumTries; i++)
uNextOp += uNextOp / uRetryIncrRatio;
return (time_t) uNextOp;
}
static bool QueMessageReadyToSend(MessageQueue *pMQ, QueueMessage *pQM)
{
return (time(NULL) > (pQM->tLastTry +
QueNextRetryOp(pQM->iNumTries, (unsigned int) pMQ->iRetryTimeout,
(unsigned int) pMQ->iRetryIncrRatio)));
}
int QueResendMessage(QUEUE_HANDLE hQueue, QMSG_HANDLE hMessage)
{
MessageQueue *pMQ = (MessageQueue *) hQueue;
QueueMessage *pQM = (QueueMessage *) hMessage;
/* Check for message expired */
if (QueMessageExpired(pMQ, pQM)) {
ErrSetErrorCode(ERR_SPOOL_FILE_EXPIRED);
return ERR_SPOOL_FILE_EXPIRED;
}
/* Move message file ( if not in rsnd dir ) */
if (strcmp(pQM->pszQueueDir, QUEUE_RSND_DIR) != 0) {
char szSourceFile[SYS_MAX_PATH] = "";
char szTargetFile[SYS_MAX_PATH] = "";
QueGetFilePath(pMQ, pQM, szSourceFile);
QueGetFilePath(pMQ, pQM, szTargetFile, QUEUE_RSND_DIR);
if (SysMoveFile(szSourceFile, szTargetFile) < 0)
return ErrGetErrorCode();
/* Change message location */
pQM->pszQueueDir = QUEUE_RSND_DIR;
}
/* Unmask temporary flags */
pQM->ulFlags = QUE_MASK_TMPFLAGS(pQM->ulFlags);
/* Add to queue */
if (QueAddRsnd(pMQ, pQM) < 0)
return ErrGetErrorCode();
return 0;
}
static int QueAddRsnd(MessageQueue *pMQ, QueueMessage *pQM)
{
/* Add the queue entry */
if (SysLockMutex(pMQ->hMutex, SYS_INFINITE_TIMEOUT) < 0)
return ErrGetErrorCode();
SYS_LIST_ADDT(&pQM->LLink, &pMQ->RsndArenaQueue);
++pMQ->iRsndArenaCount;
SysUnlockMutex(pMQ->hMutex);
return 0;
}
QMSG_HANDLE QueExtractMessage(QUEUE_HANDLE hQueue, int iTimeout)
{
MessageQueue *pMQ = (MessageQueue *) hQueue;
/* Wait for message available */
if (SysWaitEvent(pMQ->hReadyEvent, iTimeout) < 0)
return INVALID_QMSG_HANDLE;
if (SysLockMutex(pMQ->hMutex, SYS_INFINITE_TIMEOUT) < 0)
return INVALID_QMSG_HANDLE;
/* Get the first message of the queue */
SysListHead *pLLink = SYS_LIST_FIRST(&pMQ->ReadyQueue);
if (pLLink == NULL) {
SysUnlockMutex(pMQ->hMutex);
return INVALID_QMSG_HANDLE;
}
/* Remove the message from the list */
SYS_LIST_DEL(pLLink);
/* Decrement message count by resetting the event if no more messages are in */
if (--pMQ->iReadyCount == 0)
SysResetEvent(pMQ->hReadyEvent);
SysUnlockMutex(pMQ->hMutex);
/* Get queue message pointer */
QueueMessage *pQM = SYS_LIST_ENTRY(pLLink, QueueMessage, LLink);
/* Update message statistics */
++pQM->iNumTries;
pQM->tLastTry = time(NULL);
/* Update log file */
QueStatMessage(pMQ, pQM);
return (QMSG_HANDLE) pQM;
}
static unsigned int QueRsndThread(void *pThreadData)
{
MessageQueue *pMQ = (MessageQueue *) pThreadData;
int iElapsedTime = 0;
while ((pMQ->ulFlags & QUEF_SHUTDOWN) == 0) {
SysSleep(QUE_ARENA_SCAN_WAIT);
iElapsedTime += QUE_ARENA_SCAN_WAIT;
if (iElapsedTime > QUE_ARENA_SCAN_INTERVAL) {
iElapsedTime = 0;
/* Scan rsnd arena to prepare messages to resend */
QueScanRsndArena(pMQ);
}
}
pMQ->ulFlags &= ~QUEF_SHUTDOWN;
return 0;
}
static int QueScanRsndArena(MessageQueue *pMQ)
{
if (SysLockMutex(pMQ->hMutex, SYS_INFINITE_TIMEOUT) < 0)
return ErrGetErrorCode();
SysListHead *pLLink;
SYS_LIST_FOR_EACH(pLLink, &pMQ->RsndArenaQueue) {
QueueMessage *pQM = SYS_LIST_ENTRY(pLLink, QueueMessage, LLink);
if (QueMessageReadyToSend(pMQ, pQM)) {
/* Set the list pointer to the next item */
pLLink = pLLink->pPrev;
/* Remove item from resend arena */
SYS_LIST_DEL(&pQM->LLink);
--pMQ->iRsndArenaCount;
/* Add item from resend queue */
SYS_LIST_ADDT(&pQM->LLink, &pMQ->ReadyQueue);
++pMQ->iReadyCount;
}
}
/* If the count of rsnd queue is not zero, set the event */
if (pMQ->iReadyCount > 0)
SysSetEvent(pMQ->hReadyEvent);
SysUnlockMutex(pMQ->hMutex);
return 0;
}
int QueCheckMessage(QUEUE_HANDLE hQueue, QMSG_HANDLE hMessage)
{
MessageQueue *pMQ = (MessageQueue *) hQueue;
QueueMessage *pQM = (QueueMessage *) hMessage;
char szQueueFilePath[SYS_MAX_PATH] = "";
if (pQM->ulFlags & QUMF_DELETED) {
ErrSetErrorCode(ERR_MESSAGE_DELETED);
return ERR_MESSAGE_DELETED;
}
QueGetFilePath(hQueue, hMessage, szQueueFilePath, QUEUE_MESS_DIR);
if (!SysExistFile(szQueueFilePath)) {
QueGetFilePath(hQueue, hMessage, szQueueFilePath, QUEUE_RSND_DIR);
if (!SysExistFile(szQueueFilePath)) {
ErrSetErrorCode(ERR_NO_MESSAGE_FILE);
return ERR_NO_MESSAGE_FILE;
}
}
return 0;
}
static bool QueMessageDestMatch(MessageQueue *pMQ, QueueMessage *pQM,
char const *pszAddressMatch)
{
SpoolFileHeader SFH;
char szQueueFilePath[SYS_MAX_PATH] = "";
QueGetFilePath(pMQ, pQM, szQueueFilePath);
if (USmlLoadSpoolFileHeader(szQueueFilePath, SFH) < 0)
return false;
bool bAddressMatch = false;
if (strchr(pszAddressMatch, '@') == NULL) {
/* RFC style ETRN ( domain based ) */
char szDestUser[MAX_ADDR_NAME] = "";
char szDestDomain[MAX_ADDR_NAME] = "";
if (StrStringsCount(SFH.ppszRcpt) < 1 ||
USmtpSplitEmailAddr(SFH.ppszRcpt[0], szDestUser, szDestDomain) < 0) {
USmlCleanupSpoolFileHeader(SFH);
return false;
}
bAddressMatch = (StrIWildMatch(szDestDomain, pszAddressMatch) != 0);
} else {
/* XMail style ETRN ( email based ) */
bAddressMatch = (StrIWildMatch(SFH.ppszRcpt[0], pszAddressMatch) != 0);
}
USmlCleanupSpoolFileHeader(SFH);
return bAddressMatch;
}
int QueFlushRsndArena(QUEUE_HANDLE hQueue, char const *pszAddressMatch)
{
MessageQueue *pMQ = (MessageQueue *) hQueue;
if (SysLockMutex(pMQ->hMutex, SYS_INFINITE_TIMEOUT) < 0)
return ErrGetErrorCode();
SysListHead *pLLink;
SYS_LIST_FOR_EACH(pLLink, &pMQ->RsndArenaQueue) {
QueueMessage *pQM = SYS_LIST_ENTRY(pLLink, QueueMessage, LLink);
if (pszAddressMatch == NULL ||
QueMessageDestMatch(pMQ, pQM, pszAddressMatch)) {
/* Set the list pointer to the next item */
pLLink = pLLink->pPrev;
/* Remove item from resend arena */
SYS_LIST_DEL(&pQM->LLink);
--pMQ->iRsndArenaCount;
/* Add item from resend queue */
SYS_LIST_ADDT(&pQM->LLink, &pMQ->ReadyQueue);
++pMQ->iReadyCount;
}
}
/* If the count of rsnd queue is not zero, set the event */
if (pMQ->iReadyCount > 0)
SysSetEvent(pMQ->hReadyEvent);
SysUnlockMutex(pMQ->hMutex);
return 0;
}
|