5831d4a7db4117a0a82f8873c08b5a21edf31b07
[citadel.git] / citadel / nttlist.c
1 /*
2  * This module handles shared rooms, inter-Citadel mail, and outbound
3  * mailing list processing.
4  *
5  * Copyright (c) 2000-2012 by the citadel.org team
6  *
7  *  This program is open source software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License, version 3.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  */
16 #include "sysdep.h"
17 #include <stdio.h>
18 #include <libcitadel.h>
19 #include "ctdl_module.h"
20 #include "serv_extensions.h"
21
22 /*-----------------------------------------------------------------------------*
23  *                 Network maps: evaluate other nodes                          *
24  *-----------------------------------------------------------------------------*/
25 int NTTDebugEnabled = 0;
26
27 /*
28  * network_talking_to()  --  concurrency checker
29  */
30 static HashList *nttlist = NULL;
31 int CtdlNetworkTalkingTo(const char *nodename, long len, int operation)
32 {
33
34         int retval = 0;
35         HashPos *Pos = NULL;
36         void *vdata;
37
38         begin_critical_section(S_NTTLIST);
39
40         switch(operation) {
41
42                 case NTT_ADD:
43                         if (nttlist == NULL) 
44                                 nttlist = NewHash(1, NULL);
45                         Put(nttlist, nodename, len, NewStrBufPlain(nodename, len), HFreeStrBuf);
46                         if (NTTDebugEnabled) syslog(LOG_DEBUG, "nttlist: added <%s>\n", nodename);
47                         break;
48                 case NTT_REMOVE:
49                         if ((nttlist == NULL) ||
50                             (GetCount(nttlist) == 0))
51                                 break;
52                         Pos = GetNewHashPos(nttlist, 1);
53                         if (GetHashPosFromKey (nttlist, nodename, len, Pos))
54                                 DeleteEntryFromHash(nttlist, Pos);
55                         DeleteHashPos(&Pos);
56                         if (NTTDebugEnabled) syslog(LOG_DEBUG, "nttlist: removed <%s>\n", nodename);
57
58                         break;
59
60                 case NTT_CHECK:
61                         if ((nttlist == NULL) ||
62                             (GetCount(nttlist) == 0))
63                                 break;
64                         if (GetHash(nttlist, nodename, len, &vdata))
65                                 retval ++;
66                         if (NTTDebugEnabled) syslog(LOG_DEBUG, "nttlist: have [%d] <%s>\n", retval, nodename);
67                         break;
68         }
69
70         end_critical_section(S_NTTLIST);
71         return(retval);
72 }
73
74 void cleanup_nttlist(void)
75 {
76         begin_critical_section(S_NTTLIST);
77         DeleteHash(&nttlist);
78         end_critical_section(S_NTTLIST);
79 }
80
81
82
83 /*
84  * Module entry point
85  */
86 void SetNTTDebugEnabled(const int n)
87 {
88         NTTDebugEnabled = n;
89 }
90
91
92
93 /*
94  * Module entry point
95  */
96 CTDL_MODULE_INIT(nttlist)
97 {
98         if (!threading)
99         {
100                 CtdlRegisterDebugFlagHook(HKEY("networktalkingto"), SetNTTDebugEnabled, &NTTDebugEnabled);
101
102                 CtdlRegisterCleanupHook(cleanup_nttlist);
103         }
104         return "nttlist";
105 }