bin_scscf.c

Go to the documentation of this file.
00001 /*
00002  * $Id: bin_scscf.c 429 2007-08-01 10:15:27Z vingarzan $
00003  *
00004  * Copyright (C) 2004-2007 FhG Fokus
00005  *
00006  * This file is part of Open IMS Core - an open source IMS CSCFs & HSS
00007  * implementation
00008  *
00009  * Open IMS Core is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2 of the License, or
00012  * (at your option) any later version.
00013  *
00014  * For a license to use the Open IMS Core software under conditions
00015  * other than those described here, or to purchase support for this
00016  * software, please contact Fraunhofer FOKUS by e-mail at the following
00017  * addresses:
00018  *     info@open-ims.org
00019  *
00020  * Open IMS Core is distributed in the hope that it will be useful,
00021  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00022  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023  * GNU General Public License for more details.
00024  *
00025  * It has to be noted that this Open Source IMS Core System is not
00026  * intended to become or act as a product in a commercial context! Its
00027  * sole purpose is to provide an IMS core reference implementation for
00028  * IMS technology testing and IMS application prototyping for research
00029  * purposes, typically performed in IMS test-beds.
00030  *
00031  * Users of the Open Source IMS Core System have to be aware that IMS
00032  * technology may be subject of patents and licence terms, as being
00033  * specified within the various IMS-related IETF, ITU-T, ETSI, and 3GPP
00034  * standards. Thus all Open IMS Core users have to take notice of this
00035  * fact and have to agree to check out carefully before installing,
00036  * using and extending the Open Source IMS Core System, if related
00037  * patents and licences may become applicable to the intended usage
00038  * context.
00039  *
00040  * You should have received a copy of the GNU General Public License
00041  * along with this program; if not, write to the Free Software
00042  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00043  *
00044  */
00045 
00055 #include "bin_scscf.h"
00056 
00057 extern struct tm_binds tmb;         
00059 extern int r_hash_size;                     
00062 static inline int str_shm_dup(str *dest,str *src)
00063 {
00064     dest->s = shm_malloc(src->len);
00065     if (!dest->s){
00066         LOG(L_ERR,"ERR:"M_NAME":str_shm_dup: Error allocating %d bytes\n",src->len);
00067         dest->len=0;
00068         return 0;
00069     }
00070     dest->len = src->len;
00071     memcpy(dest->s,src->s,src->len);
00072     return 1;
00073 }
00074 
00081 static int bin_encode_public_identity(bin_data *x,ims_public_identity *pi)
00082 {
00083     if (!bin_encode_char(x,pi->barring)) goto error;
00084     if (!bin_encode_str(x,&(pi->public_identity))) goto error;  
00085     return 1;
00086 error:
00087     LOG(L_ERR,"ERR:"M_NAME":bin_encode_public_identity: Error while encoding.\n");
00088     return 0;       
00089 }
00090 
00097 static int bin_decode_public_identity(bin_data *x,ims_public_identity *pi)
00098 {
00099     str s;
00100     if (!bin_decode_char(x, &(pi->barring))) goto error;
00101     if (!bin_decode_str(x,&s)||!str_shm_dup(&(pi->public_identity),&s)) goto error;
00102     
00103     return 1;
00104 error:
00105     LOG(L_ERR,"ERR:"M_NAME":bin_decode_public_identity: Error while decoding (at %d (%04x)).\n",x->max,x->max);
00106     if (pi) {
00107         if (pi->public_identity.s) shm_free(pi->public_identity.s);
00108     }
00109     return 0;
00110 }
00111 
00112 
00113 
00114 
00115 
00116 
00123 static int bin_encode_spt(bin_data *x, ims_spt *spt)
00124 {
00125     unsigned char c = spt->condition_negated<<7 | spt->registration_type<<4 | spt->type;
00126     // cond negated, reg type, spt type
00127     if (!bin_encode_uchar(x,c)) goto error;
00128 
00129     //group
00130     if (!bin_encode_int(x,spt->group)) goto error;
00131 
00132     //spt
00133     switch(spt->type){
00134         case 1:
00135             if (!bin_encode_str(x,&(spt->request_uri))) goto error; 
00136             break;
00137         case 2:
00138             if (!bin_encode_str(x,&(spt->method))) goto error; 
00139             break;
00140         case 3:
00141             if (!bin_encode_short(x,spt->sip_header.type)) goto error;
00142             if (!bin_encode_str(x,&(spt->sip_header.header))) goto error; 
00143             if (!bin_encode_str(x,&(spt->sip_header.content))) goto error; 
00144             break;
00145         case 4:
00146             if (!bin_encode_char(x,spt->session_case)) goto error;
00147             break;
00148         case 5:
00149             if (!bin_encode_str(x,&(spt->session_desc.line))) goto error; 
00150             if (!bin_encode_str(x,&(spt->session_desc.content))) goto error; 
00151             break;
00152     }
00153     return 1;
00154 error:
00155     LOG(L_ERR,"ERR:"M_NAME":bin_encode_spt: Error while encoding.\n");
00156     return 0;       
00157 }
00158 
00159 
00166 static int bin_decode_spt(bin_data *x, ims_spt *spt)
00167 {
00168     unsigned char k;
00169     str s;
00170     
00171     if (!bin_decode_uchar(x,&k)) goto error;
00172     
00173     spt->type = k & 0x0F;
00174     spt->condition_negated = ((k & 0x80)!=0);
00175     spt->registration_type = ((k & 0x70)>>4);
00176     
00177     if (!bin_decode_int(x,&(spt->group))) goto error;
00178 
00179     switch (spt->type){
00180         case 1:
00181             if (!bin_decode_str(x,&s)||!str_shm_dup(&(spt->request_uri),&s)) goto error;
00182             break;
00183         case 2:
00184             if (!bin_decode_str(x,&s)||!str_shm_dup(&(spt->method),&s)) goto error;
00185             break;
00186         case 3:
00187             if (!bin_decode_short(x,&(spt->sip_header.type))) goto error;
00188             if (!bin_decode_str(x,&s)||!str_shm_dup(&(spt->sip_header.header),&s)) goto error;
00189             if (!bin_decode_str(x,&s)||!str_shm_dup(&(spt->sip_header.content),&s)) goto error;
00190             break;
00191         case 4:
00192             if (!bin_decode_char(x,&(spt->session_case))) goto error;
00193             break;
00194         case 5:
00195             if (!bin_decode_str(x,&s)||!str_shm_dup(&(spt->session_desc.line),&s)) goto error;
00196             if (!bin_decode_str(x,&s)||!str_shm_dup(&(spt->session_desc.content),&s)) goto error;
00197             break;
00198 
00199     }
00200     return 1;
00201     
00202 error:
00203     LOG(L_ERR,"ERR:"M_NAME":bin_decode_spt: Error while decoding (at %d (%04x)).\n",x->max,x->max);
00204     if (spt){
00205         switch (spt->type){
00206             case 1:
00207                 if (spt->request_uri.s) shm_free(spt->request_uri.s);
00208                 break;
00209             case 2:
00210                 if (spt->method.s) shm_free(spt->method.s);
00211                 break;
00212             case 3:
00213                 if (spt->sip_header.header.s) shm_free(spt->sip_header.header.s);
00214                 if (spt->sip_header.header.s) shm_free(spt->sip_header.content.s);
00215                 break;
00216             case 4:
00217                 break;
00218             case 5:
00219                 if (spt->sip_header.header.s) shm_free(spt->session_desc.line.s);
00220                 if (spt->sip_header.header.s) shm_free(spt->session_desc.content.s);
00221                 break;
00222         }
00223     }
00224     return 0;
00225 }   
00226 
00227 
00228 
00235 static int bin_encode_filter_criteria(bin_data *x, ims_filter_criteria *fc)
00236 {
00237     int i;
00238     char ppindicator;
00239 
00240     //priority
00241     if (!bin_encode_int(x,fc->priority)) goto error;
00242     
00243     //profile part indicator
00244     if (fc->profile_part_indicator) ppindicator = (*fc->profile_part_indicator)+1;
00245     else ppindicator = 0;
00246     if (!bin_encode_char(x,ppindicator)) goto error;
00247             
00248     // trigger point 
00249     if (fc->trigger_point) {
00250         if (!bin_encode_char(x,fc->trigger_point->condition_type_cnf)) goto error;
00251         
00252         if (!bin_encode_ushort(x,fc->trigger_point->spt_cnt)) goto error;
00253         
00254         for(i=0;i<fc->trigger_point->spt_cnt;i++)
00255             if (!bin_encode_spt(x,fc->trigger_point->spt+i)) goto error;
00256     } else {
00257         if (!bin_encode_char(x,100)) goto error;
00258     }
00259     
00260     //app server
00261     if (!bin_encode_str(x,&(fc->application_server.server_name))) goto error;
00262     if (!bin_encode_char(x,fc->application_server.default_handling)) goto error;
00263     if (!bin_encode_str(x,&(fc->application_server.service_info))) goto error;
00264     
00265     return 1;
00266 error:
00267     LOG(L_ERR,"ERR:"M_NAME":bin_encode_filter_criteria: Error while encoding.\n");
00268     return 0;       
00269 }
00270 
00271 
00278 static int bin_decode_filter_criteria(bin_data *x, ims_filter_criteria *fc)
00279 {
00280     int i,len;
00281     str s;
00282     char ppindicator,cnf;
00283     
00284     //priority
00285     if (!bin_decode_int(x,&(fc->priority))) goto error;
00286     
00287     // profile part indicator
00288     if (!bin_decode_char(x,&ppindicator)) goto error;
00289     if (!ppindicator){
00290         fc->profile_part_indicator = 0;
00291     }
00292     else {
00293         fc->profile_part_indicator = (char*)shm_malloc(sizeof(char));
00294         if (!fc->profile_part_indicator) {
00295             LOG(L_ERR,"ERR:"M_NAME":bin_decode_filter_criteria: Error allocating %d bytes.\n",sizeof(int));
00296             goto error;
00297         }
00298         *(fc->profile_part_indicator) = ppindicator-1;
00299     }
00300     
00301     //cnf 
00302     if (!bin_decode_char(x,&cnf)) goto error;
00303 
00304     if (cnf==100)
00305         fc->trigger_point=NULL;
00306     else {
00307         ims_trigger_point *tp=0;
00308         //trigger point
00309         len = sizeof(ims_trigger_point);
00310         tp = (ims_trigger_point*)shm_malloc(len);
00311         fc->trigger_point = tp;
00312         if (!tp) {
00313             LOG(L_ERR,"ERR:"M_NAME":bin_decode_filter_criteria: Error allocating %d bytes.\n",len);
00314             goto error;
00315         }
00316         memset(tp,0,len);
00317         tp->condition_type_cnf=cnf;
00318         
00319         if (!bin_decode_ushort(x,&tp->spt_cnt)) goto error;
00320         len = sizeof(ims_spt)*tp->spt_cnt;
00321         tp->spt = (ims_spt*)shm_malloc(len);
00322         if (!tp->spt) {
00323             LOG(L_ERR,"ERR:"M_NAME":bin_decode_filter_criteria: Error allocating %d bytes.\n",len);
00324             goto error;
00325         }
00326         memset(tp->spt,0,len);
00327         for(i=0;i<tp->spt_cnt;i++)
00328             if (!bin_decode_spt(x,tp->spt+i)) goto error;
00329     }
00330     //app server uri
00331     if (!bin_decode_str(x,&s)||!str_shm_dup(&(fc->application_server.server_name),&s)) goto error;
00332     // app server default handling
00333     if (!bin_decode_char(x,&(fc->application_server.default_handling)))goto error;
00334     // app server service info
00335     if (!bin_decode_str(x,&s)||!str_shm_dup(&(fc->application_server.service_info),&s)) goto error;
00336 
00337     return 1;
00338 error:
00339     LOG(L_ERR,"ERR:"M_NAME":bin_decode_filter_criteria: Error while decoding (at %d (%04x)).\n",x->max,x->max);
00340     if (fc){
00341         if (fc->trigger_point){
00342             if (fc->trigger_point){
00343                 if (fc->trigger_point->spt) shm_free(fc->trigger_point->spt);
00344             }
00345             shm_free(fc->trigger_point);
00346         }
00347         if (fc->application_server.server_name.s) shm_free(fc->application_server.server_name.s);
00348         if (fc->application_server.service_info.s) shm_free(fc->application_server.service_info.s);
00349     }
00350     return 0;       
00351 }
00352 
00353 
00354 
00355 
00356 
00357 
00364 static int bin_encode_service_profile(bin_data *x,ims_service_profile *sp)
00365 {
00366     int i;
00367         
00368     //public identity
00369     if (!bin_encode_ushort(x,sp->public_identities_cnt)) return 0;
00370     for(i=0;i<sp->public_identities_cnt;i++)
00371         if (!bin_encode_public_identity(x,sp->public_identities+i)) goto error;
00372     
00373     //filter criteria
00374     if (!bin_encode_ushort(x,sp->filter_criteria_cnt)) return 0;
00375     for(i=0;i<sp->filter_criteria_cnt;i++)
00376         if (!bin_encode_filter_criteria(x,sp->filter_criteria+i)) goto error;
00377         
00378     //cn service_auth
00379     if (sp->cn_service_auth)
00380         i = sp->cn_service_auth->subscribed_media_profile_id;
00381     else i = 0xFFFFFFFF;
00382     if (!bin_encode_int(x,i)) goto error;
00383 
00384     //shared_ifc
00385     if (!bin_encode_ushort(x,sp->shared_ifc_set_cnt)) return 0;
00386     for(i=0;i<sp->shared_ifc_set_cnt;i++)
00387         if (!bin_encode_int(x,sp->shared_ifc_set[i])) goto error;
00388 
00389     return 1;
00390 error:
00391     LOG(L_ERR,"ERR:"M_NAME":bin_encode_service_profile: Error while encoding.\n");
00392     return 0;       
00393 }
00394 
00395 
00402 static int bin_decode_service_profile(bin_data *x, ims_service_profile *sp)
00403 {
00404     int i,len;
00405 
00406     //public identities
00407     if (!bin_decode_ushort(x,&(sp->public_identities_cnt))) goto error;
00408     len = sizeof(ims_public_identity)*sp->public_identities_cnt;
00409     sp->public_identities = (ims_public_identity*)shm_malloc(len);
00410     if (!sp->public_identities) {
00411         LOG(L_ERR,"ERR:"M_NAME":bin_decode_service_profile: Error allocating %d bytes.\n",len);
00412         goto error;
00413     }
00414     memset(sp->public_identities,0,len);
00415     for(i=0;i<sp->public_identities_cnt;i++)
00416         if (!bin_decode_public_identity(x,sp->public_identities+i)) goto error;
00417     
00418     // filter criteria
00419     if (!bin_decode_ushort(x,&(sp->filter_criteria_cnt))) goto error;   
00420     len = sizeof(ims_filter_criteria)*sp->filter_criteria_cnt;
00421     sp->filter_criteria = (ims_filter_criteria*)shm_malloc(len);
00422     if (!sp->filter_criteria) {
00423         LOG(L_ERR,"ERR:"M_NAME":bin_decode_service_profile: Error allocating %d bytes.\n",len);
00424         goto error;
00425     }
00426     memset(sp->filter_criteria,0,len);
00427     for(i=0;i<sp->filter_criteria_cnt;i++)
00428         if (!bin_decode_filter_criteria(x,sp->filter_criteria+i)) goto error;
00429 
00430     // cn service auth
00431     if (!bin_decode_int(x,&i)) goto error;
00432     if (i==0xFFFFFFFF)
00433         sp->cn_service_auth = 0;
00434     else {
00435         len = sizeof(ims_cn_service_auth);
00436         sp->cn_service_auth = (ims_cn_service_auth*)shm_malloc(len);
00437         if (!sp->cn_service_auth) {
00438             LOG(L_ERR,"ERR:"M_NAME":bin_decode_service_profile: Error allocating %d bytes.\n",len);
00439             goto error;
00440         }
00441         sp->cn_service_auth->subscribed_media_profile_id=i;
00442     }
00443     
00444     //shared ifc
00445     if (!bin_decode_ushort(x,&(sp->shared_ifc_set_cnt))) goto error;    
00446     len = sizeof(int)*sp->shared_ifc_set_cnt;
00447     sp->shared_ifc_set = (int*)shm_malloc(len);
00448     if (!sp->shared_ifc_set) {
00449         LOG(L_ERR,"ERR:"M_NAME":bin_decode_service_profile: Error allocating %d bytes.\n",len);
00450         goto error;
00451     }
00452     memset(sp->shared_ifc_set,0,len);
00453     for(i=0;i<sp->shared_ifc_set_cnt;i++)
00454         if (!bin_decode_int(x,sp->shared_ifc_set+i)) goto error;
00455 
00456     return 1;
00457 error:
00458     LOG(L_ERR,"ERR:"M_NAME":bin_decode_service_profile: Error while decoding (at %d (%04x)).\n",x->max,x->max);
00459     if (sp) {
00460         if (sp->public_identities) shm_free(sp->public_identities);
00461         if (sp->filter_criteria) shm_free(sp->filter_criteria);
00462         if (sp->cn_service_auth) shm_free(sp->cn_service_auth);
00463         if (sp->shared_ifc_set) shm_free(sp->shared_ifc_set);
00464     }
00465     return 0;
00466 }
00467 
00468 
00469 
00470 
00471 
00472 
00473 
00474 
00475 
00476 
00477 
00478 
00479 
00486 int bin_encode_ims_subscription(bin_data *x, ims_subscription *s)
00487 {
00488     int i;
00489     if (!bin_encode_str(x,&(s->private_identity))) goto error;
00490     if (!bin_encode_ushort(x,s->service_profiles_cnt)) goto error;
00491 
00492     for(i=0;i<s->service_profiles_cnt;i++)
00493         if (!bin_encode_service_profile(x,s->service_profiles+i)) goto error;
00494     
00495     return 1;
00496 error:
00497     LOG(L_ERR,"ERR:"M_NAME":bin_encode_ims_subscription: Error while encoding.\n");
00498     return 0;   
00499 }
00500 
00501 
00507 ims_subscription *bin_decode_ims_subscription(bin_data *x)
00508 {
00509     ims_subscription *imss=0;
00510     int i,len;
00511     str s;
00512     
00513     imss = (ims_subscription*) shm_malloc(sizeof(ims_subscription));
00514     if (!imss) {
00515         LOG(L_ERR,"ERR:"M_NAME":bin_decode_ims_subscription: Error allocating %d bytes.\n",sizeof(ims_subscription));
00516         goto error;
00517     }
00518     memset(imss,0,sizeof(ims_subscription));
00519     
00520     if (!bin_decode_str(x,&s)||!str_shm_dup(&(imss->private_identity),&s)) goto error;
00521     if (!bin_decode_ushort(x,   &(imss->service_profiles_cnt))) goto error;
00522     
00523     len = sizeof(ims_service_profile)*imss->service_profiles_cnt;
00524     imss->service_profiles = (ims_service_profile*)shm_malloc(len);
00525     if (!imss->service_profiles) {
00526         LOG(L_ERR,"ERR:"M_NAME":bin_decode_ims_subscription: Error allocating %d bytes.\n",len);
00527         goto error;
00528     }
00529     memset(imss->service_profiles,0,len);
00530 
00531     for(i=0;i<imss->service_profiles_cnt;i++)
00532         if (!bin_decode_service_profile(x,imss->service_profiles+i)) goto error;
00533 
00534     imss->lock = lock_alloc();
00535     imss->lock = lock_init(imss->lock);
00536     imss->ref_count = 1;
00537 
00538     return imss;
00539 error:
00540     LOG(L_ERR,"ERR:"M_NAME":bin_decode_ims_subscription: Error while decoding (at %d (%04x)).\n",x->max,x->max);
00541     if (imss) {
00542         if (imss->private_identity.s) shm_free(imss->private_identity.s);
00543         if (imss->service_profiles) shm_free(imss->service_profiles);
00544         shm_free(imss);
00545     }
00546     return 0;
00547 }
00548 
00549 
00550 
00551 
00552 
00559 int bin_encode_r_contact(bin_data *x,r_contact *c)
00560 {
00561     if (!bin_encode_str(x,&(c->uri))) goto error;
00562     if (!bin_encode_time_t(x,c->expires)) goto error;
00563     if (!bin_encode_str(x,&(c->ua))) goto error;
00564     if (!bin_encode_str(x,&(c->path))) goto error;
00565 
00566     return 1;
00567 error:
00568     LOG(L_ERR,"ERR:"M_NAME":bin_encode_r_contact: Error while encoding.\n");
00569     return 0;       
00570 }
00571 
00577 r_contact* bin_decode_r_contact(bin_data *x)
00578 {
00579     r_contact *c=0;
00580     int len;
00581     str s;
00582     
00583     len = sizeof(r_contact);
00584     c = (r_contact*) shm_malloc(len);
00585     if (!c) {
00586         LOG(L_ERR,"ERR:"M_NAME":bin_decode_r_contact: Error allocating %d bytes.\n",len);
00587         goto error;
00588     }
00589     memset(c,0,len);
00590     
00591     if (!bin_decode_str(x,&s)||!str_shm_dup(&(c->uri),&s)) goto error;
00592     if (!bin_decode_time_t(x,&c->expires)) goto error;
00593     if (!bin_decode_str(x,&s)||!str_shm_dup(&(c->ua),&s)) goto error;
00594     if (!bin_decode_str(x,&s)||!str_shm_dup(&(c->path),&s)) goto error;
00595     
00596     return c;
00597 error:
00598     LOG(L_ERR,"ERR:"M_NAME":bin_decode_r_contact: Error while decoding (at %d (%04x)).\n",x->max,x->max);
00599     if (c) {
00600         if (c->uri.s) shm_free(c->uri.s);
00601         if (c->ua.s) shm_free(c->ua.s);
00602         if (c->path.s) shm_free(c->path.s);
00603         
00604         shm_free(c);
00605     }
00606     return 0;
00607 }
00608 
00609 
00616 int bin_encode_r_subscriber(bin_data *x,r_subscriber *s)
00617 {
00618     if (!bin_encode_str(x,&(s->subscriber))) goto error;
00619     if (!bin_encode_char(x,s->event)) goto error;
00620     if (!bin_encode_time_t(x,s->expires)) goto error;
00621     if (!bin_encode_dlg_t(x,s->dialog)) goto error;
00622     if (!bin_encode_int(x,s->version)) goto error;
00623 
00624     return 1;
00625 error:
00626     LOG(L_ERR,"ERR:"M_NAME":bin_encode_r_subscriber: Error while encoding.\n");
00627     return 0;       
00628 }
00629 
00635 r_subscriber* bin_decode_r_subscriber(bin_data *x)
00636 {
00637     r_subscriber *s=0;
00638     int len;
00639     str st;
00640     
00641     len = sizeof(r_subscriber);
00642     s = (r_subscriber*) shm_malloc(len);
00643     if (!s) {
00644         LOG(L_ERR,"ERR:"M_NAME":bin_decode_r_contact: Error allocating %d bytes.\n",len);
00645         goto error;
00646     }
00647     memset(s,0,len);
00648     
00649     if (!bin_decode_str(x,&st)||!str_shm_dup(&(s->subscriber),&st)) goto error;
00650     if (!bin_decode_char(x,&(s->event))) goto error;
00651     if (!bin_decode_time_t(x,&(s->expires))) goto error;
00652     if (!bin_decode_dlg_t(x,&(s->dialog))) goto error;
00653     if (!bin_decode_int(x,&(s->version))) goto error;   
00654     
00655     return s;
00656 error:
00657     LOG(L_ERR,"ERR:"M_NAME":bin_decode_r_contact: Error while decoding (at %d (%04x)).\n",x->max,x->max);
00658     if (s) {
00659         if (s->subscriber.s) shm_free(s->subscriber.s);
00660         if (s->dialog) tmb.free_dlg(s->dialog);
00661         shm_free(s);
00662     }
00663     return 0;
00664 }
00665 
00666 
00667 
00674 int bin_encode_r_public(bin_data *x,r_public *p)
00675 {
00676     unsigned short k;
00677     char ch;
00678     r_contact *c=0;
00679     r_subscriber *s=0;
00680     
00681     if (!bin_encode_str(x,&(p->aor))) goto error;
00682     if (!bin_encode_str(x,&(p->early_ims_ip))) goto error;  
00683     ch = p->reg_state;
00684     if (!bin_encode_char(x,ch)) goto error;
00685     if (!bin_encode_ims_subscription(x,p->s)) goto error;
00686     if (!bin_encode_str(x,&(p->ccf1))) goto error;
00687     if (!bin_encode_str(x,&(p->ccf2))) goto error;
00688     if (!bin_encode_str(x,&(p->ecf1))) goto error;
00689     if (!bin_encode_str(x,&(p->ecf2))) goto error;
00690     
00691     k=0;
00692     for(c=p->head;c;c=c->next)
00693         k++;
00694     if (!bin_encode_ushort(x,k)) goto error;
00695     for(c=p->head;c;c=c->next)
00696         if (!bin_encode_r_contact(x,c)) goto error; 
00697     
00698     k=0;
00699     for(s=p->shead;s;s=s->next)
00700         k++;
00701     if (!bin_encode_ushort(x,k)) goto error;
00702     for(s=p->shead;s;s=s->next)
00703         if (!bin_encode_r_subscriber(x,s)) goto error;  
00704     
00705     return 1;
00706 error:
00707     LOG(L_ERR,"ERR:"M_NAME":bin_encode_r_public: Error while encoding.\n");
00708     return 0;       
00709 }
00710 
00716 r_public* bin_decode_r_public(bin_data *x)
00717 {
00718     r_public *p=0;
00719     r_contact *c=0,*cn=0;
00720     r_subscriber *s,*sn=0;
00721     int len,i;
00722     unsigned short k;
00723     char ch;
00724     str st;
00725     
00726     len = sizeof(r_public);
00727     p = (r_public*) shm_malloc(len);
00728     if (!p) {
00729         LOG(L_ERR,"ERR:"M_NAME":bin_decode_r_public: Error allocating %d bytes.\n",len);
00730         goto error;
00731     }
00732     memset(p,0,len);
00733     
00734     if (!bin_decode_str(x,&st)||!str_shm_dup(&(p->aor),&st)) goto error;
00735     if (!bin_decode_str(x,&st)||!str_shm_dup(&(p->early_ims_ip),&st)) goto error;
00736     p->hash = get_aor_hash(p->aor,r_hash_size);
00737     if (!bin_decode_char(x,&ch)) goto error;
00738     p->reg_state = ch;
00739     
00740     p->s = bin_decode_ims_subscription(x);
00741     if (!p->s) goto error;
00742 
00743     if (!bin_decode_str(x,&st)||!str_shm_dup(&(p->ccf1),&st)) goto error;
00744     if (!bin_decode_str(x,&st)||!str_shm_dup(&(p->ccf2),&st)) goto error;
00745     if (!bin_decode_str(x,&st)||!str_shm_dup(&(p->ecf1),&st)) goto error;
00746     if (!bin_decode_str(x,&st)||!str_shm_dup(&(p->ecf2),&st)) goto error;
00747     
00748     
00749     if (!bin_decode_ushort(x,&k)) goto error;
00750     for(i=0;i<k;i++){
00751         c = bin_decode_r_contact(x);
00752         if (!c) goto error;
00753         c->prev = p->tail;
00754         c->next = 0;
00755         if (!p->head) p->head = c;
00756         if (p->tail) p->tail->next = c;
00757         p->tail = c;
00758     }
00759 
00760     if (!bin_decode_ushort(x,&k)) goto error;
00761     for(i=0;i<k;i++){
00762         s = bin_decode_r_subscriber(x);
00763         if (!s) goto error;
00764         s->prev = p->stail;
00765         s->next = 0;
00766         if (!p->shead) p->shead = s;
00767         if (p->stail) p->stail->next = s;
00768         p->stail = s;
00769     }
00770     
00771     return p;
00772 error:
00773     LOG(L_ERR,"ERR:"M_NAME":bin_decode_r_public: Error while decoding (at %d (%04x)).\n",x->max,x->max);
00774     if (p) {
00775         if (p->aor.s) shm_free(p->aor.s);
00776         while(p->head){
00777             c = p->head;
00778             cn = c->next;
00779             free_r_contact(c);
00780             p->head = cn;
00781         }
00782         while(p->shead){
00783             s = p->shead;
00784             sn = s->next;
00785             free_r_subscriber(s);
00786             p->shead = sn;
00787         }
00788         shm_free(p);
00789     }
00790     return 0;
00791 }
00792 
00793 
00794 
00795 
00796 
00797 
00798 
00799 
00806  int bin_encode_auth_vector(bin_data *x,auth_vector *v)
00807 {
00808     char ch;
00809     if (!bin_encode_int(x,v->item_number)) goto error;
00810     if (!bin_encode_uchar(x,v->type)) goto error;
00811     if (!bin_encode_str(x,&(v->authenticate))) goto error;
00812     if (!bin_encode_str(x,&(v->authorization))) goto error;
00813     if (!bin_encode_str(x,&(v->ck))) goto error;
00814     if (!bin_encode_str(x,&(v->ik))) goto error;
00815     if (!bin_encode_time_t(x,v->expires)) goto error;
00816     ch = v->status;
00817     if (!bin_encode_char(x,ch)) goto error;
00818 
00819     return 1;
00820 error:
00821     LOG(L_ERR,"ERR:"M_NAME":bin_encode_auth_vector: Error while encoding.\n");
00822     return 0;       
00823 }
00824 
00830 auth_vector* bin_decode_auth_vector(bin_data *x)
00831 {
00832     auth_vector *v=0;
00833     int len;
00834     char ch;
00835     str s;
00836     
00837     len = sizeof(auth_vector);
00838     v = (auth_vector*) shm_malloc(len);
00839     if (!v) {
00840         LOG(L_ERR,"ERR:"M_NAME":bin_decode_auth_vector: Error allocating %d bytes.\n",len);
00841         goto error;
00842     }
00843     memset(v,0,len);
00844     
00845     if (!bin_decode_int(x,&(v->item_number))) goto error;
00846     if (!bin_decode_uchar(x,&(v->type))) goto error;
00847     if (!bin_decode_str(x,&s)||!str_shm_dup(&(v->authenticate),&s)) goto error;
00848     if (!bin_decode_str(x,&s)||!str_shm_dup(&(v->authorization),&s)) goto error;
00849     if (!bin_decode_str(x,&s)||!str_shm_dup(&(v->ck),&s)) goto error;
00850     if (!bin_decode_str(x,&s)||!str_shm_dup(&(v->ik),&s)) goto error;
00851 
00852     if (!bin_decode_time_t(x,   &(v->expires))) goto error;
00853     if (!bin_decode_char(x, &ch)) goto error;
00854     v->status=ch;
00855     
00856     return v;
00857 error:
00858     LOG(L_ERR,"ERR:"M_NAME":bin_decode_auth_vector: Error while decoding (at %d (%04x)).\n",x->max,x->max);
00859     if (v) {
00860         if (v->authenticate.s) shm_free(v->authenticate.s);
00861         if (v->authorization.s) shm_free(v->authorization.s);
00862         if (v->ck.s) shm_free(v->ck.s);
00863         if (v->ik.s) shm_free(v->ik.s);
00864         shm_free(v);
00865     }
00866     return 0;
00867 }
00868 
00869 
00870 
00871 
00872 
00879  int bin_encode_auth_userdata(bin_data *x,auth_userdata *u)
00880 {
00881     unsigned short k=0;
00882     auth_vector *v;
00883     
00884     if (!bin_encode_str(x,&(u->private_identity))) goto error;
00885     if (!bin_encode_str(x,&(u->public_identity))) goto error;
00886     if (!bin_encode_time_t(x,u->expires)) goto error;
00887     
00888     for(v=u->head;v;v=v->next)
00889         k++;
00890 
00891     if (!bin_encode_ushort(x,k)) goto error;
00892     for(v=u->head;v;v=v->next)
00893         if (!bin_encode_auth_vector(x,v))