1 /*$Id$
2 *
3 * Example ser module, it will just print its string parameter to stdout
4 *
5 *
6 * Copyright (C) 2001-2003 FhG Fokus
7 *
8 * This file is part of ser, a free SIP server.
9 *
10 * ser is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version
14 *
15 * For a license to use the ser software under conditions
16 * other than those described here, or to purchase support for this
17 * software, please contact iptel.org by e-mail at the following addresses:
18 * info@iptel.org
19 *
20 * ser is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28 */
29 /*
30 * History:
31 * --------
32 * 2003-03-10 module export interface updated to the new format (andrei)
33 * 2003-03-11 flags export parameter added (janakj)
34 * 2006-01-07 str export parameter added, overloading test (tma)
35 */
36
37
38
39
40 #include "../../core/sr_module.h"
41 #include "../../core/route_struct.h"
42 #include "../../core/str.h"
43 #include <stdio.h>
44
45 MODULE_VERSION
46
47 static int print_fixup_f_1(void **param, int param_no);
48 static int print_fixup_f_2(void **param, int param_no);
49 static int print_f_0(struct sip_msg*, char*, char*);
50 static int print_f_1(struct sip_msg*, char*, char*);
51 static int print_f_2(struct sip_msg*, char*, char*);
52 static int print_f1(struct sip_msg*, char*, char*);
53 static int print_f2(struct sip_msg*, char*, char*);
54 static int print_f3(struct sip_msg*, char*, char*, char*);
55 static int print_f_var(struct sip_msg*, int argc, action_u_t argv[]);
56 static int mod_init(void);
57
58 /* the parameters are not used, they are only meant as an example*/
59 char* string_param = 0;
60 int int_param = 0;
61 str str_param = STR_STATIC_INIT("");
62
63 static cmd_export_t cmds[]={
64 {"print", print_f_0, 0, 0, 0, REQUEST_ROUTE}, // overload test
65 {"print", print_f_1, 1, print_fixup_f_1, 0, REQUEST_ROUTE},
66 {"print", print_f_2, 2, print_fixup_f_2, 0, REQUEST_ROUTE},
67 {"print1", print_f1, 1, 0, 0, REQUEST_ROUTE},
68 {"print2", print_f2, 2, fixup_var_str_12, 0, REQUEST_ROUTE},
69 {"print3", (cmd_function)print_f3, 3, 0, 0, REQUEST_ROUTE},
70 {"printv", (cmd_function)print_f_var, VAR_PARAM_NO, 0, 0, REQUEST_ROUTE},
71 {0, 0, 0, 0, 0, 0}
72 };
73
74 static param_export_t params[]={
75 {"string_param", PARAM_STRING, &string_param},
76 {"str_param", PARAM_STR, &str_param},
77 {"int_param", PARAM_INT, &int_param},
78 {0,0,0}
79 };
80
81 struct module_exports exports = {
82 "print_stdout", /* module name */
83 DEFAULT_DLFLAGS, /* dlopen flags */
84 cmds, /* exported functions */
85 params, /* exported parameters */
86 0, /* RPC method exports */
87 0, /* exported pseudo-variables */
88 0, /* response handling function */
89 mod_init, /* module initialization function */
90 0, /* per-child init function */
91 0 /* module destroy function */
92 };
93
94
mod_init(void)95 static int mod_init(void)
96 {
97 fprintf(stderr, "print - initializing\n");
98 DBG("print: string_param = '%s'\n", string_param);
99 DBG("print: str_param = '%.*s'\n", str_param.len, str_param.s);
100 DBG("print: int_param = %d\n", int_param);
101 WARN("this is an example module, it has no practical use\n");
102 return 0;
103 }
104
105
print_fixup_f(void ** param,int param_no)106 static int print_fixup_f(void **param, int param_no) {
107 action_u_t *a;
108 int n, i;
109 n = fixup_get_param_count(param, param_no);
110 for (i=1; i<=n; i++) {
111 a = fixup_get_param(param, param_no, i);
112 DBG("param #%d: '%s'\n", i, a->u.string);
113 }
114 return 1;
115 }
116
print_f_0(struct sip_msg * msg,char * s1,char * s2)117 static int print_f_0(struct sip_msg* msg, char* s1, char* s2)
118 {
119 printf("<null>\n");
120 return 1;
121 }
122
print_f_1(struct sip_msg * msg,char * s1,char * s2)123 static int print_f_1(struct sip_msg* msg, char* s1, char* s2)
124 {
125 printf("%s\n",s1);
126 return 1;
127 }
128
print_f_2(struct sip_msg * msg,char * s1,char * s2)129 static int print_f_2(struct sip_msg* msg, char* s1, char* s2)
130 {
131 printf("%s%s\n",s1, s2);
132 return 1;
133 }
134
print_fixup_f_1(void ** param,int param_no)135 static int print_fixup_f_1(void **param, int param_no) {
136 DBG("print: print_fixup_f_1('%s')\n", (char*)*param);
137 return print_fixup_f(param, param_no);
138 }
139
print_fixup_f_2(void ** param,int param_no)140 static int print_fixup_f_2(void **param, int param_no) {
141 DBG("print: print_fixup_f_2('%s')\n", (char*)*param);
142 return print_fixup_f(param, param_no);
143 }
144
145
146
147 /* 1 parameter, no fixup version */
print_f1(struct sip_msg * msg,char * s1,char * not_used)148 static int print_f1(struct sip_msg* msg, char* s1, char* not_used)
149 {
150 printf("%s\n", s1);
151 return 1;
152 }
153
154
155 /* 2 parameters, fparam fixup version */
print_f2(struct sip_msg * msg,char * s1,char * s2)156 static int print_f2(struct sip_msg* msg, char* s1, char* s2)
157 {
158 str a, b;
159 if (get_str_fparam(&a, msg, (fparam_t*)s1) !=0 ||
160 get_str_fparam(&b, msg, (fparam_t*)s2) !=0) {
161 BUG("get_str_fparam failed\n");
162 return -1;
163 }
164 printf("%.*s%.*s\n", a.len, a.s, b.len, b.s);
165 return 1;
166 }
167
168
169 /* 3 parameters, no fixup version */
print_f3(struct sip_msg * msg,char * s1,char * s2,char * s3)170 static int print_f3(struct sip_msg* msg, char* s1, char* s2, char* s3)
171 {
172 printf("%s%s%s\n", s1, s2, s3);
173 return 1;
174 }
175
176
177 /* variable number of parameters, no fixup version */
print_f_var(struct sip_msg * msg,int argc,action_u_t argv[])178 static int print_f_var(struct sip_msg* msg, int argc, action_u_t argv[])
179 {
180 int i;
181 for (i = 0; i < argc; i++)
182 printf("%s", argv[i].u.string);
183 printf("\n");
184 return 1;
185 }
186