// File: /workspace/github_c_makefile_star2_O1/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 %r15
pushq %r14
pushq %rbx
subq $0x38, %rsp
movq %rdi, %rbx
movl $0x1, 0xc(%rsp)
movl $0x2, %edi
movl $0x2, %esi
xorl %edx, %edx
callq 0x24e0
movl %eax, 0x14(%rbx)
testl %eax, %eax
js 0xaf08
movl %eax, %ebp
xorps %xmm0, %xmm0
leaq 0x10(%rsp), %r14
movaps %xmm0, 0x10(%r14)
movaps %xmm0, (%r14)
movq $0x0, 0x20(%r14)
movq 0x8(%rbx), %rsi
movl $0xf, %edx
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 0xaf25
movl $0xd1100002, 0x30(%rbx) # imm = 0xD1100002
movl 0x24(%rsp), %eax
movl %eax, 0x34(%rbx)
testl %eax, %eax
je 0xaf41
movl 0x14(%rbx), %edi
leaq 0x10(%rsp), %rdx
movl $0x8919, %esi           # imm = 0x8919
xorl %eax, %eax
callq 0x21d0
testl %eax, %eax
js 0xaf4e
movl $0xd1100002, 0x40(%rbx) # imm = 0xD1100002
movl 0x24(%rsp), %eax
movl %eax, 0x44(%rbx)
testl %eax, %eax
je 0xaf6a
movl 0x14(%rbx), %edi
leaq 0x10(%rsp), %rdx
movl $0x8933, %esi           # imm = 0x8933
xorl %eax, %eax
callq 0x21d0
testl %eax, %eax
js 0xaf86
leaq 0x10(%rsp), %r14
movl 0x10(%r14), %eax
movl %eax, 0x20(%rbx)
movq 0x8(%rbx), %rdi
movl 0x14(%rbx), %esi
callq 0x955a
movb %al, 0x55(%rbx)
movl 0x14(%rbx), %edi
xorl %r15d, %r15d
movl $0x891b, %esi           # imm = 0x891B
movq %r14, %rdx
xorl %eax, %eax
callq 0x21d0
testl %eax, %eax
js 0xaf9f
movl 0x24(%rsp), %edi
movl 0x34(%rbx), %eax
andl %edi, %eax
movl %eax, 0x50(%rbx)
callq 0x633c
movb %al, 0x54(%rbx)
movl 0x50(%rbx), %edi
movzbl 0x1c(%rbx), %ecx
addl $0x19c8, %ecx           # imm = 0x19C8
movl %r15d, (%rsp)
movzbl %al, %esi
movl $0x42, %edx
xorl %r8d, %r8d
movl $0x1, %r9d
callq 0x7f9f
movl 0x50(%rbx), %edi
movzbl 0x54(%rbx), %esi
movzbl 0x1c(%rbx), %ecx
addl $0x1a2c, %ecx           # imm = 0x1A2C
movl %r15d, (%rsp)
movl $0x43, %edx
xorl %r8d, %r8d
movl $0x1, %r9d
callq 0x7f9f
movl $0x2, %edi
movl $0x2, %esi
xorl %edx, %edx
callq 0x24e0
movl %eax, 0x10(%rbx)
testl %eax, %eax
js 0xafb8
leaq 0xc(%rsp), %rcx
movl %eax, %edi
movl $0x1, %esi
movl $0x6, %edx
movl $0x4, %r8d
callq 0x20d0
testl %eax, %eax
js 0xafd0
movl 0x10(%rbx), %edi
movq 0x8(%rbx), %rsi
callq 0x9507
testb %al, %al
js 0xafe8
leaq 0x30(%rbx), %rsi
movl 0x10(%rbx), %edi
movl $0x10, %edx
callq 0x2370
testl %eax, %eax
js 0xb024
movl 0x10(%rbx), %edi
movl $0x3, %esi
xorl %edx, %edx
xorl %eax, %eax
callq 0x20e0
movl 0x10(%rbx), %edi
orl $0x800, %eax            # imm = 0x800
movl $0x4, %esi
movl %eax, %edx
xorl %eax, %eax
callq 0x20e0
movl 0x14(%rbx), %edi
movq 0x8(%rbx), %rsi
callq 0x9507
testb %al, %al
js 0xafe8
leaq 0x40(%rbx), %rsi
movl 0x14(%rbx), %edi
movl $0x10, %edx
callq 0x2370
testl %eax, %eax
js 0xb03c
movl 0x34(%rbx), %eax
movl %eax, 0x5e(%rbx)
movl %eax, 0x62(%rbx)
movb $0x1, 0x1d(%rbx)
leaq 0xc42e(%rip), %rax      # 0x172e5
incb (%rax)
callq 0xaa6b
movq 0x8(%rbx), %rdx
leaq 0x73d1(%rip), %rsi      # 0x1229a
movl $0x3, %edi
xorl %eax, %eax
callq 0xd5b0
movq 0x8(%rbx), %rdi
callq 0x9370
movb %al, 0x24(%rbx)
movq 0x8(%rbx), %rsi
xorl %edi, %edi
callq 0x92b0
movq 0x8(%rbx), %rdi
callq 0x949b
movb %al, 0x25(%rbx)
movq 0x8(%rbx), %rsi
xorl %edi, %edi
callq 0x941f
jmp 0xb019
callq 0x2070
movl (%rax), %edi
callq 0x2480
leaq 0x715a(%rip), %rsi      # 0x12075
movl $0x3, %edi
movq %rax, %rdx
jmp 0xaf7a
movq 0x8(%rbx), %r14
callq 0x2070
movl (%rax), %edi
callq 0x2480
leaq 0x7162(%rip), %rsi      # 0x1209e
jmp 0xafff
movq 0x8(%rbx), %rdx
leaq 0x7184(%rip), %rsi      # 0x120d0
jmp 0xaf75
movq 0x8(%rbx), %r14
callq 0x2070
movl (%rax), %edi
callq 0x2480
leaq 0x719e(%rip), %rsi      # 0x12103
jmp 0xafff
movq 0x8(%rbx), %rdx
leaq 0x71ca(%rip), %rsi      # 0x1213f
movl $0x3, %edi
xorl %eax, %eax
callq 0xd5b0
jmp 0xb011
movq 0x8(%rbx), %r14
callq 0x2070
movl (%rax), %edi
callq 0x2480
leaq 0x71dc(%rip), %rsi      # 0x12179
jmp 0xafff
movq 0x8(%rbx), %r14
callq 0x2070
movl (%rax), %edi
callq 0x2480
leaq 0x71f0(%rip), %rsi      # 0x121a6
jmp 0xafff
callq 0x2070
movl (%rax), %edi
callq 0x2480
leaq 0x7212(%rip), %rsi      # 0x121dd
jmp 0xaf1b
callq 0x2070
movl (%rax), %edi
callq 0x2480
leaq 0x7220(%rip), %rsi      # 0x12203
jmp 0xaf1b
movq 0x8(%rbx), %r14
callq 0x2070
movl (%rax), %edi
callq 0x2480
leaq 0x7229(%rip), %rsi      # 0x12228
movl $0x3, %edi
movq %r14, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0xd5b0
movq %rbx, %rdi
callq 0xaafa
addq $0x38, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
callq 0x2070
movl (%rax), %edi
callq 0x2480
leaq 0x7218(%rip), %rsi      # 0x1224f
jmp 0xaf1b
callq 0x2070
movl (%rax), %edi
callq 0x2480
leaq 0x7224(%rip), %rsi      # 0x12273
jmp 0xaf1b