lcr_rpc.c

Go to the documentation of this file.
00001 /*
00002  * Various lcr related functions :: RPC API
00003  *
00004  * Copyright (C) 2009-2010 Juha Heinanen
00005  *
00006  * This file is part of SIP Router, a free SIP server.
00007  *
00008  * SIP Router is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version
00012  *
00013  * For a license to use the SIP Router software under conditions
00014  * other than those described here, or to purchase support for this
00015  * software, please contact iptel.org by e-mail at the following addresses:
00016  *    info@iptel.org
00017  *
00018  * SIP Router is distributed in the hope that it will be useful,
00019  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00020  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00021  * GNU General Public License for more details.
00022  *
00023  * You should have received a copy of the GNU General Public License 
00024  * along with this program; if not, write to the Free Software 
00025  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00026  */
00027 
00035 #include "lcr_rpc.h"
00036 
00037 #include "lcr_mod.h"
00038 #include "../../ip_addr.h"
00039 
00040 
00041 static const char* reload_doc[2] = {
00042     "Reload lcr tables from database.",
00043     0
00044 };
00045 
00046 
00047 static void reload(rpc_t* rpc, void* c)
00048 {
00049     lock_get(reload_lock);
00050     if (reload_tables() != 1)
00051         rpc->fault(c, 500, "LCR Module Reload Failed");
00052     lock_release(reload_lock);
00053 }
00054 
00055 
00056 static const char* dump_gws_doc[2] = {
00057     "Dump the contents of lcr_gws table.",
00058     0
00059 };
00060 
00061 
00062 static void dump_gws(rpc_t* rpc, void* c)
00063 {
00064     void* st;
00065     unsigned int i, j;
00066     enum sip_protos transport;
00067     str gw_name, hostname, params;
00068     str prefix, tag;
00069     struct gw_info *gws;
00070     char buf[INT2STR_MAX_LEN], *start;
00071     int len;
00072 
00073     for (j = 1; j <= lcr_count_param; j++) {
00074 
00075         gws = gw_pt[j];
00076 
00077         for (i = 1; i <= gws[0].ip_addr.u.addr32[0]; i++) {
00078             if (rpc->add(c, "{", &st) < 0) return;
00079             rpc->struct_add(st, "d", "lcr_id", j);
00080             rpc->struct_add(st, "d", "gw_id", gws[i].gw_id);
00081             rpc->struct_add(st, "d", "gw_index", i);
00082             gw_name.s=gws[i].gw_name;
00083             gw_name.len=gws[i].gw_name_len;
00084             rpc->struct_add(st, "S", "gw_name", &gw_name);
00085             if (gws[i].scheme == SIP_URI_T) {
00086                 rpc->struct_add(st, "s", "scheme", "sip");
00087             } else {
00088                 rpc->struct_add(st, "s", "scheme", "sips");
00089             }
00090             if (gws[i].ip_addr.af == AF_INET)
00091                 rpc->struct_printf(st, "ip_addr", "%d.%d.%d.%d",
00092                                         gws[i].ip_addr.u.addr32[0],
00093                                         gws[i].ip_addr.u.addr32[1],
00094                                         gws[i].ip_addr.u.addr32[2],
00095                                         gws[i].ip_addr.u.addr32[3]);
00096             else /* AF_INET6 */
00097                 rpc->struct_printf(st, "ip_addr", "%x:%x:%x:%x:%x:%x:%x:%x",
00098                                         gws[i].ip_addr.u.addr16[0],
00099                                         gws[i].ip_addr.u.addr16[1],
00100                                         gws[i].ip_addr.u.addr16[2],
00101                                         gws[i].ip_addr.u.addr16[3],
00102                                         gws[i].ip_addr.u.addr16[4],
00103                                         gws[i].ip_addr.u.addr16[5],
00104                                         gws[i].ip_addr.u.addr16[6],
00105                                         gws[i].ip_addr.u.addr16[7]);
00106             hostname.s=gws[i].hostname;
00107             hostname.len=gws[i].hostname_len;
00108             rpc->struct_add(st, "S", "hostname", &hostname);
00109             rpc->struct_add(st, "d", "port", gws[i].port);
00110             params.s=gws[i].params;
00111             params.len=gws[i].params_len;
00112             rpc->struct_add(st, "S", "params", &params);
00113             transport = gws[i].transport;
00114             switch(transport){
00115             case PROTO_UDP:
00116                 rpc->struct_add(st, "s", "transport", "UDP");
00117                 break;
00118             case PROTO_TCP:
00119                 rpc->struct_add(st, "s", "transport", "TCP");
00120                 break;
00121             case PROTO_TLS:
00122                 rpc->struct_add(st, "s", "transport", "TLS");
00123                 break;
00124             case PROTO_SCTP:
00125                 rpc->struct_add(st, "s", "transport", "SCTP");
00126                 break;
00127             case PROTO_OTHER:
00128                 rpc->struct_add(st, "s", "transport", "OTHER");
00129                 break;
00130             case PROTO_NONE:
00131                 break;
00132             }
00133             prefix.s=gws[i].prefix;
00134             prefix.len=gws[i].prefix_len;
00135             tag.s=gws[i].tag;
00136             tag.len=gws[i].tag_len;
00137             start = int2strbuf(gws[i].defunct_until, &(buf[0]), INT2STR_MAX_LEN,
00138                                &len);
00139             rpc->struct_add(st, "dSSds",
00140                             "strip",  gws[i].strip,
00141                             "prefix", &prefix,
00142                             "tag",    &tag,
00143                             "flags",  gws[i].flags,
00144                             "defunct_until",  start
00145                             );
00146         }
00147     }
00148 }
00149 
00150 
00151 static const char* dump_rules_doc[2] = {
00152     "Dump the contents of the lcr_rules table.",
00153     0
00154 };
00155 
00156 
00157 static void dump_rules(rpc_t* rpc, void* c)
00158 {
00159     int i, j;
00160     struct rule_info **rules, *rule;
00161     struct target *t;
00162     void* st;
00163     str prefix, from_uri, request_uri;
00164 
00165     for (j = 1; j <= lcr_count_param; j++) {
00166             
00167         rules = rule_pt[j];
00168 
00169         for (i = 0; i < lcr_rule_hash_size_param; i++) {
00170             rule = rules[i];
00171             while (rule) {
00172                 if (rpc->add(c, "{", &st) < 0) return;
00173                 prefix.s=rule->prefix;
00174                 prefix.len=rule->prefix_len;
00175                 from_uri.s=rule->from_uri;
00176                 from_uri.len=rule->from_uri_len;
00177                 request_uri.s=rule->request_uri;
00178                 request_uri.len=rule->request_uri_len;
00179                 rpc->struct_add(st, "ddSSSd",
00180                                 "lcr_id", j,
00181                                 "rule_id", rule->rule_id,
00182                                 "prefix", &prefix,
00183                                 "from_uri", &from_uri,
00184                                 "request_uri", &request_uri,
00185                                 "stopper", rule->stopper
00186                                 );
00187                 t = rule->targets;
00188                 while (t) {
00189                     if (rpc->add(c, "{", &st) < 0) return;
00190                     rpc->struct_add(st, "ddd",
00191                                     "gw_index", t->gw_index,
00192                                     "priority", t->priority,
00193                                     "weight", t->weight
00194                                     );
00195                     t = t->next;
00196                 }
00197                 rule=rule->next;
00198             }
00199         }
00200         rule=rules[lcr_rule_hash_size_param];
00201         while (rule) {
00202             rpc->add(c, "d", rule->prefix_len);
00203             rule=rule->next;
00204         }
00205     }
00206 }
00207 
00208 
00209 static const char* defunct_gw_doc[2] = {
00210     "Defunct gateway until speficied time (Unix timestamp).",
00211     0
00212 };
00213 
00214 
00215 static void defunct_gw(rpc_t* rpc, void* c)
00216 {
00217     unsigned int lcr_id, gw_id, until;
00218 
00219     if (rpc->scan(c, "ddd", &lcr_id, &gw_id, &until) < 3) {
00220         rpc->fault(c, 400, "lcr_id, gw_id, and timestamp parameters required");
00221         return;
00222     }
00223 
00224     if (rpc_defunct_gw(lcr_id, gw_id, until) == 0) {
00225         rpc->fault(c, 400, "parameter value error (see syslog)");
00226     }
00227     
00228     return;
00229 }
00230 
00231 
00232 rpc_export_t lcr_rpc[] = {
00233     {"lcr.reload", reload, reload_doc, 0},
00234     {"lcr.dump_gws", dump_gws, dump_gws_doc, 0},
00235     {"lcr.dump_rules", dump_rules, dump_rules_doc, 0},
00236     {"lcr.defunct_gw", defunct_gw, defunct_gw_doc, 0},
00237     {0, 0, 0, 0}
00238 };