pr_rpc.c

00001 /*
00002  * Prefix Route Module - RPC Commands
00003  *
00004  * Copyright (C) 2007 Alfred E. Heggestad
00005  * Copyright (C) 2008 Telio Telecom AS
00006  *
00007  * This file is part of ser, a free SIP server.
00008  *
00009  * ser is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2 of the License, or
00012  * (at your option) any later version
00013  *
00014  * For a license to use the ser software under conditions
00015  * other than those described here, or to purchase support for this
00016  * software, please contact iptel.org by e-mail at the following addresses:
00017  *    info@iptel.org
00018  *
00019  * ser is distributed in the hope that it will be useful,
00020  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00021  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00022  * GNU General Public License for more details.
00023  *
00024  * You should have received a copy of the GNU General Public License
00025  * along with this program; if not, write to the Free Software
00026  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00027  */
00028 
00029 #include <stdio.h>
00030 #include <stdlib.h>
00031 #include <string.h>
00032 #include "../../str.h"
00033 #include "../../dprint.h"
00034 #include "../../rpc.h"
00035 #include "tree.h"
00036 #include "pr.h"
00037 
00038 
00039 static const char *rpc_dump_doc[2]   = {"Dump the prefix route tree",   NULL};
00040 static const char *rpc_reload_doc[2] = {"Reload prefix routes from DB", NULL};
00041 
00042 
00046 static void rpc_dump(rpc_t *rpc, void *c)
00047 {
00048         char buf[1024];
00049         FILE *f;
00050 
00051         f = tmpfile();
00052         if (!f) {
00053                 rpc->fault(c, 500, "failed to open temp file");
00054                 return;
00055         }
00056 
00057         tree_print(f);
00058 
00059         rewind(f);
00060 
00061         while (!feof(f)) {
00062 
00063                 if (!fgets(buf, sizeof(buf), f))
00064                         break;
00065 
00066                 buf[strlen(buf)-1] = '\0';
00067 
00068                 rpc->printf(c, "%s", buf);
00069         }
00070 
00071         fclose(f);
00072 }
00073 
00074 
00078 static void rpc_reload(rpc_t *rpc, void *c)
00079 {
00080         LOG(L_NOTICE, "prefix_route: Reloading prefix route tree from DB\n");
00081 
00082         if (0 != pr_db_load()) {
00083                 LOG(L_ERR, "prefix_route: rpc_reload(): db_load() failed\n");
00084                 rpc->fault(c, 400, "failed to reload prefix routes");
00085         }
00086         else {
00087                 rpc->printf(c, "Prefix routes reloaded successfully");
00088         }
00089 }
00090 
00091 
00092 rpc_export_t pr_rpc[] = {
00093         {"prefix_route.reload", rpc_reload, rpc_reload_doc, 0},
00094         {"prefix_route.dump",   rpc_dump,   rpc_dump_doc,   0},
00095         {0, 0, 0, 0}
00096 };