// File: /workspace/github_c_makefile_star2_O3/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
movl $0x2, %edi
movl $0x2, %esi
xorl %edx, %edx
callq 0x24e0
testl %eax, %eax
js 0x87b9
movl %eax, %ebx
movl $0x190, %r15d           # imm = 0x190
movl $0x190, %edi            # imm = 0x190
movl $0x259, %esi            # imm = 0x259
callq 0x5d52
movq %rax, %r14
leaq 0x28(%rsp), %rdx
movl $0x190, (%rdx)          # imm = 0x190
movq %rax, 0x8(%rdx)
movl $0x8912, %esi           # imm = 0x8912
movl %ebx, %edi
xorl %eax, %eax
callq 0x21d0
testl %eax, %eax
js 0x876b
leaq 0x28(%rsp), %r12
movl 0x28(%rsp), %eax
cmpl %r15d, %eax
jl 0x8805
addl $0x190, %r15d           # imm = 0x190
movq %r14, %rdi
movl $0x641, %esi            # imm = 0x641
callq 0x628b
movl %r15d, %edi
movl $0x259, %esi            # imm = 0x259
callq 0x5d52
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
jns 0x871b
cmpb $0x1, %bpl
leaq 0x890c(%rip), %rax      # 0x11082
leaq 0x8772(%rip), %r15      # 0x10eef
cmoveq %rax, %r15
callq 0x2070
movl (%rax), %edi
callq 0x2480
leaq 0x8a44(%rip), %rsi      # 0x111d8
xorl %edi, %edi
movq %r15, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0xd88c
movl %ebx, %edi
callq 0x21f0
movq %r14, %rdi
movl $0x646, %esi            # imm = 0x646
callq 0x628b
jmp 0x87f1
cmpb $0x1, %bpl
leaq 0x88be(%rip), %rax      # 0x11082
leaq 0x8724(%rip), %rbx      # 0x10eef
cmoveq %rax, %rbx
callq 0x2070
movl (%rax), %edi
callq 0x2480
leaq 0x89c3(%rip), %rsi      # 0x111a5
xorl %edi, %edi
movq %rbx, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0xd88c
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testl %eax, %eax
jle 0x8a1f
movb $0x1, 0x23(%rsp)
leaq 0x89f3(%rip), %r13      # 0x1120c
movsbl %bpl, %r15d
movq %r14, %r12
movl %r15d, 0x24(%rsp)
movq %r12, %rbp
movzwl 0x10(%r12), %eax
addq $0x20, %r12
leaq 0x2c(%rbp), %rcx
cmpw $0xa, %ax
cmoveq %rcx, %r12
cmpw $0x2, %ax
jne 0x8a0e
movl $0x4, %edx
movq %rbp, %rdi
movq %r13, %rsi
callq 0x20a0
testl %eax, %eax
je 0x8a0e
xorps %xmm0, %xmm0
leaq 0x40(%rsp), %rax
movups %xmm0, 0x10(%rax)
movups %xmm0, (%rax)
movl $0xf, %edx
leaq 0x38(%rsp), %r15
movq %r15, %rdi
movq %rbp, %rsi
callq 0x2090
movl $0x8913, %esi           # imm = 0x8913
movl %ebx, %edi
movq %r15, %rdx
xorl %eax, %eax
callq 0x21d0
testl %eax, %eax
js 0x89aa
movl 0x48(%rsp), %eax
notl %eax
testb $0x41, %al
jne 0x8a0e
movl $0x8915, %esi           # imm = 0x8915
movl %ebx, %edi
leaq 0x38(%rsp), %r15
movq %r15, %rdx
xorl %eax, %eax
callq 0x21d0
testl %eax, %eax
js 0x89c1
movl 0x4c(%rsp), %r13d
movl $0x891b, %esi           # imm = 0x891B
movl %ebx, %edi
movq %r15, %rdx
xorl %eax, %eax
callq 0x21d0
testl %eax, %eax
js 0x89e5
movl 0x4c(%rsp), %edi
andl %edi, %r13d
callq 0x6663
movl 0x24(%rsp), %ecx
movl %ecx, 0x10(%rsp)
movl $0x1, 0x8(%rsp)
movl $0x44, (%rsp)
movzbl %al, %r15d
movl %r13d, %edi
movl %r15d, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
movq %rbp, %r9
callq 0x7cd4
movq %rbp, %rdi
leaq 0x60(%rsp), %rsi
callq 0x2cfc
testl %eax, %eax
jne 0x8a07
movzbl 0x23(%rsp), %ecx
addl $0x1a90, %ecx           # imm = 0x1A90
movl 0x24(%rsp), %edx
cmpb $0x1, %dl
movl $0x0, %eax
cmovel %eax, %ecx
movl %edx, (%rsp)
movl %r13d, %edi
movl %r15d, %esi
movl $0x44, %edx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x82f9
cmpb $0x6c, (%rbp)
jne 0x89a4
cmpb $0x6f, 0x1(%rbp)
jne 0x89a4
cmpb $0x0, 0x2(%rbp)
jne 0x89a4
movl 0x24(%rsp), %eax
movl %eax, (%rsp)
xorl %edi, %edi
xorl %esi, %esi
movl $0x44, %edx
movl $0x1a90, %ecx           # imm = 0x1A90
leaq 0x852b(%rip), %r8       # 0x10ec4
movl $0x2, %r9d
callq 0x82f9
incb 0x23(%rsp)
jmp 0x8a07
callq 0x2070
movl (%rax), %edi
callq 0x2480
xorl %edi, %edi
leaq 0x8852(%rip), %rsi      # 0x11211
jmp 0x89d6
callq 0x2070
movl (%rax), %edi
callq 0x2480
xorl %edi, %edi
leaq 0x887a(%rip), %rsi      # 0x11250
movq %rbp, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0xd88c
jmp 0x8a0e
callq 0x2070
movl (%rax), %edi
callq 0x2480
xorl %edi, %edi
leaq 0x889a(%rip), %rsi      # 0x11294
movq %rbp, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0xd88c
leaq 0x87fe(%rip), %r13      # 0x1120c
movslq 0x28(%rsp), %rax
addq %r14, %rax
cmpq %rax, %r12
jb 0x8825
movl %ebx, %edi
callq 0x21f0
movq %r14, %rdi
movl $0x645, %esi            # imm = 0x645
callq 0x628b
movl $0x1, %eax
jmp 0x87f6