// File: /workspace/github_c_makefile_star2_O2/open-mesh-mirror[P]batmand/linux/route.c
// Function: add_del_interface_rules
int add_del_interface_rules(int8_t rule_action)
{
	int32_t tmp_fd;
	uint32_t addr, netaddr;
	int len;
	uint8_t netmask, if_count = 1;
	char *buf, *buf_ptr;
	struct ifconf ifc;
	struct ifreq *ifr, ifr_tmp;
	struct batman_if *batman_if;


	tmp_fd = socket(AF_INET, SOCK_DGRAM, 0);

	if (tmp_fd < 0) {
		debug_output(0, "Error - can't %s interface rules (udp socket): %s\n", (rule_action == RULE_DEL ? "delete" : "add"), strerror(errno));
		return -1;
	}


	len = 10 * sizeof(struct ifreq); /* initial buffer size guess (10 interfaces) */

	while (1) {

		buf = debugMalloc(len, 601);
		ifc.ifc_len = len;
		ifc.ifc_buf = buf;

		if (ioctl(tmp_fd, SIOCGIFCONF, &ifc) < 0) {

			debug_output(0, "Error - can't %s interface rules (SIOCGIFCONF): %s\n", (rule_action == RULE_DEL ? "delete" : "add"), strerror(errno));
			close(tmp_fd);
			debugFree(buf, 1606);
			return -1;

		} else {

			if (ifc.ifc_len < len)
				break;

		}

		len += 10 * sizeof(struct ifreq);
		debugFree(buf, 1601);

	}

	for (buf_ptr = buf; buf_ptr < buf + ifc.ifc_len;) {

		ifr = (struct ifreq *)buf_ptr;
		buf_ptr += (ifr->ifr_addr.sa_family == AF_INET6 ? sizeof(struct sockaddr_in6) : sizeof(struct sockaddr)) + sizeof(ifr->ifr_name);

		/* ignore if not IPv4 interface */
		if (ifr->ifr_addr.sa_family != AF_INET)
			continue;

		/* the tunnel thread will remove the default route by itself */
		if (strncmp(ifr->ifr_name, "gate", 4) == 0)
			continue;

		memset(&ifr_tmp, 0, sizeof (struct ifreq));
		strncpy(ifr_tmp.ifr_name, ifr->ifr_name, IFNAMSIZ - 1);

		if (ioctl(tmp_fd, SIOCGIFFLAGS, &ifr_tmp ) < 0) {

			debug_output(0, "Error - can't get flags of interface %s (interface rules): %s\n", ifr->ifr_name, strerror(errno));
			continue;

		}

		/* ignore if not up and running */
		if (!( ifr_tmp.ifr_flags & IFF_UP) || !(ifr_tmp.ifr_flags & IFF_RUNNING))
			continue;


		if (ioctl(tmp_fd, SIOCGIFADDR, &ifr_tmp) < 0) {

			debug_output(0, "Error - can't get IP address of interface %s (interface rules): %s\n", ifr->ifr_name, strerror(errno));
			continue;

		}

		addr = ((struct sockaddr_in *)&ifr_tmp.ifr_addr)->sin_addr.s_addr;

		if (ioctl(tmp_fd, SIOCGIFNETMASK, &ifr_tmp) < 0) {

			debug_output(0, "Error - can't get netmask address of interface %s (interface rules): %s\n", ifr->ifr_name, strerror(errno));
			continue;

		}

		netaddr = (((struct sockaddr_in *)&ifr_tmp.ifr_addr)->sin_addr.s_addr & addr);
		netmask = bit_count(((struct sockaddr_in *)&ifr_tmp.ifr_addr)->sin_addr.s_addr);

		add_del_route(netaddr, netmask, 0, 0, 0, ifr->ifr_name, BATMAN_RT_TABLE_TUNNEL, ROUTE_TYPE_THROW, rule_action);

		if (is_batman_if(ifr->ifr_name, &batman_if))
			continue;

		add_del_rule(netaddr, netmask, BATMAN_RT_TABLE_TUNNEL, (rule_action == RULE_DEL ? 0 : BATMAN_RT_PRIO_TUNNEL + if_count), 0, RULE_TYPE_SRC, rule_action);

		if (strncmp( ifr->ifr_name, "lo", IFNAMSIZ - 1) == 0)
			add_del_rule(0, 0, BATMAN_RT_TABLE_TUNNEL, BATMAN_RT_PRIO_TUNNEL, "lo\0 ", RULE_TYPE_IIF, rule_action);

		if_count++;

	}

	close(tmp_fd);
	debugFree(buf, 1605);

	return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movl %edi, %ebp
pushq $0x2
popq %rdi
movl %edi, %esi
xorl %edx, %edx
callq 0x24e0
testl %eax, %eax
js 0x7e71
movl %eax, %ebx
movl $0x190, %r15d           # imm = 0x190
leaq 0x28(%rsp), %r12
movl %r15d, %edi
movl $0x259, %esi            # imm = 0x259
callq 0x58c9
movq %rax, %r14
movl %r15d, 0x28(%rsp)
movq %rax, 0x30(%rsp)
movl $0x8912, %esi           # imm = 0x8912
movl %ebx, %edi
movq %r12, %rdx
xorl %eax, %eax
callq 0x21d0
testl %eax, %eax
js 0x7eab
movl 0x28(%rsp), %eax
cmpl %r15d, %eax
jl 0x7efe
addl $0x190, %r15d           # imm = 0x190
movq %r14, %rdi
movl $0x641, %esi            # imm = 0x641
callq 0x5b1a
jmp 0x7e1f
cmpb $0x1, %bpl
leaq 0x81e6(%rip), %rax      # 0x10062
leaq 0x804c(%rip), %rbx      # 0xfecf
cmoveq %rax, %rbx
callq 0x2070
movl (%rax), %edi
callq 0x2480
leaq 0x82eb(%rip), %rsi      # 0x10185
xorl %edi, %edi
movq %rbx, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0xcb1c
jmp 0x7ef7
cmpb $0x1, %bpl
leaq 0x81ac(%rip), %rax      # 0x10062
leaq 0x8012(%rip), %r15      # 0xfecf
cmoveq %rax, %r15
callq 0x2070
movl (%rax), %edi
callq 0x2480
leaq 0x82e4(%rip), %rsi      # 0x101b8
xorl %edi, %edi
movq %r15, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0xcb1c
movl %ebx, %edi
callq 0x21f0
movq %r14, %rdi
movl $0x646, %esi            # imm = 0x646
callq 0x5b1a
pushq $-0x1
jmp 0x815c
movl %ebx, 0x20(%rsp)
movb $0x1, 0x1b(%rsp)
leaq 0x82de(%rip), %r12      # 0x101ec
leaq 0x40(%rsp), %rbx
pushq $0xf
popq %r15
movsbl %bpl, %ecx
movl %ecx, 0x24(%rsp)
movq %r14, %r13
cltq
addq %r14, %rax
cmpq %rax, %r13
jae 0x8144
movq %r13, %rbp
movzwl 0x10(%r13), %eax
addq $0x20, %r13
leaq 0x2c(%rbp), %rcx
cmpw $0xa, %ax
cmoveq %rcx, %r13
cmpw $0x2, %ax
jne 0x810c
movq %rbp, %rdi
movq %r12, %rsi
pushq $0x4
popq %rdx
callq 0x20a0
testl %eax, %eax
je 0x810c
xorps %xmm0, %xmm0
leaq 0x48(%rsp), %rax
movups %xmm0, 0x10(%rax)
movups %xmm0, (%rax)
movq %rbx, %rdi
movq %rbp, %rsi
movq %r15, %rdx
callq 0x2090
movl $0x8913, %esi           # imm = 0x8913
movl 0x20(%rsp), %edi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x21d0
testl %eax, %eax
js 0x80b8
movl 0x50(%rsp), %eax
notl %eax
testb $0x41, %al
jne 0x8105
movl $0x8915, %esi           # imm = 0x8915
movl 0x20(%rsp), %r15d
movl %r15d, %edi
movq %rbx, %r12
movq %rbx, %rdx
xorl %eax, %eax
callq 0x21d0
testl %eax, %eax
js 0x80dc
movl 0x54(%rsp), %eax
movl %eax, 0x1c(%rsp)
movl $0x891b, %esi           # imm = 0x891B
movl %r15d, %edi
movq %r12, %rdx
xorl %eax, %eax
callq 0x21d0
testl %eax, %eax
js 0x8115
movl 0x54(%rsp), %edi
movl 0x1c(%rsp), %r15d
andl %edi, %r15d
callq 0x5ed6
movl 0x24(%rsp), %r12d
movl %r12d, 0x10(%rsp)
movl $0x1, 0x8(%rsp)
movl $0x44, (%rsp)
movzbl %al, %eax
movl %r15d, 0x1c(%rsp)
movl %r15d, %edi
movl %eax, %r15d
movl %eax, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
movq %rbp, %r9
callq 0x745c
movq %rbp, %rdi
leaq 0x38(%rsp), %rsi
callq 0x2b8e
testl %eax, %eax
jne 0x8137
movzbl 0x1b(%rsp), %ecx
addl $0x1a90, %ecx           # imm = 0x1A90
cmpb $0x1, %r12b
movl $0x0, %eax
cmovel %eax, %ecx
movl %r12d, (%rsp)
movl 0x1c(%rsp), %edi
movl %r15d, %esi
pushq $0x44
popq %rdx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x7a5a
cmpb $0x6c, (%rbp)
jne 0x80b2
cmpb $0x6f, 0x1(%rbp)
jne 0x80b2
cmpb $0x0, 0x2(%rbp)
jne 0x80b2
movl %r12d, (%rsp)
xorl %edi, %edi
xorl %esi, %esi
pushq $0x44
popq %rdx
movl $0x1a90, %ecx           # imm = 0x1A90
leaq 0x7dfb(%rip), %r8       # 0xfea4
pushq $0x2
popq %r9
callq 0x7a5a
incb 0x1b(%rsp)
jmp 0x8137
callq 0x2070
movl (%rax), %edi
callq 0x2480
xorl %edi, %edi
leaq 0x8124(%rip), %rsi      # 0x101f1
movq %rbp, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0xcb1c
jmp 0x8105
callq 0x2070
movl (%rax), %edi
callq 0x2480
xorl %edi, %edi
leaq 0x813f(%rip), %rsi      # 0x10230
movq %rbp, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0xcb1c
movq %r12, %rbx
pushq $0xf
popq %r15
leaq 0x80e0(%rip), %r12      # 0x101ec
movl 0x28(%rsp), %eax
jmp 0x7f22
callq 0x2070
movl (%rax), %edi
callq 0x2480
xorl %edi, %edi
leaq 0x814a(%rip), %rsi      # 0x10274
movq %rbp, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0xcb1c
leaq 0x80ae(%rip), %r12      # 0x101ec
pushq $0xf
popq %r15
jmp 0x810c
movl 0x20(%rsp), %edi
callq 0x21f0
movq %r14, %rdi
movl $0x645, %esi            # imm = 0x645
callq 0x5b1a
pushq $0x1
popq %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq