Esempio n. 1
0
void Profiles::loadProfiles() {
  char **vals;
  Redis *redis = ntop->getRedis();
  int rc;

  rc = (redis ? redis->hashKeys(CONST_PROFILES_PREFS, &vals) : -1);
  if(rc > 0) {
    rc = min_val(rc, MAX_NUM_PROFILES);

    for(int i = 0; i < rc; i++) {
      if(vals[i] != NULL) {
	char contents[2048];

	if(redis->hashGet((char*)CONST_PROFILES_PREFS, vals[i], contents, sizeof(contents)) != -1) {
	  Profile *c = addProfile(vals[i], contents);
	  if(c) profiles[numProfiles++] = c;
	}

	free(vals[i]);
      }
    }

    free(vals);
  }
}
Esempio n. 2
0
void *Redis_create(char *host, int port) {
	redisContext *con;
	con = redisConnect(host, port);

	if (con == NULL || con->err) {
		if (con) {
			printf("Connection error: %s\n", con->errstr);
			redisFree(con);
		} else {
			printf("Connection error: can't allocate redis context\n");
		}
		exit(1);
	}

	Redis proto = { .host = host, .port = port, .con = con, .init = Redis_init,
			.readReply = Redis_readReply, .close = Redis_close, .command =
					Redis_command };

	Redis *m = calloc(1, sizeof(Redis));
	*m = proto;

	if (!m->init(m)) {
		// looks like it didn't initialize properly
		m->close(m);
		return NULL;
	} else {
		// all done, we made an object of any type
		return m;
	}

}
Esempio n. 3
0
int main(int argc, char **argv) {
    char *hostname = (argc > 1) ? argv[1] : "127.0.0.1";
    int port = (argc > 2) ? atoi(argv[2]) : 6379;

    Redis *r = Redis_create(hostname, port);
    r->command(r,"GET foo");
    r->readReply(r);
    r->close(r);
    return 0;
}
Esempio n. 4
0
static int ntop_get_redis_set_pop(lua_State* vm) {
  char *set_name, rsp[512];
  Redis *redis = ntop->getRedis();

  if(ntop_lua_check(vm, __FUNCTION__, 1, LUA_TSTRING)) return(CONST_LUA_ERROR);
  if((set_name = (char*)lua_tostring(vm, 1)) == NULL)  return(CONST_LUA_PARAM_ERROR);
lua_pushfstring(vm, "%s", redis->popSet(set_name, rsp, sizeof(rsp)));

  return(CONST_LUA_OK);
}
Esempio n. 5
0
//redis get set
TEST_F(Gtest, test_Redis_GS) {
	std::string name = "因两";
	Redis *r = new Redis();
	if (!r->connect("localhost", 6379)) {
		printf("connect error!\n");
	}
	r->set("name", name);
	printf("Get the name is %s\n", r->get("name"));

	EXPECT_EQ(name, r->get("name"));
}
Esempio n. 6
0
static int ntop_get_redis(lua_State* vm) {
  char *key, rsp[4096];
  Redis *redis = ntop->getRedis();

  if(ntop_lua_check(vm, __FUNCTION__, 1, LUA_TSTRING)) return(CONST_LUA_ERROR);
  if((key = (char*)lua_tostring(vm, 1)) == NULL)       return(CONST_LUA_PARAM_ERROR);

  lua_pushfstring(vm, "%s", (redis->get(key, rsp, sizeof(rsp)) == 0) ? rsp : (char*)"");

  return(CONST_LUA_OK);
}
Esempio n. 7
0
int main1() {
	Redis *r = new Redis();
	if (!r->connect("localhost", 6379)) {
		printf("connect error!\n");
		return 0;
	}
	r->set("name", "因两");
	printf("Get the name is %s\n", r->get("name"));
	delete r;
	return 0;
}
Esempio n. 8
0
static int ntop_del_hash_redis(lua_State* vm) {
  char *key, *member, rsp[4096];
  Redis *redis = ntop->getRedis();

  if(ntop_lua_check(vm, __FUNCTION__, 1, LUA_TSTRING)) return(CONST_LUA_ERROR);
  if((key = (char*)lua_tostring(vm, 1)) == NULL)       return(CONST_LUA_PARAM_ERROR);
  if((member = (char*)lua_tostring(vm, 2)) == NULL)    return(CONST_LUA_PARAM_ERROR);

  redis->hashDel(key, member);

  return(CONST_LUA_OK);
}
Esempio n. 9
0
//redis keys
TEST_F(Gtest, test_Redis_KEYS) {
	Redis *r = new Redis();
	if (!r->connect("localhost", 6379)) {
		printf("connect error!\n");
	}
	std::list<std::string> list = r->getPrefixKey(ConstValue::USER_PERFIX);
	for (std::list<std::string>::iterator it = list.begin(); it != list.end();
			it++) {
		std::cout << *(it) << std::endl;
	}

//	EXPECT_EQ(name, r->get("name"));
}
Esempio n. 10
0
static int ntop_set_redis(lua_State* vm) {
  char *key, *value;
  Redis *redis = ntop->getRedis();

  if(ntop_lua_check(vm, __FUNCTION__, 1, LUA_TSTRING)) return(CONST_LUA_ERROR);
  if((key = (char*)lua_tostring(vm, 1)) == NULL)       return(CONST_LUA_PARAM_ERROR);

  if(ntop_lua_check(vm, __FUNCTION__, 2, LUA_TSTRING)) return(CONST_LUA_ERROR);
  if((value = (char*)lua_tostring(vm, 2)) == NULL)     return(CONST_LUA_PARAM_ERROR);

  if(redis->set(key, value) == 0)
    return(CONST_LUA_OK);
  else
    return(CONST_LUA_ERROR);
}
Esempio n. 11
0
static void* resolveLoop(void* ptr) {
  AddressResolution *a = (AddressResolution*)ptr;
  Redis *r = ntop->getRedis();

  while(!ntop->getGlobals()->isShutdown()) {
    char numeric_ip[64];
    int rc = r->popHostToResolve(numeric_ip, sizeof(numeric_ip));

    if(rc == 0) {
      a->resolveHostName(numeric_ip);
    } else
      sleep(1);
  }

  return(NULL);
}
Esempio n. 12
0
void* HTTPBL::httpblLoop(void* ptr) {
  HTTPBL *h = (HTTPBL*)ptr;
  Redis *r = ntop->getRedis();

  while(!ntop->getGlobals()->isShutdown()) {
    char numeric_ip[64];

    int rc = r->popHostToHTTPBL(numeric_ip, sizeof(numeric_ip));

    if(rc == 0) {
      h->queryHTTPBL(numeric_ip);
    } else
      sleep(1);
  }

  return(NULL);
}
Esempio n. 13
0
static int ntop_get_resolved_address(lua_State* vm) {
  char *key, *value, rsp[256];
  Redis *redis = ntop->getRedis();

  if(ntop_lua_check(vm, __FUNCTION__, 1, LUA_TSTRING)) return(CONST_LUA_ERROR);
  if((key = (char*)lua_tostring(vm, 1)) == NULL)       return(CONST_LUA_PARAM_ERROR);

  if(redis->getAddress(key, rsp, sizeof(rsp), true) == 0) {
    value = rsp;
  } else {
    value = key;
  }

  lua_pushfstring(vm, "%s", value);

  return(CONST_LUA_OK);
}
Esempio n. 14
0
static int ntop_get_hash_keys_redis(lua_State* vm) {
  char *key, **vals;
  Redis *redis = ntop->getRedis();
  int rc, i;

  if(ntop_lua_check(vm, __FUNCTION__, 1, LUA_TSTRING)) return(CONST_LUA_ERROR);
  if((key    = (char*)lua_tostring(vm, 1)) == NULL)    return(CONST_LUA_PARAM_ERROR);

  rc = redis->hashKeys(key, &vals);

  if(rc > 0) {
    lua_newtable(vm);
    for(i = 0; i < rc; i++) {
      lua_push_str_table_entry(vm, vals[i], (char*)"");
      free(vals[i]);
    }
    free(vals);
  } else
    lua_pushnil(vm);

  return(CONST_LUA_OK);
}
Esempio n. 15
0
void* MySQLDB::queryLoop() {
  Redis *r = ntop->getRedis();
  MYSQL mysql_alt;

  if(connectToDB(&mysql_alt, true) == false)
    return(NULL);

  while(!ntop->getGlobals()->isShutdown()) {
    char sql[2048];
    int rc = r->lpop(CONST_SQL_QUEUE, sql, sizeof(sql));

    if(rc == 0) {
      if(exec_sql_query(&mysql_alt, sql, true, true, false) != 0) {
	ntop->getTrace()->traceEvent(TRACE_ERROR, "MySQL error: %s", get_last_db_error(&mysql));
	ntop->getTrace()->traceEvent(TRACE_ERROR, "%s", sql);
      }
    } else
      sleep(1);    
  }

  mysql_close(&mysql_alt);
  return(NULL);
}