change to return value convention
[sip-router] / modules / tm / tm.c
1 /*
2  * $Id$
3  *
4  * TM module
5  *
6  */
7
8 #include <stdio.h>
9 #include <string.h>
10 #include <netdb.h>
11
12 #include "../../sr_module.h"
13 #include "../../dprint.h"
14 #include "../../error.h"
15 #include "../../ut.h"
16
17 #include "sip_msg.h"
18 #include "h_table.h"
19 #include "t_funcs.h"
20
21
22
23 /*static int test_f(struct sip_msg*, char*,char*);*/
24 static int w_t_check(struct sip_msg* msg, char* str, char* str2);
25 static int w_t_send_reply(struct sip_msg* msg, char* str, char* str2);
26 static int w_t_forward(struct sip_msg* msg, char* str, char* str2);
27 static int w_t_forward_def(struct sip_msg* msg, char* str, char* str2);
28 static int w_t_release(struct sip_msg* msg, char* str, char* str2);
29 static int fixup_t_forward(void** param, int param_no);
30 static int fixup_t_forward_def(void** param, int param_no);
31 static int fixup_t_send_reply(void** param, int param_no);
32
33 static struct module_exports nm_exports= {
34         "tm_module",
35         (char*[]){                      "t_add_transaction",
36                                 "t_lookup_request",
37                                 "t_forward",
38                                 "t_forward_def",
39                                 "t_forward_uri",
40                                 "t_send_reply",
41                                 "t_retransmit_reply",
42                                 "t_release",
43                                 "t_unref"
44                         },
45         (cmd_function[]){
46                                         t_add_transaction,
47                                         w_t_check,
48                                         w_t_forward,
49                                         w_t_forward_def,
50                                         t_forward_uri,
51                                         w_t_send_reply,
52                                         t_retransmit_reply,
53                                         w_t_release,
54                                         t_unref
55                                         },
56         (int[]){
57                                 0,
58                                 0,
59                                 2,
60                                 1,
61                                 0,
62                                 2,
63                                 0,
64                                 0,
65                                 0
66                         },
67         (fixup_function[]){
68                                 0,
69                                 0,
70                                 fixup_t_forward,
71                                 fixup_t_forward_def,
72                                 0,
73                                 fixup_t_send_reply,
74                                 0,
75                                 0,
76                                 0,
77                 },
78         9,
79         (response_function) t_on_reply_received,
80         (destroy_function) tm_shutdown
81 };
82
83
84
85 #ifdef STATIC_TM
86 struct module_exports* tm_mod_register()
87 #else
88 struct module_exports* mod_register()
89 #endif
90 {
91
92         DBG( "TM - registering...\n");
93         if (tm_startup()==-1) return 0;
94         return &nm_exports;
95 }
96
97
98
99 static int fixup_t_forward(void** param, int param_no)
100 {
101         char* name;
102         struct hostent* he;
103         unsigned int port;
104         int err;
105 #ifdef DNS_IP_HACK
106         unsigned int ip;
107         int len;
108 #endif
109
110         DBG("TM module: fixup_t_forward(%s, %d)\n", (char*)*param, param_no);
111         if (param_no==1){
112                 name=*param;
113 #ifdef DNS_IP_HACK
114                 len=strlen(name);
115                 ip=str2ip(name, len, &err);
116                 if (err==0){
117                         goto copy;
118                 }
119 #endif
120                 /* fail over to normal lookup */
121                 he=gethostbyname(name);
122                 if (he==0){
123                         LOG(L_CRIT, "ERROR: mk_proxy: could not resolve hostname:"
124                                                 " \"%s\"\n", name);
125                         return E_BAD_ADDRESS;
126                 }
127                 memcpy(&ip, he->h_addr_list[0], sizeof(unsigned int));
128         copy:
129                 free(*param);
130                 *param=(void*)ip;
131                 return 0;
132         }else if (param_no==2){
133                 port=htons(str2s(*param, strlen(*param), &err));
134                 if (err==0){
135                         free(*param);
136                         *param=(void*)port;
137                         return 0;
138                 }else{
139                         LOG(L_ERR, "TM module:fixup_t_forward: bad port number <%s>\n",
140                                         *param);
141                         return E_UNSPEC;
142                 }
143         }
144         return 0;
145 }
146
147
148
149 static int fixup_t_forward_def(void** param, int param_no)
150 {
151         char* name;
152         struct hostent* he;
153         int err;
154 #ifdef DNS_IP_HACK
155         unsigned int ip;
156         int len;
157 #endif
158
159         DBG("TM module: fixup_t_forward_def(%s, %d)\n", (char*)*param, param_no);
160         if (param_no==1){
161                 name=*param;
162 #ifdef DNS_IP_HACK
163                 len=strlen(name);
164                 ip=str2ip(name, len, &err);
165                 if (err==0){
166                         goto copy;
167                 }
168 #endif
169                 /* fail over to normal lookup */
170                 he=gethostbyname(name);
171                 if (he==0){
172                         LOG(L_CRIT, "ERROR: mk_proxy: could not resolve hostname:"
173                                                 " \"%s\"\n", name);
174                         return E_BAD_ADDRESS;
175                 }
176                 memcpy(&ip, he->h_addr_list[0], sizeof(unsigned int));
177         copy:
178                 free(*param);
179                 *param=(void*)ip;
180                 return 0;
181         }
182         return 0;
183 }
184
185
186
187 static int fixup_t_send_reply(void** param, int param_no)
188 {
189         unsigned int code;
190         int err;
191
192         if (param_no==1){
193                 code=str2s(*param, strlen(*param), &err);
194                 if (err==0){
195                         free(*param);
196                         *param=(void*)code;
197                         return 0;
198                 }else{
199                         LOG(L_ERR, "TM module:fixup_t_send_reply: bad  number <%s>\n",
200                                         *param);
201                         return E_UNSPEC;
202                 }
203         }
204         /* second param => no conversion*/
205         return 0;
206 }
207
208
209
210 static int w_t_check(struct sip_msg* msg, char* str, char* str2)
211 {
212         return t_check( msg , 0 ) ? 1 : -1;
213 }
214
215 static int w_t_forward(struct sip_msg* msg, char* str, char* str2)
216 {
217         return t_forward(msg, (unsigned int) str, (unsigned int) str2);
218 }
219
220
221 static int w_t_forward_def(struct sip_msg* msg, char* str, char* str2)
222 {
223         return t_forward(msg, (unsigned int) str, 5060 );
224 }
225
226
227 static int w_t_send_reply(struct sip_msg* msg, char* str, char* str2)
228 {
229         return t_send_reply(msg, (unsigned int) str, str2);
230 }
231
232 static int w_t_release(struct sip_msg* msg, char* str, char* str2)
233 {
234         return t_release_transaction(msg);
235 }
236