@@ -31,6 +31,10 @@ extern NmapOps o;
31
31
static const char *NSE_PROTOCOL_OP[] = {" tcp" , " udp" , " sctp" , NULL };
32
32
static const int NSE_PROTOCOL[] = {IPPROTO_TCP, IPPROTO_UDP, IPPROTO_SCTP};
33
33
34
+ // Number of fields in the "version" table
35
+ // used as a hint to Lua when allocating it
36
+ #define NSE_NUM_VERSION_FIELDS 12
37
+
34
38
void set_version (lua_State *L, const struct serviceDeductions *sd)
35
39
{
36
40
nseU_setsfield (L, -1 , " name" , sd->name );
@@ -50,7 +54,7 @@ void set_version (lua_State *L, const struct serviceDeductions *sd)
50
54
sd->dtype == SERVICE_DETECTION_TABLE ? " table" :
51
55
sd->dtype == SERVICE_DETECTION_PROBED ? " probed" :
52
56
NULL );
53
- lua_newtable (L );
57
+ lua_createtable (L, sd-> cpe . size (), 0 );
54
58
for (size_t i = 0 ; i < sd->cpe .size (); i++) {
55
59
lua_pushstring (L, sd->cpe [i]);
56
60
lua_rawseti (L, -2 , i+1 );
@@ -73,7 +77,7 @@ void set_portinfo (lua_State *L, const Target *target, const Port *port)
73
77
nseU_setsfield (L, -1 , " state" , statenum2str (port->state ));
74
78
nseU_setsfield (L, -1 , " reason" , reason_str (port->reason .reason_id , 1 ));
75
79
nseU_setifield (L, -1 , " reason_ttl" , port->reason .ttl );
76
- lua_newtable (L );
80
+ lua_createtable (L, 0 , NSE_NUM_VERSION_FIELDS );
77
81
set_version (L, &sd);
78
82
lua_setfield (L, -2 , " version" );
79
83
}
@@ -108,14 +112,15 @@ static void push_osclass_table(lua_State *L,
108
112
const struct OS_Classification *osclass) {
109
113
unsigned int i;
110
114
111
- lua_newtable (L);
115
+ #define NSE_NUM_OSCLASS_FIELDS 5
116
+ lua_createtable (L, 0 , NSE_NUM_OSCLASS_FIELDS);
112
117
113
118
set_string_or_nil (L, " vendor" , osclass->OS_Vendor );
114
119
set_string_or_nil (L, " osfamily" , osclass->OS_Family );
115
120
set_string_or_nil (L, " osgen" , osclass->OS_Generation );
116
121
set_string_or_nil (L, " type" , osclass->Device_Type );
117
122
118
- lua_newtable (L );
123
+ lua_createtable (L, osclass-> cpe . size (), 0 );
119
124
for (i = 0 ; i < osclass->cpe .size (); i++) {
120
125
lua_pushstring (L, osclass->cpe [i]);
121
126
lua_rawseti (L, -2 , i + 1 );
@@ -127,12 +132,13 @@ static void push_osmatch_table(lua_State *L, const FingerMatch *match,
127
132
const OS_Classification_Results *OSR) {
128
133
int i;
129
134
130
- lua_newtable (L);
135
+ #define NSE_NUM_OSMATCH_FIELDS 2
136
+ lua_createtable (L, 0 , NSE_NUM_OSMATCH_FIELDS);
131
137
132
138
lua_pushstring (L, match->OS_name );
133
139
lua_setfield (L, -2 , " name" );
134
140
135
- lua_newtable (L );
141
+ lua_createtable (L, OSR-> OSC_num_matches , 0 );
136
142
for (i = 0 ; i < OSR->OSC_num_matches ; i++) {
137
143
push_osclass_table (L, OSR->OSC [i]);
138
144
lua_rawseti (L, -2 , i + 1 );
@@ -181,7 +187,8 @@ void set_hostinfo(lua_State *L, Target *currenths) {
181
187
push_bin_ip (L, currenths->SourceSockAddr ());
182
188
lua_setfield (L, -2 , " bin_ip_src" );
183
189
184
- lua_newtable (L);
190
+ #define NSE_NUM_TIMES_FIELDS 3
191
+ lua_createtable (L, 0 , NSE_NUM_TIMES_FIELDS);
185
192
nseU_setnfield (L, -1 , " srtt" , (lua_Number) currenths->to .srtt / 1000000.0 );
186
193
nseU_setnfield (L, -1 , " rttvar" , (lua_Number) currenths->to .rttvar / 1000000.0 );
187
194
nseU_setnfield (L, -1 , " timeout" , (lua_Number) currenths->to .timeout / 1000000.0 );
@@ -195,10 +202,11 @@ void set_hostinfo(lua_State *L, Target *currenths) {
195
202
{
196
203
std::list<TracerouteHop>::iterator it;
197
204
198
- lua_newtable (L );
205
+ lua_createtable (L, currenths-> traceroute_hops . size (), 0 );
199
206
for (it = currenths->traceroute_hops .begin (); it != currenths->traceroute_hops .end (); it++)
200
207
{
201
- lua_newtable (L);
208
+ #define NSE_NUM_TRACEROUTE_FIELDS 3
209
+ lua_createtable (L, 0 , NSE_NUM_TRACEROUTE_FIELDS);
202
210
/* fill the table if the hop has not timed out, otherwise an empty table
203
211
* is inserted */
204
212
if (!it->timedout ) {
@@ -230,7 +238,7 @@ void set_hostinfo(lua_State *L, Target *currenths) {
230
238
int i;
231
239
const OS_Classification_Results *OSR = FPR->getOSClassification ();
232
240
233
- lua_newtable (L );
241
+ lua_createtable (L, FPR-> num_perfect_matches , 0 );
234
242
for (i = 0 ; i < FPR->num_perfect_matches ; i++) {
235
243
push_osmatch_table (L, FPR->matches [i], OSR);
236
244
lua_rawseti (L, -2 , i + 1 );
@@ -460,7 +468,7 @@ static int l_get_ports (lua_State *L)
460
468
if (!(p = target->ports .nextPort (p, &port, protocol, state))) {
461
469
lua_pushnil (L);
462
470
} else {
463
- lua_newtable (L );
471
+ lua_createtable (L, 0 , NSE_NUM_PORTINFO_FIELDS );
464
472
set_portinfo (L, target, p);
465
473
}
466
474
return 1 ;
@@ -487,7 +495,7 @@ static int l_get_port_state (lua_State *L)
487
495
lua_pushnil (L);
488
496
else
489
497
{
490
- lua_newtable (L );
498
+ lua_createtable (L, 0 , NSE_NUM_PORTINFO_FIELDS );
491
499
set_portinfo (L, target, p);
492
500
}
493
501
return 1 ;
@@ -791,7 +799,7 @@ static int l_get_dns_servers (lua_State *L)
791
799
std::list<std::string> servs2 = get_dns_servers ();
792
800
std::list<std::string>::iterator servI2;
793
801
794
- lua_newtable (L );
802
+ lua_createtable (L, servs2. size (), 0 );
795
803
for (servI2 = servs2.begin (); servI2 != servs2.end (); servI2++)
796
804
nseU_appendfstr (L, -1 , " %s" , servI2->c_str ());
797
805
return 1 ;
@@ -882,10 +890,11 @@ static int l_list_interfaces (lua_State *L)
882
890
memset (ipstr, 0 , INET6_ADDRSTRLEN);
883
891
memset (&src, 0 , sizeof (src));
884
892
memset (&bcast, 0 , sizeof (bcast));
885
- lua_newtable (L ); // base table
893
+ lua_createtable (L, numifs, 0 ); // base table
886
894
887
895
for (i=0 ; i< numifs; i++) {
888
- lua_newtable (L); // interface table
896
+ #define NSE_NUM_INTERFACE_FIELDS 9
897
+ lua_createtable (L, 0 , NSE_NUM_INTERFACE_FIELDS); // interface table
889
898
nseU_setsfield (L, -1 , " device" , iflist[i].devfullname );
890
899
nseU_setsfield (L, -1 , " shortname" , iflist[i].devname );
891
900
nseU_setifield (L, -1 , " netmask" , iflist[i].netmask_bits );
0 commit comments