// File: /workspace/github_c_makefile_star2_O2/open-mesh-mirror[P]batmand/posix/init.c
// Function: activate_interface
static void activate_interface(struct batman_if *batman_if)
{
	struct ifreq int_req;
	int on = 1, sock_opts;

	if ( ( batman_if->udp_recv_sock = socket( PF_INET, SOCK_DGRAM, 0 ) ) < 0 ) {

		debug_output(3, "Error - can't create receive socket: %s\n", strerror(errno) );
		goto error;

	}

	memset( &int_req, 0, sizeof (struct ifreq) );
	strncpy( int_req.ifr_name, batman_if->dev, IFNAMSIZ - 1 );

	if ( ioctl( batman_if->udp_recv_sock, SIOCGIFADDR, &int_req ) < 0 ) {

		debug_output(3, "Error - can't get IP address of interface %s: %s\n", batman_if->dev, strerror(errno) );
		goto error;

	}

	batman_if->addr.sin_family = AF_INET;
	batman_if->addr.sin_port = htons(PORT);
	batman_if->addr.sin_addr.s_addr = ((struct sockaddr_in *)&int_req.ifr_addr)->sin_addr.s_addr;

	if (batman_if->addr.sin_addr.s_addr == 0) {

		debug_output(3, "Error - invalid ip address detected (0.0.0.0): %s\n", batman_if->dev);
		goto error;

	}

	if ( ioctl( batman_if->udp_recv_sock, SIOCGIFBRDADDR, &int_req ) < 0 ) {

		debug_output(3, "Error - can't get broadcast IP address of interface %s: %s\n", batman_if->dev, strerror(errno) );
		goto error;

	}

	batman_if->broad.sin_family = AF_INET;
	batman_if->broad.sin_port = htons(PORT);
	batman_if->broad.sin_addr.s_addr = ((struct sockaddr_in *)&int_req.ifr_broadaddr)->sin_addr.s_addr;

	if ( batman_if->broad.sin_addr.s_addr == 0 ) {

		debug_output(3, "Error - invalid broadcast address detected (0.0.0.0): %s\n", batman_if->dev );
		goto error;

	}


#ifdef __linux__
	/* The SIOCGIFINDEX ioctl is Linux specific, but I am not yet sure if the
	* equivalent exists on *BSD. There is a function called if_nametoindex()
	* on both Linux and BSD.
	* Maybe it does the same as this code and we can simply call it instead?
	* --stsp
	*/
	if ( ioctl( batman_if->udp_recv_sock, SIOCGIFINDEX, &int_req ) < 0 ) {

		debug_output(3, "Error - can't get index of interface %s: %s\n", batman_if->dev, strerror(errno) );
		goto error;

	}

	batman_if->if_index = int_req.ifr_ifindex;
#else
	batman_if->if_index = 0;
#endif
	batman_if->wifi_if = is_wifi_interface(batman_if->dev, batman_if->udp_recv_sock);

	if (ioctl(batman_if->udp_recv_sock, SIOCGIFNETMASK, &int_req) < 0) {

		debug_output(3, "Error - can't get netmask address of interface %s: %s\n", batman_if->dev, strerror(errno));
		goto error;

	}

	batman_if->netaddr = (((struct sockaddr_in *)&int_req.ifr_addr)->sin_addr.s_addr & batman_if->addr.sin_addr.s_addr);
	batman_if->netmask = bit_count(((struct sockaddr_in *)&int_req.ifr_addr)->sin_addr.s_addr);

	add_del_rule(batman_if->netaddr, batman_if->netmask, BATMAN_RT_TABLE_HOSTS, BATMAN_RT_PRIO_DEFAULT + batman_if->if_num, 0, RULE_TYPE_DST, RULE_ADD);
	add_del_rule(batman_if->netaddr, batman_if->netmask, BATMAN_RT_TABLE_UNREACH, BATMAN_RT_PRIO_UNREACH + batman_if->if_num, 0, RULE_TYPE_DST, RULE_ADD);

	if ( ( batman_if->udp_send_sock = socket( PF_INET, SOCK_DGRAM, 0 ) ) < 0 ) {

		debug_output(3, "Error - can't create send socket: %s\n", strerror(errno) );
		goto error;

	}

	if ( setsockopt( batman_if->udp_send_sock, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on) ) < 0 ) {

		debug_output(3, "Error - can't enable broadcasts: %s\n", strerror(errno) );
		goto error;

	}

	if ( bind_to_iface( batman_if->udp_send_sock, batman_if->dev ) < 0 ) {

		debug_output(3, "Cannot bind socket to device %s : %s \n", batman_if->dev, strerror(errno));
		goto error;

	}

	if ( bind( batman_if->udp_send_sock, (struct sockaddr *)&batman_if->addr, sizeof(struct sockaddr_in) ) < 0 ) {

		debug_output(3, "Error - can't bind send socket: %s\n", strerror(errno) );
		goto error;

	}

	/* make udp socket non blocking */
	sock_opts = fcntl(batman_if->udp_send_sock, F_GETFL, 0);
	fcntl(batman_if->udp_send_sock, F_SETFL, sock_opts | O_NONBLOCK);

	if ( bind_to_iface( batman_if->udp_recv_sock, batman_if->dev ) < 0 ) {

		debug_output(3, "Cannot bind socket to device %s : %s \n", batman_if->dev, strerror(errno));
		goto error;

	}

	if ( bind( batman_if->udp_recv_sock, (struct sockaddr *)&batman_if->broad, sizeof(struct sockaddr_in) ) < 0 ) {

		debug_output(3, "Error - can't bind receive socket: %s\n", strerror(errno));
		goto error;

	}

	batman_if->out.orig = batman_if->addr.sin_addr.s_addr;
	batman_if->out.prev_sender = batman_if->addr.sin_addr.s_addr;

	batman_if->if_active = 1;
	active_ifs++;

	interface_listen_sockets();
	debug_output(3, "Interface activated: %s\n", batman_if->dev);

	batman_if->if_rp_filter_old = get_rp_filter(batman_if->dev);
	set_rp_filter(0, batman_if->dev);

	batman_if->if_send_redirects_old = get_send_redirects(batman_if->dev);
	set_send_redirects(0, batman_if->dev);

	return;

error:
	deactivate_interface(batman_if);
}

// Assembly:
pushq %rbp
pushq %r14
pushq %rbx
subq $0x40, %rsp
movq %rdi, %rbx
movl $0x1, 0x14(%rsp)
pushq $0x2
popq %rdi
movl %edi, %esi
xorl %edx, %edx
callq 0x24e0
movl %eax, 0x14(%rbx)
testl %eax, %eax
js 0xa807
movl %eax, %ebp
xorps %xmm0, %xmm0
leaq 0x18(%rsp), %r14
movups %xmm0, 0x18(%r14)
movups %xmm0, 0x8(%r14)
movq 0x8(%rbx), %rsi
pushq $0xf
popq %rdx
movq %r14, %rdi
callq 0x2090
movl $0x8915, %esi           # imm = 0x8915
movl %ebp, %edi
movq %r14, %rdx
xorl %eax, %eax
callq 0x21d0
testl %eax, %eax
js 0xa822
movl $0xd1100002, 0x30(%rbx) # imm = 0xD1100002
movl 0x2c(%rsp), %eax
movl %eax, 0x34(%rbx)
testl %eax, %eax
je 0xa83e
movl 0x14(%rbx), %edi
leaq 0x18(%rsp), %rdx
movl $0x8919, %esi           # imm = 0x8919
xorl %eax, %eax
callq 0x21d0
testl %eax, %eax
js 0xa84b
movl $0xd1100002, 0x40(%rbx) # imm = 0xD1100002
movl 0x2c(%rsp), %eax
movl %eax, 0x44(%rbx)
testl %eax, %eax
je 0xa867
movl 0x14(%rbx), %edi
leaq 0x18(%rsp), %rdx
movl $0x8933, %esi           # imm = 0x8933
xorl %eax, %eax
callq 0x21d0
testl %eax, %eax
js 0xa881
leaq 0x18(%rsp), %r14
movl 0x10(%r14), %eax
movl %eax, 0x20(%rbx)
movq 0x8(%rbx), %rdi
movl 0x14(%rbx), %esi
callq 0x8f58
movb %al, 0x55(%rbx)
movl 0x14(%rbx), %edi
movl $0x891b, %esi           # imm = 0x891B
movq %r14, %rdx
xorl %eax, %eax
callq 0x21d0
testl %eax, %eax
js 0xa89a
movl 0x2c(%rsp), %edi
movl 0x34(%rbx), %eax
andl %edi, %eax
movl %eax, 0x50(%rbx)
callq 0x5ed6
movb %al, 0x54(%rbx)
movl 0x50(%rbx), %edi
movzbl 0x1c(%rbx), %ecx
addl $0x19c8, %ecx           # imm = 0x19C8
andl $0x0, (%rsp)
movzbl %al, %esi
pushq $0x42
popq %rdx
pushq $0x1
popq %rbp
xorl %r8d, %r8d
movl %ebp, %r9d
callq 0x7a5a
movl 0x50(%rbx), %edi
movzbl 0x54(%rbx), %esi
movzbl 0x1c(%rbx), %ecx
addl $0x1a2c, %ecx           # imm = 0x1A2C
andl $0x0, (%rsp)
pushq $0x43
popq %rdx
xorl %r8d, %r8d
movl %ebp, %r9d
callq 0x7a5a
pushq $0x2
popq %rdi
movl %edi, %esi
xorl %edx, %edx
callq 0x24e0
movl %eax, 0x10(%rbx)
testl %eax, %eax
js 0xa8b3
pushq $0x1
popq %rsi
pushq $0x6
popq %rdx
leaq 0x14(%rsp), %rcx
pushq $0x4
popq %r8
movl %eax, %edi
callq 0x20d0
testl %eax, %eax
js 0xa8cb
movl 0x10(%rbx), %edi
movq 0x8(%rbx), %rsi
callq 0x8f0b
testb %al, %al
js 0xa8e3
leaq 0x30(%rbx), %rsi
movl 0x10(%rbx), %edi
pushq $0x10
popq %rdx
callq 0x2370
testl %eax, %eax
js 0xa91b
movl 0x10(%rbx), %edi
pushq $0x3
popq %rsi
xorl %edx, %edx
xorl %eax, %eax
callq 0x20e0
movl 0x10(%rbx), %edi
orl $0x800, %eax            # imm = 0x800
pushq $0x4
popq %rsi
movl %eax, %edx
xorl %eax, %eax
callq 0x20e0
movl 0x14(%rbx), %edi
movq 0x8(%rbx), %rsi
callq 0x8f0b
testb %al, %al
js 0xa8e3
leaq 0x40(%rbx), %rsi
movl 0x14(%rbx), %edi
pushq $0x10
popq %rdx
callq 0x2370
testl %eax, %eax
js 0xa933
movl 0x34(%rbx), %eax
movl %eax, 0x5e(%rbx)
movl %eax, 0x62(%rbx)
movb $0x1, 0x1d(%rbx)
leaq 0xcb2d(%rip), %rax      # 0x172e5
incb (%rax)
callq 0xa3d2
movq 0x8(%rbx), %rdx
leaq 0x6b54(%rip), %rsi      # 0x1131e
pushq $0x3
popq %rdi
xorl %eax, %eax
callq 0xcb1c
movq 0x8(%rbx), %rdi
callq 0x8d7a
movb %al, 0x24(%rbx)
movq 0x8(%rbx), %rsi
xorl %edi, %edi
callq 0x8cbc
movq 0x8(%rbx), %rdi
callq 0x8ea1
movb %al, 0x25(%rbx)
movq 0x8(%rbx), %rsi
xorl %edi, %edi
callq 0x8e27
jmp 0xa912
callq 0x2070
movl (%rax), %edi
callq 0x2480
leaq 0x68df(%rip), %rsi      # 0x110f9
pushq $0x3
popq %rdi
movq %rax, %rdx
jmp 0xa875
movq 0x8(%rbx), %r14
callq 0x2070
movl (%rax), %edi
callq 0x2480
leaq 0x68e9(%rip), %rsi      # 0x11122
jmp 0xa8fa
movq 0x8(%rbx), %rdx
leaq 0x690b(%rip), %rsi      # 0x11154
jmp 0xa872
movq 0x8(%rbx), %r14
callq 0x2070
movl (%rax), %edi
callq 0x2480
leaq 0x6925(%rip), %rsi      # 0x11187
jmp 0xa8fa
movq 0x8(%rbx), %rdx
leaq 0x6951(%rip), %rsi      # 0x111c3
pushq $0x3
popq %rdi
xorl %eax, %eax
callq 0xcb1c
jmp 0xa90a
movq 0x8(%rbx), %r14
callq 0x2070
movl (%rax), %edi
callq 0x2480
leaq 0x6965(%rip), %rsi      # 0x111fd
jmp 0xa8fa
movq 0x8(%rbx), %r14
callq 0x2070
movl (%rax), %edi
callq 0x2480
leaq 0x6979(%rip), %rsi      # 0x1122a
jmp 0xa8fa
callq 0x2070
movl (%rax), %edi
callq 0x2480
leaq 0x699b(%rip), %rsi      # 0x11261
jmp 0xa81a
callq 0x2070
movl (%rax), %edi
callq 0x2480
leaq 0x69a9(%rip), %rsi      # 0x11287
jmp 0xa81a
movq 0x8(%rbx), %r14
callq 0x2070
movl (%rax), %edi
callq 0x2480
leaq 0x69b2(%rip), %rsi      # 0x112ac
pushq $0x3
popq %rdi
movq %r14, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0xcb1c
movq %rbx, %rdi
callq 0xa44a
addq $0x40, %rsp
popq %rbx
popq %r14
popq %rbp
retq
callq 0x2070
movl (%rax), %edi
callq 0x2480
leaq 0x69a5(%rip), %rsi      # 0x112d3
jmp 0xa81a
callq 0x2070
movl (%rax), %edi
callq 0x2480
leaq 0x69b1(%rip), %rsi      # 0x112f7
jmp 0xa81a