cluster.c

Go to the documentation of this file.
00001 /* $Id: cluster.c 4518 2008-07-28 15:39:28Z henningw $
00002  *
00003  * Copyright (C) 2006-2007 VozTelecom Sistemas S.L
00004  *
00005  * This file is part of Kamailio, a free SIP server.
00006  *
00007  * Kamailio is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 2 of the License, or
00010  * (at your option) any later version
00011  *
00012  * Kamailio is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License 
00018  * along with this program; if not, write to the Free Software 
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020  */
00021 
00022 
00023 #include <string.h>
00024 
00025 #include "cluster.h"
00026 #include "seas.h"
00027 #include "../../dprint.h"
00028 #include "../../mem/mem.h"
00029 
00030 char *cluster_cfg;
00031 
00032 #define eat_spaces(_p) \
00033    while( *(_p)==' ' || *(_p)=='\t' ){\
00034    (_p)++;}
00035 
00036 
00037 /**
00038  * Parses the PING configuration string. Its format is 
00039  * "ping_period:pings_lost:ping_timeout"
00040  * ping_period : time between pings
00041  * pings_lost: number of lost pings before failure
00042  * ping_timeout: time to consider a ping failed
00043  *
00044  * returns 
00045  * 0 if no clusters present
00046  * -1 if config is malformed (unable to parse);
00047  *  1 if config is successfully set
00048  */
00049 int parse_cluster_cfg(void)
00050 {
00051    char *p,*start;
00052    int n,k;
00053    struct as_entry **entry,*tmp,*tmp2;
00054 
00055    if((p=cluster_cfg)==0 || *cluster_cfg==0){
00056       return 0;
00057    }
00058    entry=&as_list;
00059 
00060    while (*p)
00061    {
00062       eat_spaces(p);
00063       /*get cluster name*/
00064       start = p;
00065       while (*p!=' ' && *p!='\t' && *p!='[' && *p!=0)
00066     p++;
00067       if ( p==start || *p==0 ){
00068     LM_ERR("cluster names must only contain alphanumeric chars\n");
00069     goto error;
00070       }
00071       if (!((*entry)=(struct as_entry*)shm_malloc(sizeof(struct as_entry)))) {
00072     LM_ERR("Out of shm mem for as_entry\n");
00073     goto error;
00074       }
00075       memset(*entry,0,sizeof(struct as_entry));
00076       if (!((*entry)->name.s=shm_malloc(p-start))) {
00077     LM_ERR("Out of shm malloc for cluster name\n");
00078     goto error;
00079       }
00080       memcpy((*entry)->name.s, start, p-start);
00081       (*entry)->name.len=p-start;
00082       (*entry)->connected=0;
00083       (*entry)->type=CLUSTER_TYPE;
00084       (*entry)->u.cs.name=(*entry)->name;
00085       /*get as names*/
00086       eat_spaces(p);
00087       if (*p!='['){
00088     LM_ERR("Malformed cluster cfg string %s\n",cluster_cfg);
00089     goto error;
00090       }
00091       p++;
00092       n=0;
00093       while (*p!=']')
00094       {
00095     eat_spaces(p);
00096     start = p;
00097     while(*p!=' ' && *p!='\t' && *p!=']' && *p!=',' && *p!=0)
00098        p++;
00099     if ( p==start || *p==0 )
00100        goto error;
00101     if (!((*entry)->u.cs.as_names[n].s=shm_malloc(p-start))) {
00102        LM_ERR("Out of shm_mem for AS name in cluster\n");
00103        goto error;
00104     }
00105     (*entry)->u.cs.as_names[n].len=p-start;
00106     memcpy((*entry)->u.cs.as_names[n].s,start,p-start);
00107     n++;
00108     if(n>=MAX_AS_PER_CLUSTER){
00109        LM_ERR("too many AS per cluster\n");
00110        goto error;
00111     }
00112     eat_spaces(p);
00113     if (*p==',') {
00114        p++;
00115        eat_spaces(p);
00116     }
00117       }
00118       p++;
00119       (*entry)->u.cs.num=n;
00120       /* end of element */
00121       eat_spaces(p);
00122       if (*p==',')
00123     p++;
00124       eat_spaces(p);
00125       entry=&((*entry)->next);
00126    }
00127    for (tmp=as_list;tmp->next;tmp=tmp->next){
00128       LM_DBG("%.*s\n",tmp->name.len,tmp->name.s);
00129    }
00130    LM_DBG("%.*s\n",tmp->name.len,tmp->name.s);
00131    entry=&(tmp->next);
00132    for(tmp=as_list;tmp;tmp=tmp->next){
00133       if (tmp->type!=CLUSTER_TYPE) 
00134     continue;
00135       LM_DBG("cluster:[%.*s]\n",tmp->name.len,tmp->name.s);
00136       for(k=0;k<tmp->u.cs.num;k++){
00137     LM_DBG("\tAS:[%.*s]\n",tmp->u.cs.as_names[k].len,tmp->u.cs.as_names[k].s);
00138     for (tmp2=as_list;tmp2;tmp2=tmp2->next) {
00139        if (tmp2->type== AS_TYPE && tmp->u.cs.as_names[k].len == tmp2->name.len && 
00140         !memcmp(tmp->u.cs.as_names[k].s,tmp2->name.s,tmp2->name.len)) {
00141           tmp->u.cs.servers[k]=&tmp2->u.as;
00142           break;
00143        }
00144     }
00145     if(tmp2)
00146        continue;
00147     if (!((*entry)=shm_malloc(sizeof(struct as_entry)))) {
00148        LM_ERR("Out of shm mem \n");
00149        goto error;
00150     }
00151     memset(*entry,0,sizeof(struct as_entry));
00152     (*entry)->type=AS_TYPE;
00153     if (!((*entry)->name.s=shm_malloc(tmp->u.cs.as_names[k].len))) {
00154        LM_ERR("out of shm mem\n");
00155        goto error;
00156     }
00157     memcpy((*entry)->name.s,tmp->u.cs.as_names[k].s,tmp->u.cs.as_names[k].len);
00158     (*entry)->name.len=tmp->u.cs.as_names[k].len;
00159     (*entry)->u.as.name=(*entry)->name;
00160     tmp->u.cs.servers[k]=&(*entry)->u.as;
00161     entry=&((*entry)->next);
00162       }
00163    }
00164    for(tmp=as_list;tmp;tmp=tmp->next){
00165       LM_DBG("%.*s %s",tmp->name.len,tmp->name.s,tmp->next?"":"\n");
00166    }
00167    return 1;
00168 error:
00169    tmp=as_list;
00170    while(tmp){
00171       for(k=0;k<tmp->u.cs.num;k++){
00172     if(tmp->u.cs.as_names[k].s)
00173        shm_free(tmp->u.cs.as_names[k].s);
00174       }
00175       if(tmp->name.s)
00176     shm_free(tmp->name.s);
00177       tmp2=tmp;
00178       tmp=tmp->next;
00179       shm_free(tmp2);
00180    }
00181    as_list=(struct as_entry *)0;
00182    return -1;
00183 }

Generated on Mon May 21 18:00:25 2012 for Kamailio - The Open Source SIP Server by  doxygen 1.5.6