interprocess_buffer.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045 #include <net-snmp/net-snmp-config.h>
00046 #include <net-snmp/net-snmp-includes.h>
00047 #include <net-snmp/agent/net-snmp-agent-includes.h>
00048
00049 #include "interprocess_buffer.h"
00050 #include "openserSIPContactTable.h"
00051 #include "openserSIPRegUserTable.h"
00052 #include "hashTable.h"
00053 #include "utilities.h"
00054
00055 #include "../usrloc/ul_callback.h"
00056
00057
00058
00059
00060
00061
00062
00063
00064 hashSlot_t *hashTable;
00065
00066
00067 interprocessBuffer_t *frontRegUserTableBuffer;
00068 interprocessBuffer_t *endRegUserTableBuffer;
00069
00070
00071
00072 gen_lock_t *interprocessCBLock;
00073
00074
00075
00076
00077
00078
00079 static void executeInterprocessBufferCmd(interprocessBuffer_t *currentBuffer);
00080
00081
00082
00083
00084
00085 int initInterprocessBuffers(void)
00086 {
00087
00088
00089 frontRegUserTableBuffer = shm_malloc(sizeof(interprocessBuffer_t));
00090 endRegUserTableBuffer = shm_malloc(sizeof(interprocessBuffer_t));
00091
00092 if(frontRegUserTableBuffer == NULL || endRegUserTableBuffer == NULL)
00093 {
00094 LM_ERR("no more shared memory\n");
00095 return -1;
00096 }
00097
00098 memset(frontRegUserTableBuffer, 0x00, sizeof(interprocessBuffer_t));
00099 memset(endRegUserTableBuffer, 0x00, sizeof(interprocessBuffer_t));
00100
00101
00102
00103
00104
00105 interprocessCBLock = lock_alloc();
00106 lock_init(interprocessCBLock);
00107
00108 hashTable = createHashTable(HASH_SIZE);
00109 if(hashTable == NULL)
00110 {
00111 LM_ERR("no more shared memory\n");
00112 shm_free(frontRegUserTableBuffer);
00113 frontRegUserTableBuffer = NULL;
00114 shm_free(endRegUserTableBuffer);
00115 endRegUserTableBuffer = NULL;
00116 return -1;
00117 }
00118
00119 return 1;
00120 }
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136 void handleContactCallbacks(ucontact_t *contactInfo, int type, void *param)
00137 {
00138 char *addressOfRecord;
00139 char *contact;
00140
00141 interprocessBuffer_t *currentBufferElement;
00142
00143 currentBufferElement = shm_malloc(sizeof(interprocessBuffer_t));
00144
00145 if (currentBufferElement == NULL)
00146 {
00147 goto error;
00148 }
00149
00150
00151
00152
00153
00154
00155
00156 convertStrToCharString(contactInfo->aor, &addressOfRecord);
00157 convertStrToCharString(&(contactInfo->c), &contact);
00158
00159 currentBufferElement->stringName = addressOfRecord;
00160 currentBufferElement->stringContact = contact;
00161 currentBufferElement->contactInfo = contactInfo;
00162 currentBufferElement->callbackType = type;
00163 currentBufferElement->next = NULL;
00164
00165
00166
00167
00168
00169
00170 lock_get(interprocessCBLock);
00171
00172
00173 if (frontRegUserTableBuffer->next == NULL) {
00174 frontRegUserTableBuffer->next = currentBufferElement;
00175 } else {
00176 endRegUserTableBuffer->next->next = currentBufferElement;
00177 }
00178
00179 endRegUserTableBuffer->next = currentBufferElement;
00180
00181 lock_release(interprocessCBLock);
00182
00183 return;
00184
00185 error:
00186 LM_ERR("Not enough shared memory for openserSIPRegUserTable insert."
00187 " (%s)\n", contactInfo->c.s);
00188 }
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208 void consumeInterprocessBuffer(void)
00209 {
00210 interprocessBuffer_t *previousBuffer;
00211 interprocessBuffer_t *currentBuffer;
00212
00213
00214 if (frontRegUserTableBuffer->next == NULL)
00215 {
00216 return;
00217 }
00218
00219
00220
00221
00222
00223
00224 lock_get(interprocessCBLock);
00225
00226 currentBuffer = frontRegUserTableBuffer->next;
00227
00228 frontRegUserTableBuffer->next = NULL;
00229 endRegUserTableBuffer->next = NULL;
00230
00231 lock_release(interprocessCBLock);
00232
00233 while (currentBuffer != NULL) {
00234
00235 executeInterprocessBufferCmd(currentBuffer);
00236
00237
00238
00239
00240
00241 previousBuffer = currentBuffer;
00242 currentBuffer = currentBuffer->next;
00243 shm_free(previousBuffer->stringName);
00244 shm_free(previousBuffer->stringContact);
00245 shm_free(previousBuffer);
00246
00247 }
00248
00249 }
00250
00251
00252
00253
00254
00255
00256
00257 static void executeInterprocessBufferCmd(interprocessBuffer_t *currentBuffer)
00258 {
00259 int delContactIndex;
00260
00261 aorToIndexStruct_t *currentUser;
00262
00263 if (currentBuffer->callbackType == UL_CONTACT_INSERT)
00264 {
00265
00266
00267 updateUser(currentBuffer->stringName);
00268 }
00269 else if (currentBuffer->callbackType != UL_CONTACT_EXPIRE)
00270 {
00271
00272
00273
00274 LM_ERR("found a command on the interprocess buffer that"
00275 " was not an INSERT or EXPIRE");
00276 return;
00277 }
00278
00279 currentUser =
00280 findHashRecord(hashTable, currentBuffer->stringName, HASH_SIZE);
00281
00282
00283
00284 if (currentUser == NULL) {
00285 LM_ERR("Received a request for contact: %s for user: %s who doesn't "
00286 "exists\n", currentBuffer->stringName,
00287 currentBuffer->stringContact);
00288 return;
00289 }
00290
00291
00292
00293 if (currentBuffer->callbackType == UL_CONTACT_INSERT) {
00294
00295
00296
00297 currentUser->contactIndex++;
00298
00299
00300
00301 if(!insertContactRecord(&(currentUser->contactList),
00302 currentUser->contactIndex,
00303 currentBuffer->stringContact)) {
00304
00305 LM_ERR("openserSIPRegUserTable was unable to allocate memory for "
00306 "adding contact: %s to user %s.\n",
00307 currentBuffer->stringName, currentBuffer->stringContact);
00308
00309
00310
00311 currentUser->contactIndex--;
00312
00313 return;
00314 }
00315
00316 if (!createContactRow(currentUser->userIndex,
00317 currentUser->contactIndex,
00318 currentBuffer->stringContact,
00319 currentBuffer->contactInfo)) {
00320
00321 deleteContactRecord(&(currentUser->contactList),
00322 currentBuffer->stringContact);
00323
00324 }
00325
00326 }
00327 else {
00328
00329 delContactIndex =
00330 deleteContactRecord(&(currentUser->contactList),
00331 currentBuffer->stringContact);
00332
00333
00334
00335 if(delContactIndex == 0) {
00336
00337 LM_ERR("Received a request to delete contact: %s for user: %s"
00338 " who doesn't exist\n", currentBuffer->stringName,
00339 currentBuffer->stringContact);
00340 return;
00341
00342 }
00343
00344 deleteContactRow(currentUser->userIndex, delContactIndex);
00345
00346 deleteUser(hashTable, currentBuffer->stringName, HASH_SIZE);
00347 }
00348 }
00349
00350 void freeInterprocessBuffer(void)
00351 {
00352 interprocessBuffer_t *currentBuffer, *previousBuffer;
00353
00354 if (frontRegUserTableBuffer->next == NULL) {
00355 LM_DBG("Nothing to clean\n");
00356 return;
00357 }
00358
00359 currentBuffer = frontRegUserTableBuffer->next;
00360
00361 frontRegUserTableBuffer->next = NULL;
00362 endRegUserTableBuffer->next = NULL;
00363
00364
00365 while (currentBuffer != NULL) {
00366
00367 previousBuffer = currentBuffer;
00368 currentBuffer = currentBuffer->next;
00369 shm_free(previousBuffer->stringName);
00370 shm_free(previousBuffer->stringContact);
00371 shm_free(previousBuffer);
00372
00373 }
00374
00375 if(frontRegUserTableBuffer)
00376 shm_free(frontRegUserTableBuffer);
00377
00378 if(endRegUserTableBuffer)
00379 shm_free(endRegUserTableBuffer);
00380
00381 }