Changeset 427:3d9f300f3b49 in freeDiameter for extensions/app_sip/libdiamsip.c
- Timestamp:
- Jul 26, 2010, 9:39:27 PM (14 years ago)
- Branch:
- default
- Phase:
- public
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
extensions/app_sip/libdiamsip.c
r420 r427 166 166 } 167 167 168 //You must free ""result"" after using this function 168 169 169 void request_mysql(char *query) 170 170 { … … 186 186 } 187 187 188 //If password is null, we just verify this user exist 189 //We don't need the password length because it is a table' 190 int get_password(const unsigned char *username, const size_t usernamelen, char *password) 191 { 192 CHECK_PARAMS(username && usernamelen); 193 194 int not_found=2; 195 size_t querylen, usernamepurelen; 196 char *query, *username_pure; 197 198 switch(as_conf->datasource) 199 { 200 //MySQL 201 case ASMYSQL: 202 //We allocate the double size of username because at worst it can be all quotes 203 username_pure=malloc(usernamelen*2+1); 204 //We purify username not to have forbidden characters 205 usernamepurelen=mysql_real_escape_string(conn, username_pure, (const char *)username, usernamelen); 206 207 //We copy username in query 208 querylen=SQL_GETPASSWORD_LEN + usernamepurelen; 209 query = malloc(querylen+2); 210 snprintf(query, querylen+1, SQL_GETPASSWORD, username_pure); 211 212 MYSQL_RES *res; 213 MYSQL_ROW row; 214 215 //We make the query 216 request_mysql(query); 217 res=mysql_use_result(conn); 218 if(res==NULL) 219 { 220 if(password!=NULL) 221 password[0]='\0'; 222 free(query); 223 return 2; 224 } 225 226 227 228 while ((row = mysql_fetch_row(res)) != NULL) 229 { 230 if(strlen(row[0])>0) 231 { 232 if(password!=NULL) 233 strcpy(password,row[0]); 234 235 not_found=0; 236 break; 237 } 238 } 239 mysql_free_result(res); 240 free(query); 241 break; 242 } 243 return not_found; 244 } 245 246 int check_sipaor(const unsigned char *username, const size_t usernamelen, const char * sip_aor,const size_t sipaorlen) 247 { 248 CHECK_PARAMS(username && usernamelen && sip_aor && sipaorlen); 249 250 int not_found=2; 251 size_t querylen, usernamepurelen; 252 char *query, *username_pure; 253 254 switch(as_conf->datasource) 255 { 256 //MySQL 257 case ASMYSQL: 258 //We allocate the double size of username because at worst it can be all quotes 259 username_pure=malloc(usernamelen*2+1); 260 //We purify username not to have forbidden characters 261 usernamepurelen=mysql_real_escape_string(conn, username_pure, (const char *)username, usernamelen); 262 263 //We get the list of AOR owned by this user 264 querylen=SQL_GETSIPAOR_LEN + usernamepurelen; 265 query = malloc(querylen+2); 266 snprintf(query, querylen+1, SQL_GETSIPAOR, username_pure); 267 268 MYSQL_RES *res; 269 MYSQL_ROW row; 270 271 //We make the query 272 request_mysql(query); 273 res=mysql_use_result(conn); 274 if(res==NULL) 275 { 276 free(query); 277 return 2; 278 } 279 280 281 not_found=1; 282 while ((row = mysql_fetch_row(res)) != NULL) 283 { 284 if(strncmp((const char *)sip_aor,row[0],sipaorlen)==0) 285 { 286 not_found=0; 287 break; 288 } 289 } 290 mysql_free_result(res); 291 free(query); 292 break; 293 } 294 return not_found; 295 } 296 188 297 int get_diameter_uri(const unsigned char *sip_aor, const size_t sipaorlen, char ** diameter_uri, size_t *diameterurilen) 189 298 { 190 CHECK_PARAMS(sip_aor && sipaorlen); 191 192 size_t querylen, sipaorpurelen; 193 char *query, *sipaor_pure; 194 int not_found=1; 195 196 197 198 199 //a sip aor must begin by "sip:" or "sips:" so it must at least be longer than 4 chars 200 if(sipaorlen<5) 201 return 2; 202 203 //NOTE: each method has to purify sip_aor itself. You must remove quotes or special chars for security 204 205 switch(as_conf->datasource) 206 { 207 //MySQL 208 case ASMYSQL: 209 210 querylen=SQL_GETDIAMURI_LEN + sipaorlen; 211 212 213 //We allocate the double size of SIP-URI because at worst it can be all quotes 214 CHECK_MALLOC(sipaor_pure=malloc(sipaorlen*2+1)); 215 //We purify SIP-URI not to have forbidden characters 216 sipaorpurelen=mysql_real_escape_string(conn, sipaor_pure, (const char *)sip_aor, sipaorlen); 217 218 219 query = malloc(querylen+sipaorpurelen+ 2); 220 snprintf(query, querylen+1, SQL_GETDIAMURI, sipaor_pure); 221 222 MYSQL_RES *res; 223 MYSQL_ROW row; 224 225 226 //We make the query 227 request_mysql(query); 228 res=mysql_use_result(conn); 229 if(res==NULL) 230 { 231 //We couldn't make the request 232 diameter_uri=NULL; 299 CHECK_PARAMS(sip_aor && sipaorlen); 300 301 size_t querylen, sipaorpurelen; 302 char *query, *sipaor_pure; 303 int not_found=2; 304 305 306 307 308 //a sip aor must begin by "sip:" or "sips:" so it must at least be longer than 4 chars 309 if(sipaorlen<5) 233 310 return 2; 234 } 235 TRACE_DEBUG(INFO,"***********%d|%d****************\n%s\n*********************************",sipaorlen,sipaorpurelen,query); 236 while ((row = mysql_fetch_row(res)) != NULL) 237 { 238 *diameterurilen=strlen(row[0]); 239 if(*diameterurilen>0) 240 { 241 CHECK_MALLOC(*diameter_uri=malloc(*diameterurilen+1)); 242 strcpy(*diameter_uri,row[0]); 243 not_found=0; 244 break; 245 } 246 } 247 mysql_free_result(res); 248 free(query); 249 free(sipaor_pure); 250 break; 311 312 //NOTE: each method has to purify sip_aor itself. You must remove quotes or special chars for security 313 switch(as_conf->datasource) 314 { 315 //MySQL 316 case ASMYSQL: 317 318 querylen=SQL_GETDIAMURI_LEN + sipaorlen; 319 320 321 //We allocate the double size of SIP-URI because at worst it can be all quotes 322 CHECK_MALLOC(sipaor_pure=malloc(sipaorlen*2+1)); 323 //We purify SIP-URI not to have forbidden characters 324 sipaorpurelen=mysql_real_escape_string(conn, sipaor_pure, (const char *)sip_aor, sipaorlen); 325 326 327 query = malloc(querylen+sipaorpurelen+ 2); 328 snprintf(query, querylen+1, SQL_GETDIAMURI, sipaor_pure); 329 330 MYSQL_RES *res; 331 MYSQL_ROW row; 332 333 334 //We make the query 335 request_mysql(query); 336 res=mysql_use_result(conn); 337 if(res==NULL) 338 { 339 //We couldn't make the request 340 diameter_uri=NULL; 341 return 2; 342 } 343 344 while ((row = mysql_fetch_row(res)) != NULL) 345 { 346 *diameterurilen=strlen(row[0]); 347 if(*diameterurilen>0) 348 { 349 CHECK_MALLOC(*diameter_uri=malloc(*diameterurilen+1)); 350 strcpy(*diameter_uri,row[0]); 351 not_found=0; 352 break; 353 } 354 } 355 mysql_free_result(res); 356 free(query); 357 free(sipaor_pure); 358 break; 251 359 252 360 default: … … 265 373 266 374 375 int exist_username(const unsigned char *sip_aor, const size_t sipaorlen) 376 { 377 CHECK_PARAMS(sip_aor && sipaorlen); 378 379 size_t querylen, sipaorpurelen; 380 char *query, *sipaor_pure; 381 int not_found=1; 382 383 //a sip aor must begin by "sip:" or "sips:" so it must at least be longer than 4 chars 384 if(sipaorlen<5) 385 return 2; 386 387 //NOTE: each method has to purify sip_aor itself. You must remove quotes or special chars for security 388 389 switch(as_conf->datasource) 390 { 391 //MySQL 392 case ASMYSQL: 393 394 querylen=SQL_GETUSERNAME_LEN + sipaorlen; 395 396 397 //We allocate the double size of SIP-URI because at worst it can be all quotes 398 CHECK_MALLOC(sipaor_pure=malloc(sipaorlen*2+1)); 399 //We purify SIP-URI not to have forbidden characters 400 sipaorpurelen=mysql_real_escape_string(conn, sipaor_pure, (const char *)sip_aor, sipaorlen); 401 402 403 query = malloc(querylen+sipaorpurelen+ 2); 404 snprintf(query, querylen+1, SQL_GETUSERNAME, sipaor_pure); 405 406 MYSQL_RES *res; 407 MYSQL_ROW row; 408 409 410 //We make the query 411 request_mysql(query); 412 res=mysql_use_result(conn); 413 if(res==NULL) 414 { 415 //We couldn't make the request 416 return 2; 417 } 418 419 while ((row = mysql_fetch_row(res)) != NULL) 420 { 421 if(strlen(row[0])>0) 422 { 423 not_found=0; 424 break; 425 } 426 } 427 mysql_free_result(res); 428 free(query); 429 free(sipaor_pure); 430 break; 431 432 default: 433 434 //We must never go here, if so, we must stop diameter_sip 435 TRACE_DEBUG(INFO,"FATAL ERROR: the datasource is unknown, please check your config file!"); 436 return 2; 437 438 break; 439 } 440 441 //0 if it was found 442 return not_found; 443 444 } 445 446 //We check if this user can go in the given network 447 int allow_roaming(const unsigned char *username, const size_t usernamelen, const char * network,const size_t networklen) 448 { 449 CHECK_PARAMS(username && usernamelen && network && networklen); 450 451 int not_found=2; 452 size_t querylen, usernamepurelen; 453 char *query, *username_pure; 454 455 switch(as_conf->datasource) 456 { 457 //MySQL 458 case ASMYSQL: 459 //We allocate the double size of username because at worst it can be all quotes 460 username_pure=malloc(usernamelen*2+1); 461 //We purify username not to have forbidden characters 462 usernamepurelen=mysql_real_escape_string(conn, username_pure, (const char *)username, usernamelen); 463 464 //We get the list of AOR owned by this user 465 querylen=SQL_GETUSERNET_LEN + usernamepurelen; 466 query = malloc(querylen+2); 467 snprintf(query, querylen+1, SQL_GETUSERNET, username_pure); 468 469 470 MYSQL_RES *res; 471 MYSQL_ROW row; 472 473 474 //We make the query 475 request_mysql(query); 476 res=mysql_use_result(conn); 477 if(res==NULL) 478 { 479 free(query); 480 return 2; 481 } 482 483 484 not_found=1; 485 while ((row = mysql_fetch_row(res)) != NULL) 486 { 487 if(strncmp((const char *)network,row[0],networklen)==0) 488 { 489 not_found=0; 490 break; 491 } 492 } 493 mysql_free_result(res); 494 free(query); 495 break; 496 } 497 return not_found; 498 } 499 500 //SIP-Server-Capabilities for the SIP-AOR 501 int get_sipserver_cap(const unsigned char *sip_aor, const size_t sipaorlen, struct avp **capabilities) 502 { 503 CHECK_PARAMS(sip_aor && sipaorlen && capabilities); 504 505 size_t querylen, sipaorpurelen; 506 char *query, *sipaor_pure; 507 int not_found=2; 508 union avp_value value; 509 struct avp *avp; 510 511 //a sip aor must begin by "sip:" or "sips:" so it must at least be longer than 4 chars 512 if(sipaorlen<5) 513 return 2; 514 515 //NOTE: each method has to purify sip_aor itself. You must remove quotes or special chars for security 516 switch(as_conf->datasource) 517 { 518 //MySQL 519 case ASMYSQL: 520 521 querylen=SQL_GETSIPSERCAP_LEN + sipaorlen; 522 523 524 //We allocate the double size of SIP-URI because at worst it can be all quotes 525 CHECK_MALLOC(sipaor_pure=malloc(sipaorlen*2+1)); 526 //We purify SIP-URI not to have forbidden characters 527 sipaorpurelen=mysql_real_escape_string(conn, sipaor_pure, (const char *)sip_aor, sipaorlen); 528 529 530 query = malloc(querylen+sipaorpurelen+ 2); 531 snprintf(query, querylen+1, SQL_GETSIPSERCAP, sipaor_pure); 532 533 MYSQL_RES *res; 534 MYSQL_ROW row; 535 536 //We make the query 537 request_mysql(query); 538 res=mysql_use_result(conn); 539 if(res==NULL) 540 { 541 //We couldn't make the request 542 return 2; 543 } 544 not_found=1; 545 while ((row = mysql_fetch_row(res)) != NULL) 546 { 547 if(atoi(row[0])==1) 548 {//mandatory 549 CHECK_FCT( fd_msg_avp_new ( sip_dict.SIP_Mandatory_Capability, 0, &avp ) ); 550 value.i32=(uint32_t)atoi(row[1]); 551 CHECK_FCT( fd_msg_avp_setvalue ( avp, &value ) ); 552 CHECK_FCT( fd_msg_avp_add ( *capabilities, MSG_BRW_LAST_CHILD, avp) ); 553 554 } 555 else 556 {//optional 557 CHECK_FCT( fd_msg_avp_new ( sip_dict.SIP_Optional_Capability, 0, &avp ) ); 558 value.i32=(uint32_t)atoi(row[1]); 559 CHECK_FCT( fd_msg_avp_setvalue ( avp, &value ) ); 560 CHECK_FCT( fd_msg_avp_add ( *capabilities, MSG_BRW_LAST_CHILD, avp) ); 561 } 562 not_found=0; 563 } 564 565 mysql_free_result(res); 566 free(query); 567 free(sipaor_pure); 568 break; 569 570 default: 571 572 //We must never go here, if so, we must stop diameter_sip 573 TRACE_DEBUG(INFO,"FATAL ERROR: the datasource is unknown, please check your config file!"); 574 return 2; 575 576 break; 577 } 578 579 //0 if it was found 580 return not_found; 581 582 } 583 584 585 //We retrieve datatype 586 int get_user_datatype(const unsigned char *username, const size_t usernamelen, char **table_supported, const int num_elements, struct avp **groupedavp) 587 { 588 CHECK_PARAMS(table_supported && num_elements && username && usernamelen && groupedavp); 589 590 591 int counter=0, not_found=1; 592 union avp_value value; 593 struct avp *avp, *rootavp; 594 size_t querylen, usernamepurelen; 595 char *query, *username_pure; 596 597 if(num_elements<1) 598 return 1; 599 600 //NOTE: each method has to purify sip_aor itself. You must remove quotes or special chars for security 601 switch(as_conf->datasource) 602 { 603 //MySQL 604 case ASMYSQL: 605 606 querylen=SQL_GETUSEDATA_LEN + usernamelen; 607 608 609 //We allocate the double size of username because at worst it can be all quotes 610 username_pure=malloc(usernamelen*2+1); 611 //We purify username not to have forbidden characters 612 usernamepurelen=mysql_real_escape_string(conn, username_pure, (const char *)username, usernamelen); 613 614 615 query = malloc(querylen+usernamelen+ 2); 616 snprintf(query, querylen+1, SQL_GETUSEDATA, username_pure); 617 618 MYSQL_RES *res; 619 MYSQL_ROW row; 620 621 //We make the query 622 request_mysql(query); 623 res=mysql_use_result(conn); 624 if(res==NULL) 625 { 626 //We couldn't make the request 627 return 2; 628 } 629 not_found=1; 630 631 counter=0; 632 unsigned long *length=0; 633 634 //int index=0;//current field number 635 636 while ((row = mysql_fetch_row(res)) != NULL) 637 { 638 length=mysql_fetch_lengths(res); 639 640 for(counter=0;counter<num_elements; counter++) 641 { 642 //TODO: check length 643 if(strcmp(table_supported[counter],row[0])) 644 { 645 CHECK_FCT( fd_msg_avp_new ( sip_dict.SIP_User_Data, 0, &rootavp ) ); 646 647 CHECK_FCT( fd_msg_avp_new ( sip_dict.SIP_User_Data_Type, 0, &avp ) ); 648 value.os.data=(unsigned char *)table_supported[counter]; 649 value.os.len=strlen(table_supported[counter]); 650 CHECK_FCT( fd_msg_avp_setvalue ( avp, &value ) ); 651 CHECK_FCT( fd_msg_avp_add ( rootavp, MSG_BRW_LAST_CHILD, avp) ); 652 //This was used 653 table_supported[counter]=NULL; 654 655 CHECK_FCT( fd_msg_avp_new ( sip_dict.SIP_User_Data_Contents, 0, &avp ) ); 656 CHECK_MALLOC(value.os.data=malloc((length[1])*sizeof(unsigned char))); 657 memcpy(value.os.data,row[1],length[1]); 658 value.os.len=(size_t)(length[1]*sizeof(unsigned char)); 659 CHECK_FCT( fd_msg_avp_setvalue ( avp, &value ) ); 660 CHECK_FCT( fd_msg_avp_add ( rootavp, MSG_BRW_LAST_CHILD, avp) ); 661 662 CHECK_FCT( fd_msg_avp_add ( *groupedavp, MSG_BRW_LAST_CHILD, rootavp) ); 663 not_found=0; 664 } 665 } 666 //index++; 667 } 668 669 mysql_free_result(res); 670 free(query); 671 free(username_pure); 672 break; 673 674 default: 675 676 //We must never go here, if so, we must stop diameter_sip 677 TRACE_DEBUG(INFO,"FATAL ERROR: the datasource is unknown, please check your config file!"); 678 return 2; 679 680 break; 681 } 682 683 //0 if it was found 684 return not_found; 685 686 } 687 688 int set_pending_flag(const unsigned char *username, const size_t usernamelen) 689 { 690 CHECK_PARAMS(username && usernamelen); 691 692 int not_found=2; 693 size_t querylen, usernamepurelen; 694 char *query, *username_pure; 695 696 switch(as_conf->datasource) 697 { 698 //MySQL 699 case ASMYSQL: 700 //We allocate the double size of username because at worst it can be all quotes 701 username_pure=malloc(usernamelen*2+1); 702 //We purify username not to have forbidden characters 703 usernamepurelen=mysql_real_escape_string(conn, username_pure, (const char *)username, usernamelen); 704 705 706 707 //We clear the flag "authentication pending" 708 querylen=SQL_SETFLAG_LEN + usernamepurelen; 709 query = malloc(querylen+2); 710 snprintf(query, querylen+1, SQL_SETFLAG, username_pure); 711 712 if (mysql_query(conn, query)) 713 { 714 TRACE_DEBUG(INFO,"Query %s failed", query); 715 free(query); 716 return 2; 717 } 718 719 free(query); 720 break; 721 } 722 return 0; 723 } 724 int clear_pending_flag(const unsigned char *username, const size_t usernamelen) 725 { 726 CHECK_PARAMS(username && usernamelen); 727 728 int not_found=2; 729 size_t querylen, usernamepurelen; 730 char *query, *username_pure; 731 732 switch(as_conf->datasource) 733 { 734 //MySQL 735 case ASMYSQL: 736 //We allocate the double size of username because at worst it can be all quotes 737 username_pure=malloc(usernamelen*2+1); 738 //We purify username not to have forbidden characters 739 usernamepurelen=mysql_real_escape_string(conn, username_pure, (const char *)username, usernamelen); 740 741 742 743 //We clear the flag "authentication pending" 744 querylen=SQL_CLEARFLAG_LEN + usernamepurelen; 745 query = malloc(querylen+2); 746 snprintf(query, querylen+1, SQL_CLEARFLAG, username_pure); 747 748 if (mysql_query(conn, query)) 749 { 750 TRACE_DEBUG(INFO,"Query %s failed", query); 751 free(query); 752 return 2; 753 } 754 755 free(query); 756 break; 757 } 758 return 0; 759 } 760 761 762 763 int set_sipserver_uri(const unsigned char *username, const size_t usernamelen, const unsigned char *sipserver_uri,const size_t sipserverurilen) 764 { 765 CHECK_PARAMS(username && usernamelen && sipserver_uri && sipserverurilen); 766 767 int not_found=2; 768 size_t querylen, usernamepurelen, sipserveruripurelen; 769 char *query, *username_pure, *sipserveruri_pure; 770 771 switch(as_conf->datasource) 772 { 773 //MySQL 774 case ASMYSQL: 775 //We allocate the double size of username because at worst it can be all quotes 776 username_pure=malloc(usernamelen*2+1); 777 //We purify username not to have forbidden characters 778 usernamepurelen=mysql_real_escape_string(conn, username_pure, (const char *)username, usernamelen); 779 780 //We allocate the double size of username because at worst it can be all quotes 781 sipserveruri_pure=malloc(sipserverurilen*2+1); 782 //We purify username not to have forbidden characters 783 sipserveruripurelen=mysql_real_escape_string(conn, sipserveruri_pure, (const char *)sipserver_uri, sipserverurilen); 784 785 //We clear the flag "authentication pending" 786 querylen=SQL_SETSIPURI_LEN + usernamepurelen + sipserveruripurelen; 787 query = malloc(querylen+2); 788 snprintf(query, querylen+1, SQL_SETSIPURI, sipserveruri_pure,username_pure); 789 790 if (mysql_query(conn, query)) 791 { 792 TRACE_DEBUG(INFO,"Query %s failed", query); 793 free(query); 794 return 2; 795 } 796 797 free(query); 798 break; 799 } 800 return 0; 801 } 802 int remove_sipserver_uri(const unsigned char *sipserver_uri,const size_t sipserverurilen) 803 { 804 CHECK_PARAMS(sipserver_uri && sipserverurilen); 805 806 int not_found=2; 807 size_t querylen, sipserveruripurelen; 808 char *query, *sipserveruri_pure; 809 810 switch(as_conf->datasource) 811 { 812 //MySQL 813 case ASMYSQL: 814 //We allocate the double size of username because at worst it can be all quotes 815 sipserveruri_pure=malloc(sipserverurilen*2+1); 816 //We purify username not to have forbidden characters 817 sipserveruripurelen=mysql_real_escape_string(conn, sipserveruri_pure, (const char *)sipserver_uri, sipserverurilen); 818 819 //We clear the flag "authentication pending" 820 querylen=SQL_RMSIPURI_LEN + sipserveruripurelen; 821 query = malloc(querylen+2); 822 snprintf(query, querylen+1, SQL_RMSIPURI, sipserveruri_pure); 823 824 if (mysql_query(conn, query)) 825 { 826 TRACE_DEBUG(INFO,"Query %s failed", query); 827 free(query); 828 return 2; 829 } 830 831 free(query); 832 break; 833 } 834 return 0; 835 } 836 int set_real_sipserver_uri(const unsigned char *username, const size_t usernamelen, const unsigned char *sipserver_uri,const size_t sipserverurilen) 837 { 838 CHECK_PARAMS(username && usernamelen && sipserver_uri && sipserverurilen); 839 840 int not_found=2; 841 size_t querylen, usernamepurelen, sipserveruripurelen; 842 char *query, *username_pure, *sipserveruri_pure; 843 844 switch(as_conf->datasource) 845 { 846 //MySQL 847 case ASMYSQL: 848 //We allocate the double size of username because at worst it can be all quotes 849 username_pure=malloc(usernamelen*2+1); 850 //We purify username not to have forbidden characters 851 usernamepurelen=mysql_real_escape_string(conn, username_pure, (const char *)username, usernamelen); 852 853 //We allocate the double size of username because at worst it can be all quotes 854 sipserveruri_pure=malloc(sipserverurilen*2+1); 855 //We purify username not to have forbidden characters 856 sipserveruripurelen=mysql_real_escape_string(conn, sipserveruri_pure, (const char *)sipserver_uri, sipserverurilen); 857 858 //We clear the flag "authentication pending" 859 querylen=SQL_SETREALSIPURI_LEN + usernamepurelen + sipserveruripurelen; 860 query = malloc(querylen+2); 861 snprintf(query, querylen+1, SQL_SETREALSIPURI, sipserveruri_pure,username_pure); 862 863 if (mysql_query(conn, query)) 864 { 865 TRACE_DEBUG(INFO,"Query %s failed", query); 866 free(query); 867 return 2; 868 } 869 870 free(query); 871 break; 872 } 873 return 0; 874 } 875 876 int get_sipserver_uri(const unsigned char *sip_aor, const size_t sipaorlen, char ** sipserver_uri, size_t *sipserverurilen) 877 { 878 CHECK_PARAMS(sip_aor && sipaorlen && sipserver_uri && sipserverurilen ); 879 880 size_t querylen, sipaorpurelen; 881 char *query, *sipaor_pure; 882 int not_found=2; 883 884 885 886 887 //a sip aor must begin by "sip:" or "sips:" so it must at least be longer than 4 chars 888 if(sipaorlen<5) 889 return 2; 890 891 //NOTE: each method has to purify sip_aor itself. You must remove quotes or special chars for security 892 893 switch(as_conf->datasource) 894 { 895 //MySQL 896 case ASMYSQL: 897 898 querylen=SQL_GETSIPSERURI_LEN + sipaorlen; 899 900 //We allocate the double size of SIP-URI because at worst it can be all quotes 901 CHECK_MALLOC(sipaor_pure=malloc(sipaorlen*2+1)); 902 //We purify SIP-URI not to have forbidden characters 903 sipaorpurelen=mysql_real_escape_string(conn, sipaor_pure, (const char *)sip_aor, sipaorlen); 904 905 906 query = malloc(querylen+sipaorpurelen+ 2); 907 snprintf(query, querylen+1, SQL_GETSIPSERURI, sipaor_pure); 908 909 MYSQL_RES *res; 910 MYSQL_ROW row; 911 912 //We make the query 913 request_mysql(query); 914 res=mysql_use_result(conn); 915 if(res==NULL) 916 { 917 //We couldn't make the request 918 sipserver_uri=NULL; 919 return 2; 920 } 921 922 not_found=1; 923 while ((row = mysql_fetch_row(res)) != NULL) 924 { 925 *sipserverurilen=strlen(row[0]); 926 if(*sipserverurilen>4) 927 { 928 CHECK_MALLOC(*sipserver_uri=malloc(*sipserverurilen+1)); 929 strcpy(*sipserver_uri,row[0]); 930 not_found=0; 931 break; 932 } 933 } 934 mysql_free_result(res); 935 free(query); 936 free(sipaor_pure); 937 break; 938 939 default: 940 941 //We must never go here, if so, we must stop diameter_sip 942 TRACE_DEBUG(INFO,"FATAL ERROR: the datasource is unknown, please check your config file!"); 943 sipserver_uri=NULL; 944 return 2; 945 946 break; 947 } 948 949 //0 if it was found 950 return not_found; 951 952 } 953 954 int count_sipaor(const struct msg * message) 955 { 956 CHECK_PARAMS(message); 957 958 struct avp_hdr *temphdr; 959 struct avp *avp; 960 int counter=0; 961 962 CHECK_FCT(fd_msg_browse ( &message, MSG_BRW_WALK, &avp, NULL)); 963 964 while(avp!=NULL) 965 { 966 967 CHECK_FCT( fd_msg_avp_hdr( avp,&temphdr )); 968 969 if(temphdr->avp_code==122 && temphdr->avp_vendor==0) 970 { 971 counter++; 972 } 973 974 CHECK_FCT(fd_msg_browse ( &message, MSG_BRW_WALK, &avp, NULL)); 975 } 976 return counter; 977 } 978 int count_supporteddatatype(const struct msg * message) 979 { 980 CHECK_PARAMS(message); 981 982 struct avp_hdr *temphdr; 983 struct avp *avp; 984 int counter=0; 985 986 CHECK_FCT(fd_msg_browse ( &message, MSG_BRW_WALK, &avp, NULL)); 987 988 while(avp!=NULL) 989 { 990 991 CHECK_FCT( fd_msg_avp_hdr( avp,&temphdr )); 992 993 if(temphdr->avp_code==388 && temphdr->avp_vendor==0) 994 { 995 counter++; 996 } 997 998 CHECK_FCT(fd_msg_browse ( &message, MSG_BRW_WALK, &avp, NULL)); 999 } 1000 return counter; 1001 } 267 1002 /* 268 1003 void nonce_add_element(char * nonce)
Note: See TracChangeset
for help on using the changeset viewer.