114 for (
int i = 0; i < 4; i++) {
115 uint64_t memcap = memcaps[i].
GetFunc();
118 float p = (float)((
double)memuse / (double)memcap);
121 percent =
MAX(p, percent);
127 #ifdef BUILD_UNIX_SOCKET
129 static int RunModeUnixSocketMaster(
void);
130 static int unix_manager_pcap_task_running = 0;
131 static int unix_manager_pcap_task_failed = 0;
132 static int unix_manager_pcap_task_interrupted = 0;
133 static struct timespec unix_manager_pcap_last_processed;
134 static SCCtrlMutex unix_manager_pcap_last_processed_mutex;
141 static TmEcode UnixSocketPcapFilesList(json_t *cmd, json_t* answer,
void *data)
149 jdata = json_object();
151 json_object_set_new(answer,
"message",
152 json_string(
"internal error at json object creation"));
155 jarray = json_array();
156 if (jarray == NULL) {
158 json_object_set_new(answer,
"message",
159 json_string(
"internal error at json object creation"));
166 json_object_set_new(jdata,
"count", json_integer(i));
167 json_object_set_new(jdata,
"files", jarray);
168 json_object_set_new(answer,
"message", jdata);
172 static TmEcode UnixSocketPcapFilesNumber(json_t *cmd, json_t* answer,
void *data)
181 json_object_set_new(answer,
"message", json_integer(i));
185 static TmEcode UnixSocketPcapCurrent(json_t *cmd, json_t* answer,
void *data)
189 if (this->current_file != NULL && this->current_file->filename != NULL) {
190 json_object_set_new(answer,
"message",
191 json_string(this->current_file->filename));
193 json_object_set_new(answer,
"message", json_string(
"None"));
198 static TmEcode UnixSocketPcapLastProcessed(json_t *cmd, json_t *answer,
void *data)
200 json_int_t epoch_millis;
205 json_object_set_new(answer,
"message",
206 json_integer(epoch_millis));
211 static TmEcode UnixSocketPcapInterrupt(json_t *cmd, json_t *answer,
void *data)
213 unix_manager_pcap_task_interrupted = 1;
215 json_object_set_new(answer,
"message", json_string(
"Interrupted"));
220 static void PcapFilesFree(
PcapFiles *cfile)
245 static TmEcode UnixListAddFile(
PcapCommand *
this,
const char *filename,
const char *output_dir,
246 uint32_t tenant_id,
bool continuous,
bool should_delete, time_t delay, time_t poll_interval)
249 if (filename == NULL ||
this == NULL)
277 cfile->
delay = delay;
292 static TmEcode UnixSocketAddPcapFileImpl(json_t *cmd, json_t* answer,
void *data,
296 const char *filename;
297 const char *output_dir;
298 uint32_t tenant_id = 0;
299 bool should_delete =
false;
301 time_t poll_interval = 5;
304 json_t *jarg = json_object_get(cmd,
"filename");
305 if (!json_is_string(jarg)) {
307 json_object_set_new(answer,
"message",
308 json_string(
"filename is not a string"));
311 filename = json_string_value(jarg);
313 json_object_set_new(answer,
"message",
314 json_string(
"filename does not exist"));
318 json_t *oarg = json_object_get(cmd,
"output-dir");
320 if (!json_is_string(oarg)) {
323 json_object_set_new(answer,
"message",
324 json_string(
"output-dir is not a string"));
327 output_dir = json_string_value(oarg);
331 json_object_set_new(answer,
"message",
332 json_string(
"output-dir param is mandatory"));
336 if (
SCStatFn(output_dir, &st) != 0) {
337 json_object_set_new(answer,
"message",
338 json_string(
"output-dir does not exist"));
342 json_t *targ = json_object_get(cmd,
"tenant");
344 if (!json_is_integer(targ)) {
345 json_object_set_new(answer,
"message",
346 json_string(
"tenant is not a number"));
349 tenant_id = json_number_value(targ);
352 json_t *delete_arg = json_object_get(cmd,
"delete-when-done");
353 if (delete_arg != NULL) {
354 should_delete = json_is_true(delete_arg);
357 json_t *delay_arg = json_object_get(cmd,
"delay");
358 if (delay_arg != NULL) {
359 if (!json_is_integer(delay_arg)) {
361 json_object_set_new(answer,
"message",
362 json_string(
"delay is not a integer"));
365 delay = json_integer_value(delay_arg);
368 json_t *interval_arg = json_object_get(cmd,
"poll-interval");
369 if (interval_arg != NULL) {
370 if (!json_is_integer(interval_arg)) {
373 json_object_set_new(answer,
"message",
374 json_string(
"poll-interval is not a integer"));
377 poll_interval = json_integer_value(interval_arg);
380 switch (UnixListAddFile(
this, filename, output_dir, tenant_id, continuous,
381 should_delete, delay, poll_interval)) {
384 json_object_set_new(answer,
"message",
385 json_string(
"Unable to add file to list"));
388 SCLogInfo(
"Added file '%s' to list", filename);
389 json_object_set_new(answer,
"message",
390 json_string(
"Successfully added file to list"));
403 static TmEcode UnixSocketAddPcapFile(json_t *cmd, json_t* answer,
void *data)
405 bool continuous =
false;
407 json_t *cont_arg = json_object_get(cmd,
"continuous");
408 if (cont_arg != NULL) {
409 continuous = json_is_true(cont_arg);
412 return UnixSocketAddPcapFileImpl(cmd, answer, data, continuous);
422 static TmEcode UnixSocketAddPcapFileContinuous(json_t *cmd, json_t* answer,
void *data)
424 return UnixSocketAddPcapFileImpl(cmd, answer, data,
true);
440 static TmEcode UnixSocketPcapFilesCheck(
void *data)
443 if (unix_manager_pcap_task_running == 1) {
446 if ((unix_manager_pcap_task_failed == 1) || (this->running == 1)) {
447 if (unix_manager_pcap_task_failed) {
448 SCLogInfo(
"Preceeding task failed, cleaning the running mode");
450 unix_manager_pcap_task_failed = 0;
456 if (this->current_file) {
457 PcapFilesFree(this->current_file);
459 this->current_file = NULL;
470 unix_manager_pcap_task_running = 1;
475 PcapFilesFree(cfile);
486 SCLogError(
"Can not set continuous mode for pcap processing");
487 PcapFilesFree(cfile);
496 SCLogError(
"Can not set delete mode for pcap processing");
497 PcapFilesFree(cfile);
501 if (cfile->
delay > 0) {
503 snprintf(tstr,
sizeof(tstr),
"%" PRIuMAX, (uintmax_t)cfile->
delay);
505 SCLogError(
"Can not set delay to '%s'", tstr);
506 PcapFilesFree(cfile);
513 snprintf(tstr,
sizeof(tstr),
"%" PRIuMAX, (uintmax_t)cfile->
poll_interval);
515 SCLogError(
"Can not set poll-interval to '%s'", tstr);
516 PcapFilesFree(cfile);
523 snprintf(tstr,
sizeof(tstr),
"%u", cfile->
tenant_id);
525 SCLogError(
"Can not set working tenant-id to '%s'", tstr);
526 PcapFilesFree(cfile);
530 SCLogInfo(
"pcap-file.tenant-id not set");
536 PcapFilesFree(cfile);
541 this->current_file = cfile;
543 SCLogInfo(
"Starting run for '%s'", this->current_file->filename);
560 #ifdef BUILD_UNIX_SOCKET
571 #ifdef BUILD_UNIX_SOCKET
574 unix_manager_pcap_last_processed.tv_sec = last_processed->tv_sec;
575 unix_manager_pcap_last_processed.tv_nsec = last_processed->tv_nsec;
580 SCLogInfo(
"Marking current task as done");
581 unix_manager_pcap_task_running = 0;
584 SCLogInfo(
"Marking current task as failed");
585 unix_manager_pcap_task_running = 0;
586 unix_manager_pcap_task_failed = 1;
590 if (unix_manager_pcap_task_interrupted == 1) {
591 SCLogInfo(
"Interrupting current run mode");
592 unix_manager_pcap_task_interrupted = 0;
602 #ifdef BUILD_UNIX_SOCKET
610 TmEcode UnixSocketDatasetAdd(json_t *cmd, json_t* answer,
void *data)
613 json_t *narg = json_object_get(cmd,
"setname");
614 if (!json_is_string(narg)) {
615 json_object_set_new(answer,
"message", json_string(
"setname is not a string"));
618 const char *set_name = json_string_value(narg);
621 json_t *targ = json_object_get(cmd,
"settype");
622 if (!json_is_string(targ)) {
623 json_object_set_new(answer,
"message", json_string(
"settype is not a string"));
626 const char *
type = json_string_value(targ);
629 json_t *varg = json_object_get(cmd,
"datavalue");
630 if (!json_is_string(varg)) {
631 json_object_set_new(answer,
"message", json_string(
"datavalue is not string"));
634 const char *value = json_string_value(varg);
636 SCLogDebug(
"dataset-add: %s type %s value %s", set_name,
type, value);
640 json_object_set_new(answer,
"message", json_string(
"unknown settype"));
646 json_object_set_new(answer,
"message", json_string(
"set not found or wrong type"));
652 json_object_set_new(answer,
"message", json_string(
"data added"));
655 json_object_set_new(answer,
"message", json_string(
"data already in set"));
658 json_object_set_new(answer,
"message", json_string(
"failed to add data"));
663 TmEcode UnixSocketDatasetRemove(json_t *cmd, json_t* answer,
void *data)
666 json_t *narg = json_object_get(cmd,
"setname");
667 if (!json_is_string(narg)) {
668 json_object_set_new(answer,
"message", json_string(
"setname is not a string"));
671 const char *set_name = json_string_value(narg);
674 json_t *targ = json_object_get(cmd,
"settype");
675 if (!json_is_string(targ)) {
676 json_object_set_new(answer,
"message", json_string(
"settype is not a string"));
679 const char *
type = json_string_value(targ);
682 json_t *varg = json_object_get(cmd,
"datavalue");
683 if (!json_is_string(varg)) {
684 json_object_set_new(answer,
"message", json_string(
"datavalue is not string"));
687 const char *value = json_string_value(varg);
689 SCLogDebug(
"dataset-remove: %s type %s value %s", set_name,
type, value);
693 json_object_set_new(answer,
"message", json_string(
"unknown settype"));
699 json_object_set_new(answer,
"message", json_string(
"set not found or wrong type"));
705 json_object_set_new(answer,
"message", json_string(
"data removed"));
708 json_object_set_new(answer,
"message", json_string(
"data is busy, try again"));
711 json_object_set_new(answer,
"message", json_string(
"failed to remove data"));
716 TmEcode UnixSocketDatasetDump(json_t *cmd, json_t *answer,
void *data)
721 json_object_set_new(answer,
"message", json_string(
"datasets dump done"));
725 TmEcode UnixSocketDatasetClear(json_t *cmd, json_t *answer,
void *data)
728 json_t *narg = json_object_get(cmd,
"setname");
729 if (!json_is_string(narg)) {
730 json_object_set_new(answer,
"message", json_string(
"setname is not a string"));
733 const char *set_name = json_string_value(narg);
736 json_t *targ = json_object_get(cmd,
"settype");
737 if (!json_is_string(targ)) {
738 json_object_set_new(answer,
"message", json_string(
"settype is not a string"));
741 const char *
type = json_string_value(targ);
745 json_object_set_new(answer,
"message", json_string(
"unknown settype"));
751 json_object_set_new(answer,
"message", json_string(
"set not found or wrong type"));
757 json_object_set_new(answer,
"message", json_string(
"dataset cleared"));
761 TmEcode UnixSocketDatasetLookup(json_t *cmd, json_t *answer,
void *data)
764 json_t *narg = json_object_get(cmd,
"setname");
765 if (!json_is_string(narg)) {
766 json_object_set_new(answer,
"message", json_string(
"setname is not a string"));
769 const char *set_name = json_string_value(narg);
772 json_t *targ = json_object_get(cmd,
"settype");
773 if (!json_is_string(targ)) {
774 json_object_set_new(answer,
"message", json_string(
"settype is not a string"));
777 const char *
type = json_string_value(targ);
780 json_t *varg = json_object_get(cmd,
"datavalue");
781 if (!json_is_string(varg)) {
782 json_object_set_new(answer,
"message", json_string(
"datavalue is not string"));
785 const char *value = json_string_value(varg);
787 SCLogDebug(
"dataset-exist: %s type %s value %s", set_name,
type, value);
791 json_object_set_new(answer,
"message", json_string(
"unknown settype"));
797 json_object_set_new(answer,
"message", json_string(
"set not found or wrong type"));
802 json_object_set_new(answer,
"message", json_string(
"item found in set"));
805 json_object_set_new(answer,
"message", json_string(
"item not found in set"));
817 TmEcode UnixSocketDatajsonAdd(json_t *cmd, json_t *answer,
void *data)
820 json_t *narg = json_object_get(cmd,
"setname");
821 if (!json_is_string(narg)) {
822 json_object_set_new(answer,
"message", json_string(
"setname is not a string"));
825 const char *set_name = json_string_value(narg);
828 json_t *targ = json_object_get(cmd,
"settype");
829 if (!json_is_string(targ)) {
830 json_object_set_new(answer,
"message", json_string(
"settype is not a string"));
833 const char *
type = json_string_value(targ);
836 json_t *varg = json_object_get(cmd,
"datavalue");
837 if (!json_is_string(varg)) {
838 json_object_set_new(answer,
"message", json_string(
"datavalue is not string"));
841 const char *value = json_string_value(varg);
844 json_t *jarg = json_object_get(cmd,
"datajson");
845 if (!json_is_string(varg)) {
846 json_object_set_new(answer,
"message", json_string(
"datajson is not string"));
849 const char *json = json_string_value(jarg);
851 SCLogDebug(
"datajson-add: %s type %s value %s json %s", set_name,
type, value, json);
855 json_object_set_new(answer,
"message", json_string(
"unknown settype"));
861 json_object_set_new(answer,
"message", json_string(
"set not found or wrong type"));
867 json_object_set_new(answer,
"message", json_string(
"data added"));
870 json_object_set_new(answer,
"message", json_string(
"data already in set"));
873 json_object_set_new(answer,
"message", json_string(
"failed to add data"));
878 static bool JsonU32Value(json_t *jarg, uint32_t *ret)
880 int64_t r = json_integer_value(jarg);
881 if (r < 0 || r > UINT32_MAX) {
895 TmEcode UnixSocketRegisterTenantHandler(json_t *cmd, json_t* answer,
void *data)
898 json_int_t traffic_id = -1;
901 SCLogInfo(
"error: multi-tenant support not enabled");
902 json_object_set_new(answer,
"message", json_string(
"multi-tenant support not enabled"));
907 json_t *jarg = json_object_get(cmd,
"id");
908 if (!json_is_integer(jarg)) {
909 SCLogInfo(
"error: command is not a string");
910 json_object_set_new(answer,
"message", json_string(
"id is not an integer"));
914 if (!JsonU32Value(jarg, &tenant_id)) {
916 json_object_set_new(answer,
"message", json_string(
"tenant_id is not a uint32"));
921 jarg = json_object_get(cmd,
"htype");
922 if (!json_is_string(jarg)) {
923 SCLogInfo(
"error: command is not a string");
924 json_object_set_new(answer,
"message", json_string(
"command is not a string"));
927 htype = json_string_value(jarg);
929 SCLogDebug(
"add-tenant-handler: %d %s", tenant_id, htype);
932 json_t *hargs = json_object_get(cmd,
"hargs");
934 if (!json_is_integer(hargs)) {
936 json_object_set_new(answer,
"message", json_string(
"hargs not a number"));
939 traffic_id = json_integer_value(hargs);
944 if (strcmp(htype,
"pcap") == 0) {
946 }
else if (strcmp(htype,
"vlan") == 0) {
947 if (traffic_id < 0) {
948 json_object_set_new(answer,
"message", json_string(
"vlan requires argument"));
951 if (traffic_id > USHRT_MAX) {
952 json_object_set_new(answer,
"message", json_string(
"vlan argument out of range"));
956 SCLogInfo(
"VLAN handler: id %u maps to tenant %u", (uint32_t)traffic_id, tenant_id);
960 json_object_set_new(answer,
"message", json_string(
"handler setup failure"));
965 json_object_set_new(answer,
"message", json_string(
"couldn't apply settings"));
972 json_object_set_new(answer,
"message", json_string(
"handler added"));
983 TmEcode UnixSocketUnregisterTenantHandler(json_t *cmd, json_t* answer,
void *data)
986 json_int_t traffic_id = -1;
989 SCLogInfo(
"error: multi-tenant support not enabled");
990 json_object_set_new(answer,
"message", json_string(
"multi-tenant support not enabled"));
995 json_t *jarg = json_object_get(cmd,
"id");
996 if (!json_is_integer(jarg)) {
997 SCLogInfo(
"error: command is not a string");
998 json_object_set_new(answer,
"message", json_string(
"id is not an integer"));
1002 if (!JsonU32Value(jarg, &tenant_id)) {
1004 json_object_set_new(answer,
"message", json_string(
"tenant_id is not a uint32"));
1009 jarg = json_object_get(cmd,
"htype");
1010 if (!json_is_string(jarg)) {
1011 SCLogInfo(
"error: command is not a string");
1012 json_object_set_new(answer,
"message", json_string(
"command is not a string"));
1015 htype = json_string_value(jarg);
1017 SCLogDebug(
"add-tenant-handler: %d %s", tenant_id, htype);
1020 json_t *hargs = json_object_get(cmd,
"hargs");
1021 if (hargs != NULL) {
1022 if (!json_is_integer(hargs)) {
1024 json_object_set_new(answer,
"message", json_string(
"hargs not a number"));
1027 traffic_id = json_integer_value(hargs);
1032 if (strcmp(htype,
"pcap") == 0) {
1034 }
else if (strcmp(htype,
"vlan") == 0) {
1035 if (traffic_id < 0) {
1036 json_object_set_new(answer,
"message", json_string(
"vlan requires argument"));
1039 if (traffic_id > USHRT_MAX) {
1040 json_object_set_new(answer,
"message", json_string(
"vlan argument out of range"));
1044 SCLogInfo(
"VLAN handler: removing mapping of %u to tenant %u", (uint32_t)traffic_id, tenant_id);
1048 json_object_set_new(answer,
"message", json_string(
"handler unregister failure"));
1054 json_object_set_new(answer,
"message", json_string(
"couldn't apply settings"));
1061 json_object_set_new(answer,
"message", json_string(
"handler removed"));
1072 TmEcode UnixSocketRegisterTenant(json_t *cmd, json_t* answer,
void *data)
1074 const char *filename;
1078 SCLogInfo(
"error: multi-tenant support not enabled");
1079 json_object_set_new(answer,
"message", json_string(
"multi-tenant support not enabled"));
1084 json_t *jarg = json_object_get(cmd,
"id");
1085 if (!json_is_integer(jarg)) {
1086 json_object_set_new(answer,
"message", json_string(
"id is not an integer"));
1090 if (!JsonU32Value(jarg, &tenant_id)) {
1092 json_object_set_new(answer,
"message", json_string(
"tenant_id is not a uint32"));
1097 jarg = json_object_get(cmd,
"filename");
1098 if (!json_is_string(jarg)) {
1099 json_object_set_new(answer,
"message", json_string(
"command is not a string"));
1102 filename = json_string_value(jarg);
1103 if (
SCStatFn(filename, &st) != 0) {
1104 json_object_set_new(answer,
"message", json_string(
"file does not exist"));
1108 SCLogDebug(
"add-tenant: %d %s", tenant_id, filename);
1113 snprintf(prefix,
sizeof(prefix),
"multi-detect.%u", tenant_id);
1115 SCLogError(
"failed to load yaml %s", filename);
1116 json_object_set_new(answer,
"message", json_string(
"failed to load yaml"));
1122 json_object_set_new(answer,
"message", json_string(
"adding tenant failed"));
1128 json_object_set_new(answer,
"message", json_string(
"couldn't apply settings"));
1135 json_object_set_new(answer,
"message", json_string(
"adding tenant succeeded"));
1139 static int reload_cnt = 1;
1147 TmEcode UnixSocketReloadTenant(json_t *cmd, json_t* answer,
void *data)
1149 const char *filename = NULL;
1153 SCLogInfo(
"error: multi-tenant support not enabled");
1154 json_object_set_new(answer,
"message", json_string(
"multi-tenant support not enabled"));
1159 json_t *jarg = json_object_get(cmd,
"id");
1160 if (!json_is_integer(jarg)) {
1161 json_object_set_new(answer,
"message", json_string(
"id is not an integer"));
1165 if (!JsonU32Value(jarg, &tenant_id)) {
1167 json_object_set_new(answer,
"message", json_string(
"tenant_id is not a uint32"));
1172 jarg = json_object_get(cmd,
"filename");
1174 if (!json_is_string(jarg)) {
1175 json_object_set_new(answer,
"message", json_string(
"command is not a string"));
1178 filename = json_string_value(jarg);
1179 if (
SCStatFn(filename, &st) != 0) {
1180 json_object_set_new(answer,
"message", json_string(
"file does not exist"));
1185 SCLogDebug(
"reload-tenant: %d %s", tenant_id, filename);
1189 json_object_set_new(answer,
"message", json_string(
"reload tenant failed"));
1197 json_object_set_new(answer,
"message", json_string(
"couldn't apply settings"));
1204 json_object_set_new(answer,
"message", json_string(
"reloading tenant succeeded"));
1215 TmEcode UnixSocketReloadTenants(json_t *cmd, json_t *answer,
void *data)
1218 SCLogInfo(
"error: multi-tenant support not enabled");
1219 json_object_set_new(answer,
"message", json_string(
"multi-tenant support not enabled"));
1224 json_object_set_new(answer,
"message", json_string(
"reload tenants failed"));
1232 json_object_set_new(answer,
"message", json_string(
"couldn't apply settings"));
1240 json_object_set_new(answer,
"message", json_string(
"reloading tenants succeeded"));
1251 TmEcode UnixSocketUnregisterTenant(json_t *cmd, json_t* answer,
void *data)
1254 SCLogInfo(
"error: multi-tenant support not enabled");
1255 json_object_set_new(answer,
"message", json_string(
"multi-tenant support not enabled"));
1260 json_t *jarg = json_object_get(cmd,
"id");
1261 if (!json_is_integer(jarg)) {
1262 SCLogInfo(
"error: command is not a string");
1263 json_object_set_new(answer,
"message", json_string(
"id is not an integer"));
1267 if (!JsonU32Value(jarg, &tenant_id)) {
1269 json_object_set_new(answer,
"message", json_string(
"tenant_id is not a uint32"));
1273 SCLogInfo(
"remove-tenant: removing tenant %d", tenant_id);
1277 snprintf(prefix,
sizeof(prefix),
"multi-detect.%u", tenant_id);
1281 json_object_set_new(answer,
"message", json_string(
"tenant detect engine not found"));
1291 json_object_set_new(answer,
"message", json_string(
"couldn't apply settings"));
1301 json_object_set_new(answer,
"message", json_string(
"removing tenant succeeded"));
1311 TmEcode UnixSocketHostbitAdd(json_t *cmd, json_t* answer,
void *data_usused)
1314 json_t *jarg = json_object_get(cmd,
"ipaddress");
1315 if (!json_is_string(jarg)) {
1316 json_object_set_new(answer,
"message", json_string(
"ipaddress is not an string"));
1319 const char *ipaddress = json_string_value(jarg);
1323 memset(&in, 0,
sizeof(in));
1324 if (inet_pton(AF_INET, ipaddress, &in) != 1) {
1326 memset(&in6, 0,
sizeof(in6));
1327 if (inet_pton(AF_INET6, ipaddress, &in6) != 1) {
1328 json_object_set_new(answer,
"message", json_string(
"invalid address string"));
1332 a.addr_data32[0] = in6[0];
1333 a.addr_data32[1] = in6[1];
1334 a.addr_data32[2] = in6[2];
1335 a.addr_data32[3] = in6[3];
1339 a.addr_data32[0] = in.s_addr;
1340 a.addr_data32[1] = 0;
1341 a.addr_data32[2] = 0;
1342 a.addr_data32[3] = 0;
1346 jarg = json_object_get(cmd,
"hostbit");
1347 if (!json_is_string(jarg)) {
1348 json_object_set_new(answer,
"message", json_string(
"hostbit is not a string"));
1351 const char *hostbit = json_string_value(jarg);
1354 json_object_set_new(answer,
"message", json_string(
"hostbit not found"));
1359 jarg = json_object_get(cmd,
"expire");
1360 if (!json_is_integer(jarg)) {
1361 json_object_set_new(answer,
"message", json_string(
"expire is not an integer"));
1365 if (!JsonU32Value(jarg, &expire)) {
1367 json_object_set_new(answer,
"message", json_string(
"expire is not a uint32"));
1371 SCLogInfo(
"add-hostbit: ip %s hostbit %s expire %us", ipaddress, hostbit, expire);
1376 if (
SCTIME_SECS(current_time) + expire > UINT32_MAX) {
1377 json_object_set_new(answer,
"message", json_string(
"couldn't set host expire"));
1384 json_object_set_new(answer,
"message", json_string(
"hostbit added"));
1387 json_object_set_new(answer,
"message", json_string(
"couldn't create host"));
1398 TmEcode UnixSocketHostbitRemove(json_t *cmd, json_t* answer,
void *data_unused)
1401 json_t *jarg = json_object_get(cmd,
"ipaddress");
1402 if (!json_is_string(jarg)) {
1403 json_object_set_new(answer,
"message", json_string(
"ipaddress is not an string"));
1406 const char *ipaddress = json_string_value(jarg);
1410 memset(&in, 0,
sizeof(in));
1411 if (inet_pton(AF_INET, ipaddress, &in) != 1) {
1413 memset(&in6, 0,
sizeof(in6));
1414 if (inet_pton(AF_INET6, ipaddress, &in6) != 1) {
1415 json_object_set_new(answer,
"message", json_string(
"invalid address string"));
1419 a.addr_data32[0] = in6[0];
1420 a.addr_data32[1] = in6[1];
1421 a.addr_data32[2] = in6[2];
1422 a.addr_data32[3] = in6[3];
1426 a.addr_data32[0] = in.s_addr;
1427 a.addr_data32[1] = 0;
1428 a.addr_data32[2] = 0;
1429 a.addr_data32[3] = 0;
1433 jarg = json_object_get(cmd,
"hostbit");
1434 if (!json_is_string(jarg)) {
1435 json_object_set_new(answer,
"message", json_string(
"hostbit is not a string"));
1439 const char *hostbit = json_string_value(jarg);
1442 json_object_set_new(answer,
"message", json_string(
"hostbit not found"));
1446 SCLogInfo(
"remove-hostbit: %s %s", ipaddress, hostbit);
1452 json_object_set_new(answer,
"message", json_string(
"hostbit removed"));
1455 json_object_set_new(answer,
"message", json_string(
"host not found"));
1471 TmEcode UnixSocketHostbitList(json_t *cmd, json_t* answer,
void *data_unused)
1474 json_t *jarg = json_object_get(cmd,
"ipaddress");
1475 if (!json_is_string(jarg)) {
1476 json_object_set_new(answer,
"message", json_string(
"ipaddress is not an string"));
1479 const char *ipaddress = json_string_value(jarg);
1483 memset(&in, 0,
sizeof(in));
1484 if (inet_pton(AF_INET, ipaddress, &in) != 1) {
1486 memset(&in6, 0,
sizeof(in6));
1487 if (inet_pton(AF_INET6, ipaddress, &in6) != 1) {
1488 json_object_set_new(answer,
"message", json_string(
"invalid address string"));
1492 a.addr_data32[0] = in6[0];
1493 a.addr_data32[1] = in6[1];
1494 a.addr_data32[2] = in6[2];
1495 a.addr_data32[3] = in6[3];
1499 a.addr_data32[0] = in.s_addr;
1500 a.addr_data32[1] = 0;
1501 a.addr_data32[2] = 0;
1502 a.addr_data32[3] = 0;
1505 SCLogInfo(
"list-hostbit: %s", ipaddress);
1513 memset(&bits, 0,
sizeof(bits));
1518 json_object_set_new(answer,
"message", json_string(
"host not found"));
1523 while (use < 256 &&
HostBitList(host, &iter) == 1) {
1524 bits[use].id = iter->
idx;
1525 bits[use].expire = iter->
expire;
1530 json_t *jdata = json_object();
1531 json_t *jarray = json_array();
1532 if (jarray == NULL || jdata == NULL) {
1536 json_decref(jarray);
1537 json_object_set_new(answer,
"message",
1538 json_string(
"internal error at json object creation"));
1542 for (i = 0; i < use; i++) {
1543 json_t *bitobject = json_object();
1544 if (bitobject == NULL)
1546 uint64_t expire = 0;
1553 json_object_set_new(bitobject,
"name", json_string(
name));
1555 json_object_set_new(bitobject,
"expire", json_integer(expire));
1556 json_array_append_new(jarray, bitobject);
1559 json_object_set_new(jdata,
"count", json_integer(i));
1560 json_object_set_new(jdata,
"hostbits", jarray);
1561 json_object_set_new(answer,
"message", jdata);
1565 static void MemcapBuildValue(uint64_t val,
char *
str, uint32_t str_len)
1567 if ((val / (1024 * 1024 * 1024)) != 0) {
1568 snprintf(
str, str_len,
"%"PRIu64
"gb", val / (1024*1024*1024));
1569 }
else if ((val / (1024 * 1024)) != 0) {
1570 snprintf(
str, str_len,
"%"PRIu64
"mb", val / (1024*1024));
1572 snprintf(
str, str_len,
"%"PRIu64
"kb", val / (1024));
1576 TmEcode UnixSocketSetMemcap(json_t *cmd, json_t* answer,
void *data)
1578 char *memcap = NULL;
1579 char *value_str = NULL;
1582 json_t *jarg = json_object_get(cmd,
"config");
1583 if (!json_is_string(jarg)) {
1584 json_object_set_new(answer,
"message", json_string(
"memcap key is not a string"));
1587 memcap = (
char *)json_string_value(jarg);
1589 jarg = json_object_get(cmd,
"memcap");
1590 if (!json_is_string(jarg)) {
1591 json_object_set_new(answer,
"message", json_string(
"memcap value is not a string"));
1594 value_str = (
char *)json_string_value(jarg);
1598 "memcap from unix socket: %s",
1600 json_object_set_new(answer,
"message",
1601 json_string(
"error parsing memcap specified, "
1602 "value not changed"));
1606 for (
size_t i = 0; i <
ARRAY_SIZE(memcaps); i++) {
1607 if (strcmp(memcaps[i].
name, memcap) == 0 && memcaps[i].
SetFunc) {
1608 int updated = memcaps[i].
SetFunc(value);
1612 snprintf(message,
sizeof(message),
1613 "memcap value for '%s' updated: %"PRIu64
" %s",
1614 memcaps[i].
name, value,
1615 (value == 0) ?
"(unlimited)" :
"");
1616 json_object_set_new(answer,
"message", json_string(message));
1620 snprintf(message,
sizeof(message),
1621 "Unlimited value is not allowed for '%s'", memcaps[i].
name);
1623 if (memcaps[i].GetMemuseFunc()) {
1625 MemcapBuildValue(memcaps[i].GetMemuseFunc(), memuse,
sizeof(memuse));
1626 snprintf(message,
sizeof(message),
1627 "memcap value specified for '%s' is less than the memory in use: %s",
1628 memcaps[i].
name, memuse);
1630 snprintf(message,
sizeof(message),
1631 "memcap value specified for '%s' is less than the memory in use",
1635 json_object_set_new(answer,
"message", json_string(message));
1641 json_object_set_new(answer,
"message",
1642 json_string(
"Memcap value not found. Use 'memcap-list' to show all"));
1646 TmEcode UnixSocketShowMemcap(json_t *cmd, json_t *answer,
void *data)
1648 char *memcap = NULL;
1650 json_t *jarg = json_object_get(cmd,
"config");
1651 if (!json_is_string(jarg)) {
1652 json_object_set_new(answer,
"message", json_string(
"memcap name is not a string"));
1655 memcap = (
char *)json_string_value(jarg);
1657 for (
size_t i = 0; i <
ARRAY_SIZE(memcaps); i++) {
1658 if (strcmp(memcaps[i].
name, memcap) == 0 && memcaps[i].
GetFunc) {
1660 uint64_t val = memcaps[i].
GetFunc();
1661 json_t *jobj = json_object();
1663 json_object_set_new(answer,
"message",
1664 json_string(
"internal error at json object creation"));
1671 MemcapBuildValue(val,
str,
sizeof(
str));
1674 json_object_set_new(jobj,
"value", json_string(
str));
1675 json_object_set_new(answer,
"message", jobj);
1680 json_object_set_new(answer,
"message",
1681 json_string(
"Memcap value not found. Use 'memcap-list' to show all"));
1685 TmEcode UnixSocketShowAllMemcap(json_t *cmd, json_t *answer,
void *data)
1687 json_t *jmemcaps = json_array();
1689 if (jmemcaps == NULL) {
1690 json_object_set_new(answer,
"message",
1691 json_string(
"internal error at json array creation"));
1695 for (
size_t i = 0; i <
ARRAY_SIZE(memcaps); i++) {
1696 json_t *jobj = json_object();
1698 json_decref(jmemcaps);
1699 json_object_set_new(answer,
"message",
1700 json_string(
"internal error at json object creation"));
1704 uint64_t val = memcaps[i].
GetFunc();
1709 MemcapBuildValue(val,
str,
sizeof(
str));
1712 json_object_set_new(jobj,
"name", json_string(memcaps[i].
name));
1713 json_object_set_new(jobj,
"value", json_string(
str));
1714 json_array_append_new(jmemcaps, jobj);
1717 json_object_set_new(answer,
"message", jmemcaps);
1721 TmEcode UnixSocketGetFlowStatsById(json_t *cmd, json_t *answer,
void *data)
1724 json_t *jarg = json_object_get(cmd,
"flow_id");
1725 if (!json_is_integer(jarg)) {
1726 SCLogInfo(
"error: command is not a string");
1727 json_object_set_new(answer,
"message", json_string(
"flow_id is not an integer"));
1730 int64_t flow_id = json_integer_value(jarg);
1734 json_object_set_new(answer,
"message", json_string(
"Not found"));
1744 json_t *flow_info = json_object();
1745 if (flow_info == NULL) {
1748 json_object_set_new(flow_info,
"pkts_toclient", json_integer(tosrcpktcnt));
1749 json_object_set_new(flow_info,
"pkts_toserver", json_integer(todstpktcnt));
1750 json_object_set_new(flow_info,
"bytes_toclient", json_integer(tosrcbytecnt));
1751 json_object_set_new(flow_info,
"bytes_toserver", json_integer(todstbytecnt));
1752 json_object_set_new(flow_info,
"age", json_integer(age));
1753 json_object_set_new(answer,
"message", flow_info);
1758 #ifdef BUILD_UNIX_SOCKET
1762 static int RunModeUnixSocketMaster(
void)
1773 pcapcmd->running = 0;
1774 pcapcmd->current_file = NULL;
1776 memset(&unix_manager_pcap_last_processed, 0,
sizeof(
struct timespec));
1780 UnixManagerRegisterCommand(
"pcap-file", UnixSocketAddPcapFile, pcapcmd,
UNIX_CMD_TAKE_ARGS);
1781 UnixManagerRegisterCommand(
"pcap-file-continuous", UnixSocketAddPcapFileContinuous, pcapcmd,
UNIX_CMD_TAKE_ARGS);
1782 UnixManagerRegisterCommand(
"pcap-file-number", UnixSocketPcapFilesNumber, pcapcmd, 0);
1783 UnixManagerRegisterCommand(
"pcap-file-list", UnixSocketPcapFilesList, pcapcmd, 0);
1784 UnixManagerRegisterCommand(
"pcap-last-processed", UnixSocketPcapLastProcessed, pcapcmd, 0);
1785 UnixManagerRegisterCommand(
"pcap-interrupt", UnixSocketPcapInterrupt, pcapcmd, 0);
1786 UnixManagerRegisterCommand(
"pcap-current", UnixSocketPcapCurrent, pcapcmd, 0);
1788 UnixManagerRegisterBackgroundTask(UnixSocketPcapFilesCheck, pcapcmd);