module_redis_packed.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * ZMap Copyright 2013 Regents of the University of Michigan
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy
  6. * of the License at http://www.apache.org/licenses/LICENSE-2.0
  7. */
  8. #include <stdlib.h>
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <assert.h>
  12. #include <sys/socket.h>
  13. #include <netinet/in.h>
  14. #include <arpa/inet.h>
  15. #include "../../lib/logger.h"
  16. #include "../../lib/xalloc.h"
  17. #include "../../lib/redis.h"
  18. #include "output_modules.h"
  19. #define UNUSED __attribute__((unused))
  20. #define BUFFER_SIZE 500
  21. static uint32_t *buffer;
  22. static int buffer_fill = 0;
  23. static char *queue_name = NULL;
  24. static int field_index = -1;
  25. int redismodule_init(struct state_conf *conf, char **fields, int fieldlens)
  26. {
  27. buffer = xcalloc(BUFFER_SIZE, sizeof(uint32_t));
  28. buffer_fill = 0;
  29. for (int i=0; i < fieldlens; i++) {
  30. if (!strcmp(fields[i], "saddr-raw")) {
  31. field_index = i;
  32. break;
  33. }
  34. }
  35. if (field_index < 0) {
  36. log_fatal("redis-module", "saddr-raw not included in output-fields");
  37. }
  38. if (conf->output_args) {
  39. redisconf_t *rconf = redis_parse_connstr(conf->output_args);
  40. if (rconf->type == T_TCP) {
  41. log_info("redis-module", "{type: TCP, server: %s, "
  42. "port: %u, list: %s}", rconf->server,
  43. rconf->port, rconf->list_name);
  44. } else {
  45. log_info("redis-module", "{type: LOCAL, path: %s, "
  46. "list: %s}", rconf->path, rconf->list_name);
  47. }
  48. queue_name = rconf->list_name;
  49. } else {
  50. queue_name = strdup("zmap_output");
  51. }
  52. return redis_init(conf->output_args);
  53. }
  54. static int redismodule_flush(void)
  55. {
  56. if (redis_lpush((char *)queue_name, buffer,
  57. buffer_fill, sizeof(uint32_t))) {
  58. return EXIT_FAILURE;
  59. }
  60. buffer_fill = 0;
  61. return EXIT_SUCCESS;
  62. }
  63. int redismodule_process(fieldset_t *fs)
  64. {
  65. field_t *f = &(fs->fields[field_index]);
  66. buffer[buffer_fill] = (uint32_t) f->value.num;
  67. if (++buffer_fill == BUFFER_SIZE) {
  68. if (redismodule_flush()) {
  69. return EXIT_FAILURE;
  70. }
  71. }
  72. return EXIT_SUCCESS;
  73. }
  74. int redismodule_close(UNUSED struct state_conf* c,
  75. UNUSED struct state_send* s,
  76. UNUSED struct state_recv* r)
  77. {
  78. if (redismodule_flush()) {
  79. return EXIT_FAILURE;
  80. }
  81. if (redis_close()) {
  82. return EXIT_FAILURE;
  83. }
  84. return EXIT_SUCCESS;
  85. }
  86. output_module_t module_redis = {
  87. .name = "redis-packed",
  88. .init = &redismodule_init,
  89. .start = NULL,
  90. .update = NULL,
  91. .update_interval = 0,
  92. .close = &redismodule_close,
  93. .process_ip = &redismodule_process,
  94. .helptext = "Flushes to redis the ip address as packed binary integer in network order\n"
  95. };