// File: /workspace/github_c_makefile_star2_O2/open-mesh-mirror[P]batmand/linux/route.c
// Function: flush_routes_rules
int flush_routes_rules(int8_t is_rule)
{
	int netlink_sock, rtl;
	size_t len;
	int32_t dest = 0, router = 0, ifi = 0;
	uint32_t prio = 0;
	int8_t rule_type = 0;
	char buf[8192], *dev = NULL;
	struct sockaddr_nl nladdr;
	struct iovec iov;
	struct msghdr msg;
	struct nlmsghdr *nh;
	struct rtmsg *rtm;
	struct req_s {
		struct rtmsg rtm;
	} *req;
	char req_buf[NLMSG_LENGTH(sizeof(struct req_s))] ALIGN_WORD;

	struct rtattr *rtap;

	iov.iov_base = buf;
	iov.iov_len  = sizeof(buf);

	nh = (struct nlmsghdr *)req_buf;
	req = (struct req_s*)NLMSG_DATA(req_buf);
	memset(&nladdr, 0, sizeof(struct sockaddr_nl));
	memset(req_buf, 0, NLMSG_LENGTH(sizeof(struct req_s)));
	memset(&msg, 0, sizeof(struct msghdr));

	nladdr.nl_family = AF_NETLINK;

	nh->nlmsg_len = NLMSG_LENGTH(sizeof(struct req_s));
	nh->nlmsg_pid = getpid();
	req->rtm.rtm_family = AF_INET;

	nh->nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
	nh->nlmsg_type = (is_rule ? RTM_GETRULE : RTM_GETROUTE);
	req->rtm.rtm_scope = RTN_UNICAST;

	if ((netlink_sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE)) < 0) {

		debug_output(0, "Error - can't create netlink socket for flushing the routing table: %s\n", strerror(errno));
		return -1;

	}


	if (sendto(netlink_sock, req_buf, nh->nlmsg_len, 0, (struct sockaddr *)&nladdr, sizeof(struct sockaddr_nl)) < 0) {

		debug_output(0, "Error - can't send message to kernel via netlink socket for flushing the routing table: %s\n", strerror(errno));
		close(netlink_sock);
		return -1;

	}

	msg.msg_name = (void *)&nladdr;
	msg.msg_namelen = sizeof(nladdr);
	msg.msg_iov = &iov;
	msg.msg_iovlen = 1;
	msg.msg_control = NULL;

	len = recvmsg(netlink_sock, &msg, 0);
	nh = (struct nlmsghdr *)buf;

	while (NLMSG_OK(nh, len)) {

		if (nh->nlmsg_type == NLMSG_DONE)
			break;

		if ((nh->nlmsg_type == NLMSG_ERROR) && (((struct nlmsgerr*)NLMSG_DATA(nh))->error != 0)) {

			debug_output(0, "Error - can't flush %s: %s \n", (is_rule ? "routing rules" : "routing table"), strerror(-((struct nlmsgerr*)NLMSG_DATA(nh))->error));
			close(netlink_sock);
			return -1;

		}

		rtm = (struct rtmsg *)NLMSG_DATA(nh);
		rtap = (struct rtattr *)RTM_RTA(rtm);
		rtl = RTM_PAYLOAD(nh);

		nh = NLMSG_NEXT(nh, len);

		if ((rtm->rtm_table != BATMAN_RT_TABLE_UNREACH) && (rtm->rtm_table != BATMAN_RT_TABLE_NETWORKS) &&
			(rtm->rtm_table != BATMAN_RT_TABLE_HOSTS) && (rtm->rtm_table != BATMAN_RT_TABLE_TUNNEL))
			continue;

		while (RTA_OK(rtap, rtl)) {

			switch(rtap->rta_type) {

				case RTA_DST:
					dest = *((int32_t *)RTA_DATA(rtap));
					rule_type = ROUTE_TYPE_UNREACHABLE;
					break;

				case RTA_SRC:
					dest = *((int32_t *)RTA_DATA(rtap));
					rule_type = ROUTE_TYPE_UNICAST;
					break;

				case RTA_GATEWAY:
					router = *((int32_t *)RTA_DATA(rtap));
					break;

				case RTA_OIF:
					ifi = *((int32_t *)RTA_DATA(rtap));
					break;

				case RTA_PRIORITY:
					prio = *((uint32_t *)RTA_DATA(rtap));
					break;

				case RTA_IIF:
					dev = ((char *)RTA_DATA(rtap));
					rule_type = ROUTE_TYPE_THROW;
					break;

				case 15:  /* FIXME: RTA_TABLE is not always available - not needed but avoid warning */
					break;

				case RTA_PREFSRC:  /* rta_type 7 - not needed but avoid warning */
					break;

				default:
					debug_output(0, "Error - unknown rta type: %i \n", rtap->rta_type);
					break;

			}

			rtap = RTA_NEXT(rtap,rtl);

		}

		if (is_rule) {
			switch (rule_type) {
			case ROUTE_TYPE_UNICAST:
				add_del_rule(dest, rtm->rtm_src_len, rtm->rtm_table, prio, 0 , rule_type, RULE_DEL);
				break;
			case ROUTE_TYPE_THROW:
				add_del_rule(dest, rtm->rtm_dst_len, rtm->rtm_table, prio, 0 , rule_type, RULE_DEL);
				break;
			case ROUTE_TYPE_UNREACHABLE:
				add_del_rule(0 , 0, rtm->rtm_table, prio, dev , rule_type, RULE_DEL);
				break;
			}
		} else {
			switch (rtm->rtm_type) {
			case RTN_UNICAST:
				rule_type = ROUTE_TYPE_UNICAST;
				break;
			case RTN_THROW:
				rule_type = ROUTE_TYPE_THROW;
				break;
			case RTN_UNREACHABLE:
				rule_type = ROUTE_TYPE_UNREACHABLE;
				break;
			default:
				rule_type = ROUTE_TYPE_UNKNOWN;
				break;
			}

			/* sometimes dest and router are not reset */
			if (rule_type == ROUTE_TYPE_UNREACHABLE)
				dest = router = 0;

			add_del_route(dest, rtm->rtm_dst_len, router, 0, ifi, "unknown", rtm->rtm_table, rule_type, ROUTE_DEL);
		}

	}

	close(netlink_sock);
	return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x20c8, %rsp           # imm = 0x20C8
movl %edi, %ebp
andq $0x0, 0x40(%rsp)
leaq 0xc0(%rsp), %r12
andl $0x0, 0x48(%rsp)
movq %r12, 0x78(%rsp)
movq $0x2000, 0x80(%rsp) # imm = 0x2000
xorps %xmm0, %xmm0
movups %xmm0, 0x58(%rsp)
andl $0x0, 0x68(%rsp)
movups %xmm0, 0x90(%rsp)
movups %xmm0, 0xa0(%rsp)
movups %xmm0, 0xb0(%rsp)
movw $0x10, 0x40(%rsp)
movl $0x1c, 0x50(%rsp)
callq 0x2100
movl %eax, 0x5c(%rsp)
movb $0x2, 0x60(%rsp)
movw $0x301, 0x56(%rsp)      # imm = 0x301
xorl %eax, %eax
testb %bpl, %bpl
setne %al
leal 0x1a(,%rax,8), %eax
movw %ax, 0x54(%rsp)
movb $0x1, 0x66(%rsp)
pushq $0x10
popq %rdi
pushq $0x2
popq %rsi
xorl %edx, %edx
callq 0x24e0
testl %eax, %eax
js 0x853a
movl %eax, %ebx
leaq 0x50(%rsp), %rsi
pushq $0x1c
popq %rdx
leaq 0x40(%rsp), %r14
pushq $0xc
popq %r9
movl %eax, %edi
xorl %ecx, %ecx
movq %r14, %r8
callq 0x21e0
testq %rax, %rax
js 0x8564
movl %ebp, 0x28(%rsp)
leaq 0x88(%rsp), %rsi
movq %r14, (%rsi)
movl $0xc, 0x8(%rsi)
leaq 0x78(%rsp), %rax
movq %rax, 0x10(%rsi)
movq $0x1, 0x18(%rsi)
andq $0x0, 0x20(%rsi)
movl %ebx, %edi
xorl %edx, %edx
callq 0x2360
movq %rax, %r15
leaq 0x7bbf(%rip), %rsi      # 0xfe40
pushq $0x1
popq %r14
xorl %r9d, %r9d
movl $0x0, 0x24(%rsp)
movl $0x0, 0x2c(%rsp)
xorl %r8d, %r8d
xorl %r10d, %r10d
movq $0x0, 0x38(%rsp)
cmpq $0x10, %r15
jb 0x855b
movq %r12, %rbp
movl (%r12), %r13d
cmpq $0x10, %r13
jb 0x855b
cmpq %r13, %r15
jb 0x855b
movzwl 0x4(%rbp), %eax
cmpl $0x2, %eax
je 0x82de
cmpl $0x3, %eax
jne 0x82e9
jmp 0x855b
movl 0x10(%rbp), %edi
testl %edi, %edi
jne 0x8585
leal 0x3(%r13), %r12d
andl $-0x4, %r12d
subq %r12, %r15
addq %rbp, %r12
movb 0x14(%rbp), %al
addb $-0x41, %al
cmpb $0x3, %al
ja 0x82a7
leaq 0x1c(%rbp), %r14
addl $-0x1c, %r13d
movl %ebx, 0x30(%rsp)
cmpl $0x4, %r13d
jl 0x83c6
movzwl (%r14), %eax
cmpl $0x4, %eax
jb 0x83c6
cmpl %eax, %r13d
jb 0x83c6
movzwl 0x2(%r14), %edx
leal -0x1(%rdx), %ecx
cmpl $0xe, %ecx
ja 0x834b
movslq (%rsi,%rcx,4), %rcx
addq %rsi, %rcx
jmpq *%rcx
movl 0x4(%r14), %r9d
movb $0x2, %r10b
jmp 0x83b5
xorl %edi, %edi
leaq 0x8046(%rip), %rsi      # 0x1039a
xorl %eax, %eax
movl %r8d, %ebx
movl %r9d, 0x34(%rsp)
movq %r10, 0x70(%rsp)
callq 0xcb1c
movq 0x70(%rsp), %r10
movl 0x34(%rsp), %r9d
movl %ebx, %r8d
leaq 0x7ac4(%rip), %rsi      # 0xfe40
movl 0x30(%rsp), %ebx
movzwl (%r14), %eax
jmp 0x83b5
movl 0x4(%r14), %ecx
movl %ecx, 0x2c(%rsp)
jmp 0x83b5
movl 0x4(%r14), %ecx
movl %ecx, 0x24(%rsp)
jmp 0x83b5
movl 0x4(%r14), %r8d
jmp 0x83b5
movl 0x4(%r14), %r9d
xorl %r10d, %r10d
jmp 0x83b5
leaq 0x4(%r14), %rcx
movq %rcx, 0x38(%rsp)
movb $0x1, %r10b
addl $0x3, %eax
andl $-0x4, %eax
subl %eax, %r13d
addq %rax, %r14
jmp 0x830c
cmpb $0x0, 0x28(%rsp)
je 0x842d
movq %rsi, %r13
cmpb $0x2, %r10b
pushq $0x1
popq %r14
je 0x8498
movzbl %r10b, %eax
cmpl $0x1, %eax
je 0x845d
testl %eax, %eax
movq %r13, %rsi
jne 0x82a7
movzbl 0x12(%rbp), %esi
movsbl 0x14(%rbp), %edx
movl $0x1, (%rsp)
movl %r9d, %edi
movl %r8d, %ecx
movl %r8d, 0x20(%rsp)
xorl %r8d, %r8d
movl %r9d, %ebp
xorl %r9d, %r9d
callq 0x7a5a
xorl %r10d, %r10d
movl %ebp, %r9d
movl 0x20(%rsp), %r8d
movq %r13, %rsi
jmp 0x82a7
movzbl 0x17(%rbp), %eax
cmpl $0x1, %eax
pushq $0x1
popq %r14
movl %r8d, 0x20(%rsp)
je 0x84e3
cmpl $0x7, %eax
je 0x84d3
cmpl $0x9, %eax
jne 0x84e8
movb $0x1, %r13b
jmp 0x84eb
movzbl 0x11(%rbp), %esi
movsbl 0x14(%rbp), %edx
movl $0x1, (%rsp)
movl %r9d, %edi
movl %r8d, %ecx
movl %r8d, 0x20(%rsp)
xorl %r8d, %r8d
movl %r9d, %ebp
movl %r14d, %r9d
callq 0x7a5a
movl %ebp, %r9d
movl 0x20(%rsp), %r8d
movb $0x1, %r10b
movq %r13, %rsi
jmp 0x82a7
movsbl 0x14(%rbp), %edx
movl $0x1, (%rsp)
xorl %edi, %edi
xorl %esi, %esi
movl %r8d, %ecx
movl %r8d, 0x20(%rsp)
movq 0x38(%rsp), %r8
movl %r9d, %ebp
pushq $0x2
popq %r9
callq 0x7a5a
movl %ebp, %r9d
movl 0x20(%rsp), %r8d
movb $0x2, %r10b
movq %r13, %rsi
jmp 0x82a7
movb $0x2, %r13b
xorl %r9d, %r9d
movl $0x0, 0x24(%rsp)
jmp 0x84eb
xorl %r13d, %r13d
jmp 0x84eb
movb $0x3, %r13b
movzbl 0x11(%rbp), %esi
movzbl 0x14(%rbp), %eax
movzbl %r13b, %ecx
movl %ecx, 0x8(%rsp)
movl %eax, (%rsp)
movl $0x1, 0x10(%rsp)
movl %r9d, %edi
movl 0x24(%rsp), %edx
xorl %ecx, %ecx
movl 0x2c(%rsp), %r8d
movl %r9d, %ebp
leaq 0x7828(%rip), %r9       # 0xfd46
callq 0x745c
movq %r13, %r10
movl %ebp, %r9d
leaq 0x7910(%rip), %rsi      # 0xfe40
movl 0x20(%rsp), %r8d
jmp 0x82a7
callq 0x2070
movl (%rax), %edi
callq 0x2480
leaq 0x7d70(%rip), %rsi      # 0x102bd
xorl %edi, %edi
movq %rax, %rdx
xorl %eax, %eax
callq 0xcb1c
jmp 0x85c0
movl %ebx, %edi
callq 0x21f0
jmp 0x85c4
callq 0x2070
movl (%rax), %edi
callq 0x2480
leaq 0x7d8e(%rip), %rsi      # 0x10305
xorl %edi, %edi
movq %rax, %rdx
xorl %eax, %eax
callq 0xcb1c
jmp 0x85b9
cmpb $0x0, 0x28(%rsp)
leaq 0x7dfb(%rip), %rax      # 0x1038c
leaq 0x7de6(%rip), %r14      # 0x1037e
cmoveq %rax, %r14
negl %edi
callq 0x2480
leaq 0x7db7(%rip), %rsi      # 0x10361
xorl %edi, %edi
movq %r14, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0xcb1c
movl %ebx, %edi
callq 0x21f0
pushq $-0x1
popq %r14
movl %r14d, %eax
addq $0x20c8, %rsp           # imm = 0x20C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)