1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
|
/*
* $Id: gflags.c,v 1.7 2006/06/07 10:45:32 bogdan_iancu Exp $
*
* Copyright (C) 2004 FhG
* Copyright (C) 2005-2006 Voice Sistem S.R.L.
*
* This file is part of openser, a free SIP server.
*
* openser is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version
*
* openser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* History:
* --------
* 2004-09-09 initial module created (jiri)
* 2006-05-31 flag range checked ; proper cleanup at module destroy ;
* got rid of memory allocation in fixup function ;
* optimized fixup function -> compute directly the bitmap ;
* allowed functions from BRANCH_ROUTE (bogdan)
*
* TODO:
* -----
* - named flags (takes a protected name list)
*
*
* gflags module: global flags; it keeps a bitmap of flags
* in shared memory and may be used to change behaviour
* of server based on value of the flags. E.g.,
* if (is_gflag("1")) { t_relay_to_udp("10.0.0.1","5060"); }
* else { t_relay_to_udp("10.0.0.2","5060"); }
* The benefit of this module is the value of the switch flags
* can be manipulated by external applications such as web interface
* or command line tools.
*
*
*/
/* flag buffer size for FIFO protocool */
#define MAX_FLAG_LEN 12
/* FIFO action protocol names */
#define FIFO_SET_GFLAG "set_gflag"
#define FIFO_IS_GFLAG "is_gflag"
#define FIFO_RESET_GFLAG "reset_gflag"
#define FIFO_GET_GFLAGS "get_gflags"
#include "../../sr_module.h"
#include "../../ut.h"
#include "../../mem/mem.h"
#include "../../mem/shm_mem.h"
#include "../../fifo_server.h"
#include <stdio.h>
MODULE_VERSION
static int set_gflag(struct sip_msg*, char *, char *);
static int reset_gflag(struct sip_msg*, char *, char *);
static int is_gflag(struct sip_msg*, char *, char *);
static int fixup_str2int( void** param, int param_no);
static int mod_init(void);
static void mod_destroy(void);
static int initial=0;
static unsigned int *gflags=0;
static cmd_export_t cmds[]={
{"set_gflag", set_gflag, 1, fixup_str2int,
REQUEST_ROUTE|FAILURE_ROUTE|ONREPLY_ROUTE|BRANCH_ROUTE},
{"reset_gflag", reset_gflag, 1, fixup_str2int,
REQUEST_ROUTE|FAILURE_ROUTE|ONREPLY_ROUTE|BRANCH_ROUTE},
{"is_gflag", is_gflag, 1, fixup_str2int,
REQUEST_ROUTE|FAILURE_ROUTE|ONREPLY_ROUTE|BRANCH_ROUTE},
{0, 0, 0, 0, 0}
};
static param_export_t params[]={
{"initial", INT_PARAM, &initial},
{0,0,0}
};
struct module_exports exports = {
"gflags",
cmds, /* exported functions */
params, /* exported parameters */
0, /* exported statistics */
mod_init, /* module initialization function */
0, /* response function*/
mod_destroy, /* destroy function */
0 /* per-child init function */
};
/**************************** fixup functions ******************************/
static int fixup_str2int( void** param, int param_no)
{
unsigned int myint;
str param_str;
/* we only fix the parameter #1 */
if (param_no!=1)
return 0;
param_str.s=(char*) *param;
param_str.len=strlen(param_str.s);
if (str2int(¶m_str, &myint )<0) {
LOG(L_ERR, "ERROR:gflags:fixup_str2int: bad number <%s>\n",
(char *)(*param));
return E_CFG;
}
if ( myint >= 8*sizeof(*gflags) ) {
LOG(L_ERR, "ERROR:gflags:fixup_str2int: flag <%d> out of "
"range [0..%lu]\n", myint, (unsigned long)8*sizeof(*gflags) );
return E_CFG;
}
/* convert from flag index to flag bitmap */
myint = 1 << myint;
/* success -- change to int */
pkg_free(*param);
*param=(void *)(long)myint;
return 0;
}
static unsigned int read_flag(FILE *pipe, char *response_file)
{
char flag_str[MAX_FLAG_LEN];
int flag_len;
unsigned int flag;
str fs;
if (!read_line(flag_str, MAX_FLAG_LEN, pipe, &flag_len)
|| flag_len == 0) {
fifo_reply(response_file, "400: gflags: invalid flag number\n");
LOG(L_ERR, "ERROR:gflags:read_flag: invalid flag number\n");
return 0;
}
fs.s=flag_str;fs.len=flag_len;
if (str2int(&fs, &flag) < 0) {
fifo_reply(response_file, "400: gflags: invalid flag format\n");
LOG(L_ERR, "ERROR:gflags:read_flag: invalid flag format\n");
return 0;
}
if ( flag >= 8*sizeof(*gflags) ) {
fifo_reply(response_file, "400: gflags: flag out of range\n");
LOG(L_ERR, "ERROR:gflags:read_flag: flag out of range\n");
return 0;
}
/* convert from flag index to flag bitmap */
flag = 1 << flag;
return flag;
}
static int fifo_set_gflag( FILE* pipe, char* response_file )
{
unsigned int flag;
flag=read_flag(pipe, response_file);
if (!flag) {
LOG(L_ERR, "ERROR:gflags:fifo_set_gflag: failed in read_flag\n");
return 1;
}
(*gflags) |= flag;
fifo_reply (response_file, "200 OK\n");
return 1;
}
static int fifo_reset_gflag( FILE* pipe, char* response_file )
{
unsigned int flag;
flag=read_flag(pipe, response_file);
if (!flag) {
LOG(L_ERR, "ERROR:gflags:fifo_reset_gflag: failed in read_flag\n");
return 1;
}
(*gflags) &= ~ flag;
fifo_reply (response_file, "200 OK\n");
return 1;
}
static int fifo_is_gflag( FILE* pipe, char* response_file )
{
unsigned int flag;
flag=read_flag(pipe, response_file);
if (!flag) {
LOG(L_ERR, "ERROR:gflags:fifo_reset_gflag: failed in read_flag\n");
return 1;
}
fifo_reply (response_file, "200 OK\n%s\n",
((*gflags) & flag) ? "TRUE" : "FALSE" );
return 1;
}
static int fifo_get_gflags( FILE* pipe, char* response_file )
{
fifo_reply (response_file, "200 OK\n0x%X\n%u\n", (*gflags),(*gflags));
return 1;
}
static int set_gflag(struct sip_msg *bar, char *flag, char *foo)
{
(*gflags) |= (unsigned int)(long)flag;
return 1;
}
static int reset_gflag(struct sip_msg *bar, char *flag, char *foo)
{
(*gflags) &= ~ ((unsigned int)(long)flag);
return 1;
}
static int is_gflag(struct sip_msg *bar, char *flag, char *foo)
{
return ( (*gflags) & ((unsigned int)(long)flag)) ? 1 : -1;
}
static int mod_init(void)
{
gflags=(unsigned int *) shm_malloc(sizeof(unsigned int));
if (!gflags) {
LOG(L_ERR, "Error: gflags/mod_init: no shmem\n");
return -1;
}
*gflags=initial;
if (register_fifo_cmd(fifo_set_gflag, FIFO_SET_GFLAG, 0) < 0) {
LOG(L_CRIT, "Cannot register FIFO_SET_GFLAG\n");
return -1;
}
if (register_fifo_cmd(fifo_reset_gflag, FIFO_RESET_GFLAG, 0) < 0) {
LOG(L_CRIT, "Cannot register FIFO_RESET_GFLAG\n");
return -1;
}
if (register_fifo_cmd(fifo_is_gflag, FIFO_IS_GFLAG, 0) < 0) {
LOG(L_CRIT, "Cannot register FIFO_SET_GFLAG\n");
return -1;
}
if (register_fifo_cmd(fifo_get_gflags, FIFO_GET_GFLAGS, 0) < 0) {
LOG(L_CRIT, "Cannot register FIFO_SET_GFLAG\n");
return -1;
}
return 0;
}
static void mod_destroy(void)
{
if (gflags)
shm_free(gflags);
}
|