~ubuntu-branches/debian/sid/kamailio/sid

« back to all changes in this revision

Viewing changes to modules/app_lua/app_lua_sr.c

  • Committer: Package Import Robot
  • Author(s): Victor Seva
  • Date: 2014-01-06 11:47:13 UTC
  • mfrom: (1.1.5)
  • Revision ID: package-import@ubuntu.com-20140106114713-t8xidp4arzrnyeya
Tags: 4.1.1-1
* New upstream release
* debian/patches:
  - add upstream fixes
* Added tls outbound websocket autheph dnssec modules
  - openssl exception added to their license
* removing sparc and ia64 from supported archs
  for mono module (Closes: #728915)

Show diffs side-by-side

added added

removed removed

Lines of Context:
39
39
#include "../../dset.h"
40
40
#include "../../parser/parse_uri.h"
41
41
#include "../../lib/kcore/cmpapi.h"
 
42
#include "../../xavp.h"
42
43
 
43
44
#include "app_lua_api.h"
44
45
#include "app_lua_sr.h"
1130
1131
        {NULL, NULL}
1131
1132
};
1132
1133
 
 
1134
 
 
1135
/**
 
1136
 * creates and push a table to the lua stack with
 
1137
 * the elements of the list
 
1138
 */
 
1139
static int lua_sr_push_str_list_table(lua_State *L, struct str_list *list) {
 
1140
        lua_Number i = 1;
 
1141
        struct str_list *k = list;
 
1142
 
 
1143
        lua_newtable(L);
 
1144
        while(k!=NULL){
 
1145
                lua_pushnumber(L, i);
 
1146
                lua_pushlstring(L, k->s.s, k->s.len);
 
1147
                lua_settable(L, -3);
 
1148
                i++;
 
1149
                k = k->next;
 
1150
        }
 
1151
        return 1;
 
1152
}
 
1153
 
 
1154
static int lua_sr_push_xavp_table(lua_State *L, sr_xavp_t *xavp);
 
1155
 
 
1156
/**
 
1157
 * creates and push a table for the key name in xavp
 
1158
 */
 
1159
static void lua_sr_push_xavp_name_table(lua_State *L, sr_xavp_t *xavp, str name) {
 
1160
        lua_Number i = 1;
 
1161
        lua_Number elem = 1;
 
1162
        sr_xavp_t *avp = xavp;
 
1163
 
 
1164
        while(avp!=NULL&&!STR_EQ(avp->name,name))
 
1165
        {
 
1166
                avp = avp->next;
 
1167
        }
 
1168
        lua_newtable(L);
 
1169
 
 
1170
        while(avp!=NULL){
 
1171
                lua_pushnumber(L, elem);
 
1172
                switch(avp->val.type) {
 
1173
                        case SR_XTYPE_NULL:
 
1174
                                lua_pushnil(L);
 
1175
                        break;
 
1176
                        case SR_XTYPE_INT:
 
1177
                                i = avp->val.v.i;
 
1178
                                lua_pushnumber(L, i);
 
1179
                        break;
 
1180
                        case SR_XTYPE_STR:
 
1181
                                lua_pushlstring(L, avp->val.v.s.s, avp->val.v.s.len);
 
1182
                        break;
 
1183
                        case SR_XTYPE_TIME:
 
1184
                        case SR_XTYPE_LONG:
 
1185
                        case SR_XTYPE_LLONG:
 
1186
                        case SR_XTYPE_DATA:
 
1187
                                lua_pushnil(L);
 
1188
                                LM_WARN("XAVP type:%d value not supported\n", avp->val.type);
 
1189
                        break;
 
1190
                        case SR_XTYPE_XAVP:
 
1191
                                if(!lua_sr_push_xavp_table(L,avp->val.v.xavp)){
 
1192
                                        LM_ERR("xavp:%.*s subtable error. Nil value added\n", avp->name.len, avp->name.s);
 
1193
                                        lua_pushnil(L);
 
1194
                                }
 
1195
                        break;
 
1196
                        default:
 
1197
                                LM_ERR("xavp:%.*s unknown type: %d. Nil value added\n",
 
1198
                                        avp->name.len, avp->name.s, avp->val.type);
 
1199
                                lua_pushnil(L);
 
1200
                        break;
 
1201
                }
 
1202
                lua_rawset(L, -3);
 
1203
                elem = elem + 1;
 
1204
                avp = xavp_get_next(avp);
 
1205
        }
 
1206
        lua_setfield(L, -2, name.s);
 
1207
}
 
1208
 
 
1209
/**
 
1210
 * creates and push a table to the lua stack with
 
1211
 * the elements of the xavp
 
1212
 */
 
1213
static int lua_sr_push_xavp_table(lua_State *L, sr_xavp_t *xavp) {
 
1214
        sr_xavp_t *avp = NULL;
 
1215
        struct str_list *keys;
 
1216
        struct str_list *k;
 
1217
 
 
1218
        if(xavp->val.type!=SR_XTYPE_XAVP){
 
1219
                LM_ERR("%s not xavp?\n", xavp->name.s);
 
1220
                return 0;
 
1221
        }
 
1222
        avp = xavp->val.v.xavp;
 
1223
        keys = xavp_get_list_key_names(xavp);
 
1224
 
 
1225
        lua_newtable(L);
 
1226
        if(keys!=NULL)
 
1227
        {
 
1228
                do
 
1229
                {
 
1230
                        lua_sr_push_xavp_name_table(L, avp, keys->s);
 
1231
                        k = keys;
 
1232
                        keys = keys->next;
 
1233
                        pkg_free(k);
 
1234
                }while(keys!=NULL);
 
1235
        }
 
1236
 
 
1237
        return 1;
 
1238
}
 
1239
 
 
1240
 /**
 
1241
 * creates and push a table to the lua stack with
 
1242
 * only the firsts elements of the xavp
 
1243
 */
 
1244
static int lua_sr_push_xavp_table_simple(lua_State *L, sr_xavp_t *xavp) {
 
1245
        lua_Number i = 1;
 
1246
        sr_xavp_t *avp = NULL;
 
1247
 
 
1248
        if(xavp->val.type!=SR_XTYPE_XAVP){
 
1249
                LM_ERR("%s not xavp?\n", xavp->name.s);
 
1250
                return 0;
 
1251
        }
 
1252
        avp = xavp->val.v.xavp;
 
1253
 
 
1254
        lua_newtable(L);
 
1255
        while(avp!=NULL){
 
1256
                switch(avp->val.type) {
 
1257
                        case SR_XTYPE_NULL:
 
1258
                                lua_pushnil(L);
 
1259
                                lua_setfield(L, -2, avp->name.s);
 
1260
                        break;
 
1261
                        case SR_XTYPE_INT:
 
1262
                                i = avp->val.v.i;
 
1263
                                lua_pushnumber(L, i);
 
1264
                                lua_setfield(L, -2, avp->name.s);
 
1265
                        break;
 
1266
                        case SR_XTYPE_STR:
 
1267
                                lua_pushlstring(L, avp->val.v.s.s, avp->val.v.s.len);
 
1268
                                lua_setfield(L, -2, avp->name.s);
 
1269
                        break;
 
1270
                        case SR_XTYPE_TIME:
 
1271
                        case SR_XTYPE_LONG:
 
1272
                        case SR_XTYPE_LLONG:
 
1273
                        case SR_XTYPE_DATA:
 
1274
                                lua_pushnil(L);
 
1275
                                lua_setfield(L, -2, avp->name.s);
 
1276
                                LM_WARN("XAVP type:%d value not supported\n", avp->val.type);
 
1277
                        break;
 
1278
                        case SR_XTYPE_XAVP:
 
1279
                                if(!lua_sr_push_xavp_table(L,avp->val.v.xavp)){
 
1280
                                        LM_ERR("xavp:%.*s subtable error. Nil value added\n", avp->name.len, avp->name.s);
 
1281
                                        lua_pushnil(L);
 
1282
                                }
 
1283
                                lua_setfield(L, -2, avp->name.s);
 
1284
                        break;
 
1285
                }
 
1286
                avp = avp->next;
 
1287
        }
 
1288
        return 1;
 
1289
}
 
1290
 
 
1291
/**
 
1292
 * puts a table with content of a xavp
 
1293
 */
 
1294
static int lua_sr_xavp_get(lua_State *L)
 
1295
{
 
1296
        str xavp_name;
 
1297
        int indx = 0;
 
1298
        sr_lua_env_t *env_L;
 
1299
        sr_xavp_t *avp;
 
1300
        int num_param = 0;
 
1301
        int param = -1;
 
1302
        int simple_flag = 0;
 
1303
 
 
1304
        env_L = sr_lua_env_get();
 
1305
        num_param = lua_gettop(L);
 
1306
        if(num_param<2 && num_param>3)
 
1307
        {
 
1308
                LM_ERR("wrong number of parameters [%d]\n", num_param);
 
1309
                return 0;
 
1310
        }
 
1311
 
 
1312
        if(num_param==3)
 
1313
        {
 
1314
                if(!lua_isnumber(L, param))
 
1315
                {
 
1316
                        LM_ERR("invalid int parameter\n");
 
1317
                        return 0;
 
1318
                }
 
1319
                simple_flag = lua_tointeger(L, param);
 
1320
                param = param - 1;
 
1321
        }
 
1322
 
 
1323
        if(!lua_isnumber(L, param))
 
1324
        {
 
1325
                LM_ERR("invalid int parameter\n");
 
1326
                return 0;
 
1327
        }
 
1328
        indx = lua_tointeger(L, param);
 
1329
        param = param - 1;
 
1330
 
 
1331
        xavp_name.s = (char*)lua_tostring(L, param);
 
1332
        if(xavp_name.s==NULL || env_L->msg==NULL)
 
1333
                return 0;
 
1334
        xavp_name.len = strlen(xavp_name.s);
 
1335
 
 
1336
        avp = xavp_get_by_index(&xavp_name, indx, NULL);
 
1337
        if(avp==NULL){
 
1338
                LM_ERR("can't get xavp:%.*s index:%d\n", xavp_name.len, xavp_name.s, indx);
 
1339
                lua_pushnil(L);
 
1340
                return 1;
 
1341
        }
 
1342
 
 
1343
        if (simple_flag != 0)
 
1344
        {
 
1345
                lua_sr_push_xavp_table_simple(L, avp);
 
1346
        }
 
1347
        else
 
1348
        {
 
1349
                lua_sr_push_xavp_table(L, avp);
 
1350
        }
 
1351
        return 1;
 
1352
}
 
1353
 
 
1354
/**
 
1355
 * puts a table with the list of keys of the xavp
 
1356
 */
 
1357
static int lua_sr_xavp_get_keys (lua_State *L)
 
1358
{
 
1359
        str xavp_name;
 
1360
        int indx = 0;
 
1361
        sr_lua_env_t *env_L;
 
1362
        sr_xavp_t *avp;
 
1363
        struct str_list *keys, *k;
 
1364
 
 
1365
        env_L = sr_lua_env_get();
 
1366
 
 
1367
        if(lua_gettop(L)<2)
 
1368
        {
 
1369
                LM_ERR("to few parameters [%d]\n",lua_gettop(L));
 
1370
                return 0;
 
1371
        }
 
1372
 
 
1373
        if(!lua_isnumber(L, -1))
 
1374
        {
 
1375
                LM_ERR("invalid int parameter\n");
 
1376
                return 0;
 
1377
        }
 
1378
        indx = lua_tointeger(L, -1);
 
1379
 
 
1380
        xavp_name.s = (char*)lua_tostring(L, -2);
 
1381
        if(xavp_name.s==NULL || env_L->msg==NULL)
 
1382
                return 0;
 
1383
        xavp_name.len = strlen(xavp_name.s);
 
1384
 
 
1385
        avp = xavp_get_by_index(&xavp_name, indx, NULL);
 
1386
        if(avp==NULL){
 
1387
                LM_ERR("can't get xavp:%.*s index:%d\n", xavp_name.len, xavp_name.s, indx);
 
1388
                lua_pushnil(L);
 
1389
                return 1;
 
1390
        }
 
1391
        keys = xavp_get_list_key_names(avp);
 
1392
        lua_sr_push_str_list_table(L, keys);
 
1393
        // free list
 
1394
        while(keys!=NULL){
 
1395
                k = keys;
 
1396
                keys = k->next;
 
1397
                pkg_free(k);
 
1398
        }
 
1399
        return 1;
 
1400
}
 
1401
 
 
1402
/**
 
1403
 *
 
1404
 */
 
1405
static const luaL_reg _sr_xavp_Map [] = {
 
1406
        {"get", lua_sr_xavp_get},
 
1407
        {"get_keys",  lua_sr_xavp_get_keys},
 
1408
        {NULL, NULL}
 
1409
};
 
1410
 
1133
1411
/**
1134
1412
 *
1135
1413
 */
1138
1416
        luaL_openlib(L, "sr",      _sr_core_Map, 0);
1139
1417
        luaL_openlib(L, "sr.hdr",  _sr_hdr_Map,  0);
1140
1418
        luaL_openlib(L, "sr.pv",   _sr_pv_Map,   0);
 
1419
        luaL_openlib(L, "sr.xavp", _sr_xavp_Map, 0);
1141
1420
}
1142
 
 
1143