// File: /workspace/github_c_makefile_star2_O2/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, 0x18(%rsp)
movq %r8, 0x8(%rsp)
movl %esi, %r15d
leaq 0x14(%rsp), %rsi
movl %edi, (%rsi)
leaq 0x4(%rsp), %r12
movl %edx, (%r12)
movq %rsp, %r13
movl %ecx, (%r13)
leaq 0x100(%rsp), %rbp
movq %rbp, 0xa0(%rsp)
movq $0x1000, 0xa8(%rsp) # imm = 0x1000
pushq $0x2
popq %rbx
leaq 0x20(%rsp), %rdx
pushq $0x10
popq %r14
movl %ebx, %edi
movl %r14d, %ecx
callq 0x2030
leaq 0x80(%rsp), %rdx
movl %ebx, %edi
movq %r12, %rsi
movl %r14d, %ecx
callq 0x2030
leaq 0xf0(%rsp), %r12
movl %ebx, %edi
movq %r13, %rsi
movq %r12, %rdx
movl %r14d, %ecx
callq 0x2030
leaq 0xfde1(%rip), %rax      # 0x172d8
cmpq $0x0, (%rax)
je 0x757a
leaq 0xfddc(%rip), %rax      # 0x172e0
movl (%rax), %edi
cmpb $0x1, 0x1150(%rsp)
leaq 0x89b6(%rip), %rax      # 0xfecb
leaq 0x89b3(%rip), %rdx      # 0xfecf
cmoveq %rax, %rdx
movsbq 0x1148(%rsp), %rax
leaq 0x894c(%rip), %rsi      # 0xfe7c
movslq (%rsi,%rax,4), %rcx
addq %rsi, %rcx
movzbl %r15b, %r9d
movzbl 0x1140(%rsp), %r10d
subq $0x8, %rsp
leaq 0x895a(%rip), %rsi      # 0xfea9
leaq 0x28(%rsp), %r8
xorl %eax, %eax
pushq %r10
pushq 0x28(%rsp)
pushq 0x20(%rsp)
pushq %r12
leaq 0xa8(%rsp), %r10
pushq %r10
callq 0x2200
addq $0x30, %rsp
jmp 0x7a34
movq 0x18(%rsp), %r10
movl 0x4(%rsp), %r14d
movl 0x14(%rsp), %eax
movl %r14d, %ecx
orl %eax, %ecx
jne 0x7614
movb 0x1148(%rsp), %r13b
cmpb $0x2, %r13b
movl %r15d, %r12d
movb 0x1140(%rsp), %r15b
je 0x7756
movq %r10, %rcx
cmpb $0x1, 0x1150(%rsp)
leaq 0x8935(%rip), %rax      # 0xfef7
leaq 0x8937(%rip), %rbx      # 0xff00
cmoveq %rax, %rbx
movzbl %r15b, %r14d
leaq 0x88fb(%rip), %rsi      # 0xfed3
pushq $0x3
popq %rdi
movq %rbx, %rdx
movl %r14d, %r8d
xorl %eax, %eax
movq %r10, %r13
callq 0xcb1c
pushq $0x4
popq %rdi
leaq 0x88de(%rip), %rsi      # 0xfed3
movb 0x1148(%rsp), %al
movq %rbx, %rdx
movq %r13, %rcx
movl %eax, %r13d
movl %r14d, %r8d
xorl %eax, %eax
callq 0xcb1c
jmp 0x7751
cmpl %eax, %r14d
setne %cl
testl %eax, %eax
sete %al
cmpb $0x1, 0x1150(%rsp)
leaq 0x88c9(%rip), %rdx      # 0xfef7
leaq 0x88cb(%rip), %r12      # 0xff00
cmoveq %rdx, %r12
orb %cl, %al
jne 0x76a3
movb 0x1140(%rsp), %al
movzbl %al, %ebx
leaq 0x88b9(%rip), %rsi      # 0xff07
xorl %r14d, %r14d
pushq $0x3
popq %rdi
leaq 0x20(%rsp), %rcx
movq %r12, %rdx
movl %ebx, %r8d
movq %r10, %r13
movq %r10, %r9
xorl %eax, %eax
callq 0xcb1c
pushq $0x4
popq %rdi
leaq 0x8891(%rip), %rsi      # 0xff07
movq %r12, %rdx
leaq 0x20(%rsp), %rcx
movl %ebx, %r8d
movq %r13, %r9
xorl %eax, %eax
callq 0xcb1c
movl %r15d, %r12d
movb 0x1140(%rsp), %r15b
movb 0x1148(%rsp), %r13b
jmp 0x7756
movb 0x1148(%rsp), %al
movsbq %al, %rax
leaq 0xf61b(%rip), %rcx      # 0x16cd0
movq (%rcx,%rax,8), %rbx
movzbl %r15b, %r13d
movb 0x1140(%rsp), %al
movzbl %al, %r11d
movq %r11, 0x98(%rsp)
subq $0x8, %rsp
leaq 0x8858(%rip), %rsi      # 0xff33
pushq $0x3
popq %rdi
leaq 0x28(%rsp), %r8
movq %r12, %rdx
movq %rbx, %rcx
movl %r13d, %r9d
xorl %eax, %eax
movq %r10, %r14
pushq %r10
pushq %r11
leaq 0x98(%rsp), %r10
pushq %r10
callq 0xcb1c
addq $0x18, %rsp
pushq $0x4
popq %rdi
leaq 0x8821(%rip), %rsi      # 0xff33
movq %r12, %rdx
movq %rbx, %rcx
leaq 0x28(%rsp), %r8
movl %r13d, %r9d
movb 0x1150(%rsp), %r13b
movl %r15d, %r12d
xorl %eax, %eax
pushq %r14
pushq 0xa8(%rsp)
leaq 0x98(%rsp), %r10
pushq %r10
movb 0x1160(%rsp), %r15b
callq 0xcb1c
addq $0x20, %rsp
movl 0x4(%rsp), %r14d
andq $0x0, 0x70(%rsp)
andl $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)
andq $0x0, 0xe0(%rsp)
movaps %xmm0, 0xd0(%rsp)
xorl %eax, %eax
testb %r13b, %r13b
sete %al
shll $0x4, %eax
xorl %ecx, %ecx
cmpl $0x0, (%rsp)
movw $0x10, 0x70(%rsp)
setne %cl
leal (%rax,%rcx,8), %ebx
addl $0x24, %ebx
movl %ebx, 0x30(%rsp)
callq 0x2100
movl %eax, 0x3c(%rsp)
movb $0x2, 0x40(%rsp)
movb %r15b, 0x44(%rsp)
movb %r12b, 0x41(%rsp)
movl %r13d, %r15d
movb 0x1150(%rsp), %r13b
cmpb $0x1, %r13b
jne 0x77f6
movl $0x50019, 0x34(%rsp)    # imm = 0x50019
movb $-0x1, 0x46(%rsp)
jmp 0x7826
movl $0xc050018, 0x34(%rsp)  # imm = 0xC050018
testb %r15b, %r15b
jne 0x79de
testl %r14d, %r14d
jne 0x79de
cmpl $0x0, (%rsp)
je 0x79de
movw $0xfd04, 0x45(%rsp)     # imm = 0xFD04
movb $0x1, 0x47(%rsp)
movl $0x10008, 0x4c(%rsp)    # imm = 0x10008
movl 0x14(%rsp), %eax
movl %eax, 0x50(%rsp)
testb %r15b, %r15b
movq 0x8(%rsp), %rax
jne 0x786c
movl $0x50008, 0x54(%rsp)    # imm = 0x50008
movl %r14d, 0x58(%rsp)
movl $0x40008, 0x5c(%rsp)    # imm = 0x40008
movl %eax, 0x60(%rsp)
movl (%rsp), %eax
testl %eax, %eax
je 0x786c
movl $0x70008, 0x64(%rsp)    # imm = 0x70008
movl %eax, 0x68(%rsp)
pushq $0x10
popq %rdi
pushq $0x2
popq %rsi
xorl %edx, %edx
callq 0x24e0
testl %eax, %eax
js 0x79bd
movl %eax, %r14d
movl %r15d, %r13d
movl %r12d, 0x8(%rsp)
movl %ebx, %edx
leaq 0x30(%rsp), %rsi
leaq 0x70(%rsp), %r15
pushq $0xc
popq %r9
movl %eax, %edi
xorl %ecx, %ecx
movq %r15, %r8
callq 0x21e0
testq %rax, %rax
js 0x7a0d
leaq 0xb0(%rsp), %rsi
movq %r15, (%rsi)
movl $0xc, 0x8(%rsi)
leaq 0xa0(%rsp), %rax
movq %rax, 0x10(%rsi)
movq $0x1, 0x18(%rsi)
andq $0x0, 0x20(%rsi)
movl %r14d, %edi
xorl %edx, %edx
callq 0x2360
movq %rax, %r15
cmpb $0x1, 0x1150(%rsp)
leaq 0x876a(%rip), %rax      # 0x10062
leaq 0x85d0(%rip), %r12      # 0xfecf
cmoveq %rax, %r12
movsbq %r13b, %rax
movq %rax, 0x18(%rsp)
movzbl 0x8(%rsp), %r13d
movzbl 0x1140(%rsp), %eax
movq %rax, 0x8(%rsp)
cmpq $0x10, %r15
jb 0x7a2c
movl (%rbp), %eax
cmpq $0x10, %rax
jb 0x7a2c
cmpq %rax, %r15
jb 0x7a2c
movzwl 0x4(%rbp), %ecx
cmpl $0x2, %ecx
je 0x7952
cmpl $0x3, %ecx
jne 0x79ac
jmp 0x7a2c
movl 0x10(%rbp), %edi
testl %edi, %edi
je 0x79ac
movq 0x18(%rsp), %rax
leaq 0xf36b(%rip), %rcx      # 0x16cd0
movq (%rcx,%rax,8), %rbx
negl %edi
callq 0x2480
movq %rax, %r10
subq $0x8, %rsp
xorl %edi, %edi
leaq 0x86ae(%rip), %rsi      # 0x1002e
movq %r12, %rdx
movq %rbx, %rcx
leaq 0x28(%rsp), %r8
movl %r13d, %r9d
xorl %eax, %eax
pushq %r10
pushq 0x18(%rsp)
leaq 0x98(%rsp), %r10
pushq %r10
callq 0xcb1c
addq $0x20, %rsp
movl (%rbp), %eax
addl $0x3, %eax
andl $-0x4, %eax
subq %rax, %r15
addq %rax, %rbp
jmp 0x791f
callq 0x2070
movl (%rax), %edi
callq 0x2480
leaq 0x85ba(%rip), %rsi      # 0xff8a
xorl %edi, %edi
movq %rax, %rdx
xorl %eax, %eax
callq 0xcb1c
jmp 0x7a34
movsbl %r15b, %edx
movw $0x4, 0x45(%rsp)
cmpl $0x2, %edx
je 0x7a50
cmpl $0x1, %edx
je 0x7a46
testl %edx, %edx
je 0x7821
leaq 0x8558(%rip), %rsi      # 0xff5a
xorl %edi, %edi
xorl %eax, %eax
callq 0xcb1c
jmp 0x7a34
callq 0x2070
movl (%rax), %edi
callq 0x2480
leaq 0x85b2(%rip), %rsi      # 0xffd2
xorl %edi, %edi
movq %rax, %rdx
xorl %eax, %eax
callq 0xcb1c
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 0x7826
movb $0x7, 0x47(%rsp)
jmp 0x7826