113 for (
int i = 0; i < 4; i++) {
114 uint64_t memcap = memcaps[i].
GetFunc();
117 float p = (float)((
double)memuse / (double)memcap);
120 percent =
MAX(
p, percent);
126 #ifdef BUILD_UNIX_SOCKET
128 static int RunModeUnixSocketMaster(
void);
129 static int unix_manager_pcap_task_running = 0;
130 static int unix_manager_pcap_task_failed = 0;
131 static int unix_manager_pcap_task_interrupted = 0;
132 static struct timespec unix_manager_pcap_last_processed;
133 static SCCtrlMutex unix_manager_pcap_last_processed_mutex;
140 static TmEcode UnixSocketPcapFilesList(json_t *cmd, json_t* answer,
void *data)
148 jdata = json_object();
150 json_object_set_new(answer,
"message",
151 json_string(
"internal error at json object creation"));
154 jarray = json_array();
155 if (jarray == NULL) {
157 json_object_set_new(answer,
"message",
158 json_string(
"internal error at json object creation"));
165 json_object_set_new(jdata,
"count", json_integer(i));
166 json_object_set_new(jdata,
"files", jarray);
167 json_object_set_new(answer,
"message", jdata);
171 static TmEcode UnixSocketPcapFilesNumber(json_t *cmd, json_t* answer,
void *data)
180 json_object_set_new(answer,
"message", json_integer(i));
184 static TmEcode UnixSocketPcapCurrent(json_t *cmd, json_t* answer,
void *data)
188 if (this->current_file != NULL && this->current_file->filename != NULL) {
189 json_object_set_new(answer,
"message",
190 json_string(this->current_file->filename));
192 json_object_set_new(answer,
"message", json_string(
"None"));
197 static TmEcode UnixSocketPcapLastProcessed(json_t *cmd, json_t *answer,
void *data)
199 json_int_t epoch_millis;
204 json_object_set_new(answer,
"message",
205 json_integer(epoch_millis));
210 static TmEcode UnixSocketPcapInterrupt(json_t *cmd, json_t *answer,
void *data)
212 unix_manager_pcap_task_interrupted = 1;
214 json_object_set_new(answer,
"message", json_string(
"Interrupted"));
219 static void PcapFilesFree(
PcapFiles *cfile)
244 static TmEcode UnixListAddFile(
PcapCommand *
this,
const char *filename,
const char *output_dir,
245 uint32_t tenant_id,
bool continuous,
bool should_delete, time_t delay, time_t poll_interval)
248 if (filename == NULL ||
this == NULL)
276 cfile->
delay = delay;
291 static TmEcode UnixSocketAddPcapFileImpl(json_t *cmd, json_t* answer,
void *data,
295 const char *filename;
296 const char *output_dir;
297 uint32_t tenant_id = 0;
298 bool should_delete =
false;
300 time_t poll_interval = 5;
303 json_t *jarg = json_object_get(cmd,
"filename");
304 if (!json_is_string(jarg)) {
306 json_object_set_new(answer,
"message",
307 json_string(
"filename is not a string"));
310 filename = json_string_value(jarg);
312 json_object_set_new(answer,
"message",
313 json_string(
"filename does not exist"));
317 json_t *oarg = json_object_get(cmd,
"output-dir");
319 if (!json_is_string(oarg)) {
322 json_object_set_new(answer,
"message",
323 json_string(
"output-dir is not a string"));
326 output_dir = json_string_value(oarg);
330 json_object_set_new(answer,
"message",
331 json_string(
"output-dir param is mandatory"));
335 if (
SCStatFn(output_dir, &st) != 0) {
336 json_object_set_new(answer,
"message",
337 json_string(
"output-dir does not exist"));
341 json_t *targ = json_object_get(cmd,
"tenant");
343 if (!json_is_integer(targ)) {
344 json_object_set_new(answer,
"message",
345 json_string(
"tenant is not a number"));
348 tenant_id = json_number_value(targ);
351 json_t *delete_arg = json_object_get(cmd,
"delete-when-done");
352 if (delete_arg != NULL) {
353 should_delete = json_is_true(delete_arg);
356 json_t *delay_arg = json_object_get(cmd,
"delay");
357 if (delay_arg != NULL) {
358 if (!json_is_integer(delay_arg)) {
360 json_object_set_new(answer,
"message",
361 json_string(
"delay is not a integer"));
364 delay = json_integer_value(delay_arg);
367 json_t *interval_arg = json_object_get(cmd,
"poll-interval");
368 if (interval_arg != NULL) {
369 if (!json_is_integer(interval_arg)) {
372 json_object_set_new(answer,
"message",
373 json_string(
"poll-interval is not a integer"));
376 poll_interval = json_integer_value(interval_arg);
379 switch (UnixListAddFile(
this, filename, output_dir, tenant_id, continuous,
380 should_delete, delay, poll_interval)) {
383 json_object_set_new(answer,
"message",
384 json_string(
"Unable to add file to list"));
387 SCLogInfo(
"Added file '%s' to list", filename);
388 json_object_set_new(answer,
"message",
389 json_string(
"Successfully added file to list"));
402 static TmEcode UnixSocketAddPcapFile(json_t *cmd, json_t* answer,
void *data)
404 bool continuous =
false;
406 json_t *cont_arg = json_object_get(cmd,
"continuous");
407 if (cont_arg != NULL) {
408 continuous = json_is_true(cont_arg);
411 return UnixSocketAddPcapFileImpl(cmd, answer, data, continuous);
421 static TmEcode UnixSocketAddPcapFileContinuous(json_t *cmd, json_t* answer,
void *data)
423 return UnixSocketAddPcapFileImpl(cmd, answer, data,
true);
439 static TmEcode UnixSocketPcapFilesCheck(
void *data)
442 if (unix_manager_pcap_task_running == 1) {
445 if ((unix_manager_pcap_task_failed == 1) || (this->running == 1)) {
446 if (unix_manager_pcap_task_failed) {
447 SCLogInfo(
"Preceeding task failed, cleaning the running mode");
449 unix_manager_pcap_task_failed = 0;
455 if (this->current_file) {
456 PcapFilesFree(this->current_file);
458 this->current_file = NULL;
469 unix_manager_pcap_task_running = 1;
474 PcapFilesFree(cfile);
485 SCLogError(
"Can not set continuous mode for pcap processing");
486 PcapFilesFree(cfile);
495 SCLogError(
"Can not set delete mode for pcap processing");
496 PcapFilesFree(cfile);
500 if (cfile->
delay > 0) {
502 snprintf(tstr,
sizeof(tstr),
"%" PRIuMAX, (uintmax_t)cfile->
delay);
504 SCLogError(
"Can not set delay to '%s'", tstr);
505 PcapFilesFree(cfile);
512 snprintf(tstr,
sizeof(tstr),
"%" PRIuMAX, (uintmax_t)cfile->
poll_interval);
514 SCLogError(
"Can not set poll-interval to '%s'", tstr);
515 PcapFilesFree(cfile);
522 snprintf(tstr,
sizeof(tstr),
"%u", cfile->
tenant_id);
524 SCLogError(
"Can not set working tenant-id to '%s'", tstr);
525 PcapFilesFree(cfile);
529 SCLogInfo(
"pcap-file.tenant-id not set");
535 PcapFilesFree(cfile);
540 this->current_file = cfile;
542 SCLogInfo(
"Starting run for '%s'", this->current_file->filename);
559 #ifdef BUILD_UNIX_SOCKET
570 #ifdef BUILD_UNIX_SOCKET
573 unix_manager_pcap_last_processed.tv_sec = last_processed->tv_sec;
574 unix_manager_pcap_last_processed.tv_nsec = last_processed->tv_nsec;
579 SCLogInfo(
"Marking current task as done");
580 unix_manager_pcap_task_running = 0;
583 SCLogInfo(
"Marking current task as failed");
584 unix_manager_pcap_task_running = 0;
585 unix_manager_pcap_task_failed = 1;
589 if (unix_manager_pcap_task_interrupted == 1) {
590 SCLogInfo(
"Interrupting current run mode");
591 unix_manager_pcap_task_interrupted = 0;
601 #ifdef BUILD_UNIX_SOCKET
609 TmEcode UnixSocketDatasetAdd(json_t *cmd, json_t* answer,
void *data)
612 json_t *narg = json_object_get(cmd,
"setname");
613 if (!json_is_string(narg)) {
614 json_object_set_new(answer,
"message", json_string(
"setname is not a string"));
617 const char *set_name = json_string_value(narg);
620 json_t *targ = json_object_get(cmd,
"settype");
621 if (!json_is_string(targ)) {
622 json_object_set_new(answer,
"message", json_string(
"settype is not a string"));
625 const char *
type = json_string_value(targ);
628 json_t *varg = json_object_get(cmd,
"datavalue");
629 if (!json_is_string(varg)) {
630 json_object_set_new(answer,
"message", json_string(
"datavalue is not string"));
633 const char *value = json_string_value(varg);
635 SCLogDebug(
"dataset-add: %s type %s value %s", set_name,
type, value);
639 json_object_set_new(answer,
"message", json_string(
"unknown settype"));
645 json_object_set_new(answer,
"message", json_string(
"set not found or wrong type"));
651 json_object_set_new(answer,
"message", json_string(
"data added"));
654 json_object_set_new(answer,
"message", json_string(
"data already in set"));
657 json_object_set_new(answer,
"message", json_string(
"failed to add data"));
662 TmEcode UnixSocketDatasetRemove(json_t *cmd, json_t* answer,
void *data)
665 json_t *narg = json_object_get(cmd,
"setname");
666 if (!json_is_string(narg)) {
667 json_object_set_new(answer,
"message", json_string(
"setname is not a string"));
670 const char *set_name = json_string_value(narg);
673 json_t *targ = json_object_get(cmd,
"settype");
674 if (!json_is_string(targ)) {
675 json_object_set_new(answer,
"message", json_string(
"settype is not a string"));
678 const char *
type = json_string_value(targ);
681 json_t *varg = json_object_get(cmd,
"datavalue");
682 if (!json_is_string(varg)) {
683 json_object_set_new(answer,
"message", json_string(
"datavalue is not string"));
686 const char *value = json_string_value(varg);
688 SCLogDebug(
"dataset-remove: %s type %s value %s", set_name,
type, value);
692 json_object_set_new(answer,
"message", json_string(
"unknown settype"));
698 json_object_set_new(answer,
"message", json_string(
"set not found or wrong type"));
704 json_object_set_new(answer,
"message", json_string(
"data removed"));
707 json_object_set_new(answer,
"message", json_string(
"data is busy, try again"));
710 json_object_set_new(answer,
"message", json_string(
"failed to remove data"));
715 TmEcode UnixSocketDatasetDump(json_t *cmd, json_t *answer,
void *data)
720 json_object_set_new(answer,
"message", json_string(
"datasets dump done"));
724 TmEcode UnixSocketDatasetClear(json_t *cmd, json_t *answer,
void *data)
727 json_t *narg = json_object_get(cmd,
"setname");
728 if (!json_is_string(narg)) {
729 json_object_set_new(answer,
"message", json_string(
"setname is not a string"));
732 const char *set_name = json_string_value(narg);
735 json_t *targ = json_object_get(cmd,
"settype");
736 if (!json_is_string(targ)) {
737 json_object_set_new(answer,
"message", json_string(
"settype is not a string"));
740 const char *
type = json_string_value(targ);
744 json_object_set_new(answer,
"message", json_string(
"unknown settype"));
750 json_object_set_new(answer,
"message", json_string(
"set not found or wrong type"));
756 json_object_set_new(answer,
"message", json_string(
"dataset cleared"));
760 TmEcode UnixSocketDatasetLookup(json_t *cmd, json_t *answer,
void *data)
763 json_t *narg = json_object_get(cmd,
"setname");
764 if (!json_is_string(narg)) {
765 json_object_set_new(answer,
"message", json_string(
"setname is not a string"));
768 const char *set_name = json_string_value(narg);
771 json_t *targ = json_object_get(cmd,
"settype");
772 if (!json_is_string(targ)) {
773 json_object_set_new(answer,
"message", json_string(
"settype is not a string"));
776 const char *
type = json_string_value(targ);
779 json_t *varg = json_object_get(cmd,
"datavalue");
780 if (!json_is_string(varg)) {
781 json_object_set_new(answer,
"message", json_string(
"datavalue is not string"));
784 const char *value = json_string_value(varg);
786 SCLogDebug(
"dataset-exist: %s type %s value %s", set_name,
type, value);
790 json_object_set_new(answer,
"message", json_string(
"unknown settype"));
796 json_object_set_new(answer,
"message", json_string(
"set not found or wrong type"));
801 json_object_set_new(answer,
"message", json_string(
"item found in set"));
804 json_object_set_new(answer,
"message", json_string(
"item not found in set"));
816 TmEcode UnixSocketDatajsonAdd(json_t *cmd, json_t *answer,
void *data)
819 json_t *narg = json_object_get(cmd,
"setname");
820 if (!json_is_string(narg)) {
821 json_object_set_new(answer,
"message", json_string(
"setname is not a string"));
824 const char *set_name = json_string_value(narg);
827 json_t *targ = json_object_get(cmd,
"settype");
828 if (!json_is_string(targ)) {
829 json_object_set_new(answer,
"message", json_string(
"settype is not a string"));
832 const char *
type = json_string_value(targ);
835 json_t *varg = json_object_get(cmd,
"datavalue");
836 if (!json_is_string(varg)) {
837 json_object_set_new(answer,
"message", json_string(
"datavalue is not string"));
840 const char *value = json_string_value(varg);
843 json_t *jarg = json_object_get(cmd,
"datajson");
844 if (!json_is_string(varg)) {
845 json_object_set_new(answer,
"message", json_string(
"datajson is not string"));
848 const char *json = json_string_value(jarg);
850 SCLogDebug(
"datajson-add: %s type %s value %s json %s", set_name,
type, value, json);
854 json_object_set_new(answer,
"message", json_string(
"unknown settype"));
860 json_object_set_new(answer,
"message", json_string(
"set not found or wrong type"));
866 json_object_set_new(answer,
"message", json_string(
"data added"));
869 json_object_set_new(answer,
"message", json_string(
"data already in set"));
872 json_object_set_new(answer,
"message", json_string(
"failed to add data"));
877 static bool JsonU32Value(json_t *jarg, uint32_t *ret)
879 int64_t r = json_integer_value(jarg);
880 if (r < 0 || r > UINT32_MAX) {
894 TmEcode UnixSocketRegisterTenantHandler(json_t *cmd, json_t* answer,
void *data)
897 json_int_t traffic_id = -1;
900 SCLogInfo(
"error: multi-tenant support not enabled");
901 json_object_set_new(answer,
"message", json_string(
"multi-tenant support not enabled"));
906 json_t *jarg = json_object_get(cmd,
"id");
907 if (!json_is_integer(jarg)) {
908 SCLogInfo(
"error: command is not a string");
909 json_object_set_new(answer,
"message", json_string(
"id is not an integer"));
913 if (!JsonU32Value(jarg, &tenant_id)) {
915 json_object_set_new(answer,
"message", json_string(
"tenant_id is not a uint32"));
920 jarg = json_object_get(cmd,
"htype");
921 if (!json_is_string(jarg)) {
922 SCLogInfo(
"error: command is not a string");
923 json_object_set_new(answer,
"message", json_string(
"command is not a string"));
926 htype = json_string_value(jarg);
928 SCLogDebug(
"add-tenant-handler: %d %s", tenant_id, htype);
931 json_t *hargs = json_object_get(cmd,
"hargs");
933 if (!json_is_integer(hargs)) {
935 json_object_set_new(answer,
"message", json_string(
"hargs not a number"));
938 traffic_id = json_integer_value(hargs);
943 if (strcmp(htype,
"pcap") == 0) {
945 }
else if (strcmp(htype,
"vlan") == 0) {
946 if (traffic_id < 0) {
947 json_object_set_new(answer,
"message", json_string(
"vlan requires argument"));
950 if (traffic_id > USHRT_MAX) {
951 json_object_set_new(answer,
"message", json_string(
"vlan argument out of range"));
955 SCLogInfo(
"VLAN handler: id %u maps to tenant %u", (uint32_t)traffic_id, tenant_id);
959 json_object_set_new(answer,
"message", json_string(
"handler setup failure"));
964 json_object_set_new(answer,
"message", json_string(
"couldn't apply settings"));
971 json_object_set_new(answer,
"message", json_string(
"handler added"));
982 TmEcode UnixSocketUnregisterTenantHandler(json_t *cmd, json_t* answer,
void *data)
985 json_int_t traffic_id = -1;
988 SCLogInfo(
"error: multi-tenant support not enabled");
989 json_object_set_new(answer,
"message", json_string(
"multi-tenant support not enabled"));
994 json_t *jarg = json_object_get(cmd,
"id");
995 if (!json_is_integer(jarg)) {
996 SCLogInfo(
"error: command is not a string");
997 json_object_set_new(answer,
"message", json_string(
"id is not an integer"));
1001 if (!JsonU32Value(jarg, &tenant_id)) {
1003 json_object_set_new(answer,
"message", json_string(
"tenant_id is not a uint32"));
1008 jarg = json_object_get(cmd,
"htype");
1009 if (!json_is_string(jarg)) {
1010 SCLogInfo(
"error: command is not a string");
1011 json_object_set_new(answer,
"message", json_string(
"command is not a string"));
1014 htype = json_string_value(jarg);
1016 SCLogDebug(
"add-tenant-handler: %d %s", tenant_id, htype);
1019 json_t *hargs = json_object_get(cmd,
"hargs");
1020 if (hargs != NULL) {
1021 if (!json_is_integer(hargs)) {
1023 json_object_set_new(answer,
"message", json_string(
"hargs not a number"));
1026 traffic_id = json_integer_value(hargs);
1031 if (strcmp(htype,
"pcap") == 0) {
1033 }
else if (strcmp(htype,
"vlan") == 0) {
1034 if (traffic_id < 0) {
1035 json_object_set_new(answer,
"message", json_string(
"vlan requires argument"));
1038 if (traffic_id > USHRT_MAX) {
1039 json_object_set_new(answer,
"message", json_string(
"vlan argument out of range"));
1043 SCLogInfo(
"VLAN handler: removing mapping of %u to tenant %u", (uint32_t)traffic_id, tenant_id);
1047 json_object_set_new(answer,
"message", json_string(
"handler unregister failure"));
1053 json_object_set_new(answer,
"message", json_string(
"couldn't apply settings"));
1060 json_object_set_new(answer,
"message", json_string(
"handler removed"));
1071 TmEcode UnixSocketRegisterTenant(json_t *cmd, json_t* answer,
void *data)
1073 const char *filename;
1077 SCLogInfo(
"error: multi-tenant support not enabled");
1078 json_object_set_new(answer,
"message", json_string(
"multi-tenant support not enabled"));
1083 json_t *jarg = json_object_get(cmd,
"id");
1084 if (!json_is_integer(jarg)) {
1085 json_object_set_new(answer,
"message", json_string(
"id is not an integer"));
1089 if (!JsonU32Value(jarg, &tenant_id)) {
1091 json_object_set_new(answer,
"message", json_string(
"tenant_id is not a uint32"));
1096 jarg = json_object_get(cmd,
"filename");
1097 if (!json_is_string(jarg)) {
1098 json_object_set_new(answer,
"message", json_string(
"command is not a string"));
1101 filename = json_string_value(jarg);
1102 if (
SCStatFn(filename, &st) != 0) {
1103 json_object_set_new(answer,
"message", json_string(
"file does not exist"));
1107 SCLogDebug(
"add-tenant: %d %s", tenant_id, filename);
1112 snprintf(prefix,
sizeof(prefix),
"multi-detect.%u", tenant_id);
1114 SCLogError(
"failed to load yaml %s", filename);
1115 json_object_set_new(answer,
"message", json_string(
"failed to load yaml"));
1121 json_object_set_new(answer,
"message", json_string(
"adding tenant failed"));
1127 json_object_set_new(answer,
"message", json_string(
"couldn't apply settings"));
1134 json_object_set_new(answer,
"message", json_string(
"adding tenant succeeded"));
1138 static int reload_cnt = 1;
1146 TmEcode UnixSocketReloadTenant(json_t *cmd, json_t* answer,
void *data)
1148 const char *filename = NULL;
1152 SCLogInfo(
"error: multi-tenant support not enabled");
1153 json_object_set_new(answer,
"message", json_string(
"multi-tenant support not enabled"));
1158 json_t *jarg = json_object_get(cmd,
"id");
1159 if (!json_is_integer(jarg)) {
1160 json_object_set_new(answer,
"message", json_string(
"id is not an integer"));
1164 if (!JsonU32Value(jarg, &tenant_id)) {
1166 json_object_set_new(answer,
"message", json_string(
"tenant_id is not a uint32"));
1171 jarg = json_object_get(cmd,
"filename");
1173 if (!json_is_string(jarg)) {
1174 json_object_set_new(answer,
"message", json_string(
"command is not a string"));
1177 filename = json_string_value(jarg);
1178 if (
SCStatFn(filename, &st) != 0) {
1179 json_object_set_new(answer,
"message", json_string(
"file does not exist"));
1184 SCLogDebug(
"reload-tenant: %d %s", tenant_id, filename);
1188 json_object_set_new(answer,
"message", json_string(
"reload tenant failed"));
1196 json_object_set_new(answer,
"message", json_string(
"couldn't apply settings"));
1203 json_object_set_new(answer,
"message", json_string(
"reloading tenant succeeded"));
1214 TmEcode UnixSocketReloadTenants(json_t *cmd, json_t *answer,
void *data)
1217 SCLogInfo(
"error: multi-tenant support not enabled");
1218 json_object_set_new(answer,
"message", json_string(
"multi-tenant support not enabled"));
1223 json_object_set_new(answer,
"message", json_string(
"reload tenants failed"));
1231 json_object_set_new(answer,
"message", json_string(
"couldn't apply settings"));
1239 json_object_set_new(answer,
"message", json_string(
"reloading tenants succeeded"));
1250 TmEcode UnixSocketUnregisterTenant(json_t *cmd, json_t* answer,
void *data)
1253 SCLogInfo(
"error: multi-tenant support not enabled");
1254 json_object_set_new(answer,
"message", json_string(
"multi-tenant support not enabled"));
1259 json_t *jarg = json_object_get(cmd,
"id");
1260 if (!json_is_integer(jarg)) {
1261 SCLogInfo(
"error: command is not a string");
1262 json_object_set_new(answer,
"message", json_string(
"id is not an integer"));
1266 if (!JsonU32Value(jarg, &tenant_id)) {
1268 json_object_set_new(answer,
"message", json_string(
"tenant_id is not a uint32"));
1272 SCLogInfo(
"remove-tenant: removing tenant %d", tenant_id);
1276 snprintf(prefix,
sizeof(prefix),
"multi-detect.%u", tenant_id);
1280 json_object_set_new(answer,
"message", json_string(
"tenant detect engine not found"));
1290 json_object_set_new(answer,
"message", json_string(
"couldn't apply settings"));
1300 json_object_set_new(answer,
"message", json_string(
"removing tenant succeeded"));
1310 TmEcode UnixSocketHostbitAdd(json_t *cmd, json_t* answer,
void *data_usused)
1313 json_t *jarg = json_object_get(cmd,
"ipaddress");
1314 if (!json_is_string(jarg)) {
1315 json_object_set_new(answer,
"message", json_string(
"ipaddress is not an string"));
1318 const char *ipaddress = json_string_value(jarg);
1322 memset(&in, 0,
sizeof(in));
1323 if (inet_pton(AF_INET, ipaddress, &in) != 1) {
1325 memset(&in6, 0,
sizeof(in6));
1326 if (inet_pton(AF_INET6, ipaddress, &in6) != 1) {
1327 json_object_set_new(answer,
"message", json_string(
"invalid address string"));
1331 a.addr_data32[0] = in6[0];
1332 a.addr_data32[1] = in6[1];
1333 a.addr_data32[2] = in6[2];
1334 a.addr_data32[3] = in6[3];
1338 a.addr_data32[0] = in.s_addr;
1339 a.addr_data32[1] = 0;
1340 a.addr_data32[2] = 0;
1341 a.addr_data32[3] = 0;
1345 jarg = json_object_get(cmd,
"hostbit");
1346 if (!json_is_string(jarg)) {
1347 json_object_set_new(answer,
"message", json_string(
"hostbit is not a string"));
1350 const char *hostbit = json_string_value(jarg);
1353 json_object_set_new(answer,
"message", json_string(
"hostbit not found"));
1358 jarg = json_object_get(cmd,
"expire");
1359 if (!json_is_integer(jarg)) {
1360 json_object_set_new(answer,
"message", json_string(
"expire is not an integer"));
1364 if (!JsonU32Value(jarg, &expire)) {
1366 json_object_set_new(answer,
"message", json_string(
"expire is not a uint32"));
1370 SCLogInfo(
"add-hostbit: ip %s hostbit %s expire %us", ipaddress, hostbit, expire);
1375 if (
SCTIME_SECS(current_time) + expire > UINT32_MAX) {
1376 json_object_set_new(answer,
"message", json_string(
"couldn't set host expire"));
1383 json_object_set_new(answer,
"message", json_string(
"hostbit added"));
1386 json_object_set_new(answer,
"message", json_string(
"couldn't create host"));
1397 TmEcode UnixSocketHostbitRemove(json_t *cmd, json_t* answer,
void *data_unused)
1400 json_t *jarg = json_object_get(cmd,
"ipaddress");
1401 if (!json_is_string(jarg)) {
1402 json_object_set_new(answer,
"message", json_string(
"ipaddress is not an string"));
1405 const char *ipaddress = json_string_value(jarg);
1409 memset(&in, 0,
sizeof(in));
1410 if (inet_pton(AF_INET, ipaddress, &in) != 1) {
1412 memset(&in6, 0,
sizeof(in6));
1413 if (inet_pton(AF_INET6, ipaddress, &in6) != 1) {
1414 json_object_set_new(answer,
"message", json_string(
"invalid address string"));
1418 a.addr_data32[0] = in6[0];
1419 a.addr_data32[1] = in6[1];
1420 a.addr_data32[2] = in6[2];
1421 a.addr_data32[3] = in6[3];
1425 a.addr_data32[0] = in.s_addr;
1426 a.addr_data32[1] = 0;
1427 a.addr_data32[2] = 0;
1428 a.addr_data32[3] = 0;
1432 jarg = json_object_get(cmd,
"hostbit");
1433 if (!json_is_string(jarg)) {
1434 json_object_set_new(answer,
"message", json_string(
"hostbit is not a string"));
1438 const char *hostbit = json_string_value(jarg);
1441 json_object_set_new(answer,
"message", json_string(
"hostbit not found"));
1445 SCLogInfo(
"remove-hostbit: %s %s", ipaddress, hostbit);
1451 json_object_set_new(answer,
"message", json_string(
"hostbit removed"));
1454 json_object_set_new(answer,
"message", json_string(
"host not found"));
1470 TmEcode UnixSocketHostbitList(json_t *cmd, json_t* answer,
void *data_unused)
1473 json_t *jarg = json_object_get(cmd,
"ipaddress");
1474 if (!json_is_string(jarg)) {
1475 json_object_set_new(answer,
"message", json_string(
"ipaddress is not an string"));
1478 const char *ipaddress = json_string_value(jarg);
1482 memset(&in, 0,
sizeof(in));
1483 if (inet_pton(AF_INET, ipaddress, &in) != 1) {
1485 memset(&in6, 0,
sizeof(in6));
1486 if (inet_pton(AF_INET6, ipaddress, &in6) != 1) {
1487 json_object_set_new(answer,
"message", json_string(
"invalid address string"));
1491 a.addr_data32[0] = in6[0];
1492 a.addr_data32[1] = in6[1];
1493 a.addr_data32[2] = in6[2];
1494 a.addr_data32[3] = in6[3];
1498 a.addr_data32[0] = in.s_addr;
1499 a.addr_data32[1] = 0;
1500 a.addr_data32[2] = 0;
1501 a.addr_data32[3] = 0;
1504 SCLogInfo(
"list-hostbit: %s", ipaddress);
1512 memset(&bits, 0,
sizeof(bits));
1517 json_object_set_new(answer,
"message", json_string(
"host not found"));
1522 while (use < 256 &&
HostBitList(host, &iter) == 1) {
1523 bits[use].id = iter->
idx;
1524 bits[use].expire = iter->
expire;
1529 json_t *jdata = json_object();
1530 json_t *jarray = json_array();
1531 if (jarray == NULL || jdata == NULL) {
1535 json_decref(jarray);
1536 json_object_set_new(answer,
"message",
1537 json_string(
"internal error at json object creation"));
1541 for (i = 0; i < use; i++) {
1542 json_t *bitobject = json_object();
1543 if (bitobject == NULL)
1545 uint64_t expire = 0;
1552 json_object_set_new(bitobject,
"name", json_string(
name));
1554 json_object_set_new(bitobject,
"expire", json_integer(expire));
1555 json_array_append_new(jarray, bitobject);
1558 json_object_set_new(jdata,
"count", json_integer(i));
1559 json_object_set_new(jdata,
"hostbits", jarray);
1560 json_object_set_new(answer,
"message", jdata);
1564 static void MemcapBuildValue(uint64_t val,
char *
str, uint32_t str_len)
1566 if ((val / (1024 * 1024 * 1024)) != 0) {
1567 snprintf(
str, str_len,
"%"PRIu64
"gb", val / (1024*1024*1024));
1568 }
else if ((val / (1024 * 1024)) != 0) {
1569 snprintf(
str, str_len,
"%"PRIu64
"mb", val / (1024*1024));
1571 snprintf(
str, str_len,
"%"PRIu64
"kb", val / (1024));
1575 TmEcode UnixSocketSetMemcap(json_t *cmd, json_t* answer,
void *data)
1577 char *memcap = NULL;
1578 char *value_str = NULL;
1581 json_t *jarg = json_object_get(cmd,
"config");
1582 if (!json_is_string(jarg)) {
1583 json_object_set_new(answer,
"message", json_string(
"memcap key is not a string"));
1586 memcap = (
char *)json_string_value(jarg);
1588 jarg = json_object_get(cmd,
"memcap");
1589 if (!json_is_string(jarg)) {
1590 json_object_set_new(answer,
"message", json_string(
"memcap value is not a string"));
1593 value_str = (
char *)json_string_value(jarg);
1597 "memcap from unix socket: %s",
1599 json_object_set_new(answer,
"message",
1600 json_string(
"error parsing memcap specified, "
1601 "value not changed"));
1605 for (
size_t i = 0; i <
ARRAY_SIZE(memcaps); i++) {
1606 if (strcmp(memcaps[i].
name, memcap) == 0 && memcaps[i].
SetFunc) {
1607 int updated = memcaps[i].
SetFunc(value);
1611 snprintf(message,
sizeof(message),
1612 "memcap value for '%s' updated: %"PRIu64
" %s",
1613 memcaps[i].
name, value,
1614 (value == 0) ?
"(unlimited)" :
"");
1615 json_object_set_new(answer,
"message", json_string(message));
1619 snprintf(message,
sizeof(message),
1620 "Unlimited value is not allowed for '%s'", memcaps[i].
name);
1622 if (memcaps[i].GetMemuseFunc()) {
1624 MemcapBuildValue(memcaps[i].GetMemuseFunc(), memuse,
sizeof(memuse));
1625 snprintf(message,
sizeof(message),
1626 "memcap value specified for '%s' is less than the memory in use: %s",
1627 memcaps[i].
name, memuse);
1629 snprintf(message,
sizeof(message),
1630 "memcap value specified for '%s' is less than the memory in use",
1634 json_object_set_new(answer,
"message", json_string(message));
1640 json_object_set_new(answer,
"message",
1641 json_string(
"Memcap value not found. Use 'memcap-list' to show all"));
1645 TmEcode UnixSocketShowMemcap(json_t *cmd, json_t *answer,
void *data)
1647 char *memcap = NULL;
1649 json_t *jarg = json_object_get(cmd,
"config");
1650 if (!json_is_string(jarg)) {
1651 json_object_set_new(answer,
"message", json_string(
"memcap name is not a string"));
1654 memcap = (
char *)json_string_value(jarg);
1656 for (
size_t i = 0; i <
ARRAY_SIZE(memcaps); i++) {
1657 if (strcmp(memcaps[i].
name, memcap) == 0 && memcaps[i].
GetFunc) {
1659 uint64_t val = memcaps[i].
GetFunc();
1660 json_t *jobj = json_object();
1662 json_object_set_new(answer,
"message",
1663 json_string(
"internal error at json object creation"));
1670 MemcapBuildValue(val,
str,
sizeof(
str));
1673 json_object_set_new(jobj,
"value", json_string(
str));
1674 json_object_set_new(answer,
"message", jobj);
1679 json_object_set_new(answer,
"message",
1680 json_string(
"Memcap value not found. Use 'memcap-list' to show all"));
1684 TmEcode UnixSocketShowAllMemcap(json_t *cmd, json_t *answer,
void *data)
1686 json_t *jmemcaps = json_array();
1688 if (jmemcaps == NULL) {
1689 json_object_set_new(answer,
"message",
1690 json_string(
"internal error at json array creation"));
1694 for (
size_t i = 0; i <
ARRAY_SIZE(memcaps); i++) {
1695 json_t *jobj = json_object();
1697 json_decref(jmemcaps);
1698 json_object_set_new(answer,
"message",
1699 json_string(
"internal error at json object creation"));
1703 uint64_t val = memcaps[i].
GetFunc();
1708 MemcapBuildValue(val,
str,
sizeof(
str));
1711 json_object_set_new(jobj,
"name", json_string(memcaps[i].
name));
1712 json_object_set_new(jobj,
"value", json_string(
str));
1713 json_array_append_new(jmemcaps, jobj);
1716 json_object_set_new(answer,
"message", jmemcaps);
1720 TmEcode UnixSocketGetFlowStatsById(json_t *cmd, json_t *answer,
void *data)
1723 json_t *jarg = json_object_get(cmd,
"flow_id");
1724 if (!json_is_integer(jarg)) {
1725 SCLogInfo(
"error: command is not a string");
1726 json_object_set_new(answer,
"message", json_string(
"flow_id is not an integer"));
1729 int64_t flow_id = json_integer_value(jarg);
1733 json_object_set_new(answer,
"message", json_string(
"Not found"));
1743 json_t *flow_info = json_object();
1744 if (flow_info == NULL) {
1747 json_object_set_new(flow_info,
"pkts_toclient", json_integer(tosrcpktcnt));
1748 json_object_set_new(flow_info,
"pkts_toserver", json_integer(todstpktcnt));
1749 json_object_set_new(flow_info,
"bytes_toclient", json_integer(tosrcbytecnt));
1750 json_object_set_new(flow_info,
"bytes_toserver", json_integer(todstbytecnt));
1751 json_object_set_new(flow_info,
"age", json_integer(age));
1752 json_object_set_new(answer,
"message", flow_info);
1757 #ifdef BUILD_UNIX_SOCKET
1761 static int RunModeUnixSocketMaster(
void)
1772 pcapcmd->running = 0;
1773 pcapcmd->current_file = NULL;
1775 memset(&unix_manager_pcap_last_processed, 0,
sizeof(
struct timespec));
1779 UnixManagerRegisterCommand(
"pcap-file", UnixSocketAddPcapFile, pcapcmd,
UNIX_CMD_TAKE_ARGS);
1780 UnixManagerRegisterCommand(
"pcap-file-continuous", UnixSocketAddPcapFileContinuous, pcapcmd,
UNIX_CMD_TAKE_ARGS);
1781 UnixManagerRegisterCommand(
"pcap-file-number", UnixSocketPcapFilesNumber, pcapcmd, 0);
1782 UnixManagerRegisterCommand(
"pcap-file-list", UnixSocketPcapFilesList, pcapcmd, 0);
1783 UnixManagerRegisterCommand(
"pcap-last-processed", UnixSocketPcapLastProcessed, pcapcmd, 0);
1784 UnixManagerRegisterCommand(
"pcap-interrupt", UnixSocketPcapInterrupt, pcapcmd, 0);
1785 UnixManagerRegisterCommand(
"pcap-current", UnixSocketPcapCurrent, pcapcmd, 0);
1787 UnixManagerRegisterBackgroundTask(UnixSocketPcapFilesCheck, pcapcmd);