Browse Source

Deleted warnings in netsuku.c

Fixing server_opt default value assignement and free. Now we allocate
server_opt field and we strcpy default values in it.
Yann Weber 7 years ago
parent
commit
d33cd91c1c
3 changed files with 99 additions and 66 deletions
  1. 17
    0
      src/conf.h
  2. 56
    66
      src/netsukuku.c
  3. 26
    0
      src/netsukuku.h

+ 17
- 0
src/conf.h View File

@@ -36,6 +36,23 @@
36 36
  	if(_val)							\
37 37
  		*(str)=xstrndup(_val, (maxbytes));			\
38 38
 })
39
+#define CONF_SET_STRN_OPT(opt, cnf_name, n) \
40
+{ \
41
+	char *v; \
42
+	v = CONF_GET_VALUE((cnf_name)); \
43
+	if(v) \
44
+	{ \
45
+		if(strlen(v) > n - 1) v[n] = '\0'; \
46
+		*opt = realloc(*opt, strlen(v)); \
47
+		if(!opt) \
48
+		{ \
49
+			perror("Unable to allocate memory for option"); \
50
+			exit(-1); \
51
+		} \
52
+		strcpy(*opt, v); \
53
+	}\
54
+}
55
+
39 56
 
40 57
 
41 58
 /* 

+ 56
- 66
src/netsukuku.c View File

@@ -194,20 +194,20 @@ fill_default_options(void)
194 194
 
195 195
 	server_opt.family = AF_INET;
196 196
 
197
-	server_opt.config_file = NTK_CONFIG_FILE;
198
-	server_opt.pid_file = NTK_PID_FILE;
197
+	set_option_literal(&(server_opt.config_file), NTK_CONFIG_FILE);
198
+	set_option_literal(&(server_opt.pid_file), NTK_PID_FILE);
199 199
 
200
-	server_opt.int_map_file = INT_MAP_FILE;
201
-	server_opt.ext_map_file = EXT_MAP_FILE;
202
-	server_opt.bnode_map_file = BNODE_MAP_FILE;
200
+	set_option_literal(&(server_opt.int_map_file), INT_MAP_FILE);
201
+	set_option_literal(&(server_opt.ext_map_file), EXT_MAP_FILE);
202
+	set_option_literal(&(server_opt.bnode_map_file), BNODE_MAP_FILE);
203 203
 
204
-	server_opt.andna_hnames_file = ANDNA_HNAMES_FILE;
205
-	server_opt.snsd_nodes_file = SNSD_NODES_FILE;
206
-	server_opt.andna_cache_file = ANDNA_CACHE_FILE;
207
-	server_opt.lclkey_file = LCLKEY_FILE;
208
-	server_opt.lcl_file = LCL_FILE;
209
-	server_opt.rhc_file = RHC_FILE;
210
-	server_opt.counter_c_file = COUNTER_C_FILE;
204
+	set_option_literal(&(server_opt.andna_hnames_file), ANDNA_HNAMES_FILE);
205
+	set_option_literal(&(server_opt.snsd_nodes_file), SNSD_NODES_FILE);
206
+	set_option_literal(&(server_opt.andna_cache_file), ANDNA_CACHE_FILE);
207
+	set_option_literal(&(server_opt.lclkey_file), LCLKEY_FILE);
208
+	set_option_literal(&(server_opt.lcl_file), LCL_FILE);
209
+	set_option_literal(&(server_opt.rhc_file), RHC_FILE);
210
+	set_option_literal(&(server_opt.counter_c_file), COUNTER_C_FILE);
211 211
 
212 212
 	server_opt.daemon = 1;
213 213
 	server_opt.dbg_lvl = 0;
@@ -219,8 +219,8 @@ fill_default_options(void)
219 219
 
220 220
 	server_opt.use_shared_inet = 1;
221 221
 
222
-	server_opt.ip_masq_script = IPMASQ_SCRIPT_FILE;
223
-	server_opt.tc_shaper_script = TCSHAPER_SCRIPT_FILE;
222
+	set_option_literal(&(server_opt.ip_masq_script), IPMASQ_SCRIPT_FILE);
223
+	set_option_literal(&(server_opt.tc_shaper_script), TCSHAPER_SCRIPT_FILE);
224 224
 
225 225
 	server_opt.max_connections = MAX_CONNECTIONS;
226 226
 	server_opt.max_accepts_per_host = MAX_ACCEPTS;
@@ -236,31 +236,31 @@ void
236 236
 fill_loaded_cfg_options(void)
237 237
 {
238 238
 	char *value;
239
-
240
-	CONF_GET_STRN_VALUE(CONF_NTK_INT_MAP_FILE, &server_opt.int_map_file,
239
+	
240
+	CONF_SET_STRN_OPT(&(server_opt.int_map_file), CONF_NTK_INT_MAP_FILE,
241
+						NAME_MAX - 1);
242
+	CONF_SET_STRN_OPT(&(server_opt.bnode_map_file), CONF_NTK_BNODE_MAP_FILE,
241 243
 						NAME_MAX - 1);
242
-	CONF_GET_STRN_VALUE(CONF_NTK_BNODE_MAP_FILE,
243
-						&server_opt.bnode_map_file, NAME_MAX - 1);
244
-	CONF_GET_STRN_VALUE(CONF_NTK_EXT_MAP_FILE, &server_opt.ext_map_file,
244
+	CONF_SET_STRN_OPT(&(server_opt.ext_map_file), CONF_NTK_EXT_MAP_FILE,
245 245
 						NAME_MAX - 1);
246 246
 
247
-	CONF_GET_STRN_VALUE(CONF_ANDNA_HNAMES_FILE,
248
-						&server_opt.andna_hnames_file, NAME_MAX - 1);
249
-	CONF_GET_STRN_VALUE(CONF_SNSD_NODES_FILE, &server_opt.snsd_nodes_file,
247
+	CONF_SET_STRN_OPT(&(server_opt.andna_hnames_file), CONF_ANDNA_HNAMES_FILE,
248
+						NAME_MAX - 1);
249
+	CONF_SET_STRN_OPT(&(server_opt.snsd_nodes_file), CONF_SNSD_NODES_FILE,
250 250
 						NAME_MAX - 1);
251 251
 
252
-	CONF_GET_STRN_VALUE(CONF_ANDNA_CACHE_FILE,
253
-						&server_opt.andna_cache_file, NAME_MAX - 1);
254
-	CONF_GET_STRN_VALUE(CONF_ANDNA_LCLKEY_FILE, &server_opt.lclkey_file,
252
+	CONF_SET_STRN_OPT(&(server_opt.andna_cache_file), CONF_ANDNA_CACHE_FILE,
253
+						NAME_MAX - 1);
254
+	CONF_SET_STRN_OPT(&(server_opt.lclkey_file), CONF_ANDNA_LCLKEY_FILE,
255
+						NAME_MAX - 1);
256
+	CONF_SET_STRN_OPT(&(server_opt.lcl_file), CONF_ANDNA_LCL_FILE,
255 257
 						NAME_MAX - 1);
256
-	CONF_GET_STRN_VALUE(CONF_ANDNA_LCL_FILE, &server_opt.lcl_file,
258
+	CONF_SET_STRN_OPT(&(server_opt.rhc_file), CONF_ANDNA_RHC_FILE,
257 259
 						NAME_MAX - 1);
258
-	CONF_GET_STRN_VALUE(CONF_ANDNA_RHC_FILE, &server_opt.rhc_file,
260
+	CONF_SET_STRN_OPT(&(server_opt.counter_c_file), CONF_ANDNA_COUNTER_C_FILE,
259 261
 						NAME_MAX - 1);
260
-	CONF_GET_STRN_VALUE(CONF_ANDNA_COUNTER_C_FILE,
261
-						&server_opt.counter_c_file, NAME_MAX - 1);
262 262
 
263
-	CONF_GET_STRN_VALUE(CONF_NTK_PID_FILE, &server_opt.pid_file,
263
+	CONF_SET_STRN_OPT(&(server_opt.pid_file), CONF_NTK_PID_FILE,
264 264
 						NAME_MAX - 1);
265 265
 	CONF_GET_INT_VALUE(CONF_NTK_MAX_CONNECTIONS,
266 266
 					   server_opt.max_connections);
@@ -319,40 +319,25 @@ free_server_opt(void)
319 319
 {
320 320
 	int i;
321 321
 
322
-	if (server_opt.config_file != NTK_CONFIG_FILE)
323
-		xfree(server_opt.config_file);
324
-	if (server_opt.pid_file != NTK_PID_FILE)
325
-		xfree(server_opt.pid_file);
326
-
327
-	if (server_opt.int_map_file != INT_MAP_FILE)
328
-		xfree(server_opt.int_map_file);
329
-	if (server_opt.ext_map_file != EXT_MAP_FILE)
330
-		xfree(server_opt.ext_map_file);
331
-	if (server_opt.bnode_map_file != BNODE_MAP_FILE)
332
-		xfree(server_opt.bnode_map_file);
333
-
334
-	if (server_opt.andna_hnames_file != ANDNA_HNAMES_FILE)
335
-		xfree(server_opt.andna_hnames_file);
336
-	if (server_opt.snsd_nodes_file != SNSD_NODES_FILE)
337
-		xfree(server_opt.snsd_nodes_file);
338
-	if (server_opt.andna_cache_file != ANDNA_CACHE_FILE)
339
-		xfree(server_opt.andna_cache_file);
340
-	if (server_opt.lclkey_file != LCLKEY_FILE)
341
-		xfree(server_opt.lclkey_file);
342
-	if (server_opt.lcl_file != LCL_FILE)
343
-		xfree(server_opt.lcl_file);
344
-	if (server_opt.rhc_file != RHC_FILE)
345
-		xfree(server_opt.rhc_file);
346
-	if (server_opt.counter_c_file != COUNTER_C_FILE)
347
-		xfree(server_opt.counter_c_file);
348
-
349
-	if (server_opt.ip_masq_script != IPMASQ_SCRIPT_FILE)
350
-		xfree(server_opt.ip_masq_script);
351
-	if (server_opt.tc_shaper_script != TCSHAPER_SCRIPT_FILE)
352
-		xfree(server_opt.tc_shaper_script);
353
-
354
-	if (server_opt.inet_gw_dev)
355
-		xfree(server_opt.inet_gw_dev);
322
+	xfree(server_opt.config_file);
323
+	xfree(server_opt.pid_file);
324
+
325
+	xfree(server_opt.int_map_file);
326
+	xfree(server_opt.ext_map_file);
327
+	xfree(server_opt.bnode_map_file);
328
+
329
+	xfree(server_opt.andna_hnames_file);
330
+	xfree(server_opt.snsd_nodes_file);
331
+	xfree(server_opt.andna_cache_file);
332
+	xfree(server_opt.lclkey_file);
333
+	xfree(server_opt.lcl_file);
334
+	xfree(server_opt.rhc_file);
335
+	xfree(server_opt.counter_c_file);
336
+
337
+	xfree(server_opt.ip_masq_script);
338
+	xfree(server_opt.tc_shaper_script);
339
+
340
+	xfree(server_opt.inet_gw_dev);
356 341
 
357 342
 	for (i = 0; i < MAX_INTERFACES && server_opt.ifs[i]; i++)
358 343
 		xfree(server_opt.ifs[i]);
@@ -360,7 +345,7 @@ free_server_opt(void)
360 345
 
361 346
 /* Removes specified existing interface, Ntkd should populate the device list
362 347
  * prior to this.
363
- * returns 0 on success, And closes ntkd on error.
348
+ * returns 0 on success, return -1 and closes ntkd on error.
364 349
 */
365 350
 
366 351
 int
@@ -383,6 +368,7 @@ exclude_interface(void)
383 368
                 else
384 369
                     fatal("Interface %s not found!", optarg);
385 370
 	}
371
+	return -1;
386 372
 }
387 373
 
388 374
 void
@@ -489,7 +475,11 @@ parse_options(int argc, char **argv)
489 475
 			server_opt.family = AF_INET6;
490 476
 			break;
491 477
 		case 'c':
492
-			server_opt.config_file = xstrndup(optarg, NAME_MAX - 1);
478
+			if(strlen(optarg) > NAME_MAX - 1)
479
+			{
480
+				optarg[NAME_MAX] = '\0';
481
+			}
482
+			set_option_str(&(server_opt.config_file), optarg);
493 483
 			break;
494 484
 		case 'l':
495 485
 			if (log_to_file(optarg) < 0)

+ 26
- 0
src/netsukuku.h View File

@@ -193,4 +193,30 @@ ServOpt server_opt;
193 193
 
194 194
 time_t sigterm_timestamp, sighup_timestamp, sigalrm_timestamp;
195 195
 
196
+/*
197
+ * set_option_literal: set an option from a default value
198
+ */
199
+#define set_option_literal(opt, l) \
200
+{ \
201
+	if(!(*opt = malloc(strlen(l)+1))) \
202
+	{ \
203
+		perror("Unable to allocate memory for option default value");\
204
+		exit(-1); \
205
+	} \
206
+	strcpy(*opt, l); \
207
+}
208
+
209
+/*
210
+ * set_option_str: set an option from a default value
211
+ */
212
+#define set_option_str(opt, str) \
213
+{ \
214
+	if(!(*opt = realloc(*opt, strlen(str) + 1))) \
215
+	{ \
216
+		perror("Unable to allocate memory for option"); \
217
+		exit(-1); \
218
+	} \
219
+	strcpy(*opt, str); \
220
+}
221
+
196 222
 #endif							/*NETSUKUKU_H */

Loading…
Cancel
Save