cr_carrier.c

Go to the documentation of this file.
00001 /*
00002  * $Id$
00003  *
00004  * Copyright (C) 2007-2008 1&1 Internet AG
00005  *
00006  * This file is part of SIP-router, a free SIP server.
00007  *
00008  * SIP-router is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version
00012  *
00013  * SIP-router is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License 
00019  * along with this program; if not, write to the Free Software 
00020  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021  */
00022 
00030 #include <stdlib.h>
00031 #include "../../mem/shm_mem.h"
00032 #include "../../ut.h"
00033 #include "cr_carrier.h"
00034 #include "cr_domain.h"
00035 #include "cr_map.h"
00036 
00037 
00048 struct carrier_data_t * create_carrier_data(int carrier_id, str *carrier_name, int domains) {
00049         struct carrier_data_t * tmp;
00050         if ((tmp = shm_malloc(sizeof(struct carrier_data_t))) == NULL) {
00051                 SHM_MEM_ERROR;
00052                 return NULL;
00053         }
00054         memset(tmp, 0, sizeof(struct carrier_data_t));
00055         tmp->id = carrier_id;
00056         tmp->name = carrier_name;
00057         tmp->domain_num = domains;
00058         if(domains > 0){
00059                 if ((tmp->domains = shm_malloc(sizeof(struct domain_data_t *) * domains)) == NULL) {
00060                         SHM_MEM_ERROR;
00061                         shm_free(tmp);
00062                         return NULL;
00063                 }
00064                 memset(tmp->domains, 0, sizeof(struct domain_data_t *) * domains);
00065         }
00066         return tmp;
00067 }
00068 
00069 
00075 void destroy_carrier_data(struct carrier_data_t *carrier_data) {
00076         int i;
00077         if (carrier_data) {
00078                 if (carrier_data->domains != NULL) {
00079                         for (i=0; i<carrier_data->domain_num; i++) {
00080                                 destroy_domain_data(carrier_data->domains[i]);
00081                         }
00082                         shm_free(carrier_data->domains);
00083                 }
00084                 shm_free(carrier_data);
00085         }
00086 }
00087 
00088 
00099 int add_domain_data(struct carrier_data_t * carrier_data, struct domain_data_t * domain_data, int index) {
00100         LM_INFO("adding domain %d '%.*s' to carrier %d '%.*s'", domain_data->id, domain_data->name->len, domain_data->name->s, carrier_data->id, carrier_data->name->len, carrier_data->name->s);
00101         LM_DBG("domain position %d (domain_num=%d, first_empty_domain=%d)", index, (int) carrier_data->domain_num, (int) carrier_data->first_empty_domain);
00102 
00103         if ((index < 0) || (index > carrier_data->first_empty_domain)) {
00104                 LM_ERR("got invalid index during binary search\n");
00105                 return -1;
00106         }
00107                 
00108         if (carrier_data->first_empty_domain >= carrier_data->domain_num) {
00109                 LM_ERR("cannot add new domain '%.*s' into carrier '%.*s' - array already full\n", domain_data->name->len, domain_data->name->s, carrier_data->name->len, carrier_data->name->s);
00110                 return -1;
00111         }
00112 
00113         if (index < carrier_data->first_empty_domain) {
00114                 /* move other entries one position up */
00115                 memmove(&carrier_data->domains[index+1], &carrier_data->domains[index], sizeof(struct domain_data_t *)*(carrier_data->first_empty_domain-index));
00116         }
00117         carrier_data->domains[index] = domain_data;
00118         carrier_data->first_empty_domain++;
00119 
00120         return 0;
00121 }
00122 
00123 
00133 struct domain_data_t * get_domain_data(struct carrier_data_t * carrier_data, int domain_id) {
00134         struct domain_data_t **ret;
00135         struct domain_data_t key;
00136         struct domain_data_t *pkey = &key;
00137 
00138         if (!carrier_data) {
00139                 LM_ERR("NULL pointer in parameter\n");
00140                 return NULL;
00141         }
00142         key.id = domain_id;
00143         ret = bsearch(&pkey, carrier_data->domains, carrier_data->domain_num, sizeof(carrier_data->domains[0]), compare_domain_data);
00144         if (ret) return *ret;
00145         return NULL;
00146 }
00147 
00148 
00155 int compare_carrier_data(const void *v1, const void *v2) {
00156   struct carrier_data_t *c1 = *(struct carrier_data_t * const *)v1;
00157         struct carrier_data_t *c2 = *(struct carrier_data_t * const *)v2;
00158         if (c1 == NULL) {
00159                 if (c2 == NULL) return 0;
00160                 else return 1;
00161         }
00162         else {
00163                 if (c2 == NULL) return -1;
00164                 else {
00165                         if (c1->id < c2->id) return -1;
00166                         else if (c1->id > c2->id) return 1;
00167                         else return 0;
00168                 }
00169         }
00170 }