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
00062 #include <time.h>
00063
00064 #include "mod.h"
00065 #include "registrar_storage.h"
00066
00067 #include "nat_helper.h"
00068 #include "security.h"
00069 #include "dlg_state.h"
00070
00071 time_t time_now;
00072 r_hash_slot *registrar=0;
00073 int r_hash_size=128;
00079 inline void r_act_time()
00080 {
00081 time_now=time(0);
00082 }
00083
00090 inline int r_valid_contact(r_contact *c)
00091 {
00092
00093 return (c->reg_state!=NOT_REGISTERED && c->reg_state!=DEREGISTERED)&&(c->expires>time_now);
00094 }
00095
00102 inline int r_reg_contact(r_contact *c)
00103 {
00104
00105 return (c->reg_state==REGISTERED)&&(c->expires>time_now);
00106 }
00107
00116 inline unsigned int get_contact_hash(str aor,int port,int transport,int hash_size)
00117 {
00118 #define h_inc h+=v^(v>>3)
00119 char* p;
00120 register unsigned v;
00121 register unsigned h;
00122
00123 h=0;
00124 for (p=aor.s; p<=(aor.s+aor.len-4); p+=4){
00125 v=(*p<<24)+(p[1]<<16)+(p[2]<<8)+p[3];
00126 h_inc;
00127 }
00128 v=0;
00129 for (;p<(aor.s+aor.len); p++) {
00130 v<<=8;
00131 v+=*p;
00132 }
00133 h_inc;
00134 v = port;
00135 h_inc;
00136
00137
00138
00139 h=((h)+(h>>11))+((h>>13)+(h>>23));
00140 return (h)%hash_size;
00141 #undef h_inc
00142 }
00143
00149 int r_storage_init(int hash_size)
00150 {
00151 int i;
00152 r_hash_size = hash_size;
00153
00154 registrar = shm_malloc(sizeof(r_hash_slot)*r_hash_size);
00155 memset(registrar,0,sizeof(r_hash_slot)*r_hash_size);
00156
00157 for(i=0;i<r_hash_size;i++){
00158 registrar[i].lock = lock_alloc();
00159 if (!registrar[i].lock){
00160 LOG(L_ERR,"ERR:"M_NAME":r_storage_init(): Error creating lock\n");
00161 return 0;
00162 }
00163 registrar[i].lock = lock_init(registrar[i].lock);
00164 }
00165
00166 if (!registrar) return 0;
00167
00168 return 1;
00169 }
00170
00174 void r_storage_destroy()
00175 {
00176 int i;
00177 r_contact *c,*nc;
00178 for(i=0;i<r_hash_size;i++){
00179 r_lock(i);
00180 c = registrar[i].head;
00181 while(c){
00182 nc = c->next;
00183 free_r_contact(c);
00184 c = nc;
00185 }
00186 r_unlock(i);
00187 lock_dealloc(registrar[i].lock);
00188 }
00189 shm_free(registrar);
00190 }
00191
00196 inline void r_lock(unsigned int hash)
00197 {
00198
00199 lock_get(registrar[hash].lock);
00200
00201 }
00206 inline void r_unlock(unsigned int hash)
00207 {
00208
00209 lock_release(registrar[hash].lock);
00210 }
00211
00219 r_public* new_r_public(str aor, int is_default)
00220 {
00221 r_public *p;
00222
00223 p = shm_malloc(sizeof(r_public));
00224 if (!p){
00225 LOG(L_ERR,"ERR:"M_NAME":new_r_public(): Unable to alloc %d bytes\n",
00226 sizeof(r_public));
00227 goto error;
00228 }
00229 memset(p,0,sizeof(r_public));
00230
00231 p->aor.s = shm_malloc(aor.len);
00232 if (!p->aor.s){
00233 LOG(L_ERR,"ERR:"M_NAME":new_r_public(): Unable to alloc %d bytes\n",
00234 aor.len);
00235 goto error;
00236 }
00237 p->aor.len = aor.len;
00238 memcpy(p->aor.s,aor.s,aor.len);
00239
00240 p->is_default = is_default;
00241
00242
00243 return p;
00244 error:
00245 if (p){
00246 if (p->aor.s) shm_free(p->aor.s);
00247 shm_free(p);
00248 }
00249 return 0;
00250 }
00251
00259 r_public* get_r_public(r_contact *c, str aor)
00260 {
00261 r_public *p=0;
00262 if (!c) return 0;
00263 p = c->head;
00264 while(p){
00265 if (p->aor.len == aor.len &&
00266 strncasecmp(p->aor.s,aor.s,aor.len)==0) return p;
00267 p = p->next;
00268 }
00269 return 0;
00270 }
00271
00272
00283 r_public* add_r_public(r_contact *c,str aor,int is_default)
00284 {
00285 r_public *p;
00286 if (!c) return 0;
00287 p = new_r_public(aor,is_default);
00288 if (!p) return 0;
00289 p->next=0;
00290 p->prev=c->tail;
00291 if (c->tail) c->tail->next = p;
00292 c->tail = p;
00293 if (!c->head) c->head=p;
00294
00295 return p;
00296 }
00297
00307 r_public* update_r_public(r_contact *c,str aor,int *is_default)
00308 {
00309 r_public *p;
00310
00311 if (!c) return 0;
00312 p = get_r_public(c,aor);
00313 if (!p){
00314 if (is_default)
00315 return add_r_public(c,aor,*is_default);
00316 else return 0;
00317 }else{
00318 if (is_default) p->is_default = *is_default;
00319 return p;
00320 }
00321 }
00322
00323
00332 void del_r_public(r_contact *c,r_public *p)
00333 {
00334 if (c->head == p) c->head = p->next;
00335 else p->prev->next = p->next;
00336 if (c->tail == p) c->tail = p->prev;
00337 else p->next->prev = p->prev;
00338 free_r_public(p);
00339 }
00340
00346 void free_r_public(r_public *p)
00347 {
00348 if (!p) return;
00349 if (p->aor.s) shm_free(p->aor.s);
00350 shm_free(p);
00351 }
00352
00370 r_ipsec* new_r_ipsec(int spi_uc,int spi_us,int spi_pc,int spi_ps,int port_uc,int port_us,
00371 str ealg_setkey,str r_ealg, str ck_esp,str alg_setkey,str r_alg, str ik_esp)
00372 {
00373 r_ipsec *ipsec;
00374
00375 ipsec = shm_malloc(sizeof(r_ipsec));
00376 if (!ipsec) {
00377 LOG(L_ERR,"ERR:"M_NAME":new_r_ipsec(): Unable to alloc %d bytes\n",
00378 sizeof(r_ipsec));
00379 return 0;
00380 }
00381 memset(ipsec,0,sizeof(r_ipsec));
00382
00383 ipsec->spi_uc = spi_uc;
00384 ipsec->spi_us = spi_us;
00385 ipsec->spi_pc = spi_pc;
00386 ipsec->spi_ps = spi_ps;
00387 ipsec->port_uc = port_uc;
00388 ipsec->port_us = port_us;
00389
00390
00391 STR_SHM_DUP(ipsec->ealg,ealg_setkey,"new_r_ipsec");
00392 STR_SHM_DUP(ipsec->r_ealg,r_ealg,"new_r_ipsec");
00393 STR_SHM_DUP(ipsec->alg,alg_setkey,"new_r_ipsec");
00394 STR_SHM_DUP(ipsec->r_alg,r_alg,"new_r_ipsec");
00395 STR_SHM_DUP(ipsec->ck,ck_esp,"new_r_ipsec");
00396 STR_SHM_DUP(ipsec->ik,ik_esp,"new_r_ipsec");
00397
00398 return ipsec;
00399 out_of_memory:
00400 if (ipsec) free_r_ipsec(ipsec);
00401 return 0;
00402 }
00403
00409 void free_r_ipsec(r_ipsec *ipsec)
00410 {
00411 if (!ipsec) return;
00412 if (ipsec->ealg.s) shm_free(ipsec->ealg.s);
00413 if (ipsec->r_ealg.s) shm_free(ipsec->r_ealg.s);
00414 if (ipsec->alg.s) shm_free(ipsec->alg.s);
00415 if (ipsec->r_alg.s) shm_free(ipsec->r_alg.s);
00416 if (ipsec->ck.s) shm_free(ipsec->ck.s);
00417 if (ipsec->ik.s) shm_free(ipsec->ik.s);
00418 shm_free(ipsec);
00419 }
00420
00429 r_tls* new_r_tls(int port_tls, unsigned long session_hash)
00430 {
00431 r_tls *tls;
00432
00433 tls = shm_malloc(sizeof(r_tls));
00434 if (!tls) {
00435 LOG(L_ERR,"ERR:"M_NAME":new_r_tls(): Unable to alloc %d bytes\n",
00436 sizeof(r_tls));
00437 return 0;
00438 }
00439 memset(tls,0,sizeof(r_tls));
00440
00441 tls->port_tls = port_tls;
00442 tls->session_hash = session_hash;
00443 return tls;
00444 }
00445
00451 void free_r_tls(r_tls *tls)
00452 {
00453 if (!tls) return;
00454 shm_free(tls);
00455 }
00456
00457 r_security *new_r_security(str sec_header,r_security_type type,float q)
00458 {
00459 r_security *s=0;
00460
00461 s = shm_malloc(sizeof(r_security));
00462 if (!s){
00463 LOG(L_ERR,"ERR:"M_NAME":save_contact_security: Error allocating %d bytes.\n",sizeof(r_security));
00464 goto error ;
00465 }
00466 memset(s,0,sizeof(r_security));
00467
00468 s->type = type;
00469 s->q = q;
00470 STR_SHM_DUP(s->sec_header,sec_header,"save_contact_security");
00471 if (!s->sec_header.s) goto error;
00472 return s;
00473 error:
00474 out_of_memory:
00475 if (s) shm_free(s);
00476 return 0;
00477 }
00478
00479
00480 void free_r_security(r_security *s)
00481 {
00482 if (!s) return;
00483 switch (s->type){
00484 case SEC_NONE:
00485 break;
00486 case SEC_TLS:
00487 if (s->data.tls) free_r_tls(s->data.tls);
00488 break;
00489 case SEC_IPSEC:
00490 if (s->data.ipsec) free_r_ipsec(s->data.ipsec);
00491 break;
00492 }
00493 if (s->sec_header.s) shm_free(s->sec_header.s);
00494 shm_free(s);
00495 }
00496
00505 r_contact* get_r_contact(str host,int port,int transport)
00506 {
00507 r_contact *c=0;
00508 unsigned int hash;
00509 if (!registrar) return 0;
00510 hash = get_contact_hash(host,port,transport,r_hash_size);
00511 r_lock(hash);
00512 c = registrar[hash].head;
00513 while(c){
00514 if (c->port == port &&
00515
00516 c->host.len == host.len &&
00517 strncasecmp(c->host.s,host.s,host.len)==0) return c;
00518 c = c->next;
00519 }
00520 r_unlock(hash);
00521 return 0;
00522 }
00523
00537 r_contact* new_r_contact(str host,int port,int transport,str uri,enum Reg_States reg_state,int expires,
00538 str *service_route,int service_route_cnt)
00539 {
00540 r_contact *c;
00541 int i;
00542
00543 c = shm_malloc(sizeof(r_contact));
00544 if (!c) {
00545 LOG(L_ERR,"ERR:"M_NAME":new_r_contact(): Unable to alloc %d bytes\n",
00546 sizeof(r_contact));
00547 goto error;
00548 }
00549 memset(c,0,sizeof(r_contact));
00550
00551 STR_SHM_DUP(c->host,host,"new_r_contact");
00552 c->port = port;
00553 c->transport = transport;
00554 c->hash = get_contact_hash(host,port,transport,r_hash_size);
00555 STR_SHM_DUP(c->uri,uri,"new_r_contact");
00556 c->reg_state = reg_state;
00557 c->expires = expires;
00558
00559 if (service_route_cnt && service_route){
00560 c->service_route = shm_malloc(service_route_cnt*sizeof(str));
00561 if (!c->service_route){
00562 LOG(L_ERR,"ERR:"M_NAME":new_r_contact(): Unable to alloc %d bytes\n",
00563 service_route_cnt*sizeof(str));
00564 goto error;
00565 }
00566 for(i=0;i<service_route_cnt;i++)
00567 STR_SHM_DUP(c->service_route[i],service_route[i],"new_r_contact");
00568 c->service_route_cnt = service_route_cnt;
00569 }
00570
00571 return c;
00572 error:
00573 out_of_memory:
00574 if (c){
00575 free_r_contact(c);
00576 }
00577 return 0;
00578 }
00579
00595 r_contact* add_r_contact(str host,int port,int transport,str uri,
00596 enum Reg_States reg_state,int expires,str *service_route,int service_route_cnt, r_nat_dest *pinhole)
00597 {
00598 r_contact *c;
00599
00600 if (!registrar) return 0;
00601 c = new_r_contact(host,port,transport,uri,reg_state, expires, service_route, service_route_cnt);
00602 if (!c) return 0;
00603 c->next=0;
00604 r_lock(c->hash);
00605 c->prev=registrar[c->hash].tail;
00606 if (c->prev) c->prev->next = c;
00607 registrar[c->hash].tail = c;
00608 if (!registrar[c->hash].head) registrar[c->hash].head=c;
00609 c->pinhole = pinhole;
00610 return c;
00611 }
00612
00628 r_contact* update_r_contact(str host,int port,int transport,
00629 str *uri,enum Reg_States *reg_state,int *expires,str **service_route,int *service_route_cnt, r_nat_dest **pinhole)
00630 {
00631 r_contact *c=0;
00632 int i;
00633
00634
00635 c = get_r_contact(host,port,transport);
00636 if (!c){
00637 if (uri&®_state && expires && service_route && service_route_cnt)
00638 return pinhole?add_r_contact(host,port,transport,*uri,*reg_state,*expires,*service_route,*service_route_cnt, *pinhole):
00639 add_r_contact(host,port,transport,*uri,*reg_state,*expires,*service_route,*service_route_cnt, 0);
00640 else return 0;
00641 }else{
00642
00643 if (c->reg_state==DEREGISTERED && reg_state && *reg_state==REGISTERED){
00644 while(c->head){
00645 del_r_public(c,c->head);
00646 }
00647 }
00648
00649 if (uri) {
00650 if (c->uri.s) shm_free(c->uri.s);
00651 STR_SHM_DUP(c->uri,*uri,"update_r_contact");
00652 }
00653 if (reg_state) c->reg_state = *reg_state;
00654 if (expires) c->expires = *expires;
00655 if (service_route){
00656 if (c->service_route){
00657 for(i=0;i<c->service_route_cnt;i++)
00658 if (c->service_route[i].s) shm_free(c->service_route[i].s);
00659 shm_free(c->service_route);
00660 c->service_route=0;
00661 c->service_route_cnt=0;
00662 }
00663 if (*service_route&&*service_route_cnt){
00664 c->service_route = shm_malloc((*service_route_cnt)*sizeof(str));
00665 if (!c->service_route){
00666 LOG(L_ERR,"ERR:"M_NAME":new_r_contact(): Unable to alloc %d bytes\n",
00667 (*service_route_cnt)*sizeof(str));
00668 }else{
00669 for(i=0;i<*service_route_cnt;i++)
00670 STR_SHM_DUP(c->service_route[i],(*service_route)[i],"new_r_contact");
00671 c->service_route_cnt = *service_route_cnt;
00672 }
00673 }
00674 }
00675 if (pinhole) c->pinhole = *pinhole;
00676 return c;
00677 }
00678
00679 out_of_memory:
00680 return c;
00681 }
00682
00694 r_contact* update_r_contact_sec(str host,int port,int transport,
00695 str *uri,enum Reg_States *reg_state,int *expires,
00696 r_security *s)
00697 {
00698 r_contact *c;
00699
00700 c = get_r_contact(host,port,transport);
00701 if (!c){
00702 if (uri&®_state){
00703 c = add_r_contact(host,port,transport,*uri,*reg_state,*expires,(str*) 0,0,0);
00704 c->security_temp = s;
00705 r_unlock(c->hash);
00706 return c;
00707 }
00708 else return 0;
00709 }else{
00710 if (c->security_temp){
00711 P_security_drop(c,c->security_temp);
00712 free_r_security(c->security_temp);
00713 }
00714 c->security_temp = s;
00715 r_unlock(c->hash);
00716 return c;
00717 }
00718 }
00719
00728 r_nat_dest* get_r_nat_pinhole(str host, int port, int transport) {
00729 r_contact * contact;
00730
00731 contact = get_r_contact(host, port, transport);
00732 if(contact != NULL) {
00733 return contact -> pinhole;
00734 }
00735 LOG(L_ERR, "ERR"M_NAME":get_r_nat_pinhole: cannot find contact %d://%.*s:%d\n", transport,host.len, host.s, port);
00736 return NULL;
00737 }
00738
00739
00745 void del_r_contact(r_contact *c)
00746 {
00747 P_drop_all_dialogs(c->host,c->port,c->transport);
00748 if (registrar[c->hash].head == c) registrar[c->hash].head = c->next;
00749 else c->prev->next = c->next;
00750 if (registrar[c->hash].tail == c) registrar[c->hash].tail = c->prev;
00751 else c->next->prev = c->prev;
00752 free_r_contact(c);
00753 }
00754
00755
00760 void free_r_contact(r_contact *c)
00761 {
00762 r_public *p,*n;
00763 int i;
00764
00765 if (!c) return;
00766 if (c->pinhole) shm_free(c->pinhole);
00767 if (c->host.s) shm_free(c->host.s);
00768 if (c->uri.s) shm_free(c->uri.s);
00769 if (c->service_route){
00770 i=0;
00771 for(i=0;i<c->service_route_cnt;i++)
00772 if (c->service_route[i].s)
00773 shm_free(c->service_route[i].s);
00774 shm_free(c->service_route);
00775 }
00776 p = c->head;
00777 while(p){
00778 n = p->next;
00779 free_r_public(p);
00780 p = n;
00781 }
00782 if (c->security_temp) free_r_security(c->security_temp);
00783 if (c->security) free_r_security(c->security);
00784 shm_free(c);
00785 }
00786
00787
00788
00789
00793 void print_r(int log_level)
00794 {
00795 r_public *p;
00796 r_contact *c;
00797 int i,j;
00798
00799 if (debug<log_level) return;
00800 r_act_time();
00801 LOG(log_level,"INF:"M_NAME":---------- Registrar Contents begin --------\n");
00802 if (!registrar) return;
00803 for(i=0;i<r_hash_size;i++){
00804 r_lock(i);
00805 c = registrar[i].head;
00806 while(c){
00807 LOG(log_level,ANSI_GREEN"INF:"M_NAME":[%4d] C: <"ANSI_RED"%d://%.*s:%d"ANSI_GREEN"> Exp:["ANSI_MAGENTA"%4ld"ANSI_GREEN"] R:["ANSI_MAGENTA"%2d"ANSI_GREEN"] <%.*s>\n",i,
00808 c->transport,c->host.len,c->host.s,c->port,
00809 c->expires-time_now,c->reg_state,
00810 c->uri.len,c->uri.s);
00811 for(j=0;j<c->service_route_cnt;j++)
00812 LOG(log_level,ANSI_GREEN"INF:"M_NAME": SR: <"ANSI_YELLOW"%.*s"ANSI_GREEN">\n",c->service_route[j].len,c->service_route[j].s);
00813 if(c->pinhole) {
00814 if (c->pinhole->nat_addr.len>=4)
00815 LOG(log_level,ANSI_GREEN"INF:"M_NAME": NAT:<"ANSI_CYAN"%d.%d.%d.%d:%d"ANSI_GREEN">\n",
00816 c->pinhole->nat_addr.u.addr[0],
00817 c->pinhole->nat_addr.u.addr[1],
00818 c->pinhole->nat_addr.u.addr[2],
00819 c->pinhole->nat_addr.u.addr[3],
00820 (unsigned short)c->pinhole->nat_port);
00821
00822 }
00823 if (c->security){
00824 switch(c->security->type){
00825 case SEC_NONE:
00826 break;
00827 case SEC_TLS:
00828 if (c->security->data.tls)
00829 LOG(log_level,ANSI_GREEN"INF:"M_NAME": TLS: %.*s tls://%.*s:%d\n",
00830 c->security->sec_header.len,c->security->sec_header.s,
00831 c->host.len,c->host.s,c->security->data.tls->port_tls);
00832 break;
00833 case SEC_IPSEC:
00834 if (c->security->data.ipsec)
00835 LOG(log_level,ANSI_GREEN"INF:"M_NAME": IPSec: UAS: %d:%d->%d UAC: %d:%d<-%d E[%.*s] I[%.*s]\n",
00836 c->security->data.ipsec->spi_uc,c->security->data.ipsec->port_uc,c->security->data.ipsec->spi_ps,
00837 c->security->data.ipsec->spi_us,c->security->data.ipsec->port_us,c->security->data.ipsec->spi_pc,
00838 c->security->data.ipsec->ealg.len,c->security->data.ipsec->ealg.s,c->security->data.ipsec->alg.len,c->security->data.ipsec->alg.s);
00839 }
00840 }
00841 if (c->security_temp){
00842 switch(c->security_temp->type){
00843 case SEC_NONE:
00844 break;
00845 case SEC_TLS:
00846 if (c->security_temp->data.tls)
00847 LOG(log_level,ANSI_GREEN"INF:"M_NAME": TLS: %.*s tls://%.*s:%d\n",
00848 c->security_temp->sec_header.len,c->security_temp->sec_header.s,
00849 c->host.len,c->host.s,c->security_temp->data.tls->port_tls);
00850 break;
00851 case SEC_IPSEC:
00852 if (c->security_temp->data.ipsec)
00853 LOG(log_level,ANSI_GREEN"INF:"M_NAME": Temp.IPSec: UAS: %d:%d->%d UAC: %d:%d<-%d E[%.*s] I[%.*s]\n",
00854 c->security_temp->data.ipsec->spi_uc,c->security_temp->data.ipsec->port_uc,c->security_temp->data.ipsec->spi_ps,
00855 c->security_temp->data.ipsec->spi_us,c->security_temp->data.ipsec->port_us,c->security_temp->data.ipsec->spi_pc,
00856 c->security_temp->data.ipsec->ealg.len,c->security_temp->data.ipsec->ealg.s,c->security_temp->data.ipsec->alg.len,c->security_temp->data.ipsec->alg.s);
00857 }
00858 }
00859
00860 p = c->head;
00861 while(p){
00862 LOG(log_level,ANSI_GREEN"INF:"M_NAME": P: D[%c] <"ANSI_BLUE"%.*s"ANSI_GREEN"> \n",
00863 (p->is_default?'X':' '),p->aor.len,p->aor.s);
00864 p = p->next;
00865 }
00866 c = c->next;
00867 }
00868 r_unlock(i);
00869 }
00870 LOG(log_level,"INF:"M_NAME":---------- Registrar Contents end ----------\n");
00871 }
00872