Browse Source

* chg sources next() method to return stag

master
Alex 'AdUser' Z 3 years ago
parent
commit
7cdcbd22e1
  1. 5
      src/source-test.c
  2. 2
      src/source.c
  3. 6
      src/source.h
  4. 6
      src/sources/files.c
  5. 8
      src/sources/portknock.c
  6. 12
      src/sources/redis.c
  7. 2
      src/sources/source.h

5
src/source-test.c

@ -19,6 +19,7 @@ int main(int argc, char *argv[]) {
f2b_config_section_t *section = NULL; f2b_config_section_t *section = NULL;
f2b_source_t *source = NULL; f2b_source_t *source = NULL;
char buf[1024] = ""; char buf[1024] = "";
uint32_t stag;
bool reset; bool reset;
if (argc < 3) if (argc < 3)
@ -54,9 +55,9 @@ int main(int argc, char *argv[]) {
while (1) { while (1) {
reset = true; reset = true;
while (f2b_source_next(source, buf, sizeof(buf), reset)) { while ((stag = f2b_source_next(source, buf, sizeof(buf), reset)) > 0) {
reset = false; reset = false;
puts(buf); printf("stag: %08X, line: %s\n", stag, buf);
} }
sleep(1); sleep(1);
} }

2
src/source.c

@ -122,7 +122,7 @@ f2b_source_destroy(f2b_source_t *source) {
free(source); free(source);
} }
bool uint32_t
f2b_source_next(f2b_source_t *source, char *buf, size_t bufsize, bool reset) { f2b_source_next(f2b_source_t *source, char *buf, size_t bufsize, bool reset) {
assert(source != NULL); assert(source != NULL);
return source->next(source->cfg, buf, bufsize, reset); return source->next(source->cfg, buf, bufsize, reset);

6
src/source.h

@ -29,7 +29,7 @@ typedef struct f2b_source_t {
/** dlsym pointer to handler of @a start command */ /** dlsym pointer to handler of @a start command */
bool (*start) (void *cfg); bool (*start) (void *cfg);
/** dlsym pointer to handler of @a next command */ /** dlsym pointer to handler of @a next command */
bool (*next) (void *cfg, char *buf, size_t bufsize, bool reset); uint32_t (*next) (void *cfg, char *buf, size_t bufsize, bool reset);
/** dlsym pointer to handler of @a stats command */ /** dlsym pointer to handler of @a stats command */
bool (*stats) (void *cfg, char *buf, size_t bufsize); bool (*stats) (void *cfg, char *buf, size_t bufsize);
/** dlsym pointer to handler of @a stop command */ /** dlsym pointer to handler of @a stop command */
@ -74,9 +74,9 @@ bool f2b_source_start (f2b_source_t *s);
* @param buf Buffer for data * @param buf Buffer for data
* @param bufsize Size of buffer for data * @param bufsize Size of buffer for data
* @param reset Reset source internals * @param reset Reset source internals
* @returns false of no data available, true otherwise with setting @a buf * @returns >0 on new data available with filling @a buf and 0 on no data/error
*/ */
bool f2b_source_next (f2b_source_t *s, char *buf, size_t bufsize, bool reset); uint32_t f2b_source_next (f2b_source_t *s, char *buf, size_t bufsize, bool reset);
/** /**
* @brief Stop given source * @brief Stop given source
* @param s Pointer to source struct * @param s Pointer to source struct

6
src/sources/files.c

@ -196,7 +196,7 @@ stop(cfg_t *cfg) {
return true; return true;
} }
bool uint32_t
next(cfg_t *cfg, char *buf, size_t bufsize, bool reset) { next(cfg_t *cfg, char *buf, size_t bufsize, bool reset) {
assert(cfg != NULL); assert(cfg != NULL);
assert(buf != NULL); assert(buf != NULL);
@ -213,10 +213,10 @@ next(cfg_t *cfg, char *buf, size_t bufsize, bool reset) {
continue; continue;
} }
if (file_getline(file, buf, bufsize)) if (file_getline(file, buf, bufsize))
return true; return file->stag;
} }
return false; return 0;
} }
bool bool

8
src/sources/portknock.c

@ -163,7 +163,7 @@ stop(cfg_t *cfg) {
return true; return true;
} }
bool uint32_t
next(cfg_t *cfg, char *buf, size_t bufsize, bool reset) { next(cfg_t *cfg, char *buf, size_t bufsize, bool reset) {
struct sockaddr_storage addr; struct sockaddr_storage addr;
socklen_t addrlen; socklen_t addrlen;
@ -191,16 +191,16 @@ next(cfg_t *cfg, char *buf, size_t bufsize, bool reset) {
close(sock); close(sock);
if (addr.ss_family == AF_INET) { if (addr.ss_family == AF_INET) {
inet_ntop(AF_INET, &(((struct sockaddr_in *) &addr)->sin_addr), buf, bufsize); inet_ntop(AF_INET, &(((struct sockaddr_in *) &addr)->sin_addr), buf, bufsize);
return true; return port->stag;
} }
if (addr.ss_family == AF_INET6) { if (addr.ss_family == AF_INET6) {
inet_ntop(AF_INET6, &(((struct sockaddr_in6 *) &addr)->sin6_addr), buf, bufsize); inet_ntop(AF_INET6, &(((struct sockaddr_in6 *) &addr)->sin6_addr), buf, bufsize);
return true; return port->stag;
} }
cfg->logcb(error, "can't convert sockaddr to string: unknown AF"); cfg->logcb(error, "can't convert sockaddr to string: unknown AF");
} }
return false; return 0;
} }
bool bool

12
src/sources/redis.c

@ -168,9 +168,9 @@ stop(cfg_t *cfg) {
return true; return true;
} }
bool uint32_t
next(cfg_t *cfg, char *buf, size_t bufsize, bool reset) { next(cfg_t *cfg, char *buf, size_t bufsize, bool reset) {
bool gotit = false; uint32_t res = 0;
assert(cfg != NULL); assert(cfg != NULL);
assert(buf != NULL); assert(buf != NULL);
assert(bufsize > 0); assert(bufsize > 0);
@ -180,11 +180,11 @@ next(cfg_t *cfg, char *buf, size_t bufsize, bool reset) {
if (!cfg->conn || cfg->conn->err) if (!cfg->conn || cfg->conn->err)
redis_connect(cfg); redis_connect(cfg);
if (!cfg->conn) if (!cfg->conn)
return false; /* reconnect failure */ return 0; /* reconnect failure */
if (cfg->conn->err) { if (cfg->conn->err) {
log_msg(cfg, error, "connection error: %s", cfg->conn->errstr); log_msg(cfg, error, "connection error: %s", cfg->conn->errstr);
return false; return 0;
} }
redisReply *reply = NULL; redisReply *reply = NULL;
@ -194,7 +194,7 @@ next(cfg_t *cfg, char *buf, size_t bufsize, bool reset) {
if (strcmp(reply->element[0]->str, "message") == 0 || if (strcmp(reply->element[0]->str, "message") == 0 ||
strcmp(reply->element[1]->str, cfg->hash) == 0) { strcmp(reply->element[1]->str, cfg->hash) == 0) {
strlcpy(buf, reply->element[2]->str, bufsize); strlcpy(buf, reply->element[2]->str, bufsize);
gotit = true; res = (uint32_t) -1;
} else { } else {
log_msg(cfg, error, "wrong redis message type: %s", reply->element[0]->str); log_msg(cfg, error, "wrong redis message type: %s", reply->element[0]->str);
} }
@ -208,7 +208,7 @@ next(cfg_t *cfg, char *buf, size_t bufsize, bool reset) {
log_msg(cfg, error, "can't get reply from server %s: %s", cfg->host, cfg->conn->errstr); log_msg(cfg, error, "can't get reply from server %s: %s", cfg->host, cfg->conn->errstr);
} }
return gotit; return res;
} }
bool bool

2
src/sources/source.h

@ -77,7 +77,7 @@ extern bool start(cfg_t *cfg);
* @param reset Reset internals to start of list * @param reset Reset internals to start of list
* @returns false if no new data available, or true otherwise with filling @a buf * @returns false if no new data available, or true otherwise with filling @a buf
*/ */
extern bool next(cfg_t *cfg, char *buf, size_t bufsize, bool reset); extern uint32_t next(cfg_t *cfg, char *buf, size_t bufsize, bool reset);
/** /**
* @brief Get statistics for source * @brief Get statistics for source
* @param cfg Module handler * @param cfg Module handler

Loading…
Cancel
Save