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);
482 set_res =
ConfSetFinal(
"pcap-file.continuous",
"false");
485 SCLogError(
"Can not set continuous mode for pcap processing");
486 PcapFilesFree(cfile);
490 set_res =
ConfSetFinal(
"pcap-file.delete-when-done",
"true");
492 set_res =
ConfSetFinal(
"pcap-file.delete-when-done",
"false");
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);
513 if (
ConfSetFinal(
"pcap-file.poll-interval", tstr) != 1) {
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"));
809 static bool JsonU32Value(json_t *jarg, uint32_t *ret)
811 int64_t r = json_integer_value(jarg);
812 if (r < 0 || r > UINT32_MAX) {
826 TmEcode UnixSocketRegisterTenantHandler(json_t *cmd, json_t* answer,
void *data)
829 json_int_t traffic_id = -1;
832 SCLogInfo(
"error: multi-tenant support not enabled");
833 json_object_set_new(answer,
"message", json_string(
"multi-tenant support not enabled"));
838 json_t *jarg = json_object_get(cmd,
"id");
839 if (!json_is_integer(jarg)) {
840 SCLogInfo(
"error: command is not a string");
841 json_object_set_new(answer,
"message", json_string(
"id is not an integer"));
845 if (!JsonU32Value(jarg, &tenant_id)) {
847 json_object_set_new(answer,
"message", json_string(
"tenant_id is not a uint32"));
852 jarg = json_object_get(cmd,
"htype");
853 if (!json_is_string(jarg)) {
854 SCLogInfo(
"error: command is not a string");
855 json_object_set_new(answer,
"message", json_string(
"command is not a string"));
858 htype = json_string_value(jarg);
860 SCLogDebug(
"add-tenant-handler: %d %s", tenant_id, htype);
863 json_t *hargs = json_object_get(cmd,
"hargs");
865 if (!json_is_integer(hargs)) {
867 json_object_set_new(answer,
"message", json_string(
"hargs not a number"));
870 traffic_id = json_integer_value(hargs);
875 if (strcmp(htype,
"pcap") == 0) {
877 }
else if (strcmp(htype,
"vlan") == 0) {
878 if (traffic_id < 0) {
879 json_object_set_new(answer,
"message", json_string(
"vlan requires argument"));
882 if (traffic_id > USHRT_MAX) {
883 json_object_set_new(answer,
"message", json_string(
"vlan argument out of range"));
887 SCLogInfo(
"VLAN handler: id %u maps to tenant %u", (uint32_t)traffic_id, tenant_id);
891 json_object_set_new(answer,
"message", json_string(
"handler setup failure"));
896 json_object_set_new(answer,
"message", json_string(
"couldn't apply settings"));
901 json_object_set_new(answer,
"message", json_string(
"handler added"));
912 TmEcode UnixSocketUnregisterTenantHandler(json_t *cmd, json_t* answer,
void *data)
915 json_int_t traffic_id = -1;
918 SCLogInfo(
"error: multi-tenant support not enabled");
919 json_object_set_new(answer,
"message", json_string(
"multi-tenant support not enabled"));
924 json_t *jarg = json_object_get(cmd,
"id");
925 if (!json_is_integer(jarg)) {
926 SCLogInfo(
"error: command is not a string");
927 json_object_set_new(answer,
"message", json_string(
"id is not an integer"));
931 if (!JsonU32Value(jarg, &tenant_id)) {
933 json_object_set_new(answer,
"message", json_string(
"tenant_id is not a uint32"));
938 jarg = json_object_get(cmd,
"htype");
939 if (!json_is_string(jarg)) {
940 SCLogInfo(
"error: command is not a string");
941 json_object_set_new(answer,
"message", json_string(
"command is not a string"));
944 htype = json_string_value(jarg);
946 SCLogDebug(
"add-tenant-handler: %d %s", tenant_id, htype);
949 json_t *hargs = json_object_get(cmd,
"hargs");
951 if (!json_is_integer(hargs)) {
953 json_object_set_new(answer,
"message", json_string(
"hargs not a number"));
956 traffic_id = json_integer_value(hargs);
961 if (strcmp(htype,
"pcap") == 0) {
963 }
else if (strcmp(htype,
"vlan") == 0) {
964 if (traffic_id < 0) {
965 json_object_set_new(answer,
"message", json_string(
"vlan requires argument"));
968 if (traffic_id > USHRT_MAX) {
969 json_object_set_new(answer,
"message", json_string(
"vlan argument out of range"));
973 SCLogInfo(
"VLAN handler: removing mapping of %u to tenant %u", (uint32_t)traffic_id, tenant_id);
977 json_object_set_new(answer,
"message", json_string(
"handler unregister failure"));
983 json_object_set_new(answer,
"message", json_string(
"couldn't apply settings"));
988 json_object_set_new(answer,
"message", json_string(
"handler removed"));
999 TmEcode UnixSocketRegisterTenant(json_t *cmd, json_t* answer,
void *data)
1001 const char *filename;
1005 SCLogInfo(
"error: multi-tenant support not enabled");
1006 json_object_set_new(answer,
"message", json_string(
"multi-tenant support not enabled"));
1011 json_t *jarg = json_object_get(cmd,
"id");
1012 if (!json_is_integer(jarg)) {
1013 json_object_set_new(answer,
"message", json_string(
"id is not an integer"));
1017 if (!JsonU32Value(jarg, &tenant_id)) {
1019 json_object_set_new(answer,
"message", json_string(
"tenant_id is not a uint32"));
1024 jarg = json_object_get(cmd,
"filename");
1025 if (!json_is_string(jarg)) {
1026 json_object_set_new(answer,
"message", json_string(
"command is not a string"));
1029 filename = json_string_value(jarg);
1030 if (
SCStatFn(filename, &st) != 0) {
1031 json_object_set_new(answer,
"message", json_string(
"file does not exist"));
1035 SCLogDebug(
"add-tenant: %d %s", tenant_id, filename);
1040 snprintf(prefix,
sizeof(prefix),
"multi-detect.%u", tenant_id);
1042 SCLogError(
"failed to load yaml %s", filename);
1043 json_object_set_new(answer,
"message", json_string(
"failed to load yaml"));
1049 json_object_set_new(answer,
"message", json_string(
"adding tenant failed"));
1055 json_object_set_new(answer,
"message", json_string(
"couldn't apply settings"));
1060 json_object_set_new(answer,
"message", json_string(
"adding tenant succeeded"));
1064 static int reload_cnt = 1;
1072 TmEcode UnixSocketReloadTenant(json_t *cmd, json_t* answer,
void *data)
1074 const char *filename = NULL;
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");
1099 if (!json_is_string(jarg)) {
1100 json_object_set_new(answer,
"message", json_string(
"command is not a string"));
1103 filename = json_string_value(jarg);
1104 if (
SCStatFn(filename, &st) != 0) {
1105 json_object_set_new(answer,
"message", json_string(
"file does not exist"));
1110 SCLogDebug(
"reload-tenant: %d %s", tenant_id, filename);
1114 json_object_set_new(answer,
"message", json_string(
"reload tenant failed"));
1122 json_object_set_new(answer,
"message", json_string(
"couldn't apply settings"));
1127 json_object_set_new(answer,
"message", json_string(
"reloading tenant succeeded"));
1138 TmEcode UnixSocketReloadTenants(json_t *cmd, json_t *answer,
void *data)
1141 SCLogInfo(
"error: multi-tenant support not enabled");
1142 json_object_set_new(answer,
"message", json_string(
"multi-tenant support not enabled"));
1147 json_object_set_new(answer,
"message", json_string(
"reload tenants failed"));
1155 json_object_set_new(answer,
"message", json_string(
"couldn't apply settings"));
1162 json_object_set_new(answer,
"message", json_string(
"reloading tenants succeeded"));
1173 TmEcode UnixSocketUnregisterTenant(json_t *cmd, json_t* answer,
void *data)
1176 SCLogInfo(
"error: multi-tenant support not enabled");
1177 json_object_set_new(answer,
"message", json_string(
"multi-tenant support not enabled"));
1182 json_t *jarg = json_object_get(cmd,
"id");
1183 if (!json_is_integer(jarg)) {
1184 SCLogInfo(
"error: command is not a string");
1185 json_object_set_new(answer,
"message", json_string(
"id is not an integer"));
1189 if (!JsonU32Value(jarg, &tenant_id)) {
1191 json_object_set_new(answer,
"message", json_string(
"tenant_id is not a uint32"));
1195 SCLogInfo(
"remove-tenant: removing tenant %d", tenant_id);
1199 snprintf(prefix,
sizeof(prefix),
"multi-detect.%u", tenant_id);
1203 json_object_set_new(answer,
"message", json_string(
"tenant detect engine not found"));
1213 json_object_set_new(answer,
"message", json_string(
"couldn't apply settings"));
1221 json_object_set_new(answer,
"message", json_string(
"removing tenant succeeded"));
1231 TmEcode UnixSocketHostbitAdd(json_t *cmd, json_t* answer,
void *data_usused)
1234 json_t *jarg = json_object_get(cmd,
"ipaddress");
1235 if (!json_is_string(jarg)) {
1236 json_object_set_new(answer,
"message", json_string(
"ipaddress is not an string"));
1239 const char *ipaddress = json_string_value(jarg);
1243 memset(&in, 0,
sizeof(in));
1244 if (inet_pton(AF_INET, ipaddress, &in) != 1) {
1246 memset(&in6, 0,
sizeof(in6));
1247 if (inet_pton(AF_INET6, ipaddress, &in) != 1) {
1248 json_object_set_new(answer,
"message", json_string(
"invalid address string"));
1252 a.addr_data32[0] = in6[0];
1253 a.addr_data32[1] = in6[1];
1254 a.addr_data32[2] = in6[2];
1255 a.addr_data32[3] = in6[3];
1259 a.addr_data32[0] = in.s_addr;
1260 a.addr_data32[1] = 0;
1261 a.addr_data32[2] = 0;
1262 a.addr_data32[3] = 0;
1266 jarg = json_object_get(cmd,
"hostbit");
1267 if (!json_is_string(jarg)) {
1268 json_object_set_new(answer,
"message", json_string(
"hostbit is not a string"));
1271 const char *hostbit = json_string_value(jarg);
1274 json_object_set_new(answer,
"message", json_string(
"hostbit not found"));
1279 jarg = json_object_get(cmd,
"expire");
1280 if (!json_is_integer(jarg)) {
1281 json_object_set_new(answer,
"message", json_string(
"expire is not an integer"));
1285 if (!JsonU32Value(jarg, &expire)) {
1287 json_object_set_new(answer,
"message", json_string(
"expire is not a uint32"));
1291 SCLogInfo(
"add-hostbit: ip %s hostbit %s expire %us", ipaddress, hostbit, expire);
1296 if (
SCTIME_SECS(current_time) + expire > UINT32_MAX) {
1297 json_object_set_new(answer,
"message", json_string(
"couldn't set host expire"));
1304 json_object_set_new(answer,
"message", json_string(
"hostbit added"));
1307 json_object_set_new(answer,
"message", json_string(
"couldn't create host"));
1318 TmEcode UnixSocketHostbitRemove(json_t *cmd, json_t* answer,
void *data_unused)
1321 json_t *jarg = json_object_get(cmd,
"ipaddress");
1322 if (!json_is_string(jarg)) {
1323 json_object_set_new(answer,
"message", json_string(
"ipaddress is not an string"));
1326 const char *ipaddress = json_string_value(jarg);
1330 memset(&in, 0,
sizeof(in));
1331 if (inet_pton(AF_INET, ipaddress, &in) != 1) {
1333 memset(&in6, 0,
sizeof(in6));
1334 if (inet_pton(AF_INET6, ipaddress, &in) != 1) {
1335 json_object_set_new(answer,
"message", json_string(
"invalid address string"));
1339 a.addr_data32[0] = in6[0];
1340 a.addr_data32[1] = in6[1];
1341 a.addr_data32[2] = in6[2];
1342 a.addr_data32[3] = in6[3];
1346 a.addr_data32[0] = in.s_addr;
1347 a.addr_data32[1] = 0;
1348 a.addr_data32[2] = 0;
1349 a.addr_data32[3] = 0;
1353 jarg = json_object_get(cmd,
"hostbit");
1354 if (!json_is_string(jarg)) {
1355 json_object_set_new(answer,
"message", json_string(
"hostbit is not a string"));
1359 const char *hostbit = json_string_value(jarg);
1362 json_object_set_new(answer,
"message", json_string(
"hostbit not found"));
1366 SCLogInfo(
"remove-hostbit: %s %s", ipaddress, hostbit);
1372 json_object_set_new(answer,
"message", json_string(
"hostbit removed"));
1375 json_object_set_new(answer,
"message", json_string(
"host not found"));
1391 TmEcode UnixSocketHostbitList(json_t *cmd, json_t* answer,
void *data_unused)
1394 json_t *jarg = json_object_get(cmd,
"ipaddress");
1395 if (!json_is_string(jarg)) {
1396 json_object_set_new(answer,
"message", json_string(
"ipaddress is not an string"));
1399 const char *ipaddress = json_string_value(jarg);
1403 memset(&in, 0,
sizeof(in));
1404 if (inet_pton(AF_INET, ipaddress, &in) != 1) {
1406 memset(&in6, 0,
sizeof(in6));
1407 if (inet_pton(AF_INET6, ipaddress, &in) != 1) {
1408 json_object_set_new(answer,
"message", json_string(
"invalid address string"));
1412 a.addr_data32[0] = in6[0];
1413 a.addr_data32[1] = in6[1];
1414 a.addr_data32[2] = in6[2];
1415 a.addr_data32[3] = in6[3];
1419 a.addr_data32[0] = in.s_addr;
1420 a.addr_data32[1] = 0;
1421 a.addr_data32[2] = 0;
1422 a.addr_data32[3] = 0;
1425 SCLogInfo(
"list-hostbit: %s", ipaddress);
1433 memset(&bits, 0,
sizeof(bits));
1438 json_object_set_new(answer,
"message", json_string(
"host not found"));
1443 while (use < 256 &&
HostBitList(host, &iter) == 1) {
1444 bits[use].id = iter->
idx;
1445 bits[use].expire = iter->
expire;
1450 json_t *jdata = json_object();
1451 json_t *jarray = json_array();
1452 if (jarray == NULL || jdata == NULL) {
1456 json_decref(jarray);
1457 json_object_set_new(answer,
"message",
1458 json_string(
"internal error at json object creation"));
1462 for (i = 0; i < use; i++) {
1463 json_t *bitobject = json_object();
1464 if (bitobject == NULL)
1466 uint32_t expire = 0;
1473 json_object_set_new(bitobject,
"name", json_string(name));
1474 SCLogDebug(
"xbit %s expire %u", name, expire);
1475 json_object_set_new(bitobject,
"expire", json_integer(expire));
1476 json_array_append_new(jarray, bitobject);
1479 json_object_set_new(jdata,
"count", json_integer(i));
1480 json_object_set_new(jdata,
"hostbits", jarray);
1481 json_object_set_new(answer,
"message", jdata);
1485 static void MemcapBuildValue(uint64_t val,
char *
str, uint32_t str_len)
1487 if ((val / (1024 * 1024 * 1024)) != 0) {
1488 snprintf(
str, str_len,
"%"PRIu64
"gb", val / (1024*1024*1024));
1489 }
else if ((val / (1024 * 1024)) != 0) {
1490 snprintf(
str, str_len,
"%"PRIu64
"mb", val / (1024*1024));
1492 snprintf(
str, str_len,
"%"PRIu64
"kb", val / (1024));
1496 TmEcode UnixSocketSetMemcap(json_t *cmd, json_t* answer,
void *data)
1498 char *memcap = NULL;
1499 char *value_str = NULL;
1502 json_t *jarg = json_object_get(cmd,
"config");
1503 if (!json_is_string(jarg)) {
1504 json_object_set_new(answer,
"message", json_string(
"memcap key is not a string"));
1507 memcap = (
char *)json_string_value(jarg);
1509 jarg = json_object_get(cmd,
"memcap");
1510 if (!json_is_string(jarg)) {
1511 json_object_set_new(answer,
"message", json_string(
"memcap value is not a string"));
1514 value_str = (
char *)json_string_value(jarg);
1518 "memcap from unix socket: %s",
1520 json_object_set_new(answer,
"message",
1521 json_string(
"error parsing memcap specified, "
1522 "value not changed"));
1526 for (
size_t i = 0; i <
ARRAY_SIZE(memcaps); i++) {
1527 if (strcmp(memcaps[i].name, memcap) == 0 && memcaps[i].
SetFunc) {
1528 int updated = memcaps[i].
SetFunc(value);
1532 snprintf(message,
sizeof(message),
1533 "memcap value for '%s' updated: %"PRIu64
" %s",
1534 memcaps[i].name, value,
1535 (value == 0) ?
"(unlimited)" :
"");
1536 json_object_set_new(answer,
"message", json_string(message));
1540 snprintf(message,
sizeof(message),
1541 "Unlimited value is not allowed for '%s'", memcaps[i].name);
1543 if (memcaps[i].GetMemuseFunc()) {
1545 MemcapBuildValue(memcaps[i].GetMemuseFunc(), memuse,
sizeof(memuse));
1546 snprintf(message,
sizeof(message),
1547 "memcap value specified for '%s' is less than the memory in use: %s",
1548 memcaps[i].name, memuse);
1550 snprintf(message,
sizeof(message),
1551 "memcap value specified for '%s' is less than the memory in use",
1555 json_object_set_new(answer,
"message", json_string(message));
1561 json_object_set_new(answer,
"message",
1562 json_string(
"Memcap value not found. Use 'memcap-list' to show all"));
1566 TmEcode UnixSocketShowMemcap(json_t *cmd, json_t *answer,
void *data)
1568 char *memcap = NULL;
1570 json_t *jarg = json_object_get(cmd,
"config");
1571 if (!json_is_string(jarg)) {
1572 json_object_set_new(answer,
"message", json_string(
"memcap name is not a string"));
1575 memcap = (
char *)json_string_value(jarg);
1577 for (
size_t i = 0; i <
ARRAY_SIZE(memcaps); i++) {
1578 if (strcmp(memcaps[i].name, memcap) == 0 && memcaps[i].
GetFunc) {
1580 uint64_t val = memcaps[i].
GetFunc();
1581 json_t *jobj = json_object();
1583 json_object_set_new(answer,
"message",
1584 json_string(
"internal error at json object creation"));
1591 MemcapBuildValue(val,
str,
sizeof(
str));
1594 json_object_set_new(jobj,
"value", json_string(
str));
1595 json_object_set_new(answer,
"message", jobj);
1600 json_object_set_new(answer,
"message",
1601 json_string(
"Memcap value not found. Use 'memcap-list' to show all"));
1605 TmEcode UnixSocketShowAllMemcap(json_t *cmd, json_t *answer,
void *data)
1607 json_t *jmemcaps = json_array();
1609 if (jmemcaps == NULL) {
1610 json_object_set_new(answer,
"message",
1611 json_string(
"internal error at json array creation"));
1615 for (
size_t i = 0; i <
ARRAY_SIZE(memcaps); i++) {
1616 json_t *jobj = json_object();
1618 json_decref(jmemcaps);
1619 json_object_set_new(answer,
"message",
1620 json_string(
"internal error at json object creation"));
1624 uint64_t val = memcaps[i].
GetFunc();
1629 MemcapBuildValue(val,
str,
sizeof(
str));
1632 json_object_set_new(jobj,
"name", json_string(memcaps[i].name));
1633 json_object_set_new(jobj,
"value", json_string(
str));
1634 json_array_append_new(jmemcaps, jobj);
1637 json_object_set_new(answer,
"message", jmemcaps);
1641 TmEcode UnixSocketGetFlowStatsById(json_t *cmd, json_t *answer,
void *data)
1644 json_t *jarg = json_object_get(cmd,
"flow_id");
1645 if (!json_is_integer(jarg)) {
1646 SCLogInfo(
"error: command is not a string");
1647 json_object_set_new(answer,
"message", json_string(
"flow_id is not an integer"));
1650 int64_t flow_id = json_integer_value(jarg);
1654 json_object_set_new(answer,
"message", json_string(
"Not found"));
1664 json_t *flow_info = json_object();
1665 if (flow_info == NULL) {
1668 json_object_set_new(flow_info,
"pkts_toclient", json_integer(tosrcpktcnt));
1669 json_object_set_new(flow_info,
"pkts_toserver", json_integer(todstpktcnt));
1670 json_object_set_new(flow_info,
"bytes_toclient", json_integer(tosrcbytecnt));
1671 json_object_set_new(flow_info,
"bytes_toserver", json_integer(todstbytecnt));
1672 json_object_set_new(flow_info,
"age", json_integer(age));
1673 json_object_set_new(answer,
"message", flow_info);
1678 #ifdef BUILD_UNIX_SOCKET
1682 static int RunModeUnixSocketMaster(
void)
1693 pcapcmd->running = 0;
1694 pcapcmd->current_file = NULL;
1696 memset(&unix_manager_pcap_last_processed, 0,
sizeof(
struct timespec));
1700 UnixManagerRegisterCommand(
"pcap-file", UnixSocketAddPcapFile, pcapcmd,
UNIX_CMD_TAKE_ARGS);
1701 UnixManagerRegisterCommand(
"pcap-file-continuous", UnixSocketAddPcapFileContinuous, pcapcmd,
UNIX_CMD_TAKE_ARGS);
1702 UnixManagerRegisterCommand(
"pcap-file-number", UnixSocketPcapFilesNumber, pcapcmd, 0);
1703 UnixManagerRegisterCommand(
"pcap-file-list", UnixSocketPcapFilesList, pcapcmd, 0);
1704 UnixManagerRegisterCommand(
"pcap-last-processed", UnixSocketPcapLastProcessed, pcapcmd, 0);
1705 UnixManagerRegisterCommand(
"pcap-interrupt", UnixSocketPcapInterrupt, pcapcmd, 0);
1706 UnixManagerRegisterCommand(
"pcap-current", UnixSocketPcapCurrent, pcapcmd, 0);
1708 UnixManagerRegisterBackgroundTask(UnixSocketPcapFilesCheck, pcapcmd);