Skip to content

Commit

Permalink
sonic default to unixsocket, and submit auth if password supplied
Browse files Browse the repository at this point in the history
  • Loading branch information
sflow committed Feb 10, 2021
1 parent 4bbb6c2 commit 515d01a
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 6 deletions.
2 changes: 1 addition & 1 deletion hsflowd.spec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Summary: host sFlow daemon
Name: hsflowd
Version: 2.0.32
Version: 2.0.33
Release: 1
License: http://sflow.net/license.html
Group: Applications/Internet
Expand Down
1 change: 1 addition & 0 deletions src/Linux/hsflowconfig.c
Original file line number Diff line number Diff line change
Expand Up @@ -1343,6 +1343,7 @@ extern "C" {
case HSPTOKEN_SONIC:
if((tok = expectToken(sp, tok, HSPTOKEN_STARTOBJ)) == NULL) return NO;
sp->sonic.sonic = YES;
sp->sonic.unixsock = YES;
level[++depth] = HSPOBJ_SONIC;
break;
case HSPTOKEN_DBUS:
Expand Down
1 change: 1 addition & 0 deletions src/Linux/hsflowd.c
Original file line number Diff line number Diff line change
Expand Up @@ -1853,6 +1853,7 @@ extern "C" {
// SONIC should be compiled with "make deb FEATURES="SONIC"
myLog(LOG_INFO, "autoload SONIC and PSAMPLE modules");
sp->sonic.sonic = YES;
sp->sonic.unixsock = YES;
sp->psample.psample = YES;
sp->psample.group = 1;
sp->psample.ds_options = HSP_SAMPLEOPT_IF_SAMPLER
Expand Down
59 changes: 54 additions & 5 deletions src/Linux/mod_sonic.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ extern "C" {
int port;
// or via unix domain socket
char *unixSocketPath;
char *passPath;
EVSocket *sock;
bool connected;
uint32_t reads;
Expand Down Expand Up @@ -139,6 +140,7 @@ extern "C" {
} HSP_mod_SONIC;

static void db_ping(EVMod *mod, HSPSonicDBClient *db);
static bool db_auth(EVMod *mod, HSPSonicDBClient *db);
static bool mapPorts(EVMod *mod);
static bool discoverNewPorts(EVMod *mod);
static void signalCounterDiscontinuity(EVMod *mod, HSPSonicPort *prt);
Expand Down Expand Up @@ -434,7 +436,7 @@ extern "C" {
return UTHashGet(mdata->dbInstances, &search);
}

static HSPSonicDBClient *addDB(EVMod *mod, char *dbInstance, char *hostname, int port, char *unixSocketPath) {
static HSPSonicDBClient *addDB(EVMod *mod, char *dbInstance, char *hostname, int port, char *unixSocketPath, char *passPath) {
HSP_mod_SONIC *mdata = (HSP_mod_SONIC *)mod->data;
myDebug(1, "addDB: %s hostname=%s, port=%d, unixSocketPath=%s", dbInstance, hostname, port, unixSocketPath);
HSPSonicDBClient *db = getDB(mod, dbInstance);
Expand All @@ -446,6 +448,7 @@ extern "C" {
db->hostname = my_strdup(hostname);
db->port = port;
db->unixSocketPath = my_strdup(unixSocketPath);
db->passPath = my_strdup(passPath);
UTHashAdd(mdata->dbInstances, db);
// the socket will be opened later
}
Expand Down Expand Up @@ -541,9 +544,15 @@ extern "C" {
for(cJSON *inst = instances->child; inst; inst = inst->next) {
cJSON *hostname = cJSON_GetObjectItem(inst, "hostname");
cJSON *port = cJSON_GetObjectItem(inst, "port");
cJSON *passPath = cJSON_GetObjectItem(inst, "password_path");
cJSON *unixSock = cJSON_GetObjectItem(inst, "unix_socket_path");
// cJSON *persist = cJSON_GetObjectItem(inst, "persistence_for_warm_boot");
addDB(mod, inst->string, hostname->valuestring, port->valueint, unixSock->valuestring);
addDB(mod,
inst->string,
hostname ? hostname->valuestring : NULL,
port ? port->valueint : 0,
unixSock ? unixSock->valuestring : NULL,
passPath ? passPath->valuestring : NULL);
}
for(cJSON *dbTab = databases->child; dbTab; dbTab = dbTab->next) {
cJSON *id = cJSON_GetObjectItem(dbTab, "id");
Expand Down Expand Up @@ -575,7 +584,8 @@ extern "C" {
configTab->evtClient = addDB(mod, HSP_SONIC_DB_CONFIG_NAME HSP_SONIC_DB_EVENT_SUFFIX,
configTab->dbClient->hostname,
configTab->dbClient->port,
configTab->dbClient->unixSocketPath);
configTab->dbClient->unixSocketPath,
configTab->dbClient->passPath);
}
}

Expand Down Expand Up @@ -663,10 +673,14 @@ extern "C" {
myDebug(1, "sonic db_connectClient %s = %s", db->dbInstance, db->unixSocketPath);
ctx = db->ctx = redisAsyncConnectUnix(db->unixSocketPath);
}
else {
else if(db->hostname
&& db->port) {
myDebug(1, "sonic db_connectClient %s = %s:%d", db->dbInstance, db->hostname, db->port);
ctx = db->ctx = redisAsyncConnect(db->hostname, db->port);
}
else {
myDebug(1, "sonic db_connectClient: missing unixsock or host:port");
}
if(ctx) {
redisAsyncSetConnectCallback(ctx, db_connectCB);
redisAsyncSetDisconnectCallback(ctx, db_disconnectCB);
Expand Down Expand Up @@ -697,7 +711,14 @@ extern "C" {
// so go ahead and issue the first query. Use a neutral "no-op"
// and save the actual discovery queries for the next step once
// everything is connected.
db_ping(mod, db);
if(db->passPath
&& db_auth(mod, db)) {
myDebug(1, "sonic db_connect(%s): auth sent", db->dbInstance);
}
else {
db_ping(mod, db);
myDebug(1, "sonic db_connect(%s): ping sent", db->dbInstance);
}
}
}
}
Expand Down Expand Up @@ -742,6 +763,34 @@ extern "C" {
}


/*_________________---------------------------__________________
_________________ db_auth __________________
-----------------___________________________------------------
*/

static void db_authCB(redisAsyncContext *ctx, void *magic, void *req_magic)
{
HSPSonicDBClient *db = (HSPSonicDBClient *)ctx->ev.data;
redisReply *reply = (redisReply *)magic;
myDebug(1, "sonic db_authCB: %s reply=%s",
db->dbInstance,
db_replyStr(reply, db->replyBuf, YES));
}

static bool db_auth(EVMod *mod, HSPSonicDBClient *db) {
myDebug(1, "sonic db_auth: %s", db->dbInstance);
char dbPasswd[256];
FILE *fp = fopen(db->passPath, "r");
if(fp) {
fgets(dbPasswd, 256, fp);
fclose(fp);
int status = redisAsyncCommand(db->ctx, db_authCB, NULL /*privData*/, "AUTH %s", dbPasswd);
myDebug(1, "sonic db_auth returned %d", status);
return YES;
}
return NO;
}

/*_________________---------------------------__________________
_________________ db_ping __________________
-----------------___________________________------------------
Expand Down

0 comments on commit 515d01a

Please sign in to comment.