cluster.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 #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
00039
00040
00041
00042
00043
00044
00045
00046
00047
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
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
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
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 }