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 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
| /*
* This program 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.
*
* This program 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
*
* Author : David Verbeiren from Intel Corporation - July 2007
* Xavier Simonart from Intel Corporation - July 2007
* Philippe Lecluse from Intel Corporation - July 2007
* Patrice Buriez from Intel Corporation - December 2008
*/
#include "sipp.hpp"
#include "user.hpp"
#include "csocket.hpp"
#include "sudp.hpp"
#ifdef HAVE_GSL
#include <gsl/gsl_rng.h>
#endif
static user_list S_user_pool[MAX_USER_POOL];
CUser::CUser(const char* a_Fields, int a_PoolId) :
m_PoolId(-1), m_PoolIndex(0),
m_pSocket(NULL), m_DoKeepAlive(0), m_LastKeepAliveTime(0)
{
// Read user constant fields
std::string line(a_Fields);
int fieldNo = 0;
int start = 0;
do {
int pos = line.find(';', start);
string* val = new string(line.substr(start, pos-start));
if (val->length()) {
//DBGINFO_USR(("User %lx field %d = %s", this, fieldNo, val->c_str()));
m_FieldVect.push_back(val);
}
start = pos;
if (pos != string::npos)
start++;
fieldNo++;
}
while (start != string::npos);
// Initialize user variables
for(int var=0; var<SCEN_VARIABLE_SIZE; var++)
m_VariableTable[var] = NULL;
initVariables();
// Move into initial pool
moveToPool(a_PoolId);
}
CUser::~CUser()
{
if (m_pSocket) {
delete m_pSocket;
}
for(int var=0; var<SCEN_VARIABLE_SIZE; var++) {
if (m_VariableTable[var])
delete m_VariableTable[var];
}
if (m_FieldVect.size()) {
m_FieldVect.clear();
}
}
void delete_users()
{
bool bShouldFirstDisassociateSocket = (transport != T_UDP);
for (int p=0; p<MAX_USER_POOL; p++) {
user_list& rPool = S_user_pool[p];
for (user_list::iterator it = rPool.begin(); it != rPool.end(); ++it) {
if (bShouldFirstDisassociateSocket) {
(*it)->disassociateSocket();
}
delete *it;
}
rPool.clear();
}
}
void CUser::initVariables()
{
// Initialize necessary User Variable based on loaded scenarios
for(int var=0; var<SCEN_VARIABLE_SIZE; var++) {
bool isUsed = false;
for (int scen=0; scen < SCEN_MAX_SCENARIO; scen++) {
if (allscenarios[scen].usedUserVariable[var] > 0) {
//DBGINFO_USR(("Scenario '%s' uses User Variable %d", allscenarios[scen].scenario_name, var));
isUsed = true;
break;
}
}
if (isUsed) {
if (m_VariableTable[var] == NULL) {
DBGINFO_USR(("Allocating user variable %d", var));
m_VariableTable[var] = new CCallVariable();
if (m_VariableTable[var] == NULL) {
ERROR ("User variable allocation failed");
}
}
}
else if (m_VariableTable[var]) {
DBGINFO_USR(("Deleting user variable %d", var));
delete m_VariableTable[var];
m_VariableTable[var] = NULL;
}
}
}
bool CUser::getField(const char* fieldName, char*& dest)
{
int fieldNo = atoi(fieldName+5 /*strlen("field")*/);
if ( (fieldNo > m_FieldVect.size()) || (m_FieldVect[fieldNo] == NULL) ) {
WARNING_P2("User Field %d not found for user %lx", fieldNo, this);
return false;
}
int len = m_FieldVect[fieldNo]->length();
strncpy(dest, m_FieldVect[fieldNo]->c_str(), len);
dest += len;
return true;
}
bool CUser::getVariableValue(int varId, char*& dest)
{
DBGINFO_USR(("CUser::getVariableValue(%d)", varId));
if (varId < SCEN_VARIABLE_SIZE) {
if (m_VariableTable[varId] != NULL) {
if(m_VariableTable[varId]->isSet()) {
dest += sprintf(dest, "%s",
m_VariableTable[varId]->getMatchingValue());
DBGINFO_USR(("USER VARIABLE %d --%s--", varId, m_VariableTable[varId]->getMatchingValue()));
return true;
}
}
}
return false;
}
bool CUser::moveToPool(int a_PoolId)
{
DBGINFO_USR(("Moving user [0x%lx] from %d:%d to pool %d", this, m_PoolId, m_PoolIndex, a_PoolId));
if (m_PoolId != a_PoolId) {
user_list* pPool = &S_user_pool[a_PoolId]; // Note poolId has been verified at scenario parse time
// Remove from current pool
if (m_PoolId != -1) { // Just move the last user from the pool into our current index
user_list* pCurPool = &S_user_pool[m_PoolId];
CUser* pLast = pCurPool->back();
DBGINFO_USR2((" moving last user [0x%lx] from %d:%d to %d:%d", pLast, m_PoolId,
pLast->m_PoolIndex, m_PoolId, m_PoolIndex));
pCurPool->at(m_PoolIndex) = pLast;
pLast->m_PoolIndex = m_PoolIndex;
pCurPool->pop_back();
}
// Put in new pool
pPool->push_back(this);
m_PoolIndex = pPool->size() - 1;
m_PoolId = a_PoolId;
DBGINFO_USR2((" user [0x%lx] now at %d:%d", this, m_PoolId, m_PoolIndex));
}
return true;
}
void CUser::associateSocket(CSocket* a_pSocket)
{
if (a_pSocket) {
m_pSocket = a_pSocket;
}
}
void CUser::dumpInfo()
{
#define MAX_USER_DUMP_SIZE 2000 // Should be big enough, but detect corruption below
char t_Buffer[MAX_USER_DUMP_SIZE+1];
char *ptr = t_Buffer;
ptr += sprintf(ptr, "-USR- %d ", m_PoolId);
for (int i=0; i< m_FieldVect.size(); i++) {
ptr += sprintf(ptr, "{%d:%s}", i, m_FieldVect[i]->c_str());
}
if (m_pSocket) {
ptr += sprintf(ptr, " [%s]", GetSockaddrStorageString(m_pSocket->GetMainIp()));
}
if (ptr-t_Buffer>MAX_USER_DUMP_SIZE) {
ERROR("CUser::DumpInfo() corruption detected");
}
DBGINFO_USR((t_Buffer));
}
int CUser::getPort(bool bClient)
{
if (m_pSocket) {
return m_pSocket->GetPort(bClient);
}
return 0;
}
char *CUser::getFullIp()
{
if (m_pSocket) {
return GetSockaddrStorageString(m_pSocket->GetMainIp());
}
return NULL;
}
bool CUser::FindUser(char *a_User)
{
int fieldNo = 0;
if ( (fieldNo > m_FieldVect.size()) || (m_FieldVect[fieldNo] == NULL) ) {
WARNING_P1("User Field %d not found", fieldNo);
return false;
}
DBGINFO_USR2(("pool %d: %s", a_PoolId, m_FieldVect[fieldNo]->c_str()));
if (strcmp(m_FieldVect[fieldNo]->c_str(), a_User) == 0) {
return true;
}
return false;
}
#ifdef HAVE_GSL
static gsl_rng *s_UserRng = NULL;
#endif
void init_user_rng()
{
#ifdef HAVE_GSL
if (s_UserRng)
return;
init_gsl_rng();
s_UserRng = gsl_rng_alloc(gsl_rng_default);
if (!s_UserRng) {
ERROR("Could not initialize GSL random number generator.\n");
}
#endif
}
void seed_user_rand(unsigned long a_Seed)
{
init_user_rng();
#ifdef HAVE_GSL
DBGINFO_USR(("s_UserRng(0x%lx) seed=%ld", s_UserRng, a_Seed));
gsl_rng_set(s_UserRng, a_Seed);
#endif
}
CUser* pick_user(int a_PoolId, UInt8 *a_User)
{
user_list* pPool = NULL;
user_list::iterator it;
char *ptr = NULL;
if (a_PoolId < MAX_USER_POOL)
pPool = &S_user_pool[a_PoolId];
if (pPool == NULL) {
ERROR_P1("pick_user() - Invalid pool id: %d", a_PoolId);
return NULL;
}
CUser* pUser = NULL;
int sz = pPool->size();
if (sz > 0) {
for (it = pPool->begin(); it != pPool->end(); ++ it) {
int fieldNo = 0;
if ((*it)->FindUser((char *)a_User) == true) {
pUser = *it;
DBGINFO_USR2(("pick_user() from pool 0x%lx", a_PoolId, pUser));
}
}
} else {
WARNING_P1("User pool id[%d] is empty -> Quitting!", a_PoolId);
int i, t_Res, t_Temp = 0;
int t_Array[MAX_USER_POOL];
t_Res = get_user_pool_info(t_Array, MAX_USER_POOL);
printf("Users: ");
for (i=0; i<t_Res; i++) {
WARNING_P2("P%d:%d ",i,t_Array[i]);
t_Temp += t_Array[i];
}
WARNING_P1("T=%d\n", t_Temp);
quitting += 1;
}
return pUser;
}
CUser* pick_user(int a_PoolId)
{
user_list* pPool = NULL;
if (a_PoolId < MAX_USER_POOL)
pPool = &S_user_pool[a_PoolId];
if (pPool == NULL) {
ERROR_P1("pick_user() - Invalid pool id: %d", a_PoolId);
return NULL;
}
CUser* pUser = NULL;
int sz = pPool->size();
if (sz > 0) {
int rnd = gsl_rng_uniform_int(s_UserRng, sz);
pUser = pPool->at(rnd);
DBGINFO_USR2(("pick_user() from pool %d (%d on sz=%d): 0x%lx", a_PoolId, rnd, sz, pUser));
if (sz == 0) {
WARNING_P1("User pool id[%d] is empty -> Quitting!", a_PoolId);
// Add info about in which pools are the users
int t_Res, i, t_Temp = 0;
int t_Array[MAX_USER_POOL];
t_Res = get_user_pool_info(t_Array, MAX_USER_POOL);
printf("Users: ");
for (i=0; i<t_Res; i++) {
WARNING_P2("P%d:%d ",i,t_Array[i]);
t_Temp += t_Array[i];
}
WARNING_P1("T=%d\n", t_Temp)
quitting += 1;
}
}
#if 0
// Don't want this anymore... (create users on demand)
if (pUser == NULL) {
// FOR NOW, we just create users on demand whenever the pool is empty
static unsigned int counter = 0;
char data[] = "subs000000;field0Val;field1Val;field2Val";
sprintf(data+4, "%06lu", counter++);
data[10] = ';';
pUser = new CUser(data, poolId);
}
#endif
return pUser;
}
void add_user(char *a_String)
{
// Extract first field => initial pool id
int poolId = atoi(a_String);
int port = 0;
std::string line(a_String);
int fieldNo = 0;
int pos = line.find("PORT=", 0);
if (pos != string::npos) {
port = atoi(a_String+pos+5);
}
const char empty[] = "";
const char* ptr = strchr(a_String, ';');
if (ptr == NULL)
ptr = empty;
CUser* pUser = new CUser(ptr, poolId);
if (pUser == NULL)
ERROR("Failed to create new user");
#if 1
bool found = false;
CSocket* t_pSocket = NULL;
if (transport == T_UDP) {
t_pSocket = new CSipUdpSocket(g_AsyncPoll, local_ip_is_ipv6);
if (!t_pSocket) {
ERROR_NO("Failed to allocate CSipUdpSocket");
}
t_pSocket->SetMainIp(&sock_start_udp);
if (port == 0) {
for (int j=0;j<2000;j++) {
if (!t_pSocket->Bind(start_udp_port)) {
found = true;
break;
}
start_udp_port++;
if (start_udp_port > 52000) // Temp limit
ERROR_NO("UDP port over 52000 !");
}
} else {
WARNING_P1("Using port %d", port);
if (port > 52000) {// Temp limit
ERROR_NO("UDP port over 52000 !");
} else if (!t_pSocket->Bind(port)) {
found = true;
WARNING_P1("port %d bound", port);
}
}
} else {
// All users share the dual server/client TCP connection to the SUT
t_pSocket = g_SutSocket;
found = true;
}
if (found) {
pUser->associateSocket(t_pSocket);
pUser->dumpInfo();
}
else {
ERROR_P1("Unable to associate UDP port arround %d", start_udp_port);
}
#endif
}
void load_users(const char* fname)
{
ifstream *inFile = new ifstream(fname);
if (!inFile->good()) {
ERROR_P1("Unable to open file %s", fname);
return ;
}
char line[MAX_CHAR_BUFFER_SIZE];
while (!inFile->eof()) {
line[0] = '\0';
inFile->getline(line, MAX_CHAR_BUFFER_SIZE);
if (*line && line[0] != '#') {
add_user(line);
}
}
delete inFile;
}
int get_user_pool_info(int *a_Array, int a_Size)
{
int i;
for (i=0; i<a_Size && i<MAX_USER_POOL; i++) {
*a_Array++ = S_user_pool[i].size();
}
return i;
}
unsigned int get_total_users()
{
unsigned int i;
unsigned int t_Number = 0;
for (i=0; i<MAX_USER_POOL; i++) {
t_Number += S_user_pool[i].size();
}
return t_Number;
}
void init_user_variables()
{
for (int p=0; p<MAX_USER_POOL; p++) {
user_list& rPool = S_user_pool[p];
for (user_list::iterator it = rPool.begin(); it != rPool.end(); ++ it) {
(*it)->initVariables();
}
}
}
///////////////////////////////////////////////////////////////////////////////
void try_users_keep_alive(unsigned long curTime, unsigned short period,
SockAddrStorage *addrDATA)
{
unsigned long trigTime = curTime - period;
const char *msgDATA = "\r\n\r\n";
int msgLen = strlen(msgDATA);
bool unused = 0;
socklen_t addrLen = SOCK_ADDR_SIZE(addrDATA);
for (int p=0; p<MAX_USER_POOL; p++) {
user_list& rPool = S_user_pool[p];
for (user_list::iterator it = rPool.begin(); it != rPool.end(); ++ it) {
CUser *u = *it;
CSocket *tsock;
if (u->getKeepAlive() && u->getLastKeepAliveTime() < trigTime
&& (tsock=u->getSocket())!=NULL ) {
u->setLastKeepAliveTime(curTime);
tsock->SendTo( (unsigned char*)msgDATA, msgLen, addrDATA, addrLen, unused);
}
}
}
}
///////////////////////////////////////////////////////////////////////////
#ifdef MULTI_IP_SUPPORT
void load_users_ip(const char* fname, const char *ip_file)
{
const char empty[] = "";
char t_IpStrings[INET6_ADDRSTRLEN];
char line[MAX_CHAR_BUFFER_SIZE];
int t_Port;
SockAddrStorage t_AddrInfo;
ifstream *ipFile = new ifstream(ip_file);
if (!ipFile->good()) {
ERROR_P1("Unable to open file %s (IP)", fname);
return ;
}
while (!ipFile->eof()) {
line[0] = '\0';
ipFile->getline(line, MAX_CHAR_BUFFER_SIZE);
if (*line && line[0] != '#') {
char *t_Ptr;
int t_ItItem = -1;
unsigned int t_InrementTo = 0;
int t_Current = 0;
unsigned int t_Values[5];
DBGINFO(("Processing: %s", line));
AddIpScheme(line);
}
}
delete ipFile;
ifstream *inFile = new ifstream(fname);
if (!inFile->good()) {
ERROR_P1("Unable to open file %s (users)", fname);
return ;
}
int local_udp_port;
while (!inFile->eof()) {
line[0] = '\0';
inFile->getline(line, MAX_CHAR_BUFFER_SIZE);
if (*line && line[0] != '#') {
int poolId = atoi(line);
char* ptr = strchr(line, ';');
if (!ptr)
ptr = (char*)empty;
if (!BuildIpSockAddrFromIpScheme(&t_AddrInfo)) {
CUser* pUser = new CUser(ptr, poolId);
if (pUser == NULL)
ERROR("Failed to create new user");
CSipUdpSocket *t_SingleUdp = new CSipUdpSocket(g_AsyncPoll, local_ip_is_ipv6);
if (!t_SingleUdp) {
ERROR_NO("Failed to allocate CSipUdpSocket");
}
local_udp_port = start_udp_port+GetIpSchemeLoopCount();
bool found = false;
t_SingleUdp->SetMainIp(&t_AddrInfo);
for (int j=0;j<2000;j++) {
if (!t_SingleUdp->Bind(local_udp_port)) {
found = true;
break;
}
local_udp_port++;
if (local_udp_port > 52000) // Temp limit
ERROR_NO("UDP port over 52000 !");
}
if (found) {
pUser->associateSocket(t_SingleUdp);
pUser->dumpInfo();
}
else {
ERROR_P2("Unable to associate UDP port arround %s port %d", GetSockaddrStorageString(&t_AddrInfo), local_udp_port);
}
} else {
ERROR_NO("BuildIpSockAddrFromIpScheme failed !");
}
}
}
ClearIpScheme();
delete inFile;
}
#endif |
Partager