// File: /workspace/github_c_makefile_star2_O1/open-mesh-mirror[P]batmand/linux/route.c
// Function: add_del_route
void add_del_route(uint32_t dest, uint8_t netmask, uint32_t router, uint32_t src_ip, int32_t ifi, const char *dev, uint8_t rt_table, int8_t route_type, int8_t route_action)
{
	int netlink_sock;
	size_t len;
	uint32_t my_router;
	char buf[4096], str1[16], str2[16], str3[16];
	struct rtattr *rta;
	struct sockaddr_nl nladdr;
	struct iovec iov;
	struct msghdr msg;
	struct nlmsghdr *nh;
	struct req_s {
		struct rtmsg rtm;
		char buff[4 * (sizeof(struct rtattr) + 4)];
	} *req;
	char req_buf[NLMSG_LENGTH(sizeof(struct req_s))] ALIGN_WORD;

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

	inet_ntop(AF_INET, &dest, str1, sizeof(str1));
	inet_ntop(AF_INET, &router, str2, sizeof(str2));
	inet_ntop(AF_INET, &src_ip, str3, sizeof(str3));

	if (policy_routing_script != NULL) {
		dprintf(policy_routing_pipe, "ROUTE %s %s %s %i %s %s %i %s %i\n", (route_action == ROUTE_DEL ? "del" : "add"), route_type_to_string_script[route_type], str1, netmask, str2, str3, ifi, dev, rt_table);
		return;
	}

	/* adding default route */
	if ((router == dest) && (dest == 0)) {

		if (route_type != ROUTE_TYPE_UNREACHABLE) {
			debug_output(3, "%s default route via %s (table %i)\n", (route_action == ROUTE_DEL ? "Deleting" : "Adding"), dev, rt_table);
			debug_output(4, "%s default route via %s (table %i)\n", (route_action == ROUTE_DEL ? "Deleting" : "Adding"), dev, rt_table);
		}

		my_router = router;

	/* single hop neigbor */
	} else if ((router == dest) && (dest != 0)) {

		debug_output(3, "%s route to %s via 0.0.0.0 (table %i - %s)\n", (route_action == ROUTE_DEL ? "Deleting" : "Adding"), str1, rt_table, dev);
		debug_output(4, "%s route to %s via 0.0.0.0 (table %i - %s)\n", (route_action == ROUTE_DEL ? "Deleting" : "Adding"), str1, rt_table, dev);
		my_router = 0;

	/* multihop neighbor */
	} else {

		debug_output(3, "%s %s to %s/%i via %s (table %i - %s)\n", (route_action == ROUTE_DEL ? "Deleting" : "Adding"), route_type_to_string[route_type], str1, netmask, str2, rt_table, dev);
		debug_output(4, "%s %s to %s/%i via %s (table %i - %s)\n", (route_action == ROUTE_DEL ? "Deleting" : "Adding"), route_type_to_string[route_type], str1, netmask, str2, rt_table, dev);
		my_router = router;

	}


	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;

	len = sizeof(struct rtmsg) + sizeof(struct rtattr) + 4;

	/* additional information for the outgoing interface and the gateway */
	if (route_type == ROUTE_TYPE_UNICAST)
		len += 2 * (sizeof(struct rtattr) + 4);

	if (src_ip != 0)
		len += sizeof(struct rtattr) + 4;

	nh->nlmsg_len = NLMSG_LENGTH(len);
	nh->nlmsg_pid = getpid();
	req->rtm.rtm_family = AF_INET;
	req->rtm.rtm_table = rt_table;
	req->rtm.rtm_dst_len = netmask;

	if (route_action == ROUTE_DEL) {

		nh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
		nh->nlmsg_type = RTM_DELROUTE;
		req->rtm.rtm_scope = RT_SCOPE_NOWHERE;

	} else {

		nh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | NLM_F_CREATE | NLM_F_APPEND;
		nh->nlmsg_type = RTM_NEWROUTE;

		if (route_type == ROUTE_TYPE_UNICAST && my_router == 0 && src_ip != 0)
			req->rtm.rtm_scope = RT_SCOPE_LINK;
		else
			req->rtm.rtm_scope = RT_SCOPE_UNIVERSE;

		req->rtm.rtm_protocol = RTPROT_STATIC; /* may be changed to some batman specific value - see <linux/rtnetlink.h> */

		switch(route_type) {
		case ROUTE_TYPE_UNICAST:
			req->rtm.rtm_type = RTN_UNICAST;
			break;
		case ROUTE_TYPE_THROW:
			req->rtm.rtm_type = RTN_THROW;
			break;
		case ROUTE_TYPE_UNREACHABLE:
			req->rtm.rtm_type = RTN_UNREACHABLE;
			break;
		default:
			debug_output(0, "Error - unknown route type (add_del_route): %i\n", route_type);
			return;
		}
	}

	rta = (struct rtattr *)req->buff;
	rta->rta_type = RTA_DST;
	rta->rta_len = sizeof(struct rtattr) + 4;
	memcpy(((char *)req->buff) + sizeof(struct rtattr), (char *)&dest, 4);

	if (route_type == ROUTE_TYPE_UNICAST) {

		rta = (struct rtattr *)(req->buff + sizeof(struct rtattr) + 4);
		rta->rta_type = RTA_GATEWAY;
		rta->rta_len = sizeof(struct rtattr) + 4;
		memcpy(((char *)req->buff) + 2 * sizeof(struct rtattr) + 4, (char *)&my_router, 4);

		rta = (struct rtattr *)(req->buff + 2 * sizeof(struct rtattr) + 8);
		rta->rta_type = RTA_OIF;
		rta->rta_len = sizeof(struct rtattr) + 4;
		memcpy(((char *)req->buff) + 3 * sizeof(struct rtattr) + 8, (char *)&ifi, 4);

		if (src_ip != 0) {
			rta = (struct rtattr *)(req->buff + 3 * sizeof(struct rtattr) + 12);
			rta->rta_type = RTA_PREFSRC;
			rta->rta_len = sizeof(struct rtattr) + 4;
			memcpy(((char *)req->buff) + 4 * sizeof(struct rtattr) + 12, (char *)&src_ip, 4);
		}

	}

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

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

	}

	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 routing table manipulation: %s\n", strerror(errno));
		close(netlink_sock);
		return;

	}


	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 %s %s to %s/%i via %s (table %i): %s\n", (route_action == ROUTE_DEL ? "delete" : "add"), route_type_to_string[route_type], str1, netmask, str2, rt_table, strerror(-((struct nlmsgerr*)NLMSG_DATA(nh))->error));

		nh = NLMSG_NEXT(nh, len);

	}

	close(netlink_sock);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1108, %rsp           # imm = 0x1108
movq %r9, %rbx
movl %r8d, %r15d
movl %esi, %r14d
leaq 0xc(%rsp), %rsi
movl %edi, (%rsi)
leaq 0x8(%rsp), %r12
movl %edx, (%r12)
leaq 0x4(%rsp), %r13
movl %ecx, (%r13)
leaq 0x100(%rsp), %rbp
movq %rbp, 0xa0(%rsp)
movq $0x1000, 0xa8(%rsp) # imm = 0x1000
leaq 0x20(%rsp), %rdx
movl $0x2, %edi
movl $0x10, %ecx
callq 0x2030
leaq 0x80(%rsp), %rdx
movl $0x2, %edi
movq %r12, %rsi
movl $0x10, %ecx
callq 0x2030
leaq 0xf0(%rsp), %r12
movl $0x2, %edi
movq %r13, %rsi
movq %r12, %rdx
movl $0x10, %ecx
callq 0x2030
leaq 0xf887(%rip), %rax      # 0x172d8
cmpq $0x0, (%rax)
je 0x7acf
leaq 0xf882(%rip), %rax      # 0x172e0
movl (%rax), %edi
cmpb $0x1, 0x1150(%rsp)
leaq 0x93cc(%rip), %rax      # 0x10e3b
leaq 0x93c9(%rip), %rdx      # 0x10e3f
cmoveq %rax, %rdx
movsbq 0x1148(%rsp), %rax
leaq 0x9362(%rip), %rsi      # 0x10dec
movslq (%rsi,%rax,4), %rcx
addq %rsi, %rcx
movzbl %r14b, %r9d
movzbl 0x1140(%rsp), %r10d
subq $0x8, %rsp
leaq 0x9370(%rip), %rsi      # 0x10e19
leaq 0x28(%rsp), %r8
xorl %eax, %eax
pushq %r10
pushq %rbx
pushq %r15
pushq %r12
leaq 0xa8(%rsp), %r10
pushq %r10
callq 0x2200
addq $0x30, %rsp
jmp 0x7f67
movq %r15, 0x18(%rsp)
movl %r14d, 0x10(%rsp)
movl 0x8(%rsp), %ecx
movl 0xc(%rsp), %eax
movl %ecx, %edx
orl %eax, %edx
jne 0x7b60
cmpb $0x2, 0x1148(%rsp)
je 0x7b4e
movq %rbx, %r14
cmpb $0x1, 0x1150(%rsp)
leaq 0x9364(%rip), %rax      # 0x10e67
leaq 0x9366(%rip), %r12      # 0x10e70
cmoveq %rax, %r12
movzbl 0x1140(%rsp), %r13d
leaq 0x9325(%rip), %rbx      # 0x10e43
movl $0x3, %edi
movq %rbx, %rsi
movq %r12, %rdx
movq %r14, %rcx
movl %r13d, %r8d
xorl %eax, %eax
callq 0xd5b0
movl $0x4, %edi
movq %rbx, %rsi
movq %r12, %rdx
movq %r14, %rcx
movl %r13d, %r8d
xorl %eax, %eax
callq 0xd5b0
movl 0x8(%rsp), %r13d
movb 0x1140(%rsp), %r14b
jmp 0x7c83
cmpl %eax, %ecx
setne %cl
testl %eax, %eax
sete %al
cmpb $0x1, 0x1150(%rsp)
leaq 0x92ee(%rip), %rdx      # 0x10e67
leaq 0x92f0(%rip), %r12      # 0x10e70
cmoveq %rdx, %r12
orb %cl, %al
movq %rbx, %r15
jne 0x7be1
movb 0x1140(%rsp), %r14b
movzbl %r14b, %ebx
leaq 0x92d9(%rip), %rsi      # 0x10e77
xorl %r13d, %r13d
leaq 0x20(%rsp), %rcx
movl $0x3, %edi
movq %r12, %rdx
movl %ebx, %r8d
movq %r15, %r9
xorl %eax, %eax
callq 0xd5b0
movl $0x4, %edi
leaq 0x92b0(%rip), %rsi      # 0x10e77
movq %r12, %rdx
leaq 0x20(%rsp), %rcx
movl %ebx, %r8d
movq %r15, %r9
xorl %eax, %eax
callq 0xd5b0
jmp 0x7c83
movsbq 0x1148(%rsp), %rax
leaq 0xf0ef(%rip), %rcx      # 0x16ce0
movq (%rcx,%rax,8), %rbx
movzbl 0x10(%rsp), %r13d
movb 0x1140(%rsp), %r14b
movzbl %r14b, %r10d
movq %r10, 0x98(%rsp)
subq $0x8, %rsp
leaq 0x9289(%rip), %rsi      # 0x10ea3
leaq 0x28(%rsp), %r8
movl $0x3, %edi
movq %r12, %rdx
movq %rbx, %rcx
movl %r13d, %r9d
xorl %eax, %eax
pushq %r15
pushq %r10
leaq 0x98(%rsp), %r10
pushq %r10
callq 0xd5b0
addq $0x18, %rsp
movl $0x4, %edi
leaq 0x9251(%rip), %rsi      # 0x10ea3
movq %r12, %rdx
movq %rbx, %rcx
leaq 0x28(%rsp), %r8
movl %r13d, %r9d
xorl %eax, %eax
pushq %r15
pushq 0xa8(%rsp)
leaq 0x98(%rsp), %r10
pushq %r10
callq 0xd5b0
addq $0x20, %rsp
movl 0x8(%rsp), %r13d
xorl %ebx, %ebx
movq %rbx, 0x70(%rsp)
movl $0x0, 0x78(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
movaps %xmm0, 0x40(%rsp)
movaps %xmm0, 0x50(%rsp)
movups %xmm0, 0x5c(%rsp)
movaps %xmm0, 0xb0(%rsp)
movaps %xmm0, 0xc0(%rsp)
movaps %xmm0, 0xd0(%rsp)
movq %rbx, 0xe0(%rsp)
xorl %eax, %eax
movb 0x1148(%rsp), %r15b
testb %r15b, %r15b
sete %al
shll $0x4, %eax
xorl %ecx, %ecx
cmpl $0x0, 0x4(%rsp)
movw $0x10, 0x70(%rsp)
setne %cl
leal (%rax,%rcx,8), %eax
addl $0x24, %eax
movl %eax, 0x30(%rsp)
callq 0x2100
movl %eax, 0x3c(%rsp)
movb $0x2, 0x40(%rsp)
movb %r14b, 0x44(%rsp)
movl 0x10(%rsp), %r12d
movb %r12b, 0x41(%rsp)
cmpb $0x1, 0x1150(%rsp)
jne 0x7ed8
movl $0x50019, 0x34(%rsp)    # imm = 0x50019
movb $-0x1, 0x46(%rsp)
movl $0x10008, 0x4c(%rsp)    # imm = 0x10008
movl 0xc(%rsp), %eax
movl %eax, 0x50(%rsp)
testb %r15b, %r15b
movq 0x18(%rsp), %rax
jne 0x7d76
movl $0x50008, 0x54(%rsp)    # imm = 0x50008
movl %r13d, 0x58(%rsp)
movl $0x40008, 0x5c(%rsp)    # imm = 0x40008
movl %eax, 0x60(%rsp)
movl 0x4(%rsp), %eax
testl %eax, %eax
je 0x7d76
movl $0x70008, 0x64(%rsp)    # imm = 0x70008
movl %eax, 0x68(%rsp)
movl $0x10, %edi
movl $0x2, %esi
xorl %edx, %edx
callq 0x24e0
testl %eax, %eax
js 0x7f1f
movl %eax, %r14d
leaq 0x30(%rsp), %rsi
movl (%rsi), %edx
leaq 0x70(%rsp), %r15
movl %eax, %edi
xorl %ecx, %ecx
movq %r15, %r8
movl $0xc, %r9d
callq 0x21e0
testq %rax, %rax
js 0x7f40
leaq 0xb0(%rsp), %rsi
movq %r15, (%rsi)
movl $0xc, 0x8(%rsi)
leaq 0xa0(%rsp), %rax
movq %rax, 0x10(%rsi)
movq $0x1, 0x18(%rsi)
movq $0x0, 0x20(%rsi)
movl %r14d, %edi
xorl %edx, %edx
callq 0x2360
cmpq $0x10, %rax
jb 0x7f5f
movq %rax, %r15
cmpb $0x1, 0x1150(%rsp)
leaq 0x91c5(%rip), %rax      # 0x10fd2
movl %r12d, %ecx
leaq 0x9028(%rip), %r12      # 0x10e3f
cmoveq %rax, %r12
movsbq 0x1148(%rsp), %rax
movq %rax, 0x18(%rsp)
movzbl %cl, %r13d
movzbl 0x1140(%rsp), %eax
movq %rax, 0x10(%rsp)
movl (%rbp), %eax
cmpq $0x10, %rax
jb 0x7f5f
cmpq %rax, %r15
jb 0x7f5f
movzwl 0x4(%rbp), %eax
cmpl $0x2, %eax
je 0x7e63
cmpl $0x3, %eax
jne 0x7eba
jmp 0x7f5f
movl 0x10(%rbp), %edi
testl %edi, %edi
je 0x7eba
movq 0x18(%rsp), %rax
leaq 0xee6a(%rip), %rcx      # 0x16ce0
movq (%rcx,%rax,8), %rbx
negl %edi
callq 0x2480
movq %rax, %r10
subq $0x8, %rsp
xorl %edi, %edi
leaq 0x910d(%rip), %rsi      # 0x10f9e
movq %r12, %rdx
movq %rbx, %rcx
leaq 0x28(%rsp), %r8
movl %r13d, %r9d
xorl %eax, %eax
pushq %r10
pushq 0x20(%rsp)
leaq 0x98(%rsp), %r10
pushq %r10
callq 0xd5b0
addq $0x20, %rsp
movl (%rbp), %eax
addl $0x3, %eax
andl $-0x4, %eax
subq %rax, %r15
addq %rax, %rbp
cmpq $0xf, %r15
ja 0x7e3a
jmp 0x7f5f
movl 0x4(%rsp), %eax
testl %eax, %eax
movl $0xfd, %ecx
cmovel %eax, %ecx
testl %r13d, %r13d
cmovnel %ebx, %ecx
movsbl %r15b, %edx
testb %r15b, %r15b
cmovnel %ebx, %ecx
movl $0xc050018, 0x34(%rsp)  # imm = 0xC050018
movb %cl, 0x46(%rsp)
movb $0x4, 0x45(%rsp)
cmpl $0x2, %edx
je 0x7f83
cmpl $0x1, %edx
je 0x7f79
testl %edx, %edx
jne 0x7f8d
movb $0x1, 0x47(%rsp)
jmp 0x7d2f
callq 0x2070
movl (%rax), %edi
callq 0x2480
leaq 0x8fc8(%rip), %rsi      # 0x10efa
xorl %edi, %edi
movq %rax, %rdx
xorl %eax, %eax
callq 0xd5b0
jmp 0x7f67
callq 0x2070
movl (%rax), %edi
callq 0x2480
leaq 0x8fef(%rip), %rsi      # 0x10f42
xorl %edi, %edi
movq %rax, %rdx
xorl %eax, %eax
callq 0xd5b0
movl %r14d, %edi
callq 0x21f0
addq $0x1108, %rsp           # imm = 0x1108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movb $0x9, 0x47(%rsp)
jmp 0x7d2f
movb $0x7, 0x47(%rsp)
jmp 0x7d2f
leaq 0x8f36(%rip), %rsi      # 0x10eca
xorl %edi, %edi
xorl %eax, %eax
callq 0xd5b0
jmp 0x7f67