NETCFG: fix leaks & oddities
[citadel.git] / citadel / netconfig.c
index 46d0d960d7df424a228be1e55fd1958f72b01a5b..52adbbddff79cdb60730b3893a4c740a42399562 100644 (file)
 #include <libcitadel.h>
 
 #include "include/ctdl_module.h"
+
+
+void vFreeRoomNetworkStruct(void *vOneRoomNetCfg);
+
 HashList *CfgTypeHash = NULL;
 HashList *RoomConfigs = NULL;
 /*-----------------------------------------------------------------------------*
@@ -114,6 +118,7 @@ void SerializeGeneric(const CfgLineType *ThisOne, StrBuf *OutputBuffer, OneRoomN
        int i;
 
        StrBufAppendBufPlain(OutputBuffer, CKEY(ThisOne->Str), 0);
+       StrBufAppendBufPlain(OutputBuffer, HKEY("|"), 0);
        for (i = 0; i < ThisOne->nSegments; i++)
        {
                StrBufAppendBuf(OutputBuffer, data->Value[i], 0);
@@ -127,7 +132,10 @@ void DeleteGenericCfgLine(const CfgLineType *ThisOne, RoomNetCfgLine **data)
 {
        int i;
 
-       for (i = 0; i < ThisOne->nSegments; i++)
+       if (*data == NULL)
+               return;
+
+       for (i = 0; i < (*data)->nValues; i++)
        {
                FreeStrBuf(&(*data)->Value[i]);
        }
@@ -171,12 +179,12 @@ int ReadRoomNetConfigFile(OneRoomNetCfg **pOneRNCfg, char *filename)
        memset(OneRNCfg, 0, sizeof(OneRoomNetCfg));
        *pOneRNCfg = OneRNCfg;
        Line = NewStrBuf();
+       InStr = NewStrBuf();
 
        while (StrBufTCP_read_line(Line, &fd, 0, &ErrStr) >= 0) {
                if (StrLength(Line) == 0)
                        continue;
                Pos = NULL;
-               InStr = NewStrBufPlain(NULL, StrLength(Line));
                StrBufExtract_NextToken(InStr, Line, &Pos, '|');
 
                pCfg = GetCfgTypeByStr(SKEY(InStr));
@@ -312,6 +320,34 @@ void SaveChangedConfigs(void)
                              NULL, 
                              maxRoomNetCfg);
 }
+
+
+void AddRoomCfgLine(OneRoomNetCfg *OneRNCfg, struct ctdlroom *qrbuf, RoomNetCfg LineType, RoomNetCfgLine *Line)
+{
+       int new = 0;
+       RoomNetCfgLine **pLine;
+       char filename[PATH_MAX];
+
+       if (OneRNCfg == NULL)
+       {
+               new = 1;
+               OneRNCfg = (OneRoomNetCfg*) malloc(sizeof(OneRoomNetCfg));
+               memset(OneRNCfg, 0, sizeof(OneRoomNetCfg));
+       }
+       pLine = &OneRNCfg->NetConfigs[LineType];
+
+       while(*pLine != NULL) pLine = &((*pLine)->next);
+       *pLine = Line;
+
+       assoc_file_name(filename, sizeof filename, qrbuf, ctdl_netcfg_dir);
+       SaveRoomNetConfigFile(OneRNCfg, filename);
+       OneRNCfg->changed = 0;
+       if (new)
+       {
+               Put(RoomConfigs, LKEY(qrbuf->QRnumber), OneRNCfg, vFreeRoomNetworkStruct);
+       }
+}
+
 void FreeRoomNetworkStructContent(OneRoomNetCfg *OneRNCfg)
 {
        RoomNetCfg eCfg;
@@ -324,11 +360,18 @@ void FreeRoomNetworkStructContent(OneRoomNetCfg *OneRNCfg)
                RoomNetCfgLine *pNext, *pName;
                
                pCfg = GetCfgTypeByEnum(eCfg, CfgIt);
-               pName= OneRNCfg->NetConfigs[pCfg->C];
+               pName= OneRNCfg->NetConfigs[eCfg];
                while (pName != NULL)
                {
                        pNext = pName->next;
-                       pCfg->DeAllocator(pCfg, &pName);
+                       if (pCfg != NULL)
+                       {
+                               pCfg->DeAllocator(pCfg, &pName);
+                       }
+                       else
+                       {
+                               DeleteGenericCfgLine(NULL, &pName);
+                       }
                        pName = pNext;
                }
        }