-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathLua.cpp
2385 lines (1840 loc) · 69.5 KB
/
Lua.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
*
* (C) 2013-14 - ntop.org
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*/
#include "ntop_includes.h"
#ifndef _GETOPT_H
#define _GETOPT_H
#endif
#ifndef LIB_VERSION
#define LIB_VERSION "1.4.7"
#endif
#define MSG_VERSION 0
struct zmq_msg_hdr {
char url[32];
u_int32_t version;
u_int32_t size;
};
extern "C" {
#include "rrd.h"
#ifdef HAVE_GEOIP
extern const char * GeoIP_lib_version(void);
#endif
};
#include "third-party/lsqlite3/lsqlite3.c"
/* Included by Categorization.cpp */
extern char* http_get(char *host, u_int port, char *page, char* rsp, u_int rsp_len);
/* ******************************* */
Lua::Lua() {
L = luaL_newstate();
}
/* ******************************* */
Lua::~Lua() {
lua_close(L);
}
/* ******************************* */
/**
* @brief Check the expected type of lua function.
* @details Find in the lua stack the function and check the function parameters types.
*
* @param vm The lua state.
* @param func The function name.
* @param pos Index of lua stack.
* @param expected_type Index of expected type.
* @return @ref CONST_LUA_ERROR if the expected type is equal to function type, @ref CONST_LUA_PARAM_ERROR otherwise.
*/
static int ntop_lua_check(lua_State* vm, const char* func,
int pos, int expected_type) {
if(lua_type(vm, pos) != expected_type) {
ntop->getTrace()->traceEvent(TRACE_ERROR,
"%s : expected %s, got %s", func,
lua_typename(vm, expected_type),
lua_typename(vm, lua_type(vm,pos)));
return(CONST_LUA_PARAM_ERROR);
}
return(CONST_LUA_ERROR);
}
/* ****************************************** */
static NetworkInterface* handle_null_interface(lua_State* vm) {
ntop->getTrace()->traceEvent(TRACE_INFO, "Null interface: did you restart ntopng in the meantime?");
return(ntop->getInterfaceId(0));
}
/* ****************************************** */
static int ntop_dump_file(lua_State* vm) {
char *fname;
FILE *fd;
struct mg_connection *conn;
lua_getglobal(vm, CONST_HTTP_CONN);
if((conn = (struct mg_connection*)lua_touserdata(vm, lua_gettop(vm))) == NULL) {
ntop->getTrace()->traceEvent(TRACE_ERROR, "INTERNAL ERROR: null HTTP connection");
return(CONST_LUA_ERROR);
}
if(ntop_lua_check(vm, __FUNCTION__, 1, LUA_TSTRING)) return(CONST_LUA_ERROR);
if((fname = (char*)lua_tostring(vm, 1)) == NULL) return(CONST_LUA_PARAM_ERROR);
ntop->fixPath(fname);
if((fd = fopen(fname, "r")) != NULL) {
char tmp[1024];
while((fgets(tmp, sizeof(tmp), fd)) != NULL)
mg_printf(conn, "%s", tmp);
fclose(fd);
return(CONST_LUA_OK);
} else {
ntop->getTrace()->traceEvent(TRACE_INFO, "Unable to read file %s", fname);
return(CONST_LUA_ERROR);
}
}
/* ****************************************** */
/**
* @brief Get default interface name.
* @details Push the default interface name of ntop into the lua stack.
*
* @param vm The lua state.
* @return @ref CONST_LUA_OK.
*/
static int ntop_get_default_interface_name(lua_State* vm) {
lua_pushstring(vm, ntop->getInterfaceId(0)->get_name());
return(CONST_LUA_OK);
}
/* ****************************************** */
/**
* @brief Set the name of active interface id into lua stack.
*
* @param vm The lua stack.
* @return @ref CONST_LUA_OK.
*/
static int ntop_set_active_interface_id(lua_State* vm) {
NetworkInterface *iface;
u_int32_t id;
if(ntop_lua_check(vm, __FUNCTION__, 1, LUA_TNUMBER)) return(CONST_LUA_ERROR);
id = (u_int32_t)lua_tonumber(vm, 1);
iface = ntop->getInterfaceId(id);
if(iface != NULL)
lua_pushstring(vm, iface->get_name());
else
lua_pushnil(vm);
return(CONST_LUA_OK);
}
/* ****************************************** */
/**
* @brief Get the ntopng interface names.
*
* @param vm The lua state.
* @return @ref CONST_LUA_OK.
*/
static int ntop_get_interface_names(lua_State* vm) {
lua_newtable(vm);
for(int i=0; i<ntop->get_num_interfaces(); i++) {
char num[8];
snprintf(num, sizeof(num), "%d", i);
lua_push_str_table_entry(vm, num, ntop->getInterfaceId(i)->get_name());
}
return(CONST_LUA_OK);
}
/* ****************************************** */
/**
* @brief Flush the host contacts of the network interface defined into lua.
*
* @param vm The lua state.
* @return @ref CONST_LUA_OK if the network_interface lua variable is not NULL, @ref CONST_LUA_ERROR otherwise.
*/
static int ntop_flush_host_contacts(lua_State* vm) {
NetworkInterface *ntop_interface;
lua_getglobal(vm, "ntop_interface");
if((ntop_interface = (NetworkInterface*)lua_touserdata(vm, lua_gettop(vm))) == NULL) {
ntop_interface = handle_null_interface(vm);
}
if(ntop_interface) {
ntop_interface->flushHostContacts();
return(CONST_LUA_OK);
} else
return(CONST_LUA_ERROR);
}
/* ****************************************** */
/**
* @brief Find the network interface and set it as global variable to lua.
*
* @param vm The lua state.
* @return @ref CONST_LUA_OK
*/
static int ntop_find_interface(lua_State* vm) {
char *ifname;
if(ntop_lua_check(vm, __FUNCTION__, 1, LUA_TSTRING)) return(CONST_LUA_ERROR);
ifname = (char*)lua_tostring(vm, 1);
lua_pushlightuserdata(vm, (char*)ntop->getNetworkInterface(ifname));
lua_setglobal(vm, "ntop_interface");
return(CONST_LUA_OK);
}
/* ****************************************** */
/**
* @brief Get the ndpi statistics of interface.
* @details Get the ntop interface global variable of lua, get nDpistats of interface and push it into lua stack.
*
* @param vm The lua state.
* @return @ref CONST_LUA_OK
*/
static int ntop_get_ndpi_interface_stats(lua_State* vm) {
NetworkInterface *ntop_interface;
NdpiStats stats;
lua_getglobal(vm, "ntop_interface");
if((ntop_interface = (NetworkInterface*)lua_touserdata(vm, lua_gettop(vm))) == NULL) {
ntop_interface = handle_null_interface(vm);
}
if(ntop_interface) {
ntop_interface->getnDPIStats(&stats);
lua_newtable(vm);
stats.lua(ntop_interface, vm);
}
return(CONST_LUA_OK);
}
/* ****************************************** */
/**
* @brief Get the ndpi protocol name of protocol id of network interface.
* @details Get the ntop interface global variable of lua. Once do that, get the protocol id of lua stack and return into lua stack "Host-to-Host Contact" if protocol id is equal to host family id; the protocol name or null otherwise.
*
* @param vm The lua state.
* @return CONST_LUA_ERROR if ntop_interface is null, CONST_LUA_OK otherwise.
*/
static int ntop_get_ndpi_protocol_name(lua_State* vm) {
NetworkInterface *ntop_interface;
NdpiStats stats;
int proto;
lua_getglobal(vm, "ntop_interface");
if((ntop_interface = (NetworkInterface*)lua_touserdata(vm, lua_gettop(vm))) == NULL) {
ntop_interface = handle_null_interface(vm);
}
if(ntop_lua_check(vm, __FUNCTION__, 1, LUA_TNUMBER)) return(CONST_LUA_ERROR);
proto = (u_int32_t)lua_tonumber(vm, 1);
if(proto == HOST_FAMILY_ID)
lua_pushstring(vm, "Host-to-Host Contact");
else {
if(ntop_interface)
lua_pushstring(vm, ntop_interface->get_ndpi_proto_name(proto));
else
lua_pushnil(vm);
}
return(CONST_LUA_OK);
}
/* ****************************************** */
/**
* @brief Get the hosts of network interface.
* @details Get the ntop interface global variable of lua and return into lua stack a new hash table of host information (Host name and number of bytes sent and received).
*
* @param vm The lua state.
* @return CONST_LUA_ERROR if ntop_interface is null, CONST_LUA_OK otherwise.
*/
static int ntop_get_interface_hosts(lua_State* vm) {
NetworkInterface *ntop_interface;
lua_getglobal(vm, "ntop_interface");
if((ntop_interface = (NetworkInterface*)lua_touserdata(vm, lua_gettop(vm))) == NULL) {
ntop_interface = handle_null_interface(vm);
}
if(ntop_interface) ntop_interface->getActiveHostsList(vm, false);
return(CONST_LUA_OK);
}
/* ****************************************** */
/**
* @brief Get the hosts information of network interface.
* @details Get the ntop interface global variable of lua and return into lua stack a new hash table of hash tables containing the host information.
*
* @param vm The lua state.
* @return CONST_LUA_ERROR if ntop_interface is null, CONST_LUA_OK otherwise.
*/
static int ntop_get_interface_hosts_info(lua_State* vm) {
NetworkInterface *ntop_interface;
bool show_details;
lua_getglobal(vm, "ntop_interface");
if((ntop_interface = (NetworkInterface*)lua_touserdata(vm, lua_gettop(vm))) == NULL) {
ntop_interface = handle_null_interface(vm);
}
/* Optional */
if(lua_type(vm, 1) != LUA_TBOOLEAN)
show_details = true;
else
show_details = lua_toboolean(vm, 1) ? true : false;
if(ntop_interface) ntop_interface->getActiveHostsList(vm, show_details);
return(CONST_LUA_OK);
}
/* ****************************************** */
/**
* @brief Get the aggregated host information of network interface.
* @details Get the family, the host name from the lua stack and the ntop interface global variable of lua and return into lua stack a new hash table of hash tables containing the aggregated host information.
*
* @param vm The lua state.
* @return CONST_LUA_ERROR if ntop_interface is null, CONST_LUA_OK otherwise.
*/
static int ntop_get_interface_aggregated_hosts_info(lua_State* vm) {
NetworkInterface *ntop_interface;
u_int16_t family = 0;
char *host = NULL;
if(lua_type(vm, 1) == LUA_TNUMBER)
family = (u_int16_t)lua_tonumber(vm, 1);
if(lua_type(vm, 2) == LUA_TSTRING)
host = (char*)lua_tostring(vm, 2);
lua_getglobal(vm, "ntop_interface");
if((ntop_interface = (NetworkInterface*)lua_touserdata(vm, lua_gettop(vm))) == NULL) {
ntop_interface = handle_null_interface(vm);
}
if(ntop_interface) ntop_interface->getActiveAggregatedHostsList(vm, family, host);
return(CONST_LUA_OK);
}
/* ****************************************** */
/**
* @brief Get the number of aggregated hosts of network interface.
* @details Get the ntop interface global variable of lua and return into lua stack the number of aggregated hosts.
*
* @param vm The lua state.
* @return CONST_LUA_ERROR if ntop_interface is null, CONST_LUA_OK otherwise.
*/
static int ntop_get_interface_num_aggregated_hosts(lua_State* vm) {
NetworkInterface *ntop_interface;
lua_getglobal(vm, "ntop_interface");
if((ntop_interface = (NetworkInterface*)lua_touserdata(vm, lua_gettop(vm))) == NULL) {
ntop_interface = handle_null_interface(vm);
}
if(ntop_interface) lua_pushnumber(vm, ntop_interface->getNumAggregatedHosts());
return(CONST_LUA_OK);
}
/* ****************************************** */
/**
* @brief Check if the specified path is a directory and it exists.
* @details True if if the specified path is a directory and it exists, false otherwise.
*
* @param vm The lua state.
* @return CONST_LUA_OK
*/
static int ntop_is_dir(lua_State* vm) {
char *path;
struct stat buf;
int rc;
if(ntop_lua_check(vm, __FUNCTION__, 1, LUA_TSTRING)) return(CONST_LUA_ERROR);
path = (char*)lua_tostring(vm, 1);
rc = ((stat(path, &buf) != 0) || (!S_ISDIR(buf.st_mode))) ? 0 : 1;
lua_pushboolean(vm, rc);
return(CONST_LUA_OK);
}
/* ****************************************** */
/**
* @brief Check if the file or directory exists.
* @details Get the path of file/direcrotry from to lua stack and push true into lua stack if it exists, false otherwise.
*
* @param vm The lua state.
* @return CONST_LUA_OK
*/
static int ntop_get_file_dir_exists(lua_State* vm) {
char *path;
struct stat buf;
int rc;
if(ntop_lua_check(vm, __FUNCTION__, 1, LUA_TSTRING)) return(CONST_LUA_ERROR);
path = (char*)lua_tostring(vm, 1);
rc = (stat(path, &buf) != 0) ? 0 : 1;
// ntop->getTrace()->traceEvent(TRACE_ERROR, "%s: %d", path, rc);
lua_pushboolean(vm, rc);
return(CONST_LUA_OK);
}
/* ****************************************** */
/**
* @brief Check if ntop is running on windows.
* @details Push into lua stack 1 if ntop is running on windows, 0 otherwise.
*
* @param vm The lua state.
* @return CONST_LUA_OK.
*/
static int ntop_is_windows(lua_State* vm) {
lua_pushboolean(vm,
#ifdef WIN32
1
#else
0
#endif
);
return(CONST_LUA_OK);
}
/* ****************************************** */
/**
* @brief Scan the input directory and return the list of files.
* @details Get the path from the lua stack and push into a new hashtable the files name existing in the directory.
*
* @param vm The lua state.
* @return CONST_LUA_OK.
*/
static int ntop_list_dir_files(lua_State* vm) {
char *path;
DIR *dirp;
struct dirent *dp;
if(ntop_lua_check(vm, __FUNCTION__, 1, LUA_TSTRING)) return(CONST_LUA_ERROR);
path = (char*)lua_tostring(vm, 1);
ntop->fixPath(path);
lua_newtable(vm);
if((dirp = opendir(path)) != NULL) {
while ((dp = readdir(dirp)) != NULL)
if(dp->d_name && (dp->d_name[0] != '.')) {
lua_push_str_table_entry(vm, dp->d_name, dp->d_name);
}
(void)closedir(dirp);
}
return(CONST_LUA_OK);
}
/* ****************************************** */
/**
* @brief Get the system time and push it into the lua stack.
*
* @param vm The lua state.
* @return CONST_LUA_OK.
*/
static int ntop_gettimemsec(lua_State* vm) {
struct timeval tp;
double ret;
gettimeofday(&tp, NULL);
ret = (((double)tp.tv_usec) / (double)1000) + tp.tv_sec;
lua_pushnumber(vm, ret);
return(CONST_LUA_OK);
}
/* ****************************************** */
static int ntop_zmq_connect(lua_State* vm) {
char *endpoint, *topic;
void *context, *subscriber;
if(ntop_lua_check(vm, __FUNCTION__, 1, LUA_TSTRING)) return(CONST_LUA_PARAM_ERROR);
if((endpoint = (char*)lua_tostring(vm, 1)) == NULL) return(CONST_LUA_PARAM_ERROR);
if(ntop_lua_check(vm, __FUNCTION__, 2, LUA_TSTRING)) return(CONST_LUA_PARAM_ERROR);
if((topic = (char*)lua_tostring(vm, 2)) == NULL) return(CONST_LUA_PARAM_ERROR);
context = zmq_ctx_new(), subscriber = zmq_socket(context, ZMQ_SUB);
if(zmq_connect(subscriber, endpoint) != 0) {
zmq_close(subscriber);
zmq_ctx_destroy(context);
return(CONST_LUA_PARAM_ERROR);
}
if(zmq_setsockopt(subscriber, ZMQ_SUBSCRIBE, topic, strlen(topic)) != 0) {
zmq_close(subscriber);
zmq_ctx_destroy(context);
return -1;
}
lua_pushlightuserdata(vm, context);
lua_setglobal(vm, "zmq_context");
lua_pushlightuserdata(vm, subscriber);
lua_setglobal(vm, "zmq_subscriber");
return(CONST_LUA_OK);
}
/* ****************************************** */
/**
* @brief Delete the specified member(field) from the redis hash stored at key.
* @details Get the key parameter from the lua stack and delete it from redis.
*
* @param vm The lua stack.
* @return CONST_LUA_OK.
*/
static int ntop_delete_redis_key(lua_State* vm) {
char *key;
if(ntop_lua_check(vm, __FUNCTION__, 1, LUA_TSTRING)) return(CONST_LUA_PARAM_ERROR);
if((key = (char*)lua_tostring(vm, 1)) == NULL) return(CONST_LUA_PARAM_ERROR);
ntop->getRedis()->del(key);
return(CONST_LUA_OK);
}
/* ****************************************** */
/**
* @brief Get the members of a redis set.
* @details Get the set key form the lua stack and push the mambers name into lua stack.
*
* @param vm The lua state.
* @return CONST_LUA_OK.
*/
static int ntop_get_set_members_redis(lua_State* vm) {
char *key;
if(ntop_lua_check(vm, __FUNCTION__, 1, LUA_TSTRING)) return(CONST_LUA_PARAM_ERROR);
if((key = (char*)lua_tostring(vm, 1)) == NULL) return(CONST_LUA_PARAM_ERROR);
ntop->getRedis()->smembers(vm, key);
return(CONST_LUA_OK);
}
/* ****************************************** */
/**
* @brief Delete the specified member(field) from the redis hash stored at key.
* @details Get the member name and the hash key form the lua stack and remove the specified member.
*
* @param vm The lua state.
* @return CONST_LUA_OK.
*/
static int ntop_delete_hash_redis_key(lua_State* vm) {
char *key, *member;
if(ntop_lua_check(vm, __FUNCTION__, 1, LUA_TSTRING)) return(CONST_LUA_PARAM_ERROR);
if((key = (char*)lua_tostring(vm, 1)) == NULL) return(CONST_LUA_PARAM_ERROR);
if(ntop_lua_check(vm, __FUNCTION__, 2, LUA_TSTRING)) return(CONST_LUA_PARAM_ERROR);
if((member = (char*)lua_tostring(vm, 2)) == NULL) return(CONST_LUA_PARAM_ERROR);
ntop->getRedis()->hashDel(key, member);
return(CONST_LUA_OK);
}
/* ****************************************** */
static int ntop_zmq_disconnect(lua_State* vm) {
void *context, *subscriber;
lua_getglobal(vm, "zmq_context");
if((context = (void*)lua_touserdata(vm, lua_gettop(vm))) == NULL) {
ntop->getTrace()->traceEvent(TRACE_ERROR, "INTERNAL ERROR: NULL context");
return(CONST_LUA_ERROR);
}
lua_getglobal(vm, "zmq_subscriber");
if((subscriber = (void*)lua_touserdata(vm, lua_gettop(vm))) == NULL) {
ntop->getTrace()->traceEvent(TRACE_ERROR, "INTERNAL ERROR: NULL subscriber");
return(CONST_LUA_ERROR);
}
zmq_close(subscriber);
zmq_ctx_destroy(context);
return(CONST_LUA_OK);
}
/* ****************************************** */
static int ntop_zmq_receive(lua_State* vm) {
NetworkInterface *ntop_interface;
void *subscriber;
int size;
struct zmq_msg_hdr h;
char *payload;
int payload_len;
zmq_pollitem_t item;
int rc;
lua_getglobal(vm, "zmq_subscriber");
if((subscriber = (void*)lua_touserdata(vm, lua_gettop(vm))) == NULL) {
ntop->getTrace()->traceEvent(TRACE_ERROR, "INTERNAL ERROR: NULL subscriber");
return(CONST_LUA_ERROR);
}
lua_getglobal(vm, "ntop_interface");
if((ntop_interface = (NetworkInterface*)lua_touserdata(vm, lua_gettop(vm))) == NULL) {
ntop_interface = handle_null_interface(vm);
}
item.socket = subscriber;
item.events = ZMQ_POLLIN;
do {
rc = zmq_poll(&item, 1, 1000);
if (rc < 0 || !ntop_interface->isRunning()) return(CONST_LUA_PARAM_ERROR);
} while (rc == 0);
size = zmq_recv(subscriber, &h, sizeof(h), 0);
if(size != sizeof(h) || h.version != MSG_VERSION) {
ntop->getTrace()->traceEvent(TRACE_WARNING, "Unsupported publisher version [%d]", h.version);
return -1;
}
payload_len = h.size + 1;
if((payload = (char*)malloc(payload_len)) != NULL) {
size = zmq_recv(subscriber, payload, payload_len, 0);
payload[h.size] = '\0';
if(size > 0) {
json_object *o = json_tokener_parse(payload);
if(o != NULL) {
struct json_object_iterator it = json_object_iter_begin(o);
struct json_object_iterator itEnd = json_object_iter_end(o);
while (!json_object_iter_equal(&it, &itEnd)) {
char *key = (char*)json_object_iter_peek_name(&it);
const char *value = json_object_get_string(json_object_iter_peek_value(&it));
ntop->getTrace()->traceEvent(TRACE_NORMAL, "[%s]=[%s]", key, value);
json_object_iter_next(&it);
}
json_object_put(o);
}
lua_pushfstring(vm, "%s", payload);
ntop->getTrace()->traceEvent(TRACE_INFO, "[%u] %s", h.size, payload);
free(payload);
return(CONST_LUA_OK);
} else {
free(payload);
return(CONST_LUA_PARAM_ERROR);
}
} else
return(CONST_LUA_PARAM_ERROR);
}
/* ****************************************** */
/**
* @brief Check if the trace level of ntop is verbose.
* @details Push true into the lua stack if the trace level of ntop is set to MAX_TRACE_LEVEL, false otherwise.
*
* @param vm The lua state.
* @return CONST_LUA_OK.
*/
static int ntop_verbose_trace(lua_State* vm) {
lua_pushboolean(vm, (ntop->getTrace()->get_trace_level() == MAX_TRACE_LEVEL) ? true : false);
return(CONST_LUA_OK);
}
/* ****************************************** */
/**
* @brief Get the flow information of network interface.
* @details Get the ntop interface global variable of lua and push the minimal information of flows into the lua stack as a new hashtable.
*
* @param vm The lua state.
* @return CONST_LUA_OK.
*/
static int ntop_get_interface_flows_info(lua_State* vm) {
NetworkInterface *ntop_interface;
lua_getglobal(vm, "ntop_interface");
if((ntop_interface = (NetworkInterface*)lua_touserdata(vm, lua_gettop(vm))) == NULL) {
ntop_interface = handle_null_interface(vm);
}
if(ntop_interface) ntop_interface->getActiveFlowsList(vm);
return(CONST_LUA_OK);
}
/* ****************************************** */
static void getHostVlanInfo(char* lua_ip, char** host_ip,
u_int16_t* vlan_id,
char *buf, u_int buf_len) {
char *where, *vlan;
snprintf(buf, buf_len, "%s", lua_ip);
(*host_ip) = strtok_r(buf, "@", &where);
vlan = strtok_r(NULL, "@", &where);
if(vlan)
(*vlan_id) = (u_int16_t)atoi(vlan);
}
/* ****************************************** */
/**
* @brief Get the host information of network interface.
* @details Get the ntop interface global variable of lua, the host ip and optional the VLAN id form the lua stack and push a new hash table of hash tables containing the host information into lua stack.
*
* @param vm The lua state.
* @return CONST_LUA_ERROR if ntop_interface is null or the host is null, CONST_LUA_OK otherwise.
*/
static int ntop_get_interface_host_info(lua_State* vm) {
NetworkInterface *ntop_interface;
char *host_ip;
u_int16_t vlan_id = 0;
char buf[64];
if(ntop_lua_check(vm, __FUNCTION__, 1, LUA_TSTRING)) return(CONST_LUA_ERROR);
getHostVlanInfo((char*)lua_tostring(vm, 1),&host_ip, &vlan_id, buf, sizeof(buf));
/* Optional VLAN id */
if(lua_type(vm, 2) == LUA_TNUMBER) vlan_id = (u_int16_t)lua_tonumber(vm, 2);
lua_getglobal(vm, "ntop_interface");
if((ntop_interface = (NetworkInterface*)lua_touserdata(vm, lua_gettop(vm))) == NULL) {
ntop_interface = handle_null_interface(vm);
}
if((!ntop_interface) || !ntop_interface->getHostInfo(vm, host_ip, vlan_id))
return(CONST_LUA_ERROR);
else
return(CONST_LUA_OK);
}
/* ****************************************** */
static int ntop_correalate_host_activity(lua_State* vm) {
NetworkInterface *ntop_interface;
char *host_ip;
u_int16_t vlan_id = 0;
char buf[64];
if(ntop_lua_check(vm, __FUNCTION__, 1, LUA_TSTRING)) return(CONST_LUA_ERROR);
getHostVlanInfo((char*)lua_tostring(vm, 1),&host_ip, &vlan_id, buf, sizeof(buf));
/* Optional VLAN id */
if(lua_type(vm, 2) == LUA_TNUMBER) vlan_id = (u_int16_t)lua_tonumber(vm, 2);
lua_getglobal(vm, "ntop_interface");
if((ntop_interface = (NetworkInterface*)lua_touserdata(vm, lua_gettop(vm))) == NULL) {
ntop_interface = handle_null_interface(vm);
}
if((!ntop_interface) || !ntop_interface->correlateHostActivity(vm, host_ip, vlan_id))
return(CONST_LUA_ERROR);
else
return(CONST_LUA_OK);
}
/* ****************************************** */
static int ntop_similar_host_activity(lua_State* vm) {
NetworkInterface *ntop_interface;
char *host_ip;
u_int16_t vlan_id = 0;
char buf[64];
if(ntop_lua_check(vm, __FUNCTION__, 1, LUA_TSTRING)) return(CONST_LUA_ERROR);
getHostVlanInfo((char*)lua_tostring(vm, 1),&host_ip, &vlan_id, buf, sizeof(buf));
/* Optional VLAN id */
if(lua_type(vm, 2) == LUA_TNUMBER) vlan_id = (u_int16_t)lua_tonumber(vm, 2);
lua_getglobal(vm, "ntop_interface");
if((ntop_interface = (NetworkInterface*)lua_touserdata(vm, lua_gettop(vm))) == NULL) {
ntop_interface = handle_null_interface(vm);
}
if((!ntop_interface) || !ntop_interface->similarHostActivity(vm, host_ip, vlan_id))
return(CONST_LUA_ERROR);
else
return(CONST_LUA_OK);
}
/* ****************************************** */
static int ntop_get_interface_host_activitymap(lua_State* vm) {
NetworkInterface *ntop_interface;
char *host_ip;
GenericHost *h;
bool aggregated;
u_int16_t vlan_id = 0;
char buf[64];
if(ntop_lua_check(vm, __FUNCTION__, 1, LUA_TSTRING)) return(CONST_LUA_ERROR);
getHostVlanInfo((char*)lua_tostring(vm, 1),&host_ip, &vlan_id, buf, sizeof(buf));
if(ntop_lua_check(vm, __FUNCTION__, 2, LUA_TBOOLEAN)) return(CONST_LUA_ERROR);
aggregated = lua_toboolean(vm, 2) ? true : false;
/* Optional VLAN id */
if(lua_type(vm, 3) == LUA_TNUMBER) vlan_id = (u_int16_t)lua_tonumber(vm, 3);
lua_getglobal(vm, "ntop_interface");
if((ntop_interface = (NetworkInterface*)lua_touserdata(vm, lua_gettop(vm))) == NULL) {
ntop_interface = handle_null_interface(vm);
}
if(!ntop_interface) return(CONST_LUA_ERROR);
if(!aggregated)
h = ntop_interface->getHost(host_ip, vlan_id);
else
h = ntop_interface->getAggregatedHost(host_ip);
if(h == NULL)
return(CONST_LUA_ERROR);
else {
char *json = h->getJsonActivityMap();
lua_pushfstring(vm, "%s", json);
free(json);
return(CONST_LUA_OK);
}
}
/* ****************************************** */
/**
* @brief Restore the host of network interface.
* @details Get the ntop interface global variable of lua and the IP address of host form the lua stack and restore the host into hash host of network interface.
*
* @param vm The lua state.
* @return CONST_LUA_ERROR if ntop_interface is null or if is impossible to restore the host, CONST_LUA_OK otherwise.
*/
static int ntop_restore_interface_host(lua_State* vm) {
NetworkInterface *ntop_interface;
char *host_ip;
u_int16_t vlan_id = 0;
char buf[64];
if(ntop_lua_check(vm, __FUNCTION__, 1, LUA_TSTRING)) return(CONST_LUA_ERROR);
getHostVlanInfo((char*)lua_tostring(vm, 1), &host_ip, &vlan_id, buf, sizeof(buf));
lua_getglobal(vm, "ntop_interface");
if((ntop_interface = (NetworkInterface*)lua_touserdata(vm, lua_gettop(vm))) == NULL) {
ntop_interface = handle_null_interface(vm);
}
if((!ntop_interface) || !ntop_interface->restoreHost(host_ip))
return(CONST_LUA_ERROR);
else
return(CONST_LUA_OK);
}
/* ****************************************** */
/**
* @brief Get the aggregated host information of network interface.
* @details Get the host name from the lua stack and the ntop interface global variable of lua and return into lua stack a new hash table of hash tables containing the aggregated host information.
*
* @param vm The lua state.
* @return CONST_LUA_ERROR if ntop_interface is null, CONST_LUA_OK otherwise.
*/
static int ntop_get_interface_aggregated_host_info(lua_State* vm) {
NetworkInterface *ntop_interface;
char *host_name;
u_int16_t vlan_id = 0;
char buf[64];
if(ntop_lua_check(vm, __FUNCTION__, 1, LUA_TSTRING)) return(CONST_LUA_ERROR);
getHostVlanInfo((char*)lua_tostring(vm, 1), &host_name, &vlan_id, buf, sizeof(buf));
lua_getglobal(vm, "ntop_interface");
if((ntop_interface = (NetworkInterface*)lua_touserdata(vm, lua_gettop(vm))) == NULL) {
ntop_interface = handle_null_interface(vm);
}
if((!ntop_interface) || (!ntop_interface->getAggregatedHostInfo(vm, host_name)))
return(CONST_LUA_ERROR);
else
return(CONST_LUA_OK);
}
/* ****************************************** */
static int ntop_get_interface_aggregation_families(lua_State* vm) {
NetworkInterface *ntop_interface;
lua_getglobal(vm, "ntop_interface");
if((ntop_interface = (NetworkInterface*)lua_touserdata(vm, lua_gettop(vm))) == NULL) {
ntop_interface = handle_null_interface(vm);
}
if((!ntop_interface) || (!ntop_interface->getAggregatedFamilies(vm)))
return(CONST_LUA_ERROR);
else
return(CONST_LUA_OK);
}
/* ****************************************** */
static int ntop_get_aggregregations_for_host(lua_State* vm) {
NetworkInterface *ntop_interface;
char *host_name;
u_int16_t vlan_id = 0;
char buf[64];
if(ntop_lua_check(vm, __FUNCTION__, 1, LUA_TSTRING)) return(CONST_LUA_ERROR);
getHostVlanInfo((char*)lua_tostring(vm, 1), &host_name, &vlan_id, buf, sizeof(buf));
lua_getglobal(vm, "ntop_interface");
if((ntop_interface = (NetworkInterface*)lua_touserdata(vm, lua_gettop(vm))) == NULL) {
ntop_interface = handle_null_interface(vm);
}
if((!ntop_interface) || (!ntop_interface->getAggregationsForHost(vm, host_name)))
return(CONST_LUA_ERROR);
else
return(CONST_LUA_OK);
}
/* ****************************************** */
static int ntop_get_interface_flows_peers(lua_State* vm) {
NetworkInterface *ntop_interface;
char *host_name;
u_int16_t vlan_id = 0;
char buf[64];
if(lua_type(vm, 1) == LUA_TSTRING)
getHostVlanInfo((char*)lua_tostring(vm, 1), &host_name, &vlan_id, buf, sizeof(buf));
else
host_name = NULL;
/* Optional VLAN id */
if(lua_type(vm, 2) == LUA_TNUMBER) vlan_id = (u_int16_t)lua_tonumber(vm, 2);
lua_getglobal(vm, "ntop_interface");
if((ntop_interface = (NetworkInterface*)lua_touserdata(vm, lua_gettop(vm))) == NULL) {
ntop_interface = handle_null_interface(vm);
}
if(ntop_interface) ntop_interface->getFlowPeersList(vm, host_name,vlan_id);
return(CONST_LUA_OK);
}
/* ****************************************** */