// File: /workspace/github_c_makefile_star2_O3/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 0xb1f9
movl %eax, %ebp
xorps %xmm0, %xmm0
leaq 0x10(%rsp), %r14
movups %xmm0, 0x18(%r14)
movups %xmm0, 0x8(%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 0xb216
movl $0xd1100002, 0x30(%rbx) # imm = 0xD1100002
movl 0x24(%rsp), %eax
movl %eax, 0x34(%rbx)
testl %eax, %eax
je 0xb232
movl 0x14(%rbx), %edi
leaq 0x10(%rsp), %rdx
movl $0x8919, %esi           # imm = 0x8919
xorl %eax, %eax
callq 0x21d0
testl %eax, %eax
js 0xb23f
movl $0xd1100002, 0x40(%rbx) # imm = 0xD1100002
movl 0x24(%rsp), %eax
movl %eax, 0x44(%rbx)
testl %eax, %eax
je 0xb25b
movl 0x14(%rbx), %edi
leaq 0x10(%rsp), %rdx
movl $0x8933, %esi           # imm = 0x8933
xorl %eax, %eax
callq 0x21d0
testl %eax, %eax
js 0xb277
leaq 0x10(%rsp), %r14
movl 0x10(%r14), %eax
movl %eax, 0x20(%rbx)
movq 0x8(%rbx), %rdi
movl 0x14(%rbx), %esi
callq 0x98b1
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 0xb290
movl 0x24(%rsp), %edi
movl 0x34(%rbx), %eax
andl %edi, %eax
movl %eax, 0x50(%rbx)
callq 0x6663
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 0x82f9
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 0x82f9
movl $0x2, %edi
movl $0x2, %esi
xorl %edx, %edx
callq 0x24e0
movl %eax, 0x10(%rbx)
testl %eax, %eax
js 0xb2a9
leaq 0xc(%rsp), %rcx
movl %eax, %edi
movl $0x1, %esi
movl $0x6, %edx
movl $0x4, %r8d
callq 0x20d0
testl %eax, %eax
js 0xb2c1
movl 0x10(%rbx), %edi
movq 0x8(%rbx), %rsi
callq 0x985e
testb %al, %al
js 0xb2d9
leaq 0x30(%rbx), %rsi
movl 0x10(%rbx), %edi
movl $0x10, %edx
callq 0x2370
testl %eax, %eax
js 0xb315
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 0x985e
testb %al, %al
js 0xb2d9
leaq 0x40(%rbx), %rsi
movl 0x14(%rbx), %edi
movl $0x10, %edx
callq 0x2370
testl %eax, %eax
js 0xb32d
movl 0x34(%rbx), %eax
movl %eax, 0x5e(%rbx)
movl %eax, 0x62(%rbx)
movb $0x1, 0x1d(%rbx)
leaq 0xc13d(%rip), %rax      # 0x172e5
incb (%rax)
callq 0xad6d
movq 0x8(%rbx), %rdx
leaq 0x7184(%rip), %rsi      # 0x1233e
movl $0x3, %edi
xorl %eax, %eax
callq 0xd88c
movq 0x8(%rbx), %rdi
callq 0x96c4
movb %al, 0x24(%rbx)
movq 0x8(%rbx), %rsi
xorl %edi, %edi
callq 0x9604
movq 0x8(%rbx), %rdi
callq 0x97f2
movb %al, 0x25(%rbx)
movq 0x8(%rbx), %rsi
xorl %edi, %edi
callq 0x9776
jmp 0xb30a
callq 0x2070
movl (%rax), %edi
callq 0x2480
leaq 0x6f0d(%rip), %rsi      # 0x12119
movl $0x3, %edi
movq %rax, %rdx
jmp 0xb26b
movq 0x8(%rbx), %r14
callq 0x2070
movl (%rax), %edi
callq 0x2480
leaq 0x6f15(%rip), %rsi      # 0x12142
jmp 0xb2f0
movq 0x8(%rbx), %rdx
leaq 0x6f37(%rip), %rsi      # 0x12174
jmp 0xb266
movq 0x8(%rbx), %r14
callq 0x2070
movl (%rax), %edi
callq 0x2480
leaq 0x6f51(%rip), %rsi      # 0x121a7
jmp 0xb2f0
movq 0x8(%rbx), %rdx
leaq 0x6f7d(%rip), %rsi      # 0x121e3
movl $0x3, %edi
xorl %eax, %eax
callq 0xd88c
jmp 0xb302
movq 0x8(%rbx), %r14
callq 0x2070
movl (%rax), %edi
callq 0x2480
leaq 0x6f8f(%rip), %rsi      # 0x1221d
jmp 0xb2f0
movq 0x8(%rbx), %r14
callq 0x2070
movl (%rax), %edi
callq 0x2480
leaq 0x6fa3(%rip), %rsi      # 0x1224a
jmp 0xb2f0
callq 0x2070
movl (%rax), %edi
callq 0x2480
leaq 0x6fc5(%rip), %rsi      # 0x12281
jmp 0xb20c
callq 0x2070
movl (%rax), %edi
callq 0x2480
leaq 0x6fd3(%rip), %rsi      # 0x122a7
jmp 0xb20c
movq 0x8(%rbx), %r14
callq 0x2070
movl (%rax), %edi
callq 0x2480
leaq 0x6fdc(%rip), %rsi      # 0x122cc
movl $0x3, %edi
movq %r14, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0xd88c
movq %rbx, %rdi
callq 0xadf9
addq $0x38, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
callq 0x2070
movl (%rax), %edi
callq 0x2480
leaq 0x6fcb(%rip), %rsi      # 0x122f3
jmp 0xb20c
callq 0x2070
movl (%rax), %edi
callq 0x2480
leaq 0x6fd7(%rip), %rsi      # 0x12317
jmp 0xb20c