// File: /workspace/github_c_makefile_star2_O1/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 $0x78, %rsp
movl %edi, %ebp
movl $0x2, %edi
movl $0x2, %esi
xorl %edx, %edx
callq 0x24e0
testl %eax, %eax
js 0x8457
movl %eax, %ebx
movl $0x190, %r15d           # imm = 0x190
movl $0x190, %edi            # imm = 0x190
movl $0x259, %esi            # imm = 0x259
callq 0x5d3e
movq %rax, %r14
leaq 0x30(%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 0x8409
leaq 0x30(%rsp), %r12
cmpl %r15d, 0x30(%rsp)
jl 0x84a3
addl $0x190, %r15d           # imm = 0x190
movq %r14, %rdi
movl $0x641, %esi            # imm = 0x641
callq 0x5f5b
movl %r15d, %edi
movl $0x259, %esi            # imm = 0x259
callq 0x5d3e
movq %rax, %r14
movl %r15d, 0x30(%rsp)
movq %rax, 0x38(%rsp)
movl $0x8912, %esi           # imm = 0x8912
movl %ebx, %edi
movq %r12, %rdx
xorl %eax, %eax
callq 0x21d0
testl %eax, %eax
jns 0x83bb
cmpb $0x1, %bpl
leaq 0x8bbe(%rip), %rax      # 0x10fd2
leaq 0x8a24(%rip), %r15      # 0x10e3f
cmoveq %rax, %r15
callq 0x2070
movl (%rax), %edi
callq 0x2480
leaq 0x8cf6(%rip), %rsi      # 0x11128
xorl %edi, %edi
movq %r15, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0xd5b0
movl %ebx, %edi
callq 0x21f0
movq %r14, %rdi
movl $0x646, %esi            # imm = 0x646
callq 0x5f5b
jmp 0x848f
cmpb $0x1, %bpl
leaq 0x8b70(%rip), %rax      # 0x10fd2
leaq 0x89d6(%rip), %rbx      # 0x10e3f
cmoveq %rax, %rbx
callq 0x2070
movl (%rax), %edi
callq 0x2480
leaq 0x8c75(%rip), %rsi      # 0x110f5
xorl %edi, %edi
movq %rbx, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0xd5b0
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x0, 0x30(%rsp)
jle 0x86cb
movb $0x1, 0x27(%rsp)
leaq 0x8ca2(%rip), %r13      # 0x1115c
movsbl %bpl, %r15d
movq %r14, %r12
movl %r15d, 0x2c(%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 0x86ba
movl $0x4, %edx
movq %rbp, %rdi
movq %r13, %rsi
callq 0x20a0
testl %eax, %eax
je 0x86ba
xorps %xmm0, %xmm0
movaps %xmm0, 0x50(%rsp)
movaps %xmm0, 0x40(%rsp)
movq $0x0, 0x60(%rsp)
movl $0xf, %edx
leaq 0x40(%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 0x8656
movl 0x50(%rsp), %eax
notl %eax
testb $0x41, %al
jne 0x86ba
movl $0x8915, %esi           # imm = 0x8915
movl %ebx, %edi
leaq 0x40(%rsp), %r15
movq %r15, %rdx
xorl %eax, %eax
callq 0x21d0
testl %eax, %eax
js 0x866d
movl 0x54(%rsp), %r13d
movl $0x891b, %esi           # imm = 0x891B
movl %ebx, %edi
movq %r15, %rdx
xorl %eax, %eax
callq 0x21d0
testl %eax, %eax
js 0x8691
movl 0x54(%rsp), %edi
andl %edi, %r13d
callq 0x633c
movl 0x2c(%rsp), %r15d
movl %r15d, 0x10(%rsp)
movl $0x1, 0x8(%rsp)
movl $0x44, (%rsp)
movzbl %al, %esi
movl %r13d, %edi
movl %esi, 0x28(%rsp)
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
movq %rbp, %r9
callq 0x79b0
movq %rbp, %rdi
leaq 0x70(%rsp), %rsi
callq 0x2cfc
testl %eax, %eax
jne 0x86b3
movzbl 0x27(%rsp), %ecx
addl $0x1a90, %ecx           # imm = 0x1A90
cmpb $0x1, %r15b
movl $0x0, %eax
cmovel %eax, %ecx
movl %r15d, (%rsp)
movl %r13d, %edi
movl 0x28(%rsp), %esi
movl $0x44, %edx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x7f9f
movl $0xf, %edx
movq %rbp, %rdi
leaq 0x8c0a(%rip), %rsi      # 0x1122d
callq 0x20a0
testl %eax, %eax
jne 0x8650
movl %r15d, (%rsp)
xorl %edi, %edi
xorl %esi, %esi
movl $0x44, %edx
movl $0x1a90, %ecx           # imm = 0x1A90
leaq 0x87cf(%rip), %r8       # 0x10e14
movl $0x2, %r9d
callq 0x7f9f
incb 0x27(%rsp)
jmp 0x86b3
callq 0x2070
movl (%rax), %edi
callq 0x2480
xorl %edi, %edi
leaq 0x8af6(%rip), %rsi      # 0x11161
jmp 0x8682
callq 0x2070
movl (%rax), %edi
callq 0x2480
xorl %edi, %edi
leaq 0x8b1e(%rip), %rsi      # 0x111a0
movq %rbp, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0xd5b0
jmp 0x86ba
callq 0x2070
movl (%rax), %edi
callq 0x2480
xorl %edi, %edi
leaq 0x8b3e(%rip), %rsi      # 0x111e4
movq %rbp, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0xd5b0
leaq 0x8aa2(%rip), %r13      # 0x1115c
movslq 0x30(%rsp), %rax
addq %r14, %rax
cmpq %rax, %r12
jb 0x84c6
movl %ebx, %edi
callq 0x21f0
movq %r14, %rdi
movl $0x645, %esi            # imm = 0x645
callq 0x5f5b
movl $0x1, %eax
jmp 0x8494