blst.c

00001 /*$Id$
00002  *
00003  * Copyright (C) 2007 iptelorg GmbH
00004  *
00005  * Permission to use, copy, modify, and distribute this software for any
00006  * purpose with or without fee is hereby granted, provided that the above
00007  * copyright notice and this permission notice appear in all copies.
00008  *
00009  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
00010  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
00011  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
00012  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
00013  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
00014  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
00015  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
00016  */
00017 /*
00018  *
00019  * Blacklist related script functions
00020  *
00021  * History:
00022  * -------
00023  *  2007-07-30  created by andrei
00024  */
00025 
00026 
00027 #include "../../modparam.h"
00028 #include "../../dprint.h"
00029 #include "../../parser/msg_parser.h"
00030 #include "../../parser/hf.h"
00031 #include "../../dst_blacklist.h"
00032 #include "../../timer_ticks.h"
00033 #include "../../ip_addr.h"
00034 #include "../../compiler_opt.h"
00035 #include "../../ut.h"
00036 #include "../../globals.h"
00037 #include "../../cfg_core.h"
00038 
00039 
00040 MODULE_VERSION
00041 
00042 
00043 
00044 static int blst_add_f(struct sip_msg*, char*, char*);
00045 static int blst_add_retry_after_f(struct sip_msg*, char*, char*);
00046 static int blst_del_f(struct sip_msg*, char*, char*);
00047 static int blst_is_blacklisted_f(struct sip_msg*, char*, char*);
00048 static int blst_set_ignore_f(struct sip_msg*, char*, char*);
00049 static int blst_clear_ignore_f(struct sip_msg*, char*, char*);
00050 static int blst_rpl_set_ignore_f(struct sip_msg*, char*, char*);
00051 static int blst_rpl_clear_ignore_f(struct sip_msg*, char*, char*);
00052 
00053 
00054 
00055 static cmd_export_t cmds[]={
00056         {"blst_add",           blst_add_f,               0,  0,
00057                         REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|ONSEND_ROUTE},
00058         {"blst_add",           blst_add_f,               1, fixup_var_int_1,
00059                         REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|ONSEND_ROUTE},
00060         {"blst_add_retry_after", blst_add_retry_after_f, 2, fixup_var_int_12,
00061                         REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|ONSEND_ROUTE},
00062         {"blst_del",           blst_del_f,               0, 0,
00063                         REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|ONSEND_ROUTE},
00064         {"blst_is_blacklisted",   blst_is_blacklisted_f, 0, 0,
00065                         REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|ONSEND_ROUTE},
00066         {"blst_set_ignore",         blst_set_ignore_f,   0,  0,
00067                 REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE|ONSEND_ROUTE},
00068         {"blst_set_ignore",         blst_set_ignore_f,   1,  fixup_var_int_1,
00069                 REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE|ONSEND_ROUTE},
00070         {"blst_clear_ignore",         blst_clear_ignore_f,   0,  0,
00071                 REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE|ONSEND_ROUTE},
00072         {"blst_clear_ignore",         blst_clear_ignore_f,   1,  fixup_var_int_1,
00073                 REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE|ONSEND_ROUTE},
00074         {"blst_rpl_set_ignore",       blst_rpl_set_ignore_f, 0,  0,
00075                 REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE},
00076         {"blst_rpl_set_ignore",      blst_rpl_set_ignore_f,  1,  fixup_var_int_1,
00077                 REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE},
00078         {"blst_rpl_clear_ignore",   blst_rpl_clear_ignore_f, 0,  0,
00079                 REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE},
00080         {"blst_rpl_clear_ignore",   blst_rpl_clear_ignore_f, 1,  fixup_var_int_1,
00081                 REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE},
00082         {0,0,0,0,0}
00083 };
00084 
00085 static param_export_t params[]={
00086         {0,0,0}
00087         }; /* no params */
00088 
00089 struct module_exports exports= {
00090         "blst",
00091         cmds,
00092         0,        /* RPC methods */
00093         params,
00094         0, /* module initialization function */
00095         0, /* response function */
00096         0, /* destroy function */
00097         0, /* on_cancel function */
00098         0, /* per-child init function */
00099 };
00100 
00101 
00102 
00103 static int blst_add_f(struct sip_msg* msg, char* to, char* foo)
00104 {
00105 #ifdef USE_DST_BLACKLIST
00106         int t;
00107         struct dest_info src;
00108         
00109         if (likely(cfg_get(core, core_cfg, use_dst_blacklist))){
00110                 t=0;
00111                 if (unlikely( to && (get_int_fparam(&t, msg, (fparam_t*)to)<0)))
00112                         return -1;
00113                 if (t==0)
00114                         t=cfg_get(core, core_cfg, blst_timeout);
00115                 init_dest_info(&src);
00116                 src.send_sock=0;
00117                 src.to=msg->rcv.src_su;
00118                 src.id=msg->rcv.proto_reserved1;
00119                 src.proto=msg->rcv.proto;
00120                 dst_blacklist_force_add_to(BLST_ADM_PROHIBITED, &src, msg,
00121                                                                         S_TO_TICKS(t));
00122                 return 1;
00123         }else{
00124                 LOG(L_WARN, "WARNING: blst: blst_add: blacklist support disabled\n");
00125         }
00126 #else /* USE_DST_BLACKLIST */
00127         LOG(L_WARN, "WARNING: blst: blst_add: blacklist support not compiled-in"
00128                         " - no effect -\n");
00129 #endif /* USE_DST_BLACKLIST */
00130         return 1;
00131 }
00132 
00133 
00134 
00135 /* returns error if no retry_after hdr field is present */
00136 static int blst_add_retry_after_f(struct sip_msg* msg, char* min, char* max)
00137 {
00138 #ifdef USE_DST_BLACKLIST
00139         int t_min, t_max, t;
00140         struct dest_info src;
00141         struct hdr_field* hf;
00142         
00143         if (likely(cfg_get(core, core_cfg, use_dst_blacklist))){
00144                 if (unlikely(get_int_fparam(&t_min, msg, (fparam_t*)min)<0)) return -1;
00145                 if (likely(max)){
00146                         if (unlikely(get_int_fparam(&t_max, msg, (fparam_t*)max)<0))
00147                                 return -1;
00148                 }else{
00149                         t_max=0;
00150                 }
00151         
00152                 init_dest_info(&src);
00153                 src.send_sock=0;
00154                 src.to=msg->rcv.src_su;
00155                 src.id=msg->rcv.proto_reserved1;
00156                 src.proto=msg->rcv.proto;
00157                 t=-1;
00158                 if ((parse_headers(msg, HDR_RETRY_AFTER_F, 0)==0) && 
00159                         (msg->parsed_flag & HDR_RETRY_AFTER_F)){
00160                         for (hf=msg->headers; hf; hf=hf->next)
00161                                 if (hf->type==HDR_RETRY_AFTER_T){
00162                                         /* found */
00163                                         t=(unsigned)(unsigned long)hf->parsed;
00164                                         break;
00165                         }
00166                 }
00167                 if (t<0)
00168                         return -1;
00169                 
00170                 t=MAX_unsigned(t, t_min);
00171                 t=MIN_unsigned(t, t_max);
00172                 if (likely(t))
00173                         dst_blacklist_force_add_to(BLST_ADM_PROHIBITED, &src, msg,
00174                                                                                 S_TO_TICKS(t));
00175                 return 1;
00176         }else{
00177                 LOG(L_WARN, "WARNING: blst: blst_add_retry_after:"
00178                                         " blacklist support disabled\n");
00179         }
00180 #else /* USE_DST_BLACKLIST */
00181         LOG(L_WARN, "WARNING: blst: blst_add_retry_after:"
00182                         " blacklist support not compiled-in - no effect -\n");
00183 #endif /* USE_DST_BLACKLIST */
00184         return 1;
00185 }
00186 
00187 
00188 
00189 static int blst_del_f(struct sip_msg* msg, char* foo, char* bar)
00190 {
00191 #ifdef USE_DST_BLACKLIST
00192         struct dest_info src;
00193         
00194         if (likely(cfg_get(core, core_cfg, use_dst_blacklist))){
00195         
00196                 init_dest_info(&src);
00197                 src.send_sock=0;
00198                 src.to=msg->rcv.src_su;
00199                 src.id=msg->rcv.proto_reserved1;
00200                 src.proto=msg->rcv.proto;
00201                 if (dst_blacklist_del(&src, msg))
00202                         return 1;
00203         }else{
00204                 LOG(L_WARN, "WARNING: blst: blst_del: blacklist support disabled\n");
00205         }
00206 #else /* USE_DST_BLACKLIST */
00207         LOG(L_WARN, "WARNING: blst: blst_del: blacklist support not compiled-in"
00208                         " - no effect -\n");
00209 #endif /* USE_DST_BLACKLIST */
00210         return -1;
00211 }
00212 
00213 
00214 
00215 static int blst_is_blacklisted_f(struct sip_msg* msg, char* foo, char* bar)
00216 {
00217 #ifdef USE_DST_BLACKLIST
00218         struct dest_info src;
00219         
00220         if (likely(cfg_get(core, core_cfg, use_dst_blacklist))){
00221                 init_dest_info(&src);
00222                 src.send_sock=0;
00223                 src.to=msg->rcv.src_su;
00224                 src.id=msg->rcv.proto_reserved1;
00225                 src.proto=msg->rcv.proto;
00226                 if (dst_is_blacklisted(&src, msg))
00227                         return 1;
00228         }else{
00229                 LOG(L_WARN, "WARNING: blst: blst_is_blacklisted:"
00230                                         " blacklist support disabled\n");
00231         }
00232 #else /* USE_DST_BLACKLIST */
00233         LOG(L_WARN, "WARNING: blst: blst_is_blacklisted:"
00234                                 " blacklist support not compiled-in - no effect -\n");
00235 #endif /* USE_DST_BLACKLIST */
00236         return -1;
00237 }
00238 
00239 
00240 
00241 static int blst_set_ignore_f(struct sip_msg* msg, char* flags, char* foo)
00242 {
00243 #ifdef USE_DST_BLACKLIST
00244         unsigned char blst_imask;
00245         int mask;
00246         
00247         if (unlikely(flags && (get_int_fparam(&mask, msg, (fparam_t*)flags)<0)))
00248                 return -1;
00249         blst_imask=flags?mask:0xff;
00250         msg->fwd_send_flags.blst_imask|=blst_imask;
00251         return 1;
00252 #else /* USE_DST_BLACKLIST */
00253         LOG(L_WARN, "WARNING: blst: blst_ignore_req: blacklist support"
00254                                 " not compiled-in - no effect -\n");
00255 #endif /* USE_DST_BLACKLIST */
00256         return 1;
00257 }
00258 
00259 
00260 
00261 static int blst_clear_ignore_f(struct sip_msg* msg, char* flags, char* foo)
00262 {
00263 #ifdef USE_DST_BLACKLIST
00264         unsigned char blst_imask;
00265         int mask;
00266         
00267         if (unlikely(flags && (get_int_fparam(&mask, msg, (fparam_t*)flags)<0)))
00268                 return -1;
00269         blst_imask=flags?mask:0xff;
00270         msg->fwd_send_flags.blst_imask&=~blst_imask;
00271         return 1;
00272 #else /* USE_DST_BLACKLIST */
00273         LOG(L_WARN, "WARNING: blst: blst_ignore_req: blacklist support"
00274                                 " not compiled-in - no effect -\n");
00275 #endif /* USE_DST_BLACKLIST */
00276         return 1;
00277 }
00278 
00279 
00280 
00281 static int blst_rpl_set_ignore_f(struct sip_msg* msg, char* flags, char* foo)
00282 {
00283 #ifdef USE_DST_BLACKLIST
00284         unsigned char blst_imask;
00285         int mask;
00286         
00287         if (unlikely(flags && (get_int_fparam(&mask, msg, (fparam_t*)flags)<0)))
00288                 return -1;
00289         blst_imask=flags?mask:0xff;
00290         msg->rpl_send_flags.blst_imask|=blst_imask;
00291         return 1;
00292 #else /* USE_DST_BLACKLIST */
00293         LOG(L_WARN, "WARNING: blst: blst_ignore_req: blacklist support"
00294                                 " not compiled-in - no effect -\n");
00295 #endif /* USE_DST_BLACKLIST */
00296         return 1;
00297 }
00298 
00299 
00300 
00301 static int blst_rpl_clear_ignore_f(struct sip_msg* msg, char* flags, char* foo)
00302 {
00303 #ifdef USE_DST_BLACKLIST
00304         unsigned char blst_imask;
00305         int mask;
00306         
00307         if (unlikely(flags && (get_int_fparam(&mask, msg, (fparam_t*)flags)<0)))
00308                 return -1;
00309         blst_imask=flags?mask:0xff;
00310         msg->rpl_send_flags.blst_imask&=~blst_imask;
00311         return 1;
00312 #else /* USE_DST_BLACKLIST */
00313         LOG(L_WARN, "WARNING: blst: blst_ignore_req: blacklist support"
00314                                 " not compiled-in - no effect -\n");
00315 #endif /* USE_DST_BLACKLIST */
00316         return 1;
00317 }