def _set_dmzlan_ifup_address_and_route(data, pos):
	ret = True
	emsg = []

	# Always ifups dmz
	e = xt_func.sudo(["ip link set dev", pos, "up"])
	if not e[0]: 
		ret = False
		emsg.append(e[1])

	for i in data["basic-subnet"]:
		ip_range = xt_func.iprange2list(i["ip"])
		inet = xt_func.block2cidr(ip_range[0]+ "/"+ i["mask"]) 
		for ip in ip_range:
			e = xt_func.sudo(["ip addr add", ip+ "/"+ i["mask"], "brd + dev", pos])
			if not e[0]: 
				ret = False
				emsg.append(e[1])
		e = xt_func.sudo(["ip route add", inet, "dev", pos, "src", ip_range[0], "table", const.RTAB[pos]])

	for i in data["static-route"]:
		e = xt_func.sudo(["ip route add", i["subnet"], "via", i["gateway"], "dev", pos, "table", const.RTAB[pos]])
		if not e[0]: 
			ret = False
			emsg.append(e[1])

	return (ret, emsg)
def _set_netpos(data):
	ret = True
	emsg = []

	for pos in const.ALL_POS:
		fname = const.XTCFG_DIR+ "netpos-"+ pos+ ".txt"
		xcfg = {
			"LOCALHOST": [], "PROXYARP": [], 
			"SUBNET": [], "ROUTE": [], "GATEWAY": []
		}
		if pos == "lan" or pos == "dmz":
			for i in data[pos]['basic-subnet']:
				xcfg["LOCALHOST"].append(i["ip"])
				ip1 = i["ip"].split("-")[0]
				cidr = xt_func.block2cidr(ip1 + "/" + i["mask"])
				xcfg["SUBNET"].append(cidr)
			xcfg["ROUTE"] = [xt_func.block2cidr(i["subnet"]) for i in data[pos]['static-route']]
			xcfg["GATEWAY"] = [i["gateway"] for i in data[pos]['static-route']]
		elif data[pos]["enable"] == 1:
			if data[pos]["type"] == "static":
				# dy_monitord charges PPPoE and DHCP types
				for i in data[pos]["static-mode"]["ip"]:
					xcfg["LOCALHOST"].append(i)
				ip1 = data[pos]["static-mode"]["ip"][0].split("-")[0]
				cidr = xt_func.block2cidr(ip1 + "/" + data[pos]["static-mode"]["mask"])
				xcfg["SUBNET"].append(cidr)
			xcfg["PROXYARP"] = [i for i in data[pos]['public-ip-passthrough']["ip"]]
		f = open(fname, "w")
		for i in ["LOCALHOST", "PROXYARP", "SUBNET", "ROUTE", "GATEWAY"]:
			f.write(i + "\n");
			for j in xcfg[i]:
				f.write(j + "\n");
		f.close()
		e = xt_func.sudo(["xtctl netpos", pos, fname]); 
		if not e[0]: 
			ret = False
			emsg.append(e[1])

	return (ret, emsg)
def _set_static_wan_ifup_address_and_route(data, pos):
	ret = True
	emsg = []

	if data["enable"] != 1: return (ret, [pos, "disabled"])

	e = xt_func.sudo(["ip link set dev", pos, "up"])
	if not e[0]: 
		ret = False
		emsg.append(e[1])

	# static supports only one subnet
	fst_ip = data["static-mode"]["ip"][0].split("-")[0]
	inet = xt_func.block2cidr(fst_ip + "/" + data["static-mode"]["mask"]) 
	for i in data["static-mode"]["ip"]:
		ip_range = xt_func.iprange2list(i)
		for ip in ip_range:
			e = xt_func.sudo(["ip addr add", ip+ "/"+ data["static-mode"]["mask"], "brd + dev", pos])
			if not e[0]: 
				ret = False
				emsg.append(e[1])
	e = xt_func.sudo(["ip route add default via", data["static-mode"]["gateway"], "dev", pos, "table", const.RTAB[pos]])
	e = xt_func.sudo(["ip route append default via", data["static-mode"]["gateway"], "dev", pos])

	if len(data["public-ip-passthrough"]["ip"]) > 0:
		#Copy addresses to DMZ for public-ip-passthrough
		for ip in ip_range:
			e = xt_func.sudo(["ip addr add", ip+ "/"+ data["static-mode"]["mask"], "brd + dev dmz"])
			if not e[0]: 
				ret = False
				emsg.append(e[1])
		#Don't forget route tables 
		e = xt_func.sudo(["ip route del", inet, "dev dmz"])
		e = xt_func.sudo(["ip route add", inet, "src", fst_ip, "dev dmz table", const.RTAB["dmz"]])

	return (ret, emsg)