module_json.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <time.h>
  12. #include <assert.h>
  13. #include "../../lib/includes.h"
  14. #include "../../lib/xalloc.h"
  15. #include <sys/time.h>
  16. #include <sys/socket.h>
  17. #include <netinet/in.h>
  18. #include <netinet/tcp.h>
  19. #include <netinet/udp.h>
  20. #include <netinet/ip_icmp.h>
  21. #include <arpa/inet.h>
  22. #include <json.h>
  23. #include "../../lib/logger.h"
  24. #include "output_modules.h"
  25. #include "../probe_modules/probe_modules.h"
  26. static FILE *file = NULL;
  27. int json_output_file_init(struct state_conf *conf, UNUSED char **fields,
  28. UNUSED int fieldlens)
  29. {
  30. assert(conf);
  31. if (!conf->output_filename) {
  32. file = stdout;
  33. } else if (!strcmp(conf->output_filename, "-")) {
  34. file = stdout;
  35. } else {
  36. if (!(file = fopen(conf->output_filename, "w"))) {
  37. log_fatal("output-json", "could not open JSON output file %s",
  38. conf->output_filename);
  39. }
  40. }
  41. check_and_log_file_error(file, "json");
  42. return EXIT_SUCCESS;
  43. }
  44. static void json_output_file_store_data(json_object *obj, const char* name,
  45. const u_char *packet, size_t buflen)
  46. {
  47. char *buf = xmalloc((buflen*2)+1);
  48. for (int i=0; i < (int) buflen; i++) {
  49. snprintf(buf + (i*2), 3, "%.2x", packet[i]);
  50. }
  51. buf[buflen*2] = 0;
  52. json_object_object_add(obj, name, json_object_new_string(buf));
  53. free(buf);
  54. }
  55. int json_output_file_ip(fieldset_t *fs)
  56. {
  57. if (!file) {
  58. return EXIT_SUCCESS;
  59. }
  60. json_object *obj = json_object_new_object();
  61. for (int i=0; i < fs->len; i++) {
  62. field_t *f = &(fs->fields[i]);
  63. if (f->type == FS_STRING) {
  64. json_object_object_add(obj, f->name,
  65. json_object_new_string((char *) f->value.ptr));
  66. } else if (f->type == FS_UINT64) {
  67. json_object_object_add(obj, f->name,
  68. json_object_new_int((int) f->value.num));
  69. } else if (f->type == FS_BINARY) {
  70. json_output_file_store_data(obj, f->name,
  71. (const u_char*) f->value.ptr, f->len);
  72. } else if (f->type == FS_NULL) {
  73. // do nothing
  74. } else {
  75. log_fatal("json", "received unknown output type");
  76. }
  77. }
  78. fprintf(file, "%s\n", json_object_to_json_string(obj));
  79. fflush(file);
  80. check_and_log_file_error(file, "json");
  81. json_object_put(obj);
  82. return EXIT_SUCCESS;
  83. }
  84. int json_output_file_close(UNUSED struct state_conf* c,
  85. UNUSED struct state_send* s, UNUSED struct state_recv* r)
  86. {
  87. if (file) {
  88. fflush(file);
  89. fclose(file);
  90. }
  91. return EXIT_SUCCESS;
  92. }
  93. output_module_t module_json_file = {
  94. .name = "json",
  95. .init = &json_output_file_init,
  96. .filter_duplicates = 0, // framework should not filter out duplicates
  97. .filter_unsuccessful = 0, // framework should not filter out unsuccessful
  98. .start = NULL,
  99. .update = NULL,
  100. .update_interval = 0,
  101. .close = &json_output_file_close,
  102. .process_ip = &json_output_file_ip,
  103. .helptext = "Outputs one or more output fileds as a json valid file. By default, the \n"
  104. "probe module does not filter out duplicates or limit to successful fields, \n"
  105. "but rather includes all received packets. Fields can be controlled by \n"
  106. "setting --output-fields. Filtering out failures and duplicate pakcets can \n"
  107. "be achieved by setting an --output-filter."
  108. };