1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945
|
/**
* $Id$
*
* Copyright (C) 2011 Daniel-Constantin Mierla (asipto.com)
*
* This file is part of Kamailio, a free SIP server.
*
* Kamailio is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version
*
* Kamailio is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/stat.h>
#include "../../sr_module.h"
#include "../../dprint.h"
#include "../../ut.h"
#include "../../mem/mem.h"
#include "../../data_lump.h"
#include "../../data_lump_rpl.h"
#include "../../lib/kcore/cmpapi.h"
#include "app_mono_api.h"
#include <mono/metadata/mono-config.h>
#define SRVERSION "1.0"
/**
*
*/
typedef struct _sr_mono_load
{
char *script;
MonoDomain *domain;
MonoAssembly *assembly;
struct _sr_mono_load *next;
} sr_mono_load_t;
static sr_mono_load_t *_sr_mono_load_list = NULL;
int sr_mono_load_class_core();
int sr_mono_load_class_pv();
int sr_mono_load_class_hdr();
/**
*
*/
static sr_mono_env_t _sr_M_env;
/**
* @return the static Mono env
*/
sr_mono_env_t *sr_mono_env_get(void)
{
return &_sr_M_env;
}
/**
*
*/
int sr_mono_load_script(char *script)
{
sr_mono_load_t *mi;
if(_sr_mono_load_list != NULL)
{
LM_ERR("only one assembly can be loaded\n");
return -1;
}
mi = (sr_mono_load_t*)pkg_malloc(sizeof(sr_mono_load_t));
if(mi==NULL)
{
LM_ERR("no more pkg\n");
return -1;
}
memset(mi, 0, sizeof(sr_mono_load_t));
mi->script = script;
mi->next = _sr_mono_load_list;
_sr_mono_load_list = mi;
return 0;
}
/**
*
*/
int sr_mono_assembly_loaded(void)
{
if(_sr_mono_load_list != NULL)
return 1;
return 0;
}
/**
*
*/
int sr_mono_register_module(char *mname)
{
// if(mono_sr_exp_register_mod(mname)==0)
// return 0;
return -1;
}
/**
*
*/
int mono_sr_init_mod(void)
{
memset(&_sr_M_env, 0, sizeof(sr_mono_env_t));
// if(mono_sr_exp_init_mod()<0)
// return -1;
return 0;
}
/**
*
*/
int mono_sr_init_load(void)
{
sr_mono_load_t *mi;
if(_sr_mono_load_list == NULL) {
LM_DBG("no assembly to load\n");
return 0;
}
mono_config_parse (NULL);
mi = _sr_mono_load_list;
if(mi->domain != NULL)
{
LM_ERR("worker mono environment already initialized\n");
return 0;
}
while(mi!=NULL)
{
mi->domain = mono_jit_init (mi->script);
if (mi->domain==NULL) {
LM_ERR("failed to init domain for: %s\n", mi->script);
return -1;
}
sr_mono_load_class_core();
sr_mono_load_class_pv();
sr_mono_load_class_hdr();
mi->assembly = mono_domain_assembly_open(mi->domain, mi->script);
if (mi->assembly==NULL) {
LM_ERR("failed to open assembly: %s\n", mi->script);
return -1;
}
mi = mi->next;
/* only one (first) assembly for now */
break;
}
return 0;
}
/**
*
*/
int mono_sr_init_probe(void)
{
/* todo: test if assemblies exist */
return 0;
}
/**
*
*/
int mono_sr_init_child(void)
{
memset(&_sr_M_env, 0, sizeof(sr_mono_env_t));
/*
* Load the default Mono configuration file, this is needed
* if you are planning on using the dllmaps defined on the
* system configuration
*/
mono_config_parse (NULL);
return 0;
}
/**
*
*/
void mono_sr_destroy(void)
{
memset(&_sr_M_env, 0, sizeof(sr_mono_env_t));
}
/**
*
*/
int mono_sr_initialized(void)
{
if(_sr_M_env.domain==NULL)
return 1;
return 1;
}
/**
*
*/
int app_mono_exec(struct sip_msg *msg, char *script, char *param)
{
int ret;
char *argv[2];
int argc;
argc = 1;
argv[0] = script;
if(param!=NULL) {
argc++;
argv[1] = param;
}
LM_DBG("executing Mono assembly: [[%s]]\n", argv[0]);
_sr_M_env.msg = msg;
mono_config_parse (NULL);
/*
* mono_jit_init() creates a domain: each assembly is
* loaded and run in a MonoDomain.
*/
_sr_M_env.domain = mono_jit_init (argv[0]);
/*
* We add our special internal functions, so that C# code
* can call us back.
*/
sr_mono_load_class_core();
sr_mono_load_class_pv();
sr_mono_load_class_hdr();
_sr_M_env.assembly = mono_domain_assembly_open(_sr_M_env.domain, argv[0]);
if (_sr_M_env.assembly==NULL) {
ret = -1;
goto done;
}
/*
* mono_jit_exec() will run the Main() method in the assembly.
* The return value needs to be looked up from
* System.Environment.ExitCode.
*/
mono_jit_exec(_sr_M_env.domain, _sr_M_env.assembly, argc, argv);
ret = mono_environment_exitcode_get();
LM_DBG("returned code from mono environment: %d\n", ret);
done:
mono_jit_cleanup(_sr_M_env.domain);
memset(&_sr_M_env, 0, sizeof(sr_mono_env_t));
return (ret==0)?1:-1;
}
/**
*
*/
int app_mono_run(struct sip_msg *msg, char *arg)
{
int ret;
char *argv[2];
int argc;
sr_mono_load_t *mi;
if(_sr_mono_load_list == NULL)
return -1;
mi = _sr_mono_load_list;
LM_DBG("running Mono assembly: [[%s]]\n", mi->script);
_sr_M_env.msg = msg;
_sr_M_env.domain = mi->domain;
_sr_M_env.assembly = mi->assembly;
if (_sr_M_env.assembly==NULL) {
LM_DBG("empty assembly\n");
memset(&_sr_M_env, 0, sizeof(sr_mono_env_t));
return -1;
}
mono_domain_set(_sr_M_env.domain, 0);
argc = 1;
argv[0] = mi->script;
if(arg!=NULL) {
argc++;
argv[1] = arg;
}
mono_jit_exec(_sr_M_env.domain, _sr_M_env.assembly, argc, argv);
ret = mono_environment_exitcode_get();
LM_DBG("returned code from mono environment: %d\n", ret);
memset(&_sr_M_env, 0, sizeof(sr_mono_env_t));
return (ret==0)?1:-1;
}
/**
*
*/
static MonoString* sr_mono_api_version() {
return mono_string_new (mono_domain_get(), SRVERSION);
}
static void sr_mono_log(int level, MonoString *text) {
char *logmsg;
logmsg = mono_string_to_utf8(text);
LOG(level, "%s", logmsg);
mono_free(logmsg);
}
static void sr_mono_err(MonoString *text) {
char *logmsg;
logmsg = mono_string_to_utf8(text);
LOG(L_ERR, "%s", logmsg);
mono_free(logmsg);
}
static void sr_mono_dbg(MonoString *text) {
char *logmsg;
logmsg = mono_string_to_utf8(text);
LOG(L_DBG, "%s", logmsg);
mono_free(logmsg);
}
/**
*
*/
static int sr_mono_modf(MonoString *nfunc)
{
int ret;
int mod_type;
struct run_act_ctx ra_ctx;
unsigned modver;
struct action *act = NULL;
sr31_cmd_export_t* expf;
sr_mono_env_t *env_M;
char *func = NULL;
env_M = sr_mono_env_get();
if(env_M->msg==NULL)
goto error;
func = mono_string_to_utf8(nfunc);
expf = find_export_record(func, 0, 0, &modver);
if (expf==NULL) {
LM_ERR("function '%s' is not available\n", func);
goto error;
}
/* check fixups */
if (expf->fixup!=NULL && expf->free_fixup==NULL) {
LM_ERR("function '%s' has fixup - cannot be used\n", func);
goto error;
}
mod_type = MODULE0_T;
act = mk_action(mod_type, 1 /* number of (type, value) pairs */,
MODEXP_ST, expf, /* function */
NUMBER_ST, 0, /* parameter number */
STRING_ST, NULL, /* param. 1 */
STRING_ST, NULL, /* param. 2 */
STRING_ST, NULL, /* param. 3 */
STRING_ST, NULL, /* param. 4 */
STRING_ST, NULL, /* param. 5 */
STRING_ST, NULL /* param. 6 */
);
if (act==NULL) {
LM_ERR("action structure could not be created for '%s'\n", func);
goto error;
}
/* handle fixups */
if (expf->fixup) {
/* no parameters */
if(expf->fixup(0, 0)<0)
{
LM_ERR("Error in fixup (0) for '%s'\n", func);
goto error;
}
}
init_run_actions_ctx(&ra_ctx);
ret = do_action(&ra_ctx, act, env_M->msg);
pkg_free(act);
mono_free(func);
return ret;
error:
if(func!=NULL)
mono_free(func);
if(act!=NULL)
pkg_free(act);
return -127;
}
static const sr_M_export_t _sr_M_export_core[] = {
{"SR.Core::APIVersion", sr_mono_api_version},
{"SR.Core::Log", sr_mono_log},
{"SR.Core::Err", sr_mono_err},
{"SR.Core::Dbg", sr_mono_dbg},
{"SR.Core::ModF", sr_mono_modf},
{NULL, NULL}
};
int sr_mono_load_class_core()
{
int i;
for(i=0; _sr_M_export_core[i].name!=NULL; i++)
mono_add_internal_call(_sr_M_export_core[i].name, _sr_M_export_core[i].method);
return 0;
}
/**
*
*/
static MonoString *sr_mono_pv_gets (MonoString *pv)
{
str pvn = {0};
pv_spec_t *pvs;
pv_value_t val;
sr_mono_env_t *env_M;
int pl;
env_M = sr_mono_env_get();
pvn.s = mono_string_to_utf8(pv);
if(pvn.s==NULL || env_M->msg==NULL)
goto error;
pvn.len = strlen(pvn.s);
LM_DBG("pv get: %s\n", pvn.s);
pl = pv_locate_name(&pvn);
if(pl != pvn.len)
{
LM_ERR("invalid pv [%s] (%d/%d)\n", pvn.s, pl, pvn.len);
goto error;
}
pvs = pv_cache_get(&pvn);
if(pvs==NULL)
{
LM_ERR("cannot get pv spec for [%s]\n", pvn.s);
goto error;
}
memset(&val, 0, sizeof(pv_value_t));
if(pv_get_spec_value(env_M->msg, pvs, &val) != 0)
{
LM_ERR("unable to get pv value for [%s]\n", pvn.s);
goto error;
}
if((val.flags&PV_VAL_NULL) || !(val.flags&PV_VAL_STR))
{
mono_free(pvn.s);
return NULL;
}
mono_free(pvn.s);
return mono_string_new_len (mono_domain_get(), val.rs.s, val.rs.len);
error:
if(pvn.s!=NULL)
mono_free(pvn.s);
return NULL;
}
/**
*
*/
static int sr_mono_pv_geti (MonoString *pv)
{
str pvn = {0};
pv_spec_t *pvs;
pv_value_t val;
int pl;
sr_mono_env_t *env_M;
env_M = sr_mono_env_get();
pvn.s = mono_string_to_utf8(pv);
if(pvn.s==NULL || env_M->msg==NULL)
goto error;
pvn.len = strlen(pvn.s);
LM_DBG("pv get: %s\n", pvn.s);
pl = pv_locate_name(&pvn);
if(pl != pvn.len)
{
LM_ERR("invalid pv [%s] (%d/%d)\n", pvn.s, pl, pvn.len);
goto error;
}
pvs = pv_cache_get(&pvn);
if(pvs==NULL)
{
LM_ERR("cannot get pv spec for [%s]\n", pvn.s);
goto error;
}
memset(&val, 0, sizeof(pv_value_t));
if(pv_get_spec_value(env_M->msg, pvs, &val) != 0)
{
LM_ERR("unable to get pv value for [%s]\n", pvn.s);
goto error;
}
if((val.flags&PV_VAL_NULL) || !(val.flags&PV_TYPE_INT))
{
mono_free(pvn.s);
return 0;
}
mono_free(pvn.s);
return val.ri;
error:
if(pvn.s!=NULL)
mono_free(pvn.s);
return 0;
}
/**
*
*/
static int sr_mono_pv_seti (MonoString *pv, int iv)
{
str pvn = {0};
pv_spec_t *pvs;
pv_value_t val;
int pl;
sr_mono_env_t *env_M;
env_M = sr_mono_env_get();
pvn.s = mono_string_to_utf8(pv);
if(pvn.s==NULL || env_M->msg==NULL)
goto error;
memset(&val, 0, sizeof(pv_value_t));
val.ri = iv;
val.flags |= PV_TYPE_INT|PV_VAL_INT;
pvn.len = strlen(pvn.s);
LM_DBG("pv set: %s\n", pvn.s);
pl = pv_locate_name(&pvn);
if(pl != pvn.len)
{
LM_ERR("invalid pv [%s] (%d/%d)\n", pvn.s, pl, pvn.len);
goto error;
}
pvs = pv_cache_get(&pvn);
if(pvs==NULL)
{
LM_ERR("cannot get pv spec for [%s]\n", pvn.s);
goto error;
}
if(pv_set_spec_value(env_M->msg, pvs, 0, &val)<0)
{
LM_ERR("unable to set pv [%s]\n", pvn.s);
goto error;
}
mono_free(pvn.s);
return 0;
error:
if(pvn.s!=NULL)
mono_free(pvn.s);
return -1;
}
/**
*
*/
static int sr_mono_pv_sets (MonoString *pv, MonoString *sv)
{
str pvn = {0};
pv_spec_t *pvs;
pv_value_t val;
int pl;
sr_mono_env_t *env_M;
env_M = sr_mono_env_get();
pvn.s = mono_string_to_utf8(pv);
if(pvn.s==NULL || env_M->msg==NULL)
goto error;
memset(&val, 0, sizeof(pv_value_t));
val.rs.s = mono_string_to_utf8(sv);
if(val.rs.s == NULL)
goto error;
val.rs.len = strlen(val.rs.s);
val.flags |= PV_VAL_STR;
pvn.len = strlen(pvn.s);
LM_DBG("pv set: %s\n", pvn.s);
pl = pv_locate_name(&pvn);
if(pl != pvn.len)
{
LM_ERR("invalid pv [%s] (%d/%d)\n", pvn.s, pl, pvn.len);
goto error;
}
pvs = pv_cache_get(&pvn);
if(pvs==NULL)
{
LM_ERR("cannot get pv spec for [%s]\n", pvn.s);
goto error;
}
if(pv_set_spec_value(env_M->msg, pvs, 0, &val)<0)
{
LM_ERR("unable to set pv [%s]\n", pvn.s);
return -1;
}
mono_free(pvn.s);
return 0;
error:
if(pvn.s!=NULL)
mono_free(pvn.s);
return -1;
}
/**
*
*/
static int sr_mono_pv_unset (MonoString *pv)
{
str pvn = {0};
pv_spec_t *pvs;
pv_value_t val;
int pl;
sr_mono_env_t *env_M;
env_M = sr_mono_env_get();
pvn.s = mono_string_to_utf8(pv);
if(pvn.s==NULL || env_M->msg==NULL)
goto error;
pvn.len = strlen(pvn.s);
LM_DBG("pv unset: %s\n", pvn.s);
pl = pv_locate_name(&pvn);
if(pl != pvn.len)
{
LM_ERR("invalid pv [%s] (%d/%d)\n", pvn.s, pl, pvn.len);
goto error;
}
pvs = pv_cache_get(&pvn);
if(pvs==NULL)
{
LM_ERR("cannot get pv spec for [%s]\n", pvn.s);
goto error;
}
memset(&val, 0, sizeof(pv_value_t));
val.flags |= PV_VAL_NULL;
if(pv_set_spec_value(env_M->msg, pvs, 0, &val)<0)
{
LM_ERR("unable to unset pv [%s]\n", pvn.s);
goto error;
}
mono_free(pvn.s);
return 0;
error:
if(pvn.s!=NULL)
mono_free(pvn.s);
return -1;
}
/**
*
*/
static int sr_mono_pv_is_null (MonoString *pv)
{
str pvn = {0};
pv_spec_t *pvs;
pv_value_t val;
int pl;
sr_mono_env_t *env_M;
env_M = sr_mono_env_get();
pvn.s = mono_string_to_utf8(pv);
if(pvn.s==NULL || env_M->msg==NULL)
goto error;
pvn.len = strlen(pvn.s);
LM_DBG("pv is null test: %s\n", pvn.s);
pl = pv_locate_name(&pvn);
if(pl != pvn.len)
{
LM_ERR("invalid pv [%s] (%d/%d)\n", pvn.s, pl, pvn.len);
goto error;
}
pvs = pv_cache_get(&pvn);
if(pvs==NULL)
{
LM_ERR("cannot get pv spec for [%s]\n", pvn.s);
goto error;
}
memset(&val, 0, sizeof(pv_value_t));
if(pv_get_spec_value(env_M->msg, pvs, &val) != 0)
{
LM_INFO("unable to get pv value for [%s]\n", pvn.s);
goto error;
}
mono_free(pvn.s);
if(val.flags&PV_VAL_NULL)
{
return 1;
}
return 0;
error:
if(pvn.s!=NULL)
mono_free(pvn.s);
return -1;
}
/**
*
*/
static const sr_M_export_t _sr_M_export_pv[] = {
{"SR.PV::GetS", sr_mono_pv_gets},
{"SR.PV::GetI", sr_mono_pv_geti},
{"SR.PV::SetI", sr_mono_pv_seti},
{"SR.PV::SetS", sr_mono_pv_sets},
{"SR.PV::Unset", sr_mono_pv_unset},
{"SR.PV::IsNull", sr_mono_pv_is_null},
{NULL, NULL}
};
int sr_mono_load_class_pv()
{
int i;
for(i=0; _sr_M_export_pv[i].name!=NULL; i++)
mono_add_internal_call(_sr_M_export_pv[i].name, _sr_M_export_pv[i].method);
return 0;
}
/**
*
*/
static int sr_mono_hdr_append (MonoString *hv)
{
struct lump* anchor;
struct hdr_field *hf;
char *hdr;
str txt = {0};
sr_mono_env_t *env_M;
env_M = sr_mono_env_get();
txt.s = mono_string_to_utf8(hv);
if(txt.s==NULL || env_M->msg==NULL)
goto error;
txt.len = strlen(txt.s);
LM_DBG("append hf: %s\n", txt.s);
if (parse_headers(env_M->msg, HDR_EOH_F, 0) == -1)
{
LM_ERR("error while parsing message\n");
goto error;
}
hf = env_M->msg->last_header;
hdr = (char*)pkg_malloc(txt.len);
if(hdr==NULL)
{
LM_ERR("no pkg memory left\n");
goto error;
}
memcpy(hdr, txt.s, txt.len);
anchor = anchor_lump(env_M->msg,
hf->name.s + hf->len - env_M->msg->buf, 0, 0);
if(insert_new_lump_before(anchor, hdr, txt.len, 0) == 0)
{
LM_ERR("can't insert lump\n");
pkg_free(hdr);
goto error;
}
mono_free(txt.s);
return 0;
error:
if(txt.s!=NULL)
mono_free(txt.s);
return -1;
}
/**
*
*/
static int sr_mono_hdr_remove (MonoString *hv)
{
struct lump* anchor;
struct hdr_field *hf;
str hname;
str txt = {0};
sr_mono_env_t *env_M;
env_M = sr_mono_env_get();
txt.s = mono_string_to_utf8(hv);
if(txt.s==NULL || env_M->msg==NULL)
goto error;
txt.len = strlen(txt.s);
LM_DBG("remove hf: %s\n", txt.s);
if (parse_headers(env_M->msg, HDR_EOH_F, 0) == -1) {
LM_ERR("error while parsing message\n");
goto error;
}
hname.s = txt.s;
hname.len = txt.len;
for (hf=env_M->msg->headers; hf; hf=hf->next)
{
if (cmp_hdrname_str(&hf->name, &hname)==0)
{
anchor=del_lump(env_M->msg,
hf->name.s - env_M->msg->buf, hf->len, 0);
if (anchor==0)
{
LM_ERR("cannot remove hdr %s\n", txt.s);
goto error;
}
}
}
mono_free(txt.s);
return 0;
error:
if(txt.s!=NULL)
mono_free(txt.s);
return -1;
}
/**
*
*/
static int sr_mono_hdr_insert (MonoString *hv)
{
struct lump* anchor;
struct hdr_field *hf;
char *hdr;
str txt = {0};
sr_mono_env_t *env_M;
env_M = sr_mono_env_get();
txt.s = mono_string_to_utf8(hv);
if(txt.s==NULL || env_M->msg==NULL)
goto error;
txt.len = strlen(txt.s);
LM_DBG("insert hf: %s\n", txt.s);
hf = env_M->msg->headers;
hdr = (char*)pkg_malloc(txt.len);
if(hdr==NULL)
{
LM_ERR("no pkg memory left\n");
goto error;
}
memcpy(hdr, txt.s, txt.len);
anchor = anchor_lump(env_M->msg,
hf->name.s + hf->len - env_M->msg->buf, 0, 0);
if(insert_new_lump_before(anchor, hdr, txt.len, 0) == 0)
{
LM_ERR("can't insert lump\n");
pkg_free(hdr);
goto error;
}
mono_free(txt.s);
return 0;
error:
if(txt.s!=NULL)
mono_free(txt.s);
return -1;
}
/**
*
*/
static int sr_mono_hdr_append_to_reply (MonoString *hv)
{
str txt = {0};
sr_mono_env_t *env_M;
env_M = sr_mono_env_get();
txt.s = mono_string_to_utf8(hv);
if(txt.s==NULL || env_M->msg==NULL)
goto error;
txt.len = strlen(txt.s);
LM_DBG("append to reply: %s\n", txt.s);
if(add_lump_rpl(env_M->msg, txt.s, txt.len, LUMP_RPL_HDR)==0)
{
LM_ERR("unable to add reply lump\n");
goto error;
}
mono_free(txt.s);
return 0;
error:
if(txt.s!=NULL)
mono_free(txt.s);
return -1;
}
/**
*
*/
static const sr_M_export_t _sr_M_export_hdr[] = {
{"SR.HDR::Append", sr_mono_hdr_append},
{"SR.HDR::Remove", sr_mono_hdr_remove},
{"SR.HDR::Insert", sr_mono_hdr_insert},
{"SR.HDR::AppendToReply", sr_mono_hdr_append_to_reply},
{NULL, NULL}
};
int sr_mono_load_class_hdr()
{
int i;
for(i=0; _sr_M_export_hdr[i].name!=NULL; i++)
mono_add_internal_call(_sr_M_export_hdr[i].name, _sr_M_export_hdr[i].method);
return 0;
}
|