Revision 9e8238e7 net_helper-ml.c

View differences:

net_helper-ml.c
43 43

  
44 44
#define FDSSIZE 16
45 45

  
46
#define STUN_SERVER_DEFAULT "130.192.9.140"	//rucola.polito.it
47
#define STUN_PORT_DEFAULT 3478
48
#define STUN_SERVERS_MAX 32
49
static char *stun_servers[STUN_SERVERS_MAX];
50
static int stun_servers_cnt = 0;
51

  
46 52
static bool connect_on_know = false;	//whether to try to connect as soon as we get to know a nodeID
47 53

  
48 54
static int sIdx = 0;
......
193 199
static void init_myNodeID_cb (socketID_handle local_socketID,int errorstatus) {
194 200
	static int stun_retry_cnt = 0;
195 201
	int stun_retries = 2;	//set number of retries (0: no retry)
202
	char *stun_server;
203
	char *c;
204
	int stun_port;
196 205

  
197 206
	switch (errorstatus) {
198 207
	case 0:
......
221 230
		break;
222 231
	case 2:
223 232
		fprintf(stderr,"Net-helper init : NAT traversal timeout while creating socket\n");
224
		if (++stun_retry_cnt > stun_retries) {
233

  
234
		if (stun_servers[stun_servers_cnt+1]) {
235
			stun_servers_cnt++;
236
		} else {
237
			stun_servers_cnt = 0;
238
			stun_retry_cnt++;
239
		}
240
		stun_server = strdup(stun_servers[stun_servers_cnt]);
241

  
242
		if ((c = strchr(stun_server,':'))) {
243
			*c = 0;
244
			stun_port = atoi(c+1);
245
		} else {
246
			stun_port = STUN_PORT_DEFAULT;
247
		}
248

  
249
		//fprintf(stderr, "STUN server: %s:%d\n", stun_server, stun_port);
250

  
251
		if (stun_retry_cnt > stun_retries) {
225 252
			fprintf(stderr,"Net-helper init : Retrying without STUN\n");
226 253
			mlSetStunServer(0,NULL);
254
		} else {
255
			mlSetStunServer(stun_port, stun_server);
227 256
		}
228
	    break;
257
		free(stun_server);
258
		break;
229 259
	default :	// should never happen
230 260
		//
231 261
		fprintf(stderr,"Net-helper init : Unknown error in ml while creating socket\n");
......
357 387
//	event_base_loopbreak(base);
358 388
}
359 389

  
360

  
361 390
struct nodeID *net_helper_init(const char *IPaddr, int port, const char *config) {
362 391

  
363 392
	struct timeval tout = NH_ML_INIT_TIMEOUT;
364 393
	int s, i;
365 394
	struct tag *cfg_tags;
366 395
	const char *res;
367
	const char *stun_server = "130.192.9.140";	//rucola.polito.it
368
	int stun_port = 3478;
396
	char *stun_server_str;
397
	char *stun_server;
398
	int stun_port;
399
	char *c;
369 400
	const char *repo_address = NULL;
370 401
	int publish_interval = 60;
371 402

  
......
388 419
		return NULL;
389 420
	}
390 421

  
391
	res = config_value_str(cfg_tags, "stun_server");
392
	if (res) {
393
		stun_server = res;
394
	}
395
	config_value_int(cfg_tags, "stun_port", &stun_port);
422
	stun_server_str = config_value_str_default(cfg_tags, "stun_server", STUN_SERVER_DEFAULT);
396 423

  
397 424
	res = config_value_str(cfg_tags, "repo_address");
398 425
	if (res) {
......
424 451

  
425 452
	mlRegisterErrorConnectionCb(&connError_cb);
426 453
	mlRegisterRecvConnectionCb(&receive_conn_cb);
454

  
455
	for (i = 1, stun_servers[0] = strdup(stun_server_str); i<STUN_SERVERS_MAX ; i++) {
456
		char *next = strchr(stun_servers[i-1], '+');
457
		if (next) {
458
			*next = 0;
459
			stun_servers[i] = strdup(next+1);
460
		} else {
461
			break;
462
		}
463
	}
464

  
465
	stun_servers_cnt = 0;
466
	stun_server = strdup(stun_servers[stun_servers_cnt]);
467
	if ((c = strchr(stun_server,':'))) {
468
		*c = 0;
469
		stun_port = atoi(c+1);
470
	} else {
471
		stun_port = STUN_PORT_DEFAULT;
472
	}
473

  
474
	//fprintf(stderr, "STUN server: %s:%d\n", stun_server, stun_port);
475

  
427 476
	s = mlInit(1, tout, port, IPaddr, stun_port, stun_server, &init_myNodeID_cb, base);
428 477
	if (s < 0) {
429 478
		fprintf(stderr, "Net-helper : error initializing ML!\n");
430 479
		free(me);
480
		free(stun_server);
431 481
		return NULL;
432 482
	}
433 483

  
......
459 509
	timeoutFired = 0;
460 510
//	fprintf(stderr,"Net-helper init : back from init!\n");
461 511

  
512
	free(stun_server);
462 513
	return me;
463 514
}
464 515

  

Also available in: Unified diff