Revision 5e9e29e0 janus.c

View differences:

janus.c
27 27
#include "apierror.h"
28 28
#include "rtcp.h"
29 29
#include "sdp.h"
30
#include "utils.h"
30 31

  
31 32

  
32 33
static janus_config *config = NULL;
......
55 56
gchar *janus_get_local_ip() {
56 57
	return local_ip;
57 58
}
59
static gchar *public_ip = NULL;
60
gchar *janus_get_public_ip() {
61
	/* Fallback to the local IP, if we have no public one */
62
	return public_ip ? public_ip : local_ip;
63
}
64
void janus_set_public_ip(const char *ip) {
65
	if(ip == NULL)
66
		return;
67
	if(public_ip != NULL)
68
		g_free(public_ip);
69
	public_ip = g_strdup(ip);
70
}
58 71
static gint stop = 0;
59 72
gint janus_is_stopping() {
60 73
	return stop;
......
754 767
		MHD_destroy_response(response);
755 768
		return ret;
756 769
	}
757
	gint64 start = g_get_monotonic_time();
770
	gint64 start = janus_get_monotonic_time();
758 771
	gint64 end = 0;
759 772
	/* We have a timeout for the long poll: 30 seconds */
760 773
	while(end-start < 30*G_USEC_PER_SEC) {
......
765 778
		}
766 779
		/* Sleep 100ms */
767 780
		g_usleep(100000);
768
		end = g_get_monotonic_time();
781
		end = janus_get_monotonic_time();
769 782
	}
770 783
	if(event == NULL || event->payload == NULL) {
771 784
		JANUS_PRINT("Long poll time out for session %"SCNu64"...\n", session_id);
......
1171 1184
			janus_config_add_item(config, "nat", "stun_port", "3478");
1172 1185
		}
1173 1186
	}
1187
	if(args_info.public_ip_given) {
1188
		janus_config_add_item(config, "nat", "public_ip", args_info.public_ip_arg);
1189
	}
1190
	if(args_info.rtp_port_range_given) {
1191
		janus_config_add_item(config, "media", "rtp_port_range", args_info.rtp_port_range_arg);
1192
	}
1174 1193
	janus_config_print(config);
1175 1194

  
1176 1195
	/* What is the local public IP? */
......
1180 1199
		JANUS_PRINT("  -- Will try to use %s\n", item->value);
1181 1200
	struct ifaddrs *myaddrs, *ifa;
1182 1201
	int status = getifaddrs(&myaddrs);
1202
	char *tmp = NULL;
1183 1203
	if (status == 0) {
1184 1204
		for (ifa = myaddrs; ifa != NULL; ifa = ifa->ifa_next) {
1185 1205
			if(ifa->ifa_addr == NULL) {
......
1202 1222
							exit(1);
1203 1223
						}
1204 1224
					} else if(strcasecmp(buf, "127.0.0.1")) {	/* FIXME Check private IP addresses as well */
1205
						local_ip = strdup(buf);
1206
						if(local_ip == NULL) {
1207
							JANUS_DEBUG("Memory error!\n");
1208
							exit(1);
1209
						}
1225
						if(tmp == NULL)	/* FIXME Take note of the first IP we find, we'll use it as a backup */
1226
							tmp = strdup(buf);
1210 1227
					}
1211 1228
				}
1212 1229
			}
......
1215 1232
		freeifaddrs(myaddrs);
1216 1233
	}
1217 1234
	if(local_ip == NULL) {
1218
		JANUS_DEBUG("Couldn't find any address! using 127.0.0.1 as local IP... (which is NOT going to work out of your machine)\n");
1219
		local_ip = "127.0.0.1";
1220
	} else {
1221
		JANUS_PRINT("Using %s as local IP...\n", local_ip);
1235
		if(tmp != NULL) {
1236
			local_ip = tmp;
1237
		} else {
1238
			JANUS_DEBUG("Couldn't find any address! using 127.0.0.1 as local IP... (which is NOT going to work out of your machine)\n");
1239
			local_ip = g_strdup("127.0.0.1");
1240
		}
1222 1241
	}
1242
	JANUS_PRINT("Using %s as local IP...\n", local_ip);
1223 1243

  
1224 1244
	/* Pre-parse the web server path, if any */
1225 1245
	ws_path = "/janus";
......
1239 1259
	/* Setup ICE stuff (e.g., checking if the provided STUN server is correct) */
1240 1260
	char *stun_server = NULL;
1241 1261
	uint16_t stun_port = 0;
1262
	uint16_t rtp_min_port = 0, rtp_max_port = 0;
1263
	item = janus_config_get_item_drilldown(config, "media", "rtp_port_range");
1264
	if(item && item->value) {
1265
		/* Split in min and max port */
1266
		char *maxport = strrchr(item->value, '-');
1267
		if(maxport != NULL) {
1268
			*maxport = '\0';
1269
			maxport++;
1270
			rtp_min_port = atoi(item->value);
1271
			rtp_max_port = atoi(maxport);
1272
			maxport--;
1273
			maxport = '-';
1274
		}
1275
		if(rtp_min_port > rtp_max_port) {
1276
			int temp_port = rtp_min_port;
1277
			rtp_min_port = rtp_max_port;
1278
			rtp_max_port = temp_port;
1279
		}
1280
		if(rtp_max_port == 0)
1281
			rtp_max_port = 65535;
1282
		JANUS_PRINT("RTP port range: %u -- %u\n", rtp_min_port, rtp_max_port);
1283
	}
1242 1284
	item = janus_config_get_item_drilldown(config, "nat", "stun_server");
1243 1285
	if(item && item->value)
1244 1286
		stun_server = (char *)item->value;
1245 1287
	item = janus_config_get_item_drilldown(config, "nat", "stun_port");
1246 1288
	if(item && item->value)
1247 1289
		stun_port = atoi(item->value);
1248
	if(janus_ice_init(stun_server, stun_port) < 0) {
1290
	if(janus_ice_init(stun_server, stun_port, rtp_min_port, rtp_max_port) < 0) {
1249 1291
		JANUS_DEBUG("Invalid STUN address %s:%u\n", stun_server, stun_port);
1250 1292
		exit(1);
1251 1293
	}
1294

  
1295
	/* Is there a public_ip value to be used for NAT traversal instead? */
1296
	item = janus_config_get_item_drilldown(config, "nat", "public_ip");
1297
	if(item && item->value) {
1298
		if(public_ip != NULL)
1299
			g_free(public_ip);
1300
		public_ip = g_strdup((char *)item->value);
1301
		if(public_ip == NULL) {
1302
			JANUS_DEBUG("Memory error\n");
1303
			exit(1);
1304
		}
1305
		JANUS_PRINT("Using %s as our public IP in SDP\n", public_ip);
1306
	}
1252 1307
	
1253 1308
	/* Setup OpenSSL stuff */
1254 1309
	item = janus_config_get_item_drilldown(config, "certificates", "cert_pem");

Also available in: Unified diff